def __init__(self, dl_path=DL_PATH, store_file=STORAGE_PATH, autostart=True): QObject.__init__(self) self._gui = MyMainWindow() self._autostart = autostart self._dlpath = dl_path self.ui = Ui_MPLayerGui() self.ui.setupUi(self._gui) self.dlList = download.DownloadList() self.nameStorage = naming.SeriesStorage(store_file) self.valid_url = False self._timer = QTimer() self._timeout = 500 # init-settings self.ui.pubAddSimple.setEnabled(False) self.ui.pubStart.setEnabled(False) self.ui.pubKill.setEnabled(False) self.ui.pubRemove.setEnabled(False) self._filter_model = QSortFilterProxyModel() self._filter_model.setSourceModel(self.nameStorage) self.ui.lvSeries.setModel(self._filter_model) self._epiController = EpisodeController(self) self.ui.lvDownloads.setModel(self.dlList) self._selDM = self.ui.lvDownloads.selectionModel() self._gui.setExitChecker(self._isSafeToExit) self._doConnections() self._loadHistory() # completion self._completer = QCompleter() self._completer.setModel(self._filter_model) self._completer.setCompletionMode(QCompleter.PopupCompletion) self._completer.setCaseSensitivity(Qt.CaseInsensitive) self._completer.setCompletionRole(Qt.DisplayRole) self.ui.ledEName.setCompleter(self._completer)
class Controller(QObject): changedURL = pyqtSignal() def __init__(self, dl_path=DL_PATH, store_file=STORAGE_PATH, autostart=True): QObject.__init__(self) self._gui = MyMainWindow() self._autostart = autostart self._dlpath = dl_path self.ui = Ui_MPLayerGui() self.ui.setupUi(self._gui) self.dlList = download.DownloadList() self.nameStorage = naming.SeriesStorage(store_file) self.valid_url = False self._timer = QTimer() self._timeout = 500 # init-settings self.ui.pubAddSimple.setEnabled(False) self.ui.pubStart.setEnabled(False) self.ui.pubKill.setEnabled(False) self.ui.pubRemove.setEnabled(False) self._filter_model = QSortFilterProxyModel() self._filter_model.setSourceModel(self.nameStorage) self.ui.lvSeries.setModel(self._filter_model) self._epiController = EpisodeController(self) self.ui.lvDownloads.setModel(self.dlList) self._selDM = self.ui.lvDownloads.selectionModel() self._gui.setExitChecker(self._isSafeToExit) self._doConnections() self._loadHistory() # completion self._completer = QCompleter() self._completer.setModel(self._filter_model) self._completer.setCompletionMode(QCompleter.PopupCompletion) self._completer.setCaseSensitivity(Qt.CaseInsensitive) self._completer.setCompletionRole(Qt.DisplayRole) self.ui.ledEName.setCompleter(self._completer) def _isSafeToExit(self): dl_safe = self.dlList.isSafeToExit() na_safe = self.nameStorage.safeToExit _log.debug("safe to exit: download-queue: %d, name-storage: %d" % (dl_safe, na_safe)) return (dl_safe and na_safe) def _updateList(self): self._filter_model.sort(0) def _loadHistory(self): try: self.nameStorage.load() self._updateList() except IOError as ex: if ex.errno == errno.ENOENT: try: self.nameStorage.store(override=True) _log.debug("Created new storage file") except Exception as e: _log.error("Failed to create history file: %s" % str(e)) else: if ex.errno in errno.errorcodes: errtext = "errno=%s" % errno.errorcodes[ex.errno] else: errtext = "errno=%d" % ex.errno _log.error("Couldn't load history: '%s' (%s)" % (errtext, str(ex))) @pyqtSlot() def _storeHistory(self): self.nameStorage.store() def _doConnections(self): self.ui.pteUrl.textChanged.connect(self._changedURL) self.ui.ledMName.textChanged.connect(self._updateSimpleButton) self._epiController.doConnections() self.changedURL.connect(self._updateSimpleButton) self._selDM.currentChanged.connect(self._selectedDLChanged) self.ui.pubAddSimple.clicked.connect(self._addSimple) self.ui.pubRemove.clicked.connect(self._removeDownload) self.ui.pubStart.clicked.connect(self._startDownload) self.ui.pubKill.clicked.connect(self._killDownload) self._timer.timeout.connect(self._updateDlSelection) self.ui.pteUrl.pasteText.connect(self.ui.pteUrl.setPlainText) self.ui.pubMplayer.clicked.connect(self._playStream) qApp.aboutToQuit.connect(self._storeHistory) @pyqtSlot(bool) def setAutostart(self, autostart): self._autostart = autostart @pyqtSlot(QString) def setDLPath(self, path): path = str(path) if isdir(path): self._dlpath = path _log.debug("Setting Download Path to '%s'." % path) else: _log.warning("'%s' isn't a directory" % path) @pyqtSlot() def _changedURL(self): """URL has been changed. This slot checks if the url widget is empty, and sends out a signal if the empty status changed since the last message. """ url_empty = self.ui.pteUrl.toPlainText().isEmpty() if url_empty and self.valid_url: self.valid_url = False self.changedURL.emit() elif (not url_empty) and (not self.valid_url): self.valid_url = True self.changedURL.emit() @pyqtSlot() def _updateSimpleButton(self): """This slot checks if simple download button should be enabled. This slot reads the valid_url member and checks the current status (empty?) of the LineEdit ledMName. """ enabled = False if self.valid_url and not self.ui.ledMName.text().isEmpty(): enabled = True if self.ui.pubAddSimple.isEnabled != enabled: self.ui.pubAddSimple.setEnabled(enabled) def _updateDlSelection(self): start = False kill = False remove = False catstream = False text = u"Select an item for more information..." index = self._selDM.currentIndex() size_str = u"" streamer = self.dlList.getStreamer(index) if streamer is not None: status = streamer.getStatus() if status & streamer.RUN_BIT: size = streamer.getSize(inc_unit=True) if size is not None: size_str = u"Current size: %s" % size kill = True catstream = True text = u"Download in progress..." elif status & streamer.FIN_BIT: size = streamer.getSize(inc_unit=True) if size is not None: size_str = u"Current size: %s" % size if status & streamer.ERROR_BIT: start = True text = u"Download failed..." else: catstream = True text = u"Download successful!" remove = True else: text = u"Ready" start = True remove = True self.ui.pubStart.setEnabled(start) self.ui.pubKill.setEnabled(kill) self.ui.pubRemove.setEnabled(remove) self.ui.pubMplayer.setEnabled(catstream) self.ui.labStatus.setText(text) self.ui.labSize.setText(size_str) @pyqtSlot(QModelIndex, QModelIndex) def _selectedDLChanged(self, sel, desl): streamer = self.dlList.getStreamer(sel) if streamer is not None: streamer.changedStatus.connect(self._updateDlSelection) self._timer.start(self._timeout) else: self._timer.stop() self._updateDlSelection() @pyqtSlot() def _startDownload(self): index = self._selDM.currentIndex() self._startSpecificStream(index) @pyqtSlot() def _killDownload(self): index = self._selDM.currentIndex() streamer = self.dlList.getStreamer(index) if streamer is not None: streamer.kill() @pyqtSlot() def _removeDownload(self): index = self._selDM.currentIndex() streamer = self.dlList.getStreamer(index) if streamer is not None: streamer.changedStatus.disconnect(self._updateDlSelection) self._selDM.clear() self.dlList.remove(index) def _startSpecificStream(self, index): wget = self.ui.chkWgetMode.isChecked() try: self.dlList.start(index, wget=wget) except download.FileExistsError as ex: reply = QMessageBox.question(self._gui, 'Overwrite?', "File '%s' exists! Overwrite?" % ex.path, QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: self.dlList.start(index, overwrite=True, wget=wget) def download(self, dlinfo): """Add download to list (and eventually start it) This method can be used to download :param dlinfo: Download information for streamer. """ dlinfo.destDir = self._dlpath index = self.dlList.add(dlinfo) self._selDM.setCurrentIndex(index, QItemSelectionModel.SelectCurrent) if self._autostart: self._startSpecificStream(index) @pyqtSlot() def _addSimple(self): """Called if a simple download has been enqueued. """ if self.valid_url and not self.ui.ledMName.text().isEmpty(): url = str(self.ui.pteUrl.toPlainText()) name = str(self.ui.ledMName.text()) dlinfo = download.DownloadInfo(url, name, self._dlpath) self.download(dlinfo) @pyqtSlot() def _playStream(self): index = self._selDM.currentIndex() streamer = self.dlList.getStreamer(index) if streamer is not None: _log.debug("Trying to play %s" % str(streamer)) if (streamer.getStatus() & streamer.FIN_BIT): streamer.playFile() else: streamer.playStream() def show(self): self._gui.show()