Colorful details window.
[python-gnt.git] / example / delugent / tordetails.py
blob5ca01f9e9b135d49a85c7b1e4a6d65451533809c
1 from deluge.ui.client import aclient as client
2 import os.path
3 import deluge.common
4 from deluge.log import LOG as log
6 import gnt
7 import gobject
8 import common
9 import local
11 class TorFileRow(gobject.GObject):
12 def decide_priority(self):
13 if not self.enabled:
14 return "Skip"
15 if self.priorities == 1:
16 return "Normal"
17 elif self.priorities < 7:
18 return "Higher"
19 else:
20 return "Highest"
22 def get_row_flag(self):
23 if not self.enabled:
24 return gnt.TEXT_FLAG_DIM
25 elif self.progress == 1.0:
26 return gnt.TEXT_FLAG_BOLD
27 else:
28 return gnt.TEXT_FLAG_NORMAL
30 def __init__(self, file):
31 self.__gobject_init__()
32 self.path = file['path']
33 self.size = file['size']
34 self.pieces = 0 #len(file['priorities'])
35 self.priorities = 1 #file['priorities']
36 self.enabled = file['priority'] #len([p for p in self.priorities if p > 0]) > 0
37 self.progress = file['progress']
39 def info(self):
40 prg = "%.1f" % (self.progress * 100)
41 return [self.path, "%s (%5s%%)" % (common.show_size(self.size), prg), self.decide_priority()]
42 gobject.type_register(TorFileRow)
44 _tor_details = {}
46 class TorDetails(gnt.Window):
47 KEYS = [
48 'name',
49 'save_path',
50 'num_files',
51 'state',
52 'tracker',
53 'total_done',
54 'total_size',
55 'download_payload_rate',
56 'total_uploaded',
57 'total_payload_upload',
58 'upload_payload_rate',
59 'num_pieces',
60 'piece_length',
61 'num_seeds', 'total_seeds',
62 'num_peers', 'total_peers',
63 'eta',
64 'ratio',
65 'distributed_copies',
66 'files',
67 'file_priorities',
68 'file_progress',
69 'is_seed',
71 DEFAULT = gnt.gnt_color_add_pair(-1, -1)
72 BLACK = gnt.gnt_color_add_pair(0, -1)
73 RED = gnt.gnt_color_add_pair(1, -1)
74 GREEN = gnt.gnt_color_add_pair(2, -1)
75 YELLOW = gnt.gnt_color_add_pair(3, -1)
76 BLUE = gnt.gnt_color_add_pair(4, -1)
77 MAGENTA = gnt.gnt_color_add_pair(5, -1)
78 CYAN = gnt.gnt_color_add_pair(6, -1)
79 WHITE = gnt.gnt_color_add_pair(7, -1)
81 INTERVAL = 5 # Update the information every 5 seconds
83 def new(cls, torrent_id):
84 if torrent_id in _tor_details:
85 return _tor_details[torrent_id]
86 r = cls(torrent_id)
87 _tor_details[torrent_id] = r
88 return r
89 new = classmethod(new)
91 def __init__(self, torrent_id):
92 gnt.Window.__init__(self)
93 self.torrent = torrent_id
94 self.set_property('vertical', True)
95 self.set_property('homogeneous', False)
96 self.set_alignment(gnt.ALIGN_MID)
97 self.set_pad(0)
98 self.set_fill(True)
99 self.set_title("Details")
100 self.status = None
102 def _on_torrent_status(status):
103 self.status = status
105 client.get_torrent_status(_on_torrent_status, self.torrent, TorDetails.KEYS)
106 client.force_call()
108 def add_info(info):
109 string = ""
110 tv = gnt.TextView()
111 for label, value in info:
112 label = "%15s: " % label
113 tv.append_text_with_flags(label, gnt.TEXT_FLAG_BOLD)
114 tv.append_text_with_flags(value + "\n", gnt.TEXT_FLAG_NORMAL)
115 string = string + label + value + "\n"
116 tv.set_flag(gnt.TEXT_VIEW_TOP_ALIGN | gnt.TEXT_VIEW_NO_SCROLL)
117 w, h = gnt.get_text_bound(string.strip())
118 tv.set_size(w + 3, h)
119 return tv
121 status = self.status
122 top = (
123 ("Name", "%s" % status['name']),
124 ("Path", "%s" % status['save_path']),
125 ("# of files", "%s" % status['num_files']),
126 ("Status", status['state']),
127 ("Tracker", status['tracker']),
129 self.add_widget(add_info(top))
130 self.add_widget(gnt.Line(False))
133 left = (
134 ("Downloaded", "%s (%s)" % (deluge.common.fsize(float(status['total_done'])),
135 deluge.common.fsize(float(status['total_size'])))),
136 ("Download Speed", "%s" % deluge.common.fspeed(float(status['download_payload_rate']))),
137 ("Uploaded", "%s (%s)" % (deluge.common.fsize(float(status['total_uploaded'])),
138 deluge.common.fsize(float(status['total_payload_upload'])))),
139 ("Upload Speed", "%s" % deluge.common.fspeed(float(status['upload_payload_rate']))),
140 ("Pieces", "%s (%s)" % (status['num_pieces'], deluge.common.fsize(status['piece_length']))),
143 right = (
144 ("Seeders", "%s (%s)" % (status['num_seeds'], status['total_seeds'])),
145 ("Peers", "%s (%s)" % (status['num_peers'], status['total_peers'])),
146 ("ETA", "%s" % deluge.common.ftime(status['eta'])),
147 ("Share Ratio", "%.1f" % status['ratio']),
148 ("Availability", "%.1f" % status['distributed_copies']),
151 files = gnt.Tree()
152 files.set_property('columns', 3)
153 files.set_col_width(1, 13)
154 files.set_column_resizable(1, False)
155 files.set_col_width(2, 10)
156 files.set_show_title(True)
157 files.set_column_title(0, 'File')
158 files.set_column_title(1, 'Size')
159 files.set_column_title(2, 'Priority')
160 files.set_column_is_right_aligned(1, True)
161 priorities = status['file_priorities']
162 progress = status['file_progress']
163 for i, file in enumerate(status['files']):
164 if i < len(priorities):
165 file['priority'] = priorities[i]
166 else:
167 file['priority'] = 1
168 file['progress'] = progress[i]
169 torfile = TorFileRow(file)
170 files.add_row_after(torfile, torfile.info(), None)
171 files.set_row_flags(torfile, torfile.get_row_flag())
172 self.add_widget(files)
175 tv = gnt.TextView()
176 tv.set_flag(gnt.TEXT_VIEW_TOP_ALIGN | gnt.TEXT_VIEW_NO_SCROLL)
177 tv.text = ""
178 def append_row(color, llabel, ltext, rlabel, rtext):
179 tv.append_text_with_flags("%15s: " % llabel, gnt.TEXT_FLAG_NORMAL | gnt.gnt_color_pair(color))
180 tv.append_text_with_tag("%-25s " % ltext, gnt.TEXT_FLAG_BOLD | gnt.gnt_color_pair(color), llabel)
181 tv.append_text_with_flags("%15s: " % rlabel, gnt.TEXT_FLAG_NORMAL | gnt.gnt_color_pair(color))
182 tv.append_text_with_tag("%-15s\n" % rtext, gnt.TEXT_FLAG_BOLD | gnt.gnt_color_pair(color), llabel + rlabel)
183 tv.text = tv.text + "%15s: %-25s %15s: %-15s\n" % (llabel, ltext, rlabel, rtext)
185 if not status['is_seed']:
186 append_row(self.GREEN, "Downloaded",
187 "%s (%s)" % (deluge.common.fsize(float(status['total_done'])),
188 deluge.common.fsize(float(status['total_size']))),
189 "Speed", "%s" % deluge.common.fspeed(float(status['download_payload_rate'])))
191 append_row(self.BLUE, "Uploaded",
192 "%s (%s)" % (deluge.common.fsize(float(status['total_uploaded'])),
193 deluge.common.fsize(float(status['total_payload_upload']))),
194 "Speed", "%s" % deluge.common.fspeed(float(status['upload_payload_rate'])))
196 append_row(self.CYAN, "Seeds", "%s (%s)" % (status['num_seeds'], status['total_seeds']),
197 "Peers", "%s (%s)" % (status['num_peers'], status['total_peers']))
199 append_row(self.MAGENTA,
200 "Pieces", "%s (%s)" % (status['num_pieces'], deluge.common.fsize(status['piece_length'])),
201 "ETA", "%s" % deluge.common.ftime(status['eta']))
203 append_row(self.DEFAULT,
204 "Share Ratio", "%.1f" % status['ratio'],
205 "Availability", "%.1f" % status['distributed_copies'])
207 w, h = gnt.get_text_bound(tv.text)
208 tv.set_size(w, h)
209 gnt.unset_flag(tv, gnt.WIDGET_GROW_Y)
211 self.add_widget(tv)
213 ok = gnt.Button("OK")
214 def close_info_dlg(b, self):
215 self.destroy()
216 del self
217 ok.connect('activate', close_info_dlg, self)
218 box = gnt.Box(False, False)
219 box.add_widget(ok)
220 self.add_widget(box)
222 def remove_self(self):
223 del _tor_details[torrent_id]
224 self.connect('destroy', remove_self)
226 gobject.type_register(TorDetails)