qBittorrent
powermanagement_x11.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2011 Vladimir Golovnev <glassez@yandex.ru>
4  *
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.
9  *
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.
14  *
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.
18  *
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.
27  */
28 
29 #include "powermanagement_x11.h"
30 
31 #include <QDBusConnection>
32 #include <QDBusMessage>
33 #include <QDBusPendingCall>
34 #include <QDBusPendingReply>
35 
37  : QObject(parent)
38 {
39  if (!QDBusConnection::sessionBus().isConnected()) {
40  qDebug("D-Bus: Could not connect to session bus");
41  m_state = Error;
42  }
43  else {
44  m_state = Idle;
45  }
46 
48  m_cookie = 0;
49  m_useGSM = false;
50 }
51 
53 {
54 }
55 
57 {
59  if ((m_state == Error) || (m_state == Idle) || (m_state == RequestIdle) || (m_state == RequestBusy))
60  return;
61 
62  qDebug("D-Bus: PowerManagementInhibitor: Requesting idle");
63 
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");
77 
79 
80  QList<QVariant> args;
81  args << m_cookie;
82  call.setArguments(args);
83 
84  QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000);
85  auto *watcher = new QDBusPendingCallWatcher(pcall, this);
86  connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply);
87 }
88 
89 
91 {
93  if ((m_state == Error) || (m_state == Busy) || (m_state == RequestBusy) || (m_state == RequestIdle))
94  return;
95 
96  qDebug("D-Bus: PowerManagementInhibitor: Requesting busy");
97 
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");
111 
113 
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);
120 
121  QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000);
122  auto *watcher = new QDBusPendingCallWatcher(pcall, this);
123  connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply);
124 }
125 
126 void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call)
127 {
128  if (m_state == RequestIdle) {
129  QDBusPendingReply<> reply = *call;
130 
131  if (reply.isError()) {
132  qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message()));
133  m_state = Error;
134  }
135  else {
136  m_state = Idle;
137  qDebug("D-Bus: PowerManagementInhibitor: Request successful");
138  if (m_intendedState == Busy)
139  requestBusy();
140  }
141  }
142  else if (m_state == RequestBusy) {
143  QDBusPendingReply<uint> reply = *call;
144 
145  if (reply.isError()) {
146  qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message()));
147 
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();
154  }
155  else {
156  m_state = Error;
157  }
158  }
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();
165  }
166  }
167  else {
168  qDebug("D-Bus: Unexpected reply in state %d", m_state);
169  m_state = Error;
170  }
171 
172  call->deleteLater();
173 }
PowerManagementInhibitor(QObject *parent=nullptr)
void onAsyncReply(QDBusPendingCallWatcher *call)
args
Definition: tstool.py:153