RazorAutostartEntry => LxQt::AutostartEntry
[lxde/liblxqt.git] / powermanager.cpp
1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
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
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 "powermanager.h"
29 #include "lxqtpower/lxqtpower.h"
30 #include <qtxdg/xdgicon.h>
31 #include <QDBusInterface>
32 #include <QMessageBox>
33 #include <QApplication>
34 #include <QDesktopWidget>
35 #include <QtDebug>
36 #include "libtranslate.h"
37
38 using namespace LxQt;
39
40 class MessageBox: public QMessageBox
41 {
42 public:
43 explicit MessageBox(QWidget *parent = 0): QMessageBox(parent) {}
44
45 static QWidget *parentWidget()
46 {
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());
58 msgBox.setWindowTitle(title);
59 msgBox.setText(text);
60 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
61
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."));
69 }
70
71
72 protected:
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
82 PowerManager::PowerManager(QObject * parent, bool skipWarning)
83 : QObject(parent),
84 m_skipWarning(skipWarning)
85 {
86 libTranslate("librazorqt");
87 m_power = new Power(this);
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&)));
92 }
93
94 PowerManager::~PowerManager()
95 {
96 // delete m_power;
97 }
98
99 QList<QAction*> PowerManager::availableActions()
100 {
101 QList<QAction*> ret;
102 QAction * act;
103
104 // TODO/FIXME: icons
105 if (m_power->canHibernate())
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 }
111
112 if (m_power->canSuspend())
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 }
118
119 if (m_power->canReboot())
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
126 if (m_power->canShutdown())
127 {
128 act = new QAction(XdgIcon::fromTheme("system-shutdown"), tr("Shutdown"), this);
129 connect(act, SIGNAL(triggered()), this, SLOT(shutdown()));
130 ret.append(act);
131 }
132
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 }
139
140 return ret;
141 }
142
143
144 void PowerManager::suspend()
145 {
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.")))
149 {
150 m_power->suspend();
151 }
152 }
153
154 void PowerManager::hibernate()
155 {
156 if (m_skipWarning ||
157 MessageBox::question(tr("Razor Session Hibernate"),
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.")))
159 {
160 m_power->hibernate();
161 }
162 }
163
164 void PowerManager::reboot()
165 {
166 if (m_skipWarning ||
167 MessageBox::question(tr("Razor Session Reboot"),
168 tr("Do you want to really restart your computer? All unsaved work will be lost...")))
169 {
170 m_power->reboot();
171 }
172 }
173
174 void PowerManager::shutdown()
175 {
176 if (m_skipWarning ||
177 MessageBox::question(tr("Razor Session Shutdown"),
178 tr("Do you want to really switch off your computer? All unsaved work will be lost...")))
179 {
180 m_power->shutdown();
181 }
182 }
183
184 void PowerManager::logout()
185 {
186 if (m_skipWarning ||
187 MessageBox::question(tr("Razor Session Logout"),
188 tr("Do you want to really logout? All unsaved work will be lost...")))
189 {
190 m_power->logout();
191 }
192 }
193
194 void PowerManager::hibernateFailed()
195 {
196 MessageBox::warning(tr("Razor Power Manager Error"), tr("Hibernate failed."));
197 }
198
199 void PowerManager::suspendFailed()
200 {
201 MessageBox::warning(tr("Razor Power Manager Error"), tr("Suspend failed."));
202 }