Improved maintainability of the DownloadItem code.

Thu, 25 Jan 2018 17:21:00 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 25 Jan 2018 17:21:00 +0100
changeset 6091
7b989321d74c
parent 6090
5cdb7ea57551
child 6092
f0d60c3de700

Improved maintainability of the DownloadItem code.

Documentation/Help/source.qch file | annotate | diff | comparison | revisions
Documentation/Help/source.qhp file | annotate | diff | comparison | revisions
Documentation/Source/eric6.Helpviewer.Download.DownloadItem.html file | annotate | diff | comparison | revisions
Documentation/Source/eric6.WebBrowser.Download.DownloadItem.html file | annotate | diff | comparison | revisions
Helpviewer/Download/DownloadItem.py file | annotate | diff | comparison | revisions
Preferences/__init__.py file | annotate | diff | comparison | revisions
WebBrowser/Download/DownloadItem.py file | annotate | diff | comparison | revisions
WebBrowser/Download/DownloadManager.py file | annotate | diff | comparison | revisions
Binary file Documentation/Help/source.qch has changed
--- a/Documentation/Help/source.qhp	Thu Jan 25 16:39:06 2018 +0100
+++ b/Documentation/Help/source.qhp	Thu Jan 25 17:21:00 2018 +0100
@@ -4166,8 +4166,6 @@
       <keyword name="DownloadItem.__initialize" id="DownloadItem.__initialize" ref="eric6.WebBrowser.Download.DownloadItem.html#DownloadItem.__initialize" />
       <keyword name="DownloadItem.__metaDataChanged" id="DownloadItem.__metaDataChanged" ref="eric6.Helpviewer.Download.DownloadItem.html#DownloadItem.__metaDataChanged" />
       <keyword name="DownloadItem.__networkError" id="DownloadItem.__networkError" ref="eric6.Helpviewer.Download.DownloadItem.html#DownloadItem.__networkError" />
-      <keyword name="DownloadItem.__open" id="DownloadItem.__open" ref="eric6.Helpviewer.Download.DownloadItem.html#DownloadItem.__open" />
-      <keyword name="DownloadItem.__open" id="DownloadItem.__open" ref="eric6.WebBrowser.Download.DownloadItem.html#DownloadItem.__open" />
       <keyword name="DownloadItem.__readyRead" id="DownloadItem.__readyRead" ref="eric6.Helpviewer.Download.DownloadItem.html#DownloadItem.__readyRead" />
       <keyword name="DownloadItem.__saveFileName" id="DownloadItem.__saveFileName" ref="eric6.Helpviewer.Download.DownloadItem.html#DownloadItem.__saveFileName" />
       <keyword name="DownloadItem.__saveFileName" id="DownloadItem.__saveFileName" ref="eric6.WebBrowser.Download.DownloadItem.html#DownloadItem.__saveFileName" />
--- a/Documentation/Source/eric6.Helpviewer.Download.DownloadItem.html	Thu Jan 25 16:39:06 2018 +0100
+++ b/Documentation/Source/eric6.Helpviewer.Download.DownloadItem.html	Thu Jan 25 17:21:00 2018 +0100
@@ -93,9 +93,6 @@
 <td><a href="#DownloadItem.__networkError">__networkError</a></td>
 <td>Private slot to handle a network error.</td>
 </tr><tr>
-<td><a href="#DownloadItem.__open">__open</a></td>
-<td>Private slot to open the downloaded file.</td>
-</tr><tr>
 <td><a href="#DownloadItem.__readyRead">__readyRead</a></td>
 <td>Private slot to read the available data.</td>
 </tr><tr>
@@ -257,11 +254,6 @@
 <b>__networkError</b>(<i></i>)
 <p>
         Private slot to handle a network error.
-</p><a NAME="DownloadItem.__open" ID="DownloadItem.__open"></a>
-<h4>DownloadItem.__open</h4>
-<b>__open</b>(<i></i>)
-<p>
-        Private slot to open the downloaded file.
 </p><a NAME="DownloadItem.__readyRead" ID="DownloadItem.__readyRead"></a>
 <h4>DownloadItem.__readyRead</h4>
 <b>__readyRead</b>(<i></i>)
--- a/Documentation/Source/eric6.WebBrowser.Download.DownloadItem.html	Thu Jan 25 16:39:06 2018 +0100
+++ b/Documentation/Source/eric6.WebBrowser.Download.DownloadItem.html	Thu Jan 25 17:21:00 2018 +0100
@@ -87,9 +87,6 @@
 <td><a href="#DownloadItem.__initialize">__initialize</a></td>
 <td>Private method to initialize the widget.</td>
 </tr><tr>
-<td><a href="#DownloadItem.__open">__open</a></td>
-<td>Private slot to open the downloaded file.</td>
-</tr><tr>
 <td><a href="#DownloadItem.__saveFileName">__saveFileName</a></td>
 <td>Private method to calculate a name for the file to download.</td>
 </tr><tr>
@@ -217,11 +214,6 @@
 <b>__initialize</b>(<i></i>)
 <p>
         Private method to initialize the widget.
