169c05fdc4080c3e73a03b448cd753d40ba6c52d
[electrum-nvc.git] / gui / qt / __init__.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 sys, time, datetime, re, threading
20 from electrum.i18n import _, set_language
21 from electrum.util import print_error, print_msg, parse_url
22 from electrum.plugins import run_hook
23 import os.path, json, ast, traceback
24 import shutil
25
26
27 try:
28     import PyQt4
29 except Exception:
30     sys.exit("Error: Could not import PyQt4 on Linux systems, you may try 'sudo apt-get install python-qt4'")
31
32 from PyQt4.QtGui import *
33 from PyQt4.QtCore import *
34 import PyQt4.QtCore as QtCore
35
36 from electrum import WalletStorage, Wallet
37 from electrum.i18n import _
38 from electrum.bitcoin import MIN_RELAY_TX_FEE
39
40 try:
41     import icons_rc
42 except Exception:
43     sys.exit("Error: Could not import icons_rc.py, please generate it with: 'pyrcc4 icons.qrc -o gui/qt/icons_rc.py'")
44
45 from util import *
46 from main_window import ElectrumWindow
47 from electrum.plugins import init_plugins
48
49
50 class OpenFileEventFilter(QObject):
51     def __init__(self, windows):
52         self.windows = windows
53         super(OpenFileEventFilter, self).__init__()
54
55     def eventFilter(self, obj, event):
56         if event.type() == QtCore.QEvent.FileOpen:
57             if len(self.windows) >= 1:
58                 self.windows[0].set_url(event.url().toEncoded())
59                 return True
60         return False
61
62
63 class ElectrumGui:
64
65     def __init__(self, config, network, app=None):
66         self.network = network
67         self.config = config
68         self.windows = []
69         self.efilter = OpenFileEventFilter(self.windows)
70         if app is None:
71             self.app = QApplication(sys.argv)
72         self.app.installEventFilter(self.efilter)
73         init_plugins(self)
74         self.payment_request = None
75
76
77     def build_tray_menu(self):
78         m = QMenu()
79         m.addAction(_("Show/Hide"), self.show_or_hide)
80         m.addAction(_("Dark/Light"), self.toggle_tray_icon)
81         m.addSeparator()
82         m.addAction(_("Exit Electrum"), self.close)
83         self.tray.setContextMenu(m)
84
85     def toggle_tray_icon(self):
86         self.dark_icon = not self.dark_icon
87         self.config.set_key("dark_icon", self.dark_icon, True)
88         icon = QIcon(":icons/electrum_dark_icon.png") if self.dark_icon else QIcon(':icons/electrum_light_icon.png')
89         self.tray.setIcon(icon)
90
91     def show_or_hide(self):
92         self.tray_activated(QSystemTrayIcon.DoubleClick)
93
94     def tray_activated(self, reason):
95         if reason == QSystemTrayIcon.DoubleClick:
96             if self.current_window.isMinimized() or self.current_window.isHidden():
97                 self.current_window.show()
98                 self.current_window.raise_()
99             else:
100                 self.current_window.hide()
101
102     def close(self):
103         self.current_window.close()
104
105
106
107     def go_full(self):
108         self.config.set_key('lite_mode', False, True)
109         self.lite_window.hide()
110         self.main_window.show()
111         self.main_window.raise_()
112         self.current_window = self.main_window
113
114     def go_lite(self):
115         self.config.set_key('lite_mode', True, True)
116         self.main_window.hide()
117         self.lite_window.show()
118         self.lite_window.raise_()
119         self.current_window = self.lite_window
120
121
122     def init_lite(self):
123         import lite_window
124         if not self.check_qt_version():
125             if self.config.get('lite_mode') is True:
126                 msg = "Electrum was unable to load the 'Lite GUI' because it needs Qt version >= 4.7.\nChanging your config to use the 'Classic' GUI"
127                 QMessageBox.warning(None, "Could not start Lite GUI.", msg)
128                 self.config.set_key('lite_mode', False, True)
129                 sys.exit(0)
130             self.lite_window = None
131             self.main_window.show()
132             self.main_window.raise_()
133             return
134
135         actuator = lite_window.MiniActuator(self.main_window)
136         actuator.load_theme()
137         self.lite_window = lite_window.MiniWindow(actuator, self.go_full, self.config)
138         driver = lite_window.MiniDriver(self.main_window, self.lite_window)
139
140         if self.config.get('lite_mode') is True:
141             self.go_lite()
142         else:
143             self.go_full()
144
145
146     def check_qt_version(self):
147         qtVersion = qVersion()
148         return int(qtVersion[0]) >= 4 and int(qtVersion[2]) >= 7
149
150
151     def set_url(self, url):
152         from electrum import util
153         from decimal import Decimal
154
155         try:
156             address, amount, label, message, request_url, url = util.parse_url(url)
157         except Exception:
158             QMessageBox.warning(self.main_window, _('Error'), _('Invalid bitcoin URL'), _('OK'))
159             return
160
161         if amount:
162             try:
163                 if self.main_window.base_unit() == 'mBTC': 
164                     amount = str( 1000* Decimal(amount))
165                 else: 
166                     amount = str(Decimal(amount))
167             except Exception:
168                 amount = "0.0"
169                 QMessageBox.warning(self.main_window, _('Error'), _('Invalid Amount'), _('OK'))
170
171         if request_url:
172             from electrum import paymentrequest
173
174         if not request_url:
175             self.main_window.set_send(address, amount, label, message)
176             self.lite_window.set_payment_fields(address, amount)
177             return
178
179         def payment_request():
180             self.payment_request = paymentrequest.PaymentRequest(self.config)
181             self.payment_request.read(request_url)
182             if self.payment_request.verify():
183                 self.main_window.emit(SIGNAL('payment_request_ok'))
184             else:
185                 self.main_window.emit(SIGNAL('payment_request_error'))
186
187         threading.Thread(target=payment_request).start()
188         self.main_window.prepare_for_payment_request()
189
190
191     def main(self, url):
192
193         storage = WalletStorage(self.config)
194         if storage.file_exists:
195             wallet = Wallet(storage)
196             action = wallet.get_action()
197         else:
198             action = 'new'
199
200         if action is not None:
201             import installwizard
202             wizard = installwizard.InstallWizard(self.config, self.network, storage)
203             wallet = wizard.run(action)
204             if not wallet: 
205                 exit()
206         else:
207             wallet.start_threads(self.network)
208
209         # init tray
210         self.dark_icon = self.config.get("dark_icon", False)
211         icon = QIcon(":icons/electrum_dark_icon.png") if self.dark_icon else QIcon(':icons/electrum_light_icon.png')
212         self.tray = QSystemTrayIcon(icon, None)
213         self.tray.setToolTip('Electrum')
214         self.tray.activated.connect(self.tray_activated)
215         self.build_tray_menu()
216         self.tray.show()
217
218         # main window
219         self.main_window = w = ElectrumWindow(self.config, self.network, self)
220         self.current_window = self.main_window
221
222         #lite window
223         self.init_lite()
224
225         # plugins that need to change the GUI do it here
226         run_hook('init')
227
228         w.load_wallet(wallet)
229
230         s = Timer()
231         s.start()
232
233         self.windows.append(w)
234         if url: 
235             self.set_url(url)
236
237         w.app = self.app
238         w.connect_slots(s)
239         w.update_wallet()
240
241         self.app.exec_()
242
243         # clipboard persistence
244         # see http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg17328.html
245         event = QtCore.QEvent(QtCore.QEvent.Clipboard)
246         self.app.sendEvent(self.app.clipboard(), event)
247
248         wallet.stop_threads()
249
250