File: powerproviders.cpp

package info (click to toggle)
cutefish-core 0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,076 kB
  • sloc: cpp: 11,327; xml: 443; sh: 29; makefile: 6
file content (448 lines) | stat: -rw-r--r-- 14,671 bytes parent folder | download
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* BEGIN_COMMON_COPYRIGHT_HEADER
 * Authors:
 *   Alexander Sokoloff <sokoloff.a@gmail.com>
 *   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 "powerproviders.h"
#include <QDBusInterface>
#include <QProcess>
#include <QDebug>
#include <signal.h> // for kill()

#define UPOWER_SERVICE          "org.freedesktop.UPower"
#define UPOWER_PATH             "/org/freedesktop/UPower"
#define UPOWER_INTERFACE        UPOWER_SERVICE

#define CONSOLEKIT_SERVICE      "org.freedesktop.ConsoleKit"
#define CONSOLEKIT_PATH         "/org/freedesktop/ConsoleKit/Manager"
#define CONSOLEKIT_INTERFACE    "org.freedesktop.ConsoleKit.Manager"

#define SYSTEMD_SERVICE         "org.freedesktop.login1"
#define SYSTEMD_PATH            "/org/freedesktop/login1"
#define SYSTEMD_INTERFACE       "org.freedesktop.login1.Manager"

#define PROPERTIES_INTERFACE    "org.freedesktop.DBus.Properties"

/************************************************
 Helper func
 ************************************************/
void printDBusMsg(const QDBusMessage &msg)
{
    qWarning() << "** Dbus error **************************";
    qWarning() << "Error name " << msg.errorName();
    qWarning() << "Error msg  " << msg.errorMessage();
    qWarning() << "****************************************";
}

/************************************************
 Helper func
 ************************************************/
static bool dbusCall(const QString &service,
              const QString &path,
              const QString &interface,
              const QDBusConnection &connection,
              const QString & method,
              PowerProvider::DbusErrorCheck errorCheck = PowerProvider::CheckDBUS
              )
{
    QDBusInterface dbus(service, path, interface, connection);
    
    if (!dbus.isValid()) {
        qWarning() << "dbusCall: QDBusInterface is invalid" << service << path << interface << method;
        if (errorCheck == PowerProvider::CheckDBUS)
        {
            // Notification::notify(
            //                         QObject::tr("Power Manager Error"),
            //                         QObject::tr("QDBusInterface is invalid") + QStringLiteral("\n\n") + service + QStringLiteral(' ') + path + QStringLiteral(' ') + interface + QStringLiteral(' ') + method,
            //                         QStringLiteral("logo.png"));
        }
        return false;
    }

    QDBusMessage msg = dbus.call(method);
    if (!msg.errorName().isEmpty()) {
        printDBusMsg(msg);
        if (errorCheck == PowerProvider::CheckDBUS)
        {
            // Notification::notify(
            //                         QObject::tr("Power Manager Error (D-BUS call)"),
            //                         msg.errorName() + QStringLiteral("\n\n") + msg.errorMessage(),
            //                         QStringLiteral("logo.png"));
        }
    }

    // If the method no returns value, we believe that it was successful.
    return msg.arguments().isEmpty() ||
           msg.arguments().constFirst().isNull() ||
           msg.arguments().constFirst().toBool();
}

/************************************************
 Helper func

 Just like dbusCall(), except that systemd
 returns a string instead of a bool, and it takes
 an "interactivity boolean" as an argument.
 ************************************************/
static bool dbusCallSystemd(const QString &service,
                     const QString &path,
                     const QString &interface,
                     const QDBusConnection &connection,
                     const QString &method,
                     bool needBoolArg,
                     PowerProvider::DbusErrorCheck errorCheck = PowerProvider::CheckDBUS
                     )
{
    QDBusInterface dbus(service, path, interface, connection);
    if (!dbus.isValid()) {
        qWarning() << "dbusCall: QDBusInterface is invalid" << service << path << interface << method;
        if (errorCheck == PowerProvider::CheckDBUS) {
            // Notification::notify(
            //                         QObject::tr("Power Manager Error"),
            //                         QObject::tr("QDBusInterface is invalid") + QStringLiteral("\n\n") + service + QStringLiteral(' ') + path + QStringLiteral(' ')+ interface + QStringLiteral(' ') + method,
            //                         QStringLiteral("logo.png"));
        }

        return false;
    }

    QDBusMessage msg = dbus.call(method, needBoolArg ? QVariant(true) : QVariant());

    if (!msg.errorName().isEmpty()) {
        printDBusMsg(msg);
        if (errorCheck == PowerProvider::CheckDBUS) {
            // Notification::notify(
            //                         QObject::tr("Power Manager Error (D-BUS call)"),
            //                         msg.errorName() + QStringLiteral("\n\n") + msg.errorMessage(),
            //                         QStringLiteral("logo.png"));
        }
    }

    // If the method no returns value, we believe that it was successful.
    if (msg.arguments().isEmpty() || msg.arguments().constFirst().isNull())
        return true;

    QString response = msg.arguments().constFirst().toString();
    qDebug() << "systemd:" << method << "=" << response;
    return response == QStringLiteral("yes") || response == QStringLiteral("challenge");
}

