Exemple #1
0
class Gui(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        
        self.setWindowTitle(cons.APP_TITLE)
        self.setWindowIcon(QIcon(os.path.join(cons.MEDIA_PATH, "misc", "ochd.ico")))
        self.resize(MIN_WIDTH, MIN_HEIGHT)
        self.center()
        
        self.restore_wnd_geometry()

        self.downloads = Downloads(self)
        self.add_downloads = AddDownloads(self)
        self.log = Log(self)
        
        self.stop = (QToolButton(), media.get_icon(media.STOP, media.MEDIUM), _('Stop Download'), self.on_stop_download, False)
        self.start = (QToolButton(), media.get_icon(media.START, media.MEDIUM), _('Start Download'), self.on_start_download, False)
        self.accounts = (QToolButton(), media.get_icon(media.ACCOUNTS, media.MEDIUM), _('Accounts'), self.on_accounts, True)
        self.preferences = (QToolButton(), media.get_icon(media.PREFERENCES, media.MEDIUM), _('Preferences'), self.on_preferences, True)
        self.about = (QToolButton(), media.get_icon(media.ABOUT, media.MEDIUM), _('About'), self.on_about, True)
        
        self.menu = QMenu()
        self.preferences[BTN].setPopupMode(QToolButton.MenuButtonPopup)
        self.preferences[BTN].setMenu(self.menu)
        
        self.toolbar = Toolbar(self, [self.start, self.stop, None, self.accounts, self.preferences, None, self.about])

        self.addToolBar(self.toolbar)

        #tabs
        self.previous_tab = None
        self.tab = QTabWidget()
        #
        self.tab.addTab(self.downloads, _('Downloads'))
        #
        self.tab_add_downloads = QWidget()
        self.tab_add_downloads.setLayout(self.add_downloads)
        self.tab.addTab(self.tab_add_downloads, _('Add downloads'))
        #
        #addons
        self.addons_manager = AddonsManager(self)
        self.addons_list = self.addons_manager.addons_list
        #...tabs
        self.addon_tab_widgets = []
        self.load_addon_tabs()
        #
        self.tab_log = QWidget()
        self.tab_log.setLayout(self.log)
        self.tab.addTab(self.tab_log, _('Log'))
        #
        self.preferences = Preferences(self.addons_list)
        #self.tab.addTab(self.preferences, 'Preferences')
        #
        self.tab.currentChanged.connect(self.on_tab_switch)
        #
        self.setCentralWidget(self.tab)

        #status bar
        self.status_bar = StatusBar(self)
        self.setStatusBar(self.status_bar)

        #drop down menu
        [addon.set_menu_item() for addon in self.addons_list]
        #self.menu.addSeparator()
        #self.menu.addAction('Preferences', self.on_preferences)

        #system tray icon
        self.tray = Tray(self)
        self.show_or_hide_tray()

        #load session.
        self.load_session()

        #add core's event loop
        self.dispatcher = ThreadDispatcher(self)
        self.dispatcher.start()

        #quit event
        events.quit.connect(self.event_close)

        #custom qt signals
        signals.switch_tab.connect(self.switch_tab)
        signals.show_or_hide_tray.connect(self.show_or_hide_tray)

        self.show()

    def customEvent(self, event):
        #process idle_queue_dispacher events
        event.callback()

    def switch_tab(self, index):
        self.tab.setCurrentIndex(index)

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())
    
    def restore_wnd_geometry(self):
        wx, wy, ww, wh = QDesktopWidget().availableGeometry().getRect()
        x, y, w, h = conf.get_window_settings()
        if ww <= w or wh <= h:
            self.showMaximized()
        elif ww > x >= 0 and wh > y >= 0: #resize and move
            self.setGeometry(x, y, w, h)
        else: #resize only
            self.resize(w, h)

    def on_stop_download(self):
        rows = self.downloads.get_selected_rows()
        if rows:
            for row in rows:
                items = self.downloads.items
                id_item = items[row][0]
                stopped = api.stop_download(id_item)
                if stopped:
                    if items[row][1] == self.downloads.icons_dict[cons.STATUS_QUEUE]:
                        items[row][1] = self.downloads.icons_dict[cons.STATUS_STOPPED]
                    self.stop[BTN].setEnabled(False)
                    self.start[BTN].setEnabled(True)

    def on_start_download(self):
        rows = self.downloads.get_selected_rows()
        if rows:
            for row in rows:
                items = self.downloads.items
                id_item = items[row][0]
                started = api.start_download(id_item)
                if started:
                    items[row][1] = self.downloads.icons_dict[cons.STATUS_QUEUE] #status
                    items[row][10] = None #status_msg
                    self.stop[BTN].setEnabled(True)
                    self.start[BTN].setEnabled(False)
            id_item_list = [row[0] for row in items]
            api.reorder_queue(id_item_list)
    
    def on_accounts(self):
        accounts = ConfigAccounts(self)
    
    def on_preferences(self):
        index_page = self.tab.indexOf(self.preferences) #get the page containig the widget
        if index_page >= 0: #if = -1 there is not such tab.
            self.tab.setCurrentIndex(index_page)
        else:
            index_page = self.tab.addTab(self.preferences, _('Preferences'))
            btn_close = QPushButton(self)
            #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_preferences)
            self.tab.tabBar().setTabButton(index_page, QTabBar.RightSide, btn_close)
            #
            last_index = self.tab.count() - 1
            self.tab.setCurrentIndex(last_index)
    
    def on_close_preferences(self):
        index_page = self.tab.indexOf(self.preferences)
        if index_page >= 0:
            self.tab.removeTab(index_page)
        #self.tab.setCurrentIndex(0)
    
    def on_about(self):
        about = About(self)
    
    def on_tab_switch(self, index):
        current_widget = self.tab.currentWidget()
        if current_widget == self.tab_log:
            self.log.on_load()
        elif current_widget in self.addon_tab_widgets:
            tab = current_widget.layout()
            tab.on_load()

        if self.previous_tab == self.preferences:
            self.previous_tab.on_close()
        elif self.previous_tab in self.addon_tab_widgets:
            tab = self.previous_tab.layout()
            tab.on_close()
        self.previous_tab = current_widget

    def show_or_hide_tray(self):
        if self.tray.is_available() and conf.get_tray_available():
            self.tray.show()
        else:
            self.tray.hide()

    def load_addon_tabs(self):
        for tab, addon in [(addon.get_tab(), addon) for addon in self.addons_list]:
            if tab is not None:
                tab_widget = QWidget()
                tab_widget.setLayout(tab)
                self.tab.addTab(tab_widget, addon.name)
                self.addon_tab_widgets.append(tab_widget)

    def load_session(self):
        """"""
        download_items = api.load_session()
        for download_item in download_items:
            self.downloads.store_item(download_item)
    
    def save_session(self):
        """"""
        id_item_list = [row[0] for row in self.downloads.items]
        api.save_session(id_item_list)
    
    def idle_timeout(self, interval, func):
        timer = QTimer()
        timer.timeout.connect(func)
        timer.start(interval)
        return timer

    def event_close(self):
        self.tray.hide()
        self.close()

    def closeEvent(self, event):
        # overridden
        if not self.tray.isVisible():
            x, y, w, h = self.geometry().getRect()
            self.hide()
            self.save_session()
            self.preferences.on_close()
            self.dispatcher.stop()
            conf.set_window_settings(x, y, w, h)
            conf.save()
            accounts_manager.save()
            event.accept()
        else:  # hide only
            self.hide()
            event.ignore()
