eric6/WebBrowser/Download/DownloadItem.py

changeset 7268
a28338eaf694
parent 7229
53054eb5b15a
child 7286
7eb04391adf7
child 7360
9190402e4505
equal deleted inserted replaced
7267:aedc309827c7 7268:a28338eaf694
8 """ 8 """
9 9
10 10
11 import os 11 import os
12 12
13 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QTime, QUrl, \ 13 from PyQt5.QtCore import (
14 QStandardPaths, QFileInfo, QDateTime 14 pyqtSlot, pyqtSignal, Qt, QTime, QUrl, QStandardPaths, QFileInfo, QDateTime
15 )
15 from PyQt5.QtGui import QPalette, QDesktopServices 16 from PyQt5.QtGui import QPalette, QDesktopServices
16 from PyQt5.QtWidgets import QWidget, QStyle, QDialog 17 from PyQt5.QtWidgets import QWidget, QStyle, QDialog
17 from PyQt5.QtWebEngineWidgets import QWebEngineDownloadItem 18 from PyQt5.QtWebEngineWidgets import QWebEngineDownloadItem
18 19
19 from E5Gui import E5FileDialog 20 from E5Gui import E5FileDialog
119 # reset info 120 # reset info
120 self.datetimeLabel.clear() 121 self.datetimeLabel.clear()
121 self.datetimeLabel.hide() 122 self.datetimeLabel.hide()
122 self.infoLabel.clear() 123 self.infoLabel.clear()
123 self.progressBar.setValue(0) 124 self.progressBar.setValue(0)
124 if self.__downloadItem.state() == \ 125 if (
125 QWebEngineDownloadItem.DownloadRequested: 126 self.__downloadItem.state() ==
127 QWebEngineDownloadItem.DownloadRequested
128 ):
126 self.__getFileName() 129 self.__getFileName()
127 if not self.__fileName: 130 if not self.__fileName:
128 self.__downloadItem.cancel() 131 self.__downloadItem.cancel()
129 else: 132 else:
130 self.__downloadItem.setPath(self.__fileName) 133 self.__downloadItem.setPath(self.__fileName)
138 Private method to get the file name to save to from the user. 141 Private method to get the file name to save to from the user.
139 """ 142 """
140 if self.__gettingFileName: 143 if self.__gettingFileName:
141 return 144 return
142 145
143 savePage = self.__downloadItem.type() == \ 146 savePage = self.__downloadItem.type() == (
144 QWebEngineDownloadItem.SavePage 147 QWebEngineDownloadItem.SavePage
148 )
145 149
146 documentLocation = QStandardPaths.writableLocation( 150 documentLocation = QStandardPaths.writableLocation(
147 QStandardPaths.DocumentsLocation) 151 QStandardPaths.DocumentsLocation)
148 downloadDirectory = WebBrowserWindow\ 152 downloadDirectory = (
149 .downloadManager().downloadDirectory() 153 WebBrowserWindow.downloadManager().downloadDirectory()
154 )
150 155
151 if self.__fileName: 156 if self.__fileName:
152 fileName = self.__fileName 157 fileName = self.__fileName
153 originalFileName = self.__originalFileName 158 originalFileName = self.__originalFileName
154 self.__toDownload = True 159 self.__toDownload = True
155 ask = False 160 ask = False
156 else: 161 else:
157 defaultFileName, originalFileName = \ 162 defaultFileName, originalFileName = self.__saveFileName(
158 self.__saveFileName( 163 documentLocation if savePage else downloadDirectory)
159 documentLocation if savePage else downloadDirectory)
160 fileName = defaultFileName 164 fileName = defaultFileName
161 self.__originalFileName = originalFileName 165 self.__originalFileName = originalFileName
162 ask = True 166 ask = True
163 self.__autoOpen = False 167 self.__autoOpen = False
164 168
195 199
196 self.__autoOpen = dlg.getAction() == "open" 200 self.__autoOpen = dlg.getAction() == "open"
197 201
198 tempLocation = QStandardPaths.writableLocation( 202 tempLocation = QStandardPaths.writableLocation(
199 QStandardPaths.TempLocation) 203 QStandardPaths.TempLocation)
200 fileName = tempLocation + '/' + \ 204 fileName = (
205 tempLocation + '/' +
201 QFileInfo(fileName).completeBaseName() 206 QFileInfo(fileName).completeBaseName()
207 )
202 208
203 if ask and not self.__autoOpen: 209 if ask and not self.__autoOpen:
204 self.__gettingFileName = True 210 self.__gettingFileName = True
205 fileName = E5FileDialog.getSaveFileName( 211 fileName = E5FileDialog.getSaveFileName(
206 None, 212 None,
227 233
228 @param fileName name of the file to save into 234 @param fileName name of the file to save into
229 @type str 235 @type str
230 """ 236 """
231 fileInfo = QFileInfo(fileName) 237 fileInfo = QFileInfo(fileName)
232 WebBrowserWindow.downloadManager()\ 238 WebBrowserWindow.downloadManager().setDownloadDirectory(
233 .setDownloadDirectory(fileInfo.absoluteDir().absolutePath()) 239 fileInfo.absoluteDir().absolutePath())
234 self.filenameLabel.setText(fileInfo.fileName()) 240 self.filenameLabel.setText(fileInfo.fileName())
235 241
236 self.__fileName = fileName 242 self.__fileName = fileName
237 243
238 # check file path for saving 244 # check file path for saving
427 remaining = "" 433 remaining = ""
428 434
429 if bytesTotal > 0: 435 if bytesTotal > 0:
430 remaining = timeString(timeRemaining) 436 remaining = timeString(timeRemaining)
431 437
432 info = self.tr("{0} of {1} ({2}/sec) {3}")\ 438 info = self.tr(
433 .format( 439 "{0} of {1} ({2}/sec) {3}"
434 dataString(self.__bytesReceived), 440 ).format(
435 bytesTotal == -1 and self.tr("?") or 441 dataString(self.__bytesReceived),
436 dataString(bytesTotal), 442 bytesTotal == -1 and self.tr("?") or
437 speedString(speed), 443 dataString(bytesTotal),
438 remaining) 444 speedString(speed),
445 remaining
446 )
439 else: 447 else:
440 if self.__bytesReceived == bytesTotal or bytesTotal == -1: 448 if self.__bytesReceived == bytesTotal or bytesTotal == -1:
441 info = self.tr("{0} downloaded")\ 449 info = self.tr(
442 .format(dataString(self.__bytesReceived)) 450 "{0} downloaded"
451 ).format(dataString(self.__bytesReceived))
443 else: 452 else:
444 info = self.tr("{0} of {1} - Stopped")\ 453 info = self.tr(
445 .format(dataString(self.__bytesReceived), 454 "{0} of {1} - Stopped"
446 dataString(bytesTotal)) 455 ).format(dataString(self.__bytesReceived),
456 dataString(bytesTotal))
447 self.infoLabel.setText(info) 457 self.infoLabel.setText(info)
448 458
449 def downloading(self): 459 def downloading(self):
450 """ 460 """
451 Public method to determine, if a download is in progress. 461 Public method to determine, if a download is in progress.

eric ide

mercurial