1 from PyQt4.QtGui import *
2 from PyQt4.QtCore import *
9 from decimal import Decimal
10 from electrum.plugins import BasePlugin
11 from electrum.i18n import _
12 from electrum_gui.qt.util import *
15 class Exchanger(threading.Thread):
17 def __init__(self, parent):
18 threading.Thread.__init__(self)
21 self.quote_currencies = None
22 self.lock = threading.Lock()
24 def exchange(self, btc_amount, quote_currency):
26 if self.quote_currencies is None:
28 quote_currencies = self.quote_currencies.copy()
29 if quote_currency not in quote_currencies:
31 return btc_amount * quote_currencies[quote_currency]
35 connection = httplib.HTTPConnection('blockchain.info')
36 connection.request("GET", "/ticker")
39 response = connection.getresponse()
40 if response.reason == httplib.responses[httplib.NOT_FOUND]:
43 response = json.loads(response.read())
49 quote_currencies[r] = self._lookup_rate(response, r)
51 self.quote_currencies = quote_currencies
52 self.parent.emit(SIGNAL("refresh_balance()"))
56 def get_currencies(self):
57 return [] if self.quote_currencies == None else sorted(self.quote_currencies.keys())
59 def _lookup_rate(self, response, quote_id):
60 return decimal.Decimal(str(response[str(quote_id)]["15m"]))
63 class Plugin(BasePlugin):
66 return "Exchange rates"
68 def description(self):
69 return """exchange rates, retrieved from blockchain.info"""
72 self.win = self.gui.main_window
73 self.exchanger = Exchanger(self.win)
74 self.win.connect(self.win, SIGNAL("refresh_balance()"), self.win.update_wallet)
76 self.exchanger.start()
77 self.gui.exchanger = self.exchanger
80 def set_quote_text(self, btc_balance, r):
81 r[0] = self.create_quote_text(Decimal(btc_balance) / 100000000)
83 def create_quote_text(self, btc_balance):
84 quote_currency = self.config.get("currency", "None")
85 quote_balance = self.exchanger.exchange(btc_balance, quote_currency)
86 if quote_balance is None:
89 quote_text = "%.2f %s" % (quote_balance, quote_currency)
93 def requires_settings(self):
97 def settings_dialog(self):
100 vbox = QVBoxLayout(d)
105 currencies = self.exchanger.get_currencies()
106 currencies.insert(0, "None")
108 cur_label=QLabel(_('Currency') + ':')
109 grid.addWidget(cur_label , 2, 0)
110 cur_combo = QComboBox()
111 cur_combo.addItems(currencies)
113 index = currencies.index(self.config.get('currency', "None"))
116 cur_combo.setCurrentIndex(index)
117 grid.addWidget(cur_combo, 2, 1)
118 grid.addWidget(HelpButton(_('Select which currency is used for quotes.') + ' '), 2, 2)
120 vbox.addLayout(ok_cancel_buttons(d))
124 cur_request = str(currencies[cur_combo.currentIndex()])
125 if cur_request != self.config.get('currency', "None"):
126 self.config.set_key('currency', cur_request, True)
127 self.win.update_wallet()