Suppress compiler warnings about unused function parameters
[lxde/liblxqt.git] / lxqtpowermanager.cpp
CommitLineData
4ef6cee9 1/* BEGIN_COMMON_COPYRIGHT_HEADER
c4af778e 2 * (c)LGPL2+
4ef6cee9 3 *
b9223fe7 4 * LXQt - a lightweight, Qt based, desktop toolset
4ef6cee9 5 * http://razor-qt.org
6 *
7 * Copyright: 2010-2011 Razor team
8 * Authors:
9 * Petr Vanek <petr@scribus.info>
10 *
11 * This program or library is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
c4af778e 14 * version 2.1 of the License, or (at your option) any later version.
4ef6cee9 15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20
21 * You should have received a copy of the GNU Lesser General
22 * Public License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301 USA
25 *
26 * END_COMMON_COPYRIGHT_HEADER */
27
b6483430 28#include "lxqtpowermanager.h"
8e6db58a 29#include "lxqtpower/lxqtpower.h"
4ef6cee9 30#include <QDBusInterface>
31#include <QMessageBox>
32#include <QApplication>
33#include <QDesktopWidget>
d3a23a9d 34#include <QtDebug>
949dba2f 35#include "lxqttranslator.h"
f9cba1ba 36#include "lxqtglobals.h"
f767d6ae 37#include "lxqtsettings.h"
e6389bfc 38#include <XdgIcon>
afd3b116 39
f05ba5af 40namespace LXQt {
8e6db58a 41
f9cba1ba 42class LXQT_API MessageBox: public QMessageBox
4ef6cee9 43{
603ca365 44 Q_DECLARE_TR_FUNCTIONS(LXQt::MessageBox)
16ae10ac 45
4ef6cee9 46public:
47 explicit MessageBox(QWidget *parent = 0): QMessageBox(parent) {}
48
e6173915 49 static QWidget *parentWidget()
4ef6cee9 50 {
d7858989 51 const QWidgetList widgets = QApplication::topLevelWidgets();
e6173915
AS
52
53 if (widgets.count())
54 return widgets.at(0);
55 else
56 return 0;
57 }
58
59 static bool question(const QString& title, const QString& text)
60 {
61 MessageBox msgBox(parentWidget());
4ef6cee9 62 msgBox.setWindowTitle(title);
63 msgBox.setText(text);
64 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
65
e6173915
AS
66 return (msgBox.exec() == QMessageBox::Yes);
67 }
68
69
70 static void warning(const QString& title, const QString& text)
71 {
7cce0c05
LP
72 Q_UNUSED(title)
73 Q_UNUSED(text)
f05ba5af 74 QMessageBox::warning(parentWidget(), tr("LXQt Power Manager Error"), tr("Hibernate failed."));
4ef6cee9 75 }
76
77
78protected:
79 virtual void resizeEvent(QResizeEvent* event)
80 {
7cce0c05 81 Q_UNUSED(event)
d7858989 82 const QRect screen = QApplication::desktop()->screenGeometry();
4ef6cee9 83 move((screen.width() - this->width()) / 2,
84 (screen.height() - this->height()) / 2);
85
86 }
87};
88
3ecf643a
CS
89PowerManager::PowerManager(QObject * parent, bool skipWarning)
90 : QObject(parent),
91 m_skipWarning(skipWarning)
4ef6cee9 92{
8e6db58a 93 m_power = new Power(this);
c7351c0e 94// connect(m_power, SIGNAL(suspendFail()), this, SLOT(suspendFailed()));
95// connect(m_power, SIGNAL(hibernateFail()), this, SLOT(hibernateFailed()));
96// connect(m_power, SIGNAL(monitoring(const QString &)),
97// this, SLOT(monitoring(const QString&)));
f767d6ae
PL
98
99 QString sessionConfig(getenv("LXQT_SESSION_CONFIG"));
100 Settings settings(sessionConfig.isEmpty() ? "session" : sessionConfig);
101 m_skipWarning = settings.value("leave_confirmation").toBool() ? false : true;
4ef6cee9 102}
103
104PowerManager::~PowerManager()
105{
c7351c0e 106// delete m_power;
4ef6cee9 107}
108
109QList<QAction*> PowerManager::availableActions()
110{
111 QList<QAction*> ret;
112 QAction * act;
113
114 // TODO/FIXME: icons
c7351c0e 115 if (m_power->canHibernate())
b051638b 116 {
117 act = new QAction(XdgIcon::fromTheme("system-suspend-hibernate"), tr("Hibernate"), this);
118 connect(act, SIGNAL(triggered()), this, SLOT(hibernate()));
119 ret.append(act);
120 }
4ef6cee9 121
c7351c0e 122 if (m_power->canSuspend())
b051638b 123 {
124 act = new QAction(XdgIcon::fromTheme("system-suspend"), tr("Suspend"), this);
125 connect(act, SIGNAL(triggered()), this, SLOT(suspend()));
126 ret.append(act);
127 }
4ef6cee9 128
c7351c0e 129 if (m_power->canReboot())
b051638b 130 {
131 act = new QAction(XdgIcon::fromTheme("system-reboot"), tr("Reboot"), this);
132 connect(act, SIGNAL(triggered()), this, SLOT(reboot()));
133 ret.append(act);
134 }
135
c7351c0e 136 if (m_power->canShutdown())
b051638b 137 {
138 act = new QAction(XdgIcon::fromTheme("system-shutdown"), tr("Shutdown"), this);
c7351c0e 139 connect(act, SIGNAL(triggered()), this, SLOT(shutdown()));
b051638b 140 ret.append(act);
141 }
4ef6cee9 142
c7351c0e 143 if (m_power->canLogout())
144 {
145 act = new QAction(XdgIcon::fromTheme("system-log-out"), tr("Logout"), this);
146 connect(act, SIGNAL(triggered()), this, SLOT(logout()));
147 ret.append(act);
148 }
4ef6cee9 149
150 return ret;
151}
f767d6ae 152
4ef6cee9 153
4ef6cee9 154void PowerManager::suspend()
155{
3ecf643a 156 if (m_skipWarning ||
f05ba5af 157 MessageBox::question(tr("LXQt Session Suspend"),
3ecf643a 158 tr("Do you want to really suspend your computer?<p>Suspends the computer into a low power state. System state is not preserved if the power is lost.")))
4ef6cee9 159 {
e6173915 160 m_power->suspend();
4ef6cee9 161 }
4ef6cee9 162}
163
164void PowerManager::hibernate()
165{
3ecf643a 166 if (m_skipWarning ||
f05ba5af 167 MessageBox::question(tr("LXQt Session Hibernate"),
e6173915 168 tr("Do you want to really hibernate your computer?<p>Hibernates the computer into a low power state. System state is preserved if the power is lost.")))
4ef6cee9 169 {
e6173915 170 m_power->hibernate();
4ef6cee9 171 }
4ef6cee9 172}
173
174void PowerManager::reboot()
175{
3ecf643a 176 if (m_skipWarning ||
f05ba5af 177 MessageBox::question(tr("LXQt Session Reboot"),
e6173915 178 tr("Do you want to really restart your computer? All unsaved work will be lost...")))
4ef6cee9 179 {
e6173915 180 m_power->reboot();
4ef6cee9 181 }
4ef6cee9 182}
183
c7351c0e 184void PowerManager::shutdown()
4ef6cee9 185{
3ecf643a 186 if (m_skipWarning ||
f05ba5af 187 MessageBox::question(tr("LXQt Session Shutdown"),
e6173915 188 tr("Do you want to really switch off your computer? All unsaved work will be lost...")))
4ef6cee9 189 {
e6173915 190 m_power->shutdown();
4ef6cee9 191 }
4ef6cee9 192}
193
194void PowerManager::logout()
195{
3ecf643a 196 if (m_skipWarning ||
f05ba5af 197 MessageBox::question(tr("LXQt Session Logout"),
e6173915 198 tr("Do you want to really logout? All unsaved work will be lost...")))
4ef6cee9 199 {
e6173915 200 m_power->logout();
4ef6cee9 201 }
4ef6cee9 202}
203
204void PowerManager::hibernateFailed()
205{
f05ba5af 206 MessageBox::warning(tr("LXQt Power Manager Error"), tr("Hibernate failed."));
4ef6cee9 207}
208
209void PowerManager::suspendFailed()
210{
f05ba5af 211 MessageBox::warning(tr("LXQt Power Manager Error"), tr("Suspend failed."));
4ef6cee9 212}
570d4cb9 213
f05ba5af 214} // namespace LXQt