PluginManager/PluginRepositoryDialog.py

changeset 270
41505c92ac31
parent 80
166d5df6337c
child 286
652f5159f1c3
child 792
a13346916170
equal deleted inserted replaced
269:56e0107f2b01 270:41505c92ac31
13 import zipfile 13 import zipfile
14 import io 14 import io
15 15
16 from PyQt4.QtGui import * 16 from PyQt4.QtGui import *
17 from PyQt4.QtCore import * 17 from PyQt4.QtCore import *
18 from PyQt4.QtNetwork import QHttp, QNetworkProxy 18 from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply
19 19
20 from .Ui_PluginRepositoryDialog import Ui_PluginRepositoryDialog 20 from .Ui_PluginRepositoryDialog import Ui_PluginRepositoryDialog
21 21
22 from UI.AuthenticationDialog import AuthenticationDialog 22 from UI.AuthenticationDialog import AuthenticationDialog
23 23
70 self.repositoryList.header().setSortIndicator(0, Qt.AscendingOrder) 70 self.repositoryList.header().setSortIndicator(0, Qt.AscendingOrder)
71 71
72 self.pluginRepositoryFile = \ 72 self.pluginRepositoryFile = \
73 os.path.join(Utilities.getConfigDir(), "PluginRepository") 73 os.path.join(Utilities.getConfigDir(), "PluginRepository")
74 74
75 self.__http = None 75 # attributes for the network objects
76 self.__networkManager = QNetworkAccessManager(self)
77 self.connect(self.__networkManager,
78 SIGNAL('proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)'),
79 self.__proxyAuthenticationRequired)
80 self.connect(self.__networkManager,
81 SIGNAL('sslErrors(QNetworkReply *, const QList<QSslError> &)'),
82 self.__sslErrors)
83 self.__replies = []
84
76 self.__doneMethod = None 85 self.__doneMethod = None
77 self.__inDownload = False 86 self.__inDownload = False
78 self.__pluginsToDownload = [] 87 self.__pluginsToDownload = []
79 self.__pluginsDownloaded = [] 88 self.__pluginsDownloaded = []
80 89
320 329
321 @param url URL for the download (string) 330 @param url URL for the download (string)
322 @param filename local name of the file (string) 331 @param filename local name of the file (string)
323 @param doneMethod method to be called when done 332 @param doneMethod method to be called when done
324 """ 333 """
325 if self.__http is None:
326 self.__http = QHttp()
327 self.connect(self.__http, SIGNAL("done(bool)"), self.__downloadFileDone)
328 self.connect(self.__http, SIGNAL("dataReadProgress(int, int)"),
329 self.__dataReadProgress)
330 self.connect(self.__http,
331 SIGNAL('proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)'),
332 self.__proxyAuthenticationRequired)
333 self.connect(self.__http, SIGNAL("sslErrors(const QList<QSslError>&)"),
334 self.__sslErrors)
335
336 if Preferences.getUI("UseProxy"):
337 host = Preferences.getUI("ProxyHost")
338 if not host:
339 QMessageBox.critical(None,
340 self.trUtf8("Error downloading file"),
341 self.trUtf8("""Proxy usage was activated"""
342 """ but no proxy host configured."""))
343 return
344 else:
345 pProxyType = Preferences.getUI("ProxyType")
346 if pProxyType == 0:
347 proxyType = QNetworkProxy.HttpProxy
348 elif pProxyType == 1:
349 proxyType = QNetworkProxy.HttpCachingProxy
350 elif pProxyType == 2:
351 proxyType = QNetworkProxy.Socks5Proxy
352 self.__proxy = QNetworkProxy(proxyType, host,
353 Preferences.getUI("ProxyPort"),
354 Preferences.getUI("ProxyUser"),
355 Preferences.getUI("ProxyPassword"))
356 self.__http.setProxy(self.__proxy)
357
358 self.__updateButton.setEnabled(False) 334 self.__updateButton.setEnabled(False)
359 self.__downloadButton.setEnabled(False) 335 self.__downloadButton.setEnabled(False)
360 self.__downloadCancelButton.setEnabled(True) 336 self.__downloadCancelButton.setEnabled(True)
361 337
362 self.statusLabel.setText(url) 338 self.statusLabel.setText(url)
365 self.__downloadURL = url 341 self.__downloadURL = url
366 self.__downloadFileName = filename 342 self.__downloadFileName = filename
367 self.__downloadIODevice = QFile(self.__downloadFileName + ".tmp") 343 self.__downloadIODevice = QFile(self.__downloadFileName + ".tmp")
368 self.__downloadCancelled = False 344 self.__downloadCancelled = False
369 345
370 if QUrl(url).scheme().lower() == 'https': 346 reply = self.__networkManager.get(QNetworkRequest(QUrl(url)))
371 connectionMode = QHttp.ConnectionModeHttps 347 self.connect(reply, SIGNAL("finished()"), self.__downloadFileDone)
372 else: 348 self.connect(reply, SIGNAL("downloadProgress(qint64, qint64)"),
373 connectionMode = QHttp.ConnectionModeHttp 349 self.__downloadProgress)
374 self.__http.setHost(QUrl(url).host(), connectionMode, QUrl(url).port(0)) 350 self.__replies.append(reply)
375 self.__http.get(QUrl(url).path(), self.__downloadIODevice) 351
376 352 def __downloadFileDone(self):
377 def __downloadFileDone(self, error):
378 """ 353 """
379 Private method called, after the file has been downloaded 354 Private method called, after the file has been downloaded
380 from the internet. 355 from the internet.
381
382 @param error flag indicating an error condition (boolean)
383 """ 356 """
384 self.__updateButton.setEnabled(True) 357 self.__updateButton.setEnabled(True)
385 self.__downloadCancelButton.setEnabled(False) 358 self.__downloadCancelButton.setEnabled(False)
386 self.statusLabel.setText(" ") 359 self.statusLabel.setText(" ")
387 360
388 ok = True 361 ok = True
389 if error or self.__http.lastResponse().statusCode() != 200: 362 reply = self.sender()
363 if reply in self.__replies:
364 self.__replies.remove(reply)
365 if reply.error() != QNetworkReply.NoError:
390 ok = False 366 ok = False
391 if not self.__downloadCancelled: 367 if not self.__downloadCancelled:
392 if error:
393 msg = self.__http.errorString()
394 else:
395 msg = self.__http.lastResponse().reasonPhrase()
396 QMessageBox.warning(None, 368 QMessageBox.warning(None,
397 self.trUtf8("Error downloading file"), 369 self.trUtf8("Error downloading file"),
398 self.trUtf8( 370 self.trUtf8(
399 """<p>Could not download the requested file from {0}.</p>""" 371 """<p>Could not download the requested file from {0}.</p>"""
400 """<p>Error: {1}</p>""" 372 """<p>Error: {1}</p>"""
401 ).format(self.__downloadURL, msg) 373 ).format(self.__downloadURL, reply.errorString())
402 ) 374 )
403 self.downloadProgress.setValue(0) 375 self.downloadProgress.setValue(0)
404 self.__downloadURL = None 376 self.__downloadURL = None
405 self.__downloadIODevice.remove() 377 self.__downloadIODevice.remove()
406 self.__downloadIODevice = None 378 self.__downloadIODevice = None
410 self.repositoryList.topLevelItem(0)) 382 self.repositoryList.topLevelItem(0))
411 else: 383 else:
412 self.__downloadButton.setEnabled(len(self.__selectedItems())) 384 self.__downloadButton.setEnabled(len(self.__selectedItems()))
413 return 385 return
414 386
387 self.__downloadIODevice.open(QIODevice.WriteOnly)
388 self.__downloadIODevice.write(reply.readAll())
389 self.__downloadIODevice.close()
415 if QFile.exists(self.__downloadFileName): 390 if QFile.exists(self.__downloadFileName):
416 QFile.remove(self.__downloadFileName) 391 QFile.remove(self.__downloadFileName)
417 self.__downloadIODevice.rename(self.__downloadFileName) 392 self.__downloadIODevice.rename(self.__downloadFileName)
418 self.__downloadIODevice = None 393 self.__downloadIODevice = None
419 self.__downloadURL = None 394 self.__downloadURL = None
423 398
424 def __downloadCancel(self): 399 def __downloadCancel(self):
425 """ 400 """
426 Private slot to cancel the current download. 401 Private slot to cancel the current download.
427 """ 402 """
428 if self.__http is not None: 403 if self.__replies:
404 reply = self.__replies[0]
429 self.__downloadCancelled = True 405 self.__downloadCancelled = True
430 self.__pluginsToDownload = [] 406 self.__pluginsToDownload = []
431 self.__http.abort() 407 reply.abort()
432 408
433 def __dataReadProgress(self, done, total): 409 def __downloadProgress(self, done, total):
434 """ 410 """
435 Private slot to show the download progress. 411 Private slot to show the download progress.
436 412
437 @param done number of bytes downloaded so far (integer) 413 @param done number of bytes downloaded so far (integer)
438 @param total total bytes to be downloaded (integer) 414 @param total total bytes to be downloaded (integer)
439 """ 415 """
440 self.downloadProgress.setMaximum(total) 416 if total:
441 self.downloadProgress.setValue(done) 417 self.downloadProgress.setMaximum(total)
418 self.downloadProgress.setValue(done)
442 419
443 def addEntry(self, name, short, description, url, author, version, filename, status): 420 def addEntry(self, name, short, description, url, author, version, filename, status):
444 """ 421 """
445 Public method to add an entry to the list. 422 Public method to add an entry to the list.
446 423
528 auth.setPassword(password) 505 auth.setPassword(password)
529 if dlg.shallSave(): 506 if dlg.shallSave():
530 Preferences.setUI("ProxyUser", username) 507 Preferences.setUI("ProxyUser", username)
531 Preferences.setUI("ProxyPassword", password) 508 Preferences.setUI("ProxyPassword", password)
532 509
533 def __sslErrors(self, sslErrors): 510 def __sslErrors(self, reply, errors):
534 """ 511 """
535 Private slot to handle SSL errors. 512 Private slot to handle SSL errors.
536 513
537 @param sslErrors list of SSL errors (list of QSslError) 514 @param reply reference to the reply object (QNetworkReply)
515 @param errors list of SSL errors (list of QSslError)
538 """ 516 """
539 errorStrings = [] 517 errorStrings = []
540 for err in sslErrors: 518 for err in errors:
541 errorStrings.append(err.errorString()) 519 errorStrings.append(err.errorString())
542 errorString = '.<br />'.join(errorStrings) 520 errorString = '.<br />'.join(errorStrings)
543 ret = QMessageBox.warning(self, 521 ret = QMessageBox.warning(self,
544 self.trUtf8("SSL Errors"), 522 self.trUtf8("SSL Errors"),
545 self.trUtf8("""<p>SSL Errors:</p>""" 523 self.trUtf8("""<p>SSL Errors:</p>"""
549 QMessageBox.StandardButtons(\ 527 QMessageBox.StandardButtons(\
550 QMessageBox.No | \ 528 QMessageBox.No | \
551 QMessageBox.Yes), 529 QMessageBox.Yes),
552 QMessageBox.No) 530 QMessageBox.No)
553 if ret == QMessageBox.Yes: 531 if ret == QMessageBox.Yes:
554 self.__http.ignoreSslErrors() 532 reply.ignoreSslErrors()
555 else: 533 else:
556 self.__downloadCancelled = True 534 self.__downloadCancelled = True
557 self.__http.abort() 535 reply.abort()
558 536
559 def getDownloadedPlugins(self): 537 def getDownloadedPlugins(self):
560 """ 538 """
561 Public method to get the list of recently downloaded plugin files. 539 Public method to get the list of recently downloaded plugin files.
562 540

eric ide

mercurial