PluginManager/PluginRepositoryDialog.py

changeset 4629
99aaac59be4f
parent 4626
c891c7ad6b60
child 4631
5c1a96925da4
--- a/PluginManager/PluginRepositoryDialog.py	Mon Dec 28 12:07:59 2015 +0100
+++ b/PluginManager/PluginRepositoryDialog.py	Tue Dec 29 15:13:23 2015 +0100
@@ -20,7 +20,7 @@
 from PyQt5.QtWidgets import QWidget, QDialogButtonBox, QAbstractButton, \
     QTreeWidgetItem, QDialog, QVBoxLayout, QMenu
 from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest, \
-    QNetworkReply
+    QNetworkReply, QNetworkConfigurationManager
 
 from .Ui_PluginRepositoryDialog import Ui_PluginRepositoryDialog
 
@@ -123,6 +123,13 @@
             self.__networkManager.sslErrors.connect(self.__sslErrors)
         self.__replies = []
         
+        self.__networkConfigurationManager = QNetworkConfigurationManager(self)
+        self.__onlineStateChanged(
+            self.__networkConfigurationManager.isOnline())
+        
+        self.__networkConfigurationManager.onlineStateChanged.connect(
+            self.__onlineStateChanged)
+        
         self.__doneMethod = None
         self.__inDownload = False
         self.__pluginsToDownload = []
@@ -134,6 +141,22 @@
         
         self.__populateList()
     
+    @pyqtSlot(bool)
+    def __onlineStateChanged(self, online):
+        """
+        Private slot handling online state changes.
+        
+        @param online flag indicating the online status
+        @type bool
+        """
+        self.__updateButton.setEnabled(online)
+        self.on_repositoryList_itemSelectionChanged()
+        if online:
+            msg = self.tr("Network Status: online")
+        else:
+            msg = self.tr("Network Status: offline")
+        self.statusLabel.setText(msg)
+    
     @pyqtSlot(QAbstractButton)
     def on_buttonBox_clicked(self, button):
         """
@@ -232,8 +255,12 @@
         """
         Private slot to handle a change of the selection.
         """
-        self.__downloadButton.setEnabled(len(self.__selectedItems()))
-        self.__downloadInstallButton.setEnabled(len(self.__selectedItems()))
+        self.__downloadButton.setEnabled(
+            len(self.__selectedItems()) and
+            self.__networkConfigurationManager.isOnline())
+        self.__downloadInstallButton.setEnabled(
+            len(self.__selectedItems()) and
+            self.__networkConfigurationManager.isOnline())
         self.__installButton.setEnabled(len(self.__selectedItems()))
     
     def __updateList(self):
@@ -395,26 +422,35 @@
         @param filename local name of the file (string)
         @param doneMethod method to be called when done
         """
-        self.__updateButton.setEnabled(False)
-        self.__downloadButton.setEnabled(False)
-        self.__downloadInstallButton.setEnabled(False)
-        self.__downloadCancelButton.setEnabled(True)
-        
-        self.statusLabel.setText(url)
-        
-        self.__doneMethod = doneMethod
-        self.__downloadURL = url
-        self.__downloadFileName = filename
-        self.__downloadIODevice = QFile(self.__downloadFileName + ".tmp")
-        self.__downloadCancelled = False
-        
-        request = QNetworkRequest(QUrl(url))
-        request.setAttribute(QNetworkRequest.CacheLoadControlAttribute,
-                             QNetworkRequest.AlwaysNetwork)
-        reply = self.__networkManager.get(request)
-        reply.finished.connect(self.__downloadFileDone)
-        reply.downloadProgress.connect(self.__downloadProgress)
-        self.__replies.append(reply)
+        if self.__networkConfigurationManager.isOnline():
+            self.__updateButton.setEnabled(False)
+            self.__downloadButton.setEnabled(False)
+            self.__downloadInstallButton.setEnabled(False)
+            self.__downloadCancelButton.setEnabled(True)
+            
+            self.statusLabel.setText(url)
+            
+            self.__doneMethod = doneMethod
+            self.__downloadURL = url
+            self.__downloadFileName = filename
+            self.__downloadIODevice = QFile(self.__downloadFileName + ".tmp")
+            self.__downloadCancelled = False
+            
+            request = QNetworkRequest(QUrl(url))
+            request.setAttribute(QNetworkRequest.CacheLoadControlAttribute,
+                                 QNetworkRequest.AlwaysNetwork)
+            reply = self.__networkManager.get(request)
+            reply.finished.connect(self.__downloadFileDone)
+            reply.downloadProgress.connect(self.__downloadProgress)
+            self.__replies.append(reply)
+        else:
+            E5MessageBox.warning(
+                self,
+                self.tr("Error downloading file"),
+                self.tr(
+                    """<p>Could not download the requested file"""
+                    """ from {0}.</p><p>Error: {1}</p>"""
+                ).format(url, self.tr("Computer is offline.")))
     
     def __downloadFileDone(self):
         """
@@ -423,7 +459,8 @@
         """
         self.__updateButton.setEnabled(True)
         self.__downloadCancelButton.setEnabled(False)
-        self.statusLabel.setText("  ")
+        self.__onlineStateChanged(
+            self.__networkConfigurationManager.isOnline())
         
         ok = True
         reply = self.sender()

eric ide

mercurial