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()
23 self.is_running = False
25 def exchange(self, btc_amount, quote_currency):
27 if self.quote_currencies is None:
29 quote_currencies = self.quote_currencies.copy()
30 if quote_currency not in quote_currencies:
32 return btc_amount * quote_currencies[quote_currency]
35 self.is_running = False
38 self.is_running = True
39 while self.is_running:
45 connection = httplib.HTTPConnection('blockchain.info')
46 connection.request("GET", "/ticker")
49 response = connection.getresponse()
50 if response.reason == httplib.responses[httplib.NOT_FOUND]:
53 response = json.loads(response.read())
59 quote_currencies[r] = self._lookup_rate(response, r)
61 self.quote_currencies = quote_currencies
64 self.parent.set_currencies(quote_currencies)
65 # print "updating exchange rate", self.quote_currencies["USD"]
68 def get_currencies(self):
69 return [] if self.quote_currencies == None else sorted(self.quote_currencies.keys())
71 def _lookup_rate(self, response, quote_id):
72 return decimal.Decimal(str(response[str(quote_id)]["15m"]))
75 class Plugin(BasePlugin):
78 return "Exchange rates"
80 def description(self):
81 return """exchange rates, retrieved from blockchain.info"""
84 def __init__(self,a,b):
85 BasePlugin.__init__(self,a,b)
86 self.currencies = [self.config.get('currency', "EUR")]
89 self.win = self.gui.main_window
90 self.win.connect(self.win, SIGNAL("refresh_currencies()"), self.win.update_status)
92 self.exchanger = Exchanger(self)
93 self.exchanger.start()
94 self.gui.exchanger = self.exchanger #
96 def set_currencies(self, quote_currencies):
97 self.currencies = sorted(quote_currencies.keys())
98 self.win.emit(SIGNAL("refresh_currencies()"))
99 self.win.emit(SIGNAL("refresh_currencies_combo()"))
101 def set_quote_text(self, btc_balance, r):
102 r[0] = self.create_quote_text(Decimal(btc_balance) / 100000000)
104 def create_quote_text(self, btc_balance):
105 quote_currency = self.config.get("currency", "EUR")
106 quote_balance = self.exchanger.exchange(btc_balance, quote_currency)
107 if quote_balance is None:
110 quote_text = "%.2f %s" % (quote_balance, quote_currency)
114 def requires_settings(self):
119 out = BasePlugin.toggle(self)
120 self.win.update_status()
125 self.exchanger.stop()
128 def settings_widget(self, window):
132 cur_request = str(self.currencies[x])
133 if cur_request != self.config.get('currency', "EUR"):
134 self.config.set_key('currency', cur_request, True)
135 self.win.update_status()
137 def set_currencies(combo):
142 combo.addItems(self.currencies)
144 index = self.currencies.index(self.config.get('currency', "EUR"))
147 combo.setCurrentIndex(index)
149 set_currencies(combo)
150 combo.currentIndexChanged.connect(on_change)
151 combo.connect(window, SIGNAL('refresh_currencies_combo()'), lambda: set_currencies(combo))