eric7/WebBrowser/SpellCheck/ManageDictionariesDialog.py

branch
eric7
changeset 8580
e91b276e0771
parent 8358
144a6b854f70
child 8585
c9996d52a1b9
diff -r cfd0e44ef084 -r e91b276e0771 eric7/WebBrowser/SpellCheck/ManageDictionariesDialog.py
--- a/eric7/WebBrowser/SpellCheck/ManageDictionariesDialog.py	Sat Sep 04 16:51:35 2021 +0200
+++ b/eric7/WebBrowser/SpellCheck/ManageDictionariesDialog.py	Sat Sep 04 20:26:21 2021 +0200
@@ -18,7 +18,7 @@
 from PyQt6.QtWidgets import (
     QDialog, QDialogButtonBox, QAbstractButton, QListWidgetItem
 )
-from PyQt6.QtNetwork import QNetworkRequest, QNetworkReply
+from PyQt6.QtNetwork import QNetworkRequest, QNetworkReply, QNetworkInformation
 
 from EricWidgets import EricMessageBox
 
@@ -69,6 +69,19 @@
         self.dictionariesUrlEdit.setText(
             Preferences.getWebBrowser("SpellCheckDictionariesUrl"))
         
+        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.__replies = []
         
         self.__downloadCancelled = False
@@ -76,6 +89,28 @@
         
         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.__refreshButton.setEnabled(online)
+        
+        msg = (
+            self.tr("Internet Reachability Status: Reachable")
+            if online else
+            self.tr("Internet Reachability Status: Not Reachable")
+        )
+        self.statusLabel.setText(msg)
+        
+        self.on_dictionariesList_itemSelectionChanged()
+    
     @pyqtSlot(QAbstractButton)
     def on_buttonBox_clicked(self, button):
         """
@@ -100,7 +135,8 @@
         """
         self.__installButton.setEnabled(
             self.locationComboBox.count() > 0 and
-            len(self.dictionariesList.selectedItems()) > 0
+            len(self.dictionariesList.selectedItems()) > 0 and
+            self.__online
         )
         
         self.__uninstallButton.setEnabled(
@@ -140,24 +176,33 @@
         
         url = self.dictionariesUrlEdit.text()
         
-        self.__refreshButton.setEnabled(False)
-        self.__installButton.setEnabled(False)
-        self.__uninstallButton.setEnabled(False)
-        self.__cancelButton.setEnabled(True)
-        
-        self.statusLabel.setText(url)
-        
-        self.__downloadCancelled = False
-        
-        request = QNetworkRequest(QUrl(url))
-        request.setAttribute(
-            QNetworkRequest.Attribute.CacheLoadControlAttribute,
-            QNetworkRequest.CacheLoadControl.AlwaysNetwork)
-        reply = WebBrowserWindow.networkManager().get(request)
-        reply.finished.connect(
-            lambda: self.__listFileDownloaded(reply))
-        reply.downloadProgress.connect(self.__downloadProgress)
-        self.__replies.append(reply)
+        if self.__online:
+            self.__refreshButton.setEnabled(False)
+            self.__installButton.setEnabled(False)
+            self.__uninstallButton.setEnabled(False)
+            self.__cancelButton.setEnabled(True)
+            
+            self.statusLabel.setText(url)
+            
+            self.__downloadCancelled = False
+            
+            request = QNetworkRequest(QUrl(url))
+            request.setAttribute(
+                QNetworkRequest.Attribute.CacheLoadControlAttribute,
+                QNetworkRequest.CacheLoadControl.AlwaysNetwork)
+            reply = WebBrowserWindow.networkManager().get(request)
+            reply.finished.connect(
+                lambda: self.__listFileDownloaded(reply))
+            reply.downloadProgress.connect(self.__downloadProgress)
+            self.__replies.append(reply)
+        else:
+            EricMessageBox.warning(
+                self,
+                self.tr("Error populating list of dictionaries"),
+                self.tr(
+                    """<p>Could not download the dictionaries list"""
+                    """ from {0}.</p><p>Error: {1}</p>"""
+                ).format(url, self.tr("No connection to Internet.")))
     
     def __listFileDownloaded(self, reply):
         """
@@ -307,7 +352,7 @@
         """
         Private method to install the selected dictionaries.
         """
-        if bool(self.locationComboBox.currentText()):
+        if self.__online and bool(self.locationComboBox.currentText()):
             self.__dictionariesToDownload = [
                 itm.data(ManageDictionariesDialog.UrlRole)
                 for itm in self.dictionariesList.selectedItems()
@@ -326,23 +371,32 @@
         """
         Private slot to download a dictionary.
         """
-        if self.__dictionariesToDownload:
-            url = self.__dictionariesToDownload.pop(0)
-            self.statusLabel.setText(url)
-            
-            self.__downloadCancelled = False
-            
-            request = QNetworkRequest(QUrl(url))
-            request.setAttribute(
-                QNetworkRequest.Attribute.CacheLoadControlAttribute,
-                QNetworkRequest.CacheLoadControl.AlwaysNetwork)
-            reply = WebBrowserWindow.networkManager().get(request)
-            reply.finished.connect(
-                lambda: self.__installDictionary(reply))
-            reply.downloadProgress.connect(self.__downloadProgress)
-            self.__replies.append(reply)
+        if self.__online:
+            if self.__dictionariesToDownload:
+                url = self.__dictionariesToDownload.pop(0)
+                self.statusLabel.setText(url)
+                
+                self.__downloadCancelled = False
+                
+                request = QNetworkRequest(QUrl(url))
+                request.setAttribute(
+                    QNetworkRequest.Attribute.CacheLoadControlAttribute,
+                    QNetworkRequest.CacheLoadControl.AlwaysNetwork)
+                reply = WebBrowserWindow.networkManager().get(request)
+                reply.finished.connect(
+                    lambda: self.__installDictionary(reply))
+                reply.downloadProgress.connect(self.__downloadProgress)
+                self.__replies.append(reply)
+            else:
+                self.__installationFinished()
         else:
-            self.__installationFinished()
+            EricMessageBox.warning(
+                self,
+                self.tr("Error downloading dictionary file"),
+                self.tr(
+                    """<p>Could not download the requested dictionary file"""
+                    """ from {0}.</p><p>Error: {1}</p>"""
+                ).format(url, self.tr("No connection to Internet.")))
             
             self.__installationFinished()
     

eric ide

mercurial