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.set_currencies(quote_currencies)
57 def get_currencies(self):
58 return [] if self.quote_currencies == None else sorted(self.quote_currencies.keys())
60 def _lookup_rate(self, response, quote_id):
61 return decimal.Decimal(str(response[str(quote_id)]["15m"]))
64 class Plugin(BasePlugin):
67 return "Exchange rates"
69 def description(self):
70 return """exchange rates, retrieved from blockchain.info"""
73 def __init__(self,a,b):
74 BasePlugin.__init__(self,a,b)
75 self.currencies = [self.config.get('currency', "EUR")]
78 self.win = self.gui.main_window
79 self.win.connect(self.win, SIGNAL("refresh_currencies()"), self.win.update_status)
81 self.exchanger = Exchanger(self)
82 self.exchanger.start()
84 def set_currencies(self, quote_currencies):
85 self.currencies = sorted(quote_currencies.keys())
86 self.win.emit(SIGNAL("refresh_currencies()"))
87 self.win.emit(SIGNAL("refresh_currencies_combo()"))
89 def set_quote_text(self, btc_balance, r):
90 r[0] = self.create_quote_text(Decimal(btc_balance) / 100000000)
92 def create_quote_text(self, btc_balance):
93 quote_currency = self.config.get("currency", "EUR")
94 quote_balance = self.exchanger.exchange(btc_balance, quote_currency)
95 if quote_balance is None:
98 quote_text = "%.2f %s" % (quote_balance, quote_currency)
102 def requires_settings(self):
107 out = BasePlugin.toggle(self)
108 self.win.update_status()
112 def settings_widget(self, window):
116 cur_request = str(self.currencies[x])
117 if cur_request != self.config.get('currency', "EUR"):
118 self.config.set_key('currency', cur_request, True)
119 self.win.update_status()
121 def set_currencies(combo):
126 combo.addItems(self.currencies)
128 index = self.currencies.index(self.config.get('currency', "EUR"))
131 combo.setCurrentIndex(index)
133 set_currencies(combo)
134 combo.currentIndexChanged.connect(on_change)
135 combo.connect(window, SIGNAL('refresh_currencies_combo()'), lambda: set_currencies(combo))