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