f4f751e44615656b9eb583f10d81a6803a646271
[lxde/liblxqt.git] / lxqtpowermanager.cpp
1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * LXQt - a lightweight, Qt based, desktop toolset
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
14 * version 2.1 of the License, or (at your option) any later version.
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
28 #include "lxqtpowermanager.h"
29 #include "lxqtpower/lxqtpower.h"
30 #include <QDBusInterface>
31 #include <QMessageBox>
32 #include <QApplication>
33 #include <QDesktopWidget>
34 #include <QtDebug>
35 #include "lxqttranslator.h"
36 #include "lxqtglobals.h"
37 #include "lxqtsettings.h"
38 #include <XdgIcon>
39
40 namespace LXQt {
41
42 class LXQT_API MessageBox: public QMessageBox
43 {
44 Q_DECLARE_TR_FUNCTIONS(LXQt::MessageBox)
45
46 public:
47 explicit MessageBox(QWidget *parent = 0): QMessageBox(parent) {}
48
49 static QWidget *parentWidget()
50 {
51 const QWidgetList widgets = QApplication::topLevelWidgets();
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());
62 msgBox.setWindowTitle(title);
63 msgBox.setText(text);
64 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
65
66 return (msgBox.exec() == QMessageBox::Yes);
67 }
68
69
70 static void warning(const QString& title, const QString& text)
71 {
72 QMessageBox::warning(parentWidget(), tr("LXQt Power Manager Error"), tr("Hibernate failed."));
73 }
74
75
76 protected:
77 virtual void resizeEvent(QResizeEvent* event)
78 {
79 const QRect screen = QApplication::desktop()->screenGeometry();
80 move((screen.width() - this->width()) / 2,
81 (screen.height() - this->height()) / 2);
82
83 }
84 };
85
86 PowerManager::PowerManager(QObject * parent, bool skipWarning)
87 : QObject(parent),
88 m_skipWarning(skipWarning)
89 {
90 m_power = new Power(this);
91 // connect(m_power, SIGNAL(suspendFail()), this, SLOT(suspendFailed()));
92 // connect(m_power, SIGNAL(hibernateFail()), this, SLOT(hibernateFailed()));
93 // connect(m_power, SIGNAL(monitoring(const QString &)),
94 // this, SLOT(monitoring(const QString&)));
95
96 QString sessionConfig(getenv("LXQT_SESSION_CONFIG"));
97 Settings settings(sessionConfig.isEmpty() ? "session" : sessionConfig);
98 m_skipWarning = settings.value("leave_confirmation").toBool() ? false : true;
99 }
100
101 PowerManager::~PowerManager()
102 {
103 // delete m_power;
104 }
105
106 QList<QAction*> PowerManager::availableActions()
107 {
108 QList<QAction*> ret;
109 QAction * act;
110
111 // TODO/FIXME: icons
112 if (m_power->canHibernate())
113 {
114 act = new QAction(XdgIcon::fromTheme("system-suspend-hibernate"), tr("Hibernate"), this);
115 connect(act, SIGNAL(triggered()), this, SLOT(hibernate()));
116 ret.append(act);
117 }
118
119 if (m_power->canSuspend())
120 {
121 act = new QAction(XdgIcon::fromTheme("system-suspend"), tr("Suspend"), this);
122 connect(act, SIGNAL(triggered()), this, SLOT(suspend()));
123 ret.append(act);
124 }
125
126 if (m_power->canReboot())
127 {
128 act = new QAction(XdgIcon::fromTheme("system-reboot"), tr("Reboot"), this);
129 connect(act, SIGNAL(triggered()), this, SLOT(reboot()));
130 ret.append(act);
131 }
132
133 if (m_power->canShutdown())
134 {
135 act = new QAction(XdgIcon::fromTheme("system-shutdown"), tr("Shutdown"), this);
136 connect(act, SIGNAL(triggered()), this, SLOT(shutdown()));
137 ret.append(act);
138 }
139
140 if (m_power->canLogout())
141 {
142 act = new QAction(XdgIcon::fromTheme("system-log-out"), tr("Logout"), this);
143 connect(act, SIGNAL(triggered()), this, SLOT(logout()));
144 ret.append(act);
145 }
146
147 return ret;
148 }
149
150
151 void PowerManager::suspend()
152 {
153 if (m_skipWarning ||
154 MessageBox::question(tr("LXQt Session Suspend"),
155 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.")))
156 {
157 m_power->suspend();
158 }
159 }
160
161 void PowerManager::hibernate()
162 {
163 if (m_skipWarning ||
164 MessageBox::question(tr("LXQt Session Hibernate"),
165 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.")))
166 {
167 m_power->hibernate();
168 }
169 }
170
171 void PowerManager::reboot()
172 {
173 if (m_skipWarning ||
174 MessageBox::question(tr("LXQt Session Reboot"),
175 tr("Do you want to really restart your computer? All unsaved work will be lost...")))
176 {
177 m_power->reboot();
178 }
179 }
180
181 void PowerManager::shutdown()
182 {
183 if (m_skipWarning ||
184 MessageBox::question(tr("LXQt Session Shutdown"),
185 tr("Do you want to really switch off your computer? All unsaved work will be lost...")))
186 {
187 m_power->shutdown();
188 }
189 }
190
191 void PowerManager::logout()
192 {
193 if (m_skipWarning ||
194 MessageBox::question(tr("LXQt Session Logout"),
195 tr("Do you want to really logout? All unsaved work will be lost...")))
196 {
197 m_power->logout();
198 }
199 }
200
201 void PowerManager::hibernateFailed()
202 {
203 MessageBox::warning(tr("LXQt Power Manager Error"), tr("Hibernate failed."));
204 }
205
206 void PowerManager::suspendFailed()
207 {
208 MessageBox::warning(tr("LXQt Power Manager Error"), tr("Suspend failed."));
209 }
210
211 } // namespace LXQt