1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* LXQt - a lightweight, Qt based, desktop toolset
* https://lxqt.org
*
* Copyright: 2010-2011 Razor team
* Authors:
* Petr Vanek <petr@scribus.info>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "lxqtpowermanager.h"
#include "lxqtpower/lxqtpower.h"
#include <QDBusInterface>
#include <QMessageBox>
#include <QApplication>
#include <QtDebug>
#include <QScreen>
#include <QFile>
#include "lxqttranslator.h"
#include "lxqtglobals.h"
#include "lxqtsettings.h"
#include <XdgIcon>
namespace LXQt {
class LXQT_API MessageBox: public QMessageBox
{
Q_DECLARE_TR_FUNCTIONS(LXQt::MessageBox)
public:
explicit MessageBox(QWidget *parent = nullptr): QMessageBox(parent) {}
static QWidget *parentWidget()
{
const QWidgetList widgets = QApplication::topLevelWidgets();
if (widgets.count())
return widgets.at(0);
else
return nullptr;
}
static bool question(const QString& title, const QString& text)
{
MessageBox msgBox(parentWidget());
msgBox.setWindowTitle(title);
msgBox.setText(text);
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
return (msgBox.exec() == QMessageBox::Yes);
}
static void warning(const QString& title, const QString& text)
{
Q_UNUSED(title)
Q_UNUSED(text)
QMessageBox::warning(parentWidget(), tr("LXQt Power Manager Error"), tr("Hibernate failed."));
}
};
PowerManager::PowerManager(QObject * parent, bool skipWarning)
: QObject(parent),
m_skipWarning(skipWarning)
{
m_power = new Power(this);
// connect(m_power, SIGNAL(suspendFail()), this, SLOT(suspendFailed()));
// connect(m_power, SIGNAL(hibernateFail()), this, SLOT(hibernateFailed()));
// connect(m_power, SIGNAL(monitoring(const QString &)),
// this, SLOT(monitoring(const QString&)));
QString sessionConfig(QFile::decodeName(qgetenv("LXQT_SESSION_CONFIG")));
Settings settings(sessionConfig.isEmpty() ? QL1SV("session") : sessionConfig);
m_skipWarning = settings.value(QL1SV("leave_confirmation")).toBool() ? false : true;
}
PowerManager::~PowerManager()
{
// delete m_power;
}
QList<QAction*> PowerManager::availableActions()
{
QList<QAction*> ret;
QAction * act;
// TODO/FIXME: icons
if (m_power->canHibernate())
{
act = new QAction(XdgIcon::fromTheme(QL1SV("system-suspend-hibernate")), tr("Hibernate"), this);
connect(act, &QAction::triggered, this, &PowerManager::hibernate);
ret.append(act);
}
if (m_power->canSuspend())
{
act = new QAction(XdgIcon::fromTheme(QL1SV("system-suspend")), tr("Suspend"), this);
connect(act, &QAction::triggered, this, &PowerManager::suspend);
ret.append(act);
}
if (m_power->canReboot())
{
act = new QAction(XdgIcon::fromTheme(QL1SV("system-reboot")), tr("Reboot"), this);
connect(act, &QAction::triggered, this, &PowerManager::reboot);
ret.append(act);
}
if (m_power->canShutdown())
{
act = new QAction(XdgIcon::fromTheme(QL1SV("system-shutdown")), tr("Shutdown"), this);
connect(act, &QAction::triggered, this, &PowerManager::shutdown);
ret.append(act);
}
if (m_power->canLogout())
{
act = new QAction(XdgIcon::fromTheme(QL1SV("system-log-out")), tr("Logout"), this);
connect(act, &QAction::triggered, this, &PowerManager::logout);
ret.append(act);
}
return ret;
}
void PowerManager::suspend()
{
if (m_skipWarning ||
MessageBox::question(tr("LXQt Session Suspend"),
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.")))
{
m_power->suspend();
}
}
void PowerManager::hibernate()
{
if (m_skipWarning ||
MessageBox::question(tr("LXQt Session Hibernate"),
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.")))
{
m_power->hibernate();
}
}
void PowerManager::reboot()
{
if (m_skipWarning ||
MessageBox::question(tr("LXQt Session Reboot"),
tr("Do you want to really restart your computer? All unsaved work will be lost...")))
{
m_power->reboot();
}
}
void PowerManager::shutdown()
{
if (m_skipWarning ||
MessageBox::question(tr("LXQt Session Shutdown"),
tr("Do you want to really switch off your computer? All unsaved work will be lost...")))
{
m_power->shutdown();
}
}
void PowerManager::logout()
{
if (m_skipWarning ||
MessageBox::question(tr("LXQt Session Logout"),
tr("Do you want to really logout? All unsaved work will be lost...")))
{
m_power->logout();
}
}
void PowerManager::hibernateFailed()
{
MessageBox::warning(tr("LXQt Power Manager Error"), tr("Hibernate failed."));
}
void PowerManager::suspendFailed()
{
MessageBox::warning(tr("LXQt Power Manager Error"), tr("Suspend failed."));
}
} // namespace LXQt
|