Continued porting the web browser. QtWebEngine

Wed, 24 Feb 2016 20:27:40 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 24 Feb 2016 20:27:40 +0100
branch
QtWebEngine
changeset 4769
2b6f7e026cdc
parent 4768
57da9217196b
child 4772
db71b47b663e

Continued porting the web browser.

- continued adding the Downloads stuff

Preferences/__init__.py file | annotate | diff | comparison | revisions
WebBrowser/Download/DownloadItem.py file | annotate | diff | comparison | revisions
WebBrowser/Download/DownloadItem.ui file | annotate | diff | comparison | revisions
WebBrowser/Download/DownloadManager.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserWindow.py file | annotate | diff | comparison | revisions
--- a/Preferences/__init__.py	Tue Feb 23 20:00:40 2016 +0100
+++ b/Preferences/__init__.py	Wed Feb 24 20:27:40 2016 +0100
@@ -1028,6 +1028,11 @@
         "RssFeeds": [],
         # Grease Monkey
         "GreaseMonkeyDisabledScripts": [],
+        # Downloads
+        "DownloadManagerRemovePolicy": 0,      # never delete downloads
+        "DownloadManagerSize": QSize(400, 300),
+        "DownloadManagerPosition": QPoint(),
+        "DownloadManagerDownloads": [],
         # Flash Cookie Manager: identical to helpDefaults
         # PIM:                  identical to helpDefaults
         # VirusTotal:           identical to helpDefaults
@@ -2683,21 +2688,21 @@
             keywords.append((keyword, engineName))
         prefClass.settings.endArray()
         return keywords
-##    elif key in ["DownloadManagerDownloads"]:
-##        # return a list of tuples of (URL, save location, done flag, page url)
-##        downloads = []
-##        length = prefClass.settings.beginReadArray("WebBrowser/" + key)
-##        for index in range(length):
-##            prefClass.settings.setArrayIndex(index)
-##            url = prefClass.settings.value("URL")
-##            location = prefClass.settings.value("Location")
-##            done = toBool(prefClass.settings.value("Done"))
-##            pageUrl = prefClass.settings.value("PageURL")
-##            if pageUrl is None:
-##                pageUrl = QUrl()
-##            downloads.append((url, location, done, pageUrl))
-##        prefClass.settings.endArray()
-##        return downloads
+    elif key in ["DownloadManagerDownloads"]:
+        # return a list of tuples of (URL, save location, done flag, page url)
+        downloads = []
+        length = prefClass.settings.beginReadArray("WebBrowser/" + key)
+        for index in range(length):
+            prefClass.settings.setArrayIndex(index)
+            url = prefClass.settings.value("URL")
+            location = prefClass.settings.value("Location")
+            done = toBool(prefClass.settings.value("Done"))
+            pageUrl = prefClass.settings.value("PageURL")
+            if pageUrl is None:
+                pageUrl = QUrl()
+            downloads.append((url, location, done, pageUrl))
+        prefClass.settings.endArray()
+        return downloads
     elif key == "RssFeeds":
         # return a list of tuples of (URL, title, icon)
         feeds = []
@@ -2729,7 +2734,8 @@
 ##                 "SearchLanguage", "SyncType", "SyncFtpPort",
 ##                 "SyncFtpIdleTimeout", "SyncEncryptionKeyLength"]:
     elif key in ["StartupBehavior", "MinimumFontSize",
-                 "MinimumLogicalFontSize", "HistoryLimit"]:
+                 "MinimumLogicalFontSize", "HistoryLimit",
+                 "DownloadManagerRemovePolicy",]:
         return int(prefClass.settings.value(
             "WebBrowser/" + key, prefClass.webBrowserDefaults[key]))
 ##    elif key in ["SingleHelpWindow", "SaveGeometry", "WebSearchSuggestions",
@@ -2806,19 +2812,19 @@
             prefClass.settings.setValue("Engine", v[1])
             index += 1
         prefClass.settings.endArray()
-##    elif key == "DownloadManagerDownloads":
-##        # value is list of tuples of (URL, save location, done flag, page url)
-##        prefClass.settings.remove("Help/" + key)
-##        prefClass.settings.beginWriteArray("WebBrowser/" + key, len(value))
-##        index = 0
-##        for v in value:
-##            prefClass.settings.setArrayIndex(index)
-##            prefClass.settings.setValue("URL", v[0])
-##            prefClass.settings.setValue("Location", v[1])
-##            prefClass.settings.setValue("Done", v[2])
-##            prefClass.settings.setValue("PageURL", v[3])
-##            index += 1
-##        prefClass.settings.endArray()
+    elif key == "DownloadManagerDownloads":
+        # value is list of tuples of (URL, save location, done flag, page url)
+        prefClass.settings.remove("Help/" + key)
+        prefClass.settings.beginWriteArray("WebBrowser/" + key, len(value))
+        index = 0
+        for v in value:
+            prefClass.settings.setArrayIndex(index)
+            prefClass.settings.setValue("URL", v[0])
+            prefClass.settings.setValue("Location", v[1])
+            prefClass.settings.setValue("Done", v[2])
+            prefClass.settings.setValue("PageURL", v[3])
+            index += 1
+        prefClass.settings.endArray()
     elif key == "RssFeeds":
         # value is list of tuples of (URL, title, icon)
         prefClass.settings.remove("WebBrowser/" + key)
