generalize plugins to all guis
[electrum-nvc.git] / gui / gui_classic / __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:
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:
43     sys.exit("Error: Could not import icons_rc.py, please generate it with: 'pyrcc4 icons.qrc -o gui/gui_classic/icons_rc.py'")
44
45 from qt_util import *
46 from main_window import ElectrumWindow
47 from electrum.plugins import init_plugins
48
49 class Timer(QtCore.QThread):
50     def run(self):
51         while True:
52             self.emit(QtCore.SIGNAL('timersignal'))
53             time.sleep(0.5)
54
55 class OpenFileEventFilter(QObject):
56     def __init__(self, windows):
57         self.windows = windows
58         super(OpenFileEventFilter, self).__init__()
59
60     def eventFilter(self, obj, event):
61         if event.type() == QtCore.QEvent.FileOpen:
62             if len(self.windows) >= 1:
63                 self.windows[0].set_url(event.url().toString())
64                 return True
65         return False
66
67
68 class ElectrumGui:
69
70     def __init__(self, config, network, app=None):
71         self.network = network
72         self.config = config
73         self.windows = []
74         self.efilter = OpenFileEventFilter(self.windows)
75         if app is None:
76             self.app = QApplication(sys.argv)
77         self.app.installEventFilter(self.efilter)
78
79         init_plugins(self)
80
81
82
83     def expand(self):
84         """Hide the lite mode window and show pro-mode."""
85         self.config.set_key('lite_mode', False, True)
86         self.mini.hide()
87         self.expert.show()
88
89     def minimize(self):
90         self.config.set_key('lite_mode', True, True)
91         self.expert.hide()
92         self.mini.show()
93
94     def init_lite(self, wallet, expert, url):
95         import lite_window
96         if not self.check_qt_version():
97             return
98
99         actuator = lite_window.MiniActuator(self.config, wallet)
100         # Should probably not modify the current path but instead
101         # change the behaviour of rsrc(...)
102         old_path = QDir.currentPath()
103         actuator.load_theme()
104
105         mini = lite_window.MiniWindow(actuator, self.expand, self.config)
106         driver = lite_window.MiniDriver(wallet, mini)
107
108         # Reset path back to original value now that loading the GUI
109         # is completed.
110         QDir.setCurrent(old_path)
111         if url:
112             payto, amount, label, message, signature, identity, url = parse_url(url)
113             mini.set_payment_fields(payto, amount)
114
115         return mini
116
117     def check_qt_version(self):
118         qtVersion = qVersion()
119         return int(qtVersion[0]) >= 4 and int(qtVersion[2]) >= 7
120     
121
122     def main(self, url):
123
124         storage = WalletStorage(self.config)
125         if not storage.file_exists:
126             import installwizard
127             wizard = installwizard.InstallWizard(self.config, self.network, storage)
128             wallet = wizard.run()
129             if not wallet: 
130                 exit()
131         else:
132             wallet = Wallet(storage)
133
134         wallet.start_threads(self.network)
135
136             
137         self.main_window = w = ElectrumWindow(self.config, self.network, self.minimize)
138
139         # plugins that need to change the GUI do it here
140         run_hook('init')
141
142         w.load_wallet(wallet)
143
144         s = Timer()
145         s.start()
146
147         self.windows.append(w)
148         if url: w.set_url(url)
149         w.app = self.app
150         w.connect_slots(s)
151         w.update_wallet()
152
153         self.expert = w
154         self.mini = self.init_lite(wallet, w, url)
155         
156         if self.config.get('lite_mode'):
157             if not self.mini:
158                 QMessageBox.warning(None,"Could not start Lite GUI.", "Electrum was unable to load the 'Lite GUI' because it needs Qt version >= 4.7.\nChanging your config to use the 'Classic' GUI")
159                 self.config.set_key('lite_mode', False, True)
160                 sys.exit(0)
161             else:
162                 self.minimize()
163         else:
164             w.show()
165             if self.mini:
166                 self.mini.hide()
167
168         self.app.exec_()
169
170         wallet.stop_threads()
171
172