Settings: unify duplicated code
[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{
16ae10ac
LP
44 Q_DECLARE_TR_FUNCTIONS(LXQt::MessageBox);
45
4ef6cee9 46public:
47 explicit MessageBox(QWidget *parent = 0): QMessageBox(parent) {}
48
e6173915 49 static QWidget *parentWidget()
4ef6cee9 50 {
e6173915
AS
51 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());
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 {
f05ba5af 72 QMessageBox::warning(parentWidget(), tr("LXQt Power Manager Error"), tr("Hibernate failed."));
4ef6cee9 73 }
74
75
76protected:
77 virtual void resizeEvent(QResizeEvent* event)
78 {
79 QRect screen = QApplication::desktop()->screenGeometry();
80 move((screen.width() - this->width()) / 2,
81 (screen.height() - this->height()) / 2);
82
83 }
84};
85
3ecf643a
CS
86PowerManager::PowerManager(QObject * parent, bool skipWarning)
87 : QObject(parent),
88 m_skipWarning(skipWarning)
4ef6cee9 89{
8e6db58a 90 m_power = new Power(this);
c7351c0e 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&)));
f767d6ae
PL
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;
4ef6cee9 99}
100
101PowerManager::~PowerManager()
102{
c7351c0e 103// delete m_power;
4ef6cee9 104}
105
106QList<QAction*> PowerManager::availableActions()
107{
108 QList<QAction*> ret;
109 QAction * act;
110
111 // TODO/FIXME: icons
c7351c0e 112 if (m_power->canHibernate())
b051638b 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 }
4ef6cee9 118
c7351c0e 119 if (m_power->canSuspend())
b051638b 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 }
4ef6cee9 125
c7351c0e 126 if (m_power->canReboot())
b051638b 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
c7351c0e 133 if (m_power->canShutdown())
b051638b 134 {
135 act = new QAction(XdgIcon::fromTheme("system-shutdown"), tr("Shutdown"), this);
c7351c0e 136 connect(act, SIGNAL(triggered()), this, SLOT(shutdown()));
b051638b 137 ret.append(act);
138 }
4ef6cee9 139
c7351c0e 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 }
4ef6cee9 146
147 return ret;
148}
f767d6ae 149
4ef6cee9 150
4ef6cee9 151void PowerManager::suspend()
152{
3ecf643a 153 if (m_skipWarning ||
f05ba5af 154 MessageBox::question(tr("LXQt Session Suspend"),
3ecf643a 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.")))
4ef6cee9 156 {
e6173915 157 m_power->suspend();
4ef6cee9 158 }
4ef6cee9 159}
160
161void PowerManager::hibernate()
162{
3ecf643a 163 if (m_skipWarning ||
f05ba5af 164 MessageBox::question(tr("LXQt Session Hibernate"),
e6173915 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.")))
4ef6cee9 166 {
e6173915 167 m_power->hibernate();
4ef6cee9 168 }
4ef6cee9 169}
170
171void PowerManager::reboot()
172{
3ecf643a 173 if (m_skipWarning ||
f05ba5af 174 MessageBox::question(tr("LXQt Session Reboot"),
e6173915 175 tr("Do you want to really restart your computer? All unsaved work will be lost...")))
4ef6cee9 176 {
e6173915 177 m_power->reboot();
4ef6cee9 178 }
4ef6cee9 179}
180
c7351c0e 181void PowerManager::shutdown()
4ef6cee9 182{
3ecf643a 183 if (m_skipWarning ||
f05ba5af 184 MessageBox::question(tr("LXQt Session Shutdown"),
e6173915 185 tr("Do you want to really switch off your computer? All unsaved work will be lost...")))
4ef6cee9 186 {
e6173915 187 m_power->shutdown();
4ef6cee9 188 }
4ef6cee9 189}
190
191void PowerManager::logout()
192{
3ecf643a 193 if (m_skipWarning ||
f05ba5af 194 MessageBox::question(tr("LXQt Session Logout"),
e6173915 195 tr("Do you want to really logout? All unsaved work will be lost...")))
4ef6cee9 196 {
e6173915 197 m_power->logout();
4ef6cee9 198 }
4ef6cee9 199}
200
201void PowerManager::hibernateFailed()
202{
f05ba5af 203 MessageBox::warning(tr("LXQt Power Manager Error"), tr("Hibernate failed."));
4ef6cee9 204}
205
206void PowerManager::suspendFailed()
207{
f05ba5af 208 MessageBox::warning(tr("LXQt Power Manager Error"), tr("Suspend failed."));
4ef6cee9 209}
570d4cb9 210
f05ba5af 211} // namespace LXQt