--- a/WebBrowser/Download/DownloadItem.py	Tue Feb 23 20:00:40 2016 +0100
+++ b/WebBrowser/Download/DownloadItem.py	Wed Feb 24 20:27:40 2016 +0100
@@ -8,26 +8,22 @@
 """
 
 from __future__ import unicode_literals
-try:
-    str = unicode
-except NameError:
-    pass
 
-from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QTime, QFile, QFileInfo, \
-    QUrl, QIODevice, QCryptographicHash, PYQT_VERSION_STR
+from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QTime, QFileInfo, QUrl, \
+    PYQT_VERSION_STR
 from PyQt5.QtGui import QPalette, QDesktopServices
 from PyQt5.QtWidgets import QWidget, QStyle, QDialog
-from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply
+from PyQt5.QtWebEngineWidgets import QWebEngineDownloadItem
 
 from E5Gui import E5FileDialog
 
 from .Ui_DownloadItem import Ui_DownloadItem
 
 from .DownloadUtilities import timeString, dataString
-##from ..HelpUtilities import parseContentDisposition
+from WebBrowser.WebBrowserWindow import WebBrowserWindow
 
 import UI.PixmapCache
-import Preferences
+import Utilities.MimeTypes
 
 
 class DownloadItem(QWidget, Ui_DownloadItem):
@@ -46,19 +42,14 @@
     DownloadSuccessful = 1
     DownloadCancelled = 2
     
-    def __init__(self, reply=None, requestFilename=False, webPage=None,
-                 download=False, parent=None, mainWindow=None):
+    def __init__(self, downloadItem, parent=None):
         """
         Constructor
         
-        @keyparam reply reference to the network reply object (QNetworkReply)
-        @keyparam requestFilename flag indicating to ask the user for a
-            filename (boolean)
-        @keyparam webPage reference to the web page object the download
-            originated from (QWebPage)
-        @keyparam download flag indicating a download operation (boolean)
+        @param downloadItem reference to the download object containing the
+        download data.
         @keyparam parent reference to the parent widget (QWidget)
-        @keyparam mainWindow reference to the main window (HelpWindow)
+        @type QWebEngineDownloadItem
         """
         super(DownloadItem, self).__init__(parent)
         self.setupUi(self)
@@ -69,93 +60,60 @@
         
         self.progressBar.setMaximum(0)
         
-        self.__isFtpDownload = reply is not None and \
-            reply.url().scheme() == "ftp"
-        
-        self.tryAgainButton.setIcon(UI.PixmapCache.getIcon("restart.png"))
-        self.tryAgainButton.setEnabled(False)
-        self.tryAgainButton.setVisible(False)
         self.stopButton.setIcon(UI.PixmapCache.getIcon("stopLoading.png"))
-        self.pauseButton.setIcon(UI.PixmapCache.getIcon("pause.png"))
         self.openButton.setIcon(UI.PixmapCache.getIcon("open.png"))
         self.openButton.setEnabled(False)
         self.openButton.setVisible(False)
-        if self.__isFtpDownload:
-            self.stopButton.setEnabled(False)
-            self.stopButton.setVisible(False)
-            self.pauseButton.setEnabled(False)
-            self.pauseButton.setVisible(False)
         
         self.__state = DownloadItem.Downloading
         
         icon = self.style().standardIcon(QStyle.SP_FileIcon)
         self.fileIcon.setPixmap(icon.pixmap(48, 48))
         
-        self.__mainWindow = mainWindow
-        self.__reply = reply
-        self.__requestFilename = requestFilename
-        self.__page = webPage
-        self.__pageUrl = webPage and webPage.mainFrame().url() or QUrl()
-        self.__toDownload = download
+        self.__downloadItem = downloadItem
+        self.__pageUrl = \
+            WebBrowserWindow.mainWindow().getWindow().currentBrowser().url() \
+            or QUrl()
         self.__bytesReceived = 0
         self.__bytesTotal = -1
         self.__downloadTime = QTime()
-        self.__output = QFile()
         self.__fileName = ""
         self.__originalFileName = ""
-        self.__startedSaving = False
         self.__finishedDownloading = False
         self.__gettingFileName = False
         self.__canceledFileSelect = False
         self.__autoOpen = False
         
-        self.__sha1Hash = QCryptographicHash(QCryptographicHash.Sha1)
-        self.__md5Hash = QCryptographicHash(QCryptographicHash.Md5)
-        
-        if not requestFilename:
-            self.__requestFilename = \
-                Preferences.getUI("RequestDownloadFilename")
-        
         self.__initialize()
     
-    def __initialize(self, tryAgain=False):
+    def __initialize(self):
         """