Exemple #2
0
class Gui(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setWindowTitle(cons.APP_TITLE)
        self.setWindowIcon(
            QIcon(os.path.join(cons.MEDIA_PATH, "misc", "ochd.ico")))
        self.resize(MIN_WIDTH, MIN_HEIGHT)
        self.center()

        self.restore_wnd_geometry()

        self.downloads = Downloads(self)
        self.add_downloads = AddDownloads(self)
        self.log = Log(self)

        self.stop = (QToolButton(), media.get_icon(media.STOP, media.MEDIUM),
                     _('Stop Download'), self.on_stop_download, False)
        self.start = (QToolButton(), media.get_icon(media.START, media.MEDIUM),
                      _('Start Download'), self.on_start_download, False)
        self.accounts = (QToolButton(),
                         media.get_icon(media.ACCOUNTS, media.MEDIUM),
                         _('Accounts'), self.on_accounts, True)
        self.preferences = (QToolButton(),
                            media.get_icon(media.PREFERENCES, media.MEDIUM),
                            _('Preferences'), self.on_preferences, True)
        self.about = (QToolButton(), media.get_icon(media.ABOUT, media.MEDIUM),
                      _('About'), self.on_about, True)

        self.menu = QMenu()
        self.preferences[BTN].setPopupMode(QToolButton.MenuButtonPopup)
        self.preferences[BTN].setMenu(self.menu)

        self.toolbar = Toolbar(self, [
            self.start, self.stop, None, self.accounts, self.preferences, None,
            self.about
        ])

        self.addToolBar(self.toolbar)

        #tabs
        self.previous_tab = None
        self.tab = QTabWidget()
        #
        self.tab.addTab(self.downloads, _('Downloads'))
        #
        self.tab_add_downloads = QWidget()
        self.tab_add_downloads.setLayout(self.add_downloads)
        self.tab.addTab(self.tab_add_downloads, _('Add downloads'))
        #
        #addons
        self.addons_manager = AddonsManager(self)
        self.addons_list = self.addons_manager.addons_list
        #...tabs
        self.addon_tab_widgets = []
        self.load_addon_tabs()
        #
        self.tab_log = QWidget()
        self.tab_log.setLayout(self.log)
        self.tab.addTab(self.tab_log, _('Log'))
        #
        self.preferences = Preferences(self.addons_list)
        #self.tab.addTab(self.preferences, 'Preferences')
        #
        self.tab.currentChanged.connect(self.on_tab_switch)
        #
        self.setCentralWidget(self.tab)

        #status bar
        self.status_bar = StatusBar(self)
        self.setStatusBar(self.status_bar)

        #drop down menu
        [addon.set_menu_item() for addon in self.addons_list]
        #self.menu.addSeparator()
        #self.menu.addAction('Preferences', self.on_preferences)

        #system tray icon
        self.tray = Tray(self)
        self.show_or_hide_tray()

        #load session.
        self.load_session()

        #add core's event loop
        self.dispatcher = ThreadDispatcher(self)
        self.dispatcher.start()

        #quit event
        events.quit.connect(self.event_close)

        #custom qt signals
        signals.switch_tab.connect(self.switch_tab)
        signals.show_or_hide_tray.connect(self.show_or_hide_tray)

        self.show()

    def customEvent(self, event):
        #process idle_queue_dispacher events
        event.callback()

    def switch_tab(self, index):
        self.tab.setCurrentIndex(index)

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def restore_wnd_geometry(self):
        wx, wy, ww, wh = QDesktopWidget().availableGeometry().getRect()
        x, y, w, h = conf.get_window_settings()
        if ww <= w or wh <= h:
            self.showMaximized()
        elif ww > x >= 0 and wh > y >= 0:  #resize and move
            self.setGeometry(x, y, w, h)
        else:  #resize only
            self.resize(w, h)

    def on_stop_download(self):
        rows = self.downloads.get_selected_rows()
        if rows:
            for row in rows:
                items = self.downloads.items
                id_item = items[row][0]
                stopped = api.stop_download(id_item)
                if stopped:
                    if items[row][1] == self.downloads.icons_dict[
                            cons.STATUS_QUEUE]:
                        items[row][1] = self.downloads.icons_dict[
                            cons.STATUS_STOPPED]
                    self.stop[BTN].setEnabled(False)
                    self.start[BTN].setEnabled(True)

    def on_start_download(self):
        rows = self.downloads.get_selected_rows()
        if rows:
            for row in rows:
                items = self.downloads.items
                id_item = items[row][0]
                started = api.start_download(id_item)
                if started:
                    items[row][1] = self.downloads.icons_dict[
                        cons.STATUS_QUEUE]  #status
                    items[row][10] = None  #status_msg
                    self.stop[BTN].setEnabled(True)
                    self.start[BTN].setEnabled(False)
            id_item_list = [row[0] for row in items]
            api.reorder_queue(id_item_list)

    def on_accounts(self):
        accounts = ConfigAccounts(self)

    def on_preferences(self):
        index_page = self.tab.indexOf(
            self.preferences)  #get the page containig the widget
        if index_page >= 0:  #if = -1 there is not such tab.
            self.tab.setCurrentIndex(index_page)
        else:
            index_page = self.tab.addTab(self.preferences, _('Preferences'))
            btn_close = QPushButton(self)
            #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_preferences)
            self.tab.tabBar().setTabButton(index_page, QTabBar.RightSide,
                                           btn_close)
            #
            last_index = self.tab.count() - 1
            self.tab.setCurrentIndex(last_index)

    def on_close_preferences(self):
        index_page = self.tab.indexOf(self.preferences)
        if index_page >= 0:
            self.tab.removeTab(index_page)
        #self.tab.setCurrentIndex(0)

    def on_about(self):
        about = About(self)

    def on_tab_switch(self, index):
        current_widget = self.tab.currentWidget()
        if current_widget == self.tab_log:
            self.log.on_load()
        elif current_widget in self.addon_tab_widgets:
            tab = current_widget.layout()
            tab.on_load()

        if self.previous_tab == self.preferences:
            self.previous_tab.on_close()
        elif self.previous_tab in self.addon_tab_widgets:
            tab = self.previous_tab.layout()
            tab.on_close()
        self.previous_tab = current_widget

    def show_or_hide_tray(self):
        if self.tray.is_available() and conf.get_tray_available():
            self.tray.show()
        else:
            self.tray.hide()

    def load_addon_tabs(self):
        for tab, addon in [(addon.get_tab(), addon)
                           for addon in self.addons_list]:
            if tab is not None:
                tab_widget = QWidget()
                tab_widget.setLayout(tab)
                self.tab.addTab(tab_widget, addon.name)
                self.addon_tab_widgets.append(tab_widget)

    def load_session(self):
        """"""
        download_items = api.load_session()
        for download_item in download_items:
            self.downloads.store_item(download_item)

    def save_session(self):
        """"""
        id_item_list = [row[0] for row in self.downloads.items]
        api.save_session(id_item_list)

    def idle_timeout(self, interval, func):
        timer = QTimer()
        timer.timeout.connect(func)
        timer.start(interval)
        return timer

    def event_close(self):
        self.tray.hide()
        self.close()

    def closeEvent(self, event):
        # overridden
        if not self.tray.isVisible():
            x, y, w, h = self.geometry().getRect()
            self.hide()
            self.save_session()
            self.preferences.on_close()
            self.dispatcher.stop()
            conf.set_window_settings(x, y, w, h)
            conf.save()
            accounts_manager.save()
            event.accept()
        else:  # hide only
            self.hide()
            event.ignore()
Exemple #3
0
class Gui(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setWindowTitle(cons.APP_TITLE)
        self.setWindowIcon(
            QIcon(os.path.join(cons.MEDIA_PATH, "misc", "ochd.ico")))
        self.resize(MIN_WIDTH, MIN_HEIGHT)
        self.center()

        self.restore_wnd_geometry()

        self.stop = (QToolButton(), media.get_icon(media.STOP, media.MEDIUM),
                     _('Stop Download'), self.on_stop_download, False)
        self.start = (QToolButton(), media.get_icon(media.START, media.MEDIUM),
                      _('Start Download'), self.on_start_download, False)
        accounts = (QToolButton(), media.get_icon(media.ACCOUNTS,
                                                  media.MEDIUM), _('Accounts'),
                    self.on_accounts, True)
        preferences = (QToolButton(),
                       media.get_icon(media.PREFERENCES, media.MEDIUM),
                       _('Preferences'), self.on_preferences, True)
        about = (QToolButton(), media.get_icon(media.ABOUT, media.MEDIUM),
                 _('About'), self.on_about, True)

        self.menu = QMenu()
        preferences[BTN].setPopupMode(QToolButton.MenuButtonPopup)
        preferences[BTN].setMenu(self.menu)

        toolbar = Toolbar(
            self,
            [self.start, self.stop, None, accounts, preferences, None, about])

        self.toolbar = self.addToolBar(toolbar)

        #self.vbox = QVBoxLayout(self)

        #tabs
        self.previous_tab = None
        self.tab = QTabWidget(self)
        #
        self.downloads = Downloads(self)
        self.tab.addTab(self.downloads, _('Downloads'))
        #
        self.add_downloads = AddDownloads(self.downloads, self)
        self.tab_add_downloads = QWidget()
        self.tab_add_downloads.setLayout(self.add_downloads)
        self.tab.addTab(self.tab_add_downloads, _('Add downloads'))
        #
        #addons
        self.addons_manager = AddonsManager(self)
        self.addons_list = self.addons_manager.addons_list
        #...tabs
        self.addon_tab_widgets = []
        self.load_addon_tabs()
        #
        self.log = Log()
        self.tab_log = QWidget()
        self.tab_log.setLayout(self.log)
        self.tab.addTab(self.tab_log, _('Log'))
        #
        self.preferences = Preferences(self.addons_list, self)
        #self.tab.addTab(self.preferences, 'Preferences')
        #
        self.tab.currentChanged.connect(self.on_tab_switch)
        #
        self.setCentralWidget(self.tab)

        #status bar
        self.status_bar = StatusBar(self)
        self.setStatusBar(self.status_bar)

        #drop down menu
        [addon.set_menu_item() for addon in self.addons_list]
        #self.menu.addSeparator()
        #self.menu.addAction('Preferences', self.on_preferences)

        #system tray icon
        self.tray = Tray(self)
        if self.tray.available:
            self.can_close = False
        else:
            self.can_close = True

        #on select button state
        self.downloads.selectionModel().selectionChanged.connect(
            self.on_selected)

        #load session.
        self.load_session()

        #add core's event loop
        self.idle_timeout(500, self.queue_loop)

        #quit event
        events.connect(cons.EVENT_QUIT, self.event_close)

        self.show()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def restore_wnd_geometry(self):
        wx, wy, ww, wh = QDesktopWidget().availableGeometry().getRect()
        x, y, w, h = conf.get_window_settings()
        if ww <= w or wh <= h:
            self.showMaximized()
        elif ww > x >= 0 and wh > y >= 0:  #resize and move
            self.setGeometry(x, y, w, h)
        else:  #resize only
            self.resize(w, h)

    def on_stop_download(self):
        rows = self.downloads.get_selected_rows()
        if rows:
            for row in rows:
                items = self.downloads.items
                id_item = items[row][0]
                stopped = api.stop_download(id_item)  #return true or false
                if stopped:
                    if items[row][1] == self.downloads.icons_dict[
                            cons.STATUS_QUEUE]:
                        items[row][1] = self.downloads.icons_dict[
                            cons.STATUS_STOPPED]
                    self.stop[BTN].setEnabled(
                        False
                    )  #deshabilitar el boton de stop ya que acaban de detener la descarga.
                    self.start[BTN].setEnabled(True)

    def on_start_download(self):
        rows = self.downloads.get_selected_rows()
        if rows:
            #id_item = model[row][0]
            for row in rows:
                items = self.downloads.items
                id_item = items[row][0]

                #TODO: Implementar lo mismo pero para stopped (buscar en lista stopped y finished para comparar)
                started = api.start_download(id_item)  #return true or false
                if started:
                    items[row][1] = self.downloads.icons_dict[
                        cons.STATUS_QUEUE]  #status
                    items[row][10] = None  #status_msg
                    self.stop[BTN].setEnabled(
                        True
                    )  #deshabilitar el boton de stop ya que acaban de detener la descarga.
                    self.start[BTN].setEnabled(False)

            #self.downloads.get_status() #iniciar update de lista.
            id_item_list = [row[0] for row in items]
            api.reorder_queue(id_item_list)

    def on_accounts(self):
        accounts = ConfigAccounts(self)

    def on_preferences(self):
        index_page = self.tab.indexOf(
            self.preferences)  #get the page containig the widget
        if index_page >= 0:  #if = -1 there is not such tab.
            self.tab.setCurrentIndex(index_page)
        else:
            index_page = self.tab.addTab(self.preferences, _('Preferences'))
            btn_close = QPushButton(self)
            #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_preferences)
            self.tab.tabBar().setTabButton(index_page, QTabBar.RightSide,
                                           btn_close)
            #
            last_index = self.tab.count() - 1
            self.tab.setCurrentIndex(last_index)

    def on_close_preferences(self):
        index_page = self.tab.indexOf(self.preferences)
        if index_page >= 0:
            self.tab.removeTab(index_page)

    def on_about(self):
        about = About(self)

    def on_tab_switch(self, index):
        current_widget = self.tab.currentWidget()
        if current_widget == self.tab_log:
            self.log.on_load()
        elif current_widget in self.addon_tab_widgets:
            tab = current_widget.layout()
            tab.on_load()

        if self.previous_tab == self.preferences:
            self.previous_tab.on_close()
        elif self.previous_tab in self.addon_tab_widgets:
            tab = self.previous_tab.layout()
            tab.on_close()
        self.previous_tab = current_widget

    def on_selected(self, current, previous):
        rows = self.downloads.get_selected_rows()

        self.stop[BTN].setEnabled(False)
        self.start[BTN].setEnabled(False)

        if len(rows) == 1:  #single selection.
            items = self.downloads.items
            row = rows[0]
            id_item = items[row][0]
            self.stop[BTN].setEnabled(True)
            self.start[BTN].setEnabled(False)
            stopped_downloads = api.get_stopped_downloads()
            try:
                item = stopped_downloads[id_item]
                self.stop[BTN].setEnabled(False)
                self.start[BTN].setEnabled(True)
            except KeyError:
                pass
        elif rows:  #multi selection
            self.stop[BTN].setEnabled(True)
            self.start[BTN].setEnabled(True)

    def load_addon_tabs(self):
        for tab, addon in [(addon.get_tab(), addon)
                           for addon in self.addons_list]:
            if tab is not None:
                tab_widget = QWidget()
                tab_widget.setLayout(tab)
                self.tab.addTab(tab_widget, addon.name)
                self.addon_tab_widgets.append(tab_widget)

    def addons_save(self):
        """"""
        [addon.save() for addon in self.addons_list]

    def load_session(self):
        """"""
        ordered_list = api.load_session()
        self.downloads.store_items(ordered_list)

    def save_session(self):
        """"""
        id_item_list = [row[0] for row in self.downloads.items]
        api.save_session(id_item_list)

    def idle_timeout(self, interval, func):
        timer = QTimer(self)
        timer.timeout.connect(func)
        timer.start(interval)
        return timer

    def queue_loop(self):
        try:
            callback = idle_loop.get_nowait()
            callback()
        except Queue.Empty:
            pass

    def event_close(self):
        self.can_close = True
        self.close()

    def closeEvent(self, event):  #overloaded method
        """
        also useful for any QWidget
        """
        if self.can_close:  #if self.canExit():
            x, y, w, h = self.geometry().getRect()
            self.hide()
            self.save_session()
            self.addons_save()
            conf.set_window_settings(x, y, w, h)
            conf.save()
            event.accept()
        else:  #hide only
            self.hide()
            event.ignore()
Exemple #4
0
class Gui(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        
        self.setWindowTitle(cons.APP_TITLE)
        self.setWindowIcon(QIcon(os.path.join(cons.MEDIA_PATH, "misc", "ochd.ico")))
        self.resize(MIN_WIDTH, MIN_HEIGHT)
        self.center()
        
        self.restore_wnd_geometry()
        
        self.stop = (QToolButton(), media.get_icon(media.STOP, media.MEDIUM), _('Stop Download'), self.on_stop_download, False)
        self.start = (QToolButton(), media.get_icon(media.START, media.MEDIUM), _('Start Download'), self.on_start_download, False)
        accounts = (QToolButton(), media.get_icon(media.ACCOUNTS, media.MEDIUM), _('Accounts'), self.on_accounts, True)
        preferences = (QToolButton(), media.get_icon(media.PREFERENCES, media.MEDIUM), _('Preferences'), self.on_preferences, True)
        about = (QToolButton(), media.get_icon(media.ABOUT, media.MEDIUM), _('About'), self.on_about, True)
        
        self.menu = QMenu()
        preferences[BTN].setPopupMode(QToolButton.MenuButtonPopup)
        preferences[BTN].setMenu(self.menu)
        
        toolbar = Toolbar(self, [self.start, self.stop, None, accounts, preferences, None, about])

        self.toolbar = self.addToolBar(toolbar)

        #self.vbox = QVBoxLayout(self)
        
        #tabs
        self.previous_tab = None
        self.tab = QTabWidget(self)
        #
        self.downloads = Downloads(self)
        self.tab.addTab(self.downloads, _('Downloads'))
        #
        self.add_downloads = AddDownloads(self.downloads, self)
        self.tab_add_downloads = QWidget()
        self.tab_add_downloads.setLayout(self.add_downloads)
        self.tab.addTab(self.tab_add_downloads, _('Add downloads'))
        #
        #addons
        self.addons_manager = AddonsManager(self)
        self.addons_list = self.addons_manager.addons_list
        #...tabs
        self.addon_tab_widgets = []
        self.load_addon_tabs()
        #
        self.log = Log()
        self.tab_log = QWidget()
        self.tab_log.setLayout(self.log)
        self.tab.addTab(self.tab_log, _('Log'))
        #
        self.preferences = Preferences(self.addons_list, self)
        #self.tab.addTab(self.preferences, 'Preferences')
        #
        self.tab.currentChanged.connect(self.on_tab_switch)
        #
        self.setCentralWidget(self.tab)
        
        #status bar
        self.status_bar = StatusBar(self)
        self.setStatusBar(self.status_bar)
        
        #drop down menu
        [addon.set_menu_item() for addon in self.addons_list]
        #self.menu.addSeparator()
        #self.menu.addAction('Preferences', self.on_preferences)

        #system tray icon
        self.tray = Tray(self)
        if self.tray.available:
            self.can_close = False
        else:
            self.can_close = True

        #on select button state
        self.downloads.selectionModel().selectionChanged.connect(self.on_selected)

        #load session.
        self.load_session()

        #add core's event loop
        self.idle_timeout(500, self.queue_loop)

        #quit event
        events.connect(cons.EVENT_QUIT, self.event_close)
        
        self.show()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())
    
    def restore_wnd_geometry(self):
        wx, wy, ww, wh = QDesktopWidget().availableGeometry().getRect()
        x, y, w, h = conf.get_window_settings()
        if ww <= w or wh <= h:
            self.showMaximized()
        elif ww > x >= 0 and wh > y >= 0: #resize and move
            self.setGeometry(x, y, w, h)
        else: #resize only
            self.resize(w, h)
    
    def on_stop_download(self):
        rows = self.downloads.get_selected_rows()
        if rows:
            for row in rows:
                items = self.downloads.items
                id_item = items[row][0]
                stopped = api.stop_download(id_item) #return true or false
                if stopped:
                    if items[row][1] == self.downloads.icons_dict[cons.STATUS_QUEUE]:
                        items[row][1] = self.downloads.icons_dict[cons.STATUS_STOPPED]
                    self.stop[BTN].setEnabled(False) #deshabilitar el boton de stop ya que acaban de detener la descarga.
                    self.start[BTN].setEnabled(True)
    
    def on_start_download(self):
        rows = self.downloads.get_selected_rows()
        if rows:
            #id_item = model[row][0]
            for row in rows:
                items = self.downloads.items
                id_item = items[row][0]

                #TODO: Implementar lo mismo pero para stopped (buscar en lista stopped y finished para comparar)
                started = api.start_download(id_item) #return true or false
                if started:
                    items[row][1] = self.downloads.icons_dict[cons.STATUS_QUEUE] #status
                    items[row][10] = None #status_msg
                    self.stop[BTN].setEnabled(True) #deshabilitar el boton de stop ya que acaban de detener la descarga.
                    self.start[BTN].setEnabled(False)
                
            #self.downloads.get_status() #iniciar update de lista.
            id_item_list = [row[0] for row in items]
            api.reorder_queue(id_item_list)
    
    def on_accounts(self):
        accounts = ConfigAccounts(self)
    
    def on_preferences(self):
        index_page = self.tab.indexOf(self.preferences) #get the page containig the widget
        if index_page >= 0: #if = -1 there is not such tab.
            self.tab.setCurrentIndex(index_page)
        else:
            index_page = self.tab.addTab(self.preferences, _('Preferences'))
            btn_close = QPushButton(self)
            #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_preferences)
            self.tab.tabBar().setTabButton(index_page, QTabBar.RightSide, btn_close)
            #
            last_index = self.tab.count() - 1
            self.tab.setCurrentIndex(last_index)
    
    def on_close_preferences(self):
        index_page = self.tab.indexOf(self.preferences)
        if index_page >= 0:
            self.tab.removeTab(index_page)
    
    def on_about(self):
        about = About(self)
    
    def on_tab_switch(self, index):
        current_widget = self.tab.currentWidget()
        if current_widget == self.tab_log:
            self.log.on_load()
        elif current_widget in self.addon_tab_widgets:
            tab = current_widget.layout()
            tab.on_load()

        if self.previous_tab == self.preferences:
            self.previous_tab.on_close()
        elif self.previous_tab in self.addon_tab_widgets:
            tab = self.previous_tab.layout()
            tab.on_close()
        self.previous_tab = current_widget
    
    def on_selected(self, current, previous):
        rows = self.downloads.get_selected_rows()
        
        self.stop[BTN].setEnabled(False)
        self.start[BTN].setEnabled(False)
        
        if len(rows) == 1: #single selection.
            items = self.downloads.items
            row = rows[0]
            id_item = items[row][0]
            self.stop[BTN].setEnabled(True)
            self.start[BTN].setEnabled(False)
            stopped_downloads = api.get_stopped_downloads()
            try:
                item = stopped_downloads[id_item]
                self.stop[BTN].setEnabled(False)
                self.start[BTN].setEnabled(True)
            except KeyError:
                pass
        elif rows: #multi selection
            self.stop[BTN].setEnabled(True)
            self.start[BTN].setEnabled(True)
    
    def load_addon_tabs(self):
        for tab, addon in [(addon.get_tab(), addon) for addon in self.addons_list]:
            if tab is not None:
                tab_widget = QWidget()
                tab_widget.setLayout(tab)
                self.tab.addTab(tab_widget, addon.name)
                self.addon_tab_widgets.append(tab_widget)

    def addons_save(self):
        """"""
        [addon.save() for addon in self.addons_list]

    def load_session(self):
        """"""
        ordered_list = api.load_session()
        self.downloads.store_items(ordered_list)
    
    def save_session(self):
        """"""
        id_item_list = [row[0] for row in self.downloads.items]
        api.save_session(id_item_list)
    
    def idle_timeout(self, interval, func):
        timer = QTimer(self)
        timer.timeout.connect(func)
        timer.start(interval)
        return timer
    
    def queue_loop(self):
        try:
            callback = idle_loop.get_nowait()
            callback()
        except Queue.Empty:
            pass

    def event_close(self):
        self.can_close = True
        self.close()

    def closeEvent(self, event): #overloaded method
        """
        also useful for any QWidget
        """
        if self.can_close: #if self.canExit():
            x, y, w, h = self.geometry().getRect()
            self.hide()
            self.save_session()
            self.addons_save()
            conf.set_window_settings(x, y, w, h)
            conf.save()
            event.accept()
        else: #hide only
            self.hide()
            event.ignore()