-
Notifications
You must be signed in to change notification settings - Fork 2
/
TorrentModel.py
142 lines (123 loc) · 4.5 KB
/
TorrentModel.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
from PySide.QtCore import QAbstractListModel, QModelIndex, Slot
class TorrentModel(QAbstractListModel):
#Enum SHOW
SHOW_ALL = 0
SHOW_ACTIVE_ONLY = 1
SHOW_DOWNLOADING_ONLY = 2
SHOW_SEEDING_ONLY = 3
SHOW_PAUSED_ONLY = 4
SHOW_FINISHED_ONLY = 5
#Role Name
NAME = 0
STATUS = 1
RATIO = 2
PRIORITY = 3
HASH = 4
ETA = 5
SIZE = 6
COMPLETED = 7
PROGRESS = 8
ID = 9
TITLE = 100
def __init__(self, server, timeout=5000):
QAbstractListModel.__init__(self)
self._loading = False
self._show = TorrentModel.SHOW_ALL
self._torrents = []
self._visibles = []
self.setRoleNames({self.ID : 'TORRENT_ID',
self.NAME : 'TORRENT_NAME',
self.STATUS : 'TORRENT_STATUS',
self.RATIO : 'TORRENT_RATIO',
self.PRIORITY : 'TORRENT_PRIORITY',
self.ETA : 'TORRENT_ETA',
self.SIZE : 'TORRENT_SIZE',
self.COMPLETED: 'TORRENT_COMPLETED',
self.PROGRESS : 'TORRENT_PROGRESS',
self.TITLE : 'title'})
self._server = server
self._server.clientConnected.connect(self.reset)
self._server.clientDisconnected.connect(self.reset)
self._server.torrentAdded.connect(self._onTorrentAdded)
self._server.torrentRemoved.connect(self._onTorrentRemoved)
self._server.torrentUpdated.connect(self._onTorrentUpdate)
@Slot(int)
def showTorrents(self, flag):
if self._show != flag:
self._show = flag
visibles = []
for t in self._torrents:
if self._torrentIsVisible(t):
visibles.append(t)
self.modelAboutToBeReset.emit()
self._visibles = visibles
self.modelReset.emit()
def _onTorrentAdded(self, torrent):
self._torrents.append(torrent)
if self._torrentIsVisible(torrent):
parent = QModelIndex()
index = len(self._visibles)
self.rowsAboutToBeInserted.emit(parent, index, index)
self._visibles.append(torrent)
self.rowsInserted.emit(parent, index, index)
def _onTorrentRemoved(self, torrent):
try:
row = self._visbiles.index(torrent)
except ValueError:
return
parent = QModelIndex()
self.rowsAboutToBeRemoved.emit(parent, row, row)
self._visibles.remove(torrent)
self.rowsRemoved.emit(parent, row, row)
self._torrents.remove(torrent)
def _onTorrentUpdate(self, torrent):
try:
row = self._visibles.index(torrent)
except ValueError:
return
index = self.index(row, 0)
self.dataChanged.emit(index, index)
def _torrentIsVisible(self, t):
if self._show == TorrentModel.SHOW_ALL:
return True
elif self._show == TorrentModel.SHOW_ACTIVE_ONLY:
return (t._obj.status != 'stopped')
elif self._show == TorrentModel.SHOW_DOWNLOADING_ONLY:
return (t._obj.status == 'downloading')
elif self._show == TorrentModel.SHOW_SEEDING_ONLY:
return (t._obj.status == 'seeding')
elif self._show == TorrentModel.SHOW_PAUSED_ONLY:
return False
elif self._show == TorrentModel.SHOW_FINISHED_ONLY:
return t._obj.finished()
else:
return True
def reset(self):
self._torrents = []
self.modelReset.emit()
def rowCount(self, index):
return len(self._visibles)
def data(self, index, role):
t = self._visibles[index.row()]
if role == TorrentModel.ID:
return t._id
elif role == TorrentModel.NAME:
return t._obj.name
elif role == TorrentModel.STATUS:
return t._obj.status
elif role == TorrentModel.RATIO:
return "%.2f" % t._obj.ratio
elif role == TorrentModel.PRIORITY:
return t._obj.priority
elif role == TorrentModel.ETA:
return t._obj.format_eta()
elif role == TorrentModel.PROGRESS:
return t._obj.progress
elif role == TorrentModel.SIZE:
return t.size()
elif role == TorrentModel.COMPLETED:
return t.completed()
elif role == TorrentModel.TITLE:
return t._obj.name
else:
return ''