9 |
9 |
10 from __future__ import unicode_literals |
10 from __future__ import unicode_literals |
11 |
11 |
12 import os |
12 import os |
13 |
13 |
14 from PyQt5.QtCore import pyqtSlot, pyqtSignal, PYQT_VERSION, Qt, QTime, QUrl, \ |
14 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QTime, QUrl, \ |
15 QStandardPaths, QFileInfo, QDateTime |
15 QStandardPaths, QFileInfo, QDateTime |
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 |
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. |
141 Private method to get the file name to save to from the user. |
139 Private method to get the file name to save to from the user. |
142 """ |
140 """ |
143 if self.__gettingFileName: |
141 if self.__gettingFileName: |
144 return |
142 return |
145 |
143 |
146 if qVersionTuple() >= (5, 8, 0) and PYQT_VERSION >= 0x50800: |
144 savePage = self.__downloadItem.type() == \ |
147 savePage = self.__downloadItem.type() == \ |
145 QWebEngineDownloadItem.SavePage |
148 QWebEngineDownloadItem.SavePage |
|
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 |
146 |
156 documentLocation = QStandardPaths.writableLocation( |
147 documentLocation = QStandardPaths.writableLocation( |
157 QStandardPaths.DocumentsLocation) |
148 QStandardPaths.DocumentsLocation) |
158 downloadDirectory = WebBrowserWindow\ |
149 downloadDirectory = WebBrowserWindow\ |
159 .downloadManager().downloadDirectory() |
150 .downloadManager().downloadDirectory() |
216 None, |
207 None, |
217 self.tr("Save File"), |
208 self.tr("Save File"), |
218 defaultFileName, |
209 defaultFileName, |
219 "") |
210 "") |
220 self.__gettingFileName = False |
211 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 |
212 |
266 if not fileName: |
213 if not fileName: |
267 self.progressBar.setVisible(False) |
214 self.progressBar.setVisible(False) |
268 self.on_stopButton_clicked() |
215 self.on_stopButton_clicked() |
269 self.filenameLabel.setText( |
216 self.filenameLabel.setText( |