WebBrowser/Download/DownloadManager.py

changeset 6134
cb0985e8da91
parent 6118
da9e08920e7c
child 6149
e611e45a17d6
equal deleted inserted replaced
6133:eba07240fac1 6134:cb0985e8da91
7 Module implementing the download manager class. 7 Module implementing the download manager class.
8 """ 8 """
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 from PyQt5.QtCore import pyqtSlot, Qt, QModelIndex, QFileInfo, QUrl 12 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QModelIndex, QFileInfo, QUrl
13 from PyQt5.QtGui import QCursor, QKeySequence 13 from PyQt5.QtGui import QCursor, QKeySequence
14 from PyQt5.QtWidgets import QDialog, QStyle, QFileIconProvider, QMenu, \ 14 from PyQt5.QtWidgets import QDialog, QStyle, QFileIconProvider, QMenu, \
15 QApplication, QShortcut 15 QApplication, QShortcut
16 16
17 from E5Gui import E5MessageBox 17 from E5Gui import E5MessageBox
32 Class implementing the download manager. 32 Class implementing the download manager.
33 """ 33 """
34 RemoveNever = 0 34 RemoveNever = 0
35 RemoveExit = 1 35 RemoveExit = 1
36 RemoveSuccessFullDownload = 2 36 RemoveSuccessFullDownload = 2
37
38 downloadsCountChanged = pyqtSignal()
37 39
38 def __init__(self, parent=None): 40 def __init__(self, parent=None):
39 """ 41 """
40 Constructor 42 Constructor
41 43
126 Public method to stop the download manager. 128 Public method to stop the download manager.
127 """ 129 """
128 self.save() 130 self.save()
129 self.close() 131 self.close()
130 132
131 def activeDownloads(self): 133 def activeDownloadsCount(self):
132 """ 134 """
133 Public method to get the number of active downloads. 135 Public method to get the number of active downloads.
134 136
135 @return number of active downloads (integer) 137 @return number of active downloads (integer)
136 """ 138 """
145 """ 147 """
146 Public method to check, if it is ok to quit. 148 Public method to check, if it is ok to quit.
147 149
148 @return flag indicating allowance to quit (boolean) 150 @return flag indicating allowance to quit (boolean)
149 """ 151 """
150 if self.activeDownloads() > 0: 152 if self.activeDownloadsCount() > 0:
151 res = E5MessageBox.yesNo( 153 res = E5MessageBox.yesNo(
152 self, 154 self,
153 self.tr(""), 155 self.tr(""),
154 self.tr("""There are %n downloads in progress.\n""" 156 self.tr("""There are %n downloads in progress.\n"""
155 """Do you want to quit anyway?""", "", 157 """Do you want to quit anyway?""", "",
156 self.activeDownloads()), 158 self.activeDownloadsCount()),
157 icon=E5MessageBox.Warning) 159 icon=E5MessageBox.Warning)
158 if not res: 160 if not res:
159 self.show() 161 self.show()
160 return False 162 return False
161 return True 163 return True
192 E5MessageBox.Abort) 194 E5MessageBox.Abort)
193 if res == E5MessageBox.Abort: 195 if res == E5MessageBox.Abort:
194 downloadItem.cancel() 196 downloadItem.cancel()
195 return 197 return
196 198
199 pageUrl = \
200 WebBrowserWindow.mainWindow().getWindow().currentBrowser().url()
197 from .DownloadItem import DownloadItem 201 from .DownloadItem import DownloadItem
198 itm = DownloadItem(downloadItem, parent=self) 202 itm = DownloadItem(downloadItem=downloadItem, pageUrl=pageUrl,
203 parent=self)
199 self.__addItem(itm) 204 self.__addItem(itm)
200 205
201 if itm.canceledFileSelect(): 206 if itm.canceledFileSelect():
202 return 207 return
203 208
219 itm.statusChanged.connect(lambda: self.__updateRow(itm)) 224 itm.statusChanged.connect(lambda: self.__updateRow(itm))
220 itm.downloadFinished.connect(self.__finished) 225 itm.downloadFinished.connect(self.__finished)
221 226
222 # insert at top of window 227 # insert at top of window
223 if append: 228 if append:
224 row = len(self.__downloads) 229 row = self.downloadsCount()
225 else: 230 else:
226 row = 0 231 row = 0
227 self.__model.beginInsertRows(QModelIndex(), row, row) 232 self.__model.beginInsertRows(QModelIndex(), row, row)
228 if append: 233 if append:
229 self.__downloads.append(itm) 234 self.__downloads.append(itm)
238 row, itm.sizeHint().height() * self.__rowHeightMultiplier) 243 row, itm.sizeHint().height() * self.__rowHeightMultiplier)
239 # just in case the download finished before the constructor returned 244 # just in case the download finished before the constructor returned
240 self.__updateRow(itm) 245 self.__updateRow(itm)
241 self.changeOccurred() 246 self.changeOccurred()
242 self.__updateActiveItemCount() 247 self.__updateActiveItemCount()
248
249 self.downloadsCountChanged.emit()
243 250
244 def __updateRow(self, itm): 251 def __updateRow(self, itm):
245 """ 252 """
246 Private slot to update a download item. 253 Private slot to update a download item.
247 254
273 280
274 if remove: 281 if remove:
275 self.__model.removeRow(row) 282 self.__model.removeRow(row)
276 283
277 self.cleanupButton.setEnabled( 284 self.cleanupButton.setEnabled(
278 (len(self.__downloads) - self.activeDownloads()) > 0) 285 (self.downloadsCount() - self.activeDownloadsCount()) > 0)
279 286
280 # record the change 287 # record the change
281 self.changeOccurred() 288 self.changeOccurred()
282 289
283 def removePolicy(self): 290 def removePolicy(self):
348 from .DownloadItem import DownloadItem 355 from .DownloadItem import DownloadItem
349 itm = DownloadItem(parent=self) 356 itm = DownloadItem(parent=self)
350 itm.setData(download) 357 itm.setData(download)
351 self.__addItem(itm, append=True) 358 self.__addItem(itm, append=True)
352 self.cleanupButton.setEnabled( 359 self.cleanupButton.setEnabled(
353 (len(self.__downloads) - self.activeDownloads()) > 0) 360 (self.downloadsCount() - self.activeDownloadsCount()) > 0)
354 361
355 self.__loaded = True 362 self.__loaded = True
356 self.__updateActiveItemCount() 363 self.__updateActiveItemCount()
364
365 self.downloadsCountChanged.emit()
357 366
358 def closeEvent(self, evt): 367 def closeEvent(self, evt):
359 """ 368 """
360 Protected event handler for the close event. 369 Protected event handler for the close event.
361 370
373 @pyqtSlot() 382 @pyqtSlot()
374 def on_cleanupButton_clicked(self): 383 def on_cleanupButton_clicked(self):
375 """ 384 """
376 Private slot cleanup the downloads. 385 Private slot cleanup the downloads.
377 """ 386 """
378 if len(self.__downloads) == 0: 387 if self.downloadsCount() == 0:
379 return 388 return
380 389
381 self.__model.removeRows(0, len(self.__downloads)) 390 self.__model.removeRows(0, self.downloadsCount())
382 if len(self.__downloads) == 0 and \ 391 if self.downloadsCount() == 0 and \
383 self.__iconProvider is not None: 392 self.__iconProvider is not None:
384 self.__iconProvider = None 393 self.__iconProvider = None
385 394
386 self.changeOccurred() 395 self.changeOccurred()
387 self.__updateActiveItemCount() 396 self.__updateActiveItemCount()
397
398 self.downloadsCountChanged.emit()
388 399
389 def __updateItemCount(self): 400 def __updateItemCount(self):
390 """ 401 """
391 Private method to update the count label. 402 Private method to update the count label.
392 """ 403 """
393 count = len(self.__downloads) 404 count = self.downloadsCount()
394 self.countLabel.setText(self.tr("%n Download(s)", "", count)) 405 self.countLabel.setText(self.tr("%n Download(s)", "", count))
395 406
396 def __updateActiveItemCount(self): 407 def __updateActiveItemCount(self):
397 """ 408 """
398 Private method to update the window title. 409 Private method to update the window title.
399 """ 410 """
400 count = self.activeDownloads() 411 count = self.activeDownloadsCount()
401 if count > 0: 412 if count > 0:
402 self.setWindowTitle( 413 self.setWindowTitle(
403 self.tr("Downloading %n file(s)", "", count)) 414 self.tr("Downloading %n file(s)", "", count))
404 else: 415 else:
405 self.setWindowTitle(self.tr("Downloads")) 416 self.setWindowTitle(self.tr("Downloads"))
409 Private slot to handle a finished download. 420 Private slot to handle a finished download.
410 """ 421 """
411 self.__updateActiveItemCount() 422 self.__updateActiveItemCount()
412 if self.isVisible(): 423 if self.isVisible():
413 QApplication.alert(self) 424 QApplication.alert(self)
425
426 self.downloadsCountChanged.emit()
414 427
415 def setDownloadDirectory(self, directory): 428 def setDownloadDirectory(self, directory):
416 """ 429 """
417 Public method to set the current download directory. 430 Public method to set the current download directory.
418 431
428 441
429 @return current download directory (string) 442 @return current download directory (string)
430 """ 443 """
431 return self.__downloadDirectory 444 return self.__downloadDirectory
432 445
433 def count(self): 446 def downloadsCount(self):
434 """ 447 """
435 Public method to get the number of downloads. 448 Public method to get the number of downloads.
436 449
437 @return number of downloads (integer) 450 @return number of downloads
451 @type int
438 """ 452 """
439 return len(self.__downloads) 453 return len(self.__downloads)
440 454
441 def downloads(self): 455 def downloads(self):
442 """ 456 """

eric ide

mercurial