Debian Codegraph @

wxgeometrie/GUI/nouvelles_versions.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # WxGeometrie
4 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
5 # Copyright (C) 2005-2013 Nicolas Pourcelot
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
21
22 import webbrowser
23 from urllib.request import urlopen
24
25 from PyQt5.QtCore import QObject, pyqtSignal
26 from PyQt5.QtWidgets import QMessageBox
27
28 from ..pylib import print_error
29 from .. import param
30 from .qtlib import GenericThread
31 from .app import app
32
33
34 class Gestionnaire_mises_a_jour(QObject):
35 url_version = "http://wxgeo.free.fr/wordpress/version_geophar"
36 url_telechargement = "http://sourceforge.net/projects/geophar/files/latest/download"
37
38 def __init__(self, parent):
39 QObject.__init__(self)
40 self.parent = parent
41 self.derniere_version = None
42 self.sent.connect(self.termine)
43
44 sent = pyqtSignal(bool, bool, str, str)
45
46
47 def termine(self, success, update, version, msg):
48 if success:
49 if update:
50 self.derniere_version = version
51 QMessageBox.information(self.parent, "Une mise à jour a été trouvée.",
52 ("La version %s de %s est sortie.\n"
53 "Vous allez être redirigé vers la page de téléchargement.")
54 % (version, param.NOMPROG))
55 webbrowser.open(self.url_telechargement)
56 else:
57 QMessageBox.information(self.parent, "Aucune mise à jour trouvée.",
58 "Aucune mise à jour n'est disponible actuellement.<br>"
59 "Consultez <a href='http://wxgeo.free.fr/wordpress/'>"
60 "http://wxgeo.free.fr</a> pour plus d'informations.")
61 else:
62 print('Connexion impossible à ' + self.url_version + ' : ' + msg)
63 QMessageBox.warning(self.parent, "Connexion impossible",
64 "Impossible de vérifier si une nouvelle version existe.<br>"
65 "Consultez <a href='http://wxgeo.free.fr/wordpress/'>"
66 "http://wxgeo.free.fr</a> pour plus d'informations.")
67
68
69 def verifier_version(self, event=None):
70 self.thread = GenericThread(self._verifier_version)
71 self.thread.start()
72
73
74 def _verifier_version(self):
75 # /!\ Ne **JAMAIS** utiliser `print()` depuis une autre thread que la principale !
76 # Utiliser `app.safe_print()` à la place.
77 version = '?'
78 success = False
79 update = False
80 msg = 'Unknown error.'
81 try:
82 if param.debug:
83 app.safe_print("Checking %s..." % self.url_version)
84 with urlopen(self.url_version) as f:
85 version = f.read(60).decode('utf8')
86 if len(version) > 50 or not version.replace(" ", "").replace(".", "").isalnum():
87 raise Exception("Incorrect file format, unable to find current version.")
88 success = True
89 if version.split(".") > param.version.split('.'):
90 update = True
91 else:
92 update = False
93 except Exception as e:
94 # /!\ print_error() is not thread safe.
95 app.safe_print_error()
96 msg = str(e)
97
98 self.sent.emit(success, update, version, msg)