1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
"""
/***************************************************************************
qgsplugininstallerinstallingdialog.py
Plugin Installer module
-------------------
Date : June 2013
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 pathlib import Path
from qgis.PyQt import uic
from qgis.PyQt.QtCore import QDir, QUrl, QFile, QCoreApplication
from qgis.PyQt.QtWidgets import QDialog
from qgis.PyQt.QtNetwork import QNetworkRequest, QNetworkReply
from qgis.core import (
QgsNetworkAccessManager,
QgsApplication,
QgsNetworkRequestParameters,
)
from qgis.utils import HOME_PLUGIN_PATH
from .installer_data import removeDir, repositories
from .unzip import unzip
Ui_QgsPluginInstallerInstallingDialogBase, _ = uic.loadUiType(
Path(__file__).parent / "qgsplugininstallerinstallingbase.ui"
)
class QgsPluginInstallerInstallingDialog(
QDialog, Ui_QgsPluginInstallerInstallingDialogBase
):
# ----------------------------------------- #
def __init__(self, parent, plugin, stable=True):
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)
self.url = QUrl(
plugin["download_url_stable"]
if stable
else plugin["download_url_experimental"]
)
self.redirectionCounter = 0
fileName = plugin["filename"]
tmpDir = QDir.tempPath()
tmpPath = QDir.cleanPath(tmpDir + "/" + fileName)
self.file = QFile(tmpPath)
self.requestDownloading()
def requestDownloading(self):
self.request = QNetworkRequest(self.url)
self.request.setAttribute(
QNetworkRequest.Attribute(
QgsNetworkRequestParameters.RequestAttributes.AttributeInitiatorClass
),
"QgsPluginInstallerInstallingDialog",
)
authcfg = repositories.all()[self.plugin["zip_repository"]]["authcfg"]
if authcfg and isinstance(authcfg, str):
if not QgsApplication.authManager().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.DialogCode.Rejected
QDialog.exec(self)
# ----------------------------------------- #
def result(self):
return self.mResult
# ----------------------------------------- #
def stateChanged(self, state):
messages = [
QCoreApplication.translate(
"QgsPluginInstallerInstallingDialog", "Installing…"
),
QCoreApplication.translate(
"QgsPluginInstallerInstallingDialog", "Resolving host name…"
),
QCoreApplication.translate(
"QgsPluginInstallerInstallingDialog", "Connecting…"
),
QCoreApplication.translate(
"QgsPluginInstallerInstallingDialog", "Host connected. Sending request…"
),
QCoreApplication.translate(
"QgsPluginInstallerInstallingDialog", "Downloading data…"
),
self.tr("Idle"),
QCoreApplication.translate(
"QgsPluginInstallerInstallingDialog", "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.NetworkError.NoError:
self.mResult = reply.errorString()
if reply.error() == QNetworkReply.NetworkError.OperationCanceledError:
self.mResult += "<br/><br/>" + QCoreApplication.translate(
"QgsPluginInstaller",
"If you haven't canceled 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
elif reply.attribute(QNetworkRequest.Attribute.HttpStatusCodeAttribute) in (
301,
302,
):
redirectionUrl = reply.attribute(
QNetworkRequest.Attribute.RedirectionTargetAttribute
)
self.redirectionCounter += 1
if self.redirectionCounter > 4:
self.mResult = QCoreApplication.translate(
"QgsPluginInstaller", "Too many redirections"
)
self.reject()
reply.deleteLater()
return
else:
if redirectionUrl.isRelative():
redirectionUrl = reply.url().resolved(redirectionUrl)
# Fire a new request and exit immediately in order to quietly destroy the old one
self.url = redirectionUrl
self.requestDownloading()
reply.deleteLater()
return
self.file.open(QFile.OpenModeFlag.WriteOnly)
self.file.write(reply.readAll())
self.file.close()
self.stateChanged(0)
reply.deleteLater()
pluginDir = 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 + str(QDir.separator()) + self.plugin["id"]).remove()
try:
unzip(
str(tmpPath), str(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(str(tmpPath), str(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):
if self.reply.isRunning():
self.reply.finished.disconnect()
self.reply.abort()
del self.reply
self.mResult = self.tr("Aborted by user")
self.reject()
|