-        Private method to (re)initialize the widget.
-        
-        @param tryAgain flag indicating a retry (boolean)
+        Private method to initialize the widget.
         """
-        if self.__reply is None:
+        if self.__downloadItem is None:
             return
         
-        self.__startedSaving = False
         self.__finishedDownloading = False
         self.__bytesReceived = 0
         self.__bytesTotal = -1
         
-        self.__sha1Hash.reset()
-        self.__md5Hash.reset()
-        
         # start timer for the download estimation
         self.__downloadTime.start()
         
-        # attach to the reply object
-        self.__url = self.__reply.url()
-        self.__reply.setParent(self)
-        self.__reply.setReadBufferSize(16 * 1024 * 1024)
-        self.__reply.readyRead.connect(self.__readyRead)
-        self.__reply.error.connect(self.__networkError)
-        self.__reply.downloadProgress.connect(self.__downloadProgress)
-        self.__reply.metaDataChanged.connect(self.__metaDataChanged)
-        self.__reply.finished.connect(self.__finished)
+        # attach to the download item object
+        self.__url = self.__downloadItem.url()
+        self.__downloadItem.downloadProgress.connect(self.__downloadProgress)
+        self.__downloadItem.finished.connect(self.__finished)
         
         # reset info
         self.infoLabel.clear()
         self.progressBar.setValue(0)
         self.__getFileName()
-        
-        if self.__reply.error() != QNetworkReply.NoError:
-            self.__networkError()
-            self.__finished()
+        if not self.__fileName:
+            self.__downloadItem.cancel()
+        else:
+            self.__downloadItem.setPath(self.__fileName)
+            self.__downloadItem.accept()
     
     def __getFileName(self):
         """
@@ -164,7 +122,6 @@
         if self.__gettingFileName:
             return
         
-        from WebBrowser.WebBrowserWindow import WebBrowserWindow
         downloadDirectory = WebBrowserWindow\
             .downloadManager().downloadDirectory()
         
@@ -180,49 +137,47 @@
             self.__originalFileName = originalFileName
             ask = True
         self.__autoOpen = False
-        if not self.__toDownload:
-            from .DownloadAskActionDialog import DownloadAskActionDialog
-            url = self.__reply.url()
-            dlg = DownloadAskActionDialog(
-                QFileInfo(originalFileName).fileName(),
-                self.__reply.header(QNetworkRequest.ContentTypeHeader),
-                "{0}://{1}".format(url.scheme(), url.authority()),
-                self)
-            if dlg.exec_() == QDialog.Rejected or dlg.getAction() == "cancel":
-                self.progressBar.setVisible(False)
-                self.__reply.close()
-                self.on_stopButton_clicked()
-                self.filenameLabel.setText(
-                    self.tr("Download canceled: {0}").format(
-                        QFileInfo(defaultFileName).fileName()))
-                self.__canceledFileSelect = True
-                return
+        from .DownloadAskActionDialog import DownloadAskActionDialog
+        url = self.__downloadItem.url()
+        mimetype = Utilities.MimeTypes.mimeType(originalFileName)
+        dlg = DownloadAskActionDialog(
+            QFileInfo(originalFileName).fileName(),
+            mimetype,
+            "{0}://{1}".format(url.scheme(), url.authority()),
+            self)
+        if dlg.exec_() == QDialog.Rejected or dlg.getAction() == "cancel":
+            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 dlg.getAction() == "scan":
+            self.__mainWindow.requestVirusTotalScan(url)
             
-            if dlg.getAction() == "scan":
-                self.__mainWindow.requestVirusTotalScan(url)
-                
-                self.progressBar.setVisible(False)
-                self.__reply.close()
-                self.on_stopButton_clicked()
-                self.filenameLabel.setText(
-                    self.tr("VirusTotal scan scheduled: {0}").format(
-                        QFileInfo(defaultFileName).fileName()))
-                self.__canceledFileSelect = True
-                return
-            
-            self.__autoOpen = dlg.getAction() == "open"
-            if PYQT_VERSION_STR >= "5.0.0":
-                from PyQt5.QtCore import QStandardPaths
-                tempLocation = QStandardPaths.storageLocation(
-                    QStandardPaths.TempLocation)
-            else:
-                from PyQt5.QtGui import QDesktopServices
-                tempLocation = QDesktopServices.storageLocation(
-                    QDesktopServices.TempLocation)
-            fileName = tempLocation + '/' + \
-                QFileInfo(fileName).completeBaseName()
+            self.progressBar.setVisible(False)
+            self.on_stopButton_clicked()
+            self.filenameLabel.setText(
+                self.tr("VirusTotal scan scheduled: {0}").format(
+                    QFileInfo(defaultFileName).fileName()))
+            self.__canceledFileSelect = True
+            return
         
