def download(self): # global storagon_username, storagon_password # print storagon_username, storagon_password login = self.storagonSDK.login(self.mainWindow.username, self.mainWindow.password) if login: print("Login Success !") while len(self.fileToDownload): if self.running_thread == self.maxthreaddownload: break filex = self.fileToDownload.pop(0) link = filex if link in self.threaddownload.keys(): th = self.threaddownload[link] # thread bi pause, gio cho download tiep if th.downloadFolder == self.fileSavePath and th.pausenow: th.pausenow = False else: self.th = DownloadThread(link, self, link, self.fileSavePath) self.threaddownload[link] = self.th # add to thread list self.th.start() self.th.finished.connect( lambda: self.download_complete_signal.emit(str(link), str(self.fileSavePath), self.th.start_time, self.th.finish_time) ) self.running_thread += 1 else: print('Login Fail')
def download(self): login = self.storagonSDK.login(self.mainWindow.username, self.mainWindow.password) if login: print "Login Success !" while len(self.fileToDownload): if self.running_thread == self.maxthreaddownload: break filex = self.fileToDownload.pop(0) link = filex.decode("utf-8") if link in self.threaddownload.keys(): th = self.threaddownload[link] # thread bi pause, gio cho download tiep if th.downloadFolder == self.fileSavePath and th.pausenow: th.pausenow = False else: self.th = DownloadThread(link, self, link, self.fileSavePath) self.threaddownload[link] = self.th # add to thread list self.th.start() filename = str(link).split('/').pop().split('#')[0] self.mainWindow.trayIcon.showMessage('Downloading', filename.decode('utf-8')) self.mainWindow.link = None self.th.finished.connect(lambda: self.download_complete_signal.emit(str(link), str(self.fileSavePath), self.th.start_time, self.th.finish_time)) self.running_thread += 1 else: print 'Login Fail'
class tabDownload(QtWidgets.QWidget): trigger = pyqtSignal(int, str) input_link_signal = pyqtSignal(str) def __init__(self, mainWindow, download_complete_signal): super(tabDownload, self).__init__(mainWindow) self.mainWindow = mainWindow global serverURL self.storagonSDK = StoragonSDK(serverURL) self.download_complete_signal = download_complete_signal self.Ui_tabDownloadForm = Ui_tabDownloadForm() self.Ui_tabDownloadForm.setupUi(self) self.table = self.Ui_tabDownloadForm.tableWidget self.table.hideColumn(3) self.table.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.table.customContextMenuRequested.connect(self.openDownloadMenu) self.setTableWidth() self.show() self.mainWindow = mainWindow self.trigger.connect(self.updateStatus) self.input_link_signal.connect(self.chooseFileToDownload) self.Ui_tabDownloadForm.btn_addfile.setIcon(QtGui.QIcon(resource_path('add.png'))) self.Ui_tabDownloadForm.btn_addfile.setIconSize(QtCore.QSize(24, 24)) self.Ui_tabDownloadForm.btn_addfile.clicked.connect(self.showinputbox) self.inputbox = None self.fileToDownload = [] self.threaddownload = {} self.maxthreaddownload = 4 self.running_thread = 0 self.fileSavePath = '' def download(self): # global storagon_username, storagon_password # print storagon_username, storagon_password login = self.storagonSDK.login(self.mainWindow.username, self.mainWindow.password) if login: print("Login Success !") while len(self.fileToDownload): if self.running_thread == self.maxthreaddownload: break filex = self.fileToDownload.pop(0) link = filex if link in self.threaddownload.keys(): th = self.threaddownload[link] # thread bi pause, gio cho download tiep if th.downloadFolder == self.fileSavePath and th.pausenow: th.pausenow = False else: self.th = DownloadThread(link, self, link, self.fileSavePath) self.threaddownload[link] = self.th # add to thread list self.th.start() self.th.finished.connect( lambda: self.download_complete_signal.emit(str(link), str(self.fileSavePath), self.th.start_time, self.th.finish_time) ) self.running_thread += 1 else: print('Login Fail') # sys.exit(1) def updateStatus(self, threadID, status=None, file_size=None, up_to_path=None, speed=None, types='download', error=None): num_row = self.table.rowCount() for i in range(num_row): try: link = self.table.item(i, 0).text() except: continue threadID = threadID if link == threadID: if file_size: size_item = self.table.item(i, 1) size_item.setText(str(file_size) + ' byte') if speed: status_item = self.table.item(i, 2) message = status + " " + speed status_item.setText(str(message)) elif error: error_item = self.table.item(i, 2) error_item.setText("Error: " + str(error)) self.running_thread -= 1 elif status: status_item = self.table.item(i, 2) status_item.setText(status) # self.table.update(self.table.indexFromItem(status_item)) if status == 'Download done!': self.running_thread -= 1 if len(self.fileToDownload): self.download() else: return True return True def setTableWidth(self): width = QtWidgets.QDesktopWidget().width() / 8 for i in range(self.table.columnCount()): self.table.setColumnWidth(i, width) def openDownloadMenu(self, position): menu = QtWidgets.QMenu(self) # upload action Actionpause = menu.addAction('&Pause') Actionpause.triggered.connect(self.run_pause_selected) Actionpause = menu.addAction('&Resume') Actionpause.triggered.connect(self.run_resume_selected) Actionpause = menu.addAction('&Stop And Remove') Actionpause.triggered.connect(self.run_remove_selected) Actionpause = menu.addAction('&Open Folder Location') Actionpause.triggered.connect(self.open_downloaded_location_selected) menu.exec_(self.table.viewport().mapToGlobal(position)) def openFolderPath(self, path): if sys.platform == 'darwin': subprocess.call(['open', '--', path]) elif sys.platform == 'linux2': subprocess.call(['gnome-open', '--', path]) elif sys.platform == 'win32': # subprocess.call(['explorer', path]) ctypes.windll.shell32.ShellExecuteW(None, u'open', u'explorer.exe', u'/n,/select, ' + path, None, 1) def open_downloaded_location_selected(self): rows = self.Ui_tabDownloadForm.tableWidget.selectionModel().selectedRows() filesavepathList = [] for row in rows: r = row.row() status = str(self.Ui_tabDownloadForm.tableWidget.item(r, 2).text()) if status.find('Download done!') != -1: filesavepath = self.Ui_tabDownloadForm.tableWidget.item(r, 3).text() filesavepathList.append(filesavepath) filesavepathSet = set(filesavepathList) # print filesavepathSet for path in filesavepathSet: if os.path.isdir(path): self.openFolderPath(path) else: QtWidgets.QMessageBox.warning(self, "Notification Box", "Folder " + path + " Doesn't Exist", QtWidgets.QMessageBox.Ok) def run_pause_selected(self): row_selected = list(set([item.row() for item in self.table.selectedItems()])) if not row_selected: QtWidgets.QMessageBox.warning(self, 'Warning', "Please Choose File(s) To Pause", QtWidgets.QMessageBox.Ok) else: for row in row_selected: link = self.table.item(row, 0).text() for threadID in self.threaddownload.keys(): if threadID == link: th = self.threaddownload[threadID] if not th.pausenow: th.pausenow = True self.running_thread -= 1 # update status status_item = self.table.item(row, 2) message = "Paused" status_item.setText(str(message)) def run_resume_selected(self): row_selected = list(set([item.row() for item in self.table.selectedItems()])) if not row_selected: row_selected = range(self.table.rowCount()) error = 0 list_remove_thread = [] for row in row_selected: file_path = self.table.item(row, 0).text() for threadID in self.threaddownload.keys(): if threadID == file_path: th = self.threaddownload[threadID] if th.pausenow: if self.running_thread < self.maxthreaddownload: th.pausenow = False self.running_thread += 1 else: error = 1 else: # check xem thread co bi loi khong status = self.table.item(row, 2).text() # Thread bi loi va gio nguoi dung resume if status.find('Error') != -1: if self.running_thread < self.maxthreaddownload: self.fileToDownload.append(file_path) list_remove_thread.append(file_path) self.running_thread -= 1 else: error = 1 # remove thread cua file down bi loi de down lai if list_remove_thread: for th in list_remove_thread: del self.threaddownload[th] self.download() if error == 1: QtWidgets.QMessageBox.warning(self, 'Warning', "Number Of Running Thread Is Maximum. Please" + "Wait Until Other Files Completed", QtWidgets.QMessageBox.Ok) def run_remove_selected(self): row_selected = list(set([item.row() for item in self.table.selectedItems()])) while len(row_selected): row = row_selected.pop(0) if len(row_selected): row_selected = [row - 1 for row in row_selected] file_path = self.table.item(row, 0).text() status = self.table.item(row, 2).text() self.table.removeRow(row) for threadID in self.threaddownload.keys(): if threadID == file_path: th = self.threaddownload[threadID] th.pausenow = True self.running_thread -= 1 if status.find('Download done!') != -1: del self.threaddownload[threadID] self.download() def showinputbox(self): if self.inputbox is None: self.inputbox = tabDownload_InputBox(tab_download=self) def chooseFileToDownload(self, links): text_links = links links = text_links.split('\n') links = filter(lambda x: x != '', links) numrow = self.table.rowCount() row_links = [] for i in range(numrow): row_links.append(self.table.item(i, 0).text()) download_links = [] for link in links: if link not in row_links: download_links.append(link) if download_links: self.fileSavePath = QtWidgets.QFileDialog.getExistingDirectory(self, "Select path to Save!", '') if self.fileSavePath: for link in download_links: curRow = self.table.rowCount() self.table.insertRow(curRow) self.table.setItem(curRow, 0, QtWidgets.QTableWidgetItem(link)) self.table.setItem(curRow, 1, QtWidgets.QTableWidgetItem(u'Updating...')) self.table.setItem(curRow, 2, QtWidgets.QTableWidgetItem(u'Ready')) self.table.setItem(curRow, 3, QtWidgets.QTableWidgetItem(self.fileSavePath)) self.fileToDownload.append(link) self.download()