/************************************************
 Helper func
 ************************************************/
bool dbusGetProperty(const QString &service,
                     const QString &path,
                     const QString &interface,
                     const QDBusConnection &connection,
                     const QString & property
                    )
{
    QDBusInterface dbus(service, path, interface, connection);
    if (!dbus.isValid())
    {
        qWarning() << "dbusGetProperty: QDBusInterface is invalid" << service << path << interface << property;
//        Notification::notify(QObject::tr("Power Manager"),
//                                  "logo.png",
//                                  QObject::tr("Power Manager Error"),
//                                  QObject::tr("QDBusInterface is invalid")+ "\n\n" + service +" " + path +" " + interface +" " + property);

        return false;
    }

    QDBusMessage msg = dbus.call(QStringLiteral("Get"), dbus.interface(), property);

    if (!msg.errorName().isEmpty())
    {
        printDBusMsg(msg);
//        Notification::notify(QObject::tr("Power Manager"),
//                                  "logo.png",
//                                  QObject::tr("Power Manager Error (Get Property)"),
//                                  msg.errorName() + "\n\n" + msg.errorMessage());
    }

    return !msg.arguments().isEmpty() &&
            msg.arguments().constFirst().value<QDBusVariant>().variant().toBool();
}

/************************************************
 PowerProvider
 ************************************************/
PowerProvider::PowerProvider(QObject *parent):
    QObject(parent)
{
}

PowerProvider::~PowerProvider()
{
}

/************************************************
 UPowerProvider
 ************************************************/
UPowerProvider::UPowerProvider(QObject *parent):
    PowerProvider(parent)
{
}

UPowerProvider::~UPowerProvider()
{
}

bool UPowerProvider::canAction(Power::Action action) const
{
    QString command;
    QString property;
    switch (action) {
    case Power::PowerHibernate:
        property = QStringLiteral("CanHibernate");
        command  = QStringLiteral("HibernateAllowed");
        break;
    case Power::PowerSuspend:
        property = QStringLiteral("CanSuspend");
        command  = QStringLiteral("SuspendAllowed");
        break;
    default:
        return false;
    }

    return dbusGetProperty(  // Whether the system is able to hibernate.
                QStringLiteral(UPOWER_SERVICE),
                QStringLiteral(UPOWER_PATH),
                QStringLiteral(PROPERTIES_INTERFACE),
                QDBusConnection::systemBus(),
                property
            )
            &&
            dbusCall( // Check if the caller has (or can get) the PolicyKit privilege to call command.
                QStringLiteral(UPOWER_SERVICE),
                QStringLiteral(UPOWER_PATH),
                QStringLiteral(UPOWER_INTERFACE),
                QDBusConnection::systemBus(),
                command,
                // canAction should be always silent because it can freeze
                // g_main_context_iteration Qt event loop in QMessageBox
                // on panel startup if there is no DBUS running.
                PowerProvider::DontCheckDBUS
            );
}

bool UPowerProvider::doAction(Power::Action action)
{
    QString command;

    switch (action) {
    case Power::PowerHibernate:
        command = QStringLiteral("Hibernate");
        break;
    case Power::PowerSuspend:
        command = QStringLiteral("Suspend");
        break;
    default:
        return false;
    }


    return dbusCall(QStringLiteral(UPOWER_SERVICE),
             QStringLiteral(UPOWER_PATH),
             QStringLiteral(UPOWER_INTERFACE),
             QDBusConnection::systemBus(),
             command );
}

/************************************************
 ConsoleKitProvider
 ************************************************/
