forked from satiricon/qbittorrent-unity-launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqbittorrent-unity-launcher.py
executable file
·245 lines (190 loc) · 7.61 KB
/
qbittorrent-unity-launcher.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Licensed under the MIT license.
import sys
import logging
import argparse
import qbittorrentrpc
from gi.repository import Unity, GLib, GObject, Dbusmenu
def spawn_async(argv, flags):
if (GLib.MAJOR_VERSION, GLib.MINOR_VERSION) < (2, 30):
_, pid = GLib.spawn_async(
None, # Inherit current directory,
argv, # Command with arguments.
None, # Inherit environment.
flags,
None, # Child setup callback.
None # User data.
)
else:
pid, _, _, _ = GLib.spawn_async(argv=argv, flags=flags)
return pid
def child_watch_add(priority, pid, on_closed, data):
if (GLib.MAJOR_VERSION, GLib.MINOR_VERSION) < (2, 30):
return GLib.child_watch_add(priority, pid, on_closed, data)
else:
return GLib.child_watch_add(
priority=priority,
pid=pid,
function=on_closed,
data=data)
class UnityLauncherEntry:
def __init__(self, name):
self.name = name
logging.debug("Get launcher entry %s", self.name)
self.entry = Unity.LauncherEntry.get_for_desktop_id(self.name)
def set_progress(self, progress):
if progress is not None:
self.entry.set_property('progress', progress)
self.entry.set_property('progress_visible', True)
else:
self.entry.set_property('progress_visible', False)
def set_count(self, count):
if count is not None:
self.entry.set_property('count', count)
self.entry.set_property('count_visible', True)
else:
self.entry.set_property('count_visible', False)
def set_quicklist_menu(self, menu):
self.entry.set_property('quicklist', menu)
class QbittorrentUnityController:
def __init__(self, qbittorrent, launcher_entry, options):
self.qbittorrent = qbittorrent
self.launcher_entry = launcher_entry
self.options = options
self.create_quicklist_menu()
def update(self):
logging.debug("Get torrents list.")
torrents = self.qbittorrent.get_downloading_torrent_list()
torrents_count = len(torrents)
progress = self.qbittorrent.get_downloading_torrent_progress()
logging.debug(
"Recieved Downloading torrents count: %d",
torrents_count)
if torrents_count > 0:
logging.info(
"Downloading torrents count: %d and progress %f",
torrents_count,
progress)
# Set launcher entry properties.
self.launcher_entry.set_count(torrents_count)
self.launcher_entry.set_progress(progress)
else:
self.launcher_entry.set_count(None)
self.launcher_entry.set_progress(None)
def create_quicklist_menu(self):
menu = Dbusmenu.Menuitem.new()
pause_all_item = Dbusmenu.Menuitem.new()
pause_all_item.property_set_bool(Dbusmenu.MENUITEM_PROP_VISIBLE, True)
pause_all_item.property_set(Dbusmenu.MENUITEM_PROP_LABEL, "Pause All")
self.pause_all_item = pause_all_item
menu.child_append(pause_all_item)
self.pause_all_item.property_set_bool(
Dbusmenu.MENUITEM_PROP_VISIBLE,
True)
all_paused = self.qbittorrent.all_paused()
logging.debug('All paused? %d', all_paused)
if all_paused:
self.pause_all_item.property_set(
Dbusmenu.MENUITEM_PROP_LABEL,
"Resume All")
self.pause_all_item.connect(
'item-activated',
self._on_menu_pause_all,
None)
self.launcher_entry.set_quicklist_menu(menu)
def _on_menu_pause_all(self, menuitem, _, data):
current_state = menuitem.property_get(Dbusmenu.MENUITEM_PROP_LABEL)
if current_state == 'Pause All':
self.qbittorrent.pause_all()
self.pause_all_item.property_set(
Dbusmenu.MENUITEM_PROP_LABEL,
"Resume All")
else:
self.qbittorrent.resume_all()
self.pause_all_item.property_set(
Dbusmenu.MENUITEM_PROP_LABEL,
"Pause All")
logging.basicConfig(level=logging.WARNING)
parser = argparse.ArgumentParser(
description="Integrate Qbittorrent into Unity Launcher.")
parser.add_argument('qbittorrent_command',
nargs='+',
help="Command and arguments to start qbittorrent")
parser.add_argument('-H', '--host',
action='store', dest='host',
default='localhost',
help="address for connecting to Qbitorrent RPC")
parser.add_argument('-p', '--port',
action='store', dest='port', type=int,
default=8080,
help="port for connecting to Qbittorrent RPC")
parser.add_argument('-U', '--user',
action='store', dest='user',
default=None,
help="user name for connecting to Qbittorent RPC")
parser.add_argument('-P', '--password',
action='store', dest='password',
default=None,
help="password for connecting to Qbittorrent RPC")
parser.add_argument('-u', '--update-interval',
action='store', dest='update_interval', type=int,
default=20,
help="interval (in seconds) between status updates")
parser.add_argument('-t', '--startup-timeout',
default=4,
help="seconds between Qbittorrent start and first connection attempt")
parser.add_argument('-l', '--launcher-entry-name',
action='store', dest='launcher_entry_name',
default='qBittorrent.desktop',
help="name of .desktop file used to start Qbittorrent")
args = parser.parse_args()
loop = GObject.MainLoop()
def start_process(command):
flags = (
# Inherit PATH environment variable.
GLib.SpawnFlags.SEARCH_PATH |
# Don't reap process automatically so it is possible
# to detect when it is closed.
GLib.SpawnFlags.DO_NOT_REAP_CHILD
)
pid = spawn_async(command, flags)
return pid
qbt_pid = start_process(args.qbittorrent_command)
logging.info("Qbittorrent started (pid: %d).", qbt_pid)
# Exit when Qbittorrent is closed.
def qbittorrent_closed(pid, status, data):
logging.info("Qbittorrent exited with status %d, exiting.", status)
GLib.spawn_close_pid(pid)
loop.quit()
child_watch_add(GLib.PRIORITY_DEFAULT, qbt_pid, qbittorrent_closed, None)
def first_update():
qbittorrent = qbittorrentrpc.Client(
args.host,
args.port,
args.user,
args.password)
launcher_entry = UnityLauncherEntry(args.launcher_entry_name)
# Create controller.
controller = QbittorrentUnityController(qbittorrent, launcher_entry, args)
# Try to update status for the first time.
controller.update()
# If all is ok, start main timer.
GObject.timeout_add_seconds(
args.update_interval,
periodic_update,
controller)
def periodic_update(controller):
logging.debug('Periodic Update')
try:
controller.update()
except:
logging.error("Connection to Qbittorrent is lost.")
sys.stderr.write("""Connection to Qbittorrent is lost. Quit.""")
loop.quit() # Terminate application loop.
GObject.timeout_add_seconds(
args.update_interval,
periodic_update,
controller)
GObject.timeout_add_seconds(args.startup_timeout, first_update)
loop.run()