📄 downloadmanager.py
字号:
else: for i in(self.NEVER_ID, self.NORMAL_ID, self.FIRST_ID): self.tool_bar.EnableTool(i, True) self.tool_bar.EnableTool(self.OPEN_ID, False) self.context_menu.Enable(self.OPEN_ID, False) if event is not None: event.Skip() def _build_tool_bar(self): self.tool_bar = BTToolBar(self, ops=[self.file_ops]) self.tool_bar.InsertSeparator(len(self.file_ops)-1) self.tool_bar.Realize() self.sizer.Insert(0, self.tool_bar, flag=wx.GROW, proportion=0) def reset_toolbar_style(self): found = self.sizer.Detach(self.tool_bar) if found: # Keep the old bars around just in case they get a # callback before we build new ones b = self.tool_bar # build the new bar self._build_tool_bar() if found: # destroy the old bar now that there's a new one b.Destroy() self.sizer.Layout() def BindChildren(self, evt_id, func): self.file_list.Bind(evt_id, func) def OnFileEvent(self, event): id = event.GetId() if self.event_table.has_key(id): e = self.event_table[id] df = launch_coroutine(gui_wrap, e.func) def error(exc_info): ns = 'core.MultiTorrent.' + repr(self.torrent.infohash) l = logging.getLogger(ns) l.error(e.func.__name__ + " failed", exc_info=exc_info) df.addErrback(error) else: print 'Not implemented!' def set_file_priority_first(self): self.set_file_priority(1) def set_file_priority_normal(self): self.set_file_priority(0) def set_file_priority_never(self): # BUG: Not implemented ## self.set_file_priority(-1) print 'Not implemented!' def set_file_priority(self, priority): files = self.file_list.get_selected_files(priority=priority) wx.the_app.set_file_priority(self.torrent.infohash, files, priority) def open_items(self): if self.torrent.completion >= 1: path = self.torrent.destination_path else: path = self.torrent.working_path dirs, files = self.file_list.get_selection() for d in dirs: if d is None: LaunchPath.launchdir(path) else: LaunchPath.launchdir(os.path.join(path, d)) # only launch complete files complete_files = self.file_list.get_complete_files(files) for f in complete_files: LaunchPath.launchfile(os.path.join(path, f)) def file_double_clicked(self, event): self.open_items() def update(self, *args): self.file_list.update_files(*args) self.check_file_selection()class LogPanel(BTPanel): def __init__(self, parent, torrent, *a, **k): BTPanel.__init__(self, parent, *a, **k) self.log = wx.TextCtrl(self, style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2) self.log.Bind(wx.EVT_TEXT, self.OnText) self.Add(self.log, flag=wx.GROW, proportion=1) class MyTorrentLogger(logging.Handler): def set_log_func(self, func): self.log_func = func def emit(self, record): gui_wrap(self.log_func, self.format(record) + '\n') l = MyTorrentLogger() l.setFormatter(bt_log_fmt) l.set_log_func(self.log.AppendText) torrent.handler.setTarget(l) torrent.handler.flush() def OnText(self, event): e = self.log.GetLastPosition() if e > MAX_TEXTCTRL_LENGTH: self.log.Remove(0, e - MAX_TEXTCTRL_LENGTH) def BindChildren(self, evt_id, func): self.log.Bind(evt_id, func)class TorrentDetailsPanel(wx.Panel): def __init__(self, parent, torrent, *a, **k): wx.Panel.__init__(self, parent, *a, **k) self.torrent = torrent self.sizer = wx.BoxSizer(wx.HORIZONTAL) self.scrolled_window = wx.ScrolledWindow(self) self.scrolled_window.SetScrollRate(1, 1) self.sizer.Add(self.scrolled_window, flag=wx.GROW, proportion=1) self.scroll_sizer = wx.BoxSizer(wx.HORIZONTAL) self.panel = wx.Panel(self.scrolled_window) self.scroll_sizer.Add(self.panel, flag=wx.GROW, proportion=1) self.outer = VSizer() self.swarm_fgsizer = LabelValueFlexGridSizer(self.panel, 5, 2, SPACING, SPACING) self.swarm_fgsizer.SetFlexibleDirection(wx.HORIZONTAL) self.swarm_fgsizer.AddGrowableCol(1) self.swarm_static_box = wx.StaticBox(self.panel, label=_("Swarm:")) self.swarm_static_box_sizer = wx.StaticBoxSizer(self.swarm_static_box, wx.HORIZONTAL) self.swarm_static_box_sizer.Add(self.swarm_fgsizer, flag=wx.GROW|wx.ALL, border=SPACING, proportion=1) self.outer.AddFirst(self.swarm_static_box_sizer, flag=wx.GROW, border=SPACING, proportion=1) for label, item in zip((_("Tracker total peers:"), _("Distributed copies:"), _("Swarm speed:"), _("Discarded data:"), _("Next announce:"),), ('tracker_peers' , 'distributed' , 'swarm_speed' , 'discarded' , 'announce' ,)): t = self.swarm_fgsizer.add_pair(label, '') self.__dict__[item] = t metainfo = self.torrent.metainfo rows = 4 if metainfo.announce_list is not None: rows += sum([len(l) for l in metainfo.announce_list]) - 1 self.torrent_fgsizer = LabelValueFlexGridSizer(self.panel, rows, 2, SPACING, SPACING) self.torrent_fgsizer.SetFlexibleDirection(wx.HORIZONTAL) self.torrent_fgsizer.AddGrowableCol(1) self.torrent_static_box = wx.StaticBox(self.panel, label=_("Torrent file:")) self.torrent_static_box_sizer = wx.StaticBoxSizer(self.torrent_static_box, wx.HORIZONTAL) self.torrent_static_box_sizer.Add(self.torrent_fgsizer, flag=wx.GROW|wx.ALL, border=SPACING, proportion=1) self.outer.Add(self.torrent_static_box_sizer, flag=wx.GROW, border=SPACING, proportion=1) # announce Singular Plural Backup, singular Backup, plural announce_labels = ((_("Tracker:"), _("Trackers:")), (_("Backup tracker:"), _("Backup trackers:"))) if metainfo.is_trackerless: self.torrent_fgsizer.add_pair(announce_labels[0][0], _("(trackerless torrent)")) else: if metainfo.announce_list is None: self.torrent_fgsizer.add_pair(announce_labels[0][0], metainfo.announce) else: for i, l in enumerate(metainfo.announce_list): label = announce_labels[i!=0][len(l)!=1] self.torrent_fgsizer.add_pair(label, l[0]) for t in l[1:]: self.torrent_fgsizer.add_pair('', t) # infohash self.torrent_fgsizer.add_pair(_("Infohash:"), repr(metainfo.infohash)) # pieces pl = metainfo.piece_length tl = metainfo.total_bytes count, lastlen = divmod(tl, pl) pieces = "%s x %d + %s" % (Size(pl), count, Size(lastlen)) self.torrent_fgsizer.add_pair(_("Pieces:"), pieces) self.piece_count = count + (lastlen > 0) # creation date time_str = time.asctime(time.localtime(metainfo.creation_date)) self.torrent_fgsizer.add_pair(_("Created on:"), time_str) self.panel.SetSizerAndFit(self.outer) self.scrolled_window.SetSizer(self.scroll_sizer) self.SetSizerAndFit(self.sizer) # this fixes background repaint issues on XP w/ themes def OnSize(event): self.Refresh() event.Skip() self.Bind(wx.EVT_SIZE, OnSize) def GetBestFittingSize(self): ssbs = self.swarm_static_box.GetBestFittingSize() tsbs = self.torrent_static_box.GetBestFittingSize() return wx.Size(max(ssbs.x, tsbs.x) + SPACING*4, ssbs.y + tsbs.y + SPACING*2) def update(self, statistics): tp = statistics.get('trackerPeers', None) ts = statistics.get('trackerSeeds', None) if tp is None: self.tracker_peers.SetLabel(_('Unknown')) elif (ts is None) or (ts == 0): self.tracker_peers.SetLabel('%s' % (str(tp),)) elif ts == tp: self.tracker_peers.SetLabel(_('%s (all seeds)') % (str(tp),)) elif ts == 1: self.tracker_peers.SetLabel(_('%s (%s seed)') % (str(tp), str(ts))) else: self.tracker_peers.SetLabel(_('%s (%s seeds)') % (str(tp), str(ts))) dc = statistics.get('distributed_copies', -1) if dc >= 0: dist_label = '%0.2f' % dc else: dist_label = '?' self.distributed.SetLabel(dist_label) self.discarded.SetLabel(unicode(Size(statistics.get('discarded',0)))) self.swarm_speed.SetLabel(unicode(Rate(statistics.get('swarm_speed',0)))) t = statistics.get('announceTime') if t is not None: self.announce.SetLabel(unicode(Duration(t*-1))) else: # TODO: None means the torrent is not initialized yet self.announce.SetLabel('')class TorrentInfoPanel(BTPanel): def __init__(self, parent, torrent, *a, **k): BTPanel.__init__(self, parent, *a, **k) self.parent = parent self.torrent = torrent metainfo = self.torrent.metainfo vspacing = SPACING hspacing = SPACING if os.name == 'nt': vspacing /= 2 hspacing *= 3 # title self.title_sizer = LabelValueFlexGridSizer(self, 1, 2, vspacing, SPACING) self.title_sizer.SetFlexibleDirection(wx.HORIZONTAL) self.title_sizer.AddGrowableCol(1) if metainfo.title is not None: self.title_sizer.add_pair(_("Torrent title:"), metainfo.title.replace('&', '&&')) else: self.title_sizer.add_pair(_("Torrent name:"), metainfo.name.replace('&', '&&')) self.Add(self.title_sizer, flag=wx.GROW|wx.ALL, border=SPACING) # dynamic info self.dynamic_sizer = LabelValueFlexGridSizer(self, 2, 4, vspacing, hspacing) self.dynamic_sizer.SetFlexibleDirection(wx.HORIZONTAL) self.dynamic_sizer.SetMinSize((375, -1)) self.dynamic_sizer.AddGrowableCol(1) self.dynamic_sizer.AddGrowableCol(3) self.download_rate = self.dynamic_sizer.add_pair(_("Download rate:"), '') self.upload_rate = self.dynamic_sizer.add_pair(_("Upload rate:"), '') self.time_remaining = self.dynamic_sizer.add_pair(_("Time remaining:"), '') self.peers = self.dynamic_sizer.add_pair(_("Peers:"), '') self.eta_inserted = True self.Add(self.dynamic_sizer, flag=wx.GROW|wx.ALL^wx.TOP, border=SPACING) self.piece_bar = FancyDownloadGauge(self, border=False, size=wx.Size(-1, -1), style=wx.SUNKEN_BORDER) self.Add(self.piece_bar, flag=wx.GROW|wx.ALL^wx.TOP, border=SPACING) # static info self.static_sizer = LabelValueFlexGridSizer(self, 2, 2, vspacing, hspacing) self.static_sizer.AddGrowableCol(1) # original filename fullpath = self.torrent.destination_path if fullpath is not None: self.static_sizer.add_pair(_("Save as:"), fullpath.replace('&', '&&'), dotify_value=True) # size size = Size(metainfo.total_bytes) num_files = _(", in one file") if metainfo.is_batch: num_files = _(", in %d files") % len(metainfo.sizes) self.static_sizer.add_pair(_("Total size:"), unicode(size)+num_files) self.Add(self.static_sizer, flag=wx.GROW|wx.ALL^wx.TOP, border=SPACING) def change_to_completed(self): # Remove various download stats. for i in (5,4,1,0): si = self.dynamic_sizer.GetItem(i) w = si.GetWindow() self.dynamic_sizer.Detach(i) w.Hide() w.Destroy() self.dynamic_sizer.Layout() self.GetParent().GetSizer().Layout() def change_label(self, stats, widget, key, renderer): ov = widget.GetLabel() nv = unicode(renderer(stats.get(key, None))) if ov != nv: widget.SetLabel(nv) return True return False def update(self, statistics): layout = False # set uprate if self.change_label(statistics, self.upload_rate, 'upRate', Rate): layout = True # set peers np = statistics.get('numPeers', 0) ns = statistics.get('numSeeds', 0) if ns == 0: nv = '%s' % (str(np),) elif ns == np: nv = _('%s (all seeds)') % (str(np),) elif ns == 1: nv = _('%s (%s seed)') % (str(np), str(ns))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -