plugins settings widget
[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.set_currencies(quote_currencies)
53         except KeyError:
54             pass
55
56             
57     def get_currencies(self):
58         return [] if self.quote_currencies == None else sorted(self.quote_currencies.keys())
59
60     def _lookup_rate(self, response, quote_id):
61         return decimal.Decimal(str(response[str(quote_id)]["15m"]))
62
63
64 class Plugin(BasePlugin):
65
66     def fullname(self):
67         return "Exchange rates"
68
69     def description(self):
70         return """exchange rates, retrieved from blockchain.info"""
71
72
73     def __init__(self,a,b):
74         BasePlugin.__init__(self,a,b)
75         self.currencies = [self.config.get('currency', "EUR")]
76
77     def init(self):
78         self.win = self.gui.main_window
79         self.win.connect(self.win, SIGNAL("refresh_currencies()"), self.win.update_status)
80         # Do price discovery
81         self.exchanger = Exchanger(self)
82         self.exchanger.start()
83
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()"))
88
89     def set_quote_text(self, btc_balance, r):
90         r[0] = self.create_quote_text(Decimal(btc_balance) / 100000000)
91
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:
96             quote_text = ""
97         else:
98             quote_text = "%.2f %s" % (quote_balance, quote_currency)
99         return quote_text
100
101
102     def requires_settings(self):
103         return True
104
105
106     def toggle(self):
107         out = BasePlugin.toggle(self)
108         self.win.update_status()
109         return out
110
111
112     def settings_widget(self, window):
113         combo = QComboBox()
114
115         def on_change(x):
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()
120
121         def set_currencies(combo):
122             try:
123                 combo.clear()
124             except:
125                 return
126             combo.addItems(self.currencies)
127             try:
128                 index = self.currencies.index(self.config.get('currency', "EUR"))
129             except:
130                 index = 0
131             combo.setCurrentIndex(index)
132
133         set_currencies(combo)
134         combo.currentIndexChanged.connect(on_change)
135         combo.connect(window, SIGNAL('refresh_currencies_combo()'), lambda: set_currencies(combo))
136         return combo
137
138
139