ConsoleKitProvider::ConsoleKitProvider(QObject *parent):
    PowerProvider(parent)
{
}

ConsoleKitProvider::~ConsoleKitProvider()
{
}

bool ConsoleKitProvider::canAction(Power::Action action) const
{
    QString command;
    switch (action) {
    case Power::PowerReboot:
        command = QStringLiteral("CanReboot");
        break;

    case Power::PowerShutdown:
        command = QStringLiteral("CanPowerOff");
        break;

    case Power::PowerHibernate:
        command  = QStringLiteral("CanHibernate");
        break;

    case Power::PowerSuspend:
        command  = QStringLiteral("CanSuspend");
        break;

    default:
        return false;
    }

    return dbusCallSystemd(QStringLiteral(CONSOLEKIT_SERVICE),
                    QStringLiteral(CONSOLEKIT_PATH),
                    QStringLiteral(CONSOLEKIT_INTERFACE),
                    QDBusConnection::systemBus(),
                    command,
                    false,
                    // canAction should be always silent because it can freeze
                    // g_main_context_iteration Qt event loop in QMessageBox
                    // on panel startup if there is no DBUS running.
                    PowerProvider::DontCheckDBUS
                   );
}

bool ConsoleKitProvider::doAction(Power::Action action)
{
    QString command;
    switch (action) {
    case Power::PowerReboot:
        command = QStringLiteral("Reboot");
        break;
    case Power::PowerShutdown:
        command = QStringLiteral("PowerOff");
        break;
    case Power::PowerHibernate:
        command = QStringLiteral("Hibernate");
        break;
    case Power::PowerSuspend:
        command = QStringLiteral("Suspend");
        break;
    default:
        return false;
    }

    return dbusCallSystemd(QStringLiteral(CONSOLEKIT_SERVICE),
                QStringLiteral(CONSOLEKIT_PATH),
                QStringLiteral(CONSOLEKIT_INTERFACE),
                QDBusConnection::systemBus(),
                command,
                true);
}

/************************************************
  SystemdProvider

  http://www.freedesktop.org/wiki/Software/systemd/logind
 ************************************************/

SystemdProvider::SystemdProvider(QObject *parent):
    PowerProvider(parent)
{
}

SystemdProvider::~SystemdProvider()
{
}

bool SystemdProvider::canAction(Power::Action action) const
{
    QString command;

    switch (action) {
    case Power::PowerReboot:
        command = QStringLiteral("CanReboot");
        break;
    case Power::PowerShutdown:
        command = QStringLiteral("CanPowerOff");
        break;
    case Power::PowerSuspend:
        command = QStringLiteral("CanSuspend");
        break;
    case Power::PowerHibernate:
        command = QStringLiteral("CanHibernate");
        break;
    default:
        return false;
    }

    return dbusCallSystemd(QStringLiteral(SYSTEMD_SERVICE),
                    QStringLiteral(SYSTEMD_PATH),
                    QStringLiteral(SYSTEMD_INTERFACE),
                    QDBusConnection::systemBus(),
                    command,
                    false,
                    // canAction should be always silent because it can freeze
                    // g_main_context_iteration Qt event loop in QMessageBox
                    // on panel startup if there is no DBUS running.
                    PowerProvider::DontCheckDBUS
                   );
}

bool SystemdProvider::doAction(Power::Action action)
{
    QString command;

    switch (action) {
    case Power::PowerReboot:
        command = QStringLiteral("Reboot");
        break;
    case Power::PowerShutdown:
        command = QStringLiteral("PowerOff");
        break;
    case Power::PowerSuspend:
        command = QStringLiteral("Suspend");
        break;
    case Power::PowerHibernate:
        command = QStringLiteral("Hibernate");
        break;
    default:
        return false;
    }

    return dbusCallSystemd(QStringLiteral(SYSTEMD_SERVICE),
             QStringLiteral(SYSTEMD_PATH),
             QStringLiteral(SYSTEMD_INTERFACE),
             QDBusConnection::systemBus(),
             command,
             true
            );
}

/************************************************
  HalProvider
 ************************************************/
HalProvider::HalProvider(QObject *parent):
    PowerProvider(parent)
{
}

HalProvider::~HalProvider()
{
}

bool HalProvider::canAction(Power::Action action) const
{
    Q_UNUSED(action)
    return false;
}

bool HalProvider::doAction(Power::Action action)
{
    Q_UNUSED(action)
    return false;
}