move tray logic to ElectrumGui object. fixes #468
[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
75
76     def build_tray_menu(self):
77         m = QMenu()
78         m.addAction(_("Show/Hide"), self.show_or_hide)
79         m.addAction(_("Dark/Light"), self.toggle_tray_icon)
80         m.addSeparator()
81         m.addAction(_("Exit Electrum"), self.close)
82         self.tray.setContextMenu(m)
83
84     def toggle_tray_icon(self):
85         self.dark_icon = not self.dark_icon
86         self.config.set_key("dark_icon", self.dark_icon, True)
87         icon = QIcon(":icons/electrum_dark_icon.png") if self.dark_icon else QIcon(':icons/electrum_light_icon.png')
88         self.tray.setIcon(icon)
89
90     def show_or_hide(self):
91         self.tray_activated(QSystemTrayIcon.DoubleClick)
92
93     def tray_activated(self, reason):
94         if reason == QSystemTrayIcon.DoubleClick:
95             if self.current_window.isMinimized() or self.current_window.isHidden():
96                 self.current_window.show()
97                 self.current_window.raise_()
98             else:
99                 self.current_window.hide()
100
101     def close(self):
102         self.current_window.close()
103
104
105
106     def go_full(self):
107         self.config.set_key('lite_mode', False, True)
108         self.lite_window.hide()
109         self.main_window.show()
110         self.main_window.raise_()
111         self.current_window = self.main_window
112
113     def go_lite(self):
114         self.config.set_key('lite_mode', True, True)
115         self.main_window.hide()
116         self.lite_window.show()
117         self.lite_window.raise_()
118         self.current_window = self.lite_window
119
120
121     def init_lite(self):
122         import lite_window
123         if not self.check_qt_version():
124             if self.config.get('lite_mode') is True:
125                 msg = "Electrum was unable to load the 'Lite GUI' because it needs Qt version >= 4.7.\nChanging your config to use the 'Classic' GUI"
126                 QMessageBox.warning(None, "Could not start Lite GUI.", msg)
127                 self.config.set_key('lite_mode', False, True)
128                 sys.exit(0)
129             self.lite_window = None
130             self.main_window.show()
131             self.main_window.raise_()
132             return
133
134         actuator = lite_window.MiniActuator(self.main_window)
135         actuator.load_theme()
136         self.lite_window = lite_window.MiniWindow(actuator, self.go_full, self.config)
137         driver = lite_window.MiniDriver(self.main_window, self.lite_window)
138
139         if self.config.get('lite_mode') is True:
140             self.go_lite()
141         else:
142             self.go_full()
143
144
145     def check_qt_version(self):
146         qtVersion = qVersion()
147         return int(qtVersion[0]) >= 4 and int(qtVersion[2]) >= 7
148
149
150
151     def main(self, url):
152
153         storage = WalletStorage(self.config)
154         if not storage.file_exists:
155             import installwizard
156             wizard = installwizard.InstallWizard(self.config, self.network, storage)
157             wallet = wizard.run()
158             if not wallet: 
159                 exit()
160
161         elif storage.get('wallet_type') in ['2of3'] and storage.get('seed') is None:
162             import installwizard
163             wizard = installwizard.InstallWizard(self.config, self.network, storage)
164             wallet = wizard.run(action= 'create2of3')
165             if not wallet: 
166                 exit()
167
168         else:
169             wallet = Wallet(storage)
170             wallet.start_threads(self.network)
171             
172
173         # init tray
174         self.dark_icon = self.config.get("dark_icon", False)
175         icon = QIcon(":icons/electrum_dark_icon.png") if self.dark_icon else QIcon(':icons/electrum_light_icon.png')
176         self.tray = QSystemTrayIcon(icon, None)
177         self.tray.setToolTip('Electrum')
178         self.tray.activated.connect(self.tray_activated)
179         self.build_tray_menu()
180         self.tray.show()
181
182         # main window
183         self.main_window = w = ElectrumWindow(self.config, self.network, self)
184         self.current_window = self.main_window
185
186         #lite window
187         self.init_lite()
188
189         # plugins that need to change the GUI do it here
190         run_hook('init')
191
192         w.load_wallet(wallet)
193
194         s = Timer()
195         s.start()
196
197         self.windows.append(w)
198         if url: w.set_url(url)
199         w.app = self.app
200         w.connect_slots(s)
201         w.update_wallet()
202
203         self.app.exec_()
204
205         wallet.stop_threads()
206
207