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
)
39 if (!QDBusConnection::sessionBus().isConnected()) {
40 qDebug("D-Bus: Could not connect to session bus");
47 m_intendedState
= Idle
;
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
))
62 qDebug("D-Bus: PowerManagementInhibitor: Requesting idle");
66 call
= QDBusMessage::createMethodCall(
67 "org.freedesktop.PowerManagement",
68 "/org/freedesktop/PowerManagement/Inhibit",
69 "org.freedesktop.PowerManagement.Inhibit",
72 call
= QDBusMessage::createMethodCall(
73 "org.gnome.SessionManager",
74 "/org/gnome/SessionManager",
75 "org.gnome.SessionManager",
78 m_state
= RequestIdle
;
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
))
96 qDebug("D-Bus: PowerManagementInhibitor: Requesting busy");
100 call
= QDBusMessage::createMethodCall(
101 "org.freedesktop.PowerManagement",
102 "/org/freedesktop/PowerManagement/Inhibit",
103 "org.freedesktop.PowerManagement.Inhibit",
106 call
= QDBusMessage::createMethodCall(
107 "org.gnome.SessionManager",
108 "/org/gnome/SessionManager",
109 "org.gnome.SessionManager",
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()));
137 qDebug("D-Bus: PowerManagementInhibitor: Request successful");
138 if (m_intendedState
== Busy
)
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()));
149 qDebug("D-Bus: Falling back to org.gnome.SessionManager");
152 if (m_intendedState
== Busy
)
161 m_cookie
= reply
.value();
162 qDebug("D-Bus: PowerManagementInhibitor: Request successful, cookie is %d", m_cookie
);
163 if (m_intendedState
== Idle
)
168 qDebug("D-Bus: Unexpected reply in state %d", m_state
);