generalize plugins to all guis
This commit is contained in:
@@ -29,7 +29,8 @@ class Plugin(BasePlugin):
|
||||
self.authorities = self.config.get('authorities', {}) # trusted addresses
|
||||
self.receipts = self.config.get('receipts',{}) # signed URIs
|
||||
|
||||
|
||||
def is_available(self):
|
||||
return False
|
||||
|
||||
def timer_actions(self):
|
||||
if self.gui.payto_e.hasFocus():
|
||||
|
||||
135
plugins/exchange_rate.py
Normal file
135
plugins/exchange_rate.py
Normal file
@@ -0,0 +1,135 @@
|
||||
from PyQt4.QtGui import *
|
||||
from PyQt4.QtCore import *
|
||||
|
||||
import decimal
|
||||
import httplib
|
||||
import json
|
||||
import threading
|
||||
import re
|
||||
from decimal import Decimal
|
||||
from electrum.plugins import BasePlugin
|
||||
from electrum.i18n import _
|
||||
from electrum_gui.gui_classic.qt_util import *
|
||||
|
||||
|
||||
class Exchanger(threading.Thread):
|
||||
|
||||
def __init__(self, parent):
|
||||
threading.Thread.__init__(self)
|
||||
self.daemon = True
|
||||
self.parent = parent
|
||||
self.quote_currencies = None
|
||||
self.lock = threading.Lock()
|
||||
|
||||
def exchange(self, btc_amount, quote_currency):
|
||||
with self.lock:
|
||||
if self.quote_currencies is None:
|
||||
return None
|
||||
quote_currencies = self.quote_currencies.copy()
|
||||
if quote_currency not in quote_currencies:
|
||||
return None
|
||||
return btc_amount * quote_currencies[quote_currency]
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
connection = httplib.HTTPConnection('blockchain.info')
|
||||
connection.request("GET", "/ticker")
|
||||
except:
|
||||
return
|
||||
response = connection.getresponse()
|
||||
if response.reason == httplib.responses[httplib.NOT_FOUND]:
|
||||
return
|
||||
try:
|
||||
response = json.loads(response.read())
|
||||
except:
|
||||
return
|
||||
quote_currencies = {}
|
||||
try:
|
||||
for r in response:
|
||||
quote_currencies[r] = self._lookup_rate(response, r)
|
||||
with self.lock:
|
||||
self.quote_currencies = quote_currencies
|
||||
self.parent.emit(SIGNAL("refresh_balance()"))
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
print self.quote_currencies
|
||||
|
||||
def get_currencies(self):
|
||||
return [] if self.quote_currencies == None else sorted(self.quote_currencies.keys())
|
||||
|
||||
def _lookup_rate(self, response, quote_id):
|
||||
return decimal.Decimal(str(response[str(quote_id)]["15m"]))
|
||||
|
||||
|
||||
class Plugin(BasePlugin):
|
||||
|
||||
def fullname(self):
|
||||
return "Exchange rates"
|
||||
|
||||
def description(self):
|
||||
return """exchange rates"""
|
||||
|
||||
def init(self):
|
||||
self.win = self.gui.main_window
|
||||
self.exchanger = Exchanger(self.win)
|
||||
self.win.connect(self.win, SIGNAL("refresh_balance()"), self.win.update_wallet)
|
||||
# Do price discovery
|
||||
self.exchanger.start()
|
||||
self.gui.exchanger = self.exchanger
|
||||
|
||||
def set_status_text(self, text):
|
||||
m = re.match( _( "Balance" ) + ": (\d.+) " + self.win.base_unit(), str(text))
|
||||
if m:
|
||||
amount = Decimal(m.group(1))
|
||||
text += self.create_quote_text(amount)
|
||||
self.win.balance_label.setText(text)
|
||||
|
||||
def create_quote_text(self, btc_balance):
|
||||
quote_currency = self.config.get("currency", "None")
|
||||
quote_balance = self.exchanger.exchange(btc_balance, quote_currency)
|
||||
if quote_balance is None:
|
||||
quote_text = ""
|
||||
else:
|
||||
quote_text = " (%.2f %s)" % (quote_balance, quote_currency)
|
||||
return quote_text
|
||||
|
||||
|
||||
def requires_settings(self):
|
||||
return True
|
||||
|
||||
|
||||
def settings_dialog(self):
|
||||
d = QDialog(self.win)
|
||||
|
||||
vbox = QVBoxLayout(d)
|
||||
|
||||
grid = QGridLayout()
|
||||
vbox.addLayout(grid)
|
||||
|
||||
currencies = self.exchanger.get_currencies()
|
||||
currencies.insert(0, "None")
|
||||
|
||||
cur_label=QLabel(_('Currency') + ':')
|
||||
grid.addWidget(cur_label , 2, 0)
|
||||
cur_combo = QComboBox()
|
||||
cur_combo.addItems(currencies)
|
||||
try:
|
||||
index = currencies.index(self.config.get('currency', "None"))
|
||||
except:
|
||||
index = 0
|
||||
cur_combo.setCurrentIndex(index)
|
||||
grid.addWidget(cur_combo, 2, 1)
|
||||
grid.addWidget(HelpButton(_('Select which currency is used for quotes.') + ' '), 2, 2)
|
||||
|
||||
vbox.addLayout(ok_cancel_buttons(d))
|
||||
|
||||
if d.exec_():
|
||||
|
||||
cur_request = str(currencies[cur_combo.currentIndex()])
|
||||
if cur_request != self.config.get('currency', "None"):
|
||||
self.config.set_key('currency', cur_request, True)
|
||||
self.win.update_wallet()
|
||||
|
||||
|
||||
|
||||
@@ -47,7 +47,8 @@ class Plugin(BasePlugin):
|
||||
|
||||
def init(self):
|
||||
self.target_host = 'labelectrum.herokuapp.com'
|
||||
self.wallet = self.gui.wallet
|
||||
self.window = self.gui.main_window
|
||||
self.wallet = self.window.wallet
|
||||
self.labels = self.wallet.labels
|
||||
self.transactions = self.wallet.transactions
|
||||
mpk = self.wallet.master_public_keys["m/0'/"][1]
|
||||
@@ -103,7 +104,7 @@ class Plugin(BasePlugin):
|
||||
self.download.setEnabled(False)
|
||||
self.accept.setEnabled(False)
|
||||
|
||||
d = QDialog(self.gui)
|
||||
d = QDialog(self.window)
|
||||
layout = QGridLayout(d)
|
||||
layout.addWidget(QLabel("API Key: "),0,0)
|
||||
|
||||
@@ -161,10 +162,10 @@ class Plugin(BasePlugin):
|
||||
def full_pull(self, force = False):
|
||||
if self.do_full_pull(force) and force:
|
||||
QMessageBox.information(None, _("Labels synchronized"), _("Your labels have been synchronized."))
|
||||
self.gui.update_history_tab()
|
||||
self.gui.update_completions()
|
||||
self.gui.update_receive_tab()
|
||||
self.gui.update_contacts_tab()
|
||||
self.window.update_history_tab()
|
||||
self.window.update_completions()
|
||||
self.window.update_receive_tab()
|
||||
self.window.update_contacts_tab()
|
||||
|
||||
def do_full_push(self):
|
||||
try:
|
||||
|
||||
@@ -101,19 +101,21 @@ class Plugin(BasePlugin):
|
||||
return _('Show QR code window and amounts requested for each address. Add menu item to request amount.')
|
||||
|
||||
def init(self):
|
||||
self.window = self.gui.main_window
|
||||
|
||||
self.qr_window = None
|
||||
self.merchant_name = self.config.get('merchant_name', 'Invoice')
|
||||
|
||||
self.gui.expert_mode = True
|
||||
self.gui.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Request')])
|
||||
self.window.expert_mode = True
|
||||
self.window.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Request')])
|
||||
self.requested_amounts = {}
|
||||
self.toggle_QR_window(True)
|
||||
|
||||
def load_wallet(self):
|
||||
self.requested_amounts = self.gui.wallet.storage.get('requested_amounts',{})
|
||||
self.requested_amounts = self.window.wallet.storage.get('requested_amounts',{})
|
||||
|
||||
def close(self):
|
||||
self.gui.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Tx')])
|
||||
self.window.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Tx')])
|
||||
self.toggle_QR_window(False)
|
||||
|
||||
|
||||
@@ -133,10 +135,10 @@ class Plugin(BasePlugin):
|
||||
self.qr_window = QR_Window(self.gui.exchanger)
|
||||
self.qr_window.setVisible(True)
|
||||
self.qr_window_geometry = self.qr_window.geometry()
|
||||
item = self.gui.receive_list.currentItem()
|
||||
item = self.window.receive_list.currentItem()
|
||||
if item:
|
||||
address = str(item.text(1))
|
||||
label = self.gui.wallet.labels.get(address)
|
||||
label = self.window.wallet.labels.get(address)
|
||||
amount, currency = self.requested_amounts.get(address, (None, None))
|
||||
self.qr_window.set_content( address, label, amount, currency )
|
||||
|
||||
@@ -166,7 +168,7 @@ class Plugin(BasePlugin):
|
||||
def current_item_changed(self, a):
|
||||
if a is not None and self.qr_window and self.qr_window.isVisible():
|
||||
address = str(a.text(0))
|
||||
label = self.gui.wallet.labels.get(address)
|
||||
label = self.window.wallet.labels.get(address)
|
||||
try:
|
||||
amount, currency = self.requested_amounts.get(address, (None, None))
|
||||
except:
|
||||
@@ -181,7 +183,7 @@ class Plugin(BasePlugin):
|
||||
address = str( item.text(0) )
|
||||
text = str( item.text(column) )
|
||||
try:
|
||||
seq = self.gui.wallet.get_address_index(address)
|
||||
seq = self.window.wallet.get_address_index(address)
|
||||
index = seq[1][1]
|
||||
except:
|
||||
print "cannot get index"
|
||||
@@ -199,12 +201,12 @@ class Plugin(BasePlugin):
|
||||
currency = currency.upper()
|
||||
|
||||
self.requested_amounts[address] = (amount, currency)
|
||||
self.gui.wallet.storage.put('requested_amounts', self.requested_amounts, True)
|
||||
self.window.wallet.storage.put('requested_amounts', self.requested_amounts, True)
|
||||
|
||||
label = self.gui.wallet.labels.get(address)
|
||||
label = self.window.wallet.labels.get(address)
|
||||
if label is None:
|
||||
label = self.merchant_name + ' - %04d'%(index+1)
|
||||
self.gui.wallet.labels[address] = label
|
||||
self.window.wallet.labels[address] = label
|
||||
|
||||
if self.qr_window:
|
||||
self.qr_window.set_content( address, label, amount, currency )
|
||||
@@ -214,13 +216,13 @@ class Plugin(BasePlugin):
|
||||
if address in self.requested_amounts:
|
||||
self.requested_amounts.pop(address)
|
||||
|
||||
self.gui.update_receive_item(self.gui.receive_list.currentItem())
|
||||
self.window.update_receive_item(self.window.receive_list.currentItem())
|
||||
|
||||
|
||||
|
||||
|
||||
def edit_amount(self):
|
||||
l = self.gui.receive_list
|
||||
l = self.window.receive_list
|
||||
item = l.currentItem()
|
||||
item.setFlags(Qt.ItemIsEditable|Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
|
||||
l.editItem( item, column_index )
|
||||
|
||||
Reference in New Issue
Block a user