2013-09-11 04:43:37 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Electrum - lightweight Bitcoin client
|
|
|
|
# Copyright (C) 2012 thomasv@gitorious
|
|
|
|
#
|
2016-02-23 02:36:42 -08:00
|
|
|
# Permission is hereby granted, free of charge, to any person
|
|
|
|
# obtaining a copy of this software and associated documentation files
|
|
|
|
# (the "Software"), to deal in the Software without restriction,
|
|
|
|
# including without limitation the rights to use, copy, modify, merge,
|
|
|
|
# publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
# and to permit persons to whom the Software is furnished to do so,
|
|
|
|
# subject to the following conditions:
|
2013-09-11 04:43:37 -07:00
|
|
|
#
|
2016-02-23 02:36:42 -08:00
|
|
|
# The above copyright notice and this permission notice shall be
|
|
|
|
# included in all copies or substantial portions of the Software.
|
2013-09-11 04:43:37 -07:00
|
|
|
#
|
2016-02-23 02:36:42 -08:00
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
2013-09-11 04:43:37 -07:00
|
|
|
|
2014-09-08 02:02:55 -07:00
|
|
|
import sys
|
2015-09-03 01:23:43 -07:00
|
|
|
import os
|
2014-08-06 04:15:53 -07:00
|
|
|
import signal
|
2013-09-11 04:43:37 -07:00
|
|
|
|
|
|
|
try:
|
|
|
|
import PyQt4
|
2013-11-10 12:30:57 -08:00
|
|
|
except Exception:
|
2013-09-11 04:43:37 -07:00
|
|
|
sys.exit("Error: Could not import PyQt4 on Linux systems, you may try 'sudo apt-get install python-qt4'")
|
|
|
|
|
|
|
|
from PyQt4.QtGui import *
|
|
|
|
from PyQt4.QtCore import *
|
|
|
|
import PyQt4.QtCore as QtCore
|
|
|
|
|
2014-09-08 02:02:55 -07:00
|
|
|
from electrum.i18n import _, set_language
|
2015-09-01 20:06:11 -07:00
|
|
|
from electrum.plugins import run_hook
|
2015-09-02 03:05:33 -07:00
|
|
|
from electrum import SimpleConfig, Wallet, WalletStorage
|
2015-09-03 07:52:40 -07:00
|
|
|
from electrum.paymentrequest import InvoiceStore
|
|
|
|
from electrum.contacts import Contacts
|
2015-11-13 06:11:43 -08:00
|
|
|
from electrum.synchronizer import Synchronizer
|
|
|
|
from electrum.verifier import SPV
|
|
|
|
from electrum.util import DebugMem
|
|
|
|
from electrum.wallet import Abstract_Wallet
|
2015-09-26 01:19:42 -07:00
|
|
|
from installwizard import InstallWizard
|
|
|
|
|
2013-09-11 04:43:37 -07:00
|
|
|
|
|
|
|
try:
|
|
|
|
import icons_rc
|
2013-11-10 12:30:57 -08:00
|
|
|
except Exception:
|
2013-09-30 01:30:43 -07:00
|
|
|
sys.exit("Error: Could not import icons_rc.py, please generate it with: 'pyrcc4 icons.qrc -o gui/qt/icons_rc.py'")
|
2013-09-11 04:43:37 -07:00
|
|
|
|
2015-09-01 20:06:11 -07:00
|
|
|
from util import * # * needed for plugins
|
2013-09-11 04:43:37 -07:00
|
|
|
from main_window import ElectrumWindow
|
|
|
|
|
|
|
|
|
|
|
|
class OpenFileEventFilter(QObject):
|
|
|
|
def __init__(self, windows):
|
|
|
|
self.windows = windows
|
|
|
|
super(OpenFileEventFilter, self).__init__()
|
|
|
|
|
|
|
|
def eventFilter(self, obj, event):
|
|
|
|
if event.type() == QtCore.QEvent.FileOpen:
|
|
|
|
if len(self.windows) >= 1:
|
2015-07-21 02:40:55 -07:00
|
|
|
self.windows[0].pay_to_URI(event.url().toEncoded())
|
2013-09-11 04:43:37 -07:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2015-08-26 08:44:19 -07:00
|
|
|
|
2016-01-09 19:56:12 -08:00
|
|
|
class ElectrumGui:
|
2013-09-11 04:43:37 -07:00
|
|
|
|
2016-01-29 05:25:59 -08:00
|
|
|
def __init__(self, config, daemon, plugins):
|
2014-09-08 02:02:55 -07:00
|
|
|
set_language(config.get('language'))
|
2015-11-13 06:11:43 -08:00
|
|
|
# Uncomment this call to verify objects are being properly
|
|
|
|
# GC-ed when windows are closed
|
|
|
|
#network.add_jobs([DebugMem([Abstract_Wallet, SPV, Synchronizer,
|
|
|
|
# ElectrumWindow], interval=5)])
|
2013-09-11 04:43:37 -07:00
|
|
|
self.config = config
|
2016-01-05 04:51:03 -08:00
|
|
|
self.daemon = daemon
|
2015-09-02 20:02:03 -07:00
|
|
|
self.plugins = plugins
|
2015-09-01 00:35:32 -07:00
|
|
|
self.windows = []
|
2013-09-11 04:43:37 -07:00
|
|
|
self.efilter = OpenFileEventFilter(self.windows)
|
2015-08-26 08:44:19 -07:00
|
|
|
self.app = QApplication(sys.argv)
|
2013-09-11 04:43:37 -07:00
|
|
|
self.app.installEventFilter(self.efilter)
|
2015-08-26 08:44:19 -07:00
|
|
|
self.timer = Timer()
|
2015-09-03 07:52:40 -07:00
|
|
|
# shared objects
|
|
|
|
self.invoices = InvoiceStore(self.config)
|
|
|
|
self.contacts = Contacts(self.config)
|
2015-09-01 19:45:05 -07:00
|
|
|
# init tray
|
|
|
|
self.dark_icon = self.config.get("dark_icon", False)
|
|
|
|
self.tray = QSystemTrayIcon(self.tray_icon(), None)
|
|
|
|
self.tray.setToolTip('Electrum')
|
|
|
|
self.tray.activated.connect(self.tray_activated)
|
|
|
|
self.build_tray_menu()
|
|
|
|
self.tray.show()
|
2015-08-26 08:44:19 -07:00
|
|
|
self.app.connect(self.app, QtCore.SIGNAL('new_window'), self.start_new_window)
|
2016-01-28 05:57:59 -08:00
|
|
|
run_hook('init_qt', self)
|
2015-08-26 08:44:19 -07:00
|
|
|
|
2014-04-25 02:22:16 -07:00
|
|
|
def build_tray_menu(self):
|
2015-09-01 19:45:05 -07:00
|
|
|
# Avoid immediate GC of old menu when window closed via its action
|
|
|
|
self.old_menu = self.tray.contextMenu()
|
2014-04-25 02:22:16 -07:00
|
|
|
m = QMenu()
|
2015-09-01 19:45:05 -07:00
|
|
|
for window in self.windows:
|
|
|
|
submenu = m.addMenu(window.wallet.basename())
|
|
|
|
submenu.addAction(_("Show/Hide"), window.show_or_hide)
|
|
|
|
submenu.addAction(_("Close"), window.close)
|
2014-04-25 02:22:16 -07:00
|
|
|
m.addAction(_("Dark/Light"), self.toggle_tray_icon)
|
|
|
|
m.addSeparator()
|
|
|
|
m.addAction(_("Exit Electrum"), self.close)
|
|
|
|
self.tray.setContextMenu(m)
|
|
|
|
|
2015-09-01 19:45:05 -07:00
|
|
|
def tray_icon(self):
|
|
|
|
if self.dark_icon:
|
|
|
|
return QIcon(':icons/electrum_dark_icon.png')
|
|
|
|
else:
|
|
|
|
return QIcon(':icons/electrum_light_icon.png')
|
|
|
|
|
2014-04-25 02:22:16 -07:00
|
|
|
def toggle_tray_icon(self):
|
|
|
|
self.dark_icon = not self.dark_icon
|
|
|
|
self.config.set_key("dark_icon", self.dark_icon, True)
|
2015-09-01 19:45:05 -07:00
|
|
|
self.tray.setIcon(self.tray_icon())
|
2014-04-25 02:22:16 -07:00
|
|
|
|
|
|
|
def tray_activated(self, reason):
|
|
|
|
if reason == QSystemTrayIcon.DoubleClick:
|
2015-09-01 19:45:05 -07:00
|
|
|
if all([w.is_hidden() for w in self.windows]):
|
|
|
|
for w in self.windows:
|
|
|
|
w.bring_to_top()
|
2014-04-25 02:22:16 -07:00
|
|
|
else:
|
2015-09-01 19:45:05 -07:00
|
|
|
for w in self.windows:
|
|
|
|
w.hide()
|
2014-04-25 02:22:16 -07:00
|
|
|
|
|
|
|
def close(self):
|
2015-09-01 19:45:05 -07:00
|
|
|
for window in self.windows:
|
|
|
|
window.close()
|
2014-04-25 02:22:16 -07:00
|
|
|
|
2015-09-03 02:27:33 -07:00
|
|
|
def new_window(self, path, uri=None):
|
2015-09-02 04:34:40 -07:00
|
|
|
# Use a signal as can be called from daemon thread
|
2015-09-03 01:23:43 -07:00
|
|
|
self.app.emit(SIGNAL('new_window'), path, uri)
|
2015-09-01 03:16:07 -07:00
|
|
|
|
2016-01-05 04:51:03 -08:00
|
|
|
def create_window_for_wallet(self, wallet):
|
2015-12-30 19:02:16 -08:00
|
|
|
w = ElectrumWindow(self, wallet)
|
|
|
|
self.windows.append(w)
|
|
|
|
self.build_tray_menu()
|
2015-12-30 18:36:33 -08:00
|
|
|
# FIXME: Remove in favour of the load_wallet hook
|
2015-12-30 19:02:16 -08:00
|
|
|
run_hook('on_new_window', w)
|
|
|
|
return w
|
|
|
|
|
2016-01-13 02:27:17 -08:00
|
|
|
def get_wizard(self):
|
|
|
|
return InstallWizard(self.config, self.app, self.plugins)
|
|
|
|
|
2015-09-03 01:23:43 -07:00
|
|
|
def start_new_window(self, path, uri):
|
2015-12-30 18:36:33 -08:00
|
|
|
'''Raises the window for the wallet if it is open. Otherwise
|
|
|
|
opens the wallet and creates a new window for it.'''
|
2015-09-01 00:35:32 -07:00
|
|
|
for w in self.windows:
|
2015-09-02 01:06:45 -07:00
|
|
|
if w.wallet.storage.path == path:
|
2015-09-01 19:45:05 -07:00
|
|
|
w.bring_to_top()
|
2015-09-01 00:35:32 -07:00
|
|
|
break
|
|
|
|
else:
|
2016-01-13 02:27:17 -08:00
|
|
|
wallet = self.daemon.load_wallet(path, self.get_wizard)
|
2016-01-05 04:51:03 -08:00
|
|
|
if not wallet:
|
2015-12-30 18:36:33 -08:00
|
|
|
return
|
2016-01-05 04:51:03 -08:00
|
|
|
w = self.create_window_for_wallet(wallet)
|
2014-04-25 02:22:16 -07:00
|
|
|
|
2015-12-30 18:36:33 -08:00
|
|
|
if uri:
|
2015-09-03 02:07:15 -07:00
|
|
|
w.pay_to_URI(uri)
|
2015-09-03 01:23:43 -07:00
|
|
|
|
2015-08-26 08:44:19 -07:00
|
|
|
return w
|
2014-04-25 02:22:16 -07:00
|
|
|
|
2015-09-01 19:45:05 -07:00
|
|
|
def close_window(self, window):
|
2016-01-12 03:19:21 -08:00
|
|
|
self.windows.remove(window)
|
2015-09-01 19:45:05 -07:00
|
|
|
self.build_tray_menu()
|
2015-12-05 07:53:56 -08:00
|
|
|
# save wallet path of last open window
|
2016-03-08 02:10:04 -08:00
|
|
|
if not self.windows:
|
|
|
|
self.config.save_last_wallet(window.wallet)
|
2016-01-12 03:19:21 -08:00
|
|
|
run_hook('on_close_window', window)
|
2014-04-25 02:22:16 -07:00
|
|
|
|
2015-08-26 08:44:19 -07:00
|
|
|
def main(self):
|
|
|
|
self.timer.start()
|
2016-03-08 02:10:04 -08:00
|
|
|
self.config.open_last_wallet()
|
2016-05-30 01:53:52 -07:00
|
|
|
path = self.config.get_wallet_path()
|
|
|
|
if not self.start_new_window(path, self.config.get('url')):
|
2015-09-02 03:05:33 -07:00
|
|
|
return
|
2015-08-22 02:23:54 -07:00
|
|
|
|
2014-08-06 04:15:53 -07:00
|
|
|
signal.signal(signal.SIGINT, lambda *args: self.app.quit())
|
2015-08-26 08:44:19 -07:00
|
|
|
|
|
|
|
# main loop
|
2013-09-11 04:43:37 -07:00
|
|
|
self.app.exec_()
|
|
|
|
|
2015-12-19 23:15:33 -08:00
|
|
|
# Shut down the timer cleanly
|
|
|
|
self.timer.stop()
|
|
|
|
|
2015-08-26 08:44:19 -07:00
|
|
|
# clipboard persistence. see http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg17328.html
|
2014-05-08 10:12:13 -07:00
|
|
|
event = QtCore.QEvent(QtCore.QEvent.Clipboard)
|
|
|
|
self.app.sendEvent(self.app.clipboard(), event)
|
|
|
|
|
2015-09-04 02:29:52 -07:00
|
|
|
self.tray.hide()
|