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 . *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
"""
from PyQt4 . QtCore import *
from PyQt4 . QtGui import *
2013-06-23 21:15:58 +02:00
from qgis . core import QgsApplication , QgsNetworkAccessManager
2013-05-19 17:44:57 +02:00
from ui_qgsplugininstallerinstallingbase import Ui_QgsPluginInstallerInstallingDialogBase
from installer_data import *
from unzip import unzip
class QgsPluginInstallerInstallingDialog ( QDialog , Ui_QgsPluginInstallerInstallingDialogBase ) :
# ----------------------------------------- #
def __init__ ( self , parent , plugin ) :
QDialog . __init__ ( self , parent )
self . setupUi ( self )
self . plugin = plugin
2013-06-08 16:00:13 +10:00
self . mResult = " "
2013-05-19 17:44:57 +02:00
self . progressBar . setRange ( 0 , 0 )
2013-06-08 16:00:13 +10:00
self . progressBar . setFormat ( " % p % " )
self . labelName . setText ( plugin [ " name " ] )
self . buttonBox . clicked . connect ( self . abort )
2013-05-19 17:44:57 +02:00
url = QUrl ( plugin [ " download_url " ] )
2013-06-20 23:40:51 +02:00
2013-05-19 17:44:57 +02:00
fileName = plugin [ " filename " ]
tmpDir = QDir . tempPath ( )
tmpPath = QDir . cleanPath ( tmpDir + " / " + fileName )
self . file = QFile ( tmpPath )
2013-06-20 23:40:51 +02:00
2013-06-11 21:19:17 +01:00
self . request = QNetworkRequest ( url )
2013-06-23 21:15:58 +02:00
self . reply = QgsNetworkAccessManager . instance ( ) . get ( self . request )
2013-06-11 21:19:17 +01:00
self . reply . downloadProgress . connect ( self . readProgress )
2013-06-23 21:15:58 +02:00
self . reply . finished . connect ( self . requestFinished )
2013-06-11 21:19:17 +01:00
self . stateChanged ( 4 )
2013-05-19 17:44:57 +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 ) :
2013-06-11 21:19:17 +01:00
if total > 0 :
self . progressBar . setMaximum ( total )
self . progressBar . setValue ( done )
2013-05-19 17:44:57 +02:00
2013-06-25 21:25:38 +02:00
2013-05-19 17:44:57 +02:00
# ----------------------------------------- #
2013-06-23 21:15:58 +02:00
def requestFinished ( self ) :
reply = self . sender ( )
2013-05-19 17:44:57 +02:00
self . buttonBox . setEnabled ( False )
2013-06-20 23:40:51 +02:00
if reply . error ( ) != QNetworkReply . NoError :
2013-06-25 15:01:18 +02:00
self . mResult = reply . errorString ( )
2013-06-25 21:25:38 +02:00
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. " )
2013-05-19 17:44:57 +02:00
self . reject ( )
2013-07-07 21:43:09 +02:00
reply . deleteLater ( )
2013-05-19 17:44:57 +02:00
return
2013-06-11 21:19:17 +01:00
self . file . open ( QFile . WriteOnly )
self . file . write ( reply . readAll ( ) )
2013-05-19 17:44:57 +02:00
self . file . close ( )
2013-06-11 21:19:17 +01:00
self . stateChanged ( 0 )
2013-07-07 21:43:09 +02:00
reply . deleteLater ( )
2013-05-19 17:44:57 +02:00
pluginDir = QFileInfo ( QgsApplication . qgisUserDbFilePath ( ) ) . path ( ) + " /python/plugins "
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:
2013-06-08 16:00:13 +10:00
QFile ( pluginDir + unicode ( QDir . separator ( ) ) + self . plugin [ " id " ] ) . remove ( )
2013-05-19 17:44:57 +02:00
try :
unzip ( unicode ( tmpPath ) , unicode ( pluginDir ) ) # test extract. If fails, then exception will be raised and no removing occurs
# removing old plugin files if exist
removeDir ( QDir . cleanPath ( pluginDir + " / " + self . plugin [ " id " ] ) ) # remove old plugin if exists
unzip ( unicode ( tmpPath ) , unicode ( pluginDir ) ) # final extract.
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 ) :
2013-06-11 21:19:17 +01:00
if self . reply . isRunning ( ) :
2013-06-23 21:15:58 +02:00
self . reply . finished . disconnect ( )
2013-06-20 23:40:51 +02:00
self . reply . abort ( )
2013-06-23 21:15:58 +02:00
del self . reply
2013-05-19 17:44:57 +02:00
self . mResult = self . tr ( " Aborted by user " )
self . reject ( )