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 <QDBusInterface>
33 #include <QDBusPendingCall>
34 #include <QDBusPendingReply>
36 #include "base/global.h"
37 #include "base/logger.h"
39 PowerManagementInhibitor::PowerManagementInhibitor(QObject
*parent
)
41 , m_busInterface
{new QDBusInterface(u
"org.gnome.SessionManager"_s
, u
"/org/gnome/SessionManager"_s
42 , u
"org.gnome.SessionManager"_s
, QDBusConnection::sessionBus(), this)}
44 if (!m_busInterface
->isValid())
46 delete m_busInterface
;
48 m_busInterface
= new QDBusInterface(u
"org.freedesktop.login1"_s
, u
"/org/freedesktop/login1"_s
49 , u
"org.freedesktop.login1.Manager"_s
, QDBusConnection::systemBus(), this);
50 m_manager
= ManagerType::Systemd
;
51 if (!m_busInterface
->isValid())
53 delete m_busInterface
;
55 m_busInterface
= new QDBusInterface(u
"org.freedesktop.PowerManagement"_s
, u
"/org/freedesktop/PowerManagement/Inhibit"_s
56 , u
"org.freedesktop.PowerManagement.Inhibit"_s
, QDBusConnection::sessionBus(), this);
57 m_manager
= ManagerType::Freedesktop
;
58 if (!m_busInterface
->isValid())
60 delete m_busInterface
;
61 m_busInterface
= nullptr;
69 LogMsg(tr("Power management found suitable D-Bus interface. Interface: %1").arg(m_busInterface
->interface()));
73 LogMsg(tr("Power management error. Did not found suitable D-Bus interface."), Log::WARNING
);
77 void PowerManagementInhibitor::requestIdle()
79 m_intendedState
= Idle
;
80 if ((m_state
== Error
) || (m_state
== Idle
) || (m_state
== RequestIdle
) || (m_state
== RequestBusy
))
83 if (m_manager
== ManagerType::Systemd
)
90 m_state
= RequestIdle
;
92 const QString method
= (m_manager
== ManagerType::Gnome
)
95 const QDBusPendingCall pcall
= m_busInterface
->asyncCall(method
, m_cookie
);
96 const auto *watcher
= new QDBusPendingCallWatcher(pcall
, this);
97 connect(watcher
, &QDBusPendingCallWatcher::finished
, this, &PowerManagementInhibitor::onAsyncReply
);
100 void PowerManagementInhibitor::requestBusy()
102 m_intendedState
= Busy
;
103 if ((m_state
== Error
) || (m_state
== Busy
) || (m_state
== RequestBusy
) || (m_state
== RequestIdle
))
106 m_state
= RequestBusy
;
108 const QString message
= u
"Active torrents are currently present"_s
;
110 QList
<QVariant
> args
;
113 case ManagerType::Freedesktop
:
114 args
= {u
"qBittorrent"_s
, message
};
116 case ManagerType::Gnome
:
117 args
= {u
"qBittorrent"_s
, 0u, message
, 4u};
119 case ManagerType::Systemd
:
120 args
= {u
"sleep"_s
, u
"qBittorrent"_s
, message
, u
"block"_s
};
124 const QDBusPendingCall pcall
= m_busInterface
->asyncCallWithArgumentList(u
"Inhibit"_s
, args
);
125 const auto *watcher
= new QDBusPendingCallWatcher(pcall
, this);
126 connect(watcher
, &QDBusPendingCallWatcher::finished
, this, &PowerManagementInhibitor::onAsyncReply
);
129 void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher
*call
)
133 if (m_state
== RequestIdle
)
135 const QDBusPendingReply reply
= *call
;
139 LogMsg(tr("Power management error. Action: %1. Error: %2").arg(u
"RequestIdle"_s
140 , reply
.error().message()), Log::WARNING
);
146 if (m_intendedState
== Busy
)
150 else if (m_state
== RequestBusy
)
152 if (m_manager
== ManagerType::Systemd
)
154 const QDBusPendingReply
<QDBusUnixFileDescriptor
> reply
= *call
;
158 LogMsg(tr("Power management error. Action: %1. Error: %2").arg(u
"RequestBusy"_s
159 , reply
.error().message()), Log::WARNING
);
165 m_fd
= reply
.value();
166 if (m_intendedState
== Idle
)
172 const QDBusPendingReply
<uint
> reply
= *call
;
176 LogMsg(tr("Power management error. Action: %1. Error: %2").arg(u
"RequestBusy"_s
177 , reply
.error().message()), Log::WARNING
);
183 m_cookie
= reply
.value();
184 if (m_intendedState
== Idle
)
191 const QDBusPendingReply reply
= *call
;
192 const QDBusError error
= reply
.error();
196 LogMsg(tr("Power management unexpected error. State: %1. Error: %2").arg(QString::number(m_state
)
197 , error
.message()), Log::WARNING
);