remove ElectrumGui class for lite window
[electrum-nvc.git] / gui / gui_classic / lite_window.py
1 import sys
2
3 # Let's do some dep checking and handle missing ones gracefully
4 try:
5     from PyQt4.QtCore import *
6     from PyQt4.QtGui import *
7     from PyQt4.Qt import Qt
8     import PyQt4.QtCore as QtCore
9
10 except ImportError:
11     print "You need to have PyQT installed to run Electrum in graphical mode."
12     print "If you have pip installed try 'sudo pip install pyqt' if you are on Debian/Ubuntu try 'sudo apt-get install python-qt4'."
13     sys.exit(0)
14
15 from decimal import Decimal as D
16 from electrum.util import get_resource_path as rsrc
17 from electrum.bitcoin import is_valid
18 from electrum.i18n import _
19 import decimal
20 import exchange_rate
21 import json
22 import os.path
23 import random
24 import re
25 import time
26 from electrum.wallet import Wallet, WalletStorage
27 import webbrowser
28 import history_widget
29 import receiving_widget
30 from electrum import util
31 import csv 
32 import datetime
33
34 from electrum.version import ELECTRUM_VERSION as electrum_version
35 from electrum.util import format_satoshis, age
36
37 from main_window import ElectrumWindow
38 import shutil
39
40 from qt_util import *
41
42 bitcoin = lambda v: v * 100000000
43
44 def IconButton(filename, parent=None):
45     pixmap = QPixmap(filename)
46     icon = QIcon(pixmap)
47     return QPushButton(icon, "", parent)
48
49 class Timer(QThread):
50     def run(self):
51         while True:
52             self.emit(SIGNAL('timersignal'))
53             time.sleep(0.5)
54
55 def resize_line_edit_width(line_edit, text_input):
56     metrics = QFontMetrics(qApp.font())
57     # Create an extra character to add some space on the end
58     text_input += "A"
59     line_edit.setMinimumWidth(metrics.width(text_input))
60
61 def load_theme_name(theme_path):
62     try:
63         with open(os.path.join(theme_path, "name.cfg")) as name_cfg_file:
64             return name_cfg_file.read().rstrip("\n").strip()
65     except IOError:
66         return None
67
68
69 def theme_dirs_from_prefix(prefix):
70     if not os.path.exists(prefix):
71         return []
72     theme_paths = {}
73     for potential_theme in os.listdir(prefix):
74         theme_full_path = os.path.join(prefix, potential_theme)
75         theme_css = os.path.join(theme_full_path, "style.css")
76         if not os.path.exists(theme_css):
77             continue
78         theme_name = load_theme_name(theme_full_path)
79         if theme_name is None:
80             continue
81         theme_paths[theme_name] = prefix, potential_theme
82     return theme_paths
83
84 def load_theme_paths():
85     theme_paths = {}
86     prefixes = (util.local_data_dir(), util.appdata_dir())
87     for prefix in prefixes:
88         theme_paths.update(theme_dirs_from_prefix(prefix))
89     return theme_paths
90
91
92 def csv_transaction(wallet):
93     try:
94         select_export = _('Select file to export your wallet transactions to')
95         fileName = QFileDialog.getSaveFileName(QWidget(), select_export, os.path.expanduser('~/electrum-history.csv'), "*.csv")
96         if fileName:
97             with open(fileName, "w+") as csvfile:
98                 transaction = csv.writer(csvfile)
99                 transaction.writerow(["transaction_hash","label", "confirmations", "value", "fee", "balance", "timestamp"])
100                 for item in wallet.get_tx_history():
101                     tx_hash, confirmations, is_mine, value, fee, balance, timestamp = item
102                     if confirmations:
103                         if timestamp is not None:
104                             try:
105                                 time_string = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
106                             except [RuntimeError, TypeError, NameError] as reason:
107                                 time_string = "unknown"
108                                 pass
109                         else:
110                           time_string = "unknown"
111                     else:
112                         time_string = "pending"
113
114                     if value is not None:
115                         value_string = format_satoshis(value, True, wallet.num_zeros)
116                     else:
117                         value_string = '--'
118
119                     if fee is not None:
120                         fee_string = format_satoshis(fee, True, wallet.num_zeros)
121                     else:
122                         fee_string = '0'
123
124                     if tx_hash:
125                         label, is_default_label = wallet.get_label(tx_hash)
126                     else:
127                       label = ""
128
129                     balance_string = format_satoshis(balance, False, wallet.num_zeros)
130                     transaction.writerow([tx_hash, label, confirmations, value_string, fee_string, balance_string, time_string])
131                 QMessageBox.information(None,"CSV Export created", "Your CSV export has been successfully created.")
132     except (IOError, os.error), reason:
133         export_error_label = _("Electrum was unable to produce a transaction export.")
134         QMessageBox.critical(None,"Unable to create csv", export_error_label + "\n" + str(reason))
135
136
137
138 class TransactionWindow(QDialog):
139
140     def set_label(self):
141         label = unicode(self.label_edit.text())
142         self.parent.wallet.labels[self.tx_id] = label
143
144         super(TransactionWindow, self).accept() 
145
146     def __init__(self, transaction_id, parent):
147         super(TransactionWindow, self).__init__()
148
149         self.tx_id = str(transaction_id)
150         self.parent = parent
151
152         self.setModal(True)
153         self.resize(200,100)
154         self.setWindowTitle(_("Transaction successfully sent"))
155
156         self.layout = QGridLayout(self)
157         history_label = "%s\n%s" % (_("Your transaction has been sent."), _("Please enter a label for this transaction for future reference."))
158         self.layout.addWidget(QLabel(history_label))
159
160         self.label_edit = QLineEdit()
161         self.label_edit.setPlaceholderText(_("Transaction label"))
162         self.label_edit.setObjectName("label_input")
163         self.label_edit.setAttribute(Qt.WA_MacShowFocusRect, 0)
164         self.label_edit.setFocusPolicy(Qt.ClickFocus)
165         self.layout.addWidget(self.label_edit)
166
167         self.save_button = QPushButton(_("Save"))
168         self.layout.addWidget(self.save_button)
169         self.save_button.clicked.connect(self.set_label)
170
171         self.exec_()
172
173 class MiniWindow(QDialog):
174
175     def __init__(self, actuator, expand_callback, config):
176         super(MiniWindow, self).__init__()
177         tx = "e08115d0f7819aee65b9d24f81ef9d46eb62bb67ddef5318156cbc3ceb7b703e"
178
179         self.actuator = actuator
180         self.config = config
181         self.btc_balance = None
182         self.quote_currencies = ["BRL", "CNY", "EUR", "GBP", "RUB", "USD"]
183         self.actuator.set_configured_currency(self.set_quote_currency)
184         self.exchanger = exchange_rate.Exchanger(self)
185         # Needed because price discovery is done in a different thread
186         # which needs to be sent back to this main one to update the GUI
187         self.connect(self, SIGNAL("refresh_balance()"), self.refresh_balance)
188
189         self.balance_label = BalanceLabel(self.change_quote_currency)
190         self.balance_label.setObjectName("balance_label")
191
192
193         # Bitcoin address code
194         self.address_input = QLineEdit()
195         self.address_input.setPlaceholderText(_("Enter a Bitcoin address or contact"))
196         self.address_input.setObjectName("address_input")
197
198         self.address_input.setFocusPolicy(Qt.ClickFocus)
199
200         self.address_input.textChanged.connect(self.address_field_changed)
201         resize_line_edit_width(self.address_input,
202                                "1BtaFUr3qVvAmwrsuDuu5zk6e4s2rxd2Gy")
203
204         self.address_completions = QStringListModel()
205         address_completer = QCompleter(self.address_input)
206         address_completer.setCaseSensitivity(False)
207         address_completer.setModel(self.address_completions)
208         self.address_input.setCompleter(address_completer)
209
210         address_layout = QHBoxLayout()
211         address_layout.addWidget(self.address_input)
212
213         self.amount_input = QLineEdit()
214         self.amount_input.setPlaceholderText(_("... and amount"))
215         self.amount_input.setObjectName("amount_input")
216
217         self.amount_input.setFocusPolicy(Qt.ClickFocus)
218         # This is changed according to the user's displayed balance
219         self.amount_validator = QDoubleValidator(self.amount_input)
220         self.amount_validator.setNotation(QDoubleValidator.StandardNotation)
221         self.amount_validator.setDecimals(8)
222         self.amount_input.setValidator(self.amount_validator)
223
224         # This removes the very ugly OSX highlighting, please leave this in :D
225         self.address_input.setAttribute(Qt.WA_MacShowFocusRect, 0)
226         self.amount_input.setAttribute(Qt.WA_MacShowFocusRect, 0)
227         self.amount_input.textChanged.connect(self.amount_input_changed)
228
229         if self.actuator.wallet.seed:
230             self.send_button = QPushButton(_("&Send"))
231         else:
232             self.send_button = QPushButton(_("&Create"))
233
234         self.send_button.setObjectName("send_button")
235         self.send_button.setDisabled(True);
236         self.send_button.clicked.connect(self.send)
237
238         # Creating the receive button
239         self.switch_button = QPushButton( QIcon(":icons/switchgui.png"),'' )
240         self.switch_button.setMaximumWidth(25)
241         self.switch_button.setFlat(True)
242         self.switch_button.clicked.connect(expand_callback)
243
244         main_layout = QGridLayout(self)
245
246         main_layout.addWidget(self.balance_label, 0, 0, 1, 3)
247         main_layout.addWidget(self.switch_button, 0, 3)
248
249         main_layout.addWidget(self.address_input, 1, 0, 1, 4)
250         main_layout.addWidget(self.amount_input, 2, 0, 1, 2)
251         main_layout.addWidget(self.send_button, 2, 2, 1, 2)
252
253         self.send_button.setMaximumWidth(125)
254
255         self.history_list = history_widget.HistoryWidget()
256         self.history_list.setObjectName("history")
257         self.history_list.hide()
258         self.history_list.setAlternatingRowColors(True)
259
260         main_layout.addWidget(self.history_list, 3, 0, 1, 4)
261
262         self.receiving = receiving_widget.ReceivingWidget(self)
263         self.receiving.setObjectName("receiving")
264
265         # Add to the right side 
266         self.receiving_box = QGroupBox(_("Select a receiving address"))
267         extra_layout = QGridLayout()
268
269         # Checkbox to filter used addresses
270         hide_used = QCheckBox(_('Hide used addresses'))
271         hide_used.setChecked(True)
272         hide_used.stateChanged.connect(self.receiving.toggle_used)
273
274         # Events for receiving addresses
275         self.receiving.clicked.connect(self.receiving.copy_address)
276         self.receiving.itemDoubleClicked.connect(self.receiving.edit_label)
277         self.receiving.itemChanged.connect(self.receiving.update_label)
278
279
280         # Label
281         extra_layout.addWidget( QLabel(_('Selecting an address will copy it to the clipboard.') + '\n' + _('Double clicking the label will allow you to edit it.') ),0,0)
282
283         extra_layout.addWidget(self.receiving, 1,0)
284         extra_layout.addWidget(hide_used, 2,0)
285         extra_layout.setColumnMinimumWidth(0,200)
286
287         self.receiving_box.setLayout(extra_layout)
288         main_layout.addWidget(self.receiving_box,0,4,-1,3)
289         self.receiving_box.hide()
290
291         # Creating the menu bar
292         menubar = QMenuBar()
293         electrum_menu = menubar.addMenu(_("&Electrum"))
294
295         quit_option = electrum_menu.addAction(_("&Close"))
296
297         quit_option.triggered.connect(self.close)
298
299         view_menu = menubar.addMenu(_("&View"))
300         extra_menu = menubar.addMenu(_("&Extra"))
301
302         backup_wallet_menu = extra_menu.addAction( _("&Create wallet backup"))
303         backup_wallet_menu.triggered.connect(lambda: backup_wallet(self.config.path))
304
305         export_csv = extra_menu.addAction( _("&Export transactions to CSV") )
306         export_csv.triggered.connect(lambda: csv_transaction(self.actuator.wallet))
307         
308         master_key = extra_menu.addAction( _("Copy master public key to clipboard") ) 
309         master_key.triggered.connect(self.actuator.copy_master_public_key)
310
311         expert_gui = view_menu.addAction(_("&Classic GUI"))
312         expert_gui.triggered.connect(expand_callback)
313         themes_menu = view_menu.addMenu(_("&Themes"))
314         selected_theme = self.actuator.selected_theme()
315         theme_group = QActionGroup(self)
316         for theme_name in self.actuator.theme_names():
317             theme_action = themes_menu.addAction(theme_name)
318             theme_action.setCheckable(True)
319             if selected_theme == theme_name:
320                 theme_action.setChecked(True)
321             class SelectThemeFunctor:
322                 def __init__(self, theme_name, toggle_theme):
323                     self.theme_name = theme_name
324                     self.toggle_theme = toggle_theme
325                 def __call__(self, checked):
326                     if checked:
327                         self.toggle_theme(self.theme_name)
328             delegate = SelectThemeFunctor(theme_name, self.toggle_theme)
329             theme_action.toggled.connect(delegate)
330             theme_group.addAction(theme_action)
331         view_menu.addSeparator()
332
333         show_receiving = view_menu.addAction(_("Show Receiving addresses"))
334         show_receiving.setCheckable(True)
335         show_receiving.toggled.connect(self.toggle_receiving_layout)
336
337         show_receiving_toggle = self.config.get("gui_show_receiving",False)
338         show_receiving.setChecked(show_receiving_toggle)
339         self.show_receiving = show_receiving
340
341         self.toggle_receiving_layout(show_receiving_toggle)
342
343
344         show_history = view_menu.addAction(_("Show History"))
345         show_history.setCheckable(True)
346         show_history.toggled.connect(self.show_history)
347
348         help_menu = menubar.addMenu(_("&Help"))
349         the_website = help_menu.addAction(_("&Website"))
350         the_website.triggered.connect(self.the_website)
351         help_menu.addSeparator()
352         report_bug = help_menu.addAction(_("&Report Bug"))
353         report_bug.triggered.connect(self.show_report_bug)
354         show_about = help_menu.addAction(_("&About"))
355         show_about.triggered.connect(self.show_about)
356         main_layout.setMenuBar(menubar)
357         self.main_layout = main_layout
358
359         quit_shortcut = QShortcut(QKeySequence("Ctrl+Q"), self)
360         quit_shortcut.activated.connect(self.close)
361         close_shortcut = QShortcut(QKeySequence("Ctrl+W"), self)
362         close_shortcut.activated.connect(self.close)
363
364         g = self.config.get("winpos-lite",[4, 25, 351, 149])
365         self.setGeometry(g[0], g[1], g[2], g[3])
366
367         show_hist = self.config.get("gui_show_history",False)
368         show_history.setChecked(show_hist)
369         self.show_history(show_hist)
370         
371         self.setWindowIcon(QIcon(":icons/electrum.png"))
372         self.setWindowTitle("Electrum")
373         self.setWindowFlags(Qt.Window|Qt.MSWindowsFixedSizeDialogHint)
374         self.layout().setSizeConstraint(QLayout.SetFixedSize)
375         self.setObjectName("main_window")
376         self.show()
377
378
379     def toggle_theme(self, theme_name):
380         old_path = QDir.currentPath()
381         self.actuator.change_theme(theme_name)
382         # Recompute style globally
383         qApp.style().unpolish(self)
384         qApp.style().polish(self)
385         QDir.setCurrent(old_path)
386
387     def closeEvent(self, event):
388         g = self.geometry()
389         self.config.set_key("winpos-lite", [g.left(),g.top(),g.width(),g.height()],True)
390         self.config.set_key("gui_show_history", self.history_list.isVisible(),True)
391         self.config.set_key("gui_show_receiving", self.receiving_box.isVisible(),True)
392         
393         super(MiniWindow, self).closeEvent(event)
394         qApp.quit()
395
396     def set_payment_fields(self, dest_address, amount):
397         self.address_input.setText(dest_address)
398         self.address_field_changed(dest_address)
399         self.amount_input.setText(amount)
400
401     def activate(self):
402         pass
403
404     def deactivate(self):
405         pass
406
407     def set_quote_currency(self, currency):
408         """Set and display the fiat currency country."""
409         if currency not in self.quote_currencies:
410             return
411         self.quote_currencies.remove(currency)
412         self.quote_currencies.insert(0, currency)
413         self.refresh_balance()
414
415     def change_quote_currency(self, forward=True):
416         if forward:
417             self.quote_currencies = \
418                 self.quote_currencies[1:] + self.quote_currencies[0:1]
419         else:
420             self.quote_currencies = \
421                 self.quote_currencies[-1:] + self.quote_currencies[0:-1]
422         self.actuator.set_config_currency(self.quote_currencies[0])
423         self.refresh_balance()
424
425     def refresh_balance(self):
426         if self.btc_balance is None:
427             # Price has been discovered before wallet has been loaded
428             # and server connect... so bail.
429             return
430         self.set_balances(self.btc_balance)
431         self.amount_input_changed(self.amount_input.text())
432
433     def set_balances(self, btc_balance):
434         """Set the bitcoin balance and update the amount label accordingly."""
435         self.btc_balance = btc_balance
436         quote_text = self.create_quote_text(btc_balance)
437         if quote_text:
438             quote_text = "(%s)" % quote_text
439         btc_balance = "%.4f" % (btc_balance / bitcoin(1))
440         self.balance_label.set_balance_text(btc_balance, quote_text)
441         self.setWindowTitle("Electrum %s - %s BTC" % (electrum_version, btc_balance))
442
443     def amount_input_changed(self, amount_text):
444         """Update the number of bitcoins displayed."""
445         self.check_button_status()
446
447         try:
448             amount = D(str(amount_text))
449         except decimal.InvalidOperation:
450             self.balance_label.show_balance()
451         else:
452             quote_text = self.create_quote_text(amount * bitcoin(1))
453             if quote_text:
454                 self.balance_label.set_amount_text(quote_text)
455                 self.balance_label.show_amount()
456             else:
457                 self.balance_label.show_balance()
458
459     def create_quote_text(self, btc_balance):
460         """Return a string copy of the amount fiat currency the 
461         user has in bitcoins."""
462         quote_currency = self.quote_currencies[0]
463         quote_balance = self.exchanger.exchange(btc_balance, quote_currency)
464         if quote_balance is None:
465             quote_text = ""
466         else:
467             quote_text = "%.2f %s" % ((quote_balance / bitcoin(1)),
468                                       quote_currency)
469         return quote_text
470
471     def send(self):
472         if self.actuator.send(self.address_input.text(),
473                               self.amount_input.text(), self):
474             self.address_input.setText("")
475             self.amount_input.setText("")
476
477     def check_button_status(self):
478         """Check that the bitcoin address is valid and that something
479         is entered in the amount before making the send button clickable."""
480         try:
481             value = D(str(self.amount_input.text())) * 10**8
482         except decimal.InvalidOperation:
483             value = None
484         # self.address_input.property(...) returns a qVariant, not a bool.
485         # The == is needed to properly invoke a comparison.
486         if (self.address_input.property("isValid") == True and
487             value is not None and 0 < value <= self.btc_balance):
488             self.send_button.setDisabled(False)
489         else:
490             self.send_button.setDisabled(True)
491
492     def address_field_changed(self, address):
493         # label or alias, with address in brackets
494         match2 = re.match("(.*?)\s*\<([1-9A-HJ-NP-Za-km-z]{26,})\>",
495                           address)
496         if match2:
497           address = match2.group(2)
498           self.address_input.setText(address)
499
500         if is_valid(address):
501             self.check_button_status()
502             self.address_input.setProperty("isValid", True)
503             self.recompute_style(self.address_input)
504         else:
505             self.send_button.setDisabled(True)
506             self.address_input.setProperty("isValid", False)
507             self.recompute_style(self.address_input)
508
509         if len(address) == 0:
510             self.address_input.setProperty("isValid", None)
511             self.recompute_style(self.address_input)
512
513     def recompute_style(self, element):
514         self.style().unpolish(element)
515         self.style().polish(element)
516
517     def copy_address(self):
518         receive_popup = ReceivePopup(self.receive_button)
519         self.actuator.copy_address(receive_popup)
520
521     def update_completions(self, completions):
522         self.address_completions.setStringList(completions)
523  
524
525     def update_history(self, tx_history):
526
527         self.history_list.empty()
528
529         for item in tx_history[-10:]:
530             tx_hash, conf, is_mine, value, fee, balance, timestamp = item
531             label = self.actuator.wallet.get_label(tx_hash)[0]
532             #amount = D(value) / 10**8
533             v_str = format_satoshis(value, True)
534             self.history_list.append(label, v_str, age(timestamp))
535
536     def acceptbit(self):
537         self.actuator.acceptbit(self.quote_currencies[0])
538
539     def the_website(self):
540         webbrowser.open("http://electrum.org")
541
542     def show_about(self):
543         QMessageBox.about(self, "Electrum",
544             _("Version")+" %s" % (electrum_version) + "\n\n" + _("Electrum's focus is speed, with low resource usage and simplifying Bitcoin. You do not need to perform regular backups, because your wallet can be recovered from a secret phrase that you can memorize or write on paper. Startup times are instant because it operates in conjunction with high-performance servers that handle the most complicated parts of the Bitcoin system."))
545
546     def show_report_bug(self):
547         QMessageBox.information(self, "Electrum - " + _("Reporting Bugs"),
548             _("Please report any bugs as issues on github:")+" <a href=\"https://github.com/spesmilo/electrum/issues\">https://github.com/spesmilo/electrum/issues</a>")
549
550     def toggle_receiving_layout(self, toggle_state):
551         if toggle_state:
552           self.receiving_box.show()
553         else:
554           self.receiving_box.hide()
555
556     def show_history(self, toggle_state):
557         if toggle_state:
558             self.main_layout.setRowMinimumHeight(3,200)
559             self.history_list.show()
560         else:
561             self.main_layout.setRowMinimumHeight(3,0)
562             self.history_list.hide()
563
564 class BalanceLabel(QLabel):
565
566     SHOW_CONNECTING = 1
567     SHOW_BALANCE = 2
568     SHOW_AMOUNT = 3
569
570     def __init__(self, change_quote_currency, parent=None):
571         super(QLabel, self).__init__(_("Connecting..."), parent)
572         self.change_quote_currency = change_quote_currency
573         self.state = self.SHOW_CONNECTING
574         self.balance_text = ""
575         self.amount_text = ""
576
577     def mousePressEvent(self, event):
578         """Change the fiat currency selection if window background is clicked."""
579         if self.state != self.SHOW_CONNECTING:
580             self.change_quote_currency(event.button() == Qt.LeftButton)
581
582     def set_balance_text(self, btc_balance, quote_text):
583         """Set the amount of bitcoins in the gui."""
584         if self.state == self.SHOW_CONNECTING:
585             self.state = self.SHOW_BALANCE
586         self.balance_text = "<span style='font-size: 18pt'>%s</span> <span style='font-size: 10pt'>BTC</span> <span style='font-size: 10pt'>%s</span>" % (btc_balance, quote_text)
587         if self.state == self.SHOW_BALANCE:
588             self.setText(self.balance_text)
589
590     def set_amount_text(self, quote_text):
591         self.amount_text = "<span style='font-size: 10pt'>%s</span>" % quote_text
592         if self.state == self.SHOW_AMOUNT:
593             self.setText(self.amount_text)
594
595     def show_balance(self):
596         if self.state == self.SHOW_AMOUNT:
597             self.state = self.SHOW_BALANCE
598             self.setText(self.balance_text)
599
600     def show_amount(self):
601         if self.state == self.SHOW_BALANCE:
602             self.state = self.SHOW_AMOUNT
603             self.setText(self.amount_text)
604
605 def ok_cancel_buttons(dialog):
606     row_layout = QHBoxLayout()
607     row_layout.addStretch(1)
608     ok_button = QPushButton(_("OK"))
609     row_layout.addWidget(ok_button)
610     ok_button.clicked.connect(dialog.accept)
611     cancel_button = QPushButton(_("Cancel"))
612     row_layout.addWidget(cancel_button)
613     cancel_button.clicked.connect(dialog.reject)
614     return row_layout
615
616 class PasswordDialog(QDialog):
617
618     def __init__(self, parent):
619         super(QDialog, self).__init__(parent)
620
621         self.setModal(True)
622
623         self.password_input = QLineEdit()
624         self.password_input.setEchoMode(QLineEdit.Password)
625
626         main_layout = QVBoxLayout(self)
627         message = _('Please enter your password')
628         main_layout.addWidget(QLabel(message))
629
630         grid = QGridLayout()
631         grid.setSpacing(8)
632         grid.addWidget(QLabel(_('Password')), 1, 0)
633         grid.addWidget(self.password_input, 1, 1)
634         main_layout.addLayout(grid)
635
636         main_layout.addLayout(ok_cancel_buttons(self))
637         self.setLayout(main_layout) 
638
639     def run(self):
640         if not self.exec_():
641             return
642         return unicode(self.password_input.text())
643
644 class ReceivePopup(QDialog):
645
646     def leaveEvent(self, event):
647         self.close()
648
649     def setup(self, address):
650         label = QLabel(_("Copied your Bitcoin address to the clipboard!"))
651         address_display = QLineEdit(address)
652         address_display.setReadOnly(True)
653         resize_line_edit_width(address_display, address)
654
655         main_layout = QVBoxLayout(self)
656         main_layout.addWidget(label)
657         main_layout.addWidget(address_display)
658
659         self.setMouseTracking(True)
660         self.setWindowTitle("Electrum - " + _("Receive Bitcoin payment"))
661         self.setWindowFlags(Qt.Window|Qt.FramelessWindowHint|
662                             Qt.MSWindowsFixedSizeDialogHint)
663         self.layout().setSizeConstraint(QLayout.SetFixedSize)
664         #self.setFrameStyle(QFrame.WinPanel|QFrame.Raised)
665         #self.setAlignment(Qt.AlignCenter)
666
667     def popup(self):
668         parent = self.parent()
669         top_left_pos = parent.mapToGlobal(parent.rect().bottomLeft())
670         self.move(top_left_pos)
671         center_mouse_pos = self.mapToGlobal(self.rect().center())
672         QCursor.setPos(center_mouse_pos)
673         self.show()
674
675 class MiniActuator:
676     """Initialize the definitions relating to themes and 
677     sending/receiving bitcoins."""
678     
679     
680     def __init__(self, config, wallet):
681         """Retrieve the gui theme used in previous session."""
682         self.config = config
683         self.wallet = wallet
684         self.theme_name = self.config.get('litegui_theme','Cleanlook')
685         self.themes = load_theme_paths()
686
687     def load_theme(self):
688         """Load theme retrieved from wallet file."""
689         try:
690             theme_prefix, theme_path = self.themes[self.theme_name]
691         except KeyError:
692             util.print_error("Theme not found!", self.theme_name)
693             return
694         QDir.setCurrent(os.path.join(theme_prefix, theme_path))
695         with open(rsrc("style.css")) as style_file:
696             qApp.setStyleSheet(style_file.read())
697
698     def theme_names(self):
699         """Sort themes."""
700         return sorted(self.themes.keys())
701     
702     def selected_theme(self):
703         """Select theme."""
704         return self.theme_name
705
706     def change_theme(self, theme_name):
707         """Change theme."""
708         self.theme_name = theme_name
709         self.config.set_key('litegui_theme',theme_name)
710         self.load_theme()
711     
712     def set_configured_currency(self, set_quote_currency):
713         """Set the inital fiat currency conversion country (USD/EUR/GBP) in 
714         the GUI to what it was set to in the wallet."""
715         currency = self.config.get('currency')
716         # currency can be none when Electrum is used for the first
717         # time and no setting has been created yet.
718         if currency is not None:
719             set_quote_currency(currency)
720
721     def set_config_currency(self, conversion_currency):
722         """Change the wallet fiat currency country."""
723         self.config.set_key('conversion_currency',conversion_currency,True)
724
725     def copy_address(self, receive_popup):
726         """Copy the wallet addresses into the client."""
727         addrs = [addr for addr in self.wallet.addresses(True)
728                  if not self.wallet.is_change(addr)]
729         # Select most recent addresses from gap limit
730         addrs = addrs[-self.wallet.gap_limit:]
731         copied_address = random.choice(addrs)
732         qApp.clipboard().setText(copied_address)
733         receive_popup.setup(copied_address)
734         receive_popup.popup()
735
736     def waiting_dialog(self, f):
737         s = Timer()
738         s.start()
739         w = QDialog()
740         w.resize(200, 70)
741         w.setWindowTitle('Electrum')
742         l = QLabel('Sending transaction, please wait.')
743         vbox = QVBoxLayout()
744         vbox.addWidget(l)
745         w.setLayout(vbox)
746         w.show()
747         def ff():
748             s = f()
749             if s: l.setText(s)
750             else: w.close()
751         w.connect(s, QtCore.SIGNAL('timersignal'), ff)
752         w.exec_()
753         w.destroy()
754
755
756     def send(self, address, amount, parent_window):
757         """Send bitcoins to the target address."""
758         dest_address = self.fetch_destination(address)
759
760         if dest_address is None or not is_valid(dest_address):
761             QMessageBox.warning(parent_window, _('Error'), 
762                 _('Invalid Bitcoin Address') + ':\n' + address, _('OK'))
763             return False
764
765         convert_amount = lambda amount: \
766             int(D(unicode(amount)) * bitcoin(1))
767         amount = convert_amount(amount)
768
769         if self.wallet.use_encryption:
770             password_dialog = PasswordDialog(parent_window)
771             password = password_dialog.run()
772             if not password:
773                 return
774         else:
775             password = None
776
777         fee = 0
778         # 0.1 BTC = 10000000
779         if amount < bitcoin(1) / 10:
780             # 0.001 BTC
781             fee = bitcoin(1) / 1000
782
783         try:
784             tx = self.wallet.mktx([(dest_address, amount)], password, fee)
785         except BaseException as error:
786             QMessageBox.warning(parent_window, _('Error'), str(error), _('OK'))
787             return False
788
789         if tx.is_complete:
790             h = self.wallet.send_tx(tx)
791
792             self.waiting_dialog(lambda: False if self.wallet.tx_event.isSet() else _("Sending transaction, please wait..."))
793               
794             status, message = self.wallet.receive_tx(h)
795
796             if not status:
797                 import tempfile
798                 dumpf = tempfile.NamedTemporaryFile(delete=False)
799                 dumpf.write(tx)
800                 dumpf.close()
801                 print "Dumped error tx to", dumpf.name
802                 QMessageBox.warning(parent_window, _('Error'), message, _('OK'))
803                 return False
804           
805             TransactionWindow(message, self)
806         else:
807             filename = 'unsigned_tx_%s' % (time.mktime(time.gmtime()))
808             try:
809                 fileName = QFileDialog.getSaveFileName(QWidget(), _("Select a transaction filename"), os.path.expanduser('~/%s' % (filename)))
810                 with open(fileName,'w') as f:
811                     f.write(json.dumps(tx.as_dict(),indent=4) + '\n')
812                 QMessageBox.information(QWidget(), _('Unsigned transaction created'), _("Unsigned transaction was saved to file:") + " " +fileName, _('OK'))
813             except BaseException as e:
814                 QMessageBox.warning(QWidget(), _('Error'), _('Could not write transaction to file: %s' % e), _('OK'))
815         return True
816
817     def fetch_destination(self, address):
818         recipient = unicode(address).strip()
819
820         # alias
821         match1 = re.match("^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$",
822                           recipient)
823
824         # label or alias, with address in brackets
825         match2 = re.match("(.*?)\s*\<([1-9A-HJ-NP-Za-km-z]{26,})\>",
826                           recipient)
827         
828         if match1:
829             dest_address = \
830                 self.wallet.get_alias(recipient, True, 
831                                       self.show_message, self.question)
832             return dest_address
833         elif match2:
834             return match2.group(2)
835         else:
836             return recipient
837
838
839     def copy_master_public_key(self):
840         master_pubkey = self.wallet.get_master_public_key()
841         qApp.clipboard().setText(master_pubkey)
842         QMessageBox.information(None, _("Copy successful"), _("Your master public key has been copied to your clipboard."))
843         
844
845     def acceptbit(self, currency):
846         master_pubkey = self.wallet.master_public_key
847         url = "http://acceptbit.com/mpk/%s/%s" % (master_pubkey, currency)
848         webbrowser.open(url)
849
850     def show_seed_dialog(self):
851         ElectrumWindow.show_seed_dialog(self.wallet)
852
853 class MiniDriver(QObject):
854
855     INITIALIZING = 0
856     CONNECTING = 1
857     SYNCHRONIZING = 2
858     READY = 3
859
860     def __init__(self, wallet, window):
861         super(QObject, self).__init__()
862
863         self.wallet = wallet
864         self.window = window
865
866         self.wallet.network.register_callback('updated',self.update_callback)
867         self.wallet.network.register_callback('connected', self.update_callback)
868         self.wallet.network.register_callback('disconnected', self.update_callback)
869
870         self.state = None
871
872         self.initializing()
873         self.connect(self, SIGNAL("updatesignal()"), self.update)
874         self.update_callback()
875
876     # This is a hack to workaround that Qt does not like changing the
877     # window properties from this other thread before the runloop has
878     # been called from.
879     def update_callback(self):
880         self.emit(SIGNAL("updatesignal()"))
881
882     def update(self):
883         if not self.wallet.interface:
884             self.initializing()
885         elif not self.wallet.interface.is_connected:
886             self.connecting()
887         elif not self.wallet.up_to_date:
888             self.synchronizing()
889         else:
890             self.ready()
891
892         if self.wallet.up_to_date:
893             self.update_balance()
894             self.update_completions()
895             self.update_history()
896
897     def initializing(self):
898         if self.state == self.INITIALIZING:
899             return
900         self.state = self.INITIALIZING
901         self.window.deactivate()
902
903     def connecting(self):
904         if self.state == self.CONNECTING:
905             return
906         self.state = self.CONNECTING
907         self.window.deactivate()
908
909     def synchronizing(self):
910         if self.state == self.SYNCHRONIZING:
911             return
912         self.state = self.SYNCHRONIZING
913         self.window.deactivate()
914
915     def ready(self):
916         if self.state == self.READY:
917             return
918         self.state = self.READY
919         self.window.activate()
920
921     def update_balance(self):
922         conf_balance, unconf_balance = self.wallet.get_balance()
923         balance = D(conf_balance + unconf_balance)
924         self.window.set_balances(balance)
925
926     def update_completions(self):
927         completions = []
928         for addr, label in self.wallet.labels.items():
929             if addr in self.wallet.addressbook:
930                 completions.append("%s <%s>" % (label, addr))
931         self.window.update_completions(completions)
932
933     def update_history(self):
934         tx_history = self.wallet.get_tx_history()
935         self.window.update_history(tx_history)
936
937
938 if __name__ == "__main__":
939     app = QApplication(sys.argv)
940     with open(rsrc("style.css")) as style_file:
941         app.setStyleSheet(style_file.read())
942     mini = MiniWindow()
943     sys.exit(app.exec_())
944