-</p><a NAME="DownloadItem.__open" ID="DownloadItem.__open"></a>
-<h4>DownloadItem.__open</h4>
-<b>__open</b>(<i></i>)
-<p>
-        Private slot to open the downloaded file.
 </p><a NAME="DownloadItem.__saveFileName" ID="DownloadItem.__saveFileName"></a>
 <h4>DownloadItem.__saveFileName</h4>
 <b>__saveFileName</b>(<i>directory</i>)
@@ -365,14 +357,15 @@
 </p><dl>
 <dt>Returns:</dt>
 <dd>
-tuple of URL, save location, flag, the
-            URL of the related web page and the date and time
-            of the download
+dictionary containing the URL, save location, done flag,
+            the URL of the related web page and the date and time of the
+            download
 </dd>
 </dl><dl>
 <dt>Return Type:</dt>
 <dd>
-tuple of (QUrl, str, bool, QUrl, QDateTime)
+dict of {"URL": QUrl, "Location": str, "Done": bool,
+            "PageURL": QUrl, "Downloaded": QDateTime}
 </dd>
 </dl><a NAME="DownloadItem.getInfoData" ID="DownloadItem.getInfoData"></a>
 <h4>DownloadItem.getInfoData</h4>
@@ -440,11 +433,12 @@
 <p>
         Public method to set the relevant download data.
 </p><dl>
-<dt><i>data</i> (QUrl, str, bool, QUrl, QDateTime)</dt>
+<dt><i>data</i> (dict of {"URL": QUrl, "Location": str, "Done": bool,)</dt>
 <dd>
-tuple of URL, save location, flag, the
-            URL of the related web page and the date and time
-            of the download
+dictionary containing the URL, save location, done flag,
+            the URL of the related web page and the date and time of the
+            download
+            "PageURL": QUrl, "Downloaded": QDateTime}
 </dd>
 </dl><a NAME="DownloadItem.setIcon" ID="DownloadItem.setIcon"></a>
 <h4>DownloadItem.setIcon</h4>
--- a/Helpviewer/Download/DownloadItem.py	Thu Jan 25 16:39:06 2018 +0100
+++ b/Helpviewer/Download/DownloadItem.py	Thu Jan 25 17:21:00 2018 +0100
@@ -299,14 +299,6 @@
                     i += 1
         return name, origName
     
-    def __open(self):
-        """
-        Private slot to open the downloaded file.
-        """
-        info = QFileInfo(self.__output)
-        url = QUrl.fromLocalFile(info.absoluteFilePath())
-        QDesktopServices.openUrl(url)
-    
     @pyqtSlot()
     def on_tryAgainButton_clicked(self):
         """
@@ -642,7 +634,7 @@
         self.downloadFinished.emit()
         
         if self.__autoOpen:
-            self.__open()
+            self.openFile()
     
     def canceledFileSelect(self):
         """
--- a/Preferences/__init__.py	Thu Jan 25 16:39:06 2018 +0100
+++ b/Preferences/__init__.py	Thu Jan 25 17:21:00 2018 +0100
@@ -2728,7 +2728,7 @@
             keywords.append((keyword, engineName))
         prefClass.settings.endArray()
         return keywords
-    elif key in ["DownloadManagerDownloads"]:
+    elif key == "DownloadManagerDownloads":
         # return a list of tuples of (URL, save location, done flag, page url,
         # date/time downloaded)
         downloads = []
@@ -2926,26 +2926,27 @@
             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,
-        # date/time downloaded)
+    elif key == "DownloadManagerDownloads":
+        # return a list of dicts containing the URL, save location, done flag,
+        # page URL, date/time downloaded
         downloads = []
         length = prefClass.settings.beginReadArray("WebBrowser/" + key)
         for index in range(length):
+            download = {}
             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()
-            downloaded = prefClass.settings.value("Downloaded")
-            if downloaded is None:
-                downloaded = QDateTime()
+            download["URL"] = prefClass.settings.value("URL")
+            download["Location"] = prefClass.settings.value("Location")
+            download["Done"] = toBool(prefClass.settings.value("Done"))
+            download["PageURL"] = prefClass.settings.value("PageURL")
+            if download["PageURL"] is None:
+                download["PageURL"] = QUrl()
+            download["Downloaded"] = prefClass.settings.value("Downloaded")
+            if download["Downloaded"] is None:
+                download["Downloaded"] = QDateTime()
             else:
-                downloaded = QDateTime.fromString(
-                    downloaded, "yyyy-MM-dd hh:mm:ss")
-            downloads.append((url, location, done, pageUrl, downloaded))
+                download["Downloaded"] = QDateTime.fromString(
+                    download["Downloaded"], "yyyy-MM-dd hh:mm:ss")
+            downloads.append(download)
         prefClass.settings.endArray()
         return downloads
     elif key == "RssFeeds":
@@ -3060,19 +3061,19 @@
             index += 1
         prefClass.settings.endArray()
     elif key == "DownloadManagerDownloads":
-        # value is list of tuples of (URL, save location, done flag, page url,
-        # date/time downloaded)
+        # value is list of dicts containing the URL, save location, done flag,
+        # page URL, date/time downloaded
         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])
