generalize plugins to all guis
[electrum-nvc.git] / gui / qt / version_getter.py
1 #!/usr/bin/env python
2 #
3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2012 thomasv@gitorious
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 import threading, httplib, re, socket
20 import webbrowser
21 from PyQt4.QtGui import *
22 from PyQt4.QtCore import *
23 import PyQt4.QtCore as QtCore
24
25 from electrum.i18n import _
26 from electrum import ELECTRUM_VERSION, print_error
27
28 class VersionGetter(threading.Thread):
29
30     def __init__(self, label):
31         threading.Thread.__init__(self)
32         self.label = label
33         
34     def run(self):
35         try:
36             con = httplib.HTTPConnection('electrum.org', 80, timeout=5)
37             con.request("GET", "/version")
38             res = con.getresponse()
39         except socket.error as msg:
40             print_error("Could not retrieve version information")
41             return
42             
43         if res.status == 200:
44             latest_version = res.read()
45             latest_version = latest_version.replace("\n","")
46             if(re.match('^\d+(\.\d+)*$', latest_version)):
47                 self.label.callback(latest_version)
48
49 class UpdateLabel(QLabel):
50     def __init__(self, config, sb):
51         QLabel.__init__(self)
52         self.new_version = False
53         self.sb = sb
54         self.config = config
55         self.current_version = ELECTRUM_VERSION
56         self.connect(self, QtCore.SIGNAL('new_electrum_version'), self.new_electrum_version)
57         VersionGetter(self).start()
58
59     def callback(self, version):
60         self.latest_version = version
61         if(self.compare_versions(self.latest_version, self.current_version) == 1):
62             latest_seen = self.config.get("last_seen_version",ELECTRUM_VERSION)
63             if(self.compare_versions(self.latest_version, latest_seen) == 1):
64                 self.new_version = True
65                 self.emit(QtCore.SIGNAL('new_electrum_version'))
66
67     def new_electrum_version(self):
68         if self.new_version:
69             self.setText(_("New version available") + ": " + self.latest_version)
70             self.sb.insertPermanentWidget(1, self)
71
72     def compare_versions(self, version1, version2):
73         def normalize(v):
74             return [int(x) for x in re.sub(r'(\.0+)*$','', v).split(".")]
75         return cmp(normalize(version1), normalize(version2))
76
77     def ignore_this_version(self):
78         self.setText("")
79         self.config.set_key("last_seen_version", self.latest_version, True)
80         QMessageBox.information(self, _("Preference saved"), _("Notifications about this update will not be shown again."))
81         self.dialog.done(0)
82
83     def ignore_all_version(self):
84         self.setText("")
85         self.config.set_key("last_seen_version", "9.9.9", True)
86         QMessageBox.information(self, _("Preference saved"), _("No more notifications about version updates will be shown."))
87         self.dialog.done(0)
88   
89     def open_website(self):
90         webbrowser.open("http://electrum.org/download.html")
91         self.dialog.done(0)
92
93     def mouseReleaseEvent(self, event):
94         dialog = QDialog(self)
95         dialog.setWindowTitle(_('Electrum update'))
96         dialog.setModal(1)
97
98         main_layout = QGridLayout()
99         main_layout.addWidget(QLabel(_("A new version of Electrum is available:")+" " + self.latest_version), 0,0,1,3)
100         
101         ignore_version = QPushButton(_("Ignore this version"))
102         ignore_version.clicked.connect(self.ignore_this_version)
103
104         ignore_all_versions = QPushButton(_("Ignore all versions"))
105         ignore_all_versions.clicked.connect(self.ignore_all_version)
106
107         open_website = QPushButton(_("Goto download page"))
108         open_website.clicked.connect(self.open_website)
109
110         main_layout.addWidget(ignore_version, 1, 0)
111         main_layout.addWidget(ignore_all_versions, 1, 1)
112         main_layout.addWidget(open_website, 1, 2)
113
114         dialog.setLayout(main_layout)
115
116         self.dialog = dialog
117         
118         if not dialog.exec_(): return
119
120