Repository navigation
Expand file tree
/
Copy pathCollector.py
More file actions
407 lines (366 loc) · 13.7 KB
/
Copy pathCollector.py
File metadata and controls
407 lines (366 loc) · 13.7 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# -*- coding: utf-8 -*-
"""
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
@author: mkaay
"""
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from module.PyFile import statusMap
from module.utils import formatSize
from module.remote.thriftbackend.ThriftClient import Destination, FileDoesNotExists, ElementType
statusMapReverse = dict((v,k) for k, v in statusMap.iteritems())
translatedStatusMap = {} # -> CollectorModel.__init__
class CollectorModel(QAbstractItemModel):
"""
model for the collector view
"""
def __init__(self, view, connector):
QAbstractItemModel.__init__(self)
self.connector = connector
self.view = view
self._data = []
self.cols = 4
self.interval = 1
self.mutex = QMutex()
global translatedStatusMap # workaround because i18n is not running at import time
translatedStatusMap = {
"finished": _("finished"),
"offline": _("offline"),
"online": _("online"),
"queued": _("queued"),
"skipped": _("skipped"),
"waiting": _("waiting"),
"temp. offline": _("temp. offline"),
"starting": _("starting"),
"failed": _("failed"),
"aborted": _("aborted"),
"decrypting": _("decrypting"),
"custom": _("custom"),
"downloading": _("downloading"),
"processing": _("processing")
}
def translateStatus(self, string):
"""
used to convert to locale specific status
"""
return translatedStatusMap[string]
def addEvent(self, event):
"""
called from main loop, pass events to the correct methods
"""
QMutexLocker(self.mutex)
if event.eventname == "reload":
self.fullReload()
elif event.eventname == "remove":
self.removeEvent(event)
elif event.eventname == "insert":
self.insertEvent(event)
elif event.eventname == "update":
self.updateEvent(event)
def fullReload(self):
"""
reload whole model, used at startup to load initial data
"""
self._data = []
order = self.connector.getPackageOrder(Destination.Collector)
self.beginInsertRows(QModelIndex(), 0, len(order.values()))
for position, pid in order.iteritems():
pack = self.connector.getPackageData(pid)
package = Package(pack)
self._data.append(package)
self._data = sorted(self._data, key=lambda p: p.data["order"])
self.endInsertRows()
def removeEvent(self, event):
"""
remove an element from model
"""
if event.type == ElementType.File:
for p, package in enumerate(self._data):
for k, child in enumerate(package.children):
if child.id == event.id:
self.beginRemoveRows(self.index(p, 0), k, k)
del package.children[k]
self.endRemoveRows()
break
else:
for k, package in enumerate(self._data):
if package.id == event.id:
self.beginRemoveRows(QModelIndex(), k, k)
del self._data[k]
self.endRemoveRows()
break
def insertEvent(self, event):
"""
inserts a new element in the model
"""
if event.type == ElementType.File:
try:
info = self.connector.getFileData(event.id)
except FileDoesNotExists:
return
for k, package in enumerate(self._data):
if package.id == info.package:
if package.getChild(info.fid):
self.updateEvent(event)
break
self.beginInsertRows(self.index(k, 0), info.order, info.order)
package.addChild(info)
self.endInsertRows()
break
else:
data = self.connector.getPackageData(event.id)
package = Package(data)
self.beginInsertRows(QModelIndex(), data.order, data.order)
self._data.insert(data.order, package)
self.endInsertRows()
def updateEvent(self, event):
"""
update an element in the model
"""
if event.type == ElementType.File:
try:
info = self.connector.proxy.getFileData(event.id)
except FileDoesNotExists:
return
for p, package in enumerate(self._data):
if package.id == info.packageID:
for k, child in enumerate(package.children):
if child.id == event.id:
child.update(info)
if not info.status == 12:
child.data["downloading"] = None
self.emit(SIGNAL("dataChanged(const QModelIndex &, const QModelIndex &)"), self.index(k, 0, self.index(p, 0)), self.index(k, self.cols, self.index(p, self.cols)))
break
else:
data = self.connector.getPackageData(event.id)
if not data:
return
for p, package in enumerate(self._data):
if package.id == event.id:
package.update(data)
self.emit(SIGNAL("dataChanged(const QModelIndex &, const QModelIndex &)"), self.index(p, 0), self.index(p, self.cols))
break
def data(self, index, role=Qt.DisplayRole):
"""
return cell data
"""
if not index.isValid():
return QVariant()
if role == Qt.DisplayRole:
if index.column() == 0:
return QVariant(index.internalPointer().data["name"])
elif index.column() == 1:
item = index.internalPointer()
plugins = []
if isinstance(item, Package):
for child in item.children:
if not child.data["plugin"] in plugins:
plugins.append(child.data["plugin"])
else:
plugins.append(item.data["plugin"])
return QVariant(", ".join(plugins))
elif index.column() == 2:
item = index.internalPointer()
status = 0
if isinstance(item, Package):
for child in item.children:
if child.data["status"] > status:
status = child.data["status"]
else:
status = item.data["status"]
return QVariant(self.translateStatus(statusMapReverse[status]))
elif index.column() == 3:
item = index.internalPointer()
if isinstance(item, Link):
return QVariant(formatSize(item.data["size"]))
else:
ms = 0
for c in item.children:
ms += c.data["size"]
return QVariant(formatSize(ms))
elif role == Qt.EditRole:
if index.column() == 0:
return QVariant(index.internalPointer().data["name"])
return QVariant()
def index(self, row, column, parent=QModelIndex()):
"""
creates a cell index with pointer to the data
"""
if parent == QModelIndex() and len(self._data) > row:
pointer = self._data[row]
index = self.createIndex(row, column, pointer)
elif parent.isValid():
try:
pointer = parent.internalPointer().children[row]
except:
return QModelIndex()
index = self.createIndex(row, column, pointer)
else:
index = QModelIndex()
return index
def parent(self, index):
"""
return index of parent element
only valid for links
"""
if index == QModelIndex():
return QModelIndex()
if index.isValid():
link = index.internalPointer()
if isinstance(link, Link):
for k, pack in enumerate(self._data):
if pack == link.package:
return self.createIndex(k, 0, link.package)
return QModelIndex()
def rowCount(self, parent=QModelIndex()):
"""
returns row count for the element
"""
if parent == QModelIndex():
#return package count
return len(self._data)
else:
if parent.isValid():
#index is valid
pack = parent.internalPointer()
if isinstance(pack, Package):
#index points to a package
#return len of children
return len(pack.children)
else:
#index is invalid
return False
#files have no children
return 0
def columnCount(self, parent=QModelIndex()):
return self.cols
def hasChildren(self, parent=QModelIndex()):
if not parent.isValid():
return True
return self.rowCount(parent) > 0
def canFetchMore(self, parent):
return False
def headerData(self, section, orientation, role=Qt.DisplayRole):
"""
returns column heading
"""
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
if section == 0:
return QVariant(_("Name"))
elif section == 1:
return QVariant(_("Plugin"))
elif section == 2:
return QVariant(_("Status"))
elif section == 3:
return QVariant(_("Size"))
return QVariant()
def flags(self, index):
"""
cell flags
"""
if index.column() == 0 and self.parent(index) == QModelIndex():
return Qt.ItemIsSelectable | Qt.ItemIsEditable | Qt.ItemIsEnabled
return Qt.ItemIsSelectable | Qt.ItemIsEnabled
def setData(self, index, value, role=Qt.EditRole):
"""
called if package name editing is finished, sets new name
"""
if index.column() == 0 and self.parent(index) == QModelIndex() and role == Qt.EditRole:
self.connector.setPackageName(index.internalPointer().id, str(value.toString()))
return True
class Package(object):
"""
package object in the model
"""
def __init__(self, pack):
self.id = pack.pid
self.children = []
for f in pack.links:
self.addChild(f)
self.data = {}
self.update(pack)
def update(self, pack):
"""
update data dict from thift object
"""
data = {
"name": pack.name,
"folder": pack.folder,
"site": pack.site,
"password": pack.password,
"order": pack.order,
}
self.data.update(data)
def addChild(self, f):
"""
add child (Link) to package
"""
self.children.insert(f.order, Link(f, self))
self.children = sorted(self.children, key=lambda l: l.data["order"])
def getChild(self, fid):
"""
get child from package
"""
for child in self.children:
if child.id == int(fid):
return child
return None
def getChildKey(self, fid):
"""
get child index
"""
for k, child in enumerate(self.children):
if child.id == int(fid):
return k
return None
def removeChild(self, fid):
"""
remove child
"""
for k, child in enumerate(self.children):
if child.id == int(fid):
del self.children[k]
class Link(object):
def __init__(self, f, pack):
self.data = {"downloading": None}
self.update(f)
self.id = f.fid
self.package = pack
def update(self, f):
"""
update data dict from thift object
"""
data = {
"url": f.url,
"name": f.name,
"plugin": f.plugin,
"size": f.size,
"format_size": f.format_size,
"status": f.status,
"statusmsg": f.statusmsg,
"package": f.packageID,
"error": f.error,
"order": f.order,
}
self.data.update(data)
class CollectorView(QTreeView):
"""
view component for collector
"""
def __init__(self, connector):
QTreeView.__init__(self)
self.setModel(CollectorModel(self, connector))
self.setColumnWidth(0, 500)
self.setColumnWidth(1, 100)
self.setColumnWidth(2, 200)
self.setColumnWidth(3, 100)
self.setEditTriggers(QAbstractItemView.DoubleClicked | QAbstractItemView.EditKeyPressed)