fix conversion in exchange rate plugin
[electrum-nvc.git] / plugins / exchange_rate.py
1 from PyQt4.QtGui import *
2 from PyQt4.QtCore import *
3
4 import decimal
5 import httplib
6 import json
7 import threading
8 import re
9 from decimal import Decimal
10 from electrum.plugins import BasePlugin
11 from electrum.i18n import _
12 from electrum_gui.qt.util import *
13
14
15 class Exchanger(threading.Thread):
16
17     def __init__(self, parent):
18         threading.Thread.__init__(self)
19         self.daemon = True
20         self.parent = parent
21         self.quote_currencies = None
22         self.lock = threading.Lock()
23
24     def exchange(self, btc_amount, quote_currency):
25         with self.lock:
26             if self.quote_currencies is None:
27                 return None
28             quote_currencies = self.quote_currencies.copy()
29         if quote_currency not in quote_currencies:
30             return None
31         return btc_amount * quote_currencies[quote_currency]
32
33     def run(self):
34         try:
35             connection = httplib.HTTPConnection('blockchain.info')
36             connection.request("GET", "/ticker")
37         except:
38             return
39         response = connection.getresponse()
40         if response.reason == httplib.responses[httplib.NOT_FOUND]:
41             return
42         try:
43             response = json.loads(response.read())
44         except:
45             return
46         quote_currencies = {}
47         try:
48             for r in response:
49                 quote_currencies[r] = self._lookup_rate(response, r)
50             with self.lock:
51                 self.quote_currencies = quote_currencies
52             self.parent.emit(SIGNAL("refresh_balance()"))
53         except KeyError:
54             pass
55
56         print self.quote_currencies
57             
58     def get_currencies(self):
59         return [] if self.quote_currencies == None else sorted(self.quote_currencies.keys())
60
61     def _lookup_rate(self, response, quote_id):
62         return decimal.Decimal(str(response[str(quote_id)]["15m"]))
63
64
65 class Plugin(BasePlugin):
66
67     def fullname(self):
68         return "Exchange rates"
69
70     def description(self):
71         return """exchange rates, retrieved from blockchain.info"""
72
73     def init(self):
74         self.win = self.gui.main_window
75         self.exchanger = Exchanger(self.win)
76         self.win.connect(self.win, SIGNAL("refresh_balance()"), self.win.update_wallet)
77         # Do price discovery
78         self.exchanger.start()
79         self.gui.exchanger = self.exchanger
80
81     def set_status_text(self, text):
82         m = re.match( _( "Balance" ) + ": (\d.+) " + self.win.base_unit(), str(text))
83         if m:
84             amount = Decimal(m.group(1))
85             if self.win.base_unit() == 'mBTC': amount = amount / 1000
86             text += self.create_quote_text(amount)
87             self.win.balance_label.setText(text)
88
89     def create_quote_text(self, btc_balance):
90         quote_currency = self.config.get("currency", "None")
91         quote_balance = self.exchanger.exchange(btc_balance, quote_currency)
92         if quote_balance is None:
93             quote_text = ""
94         else:
95             quote_text = "  (%.2f %s)" % (quote_balance, quote_currency)
96         return quote_text
97
98
99     def requires_settings(self):
100         return True
101
102
103     def settings_dialog(self):
104         d = QDialog(self.win)
105
106         vbox = QVBoxLayout(d)
107
108         grid = QGridLayout()
109         vbox.addLayout(grid)
110
111         currencies = self.exchanger.get_currencies()
112         currencies.insert(0, "None")
113
114         cur_label=QLabel(_('Currency') + ':')
115         grid.addWidget(cur_label , 2, 0)
116         cur_combo = QComboBox()
117         cur_combo.addItems(currencies)
118         try:
119             index = currencies.index(self.config.get('currency', "None"))
120         except:
121             index = 0
122         cur_combo.setCurrentIndex(index)
123         grid.addWidget(cur_combo, 2, 1)
124         grid.addWidget(HelpButton(_('Select which currency is used for quotes.') + ' '), 2, 2)
125
126         vbox.addLayout(ok_cancel_buttons(d))
127
128         if d.exec_():
129
130             cur_request = str(currencies[cur_combo.currentIndex()])
131             if cur_request != self.config.get('currency', "None"):
132                 self.config.set_key('currency', cur_request, True)
133                 self.win.update_wallet()
134
135
136