+            prefClass.settings.setValue("URL", v["URL"])
+            prefClass.settings.setValue("Location", v["Location"])
+            prefClass.settings.setValue("Done", v["Done"])
+            prefClass.settings.setValue("PageURL", v["PageURL"])
             prefClass.settings.setValue(
-                "Downloaded", v[4].toString("yyyy-MM-dd hh:mm:ss"))
+                "Downloaded", v["Downloaded"].toString("yyyy-MM-dd hh:mm:ss"))
             index += 1
         prefClass.settings.endArray()
     elif key == "RssFeeds":
--- a/WebBrowser/Download/DownloadItem.py	Thu Jan 25 16:39:06 2018 +0100
+++ b/WebBrowser/Download/DownloadItem.py	Thu Jan 25 17:21:00 2018 +0100
@@ -319,15 +319,6 @@
             name += '.' + endName
         return name, origName
     
-    # TODO: duplicate code to openFile()
-    def __open(self):
-        """
-        Private slot to open the downloaded file.
-        """
-        info = QFileInfo(self.__fileName)
-        url = QUrl.fromLocalFile(info.absoluteFilePath())
-        QDesktopServices.openUrl(url)
-    
     @pyqtSlot(bool)
     def on_pauseButton_clicked(self, checked):
         """
@@ -545,7 +536,7 @@
         self.downloadFinished.emit()
         
         if self.__autoOpen:
-            self.__open()
+            self.openFile()
     
     def canceledFileSelect(self):
         """
@@ -579,49 +570,53 @@
         """
         return QFileInfo(self.__fileName).absoluteFilePath()
     
-    # TODO: change from tuple to dict
     def getData(self):
         """
         Public method to get the relevant download data.
         
-        @return tuple of URL, save location, flag, the
-            URL of the related web page and the date and time
-            of the download
-        @rtype tuple of (QUrl, str, bool, QUrl, QDateTime)
+        @return dictionary containing the URL, save location, done flag,
+            the URL of the related web page and the date and time of the
+            download
+        @rtype dict of {"URL": QUrl, "Location": str, "Done": bool,
+            "PageURL": QUrl, "Downloaded": QDateTime}
         """
-        return (self.__url, QFileInfo(self.__fileName).filePath(),
-                self.downloadedSuccessfully(), self.__pageUrl,
-                self.__downloadedDateTime)
+        return {
+            "URL": self.__url,
+            "Location": QFileInfo(self.__fileName).filePath(),
+            "Done": self.downloadedSuccessfully(),
+            "PageURL": self.__pageUrl,
+            "Downloaded": self.__downloadedDateTime
+        }
     
-    # TODO: change from tuple to dict
     def setData(self, data):
         """
         Public method to set the relevant download data.
         
-        @param data tuple of URL, save location, flag, the
-            URL of the related web page and the date and time
-            of the download
-        @type QUrl, str, bool, QUrl, QDateTime
+        @param data dictionary containing the URL, save location, done flag,
+            the URL of the related web page and the date and time of the
+            download
+        @type dict of {"URL": QUrl, "Location": str, "Done": bool,
+            "PageURL": QUrl, "Downloaded": QDateTime}
         """
-        self.__url = data[0]
-        self.__fileName = data[1]
-        self.__pageUrl = data[3]
+        self.__url = data["URL"]
+        self.__fileName = data["Location"]
+        self.__pageUrl = data["PageURL"]
         
         self.filenameLabel.setText(QFileInfo(self.__fileName).fileName())
         self.infoLabel.setText(self.__fileName)
         
-        if len(data) == 5:
-            self.__setDateTime(data[4])
-        else:
+        try:
+            self.__setDateTime(data["Downloaded"])
+        except KeyError:
             self.__setDateTime(QDateTime())
         
         self.pauseButton.setEnabled(False)
         self.pauseButton.setVisible(False)
         self.stopButton.setEnabled(False)
         self.stopButton.setVisible(False)
-        self.openButton.setEnabled(data[2])
-        self.openButton.setVisible(data[2])
-        if data[2]:
+        self.openButton.setEnabled(data["Done"])
+        self.openButton.setVisible(data["Done"])
+        if data["Done"]:
             self.__state = DownloadItem.DownloadSuccessful
         else:
             self.__state = DownloadItem.DownloadCancelled
--- a/WebBrowser/Download/DownloadManager.py	Thu Jan 25 16:39:06 2018 +0100
+++ b/WebBrowser/Download/DownloadManager.py	Thu Jan 25 17:21:00 2018 +0100
@@ -342,8 +342,8 @@
         if not WebBrowserWindow.isPrivate():
             downloads = Preferences.getWebBrowser("DownloadManagerDownloads")
             for download in downloads:
-                if not download[0].isEmpty() and \
-                   download[1] != "":
+                if not download["URL"].isEmpty() and \
+                   bool(download["Location"]):
                     from .DownloadItem import DownloadItem
                     itm = DownloadItem(parent=self)
                     itm.setData(download)

eric ide

mercurial