Initial novacoin support
[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_nvc.i18n import _
26 from electrum_nvc 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         # prevent HTTP leaks if a proxy is set
58         if self.config.get('proxy'):
59             return
60         VersionGetter(self).start()
61
62     def callback(self, version):
63         self.latest_version = version
64         if(self.compare_versions(self.latest_version, self.current_version) == 1):
65             latest_seen = self.config.get("last_seen_version",ELECTRUM_VERSION)
66             if(self.compare_versions(self.latest_version, latest_seen) == 1):
67                 self.new_version = True
68                 self.emit(QtCore.SIGNAL('new_electrum_version'))
69
70     def new_electrum_version(self):
71         if self.new_version:
72             self.setText(_("New version available") + ": " + self.latest_version)
73             self.sb.insertPermanentWidget(1, self)
74
75     def compare_versions(self, version1, version2):
76         def normalize(v):
77             return [int(x) for x in re.sub(r'(\.0+)*$','', v).split(".")]
78         return cmp(normalize(version1), normalize(version2))
79
80     def ignore_this_version(self):
81         self.setText("")
82         self.config.set_key("last_seen_version", self.latest_version, True)
83         QMessageBox.information(self, _("Preference saved"), _("Notifications about this update will not be shown again."))
84         self.dialog.done(0)
85
86     def ignore_all_version(self):
87         self.setText("")
88         self.config.set_key("last_seen_version", "9.9.9", True)
89         QMessageBox.information(self, _("Preference saved"), _("No more notifications about version updates will be shown."))
90         self.dialog.done(0)
91   
92     def open_website(self):
93         webbrowser.open("http://electrum.org/download.html")
94         self.dialog.done(0)
95
96     def mouseReleaseEvent(self, event):
97         dialog = QDialog(self)
98         dialog.setWindowTitle(_('Electrum update'))
99         dialog.setModal(1)
100
101         main_layout = QGridLayout()
102         main_layout.addWidget(QLabel(_("A new version of Electrum is available:")+" " + self.latest_version), 0,0,1,3)
103         
104         ignore_version = QPushButton(_("Ignore this version"))
105         ignore_version.clicked.connect(self.ignore_this_version)
106
107         ignore_all_versions = QPushButton(_("Ignore all versions"))
108         ignore_all_versions.clicked.connect(self.ignore_all_version)
109
110         open_website = QPushButton(_("Goto download page"))
111         open_website.clicked.connect(self.open_website)
112
113         main_layout.addWidget(ignore_version, 1, 0)
114         main_layout.addWidget(ignore_all_versions, 1, 1)
115         main_layout.addWidget(open_website, 1, 2)
116
117         dialog.setLayout(main_layout)
118
119         self.dialog = dialog
120         
121         if not dialog.exec_(): return
122
123