WebBrowser/Download/DownloadItem.py

changeset 5194
4750c83cc718
parent 5033
d1c2651060ec
child 5253
57276f763bf6
diff -r 04ba3ea482fe -r 4750c83cc718 WebBrowser/Download/DownloadItem.py
--- a/WebBrowser/Download/DownloadItem.py	Sat Oct 01 15:12:23 2016 +0200
+++ b/WebBrowser/Download/DownloadItem.py	Mon Oct 03 19:02:45 2016 +0200
@@ -9,8 +9,10 @@
 
 from __future__ import unicode_literals
 
-from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QTime, QFileInfo, QUrl, \
-    PYQT_VERSION_STR
+import os
+
+from PyQt5.QtCore import pyqtSlot, pyqtSignal, qVersion, Qt, QTime, QUrl, \
+    QStandardPaths, QFileInfo
 from PyQt5.QtGui import QPalette, QDesktopServices
 from PyQt5.QtWidgets import QWidget, QStyle, QDialog
 from PyQt5.QtWebEngineWidgets import QWebEngineDownloadItem
@@ -24,6 +26,7 @@
 
 import UI.PixmapCache
 import Utilities.MimeTypes
+import Globals
 
 
 class DownloadItem(QWidget, Ui_DownloadItem):
@@ -121,6 +124,15 @@
         if self.__gettingFileName:
             return
         
+        if qVersion() >= "5.7.0":
+            savePage = self.__downloadItem.savePageFormat() != \
+                        QWebEngineDownloadItem.UnknownSaveFormat
+        else:
+            savePage = self.__downloadItem.path().lower().endswith(
+                (".mhtml", ".mht"))
+        
+        documentLocation = QStandardPaths.writableLocation(
+            QStandardPaths.DocumentsLocation)
         downloadDirectory = WebBrowserWindow\
             .downloadManager().downloadDirectory()
         
@@ -131,13 +143,14 @@
             ask = False
         else:
             defaultFileName, originalFileName = \
-                self.__saveFileName(downloadDirectory)
+                self.__saveFileName(
+                    documentLocation if savePage else downloadDirectory)
             fileName = defaultFileName
             self.__originalFileName = originalFileName
             ask = True
         self.__autoOpen = False
         
-        if not originalFileName.lower().endswith(".mhtml"):
+        if not savePage:
             from .DownloadAskActionDialog import DownloadAskActionDialog
             url = self.__downloadItem.url()
             mimetype = Utilities.MimeTypes.mimeType(originalFileName)
@@ -168,36 +181,72 @@
                 return
         
             self.__autoOpen = dlg.getAction() == "open"
+            
+            tempLocation = QStandardPaths.writableLocation(
+                QStandardPaths.TempLocation)
+            fileName = tempLocation + '/' + \
+                QFileInfo(fileName).completeBaseName()
+            
+            if ask and not self.__autoOpen:
+                self.__gettingFileName = True
+                fileName = E5FileDialog.getSaveFileName(
+                    None,
+                    self.tr("Save File"),
+                    defaultFileName,
+                    "")
+                self.__gettingFileName = False
         else:
             self.__autoOpen = False
-        
-        if PYQT_VERSION_STR >= "5.0.0":
-            from PyQt5.QtCore import QStandardPaths
-            tempLocation = QStandardPaths.standardLocations(
-                QStandardPaths.TempLocation)[0]
-        else:
-            from PyQt5.QtGui import QDesktopServices
-            tempLocation = QDesktopServices.storageLocation(
-                QDesktopServices.TempLocation)
-        fileName = tempLocation + '/' + \
-            QFileInfo(fileName).completeBaseName()
-        
-        if ask and not self.__autoOpen:
+            
+            filterList = [
+                self.tr("Web Archive (*.mhtml *.mht)"),
+                self.tr("HTML File (*.html *.htm)"),
+                self.tr("HTML File with all resources (*.html *.htm)"),
+            ]
+            extensionsList = [
+                # tuple of extensions for *nix and Windows
+                # keep in sync with filters list
+                (".mhtml", ".mht"),
+                (".html", ".htm"),
+                (".html", ".htm"),
+            ]
             self.__gettingFileName = True
-            fileName = E5FileDialog.getSaveFileName(
+            fileName, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
                 None,
-                self.tr("Save File"),
+                self.tr("Save Web Page"),
                 defaultFileName,
-                "")
+                ";;".join(filterList),
+                None)
             self.__gettingFileName = False
-            if not fileName:
-                self.progressBar.setVisible(False)
-                self.on_stopButton_clicked()
-                self.filenameLabel.setText(
-                    self.tr("Download canceled: {0}")
-                        .format(QFileInfo(defaultFileName).fileName()))
-                self.__canceledFileSelect = True
-                return
+            if fileName:
+                index = filterList.index(selectedFilter)
+                if index == 0:
+                    self.__downloadItem.setSavePageFormat(
+                        QWebEngineDownloadItem.MimeHtmlSaveFormat)
+                elif index == 1:
+                    self.__downloadItem.setSavePageFormat(
+                        QWebEngineDownloadItem.SingleHtmlSaveFormat)
+                else:
+                    self.__downloadItem.setSavePageFormat(
+                        QWebEngineDownloadItem.CompleteHtmlSaveFormat)
+                extension = os.path.splitext(fileName)[1]
+                if not extension:
+                    # add the platform specific default extension
+                    if Globals.isWindowsPlatform():
+                        extensionsIndex = 1
+                    else:
+                        extensionsIndex = 0
+                    extensions = extensionsList[index]
+                    fileName += extensions[extensionsIndex]
+        
+        if not fileName:
+            self.progressBar.setVisible(False)
+            self.on_stopButton_clicked()
+            self.filenameLabel.setText(
+                self.tr("Download canceled: {0}")
+                    .format(QFileInfo(defaultFileName).fileName()))
+            self.__canceledFileSelect = True
+            return
         
         fileInfo = QFileInfo(fileName)
         WebBrowserWindow.downloadManager()\
@@ -235,7 +284,7 @@
         if endName:
             origName += '.' + endName
         
-        name = directory + baseName
+        name = os.path.join(directory, baseName)
         if endName:
             name += '.' + endName
         return name, origName

eric ide

mercurial