eric7/PluginManager/PluginRepositoryDialog.py

branch
eric7
changeset 8580
e91b276e0771
parent 8366
2a9f5153c438
child 8585
c9996d52a1b9
diff -r cfd0e44ef084 -r e91b276e0771 eric7/PluginManager/PluginRepositoryDialog.py
--- a/eric7/PluginManager/PluginRepositoryDialog.py	Sat Sep 04 16:51:35 2021 +0200
+++ b/eric7/PluginManager/PluginRepositoryDialog.py	Sat Sep 04 20:26:21 2021 +0200
@@ -22,7 +22,7 @@
     QVBoxLayout, QMenu
 )
 from PyQt6.QtNetwork import (
-    QNetworkAccessManager, QNetworkRequest, QNetworkReply
+    QNetworkAccessManager, QNetworkRequest, QNetworkReply, QNetworkInformation
 )
 
 from .Ui_PluginRepositoryDialog import Ui_PluginRepositoryDialog
@@ -142,6 +142,20 @@
             self.__networkManager.sslErrors.connect(self.__sslErrors)
         self.__replies = []
         
+        if (
+            Preferences.getUI("DynamicOnlineCheck") and
+            QNetworkInformation.load(QNetworkInformation.Feature.Reachability)
+        ):
+            self.__reachabilityChanged(
+                QNetworkInformation.instance().reachability())
+            # TODO: QNetworkInformation: re-enable once problem is clear
+##            QNetworkInformation.instance().reachabilityChanged.connect(
+##                self.__reachabilityChanged)
+        else:
+            # assume to be 'always online' if no backend could be loaded or
+            # dynamic online check is switched of
+            self.__reachabilityChanged(QNetworkInformation.Reachability.Online)
+        
         self.__pluginsToDownload = []
         self.__pluginsDownloaded = []
         self.__isDownloadInstall = False
@@ -151,6 +165,27 @@
         
         self.__populateList()
     
+    @pyqtSlot(QNetworkInformation.Reachability)
+    def __reachabilityChanged(self, reachability):
+        """
+        Private slot handling reachability state changes.
+        
+        @param reachability new reachability state
+        @type QNetworkInformation.Reachability
+        """
+        online = reachability == QNetworkInformation.Reachability.Online
+        self.__online = online
+        
+        self.__updateButton.setEnabled(online)
+        self.on_repositoryList_itemSelectionChanged()
+        
+        msg = (
+            self.tr("Internet Reachability Status: Reachable")
+            if online else
+            self.tr("Internet Reachability Status: Not Reachable")
+        )
+        self.statusLabel.setText(msg)
+    
     @pyqtSlot(QAbstractButton)
     def on_buttonBox_clicked(self, button):
         """
@@ -266,8 +301,8 @@
         Private slot to handle a change of the selection.
         """
         enable = bool(self.__selectedItems())
-        self.__downloadButton.setEnabled(enable)
-        self.__downloadInstallButton.setEnabled(enable)
+        self.__downloadButton.setEnabled(enable and self.__online)
+        self.__downloadInstallButton.setEnabled(enable and self.__online)
         self.__installButton.setEnabled(enable)
     
     def __updateList(self):
@@ -444,23 +479,32 @@
         @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.__closeButton.setEnabled(False)
-        self.__downloadCancelButton.setEnabled(True)
-        
-        self.statusLabel.setText(url)
-        
-        request = QNetworkRequest(QUrl(url))
-        request.setAttribute(
-            QNetworkRequest.Attribute.CacheLoadControlAttribute,
-            QNetworkRequest.CacheLoadControl.AlwaysNetwork)
-        reply = self.__networkManager.get(request)
-        reply.finished.connect(
-            lambda: self.__downloadFileDone(reply, filename, doneMethod))
-        reply.downloadProgress.connect(self.__downloadProgress)
-        self.__replies.append(reply)
+        if self.__online:
+            self.__updateButton.setEnabled(False)
+            self.__downloadButton.setEnabled(False)
+            self.__downloadInstallButton.setEnabled(False)
+            self.__closeButton.setEnabled(False)
+            self.__downloadCancelButton.setEnabled(True)
+            
+            self.statusLabel.setText(url)
+            
+            request = QNetworkRequest(QUrl(url))
+            request.setAttribute(
+                QNetworkRequest.Attribute.CacheLoadControlAttribute,
+                QNetworkRequest.CacheLoadControl.AlwaysNetwork)
+            reply = self.__networkManager.get(request)
+            reply.finished.connect(
+                lambda: self.__downloadFileDone(reply, filename, doneMethod))
+            reply.downloadProgress.connect(self.__downloadProgress)
+            self.__replies.append(reply)
+        else:
+            EricMessageBox.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("No connection to Internet.")))
     
     def __downloadFileDone(self, reply, fileName, doneMethod):
         """

eric ide

mercurial