eric6/WebBrowser/Download/DownloadItem.py

branch
maintenance
changeset 7286
7eb04391adf7
parent 6989
8b8cadf8d7e9
parent 7268
a28338eaf694
child 7362
028bf21bb5a2
equal deleted inserted replaced
7226:babe80d84a3e 7286:7eb04391adf7
5 5
6 """ 6 """
7 Module implementing a widget controlling a download. 7 Module implementing a widget controlling a download.
8 """ 8 """
9 9
10 from __future__ import unicode_literals
11 10
12 import os 11 import os
13 12
14 from PyQt5.QtCore import pyqtSlot, pyqtSignal, PYQT_VERSION, Qt, QTime, QUrl, \ 13 from PyQt5.QtCore import (
15 QStandardPaths, QFileInfo, QDateTime 14 pyqtSlot, pyqtSignal, Qt, QTime, QUrl, QStandardPaths, QFileInfo, QDateTime
15 )
16 from PyQt5.QtGui import QPalette, QDesktopServices 16 from PyQt5.QtGui import QPalette, QDesktopServices
17 from PyQt5.QtWidgets import QWidget, QStyle, QDialog 17 from PyQt5.QtWidgets import QWidget, QStyle, QDialog
18 from PyQt5.QtWebEngineWidgets import QWebEngineDownloadItem 18 from PyQt5.QtWebEngineWidgets import QWebEngineDownloadItem
19 19
20 from E5Gui import E5FileDialog 20 from E5Gui import E5FileDialog
24 from .DownloadUtilities import timeString, dataString, speedString 24 from .DownloadUtilities import timeString, dataString, speedString
25 from WebBrowser.WebBrowserWindow import WebBrowserWindow 25 from WebBrowser.WebBrowserWindow import WebBrowserWindow
26 26
27 import UI.PixmapCache 27 import UI.PixmapCache
28 import Utilities.MimeTypes 28 import Utilities.MimeTypes
29 import Globals
30 from Globals import qVersionTuple
31 29
32 30
33 class DownloadItem(QWidget, Ui_DownloadItem): 31 class DownloadItem(QWidget, Ui_DownloadItem):
34 """ 32 """
35 Class implementing a widget controlling a download. 33 Class implementing a widget controlling a download.
122 # reset info 120 # reset info
123 self.datetimeLabel.clear() 121 self.datetimeLabel.clear()
124 self.datetimeLabel.hide() 122 self.datetimeLabel.hide()
125 self.infoLabel.clear() 123 self.infoLabel.clear()
126 self.progressBar.setValue(0) 124 self.progressBar.setValue(0)
127 if self.__downloadItem.state() == \ 125 if (
128 QWebEngineDownloadItem.DownloadRequested: 126 self.__downloadItem.state() ==
127 QWebEngineDownloadItem.DownloadRequested
128 ):
129 self.__getFileName() 129 self.__getFileName()
130 if not self.__fileName: 130 if not self.__fileName:
131 self.__downloadItem.cancel() 131 self.__downloadItem.cancel()
132 else: 132 else:
133 self.__downloadItem.setPath(self.__fileName) 133 self.__downloadItem.setPath(self.__fileName)
141 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.
142 """ 142 """
143 if self.__gettingFileName: 143 if self.__gettingFileName:
144 return 144 return
145 145
146 if qVersionTuple() >= (5, 8, 0) and PYQT_VERSION >= 0x50800: 146 savePage = self.__downloadItem.type() == (
147 savePage = self.__downloadItem.type() == \ 147 QWebEngineDownloadItem.SavePage
148 QWebEngineDownloadItem.SavePage 148 )
149 elif qVersionTuple() >= (5, 7, 0) and PYQT_VERSION >= 0x50700:
150 savePage = self.__downloadItem.savePageFormat() != \
151 QWebEngineDownloadItem.UnknownSaveFormat
152 else:
153 savePage = self.__downloadItem.path().lower().endswith(
154 (".mhtml", ".mht"))
155 149
156 documentLocation = QStandardPaths.writableLocation( 150 documentLocation = QStandardPaths.writableLocation(
157 QStandardPaths.DocumentsLocation) 151 QStandardPaths.DocumentsLocation)
158 downloadDirectory = WebBrowserWindow\ 152 downloadDirectory = (
159 .downloadManager().downloadDirectory() 153 WebBrowserWindow.downloadManager().downloadDirectory()
154 )
160 155
161 if self.__fileName: 156 if self.__fileName:
162 fileName = self.__fileName 157 fileName = self.__fileName
163 originalFileName = self.__originalFileName 158 originalFileName = self.__originalFileName
164 self.__toDownload = True 159 self.__toDownload = True
165 ask = False 160 ask = False
166 else: 161 else:
167 defaultFileName, originalFileName = \ 162 defaultFileName, originalFileName = self.__saveFileName(
168 self.__saveFileName( 163 documentLocation if savePage else downloadDirectory)
169 documentLocation if savePage else downloadDirectory)
170 fileName = defaultFileName 164 fileName = defaultFileName
171 self.__originalFileName = originalFileName 165 self.__originalFileName = originalFileName
172 ask = True 166 ask = True
173 self.__autoOpen = False 167 self.__autoOpen = False
174 168
205 199
206 self.__autoOpen = dlg.getAction() == "open" 200 self.__autoOpen = dlg.getAction() == "open"
207 201
208 tempLocation = QStandardPaths.writableLocation( 202 tempLocation = QStandardPaths.writableLocation(
209 QStandardPaths.TempLocation) 203 QStandardPaths.TempLocation)
210 fileName = tempLocation + '/' + \ 204 fileName = (
205 tempLocation + '/' +
211 QFileInfo(fileName).completeBaseName() 206 QFileInfo(fileName).completeBaseName()
207 )
212 208
213 if ask and not self.__autoOpen: 209 if ask and not self.__autoOpen:
214 self.__gettingFileName = True 210 self.__gettingFileName = True
215 fileName = E5FileDialog.getSaveFileName( 211 fileName = E5FileDialog.getSaveFileName(
216 None, 212 None,
217 self.tr("Save File"), 213 self.tr("Save File"),
218 defaultFileName, 214 defaultFileName,
219 "") 215 "")
220 self.__gettingFileName = False 216 self.__gettingFileName = False
221 else:
222 # save page file name and format selection for Qt < 5.8.0
223 self.__autoOpen = False
224
225 filterList = [
226 self.tr("Web Archive (*.mhtml *.mht)"),
227 self.tr("HTML File (*.html *.htm)"),
228 self.tr("HTML File with all resources (*.html *.htm)"),
229 ]
230 extensionsList = [
231 # tuple of extensions for *nix and Windows
232 # keep in sync with filters list
233 (".mhtml", ".mht"),
234 (".html", ".htm"),
235 (".html", ".htm"),
236 ]
237 self.__gettingFileName = True
238 fileName, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
239 None,
240 self.tr("Save Web Page"),
241 defaultFileName,
242 ";;".join(filterList),
243 None)
244 self.__gettingFileName = False
245 if fileName:
246 index = filterList.index(selectedFilter)
247 if index == 0:
248 self.__downloadItem.setSavePageFormat(
249 QWebEngineDownloadItem.MimeHtmlSaveFormat)
250 elif index == 1:
251 self.__downloadItem.setSavePageFormat(
252 QWebEngineDownloadItem.SingleHtmlSaveFormat)
253 else:
254 self.__downloadItem.setSavePageFormat(
255 QWebEngineDownloadItem.CompleteHtmlSaveFormat)
256 extension = os.path.splitext(fileName)[1]
257 if not extension:
258 # add the platform specific default extension
259 if Globals.isWindowsPlatform():
260 extensionsIndex = 1
261 else:
262 extensionsIndex = 0
263 extensions = extensionsList[index]
264 fileName += extensions[extensionsIndex]
265 217
266 if not fileName: 218 if not fileName:
267 self.progressBar.setVisible(False) 219 self.progressBar.setVisible(False)
268 self.on_stopButton_clicked() 220 self.on_stopButton_clicked()
269 self.filenameLabel.setText( 221 self.filenameLabel.setText(
281 233
282 @param fileName name of the file to save into 234 @param fileName name of the file to save into
283 @type str 235 @type str
284 """ 236 """
285 fileInfo = QFileInfo(fileName) 237 fileInfo = QFileInfo(fileName)
286 WebBrowserWindow.downloadManager()\ 238 WebBrowserWindow.downloadManager().setDownloadDirectory(
287 .setDownloadDirectory(fileInfo.absoluteDir().absolutePath()) 239 fileInfo.absoluteDir().absolutePath())
288 self.filenameLabel.setText(fileInfo.fileName()) 240 self.filenameLabel.setText(fileInfo.fileName())
289 241
290 self.__fileName = fileName 242 self.__fileName = fileName
291 243
292 # check file path for saving 244 # check file path for saving
481 remaining = "" 433 remaining = ""
482 434
483 if bytesTotal > 0: 435 if bytesTotal > 0:
484 remaining = timeString(timeRemaining) 436 remaining = timeString(timeRemaining)
485 437
486 info = self.tr("{0} of {1} ({2}/sec) {3}")\ 438 info = self.tr(
487 .format( 439 "{0} of {1} ({2}/sec) {3}"
488 dataString(self.__bytesReceived), 440 ).format(
489 bytesTotal == -1 and self.tr("?") or 441 dataString(self.__bytesReceived),
490 dataString(bytesTotal), 442 bytesTotal == -1 and self.tr("?") or
491 speedString(speed), 443 dataString(bytesTotal),
492 remaining) 444 speedString(speed),
445 remaining
446 )
493 else: 447 else:
494 if self.__bytesReceived == bytesTotal or bytesTotal == -1: 448 if self.__bytesReceived == bytesTotal or bytesTotal == -1:
495 info = self.tr("{0} downloaded")\ 449 info = self.tr(
496 .format(dataString(self.__bytesReceived)) 450 "{0} downloaded"
451 ).format(dataString(self.__bytesReceived))
497 else: 452 else:
498 info = self.tr("{0} of {1} - Stopped")\ 453 info = self.tr(
499 .format(dataString(self.__bytesReceived), 454 "{0} of {1} - Stopped"
500 dataString(bytesTotal)) 455 ).format(dataString(self.__bytesReceived),
456 dataString(bytesTotal))
501 self.infoLabel.setText(info) 457 self.infoLabel.setText(info)
502 458
503 def downloading(self): 459 def downloading(self):
504 """ 460 """
505 Public method to determine, if a download is in progress. 461 Public method to determine, if a download is in progress.

eric ide

mercurial