eric6/WebBrowser/SpellCheck/ManageDictionariesDialog.py

changeset 7946
6901746220fc
parent 7923
91e843545d9a
child 8143
2c730d5fd177
equal deleted inserted replaced
7945:76daafe10009 7946:6901746220fc
15 15
16 from PyQt5.QtCore import pyqtSlot, Qt, QUrl 16 from PyQt5.QtCore import pyqtSlot, Qt, QUrl
17 from PyQt5.QtWidgets import ( 17 from PyQt5.QtWidgets import (
18 QDialog, QDialogButtonBox, QAbstractButton, QListWidgetItem 18 QDialog, QDialogButtonBox, QAbstractButton, QListWidgetItem
19 ) 19 )
20 from PyQt5.QtNetwork import ( 20 from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply
21 QNetworkConfigurationManager, QNetworkRequest, QNetworkReply
22 )
23 21
24 from E5Gui import E5MessageBox 22 from E5Gui import E5MessageBox
25 23
26 from .Ui_ManageDictionariesDialog import Ui_ManageDictionariesDialog 24 from .Ui_ManageDictionariesDialog import Ui_ManageDictionariesDialog
27 25
66 self.locationComboBox.addItems(writeableDirectories) 64 self.locationComboBox.addItems(writeableDirectories)
67 65
68 self.dictionariesUrlEdit.setText( 66 self.dictionariesUrlEdit.setText(
69 Preferences.getWebBrowser("SpellCheckDictionariesUrl")) 67 Preferences.getWebBrowser("SpellCheckDictionariesUrl"))
70 68
71 if Preferences.getUI("DynamicOnlineCheck"):
72 self.__networkConfigurationManager = QNetworkConfigurationManager(
73 self)
74 self.__onlineStateChanged(
75 self.__networkConfigurationManager.isOnline())
76 self.__networkConfigurationManager.onlineStateChanged.connect(
77 self.__onlineStateChanged)
78 else:
79 self.__networkConfigurationManager = None
80 self.__onlineStateChanged(True)
81 self.__replies = [] 69 self.__replies = []
82 70
83 self.__downloadCancelled = False 71 self.__downloadCancelled = False
84 self.__dictionariesToDownload = [] 72 self.__dictionariesToDownload = []
85 73
86 self.__populateList() 74 self.__populateList()
87
88 @pyqtSlot(bool)
89 def __onlineStateChanged(self, online):
90 """
91 Private slot handling online state changes.
92
93 @param online flag indicating the online status
94 @type bool
95 """
96 self.__refreshButton.setEnabled(online)
97
98 if online:
99 msg = self.tr("Network Status: online")
100 else:
101 msg = self.tr("Network Status: offline")
102 self.statusLabel.setText(msg)
103
104 self.on_dictionariesList_itemSelectionChanged()
105
106 def __isOnline(self):
107 """
108 Private method to check the online status.
109
110 @return flag indicating the online status
111 @rtype bool
112 """
113 if self.__networkConfigurationManager is not None:
114 return self.__networkConfigurationManager.isOnline()
115 else:
116 return True
117 75
118 @pyqtSlot(QAbstractButton) 76 @pyqtSlot(QAbstractButton)
119 def on_buttonBox_clicked(self, button): 77 def on_buttonBox_clicked(self, button):
120 """ 78 """
121 Private slot to handle the click of a button of the button box. 79 Private slot to handle the click of a button of the button box.
137 """ 95 """
138 Private slot to handle a change of the selection. 96 Private slot to handle a change of the selection.
139 """ 97 """
140 self.__installButton.setEnabled( 98 self.__installButton.setEnabled(
141 self.locationComboBox.count() > 0 and 99 self.locationComboBox.count() > 0 and
142 len(self.dictionariesList.selectedItems()) > 0 and 100 len(self.dictionariesList.selectedItems()) > 0
143 self.__isOnline()
144 ) 101 )
145 102
146 self.__uninstallButton.setEnabled( 103 self.__uninstallButton.setEnabled(
147 self.locationComboBox.count() > 0 and 104 self.locationComboBox.count() > 0 and
148 len([itm 105 len([itm
178 self.dictionariesList.clear() 135 self.dictionariesList.clear()
179 self.downloadProgress.setValue(0) 136 self.downloadProgress.setValue(0)
180 137
181 url = self.dictionariesUrlEdit.text() 138 url = self.dictionariesUrlEdit.text()
182 139
183 if self.__isOnline(): 140 self.__refreshButton.setEnabled(False)
184 self.__refreshButton.setEnabled(False) 141 self.__installButton.setEnabled(False)
185 self.__installButton.setEnabled(False) 142 self.__uninstallButton.setEnabled(False)
186 self.__uninstallButton.setEnabled(False) 143 self.__cancelButton.setEnabled(True)
187 self.__cancelButton.setEnabled(True) 144
188 145 self.statusLabel.setText(url)
189 self.statusLabel.setText(url) 146
190 147 self.__downloadCancelled = False
191 self.__downloadCancelled = False 148
192 149 request = QNetworkRequest(QUrl(url))
193 request = QNetworkRequest(QUrl(url)) 150 request.setAttribute(QNetworkRequest.CacheLoadControlAttribute,
194 request.setAttribute(QNetworkRequest.CacheLoadControlAttribute, 151 QNetworkRequest.AlwaysNetwork)
195 QNetworkRequest.AlwaysNetwork) 152 reply = WebBrowserWindow.networkManager().get(request)
196 reply = WebBrowserWindow.networkManager().get(request) 153 reply.finished.connect(
197 reply.finished.connect( 154 lambda: self.__listFileDownloaded(reply))
198 lambda: self.__listFileDownloaded(reply)) 155 reply.downloadProgress.connect(self.__downloadProgress)
199 reply.downloadProgress.connect(self.__downloadProgress) 156 self.__replies.append(reply)
200 self.__replies.append(reply)
201 else:
202 E5MessageBox.warning(
203 self,
204 self.tr("Error populating list of dictionaries"),
205 self.tr(
206 """<p>Could not download the dictionaries list"""
207 """ from {0}.</p><p>Error: {1}</p>"""
208 ).format(url, self.tr("Computer is offline.")))
209 157
210 def __listFileDownloaded(self, reply): 158 def __listFileDownloaded(self, reply):
211 """ 159 """
212 Private method called, after the dictionaries list file has been 160 Private method called, after the dictionaries list file has been
213 downloaded from the Internet. 161 downloaded from the Internet.
215 @param reply reference to the network reply 163 @param reply reference to the network reply
216 @type QNetworkReply 164 @type QNetworkReply
217 """ 165 """
218 self.__refreshButton.setEnabled(True) 166 self.__refreshButton.setEnabled(True)
219 self.__cancelButton.setEnabled(False) 167 self.__cancelButton.setEnabled(False)
220 self.__onlineStateChanged(self.__isOnline())
221 168
222 self.downloadProgress.setValue(0) 169 self.downloadProgress.setValue(0)
223 170
224 if reply in self.__replies: 171 if reply in self.__replies:
225 self.__replies.remove(reply) 172 self.__replies.remove(reply)
354 301
355 def __installSelected(self): 302 def __installSelected(self):
356 """ 303 """
357 Private method to install the selected dictionaries. 304 Private method to install the selected dictionaries.
358 """ 305 """
359 if self.__isOnline() and bool(self.locationComboBox.currentText()): 306 if bool(self.locationComboBox.currentText()):
360 self.__dictionariesToDownload = [ 307 self.__dictionariesToDownload = [
361 itm.data(ManageDictionariesDialog.UrlRole) 308 itm.data(ManageDictionariesDialog.UrlRole)
362 for itm in self.dictionariesList.selectedItems() 309 for itm in self.dictionariesList.selectedItems()
363 ] 310 ]
364 311
373 320
374 def __downloadDictionary(self): 321 def __downloadDictionary(self):
375 """ 322 """
376 Private slot to download a dictionary. 323 Private slot to download a dictionary.
377 """ 324 """
378 if self.__isOnline(): 325 if self.__dictionariesToDownload:
379 if self.__dictionariesToDownload: 326 url = self.__dictionariesToDownload.pop(0)
380 url = self.__dictionariesToDownload.pop(0) 327 self.statusLabel.setText(url)
381 self.statusLabel.setText(url) 328
382 329 self.__downloadCancelled = False
383 self.__downloadCancelled = False 330
384 331 request = QNetworkRequest(QUrl(url))
385 request = QNetworkRequest(QUrl(url)) 332 request.setAttribute(QNetworkRequest.CacheLoadControlAttribute,
386 request.setAttribute(QNetworkRequest.CacheLoadControlAttribute, 333 QNetworkRequest.AlwaysNetwork)
387 QNetworkRequest.AlwaysNetwork) 334 reply = WebBrowserWindow.networkManager().get(request)
388 reply = WebBrowserWindow.networkManager().get(request) 335 reply.finished.connect(
389 reply.finished.connect( 336 lambda: self.__installDictionary(reply))
390 lambda: self.__installDictionary(reply)) 337 reply.downloadProgress.connect(self.__downloadProgress)
391 reply.downloadProgress.connect(self.__downloadProgress) 338 self.__replies.append(reply)
392 self.__replies.append(reply)
393 else:
394 self.__installationFinished()
395 else: 339 else:
396 E5MessageBox.warning( 340 self.__installationFinished()
397 self,
398 self.tr("Error downloading dictionary file"),
399 self.tr(
400 """<p>Could not download the requested dictionary file"""
401 """ from {0}.</p><p>Error: {1}</p>"""
402 ).format(url, self.tr("Computer is offline.")))
403 341
404 self.__installationFinished() 342 self.__installationFinished()
405 343
406 def __installDictionary(self, reply): 344 def __installDictionary(self, reply):
407 """ 345 """
452 Private method called after all selected dictionaries have been 390 Private method called after all selected dictionaries have been
453 installed. 391 installed.
454 """ 392 """
455 self.__refreshButton.setEnabled(True) 393 self.__refreshButton.setEnabled(True)
456 self.__cancelButton.setEnabled(False) 394 self.__cancelButton.setEnabled(False)
457 self.__onlineStateChanged(self.__isOnline())
458 395
459 self.dictionariesList.clearSelection() 396 self.dictionariesList.clearSelection()
460 self.downloadProgress.setValue(0) 397 self.downloadProgress.setValue(0)
461 398
462 self.__checkInstalledDictionaries() 399 self.__checkInstalledDictionaries()

eric ide

mercurial