-        if ask and not self.__autoOpen and self.__requestFilename:
+        self.__autoOpen = dlg.getAction() == "open"
+        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:
             self.__gettingFileName = True
             fileName = E5FileDialog.getSaveFileName(
                 None,
@@ -232,7 +187,6 @@
             self.__gettingFileName = False
             if not fileName:
                 self.progressBar.setVisible(False)
-                self.__reply.close()
                 self.on_stopButton_clicked()
                 self.filenameLabel.setText(
                     self.tr("Download canceled: {0}")
@@ -245,7 +199,6 @@
             .setDownloadDirectory(fileInfo.absoluteDir().absolutePath())
         self.filenameLabel.setText(fileInfo.fileName())
         
-        self.__output.setFileName(fileName + ".part")
         self.__fileName = fileName
         
         # check file path for saving
@@ -260,8 +213,6 @@
                 return
         
         self.filenameLabel.setText(QFileInfo(self.__fileName).fileName())
-        if self.__requestFilename:
-            self.__readyRead()
     
     def __saveFileName(self, directory):
         """
@@ -270,7 +221,7 @@
         @param directory name of the directory to store the file into (string)
         @return proposed filename and original filename (string, string)
         """
-        path = parseContentDisposition(self.__reply)
+        path = self.__downloadItem.path()
         info = QFileInfo(path)
         baseName = info.completeBaseName()
         endName = info.suffix()
@@ -282,80 +233,17 @@
         name = directory + baseName
         if endName:
             name += '.' + endName
-            if not self.__requestFilename:
-                # do not overwrite, if the user is not being asked
-                i = 1
-                while QFile.exists(name):
-                    # file exists already, don't overwrite
-                    name = directory + baseName + ('-{0:d}'.format(i))
-                    if endName:
-                        name += '.' + endName
-                    i += 1
         return name, origName
     
     def __open(self):
         """
         Private slot to open the downloaded file.
         """
-        info = QFileInfo(self.__output)
+        info = QFileInfo(self.__fileName)
         url = QUrl.fromLocalFile(info.absoluteFilePath())
         QDesktopServices.openUrl(url)
     
     @pyqtSlot()
-    def on_tryAgainButton_clicked(self):
-        """
-        Private slot to retry the download.
-        """
-        self.retry()
-    
-    def retry(self):
-        """
-        Public slot to retry the download.
-        """
-        if not self.tryAgainButton.isEnabled():
-            return
-        
-        self.tryAgainButton.setEnabled(False)
-        self.tryAgainButton.setVisible(False)
-        self.openButton.setEnabled(False)
-        self.openButton.setVisible(False)
-        if not self.__isFtpDownload:
-            self.stopButton.setEnabled(True)
-            self.stopButton.setVisible(True)
-            self.pauseButton.setEnabled(True)
-            self.pauseButton.setVisible(True)
-        self.progressBar.setVisible(True)
-        
-        if self.__page:
-            nam = self.__page.networkAccessManager()
-        else:
-            from WebBrowser.WebBrowserWindow import WebBrowserWindow
-            nam = WebBrowserWindow.networkAccessManager()
-        reply = nam.get(QNetworkRequest(self.__url))
-        if self.__output.exists():
-            self.__output.remove()
-        self.__output = QFile()
-        self.__reply = reply
-        self.__initialize(tryAgain=True)
-        self.__state = DownloadItem.Downloading
-        self.statusChanged.emit()
-    
-    @pyqtSlot(bool)
-    def on_pauseButton_clicked(self, checked):
-        """
-        Private slot to pause the download.
-        
-        @param checked flag indicating the state of the button (boolean)
-        """
-        if checked:
-            self.__reply.readyRead.disconnect(self.__readyRead)
-            self.__reply.setReadBufferSize(16 * 1024)
-        else:
-            self.__reply.readyRead.connect(self.__readyRead)
-            self.__reply.setReadBufferSize(16 * 1024 * 1024)
-            self.__readyRead()
-    
-    @pyqtSlot()
     def on_stopButton_clicked(self):
         """
         Private slot to stop the download.
@@ -367,18 +255,13 @@
         Public slot to stop the download.
         """
         self.setUpdatesEnabled(False)
-        if not self.__isFtpDownload:
-            self.stopButton.setEnabled(False)
-            self.stopButton.setVisible(False)
-            self.pauseButton.setEnabled(False)
-            self.pauseButton.setVisible(False)
-        self.tryAgainButton.setEnabled(True)
-        self.tryAgainButton.setVisible(True)
+        self.stopButton.setEnabled(False)
+        self.stopButton.setVisible(False)
         self.openButton.setEnabled(False)
         self.openButton.setVisible(False)
         self.setUpdatesEnabled(True)
         self.__state = DownloadItem.DownloadCancelled
-        self.__reply.abort()
+        self.__downloadItem.cancel()
         self.downloadFinished.emit()
     
     @pyqtSlot()
@@ -404,63 +287,6 @@
         url = QUrl.fromLocalFile(info.absolutePath())
         QDesktopServices.openUrl(url)
     
-    def __readyRead(self):
-        """
-        Private slot to read the available data.
-        """
-        if self.__requestFilename and not self.__output.fileName():
-            return
-        
-        if not self.__output.isOpen():
-            # in case someone else has already put a file there
-            if not self.__requestFilename:
-                self.__getFileName()
-            if not self.__output.open(QIODevice.WriteOnly):
-                self.infoLabel.setText(
-                    self.tr("Error opening save file: {0}")
-                    .format(self.__output.errorString()))
-                self.on_stopButton_clicked()
-                self.statusChanged.emit()
-                return
-            self.statusChanged.emit()
-        
-        buffer = self.__reply.readAll()
-        self.__sha1Hash.addData(buffer)
-        self.__md5Hash.addData(buffer)
-        bytesWritten = self.__output.write(buffer)
-        if bytesWritten == -1:
-            self.infoLabel.setText(
-                self.tr("Error saving: {0}")
-                    .format(self.__output.errorString()))
-            self.on_stopButton_clicked()
-        else:
-            self.__startedSaving = True
-            if self.__finishedDownloading:
-                self.__finished()
-    
-    def __networkError(self):
-        """
-        Private slot to handle a network error.
-        """
-        self.infoLabel.setText(
-            self.tr("Network Error: {0}")
-                .format(self.__reply.errorString()))
-        self.tryAgainButton.setEnabled(True)
-        self.tryAgainButton.setVisible(True)
-        self.downloadFinished.emit()
-    
-    def __metaDataChanged(self):
-        """
-        Private slot to handle a change of the meta data.
-        """
-        locationHeader = self.__reply.header(QNetworkRequest.LocationHeader)
-        if locationHeader and locationHeader.isValid():
-            self.__url = QUrl(locationHeader)
-            from WebBrowser.WebBrowserWindow import WebBrowserWindow
-            self.__reply = WebBrowserWindow\
-                .networkAccessManager().get(QNetworkRequest(self.__url))
-            self.__initialize()
-    
     def __downloadProgress(self, bytesReceived, bytesTotal):
         """
         Private method to show the download progress.
@@ -488,10 +314,7 @@
         @return total number of bytes (integer)
         """
         if self.__bytesTotal == -1:
-            self.__bytesTotal = self.__reply.header(
-                QNetworkRequest.ContentLengthHeader)
-            if self.__bytesTotal is None:
-                self.__bytesTotal = -1
+            self.__bytesTotal = self.__downloadItem.totalBytes()
         return self.__bytesTotal
     
     def bytesReceived(self):
@@ -541,9 +364,6 @@
         """
         Private method to update the info label.
         """
-        if self.__reply.error() != QNetworkReply.NoError:
-            return
-        
         bytesTotal = self.bytesTotal()
         running = not self.downloadedSuccessfully()
         
@@ -566,13 +386,8 @@
                     remaining)
         else:
             if self.__bytesReceived == bytesTotal or bytesTotal == -1:
-                info = self.tr("{0} downloaded\nSHA1: {1}\nMD5: {2}")\
-                    .format(dataString(self.__output.size()),
-                            str(self.__sha1Hash.result().toHex(),
-                                encoding="ascii"),
-                            str(self.__md5Hash.result().toHex(),
-                                encoding="ascii")
-                            )
+                info = self.tr("{0} downloaded")\
+                    .format(dataString(self.__output.size()))
             else:
                 info = self.tr("{0} of {1} - Stopped")\
                     .format(dataString(self.__bytesReceived),
@@ -608,23 +423,15 @@
         Private slot to handle the download finished.
         """
         self.__finishedDownloading = True
-        if not self.__startedSaving:
-            return
         
-        noError = self.__reply.error() == QNetworkReply.NoError
+        noError = (self.__downloadItem.state() == 
+                   QWebEngineDownloadItem.DownloadCompleted)
         
         self.progressBar.setVisible(False)
-        if not self.__isFtpDownload:
-            self.stopButton.setEnabled(False)
-            self.stopButton.setVisible(False)
-            self.pauseButton.setEnabled(False)
-            self.pauseButton.setVisible(False)
+        self.stopButton.setEnabled(False)
+        self.stopButton.setVisible(False)
         self.openButton.setEnabled(noError)
         self.openButton.setVisible(noError)
-        self.__output.close()
-        if QFile.exists(self.__fileName):
-            QFile.remove(self.__fileName)
-        self.__output.rename(self.__fileName)
         self.__updateInfoLabel()
         self.__state = DownloadItem.DownloadSuccessful
         self.statusChanged.emit()
@@ -685,19 +492,14 @@
         self.__url = data[0]
         self.__fileName = data[1]
         self.__pageUrl = data[3]
-        self.__isFtpDownload = self.__url.scheme() == "ftp"
         
         self.filenameLabel.setText(QFileInfo(self.__fileName).fileName())
         self.infoLabel.setText(self.__fileName)
         
         self.stopButton.setEnabled(False)
         self.stopButton.setVisible(False)
-        self.pauseButton.setEnabled(False)
-        self.pauseButton.setVisible(False)
         self.openButton.setEnabled(data[2])
         self.openButton.setVisible(data[2])
-        self.tryAgainButton.setEnabled(not data[2])
-        self.tryAgainButton.setVisible(not data[2])
         if data[2]:
             self.__state = DownloadItem.DownloadSuccessful
         else:
--- a/WebBrowser/Download/DownloadItem.ui	Tue Feb 23 20:00:40 2016 +0100
+++ b/WebBrowser/Download/DownloadItem.ui	Wed Feb 24 20:27:40 2016 +0100
@@ -7,7 +7,7 @@
     <x>0</x>
     <y>0</y>
     <width>400</width>
-    <height>82</height>
+    <height>87</height>
    </rect>
   </property>
   <property name="sizePolicy">
@@ -73,29 +73,6 @@
     </layout>
    </item>
    <item>
-    <widget class="QToolButton" name="tryAgainButton">
-     <property name="toolTip">
-      <string>Press to repeat the download</string>
-     </property>
-     <property name="text">
-      <string/>
-     </property>
-    </widget>
-   </item>
-   <item>
-    <widget class="QToolButton" name="pauseButton">
-     <property name="toolTip">
-      <string>Press to pause the download</string>
-     </property>
-     <property name="text">
-      <string/>
-     </property>
-     <property name="checkable">
-      <bool>true</bool>
-     </property>
-    </widget>
-   </item>
-   <item>
     <widget class="QToolButton" name="stopButton">
      <property name="toolTip">
       <string>Press to cancel the download</string>
@@ -118,8 +95,6 @@
   </layout>
  </widget>
  <tabstops>
-  <tabstop>tryAgainButton</tabstop>
-  <tabstop>pauseButton</tabstop>
   <tabstop>stopButton</tabstop>
   <tabstop>openButton</tabstop>
  </tabstops>
--- a/WebBrowser/Download/DownloadManager.py	Tue Feb 23 20:00:40 2016 +0100
+++ b/WebBrowser/Download/DownloadManager.py	Wed Feb 24 20:27:40 2016 +0100
@@ -10,11 +10,9 @@
 from __future__ import unicode_literals
 
 from PyQt5.QtCore import pyqtSlot, Qt, QModelIndex, QFileInfo
-from PyQt5.QtGui import QCursor
+from PyQt5.QtGui import QCursor, QKeySequence
 from PyQt5.QtWidgets import QDialog, QStyle, QFileIconProvider, QMenu, \
-    QApplication
-from PyQt5.QtNetwork import QNetworkRequest
-from PyQt5.QtWebKit import QWebSettings
+    QApplication, QShortcut
 
 from E5Gui import E5MessageBox
 
@@ -50,8 +48,7 @@
         self.__saveTimer = AutoSaver(self, self.save)
         
         self.__model = DownloadModel(self)
-        self.__manager = Helpviewer.HelpWindow.HelpWindow\
-            .networkAccessManager()
+        self.__manager = WebBrowserWindow.networkManager()
         
         self.__iconProvider = None
         self.__downloads = []
@@ -70,6 +67,9 @@
         self.downloadsView.customContextMenuRequested.connect(
             self.__customContextMenuRequested)
         
+        self.__clearShortcut = QShortcut(QKeySequence("Ctrl+L"), self)
+        self.__clearShortcut.activated.connect(self.on_cleanupButton_clicked)
+        
         self.__load()
     
     def __customContextMenuRequested(self, pos):
@@ -164,51 +164,19 @@
                 return False
         return True
     
-    def download(self, requestOrUrl, requestFileName=False, mainWindow=None):
+    def download(self, downloadItem):
         """
         Public method to download a file.
         
-        @param requestOrUrl reference to a request object (QNetworkRequest)
-            or a URL to be downloaded (QUrl)
-        @keyparam requestFileName flag indicating to ask for the
-            download file name (boolean)
-        @keyparam mainWindow reference to the main window (HelpWindow)
+        @param downloadItem reference to the download object containing the
+        download data.
+        @type QWebEngineDownloadItem
         """
-        request = QNetworkRequest(requestOrUrl)
-        if request.url().isEmpty():
-            return
-        self.handleUnsupportedContent(
-            self.__manager.get(request),
-            requestFileName=requestFileName,
-            download=True,
-            mainWindow=mainWindow)
-    
-    def handleUnsupportedContent(self, reply, requestFileName=False,
-                                 webPage=None, download=False,
-                                 mainWindow=None):
-        """
-        Public method to handle unsupported content by downloading the
-        referenced resource.
-        
-        @param reply reference to the reply object (QNetworkReply)
-        @keyparam requestFileName indicating to ask for a filename
-            (boolean)
-        @keyparam webPage reference to the web page (HelpWebPage)
-        @keyparam download flag indicating a download request (boolean)
-        @keyparam mainWindow reference to the main window (HelpWindow)
-        """
-        if reply is None or reply.url().isEmpty():
-            return
-        
-        size = reply.header(QNetworkRequest.ContentLengthHeader)
-        if size == 0:
+        if downloadItem.url().isEmpty():
             return
         
         from .DownloadItem import DownloadItem
-        itm = DownloadItem(
-            reply=reply, requestFilename=requestFileName,
-            webPage=webPage, download=download, parent=self,
-            mainWindow=mainWindow)
+        itm = DownloadItem(downloadItem, parent=self)
         self.__addItem(itm)
         
         if itm.canceledFileSelect():
@@ -271,10 +239,11 @@
             max(oldHeight, itm.minimumSizeHint().height() * 1.5))
         
         remove = False
-        globalSettings = QWebSettings.globalSettings()
-        if not itm.downloading() and \
-           globalSettings.testAttribute(QWebSettings.PrivateBrowsingEnabled):
-            remove = True
+        # TODO: Private Browsing
+##        globalSettings = QWebSettings.globalSettings()
+##        if not itm.downloading() and \
+##           globalSettings.testAttribute(QWebSettings.PrivateBrowsingEnabled):
+##            remove = True
         
         if itm.downloadedSuccessfully() and \
            self.removePolicy() == DownloadManager.RemoveSuccessFullDownload:
@@ -312,7 +281,7 @@
         if policy == self.removePolicy():
             return
         
-        Preferences.getWebBrowser("DownloadManagerRemovePolicy", self.policy)
+        Preferences.setWebBrowser("DownloadManagerRemovePolicy", self.policy)
     
     def save(self):
         """
@@ -326,10 +295,11 @@
         if self.removePolicy() == DownloadManager.RemoveExit:
             return
         
-        downloads = []
-        for download in self.__downloads:
-            downloads.append(download.getData())
-        Preferences.setWebBrowser("DownloadManagerDownloads", downloads)
+        # TODO: Downloads: check saving downloads
+##        downloads = []
+##        for download in self.__downloads:
+##            downloads.append(download.getData())
+##        Preferences.setWebBrowser("DownloadManagerDownloads", downloads)
     
     def __load(self):
         """
@@ -344,14 +314,15 @@
         pos = Preferences.getWebBrowser("DownloadManagerPosition")
         self.move(pos)
         
-        downloads = Preferences.getWebBrowser("DownloadManagerDownloads")
-        for download in downloads:
-            if not download[0].isEmpty() and \
-               download[1] != "":
-                from .DownloadItem import DownloadItem
-                itm = DownloadItem(parent=self)
-                itm.setData(download)
-                self.__addItem(itm)
+        # TODO: Downloads: check laoding downloads
+##        downloads = Preferences.getWebBrowser("DownloadManagerDownloads")
+##        for download in downloads:
+##            if not download[0].isEmpty() and \
+##               download[1] != "":
+##                from .DownloadItem import DownloadItem
+##                itm = DownloadItem(parent=self)
+##                itm.setData(download)
+##                self.__addItem(itm)
         self.cleanupButton.setEnabled(
             (len(self.__downloads) - self.activeDownloads()) > 0)
         
@@ -451,6 +422,7 @@
     ## Context menu related methods below
     ###########################################################################
     
+    # TODO: Downloads: check the context menu actions
     def __currentItem(self):
         """
         Private method to get a reference to the current item.
--- a/WebBrowser/WebBrowserWindow.py	Tue Feb 23 20:00:40 2016 +0100
+++ b/WebBrowser/WebBrowserWindow.py	Wed Feb 24 20:27:40 2016 +0100
@@ -88,7 +88,7 @@
     _historyManager = None
     _passwordManager = None
 ##    _adblockManager = None
-##    _downloadManager = None
+    _downloadManager = None
     _feedsManager = None
 ##    _userAgentsManager = None
 ##    _syncManager = None
@@ -1624,21 +1624,20 @@
 ##                self.__showNetworkMonitor)
 ##        self.__actions.append(self.toolsMonitorAct)
         
-        # TODO: Download Manager
-##        self.showDownloadManagerAct = E5Action(
-##            self.tr('Downloads'),
-##            self.tr('Downloads'),
-##            0, 0, self, 'webbrowser_show_downloads')
-##        self.showDownloadManagerAct.setStatusTip(self.tr(
-##            'Shows the downloads window'))
-##        self.showDownloadManagerAct.setWhatsThis(self.tr(
-##            """<b>Downloads</b>"""
-##            """<p>Shows the downloads window.</p>"""
-##        ))
-##        if not self.__initShortcutsOnly:
-##            self.showDownloadManagerAct.triggered.connect(
-##                self.__showDownloadsWindow)
-##        self.__actions.append(self.showDownloadManagerAct)
+        self.showDownloadManagerAct = E5Action(
+            self.tr('Downloads'),
+            self.tr('Downloads'),
+            0, 0, self, 'webbrowser_show_downloads')
+        self.showDownloadManagerAct.setStatusTip(self.tr(
+            'Shows the downloads window'))
+        self.showDownloadManagerAct.setWhatsThis(self.tr(
+            """<b>Downloads</b>"""
+            """<p>Shows the downloads window.</p>"""
+        ))
+        if not self.__initShortcutsOnly:
+            self.showDownloadManagerAct.triggered.connect(
+                self.__showDownloadsWindow)
+        self.__actions.append(self.showDownloadManagerAct)
         
         self.feedsManagerAct = E5Action(
             self.tr('RSS Feeds Dialog'),
@@ -1893,14 +1892,14 @@
         
         menu = mb.addMenu(self.tr("&Window"))
         menu.setTearOffEnabled(True)
-##        menu.addAction(self.showDownloadManagerAct)
+        menu.addAction(self.showDownloadManagerAct)
 ##        if WebBrowserWindow.UseQtHelp:
 ##            menu.addSeparator()
 ##            menu.addAction(self.showTocAct)
 ##            menu.addAction(self.showIndexAct)
 ##            menu.addAction(self.showSearchAct)
-##        
-##        mb.addSeparator()
+        
+        mb.addSeparator()
         
         menu = mb.addMenu(self.tr('&Help'))
         menu.setTearOffEnabled(True)
@@ -2460,11 +2459,11 @@
         if not self.__tabWidget.shallShutDown():
             return False
         
-##        if not self.downloadManager().allowQuit():
-##            return False
-##        
-##        self.downloadManager().shutdown()
-##        
+        if not self.downloadManager().allowQuit():
+            return False
+        
+        self.downloadManager().shutdown()
+        
 ##        self.__closeNetworkMonitor()
 ##        
 ##        self.cookieJar().close()
@@ -3289,10 +3288,9 @@
                 self.__tabWidget.clearClosedTabsList()
             if searches:
                 self.searchEdit.clear()
-            # TODO: Downloads
-##            if downloads:
-##                self.downloadManager().cleanup()
-##                self.downloadManager().hide()
+            if downloads:
+                self.downloadManager().cleanup()
+                self.downloadManager().hide()
             if favicons:
                 self.__clearIconsDatabase()
             # TODO: Cache Cleaning
@@ -3403,12 +3401,12 @@
 ##        monitor = E5NetworkMonitor.instance(self.networkManager())
 ##        monitor.show()
 ##        
-##    def __showDownloadsWindow(self):
-##        """
-##        Private slot to show the downloads dialog.
-##        """
-##        self.downloadManager().show()
-##        
+    def __showDownloadsWindow(self):
+        """
+        Private slot to show the downloads dialog.
+        """
+        self.downloadManager().show()
+        
 ##    def __closeNetworkMonitor(self):
 ##        """
 ##        Private slot to close the network monitor dialog.
@@ -3526,19 +3524,19 @@
 ##        """
 ##        return self.__adBlockIcon
 ##    
-##    @classmethod
-##    def downloadManager(cls):
-##        """
-##        Class method to get a reference to the download manager.
-##        
-##        @return reference to the download manager (DownloadManager)
-##        """
-##        if cls._downloadManager is None:
-##            from .Download.DownloadManager import DownloadManager
-##            cls._downloadManager = DownloadManager()
-##        
-##        return cls._downloadManager
-##        
+    @classmethod
+    def downloadManager(cls):
+        """
+        Class method to get a reference to the download manager.
+        
+        @return reference to the download manager (DownloadManager)
+        """
+        if cls._downloadManager is None:
+            from .Download.DownloadManager import DownloadManager
+            cls._downloadManager = DownloadManager()
+        
+        return cls._downloadManager
+        
     @classmethod
     def personalInformationManager(cls):
         """
@@ -4084,9 +4082,7 @@
         @param download reference to the download data
         @type QWebEngineDownloadItem
         """
-        pass
-        # TODO: DownloadManager
-##        self.downloadManager().download(download, mainWindow=self)
+        self.downloadManager().download(download)
     
     ########################################
     ## Support for web engine profiles below

eric ide

mercurial