~lubuntu-dev/lxde/liblxqt-ubuntu-tmp

1 by Andrew Lee (李健秋)
Import upstream version 0.9.0+20150806
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
public:
45
    explicit MessageBox(QWidget *parent = 0): QMessageBox(parent) {}
46
47
    static QWidget *parentWidget()
48
    {
49
        QWidgetList widgets = QApplication::topLevelWidgets();
50
51
        if (widgets.count())
52
            return widgets.at(0);
53
        else
54
            return 0;
55
    }
56
57
    static bool question(const QString& title, const QString& text)
58
    {
59
        MessageBox msgBox(parentWidget());
60
        msgBox.setWindowTitle(title);
61
        msgBox.setText(text);
62
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
63
64
        return (msgBox.exec() == QMessageBox::Yes);
65
    }
66
67
68
    static void warning(const QString& title, const QString& text)
69
    {
70
        QMessageBox::warning(parentWidget(), tr("LxQt Power Manager Error"), tr("Hibernate failed."));
71
    }
72
73
74
protected:
75
    virtual void resizeEvent(QResizeEvent* event)
76
    {
77
        QRect screen = QApplication::desktop()->screenGeometry();
78
        move((screen.width()  - this->width()) / 2,
79
             (screen.height() - this->height()) / 2);
80
81
    }
82
};
83
84
PowerManager::PowerManager(QObject * parent, bool skipWarning)
85
    : QObject(parent),
86
        m_skipWarning(skipWarning)
87
{
88
    m_power = new Power(this);
89
//    connect(m_power, SIGNAL(suspendFail()), this, SLOT(suspendFailed()));
90
//    connect(m_power, SIGNAL(hibernateFail()), this, SLOT(hibernateFailed()));
91
//    connect(m_power, SIGNAL(monitoring(const QString &)),
92
//            this, SLOT(monitoring(const QString&)));
93
94
    QString sessionConfig(getenv("LXQT_SESSION_CONFIG"));
95
    Settings settings(sessionConfig.isEmpty() ? "session" : sessionConfig);
96
    m_skipWarning = settings.value("leave_confirmation").toBool() ? false : true;
97
}
98
99
PowerManager::~PowerManager()
100
{
101
//    delete m_power;
102
}
103
104
QList<QAction*> PowerManager::availableActions()
105
{
106
    QList<QAction*> ret;
107
    QAction * act;
108
109
    // TODO/FIXME: icons
110
    if (m_power->canHibernate())
111
    {
112
        act = new QAction(XdgIcon::fromTheme("system-suspend-hibernate"), tr("Hibernate"), this);
113
        connect(act, SIGNAL(triggered()), this, SLOT(hibernate()));
114
        ret.append(act);
115
    }
116
117
    if (m_power->canSuspend())
118
    {
119
        act = new QAction(XdgIcon::fromTheme("system-suspend"), tr("Suspend"), this);
120
        connect(act, SIGNAL(triggered()), this, SLOT(suspend()));
121
        ret.append(act);
122
    }
123
124
    if (m_power->canReboot())
125
    {
126
        act = new QAction(XdgIcon::fromTheme("system-reboot"), tr("Reboot"), this);
127
        connect(act, SIGNAL(triggered()), this, SLOT(reboot()));
128
        ret.append(act);
129
    }
130
131
    if (m_power->canShutdown())
132
    {
133
        act = new QAction(XdgIcon::fromTheme("system-shutdown"), tr("Shutdown"), this);
134
        connect(act, SIGNAL(triggered()), this, SLOT(shutdown()));
135
        ret.append(act);
136
    }
137
138
    if (m_power->canLogout())
139
    {
140
        act = new QAction(XdgIcon::fromTheme("system-log-out"), tr("Logout"), this);
141
        connect(act, SIGNAL(triggered()), this, SLOT(logout()));
142
        ret.append(act);
143
    }
144
145
    return ret;
146
}
147
148
149
void PowerManager::suspend()
150
{
151
     if (m_skipWarning ||
152
         MessageBox::question(tr("LxQt Session Suspend"),
153
                              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.")))
154
    {
155
        m_power->suspend();
156
    }
157
}
158
159
void PowerManager::hibernate()
160
{
161
    if (m_skipWarning ||
162
        MessageBox::question(tr("LxQt Session Hibernate"),
163
                             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.")))
164
    {
165
        m_power->hibernate();
166
    }
167
}
168
169
void PowerManager::reboot()
170
{
171
    if (m_skipWarning ||
172
        MessageBox::question(tr("LxQt Session Reboot"),
173
                             tr("Do you want to really restart your computer? All unsaved work will be lost...")))
174
    {
175
        m_power->reboot();
176
    }
177
}
178
179
void PowerManager::shutdown()
180
{
181
    if (m_skipWarning ||
182
        MessageBox::question(tr("LxQt Session Shutdown"),
183
                             tr("Do you want to really switch off your computer? All unsaved work will be lost...")))
184
    {
185
        m_power->shutdown();
186
    }
187
}
188
189
void PowerManager::logout()
190
{
191
    if (m_skipWarning ||
192
        MessageBox::question(tr("LxQt Session Logout"),
193
                             tr("Do you want to really logout? All unsaved work will be lost...")))
194
    {
195
        m_power->logout();
196
    }
197
}
198
199
void PowerManager::hibernateFailed()
200
{
201
    MessageBox::warning(tr("LxQt Power Manager Error"), tr("Hibernate failed."));
202
}
203
204
void PowerManager::suspendFailed()
205
{
206
    MessageBox::warning(tr("LxQt Power Manager Error"), tr("Suspend failed."));
207
}
208
209
} // namespace LxQt