class UnRARGUI: def __init__(self, parent): self.weak_parent = weakref.ref(parent) self.unrar = UnRAR() self.unrar_tab = UnRARTab(self.unrar, parent) self.tab_widget = QWidget() self.tab_widget.setLayout(self.unrar_tab) @property def parent(self): return self.weak_parent() def add_file(self, download_item): self.file_name = download_item.name if self.can_extract(): file_path = os.path.join(download_item.path, self.file_name) self.unrar.add(file_path, download_item.path, download_item.id) #file_path, dest_path self.add_tab() self.unrar_tab.store_items([download_item, ]) def add_tab(self): if self.parent.tab.indexOf(self.tab_widget) < 0: index_page = 2 self.parent.tab.insertTab(index_page, self.tab_widget, _('Extracting')) btn_close = QPushButton(self.parent) btn_close.setFixedHeight(12) btn_close.setFixedWidth(12) btn_close.clicked.connect(self.on_close_tab) self.parent.tab.tabBar().setTabButton(index_page, QTabBar.RightSide, btn_close) def on_close_tab(self): index_page = self.parent.tab.indexOf(self.tab_widget) if index_page >= 0: self.unrar_tab.clear_list() self.parent.tab.removeTab(index_page) def can_extract(self): pattern = '^(?P<name>.*?)(?P<part>\.part\d+)?(?P<ext>\.rar|\.r\d+)$' m = re.match(pattern, self.file_name) if m is not None: #is rar file if not self.has_segments_left(pattern, m.group('name')): if m.group('part') is not None: #new ext. style self.file_name = "".join((m.group('name'), ".part1.rar")) else: #single part or old ext. style self.file_name = "".join((m.group('name'), ".rar")) return True return False def has_segments_left(self, pattern, name): """""" for download_item in api.get_active_downloads().values() + api.get_queue_downloads().values() + api.get_stopped_downloads().values(): m = re.match(pattern, download_item.name) if m is not None: if name == m.group('name'): return True return False
class UnRARGUI: def __init__(self, parent): self.parent = parent self.unrar = UnRAR() self.unrar_tab = UnRARTab(self.unrar, self.parent) self.tab_widget = QWidget() self.tab_widget.setLayout(self.unrar_tab) def add_file(self, download_item): self.file_name = download_item.name if self.can_extract(): file_path = os.path.join(download_item.path, self.file_name) self.unrar.add(file_path, download_item.path, download_item.id) #file_path, dest_path #todo: check if tab is in the app, add it, add item. Check if tab doesnt get destroyed. #add it to queue, doesnt matter if we r extracting or not. #self.add_msg_box(th_id) self.add_tab() self.unrar_tab.store_items([ download_item, ]) def add_tab(self): if self.parent.tab.indexOf(self.tab_widget) < 0: index_page = 2 self.parent.tab.insertTab(index_page, self.tab_widget, _('Extracting')) btn_close = QPushButton(self.parent) #btn_close.setIcon(QIcon('stop.png')) #btn_close.setIconSize(QSize(10, 10)) btn_close.setFixedHeight(12) btn_close.setFixedWidth(12) #btn_close.setFlat(True) btn_close.clicked.connect(self.on_close_tab) self.parent.tab.tabBar().setTabButton(index_page, QTabBar.RightSide, btn_close) # #last_index = self.tab.count() - 1 #self.parent.tab.setCurrentIndex(last_index) def on_close_tab(self): index_page = self.parent.tab.indexOf(self.tab_widget) if index_page >= 0: self.parent.tab.removeTab(index_page) def can_extract(self): NAME, PART, EXT = range(3) pattern = "^(.*?)(\.part\d+)?(\.rar|\.r\d+)$" #capture name m = re.match(pattern, self.file_name) if m is not None: #is rar file m_tuple = m.groups() if not self.has_segments_left(m_tuple[NAME], pattern): #not segment left. if m_tuple[PART] is not None: #new ext. style self.file_name = "".join((m_tuple[NAME], ".part1.rar")) else: #single part or old ext. style self.file_name = "".join((m_tuple[NAME], ".rar")) return True return False def has_segments_left(self, name, pattern): """""" NAME, PART, EXT = range(3) for download_item in api.get_active_downloads().values( ) + api.get_queue_downloads().values() + api.get_stopped_downloads( ).values(): match_name = re.match(pattern, download_item.name) if match_name is not None: if name == match_name.groups()[NAME]: return True return False
class UnRARGUI: def __init__(self, parent): self.parent = parent self.unrar = UnRAR() self.unrar_tab = UnRARTab(self.unrar, self.parent) self.tab_widget = QWidget() self.tab_widget.setLayout(self.unrar_tab) def add_file(self, download_item): self.file_name = download_item.name if self.can_extract(): file_path = os.path.join(download_item.path, self.file_name) self.unrar.add(file_path, download_item.path, download_item.id) #file_path, dest_path #todo: check if tab is in the app, add it, add item. Check if tab doesnt get destroyed. #add it to queue, doesnt matter if we r extracting or not. #self.add_msg_box(th_id) self.add_tab() self.unrar_tab.store_items([download_item, ]) def add_tab(self): if self.parent.tab.indexOf(self.tab_widget) < 0: index_page = 2 self.parent.tab.insertTab(index_page, self.tab_widget, _('Extracting')) btn_close = QPushButton(self.parent) #btn_close.setIcon(QIcon('stop.png')) #btn_close.setIconSize(QSize(10, 10)) btn_close.setFixedHeight(12) btn_close.setFixedWidth(12) #btn_close.setFlat(True) btn_close.clicked.connect(self.on_close_tab) self.parent.tab.tabBar().setTabButton(index_page, QTabBar.RightSide, btn_close) # #last_index = self.tab.count() - 1 #self.parent.tab.setCurrentIndex(last_index) def on_close_tab(self): index_page = self.parent.tab.indexOf(self.tab_widget) if index_page >= 0: self.parent.tab.removeTab(index_page) def can_extract(self): NAME, PART, EXT = range(3) pattern = "^(.*?)(\.part\d+)?(\.rar|\.r\d+)$" #capture name m = re.match(pattern, self.file_name) if m is not None: #is rar file m_tuple = m.groups() if not self.has_segments_left(m_tuple[NAME], pattern): #not segment left. if m_tuple[PART] is not None: #new ext. style self.file_name = "".join((m_tuple[NAME], ".part1.rar")) else: #single part or old ext. style self.file_name = "".join((m_tuple[NAME], ".rar")) return True return False def has_segments_left(self, name, pattern): """""" NAME, PART, EXT = range(3) for download_item in api.get_active_downloads().values() + api.get_queue_downloads().values() + api.get_stopped_downloads().values(): match_name = re.match(pattern, download_item.name) if match_name is not None: if name == match_name.groups()[NAME]: return True return False
class UnRARGUI: def __init__(self, parent): self.weak_parent = weakref.ref(parent) self.unrar = UnRAR() self.unrar_tab = UnRARTab(self.unrar, parent) self.tab_widget = QWidget() self.tab_widget.setLayout(self.unrar_tab) @property def parent(self): return self.weak_parent() def add_file(self, download_item): file_name = self.get_first_volume_name(download_item.name) if file_name is not None and self.can_extract(download_item.path, file_name): file_path = os.path.join(download_item.path, file_name) self.unrar.add(file_path, download_item.path, download_item.id) #file_path, dest_path self.add_tab() self.unrar_tab.store_items([download_item, ]) def add_tab(self): if self.parent.tab.indexOf(self.tab_widget) < 0: index_page = 2 self.parent.tab.insertTab(index_page, self.tab_widget, _('Extracting')) self.btn_close = QPushButton() self.btn_close.setFixedHeight(12) self.btn_close.setFixedWidth(12) self.btn_close.clicked.connect(self.on_close_tab) self.parent.tab.tabBar().setTabButton(index_page, QTabBar.RightSide, self.btn_close) def on_close_tab(self): index_page = self.parent.tab.indexOf(self.tab_widget) if index_page >= 0: self.unrar_tab.clear_list() self.parent.tab.removeTab(index_page) def can_extract(self, path, file_name): m = re.match(unrar.RAR_FILE_PATTERN, file_name) if m is not None: #is rar file if os.path.isfile(os.path.join(path, file_name)) and not self.has_segments_left(m.group('name')): return True return False def get_first_volume_name(self, file_name): m = re.match(unrar.RAR_FILE_PATTERN, file_name) if m is not None: # is rar file if m.group('part') is not None: # new ext. style if len(m.group('part')) == 6: # .partX return "".join((m.group('name'), ".part1.rar")) else: # .partXX return "".join((m.group('name'), ".part01.rar")) else: # single part or old ext. style return "".join((m.group('name'), ".rar")) else: return def has_segments_left(self, name): """""" for download_item in api.get_active_downloads().values() + api.get_queue_downloads().values() + api.get_stopped_downloads().values(): m = re.match(unrar.RAR_FILE_PATTERN, download_item.name) if m is not None: if name == m.group('name'): return True return False
class UnRARGUI: """""" def __init__(self, event, download_item, parent, widgets_list): """""" self.download_item = download_item self.parent = parent self.event = event self.widgets_list = widgets_list self.hbox = gtk.HBox() self.unrar = UnRAR() self.is_extracting = False dest_path = self.download_item.path NAME, PART, EXT = range(3) pattern = "^(.*?)(\.part\d+)?(\.rar|\.r\d+)$" #capture name m = re.match(pattern, self.download_item.name) if m is not None: #is rar file m_tuple = m.groups() if not self.has_segments_left(m_tuple[NAME], pattern): #not segment left. if m_tuple[PART] is not None: #new ext. style file_name = "".join((m_tuple[NAME], ".part1.rar")) else: #single part or old ext. style file_name = "".join((m_tuple[NAME], ".rar")) file_path = os.path.join(self.download_item.path, file_name) th_id = self.unrar.add(file_path, dest_path) self.add_msg_box(th_id) self.is_extracting = True def add_msg_box(self, th_id): """""" [frame.hide_all() for frame in self.widgets_list] frame = gtk.Frame(label=None) halign1 = gtk.Alignment(0, 0, 0, 0) #left halign2 = gtk.Alignment(1, 0, 0, 0) #left hbox = gtk.HBox(False, 20) label = gtk.Label(_("Extracting:") + " " + self.download_item.name) hbox.pack_start(label) status_label = gtk.Label("(" + _("Running") + ")") hbox.pack_start(status_label) halign1.add(hbox) #get a stock close button image close_image = gtk.image_new_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU) image_w, image_h = gtk.icon_size_lookup(gtk.ICON_SIZE_MENU) #make the close button btn = gtk.Button() btn.set_relief(gtk.RELIEF_NONE) btn.set_focus_on_click(False) btn.add(close_image) btn.set_size_request(image_w + 4, image_h + 4) #this reduces the size of the button style = gtk.RcStyle() style.xthickness = 0 style.ythickness = 0 btn.modify_style(style) btn.connect('clicked', self.on_close_button, frame) halign2.add(btn) hbox3 = gtk.HBox() hbox3.pack_start(halign1) hbox3.pack_start(halign2) frame.add(hbox3) self.widgets_list.append(frame) self.parent.vbox2.pack_start(frame, False, False) frame.show_all() btn.hide() #close btn gobject.timeout_add(1000, self.update_status, th_id, frame, btn, status_label) def update_status(self, th_id, frame, close_btn, status_label): """""" status_tuple = self.unrar.get_status(th_id) if status_tuple is not None: if status_tuple[COMPLETE]: if status_tuple[ERR_MSG] is None: self.widgets_list.remove(frame) frame.hide_all() frame.destroy() if self.widgets_list: self.widgets_list[0].show_all() else: status_label.set_text("(" + status_tuple[ERR_MSG] + ")") close_btn.show() #show close button self.event.set() return False return True def on_close_button(self, widget, frame): """""" self.widgets_list.remove(frame) frame.hide_all() frame.destroy() if self.widgets_list: frame = self.widgets_list[0] frame.show_all() def has_segments_left(self, name, pattern): """""" NAME, PART, EXT = range(3) for download_item in api.get_active_downloads().values( ) + api.get_queue_downloads().values() + api.get_stopped_downloads( ).values(): match_name = re.match(pattern, download_item.name) if match_name is not None: if name == match_name.groups()[NAME]: return True return False
class UnRARGUI: def __init__(self, parent): self.weak_parent = weakref.ref(parent) self.unrar = UnRAR() self.unrar_tab = UnRARTab(self.unrar, parent) self.tab_widget = QWidget() self.tab_widget.setLayout(self.unrar_tab) @property def parent(self): return self.weak_parent() def add_file(self, download_item): file_name = self.get_first_volume_name(download_item.name) if file_name is not None and self.can_extract(download_item.path, file_name): file_path = os.path.join(download_item.path, file_name) self.unrar.add(file_path, download_item.path, download_item.id) #file_path, dest_path self.add_tab() self.unrar_tab.store_items([ download_item, ]) def add_tab(self): if self.parent.tab.indexOf(self.tab_widget) < 0: index_page = 2 self.parent.tab.insertTab(index_page, self.tab_widget, _('Extracting')) self.btn_close = QPushButton() self.btn_close.setFixedHeight(12) self.btn_close.setFixedWidth(12) self.btn_close.clicked.connect(self.on_close_tab) self.parent.tab.tabBar().setTabButton(index_page, QTabBar.RightSide, self.btn_close) def on_close_tab(self): index_page = self.parent.tab.indexOf(self.tab_widget) if index_page >= 0: self.unrar_tab.clear_list() self.parent.tab.removeTab(index_page) def can_extract(self, path, file_name): m = re.match(unrar.RAR_FILE_PATTERN, file_name) if m is not None: #is rar file if os.path.isfile(os.path.join( path, file_name)) and not self.has_segments_left( m.group('name')): return True return False def get_first_volume_name(self, file_name): m = re.match(unrar.RAR_FILE_PATTERN, file_name) if m is not None: # is rar file if m.group('part') is not None: # new ext. style if len(m.group('part')) == 6: # .partX return "".join((m.group('name'), ".part1.rar")) else: # .partXX return "".join((m.group('name'), ".part01.rar")) else: # single part or old ext. style return "".join((m.group('name'), ".rar")) else: return def has_segments_left(self, name): """""" for download_item in api.get_active_downloads().values( ) + api.get_queue_downloads().values() + api.get_stopped_downloads( ).values(): m = re.match(unrar.RAR_FILE_PATTERN, download_item.name) if m is not None: if name == m.group('name'): return True return False
class UnRARGUI: """""" def __init__(self, event, download_item, parent, widgets_list): """""" self.download_item = download_item self.parent = parent self.event = event self.widgets_list = widgets_list self.hbox = gtk.HBox() self.unrar = UnRAR() self.is_extracting = False dest_path = self.download_item.path NAME, PART, EXT = range(3) pattern = "^(.*?)(\.part\d+)?(\.rar|\.r\d+)$" #capture name m = re.match(pattern, self.download_item.name) if m is not None: #is rar file m_tuple = m.groups() if not self.has_segments_left(m_tuple[NAME], pattern): #not segment left. if m_tuple[PART] is not None: #new ext. style file_name = "".join((m_tuple[NAME], ".part1.rar")) else: #single part or old ext. style file_name = "".join((m_tuple[NAME], ".rar")) file_path = os.path.join(self.download_item.path, file_name) th_id = self.unrar.add(file_path, dest_path) self.add_msg_box(th_id) self.is_extracting = True def add_msg_box(self, th_id): """""" [frame.hide_all() for frame in self.widgets_list] frame = gtk.Frame(label=None) halign1 = gtk.Alignment(0, 0, 0, 0) #left halign2 = gtk.Alignment(1, 0, 0, 0) #left hbox = gtk.HBox(False, 20) label = gtk.Label(_("Extracting:") + " " + self.download_item.name) hbox.pack_start(label) status_label = gtk.Label("(" + _("Running") + ")") hbox.pack_start(status_label) halign1.add(hbox) #get a stock close button image close_image = gtk.image_new_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU) image_w, image_h = gtk.icon_size_lookup(gtk.ICON_SIZE_MENU) #make the close button btn = gtk.Button() btn.set_relief(gtk.RELIEF_NONE) btn.set_focus_on_click(False) btn.add(close_image) btn.set_size_request(image_w + 4, image_h + 4) #this reduces the size of the button style = gtk.RcStyle() style.xthickness = 0 style.ythickness = 0 btn.modify_style(style) btn.connect('clicked', self.on_close_button, frame) halign2.add(btn) hbox3 = gtk.HBox() hbox3.pack_start(halign1) hbox3.pack_start(halign2) frame.add(hbox3) self.widgets_list.append(frame) self.parent.vbox2.pack_start(frame, False, False) frame.show_all() btn.hide() #close btn gobject.timeout_add(1000, self.update_status, th_id, frame, btn, status_label) def update_status(self, th_id, frame, close_btn, status_label): """""" status_tuple = self.unrar.get_status(th_id) if status_tuple is not None: if status_tuple[COMPLETE]: if status_tuple[ERR_MSG] is None: self.widgets_list.remove(frame) frame.hide_all() frame.destroy() if self.widgets_list: self.widgets_list[0].show_all() else: status_label.set_text("(" + status_tuple[ERR_MSG] + ")") close_btn.show() #show close button self.event.set() return False return True def on_close_button(self, widget, frame): """""" self.widgets_list.remove(frame) frame.hide_all() frame.destroy() if self.widgets_list: frame = self.widgets_list[0] frame.show_all() def has_segments_left(self, name, pattern): """""" NAME, PART, EXT = range(3) for download_item in api.get_active_downloads().values() + api.get_queue_downloads().values() + api.get_stopped_downloads().values(): match_name = re.match(pattern, download_item.name) if match_name is not None: if name == match_name.groups()[NAME]: return True return False