2013-05-19 17:44:57 +02:00
# -*- coding:utf-8 -*-
"""
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2013-06-08 22:10:07 +02:00
qgsplugininstallerinstallingdialog . py
Plugin Installer module
2013-05-19 17:44:57 +02:00
- - - - - - - - - - - - - - - - - - -
2013-06-08 22:10:07 +02:00
Date : June 2013
2013-05-19 17:44:57 +02:00
Copyright : ( C ) 2013 by Borys Jurgiel
Email : info at borysjurgiel dot pl
This module is based on former plugin_installer plugin :
Copyright ( C ) 2007 - 2008 Matthew Perry
Copyright ( C ) 2008 - 2013 Borys Jurgiel
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* This program is free software ; you can redistribute it and / or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation ; either version 2 of the License , or *
* ( at your option ) any later version . *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
"""
2016-03-21 05:04:29 +01:00
from PyQt . QtCore import QDir , QUrl , QFile , QCoreApplication
from PyQt . QtWidgets import QDialog
from PyQt . QtNetwork import QNetworkRequest , QNetworkReply
2013-05-19 17:44:57 +02:00
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
import qgis
2016-02-26 11:54:08 +02:00
from qgis . core import QgsNetworkAccessManager , QgsAuthManager
2013-05-19 17:44:57 +02:00
2016-03-21 05:04:29 +01:00
from . ui_qgsplugininstallerinstallingbase import Ui_QgsPluginInstallerInstallingDialogBase
from . installer_data import removeDir , repositories
from . unzip import unzip
2013-05-19 17:44:57 +02:00
class QgsPluginInstallerInstallingDialog ( QDialog , Ui_QgsPluginInstallerInstallingDialogBase ) :
2015-08-22 14:29:41 +02:00
# ----------------------------------------- #
def __init__ ( self , parent , plugin ) :
QDialog . __init__ ( self , parent )
self . setupUi ( self )
self . plugin = plugin
self . mResult = " "
self . progressBar . setRange ( 0 , 0 )
self . progressBar . setFormat ( " % p % " )
self . labelName . setText ( plugin [ " name " ] )
self . buttonBox . clicked . connect ( self . abort )
url = QUrl ( plugin [ " download_url " ] )
fileName = plugin [ " filename " ]
tmpDir = QDir . tempPath ( )
tmpPath = QDir . cleanPath ( tmpDir + " / " + fileName )
self . file = QFile ( tmpPath )
self . request = QNetworkRequest ( url )
2016-02-26 11:54:08 +02:00
authcfg = repositories . all ( ) [ plugin [ " zip_repository " ] ] [ " authcfg " ]
if authcfg and isinstance ( authcfg , basestring ) :
if not QgsAuthManager . instance ( ) . updateNetworkRequest (
self . request , authcfg . strip ( ) ) :
self . mResult = self . tr (
" Update of network request with authentication "
" credentials FAILED for configuration ' {0} ' " ) . format ( authcfg )
self . request = None
if self . request is not None :
self . reply = QgsNetworkAccessManager . instance ( ) . get ( self . request )
self . reply . downloadProgress . connect ( self . readProgress )
self . reply . finished . connect ( self . requestFinished )
self . stateChanged ( 4 )
def exec_ ( self ) :
if self . request is None :
return QDialog . Rejected
QDialog . exec_ ( self )
2015-08-22 14:29:41 +02:00
# ----------------------------------------- #
def result ( self ) :
return self . mResult
# ----------------------------------------- #
def stateChanged ( self , state ) :
messages = [ self . tr ( " Installing... " ) , self . tr ( " Resolving host name... " ) , self . tr ( " Connecting... " ) , self . tr ( " Host connected. Sending request... " ) , self . tr ( " Downloading data... " ) , self . tr ( " Idle " ) , self . tr ( " Closing connection... " ) , self . tr ( " Error " ) ]
self . labelState . setText ( messages [ state ] )
# ----------------------------------------- #
def readProgress ( self , done , total ) :
if total > 0 :
self . progressBar . setMaximum ( total )
self . progressBar . setValue ( done )
# ----------------------------------------- #
def requestFinished ( self ) :
reply = self . sender ( )
self . buttonBox . setEnabled ( False )
if reply . error ( ) != QNetworkReply . NoError :
self . mResult = reply . errorString ( )
if reply . error ( ) == QNetworkReply . OperationCanceledError :
self . mResult + = " <br/><br/> " + QCoreApplication . translate ( " QgsPluginInstaller " , " If you haven ' t cancelled the download manually, it might be caused by a timeout. In this case consider increasing the connection timeout value in QGIS options. " )
self . reject ( )
reply . deleteLater ( )
return
self . file . open ( QFile . WriteOnly )
self . file . write ( reply . readAll ( ) )
self . file . close ( )
self . stateChanged ( 0 )
reply . deleteLater ( )
pluginDir = qgis . utils . home_plugin_path
tmpPath = self . file . fileName ( )
# make sure that the parent directory exists
if not QDir ( pluginDir ) . exists ( ) :
QDir ( ) . mkpath ( pluginDir )
# if the target directory already exists as a link, remove the link without resolving:
QFile ( pluginDir + unicode ( QDir . separator ( ) ) + self . plugin [ " id " ] ) . remove ( )
try :
2016-03-21 05:04:29 +01:00
unzip ( unicode ( tmpPath ) , unicode ( pluginDir ) ) # test extract. If fails, then exception will be raised and no removing occurs
2015-08-22 14:29:41 +02:00
# removing old plugin files if exist
2016-03-21 05:04:29 +01:00
removeDir ( QDir . cleanPath ( pluginDir + " / " + self . plugin [ " id " ] ) ) # remove old plugin if exists
unzip ( unicode ( tmpPath ) , unicode ( pluginDir ) ) # final extract.
2015-08-22 14:29:41 +02:00
except :
self . mResult = self . tr ( " Failed to unzip the plugin package. Probably it ' s broken or missing from the repository. You may also want to make sure that you have write permission to the plugin directory: " ) + " \n " + pluginDir
self . reject ( )
return
try :
# cleaning: removing the temporary zip file
QFile ( tmpPath ) . remove ( )
except :
pass
self . close ( )
# ----------------------------------------- #
def abort ( self ) :
if self . reply . isRunning ( ) :
self . reply . finished . disconnect ( )
self . reply . abort ( )
del self . reply
self . mResult = self . tr ( " Aborted by user " )
self . reject ( )