first commit
[bitcoin:spesmilo.git] / main.py
1 from PySide.QtCore import *
2 from PySide.QtGui import *
3 from PySide.QtWebKit import *
4 import core_interface
5 import cashier
6 import send
7
8 class ConnectingDialog(QDialog):
9     def __init__(self, parent):
10         super(ConnectingDialog, self).__init__(parent)
11         main_layout = QVBoxLayout(self)
12         main_layout.addWidget(QLabel(self.tr('Connecting...')))
13         progressbar = QProgressBar()
14         progressbar.setMinimum(0)
15         progressbar.setMaximum(0)
16         main_layout.addWidget(progressbar)
17         
18         button_layout = QHBoxLayout()
19         button_layout.addStretch()
20         abortbtn = QPushButton('&Abort')
21         abortbtn.clicked.connect(self.stop)
22         button_layout.addWidget(abortbtn)
23         main_layout.addLayout(button_layout)
24
25         if parent is not None:
26             self.setWindowIcon(parent.bitcoin_icon)
27         self.setAttribute(Qt.WA_DeleteOnClose, False)
28         self.show()
29
30     def stop(self):
31         self.hide()
32         self.parent().stop()
33
34     def closeEvent(self, event):
35         self.hide()
36         event.ignore()
37
38 class TrayIcon(QSystemTrayIcon):
39     def __init__(self, core, parent):
40         super(TrayIcon, self).__init__(parent)
41         self.core = core
42         self.current_window = None
43         self.create_menu()
44         self.setIcon(self.parent().bitcoin_icon)
45         self.activated.connect(self.toggle_window)
46         self.show()
47     
48     def create_menu(self):
49         tray_menu = QMenu()
50         self.cash_act = QAction(self.tr('&Cashier'), self)
51         self.cash_act.triggered.connect(self.show_cashier)
52         self.cash_act.setDisabled(True)
53         tray_menu.addAction(self.cash_act)
54         self.send_act = QAction(self.tr('&Send funds'), self)
55         self.send_act.triggered.connect(self.show_send)
56         self.send_act.setDisabled(True)
57         tray_menu.addAction(self.send_act)
58         tray_menu.addSeparator()
59         quit_act = QAction(self.tr('&Quit'), self)
60         quit_act.triggered.connect(self.quit)
61         tray_menu.addAction(quit_act)
62         self.setContextMenu(tray_menu)
63
64     def delete_window(self):
65         if self.current_window is not None:
66             self.current_window.deleteLater()
67
68     def create_connecting(self):
69         self.delete_window()
70         self.current_window = ConnectingDialog(self.parent())
71
72     def create_cashier(self):
73         self.cash_act.setDisabled(False)
74         self.send_act.setDisabled(False)
75         self.delete_window()
76         self.current_window = cashier.Cashier(self.core, qApp.clipboard(),
77                                               self.parent())
78         self.show_cashier()
79
80     def show_cashier(self):
81         self.current_window.show()
82
83     def toggle_window(self, reason):
84         if reason == self.Trigger:
85             if self.current_window is not None:
86                 if self.current_window.isVisible():
87                     self.current_window.hide()
88                 else:
89                     self.current_window.show()
90
91     def show_send(self):
92         send.SendDialog(self.core, self.parent())
93
94     def quit(self):
95         self.parent().stop()
96
97 class RootWindow(QMainWindow):
98     CLIENT_NONE = 0
99     CLIENT_CONNECTING = 1
100     CLIENT_DOWNLOADING = 2
101     CLIENT_RUNNING = 3
102
103     def __init__(self):
104         super(RootWindow, self).__init__()
105         self.core = core_interface.CoreInterface()
106         icon = lambda s: QIcon('./icons/' + s)
107         self.bitcoin_icon = icon('bitcoin32.xpm')
108         self.tray = TrayIcon(self.core, self)
109
110         self.state = self.CLIENT_NONE
111         refresh_state_timer = QTimer(self)
112         refresh_state_timer.timeout.connect(self.refresh_state)
113         refresh_state_timer.start(1000)
114         self.refresh_state()
115
116     def refresh_state(self):
117         if self.state == self.CLIENT_NONE:
118             self.state = self.CLIENT_CONNECTING
119             # show initialising dialog
120             self.tray.create_connecting()
121         elif self.state == self.CLIENT_CONNECTING:
122             if self.core.is_initialised():
123                 # some voodoo here checking whether we have blocks
124                 self.state = self.CLIENT_RUNNING
125                 self.tray.create_cashier()
126
127     def closeEvent(self, event):
128         super(RootWindow, self).closeEvent(event)
129         self.stop()
130
131     def stop(self):
132         try:
133             # Keep looping until connected so we can issue the stop command
134             while True:
135                 try:
136                     self.core.stop()
137                 except core_interface.JSONRPCException:
138                     pass
139                 except:
140                     raise
141                 else:
142                     break
143         # Re-proprogate exception & trigger app exit so we can break out
144         except:
145             raise
146         finally:
147             qApp.quit()
148
149 if __name__ == '__main__':
150     import os
151     import sys
152
153     os.system('bitcoind')
154     app = QApplication(sys.argv)
155     translator = QTranslator()
156     #translator.load('il8n/eo_EO')
157     app.installTranslator(translator)
158     app.setQuitOnLastWindowClosed(False)
159     rootwindow = RootWindow()
160     sys.exit(app.exec_())
161