Make lxqt globals a public header
[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 Q_UNUSED(title)
73 Q_UNUSED(text)
74 QMessageBox::warning(parentWidget(), tr("LXQt Power Manager Error"), tr("Hibernate failed."));
75 }
76
77
78 protected:
79 virtual void resizeEvent(QResizeEvent* event)
80 {
81 Q_UNUSED(event)
82 const QRect screen = QApplication::desktop()->screenGeometry();
83 move((screen.width() - this->width()) / 2,
84 (screen.height() - this->height()) / 2);
85
86 }
87 };
88
89 PowerManager::PowerManager(QObject * parent, bool skipWarning)
90 : QObject(parent),
91 m_skipWarning(skipWarning)
92 {
93 m_power = new Power(this);
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&)));
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;
102 }
103
104 PowerManager::~PowerManager()
105 {
106 // delete m_power;
107 }
108
109 QList<QAction*> PowerManager::availableActions()
110 {
111 QList<QAction*> ret;
112 QAction * act;
113
114 // TODO/FIXME: icons
115 if (m_power->canHibernate())
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 }
121
122 if (m_power->canSuspend())
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 }
128
129 if (m_power->canReboot())
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
136 if (m_power->canShutdown())
137 {
138 act = new QAction(XdgIcon::fromTheme("system-shutdown"), tr("Shutdown"), this);
139 connect(act, SIGNAL(triggered()), this, SLOT(shutdown()));
140 ret.append(act);
141 }
142
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 }
149
150 return ret;
151 }
152
153
154 void PowerManager::suspend()
155 {
156 if (m_skipWarning ||
157 MessageBox::question(tr("LXQt Session Suspend"),
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.")))
159 {
160 m_power->suspend();
161 }
162 }
163
164 void PowerManager::hibernate()
165 {
166 if (m_skipWarning ||
167 MessageBox::question(tr("LXQt Session Hibernate"),
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.")))
169 {
170 m_power->hibernate();
171 }
172 }
173
174 void PowerManager::reboot()
175 {
176 if (m_skipWarning ||
177 MessageBox::question(tr("LXQt Session Reboot"),
178 tr("Do you want to really restart your computer? All unsaved work will be lost...")))
179 {
180 m_power->reboot();
181 }
182 }
183
184 void PowerManager::shutdown()
185 {
186 if (m_skipWarning ||
187 MessageBox::question(tr("LXQt Session Shutdown"),
188 tr("Do you want to really switch off your computer? All unsaved work will be lost...")))
189 {
190 m_power->shutdown();
191 }
192 }
193
194 void PowerManager::logout()
195 {
196 if (m_skipWarning ||
197 MessageBox::question(tr("LXQt Session Logout"),
198 tr("Do you want to really logout? All unsaved work will be lost...")))
199 {
200 m_power->logout();
201 }
202 }
203
204 void PowerManager::hibernateFailed()
205 {
206 MessageBox::warning(tr("LXQt Power Manager Error"), tr("Hibernate failed."));
207 }
208
209 void PowerManager::suspendFailed()
210 {
211 MessageBox::warning(tr("LXQt Power Manager Error"), tr("Suspend failed."));
212 }
213
214 } // namespace LXQt