Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / gui / powermanagement / powermanagement_x11.cpp
blob841bae4fd4de4686334016e861586ff45caa5bbe
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2011 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "powermanagement_x11.h"
31 #include <QDBusConnection>
32 #include <QDBusMessage>
33 #include <QDBusPendingCall>
34 #include <QDBusPendingReply>
36 PowerManagementInhibitor::PowerManagementInhibitor(QObject *parent)
37 : QObject(parent)
39 if (!QDBusConnection::sessionBus().isConnected()) {
40 qDebug("D-Bus: Could not connect to session bus");
41 m_state = Error;
43 else {
44 m_state = Idle;
47 m_intendedState = Idle;
48 m_cookie = 0;
49 m_useGSM = false;
52 PowerManagementInhibitor::~PowerManagementInhibitor()
56 void PowerManagementInhibitor::requestIdle()
58 m_intendedState = Idle;
59 if ((m_state == Error) || (m_state == Idle) || (m_state == RequestIdle) || (m_state == RequestBusy))
60 return;
62 qDebug("D-Bus: PowerManagementInhibitor: Requesting idle");
64 QDBusMessage call;
65 if (!m_useGSM)
66 call = QDBusMessage::createMethodCall(
67 "org.freedesktop.PowerManagement",
68 "/org/freedesktop/PowerManagement/Inhibit",
69 "org.freedesktop.PowerManagement.Inhibit",
70 "UnInhibit");
71 else
72 call = QDBusMessage::createMethodCall(
73 "org.gnome.SessionManager",
74 "/org/gnome/SessionManager",
75 "org.gnome.SessionManager",
76 "Uninhibit");
78 m_state = RequestIdle;
80 QList<QVariant> args;
81 args << m_cookie;
82 call.setArguments(args);
84 QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000);
85 auto *watcher = new QDBusPendingCallWatcher(pcall, this);
86 connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply);
90 void PowerManagementInhibitor::requestBusy()
92 m_intendedState = Busy;
93 if ((m_state == Error) || (m_state == Busy) || (m_state == RequestBusy) || (m_state == RequestIdle))
94 return;
96 qDebug("D-Bus: PowerManagementInhibitor: Requesting busy");
98 QDBusMessage call;
99 if (!m_useGSM)
100 call = QDBusMessage::createMethodCall(
101 "org.freedesktop.PowerManagement",
102 "/org/freedesktop/PowerManagement/Inhibit",
103 "org.freedesktop.PowerManagement.Inhibit",
104 "Inhibit");
105 else
106 call = QDBusMessage::createMethodCall(
107 "org.gnome.SessionManager",
108 "/org/gnome/SessionManager",
109 "org.gnome.SessionManager",
110 "Inhibit");
112 m_state = RequestBusy;
114 QList<QVariant> args;
115 args << "qBittorrent";
116 if (m_useGSM) args << 0u;
117 args << "Active torrents are presented";
118 if (m_useGSM) args << 8u;
119 call.setArguments(args);
121 QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000);
122 auto *watcher = new QDBusPendingCallWatcher(pcall, this);
123 connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply);
126 void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call)
128 if (m_state == RequestIdle) {
129 QDBusPendingReply<> reply = *call;
131 if (reply.isError()) {
132 qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message()));
133 m_state = Error;
135 else {
136 m_state = Idle;
137 qDebug("D-Bus: PowerManagementInhibitor: Request successful");
138 if (m_intendedState == Busy)
139 requestBusy();
142 else if (m_state == RequestBusy) {
143 QDBusPendingReply<uint> reply = *call;
145 if (reply.isError()) {
146 qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message()));
148 if (!m_useGSM) {
149 qDebug("D-Bus: Falling back to org.gnome.SessionManager");
150 m_useGSM = true;
151 m_state = Idle;
152 if (m_intendedState == Busy)
153 requestBusy();
155 else {
156 m_state = Error;
159 else {
160 m_state = Busy;
161 m_cookie = reply.value();
162 qDebug("D-Bus: PowerManagementInhibitor: Request successful, cookie is %d", m_cookie);
163 if (m_intendedState == Idle)
164 requestIdle();
167 else {
168 qDebug("D-Bus: Unexpected reply in state %d", m_state);
169 m_state = Error;
172 call->deleteLater();