cddf5aea33d4339006be1b229875090e2c16f24e
[electrum-nvc.git] / plugins / exchange_rate.py
1 from PyQt4.QtGui import *
2 from PyQt4.QtCore import *
3
4 import datetime
5 import decimal
6 import httplib
7 import json
8 import threading
9 import time
10 import re
11 from decimal import Decimal
12 from electrum.plugins import BasePlugin
13 from electrum.i18n import _
14 from electrum_gui.qt.util import *
15 from electrum_gui.qt.amountedit import AmountEdit
16
17
18 EXCHANGES = ["BitcoinAverage",
19              "BitcoinVenezuela",
20              "Bitcurex",
21              "Bitmarket",
22              "BitPay",
23              "Blockchain",
24              "BTCChina",
25              "CaVirtEx",
26              "Coinbase",
27              "CoinDesk",
28              "LocalBitcoins",
29              "Winkdex"]
30
31
32 class Exchanger(threading.Thread):
33
34     def __init__(self, parent):
35         threading.Thread.__init__(self)
36         self.daemon = True
37         self.parent = parent
38         self.quote_currencies = None
39         self.lock = threading.Lock()
40         self.query_rates = threading.Event()
41         self.use_exchange = self.parent.config.get('use_exchange', "Blockchain")
42         self.parent.exchanges = EXCHANGES
43         self.parent.currencies = ["EUR","GBP","USD","PLN"]
44         self.parent.win.emit(SIGNAL("refresh_exchanges_combo()"))
45         self.parent.win.emit(SIGNAL("refresh_currencies_combo()"))
46         self.is_running = False
47
48     def get_json(self, site, get_string):
49         try:
50             connection = httplib.HTTPSConnection(site)
51             connection.request("GET", get_string)
52         except Exception:
53             raise
54         resp = connection.getresponse()
55         if resp.reason == httplib.responses[httplib.NOT_FOUND]:
56             raise
57         try:
58             json_resp = json.loads(resp.read())
59         except Exception:
60             raise
61         return json_resp
62
63
64     def exchange(self, btc_amount, quote_currency):
65         with self.lock:
66             if self.quote_currencies is None:
67                 return None
68             quote_currencies = self.quote_currencies.copy()
69         if quote_currency not in quote_currencies:
70             return None
71         if self.use_exchange == "CoinDesk":
72             try:
73                 resp_rate = self.get_json('api.coindesk.com', "/v1/bpi/currentprice/" + str(quote_currency) + ".json")
74             except Exception:
75                 return
76             return btc_amount * decimal.Decimal(str(resp_rate["bpi"][str(quote_currency)]["rate_float"]))
77         return btc_amount * decimal.Decimal(str(quote_currencies[quote_currency]))
78
79     def stop(self):
80         self.is_running = False
81
82     def update_rate(self):
83         self.use_exchange = self.parent.config.get('use_exchange', "Blockchain")
84         update_rates = {
85             "BitcoinAverage": self.update_ba,
86             "BitcoinVenezuela": self.update_bv,
87             "Bitcurex": self.update_bx,
88             "Bitmarket": self.update_bm,
89             "BitPay": self.update_bp,
90             "Blockchain": self.update_bc,
91             "BTCChina": self.update_CNY,
92             "CaVirtEx": self.update_cv,
93             "CoinDesk": self.update_cd,
94             "Coinbase": self.update_cb,
95             "LocalBitcoins": self.update_lb,
96             "Winkdex": self.update_wd,
97         }
98         try:
99             update_rates[self.use_exchange]()
100         except KeyError:
101             return
102
103     def run(self):
104         self.is_running = True
105         while self.is_running:
106             self.query_rates.clear()
107             self.update_rate()
108             self.query_rates.wait(150)
109
110
111     def update_cd(self):
112         try:
113             resp_currencies = self.get_json('api.coindesk.com', "/v1/bpi/supported-currencies.json")
114         except Exception:
115             return
116
117         quote_currencies = {}
118         for cur in resp_currencies:
119             quote_currencies[str(cur["currency"])] = 0.0
120         with self.lock:
121             self.quote_currencies = quote_currencies
122         self.parent.set_currencies(quote_currencies)
123
124     def update_wd(self):
125         try:
126             winkresp = self.get_json('winkdex.com', "/static/data/0_600_288.json")
127             ####could need nonce value in GET, no Docs available
128         except Exception:
129             return
130         quote_currencies = {"USD": 0.0}
131         ####get y of highest x in "prices"
132         lenprices = len(winkresp["prices"])
133         usdprice = winkresp["prices"][lenprices-1]["y"]
134         try:
135             quote_currencies["USD"] = decimal.Decimal(str(usdprice))
136             with self.lock:
137                 self.quote_currencies = quote_currencies
138         except KeyError:
139             pass
140         self.parent.set_currencies(quote_currencies)
141
142     def update_cv(self):
143         try:
144             jsonresp = self.get_json('www.cavirtex.com', "/api/CAD/ticker.json")
145         except Exception:
146             return
147         quote_currencies = {"CAD": 0.0}
148         cadprice = jsonresp["last"]
149         try:
150             quote_currencies["CAD"] = decimal.Decimal(str(cadprice))
151             with self.lock:
152                 self.quote_currencies = quote_currencies
153         except KeyError:
154             pass
155         self.parent.set_currencies(quote_currencies)
156
157     def update_bm(self):
158         try:
159             jsonresp = self.get_json('www.bitmarket.pl', "/json/BTCPLN/ticker.json")
160         except Exception:
161             return
162         quote_currencies = {"PLN": 0.0}
163         pln_price = jsonresp["last"]
164         try:
165             quote_currencies["PLN"] = decimal.Decimal(str(pln_price))
166             with self.lock:
167                 self.quote_currencies = quote_currencies
168         except KeyError:
169             pass
170         self.parent.set_currencies(quote_currencies)
171
172     def update_bx(self):
173         try:
174             jsonresp = self.get_json('pln.bitcurex.com', "/data/ticker.json")
175         except Exception:
176             return
177         quote_currencies = {"PLN": 0.0}
178         pln_price = jsonresp["last"]
179         try:
180             quote_currencies["PLN"] = decimal.Decimal(str(pln_price))
181             with self.lock:
182                 self.quote_currencies = quote_currencies
183         except KeyError:
184             pass
185         self.parent.set_currencies(quote_currencies)
186
187     def update_CNY(self):
188         try:
189             jsonresp = self.get_json('data.btcchina.com', "/data/ticker")
190         except Exception:
191             return
192         quote_currencies = {"CNY": 0.0}
193         cnyprice = jsonresp["ticker"]["last"]
194         try:
195             quote_currencies["CNY"] = decimal.Decimal(str(cnyprice))
196             with self.lock:
197                 self.quote_currencies = quote_currencies
198         except KeyError:
199             pass
200         self.parent.set_currencies(quote_currencies)
201
202     def update_bp(self):
203         try:
204             jsonresp = self.get_json('bitpay.com', "/api/rates")
205         except Exception:
206             return
207         quote_currencies = {}
208         try:
209             for r in jsonresp:
210                 quote_currencies[str(r["code"])] = decimal.Decimal(r["rate"])
211             with self.lock:
212                 self.quote_currencies = quote_currencies
213         except KeyError:
214             pass
215         self.parent.set_currencies(quote_currencies)
216
217     def update_cb(self):
218         try:
219             jsonresp = self.get_json('coinbase.com', "/api/v1/currencies/exchange_rates")
220         except Exception:
221             return
222
223         quote_currencies = {}
224         try:
225             for r in jsonresp:
226                 if r[:7] == "btc_to_":
227                     quote_currencies[r[7:].upper()] = self._lookup_rate_cb(jsonresp, r)
228             with self.lock:
229                 self.quote_currencies = quote_currencies
230         except KeyError:
231             pass
232         self.parent.set_currencies(quote_currencies)
233
234
235     def update_bc(self):
236         try:
237             jsonresp = self.get_json('blockchain.info', "/ticker")
238         except Exception:
239             return
240         quote_currencies = {}
241         try:
242             for r in jsonresp:
243                 quote_currencies[r] = self._lookup_rate(jsonresp, r)
244             with self.lock:
245                 self.quote_currencies = quote_currencies
246         except KeyError:
247             pass
248         self.parent.set_currencies(quote_currencies)
249         # print "updating exchange rate", self.quote_currencies["USD"]
250
251     def update_lb(self):
252         try:
253             jsonresp = self.get_json('localbitcoins.com', "/bitcoinaverage/ticker-all-currencies/")
254         except Exception:
255             return
256         quote_currencies = {}
257         try:
258             for r in jsonresp:
259                 quote_currencies[r] = self._lookup_rate_lb(jsonresp, r)
260             with self.lock:
261                 self.quote_currencies = quote_currencies
262         except KeyError:
263             pass
264         self.parent.set_currencies(quote_currencies)
265
266
267     def update_bv(self):
268         try:
269             jsonresp = self.get_json('api.bitcoinvenezuela.com', "/")
270         except Exception:
271             return
272         quote_currencies = {}
273         try:
274             for r in jsonresp["BTC"]:
275                 quote_currencies[r] = Decimal(jsonresp["BTC"][r])
276             with self.lock:
277                 self.quote_currencies = quote_currencies
278         except KeyError:
279             pass
280         self.parent.set_currencies(quote_currencies)
281
282
283     def update_ba(self):
284         try:
285             jsonresp = self.get_json('api.bitcoinaverage.com', "/ticker/global/all")
286         except Exception:
287             return
288         quote_currencies = {}
289         try:
290             for r in jsonresp:
291                 if not r == "timestamp":
292                     quote_currencies[r] = self._lookup_rate_ba(jsonresp, r)
293             with self.lock:
294                 self.quote_currencies = quote_currencies
295         except KeyError:
296             pass
297         self.parent.set_currencies(quote_currencies)
298
299
300     def get_currencies(self):
301         return [] if self.quote_currencies == None else sorted(self.quote_currencies.keys())
302
303     def _lookup_rate(self, response, quote_id):
304         return decimal.Decimal(str(response[str(quote_id)]["15m"]))
305     def _lookup_rate_cb(self, response, quote_id):
306         return decimal.Decimal(str(response[str(quote_id)]))
307     def _lookup_rate_ba(self, response, quote_id):
308         return decimal.Decimal(response[str(quote_id)]["last"])
309     def _lookup_rate_lb(self, response, quote_id):
310         return decimal.Decimal(response[str(quote_id)]["rates"]["last"])
311
312
313 class Plugin(BasePlugin):
314
315     def fullname(self):
316         return "Exchange rates"
317
318     def description(self):
319         return """exchange rates, retrieved from blockchain.info, CoinDesk, or Coinbase"""
320
321
322     def __init__(self,a,b):
323         BasePlugin.__init__(self,a,b)
324         self.currencies = [self.config.get('currency', "EUR")]
325         self.exchanges = [self.config.get('use_exchange', "Blockchain")]
326
327     def init(self):
328         self.win = self.gui.main_window
329         self.win.connect(self.win, SIGNAL("refresh_currencies()"), self.win.update_status)
330         self.btc_rate = Decimal("0.0")
331         # Do price discovery
332         self.exchanger = Exchanger(self)
333         self.exchanger.start()
334         self.gui.exchanger = self.exchanger #
335
336     def set_currencies(self, currency_options):
337         self.currencies = sorted(currency_options)
338         self.win.emit(SIGNAL("refresh_currencies()"))
339         self.win.emit(SIGNAL("refresh_currencies_combo()"))
340
341     def get_fiat_balance_text(self, btc_balance, r):
342         # return balance as: 1.23 USD
343         r[0] = self.create_fiat_balance_text(Decimal(btc_balance) / 100000000)
344
345     def get_fiat_price_text(self, r):
346         # return BTC price as: 123.45 USD
347         r[0] = self.create_fiat_balance_text(1)
348         quote = r[0]
349         if quote:
350             r[0] = "%s"%quote
351
352     def get_fiat_status_text(self, btc_balance, r2):
353         # return status as:   (1.23 USD)    1 BTC~123.45 USD
354         text = ""
355         r = {}
356         self.get_fiat_price_text(r)
357         quote = r.get(0)
358         if quote:
359             price_text = "1 BTC~%s"%quote
360             fiat_currency = quote[-3:]
361             btc_price = quote[:-4]
362             fiat_balance = Decimal(btc_price) * (Decimal(btc_balance)/100000000)
363             balance_text = "(%.2f %s)" % (fiat_balance,fiat_currency)
364             text = "  " + balance_text + "     " + price_text + " "
365         r2[0] = text
366
367     def create_fiat_balance_text(self, btc_balance):
368         quote_currency = self.config.get("currency", "EUR")
369         self.exchanger.use_exchange = self.config.get("use_exchange", "Blockchain")
370         cur_rate = self.exchanger.exchange(Decimal("1.0"), quote_currency)
371         if cur_rate is None:
372             quote_text = ""
373         else:
374             quote_balance = btc_balance * Decimal(cur_rate)
375             self.btc_rate = cur_rate
376             quote_text = "%.2f %s" % (quote_balance, quote_currency)
377         return quote_text
378
379     def load_wallet(self, wallet):
380         self.wallet = wallet
381         tx_list = {}
382         for item in self.wallet.get_tx_history(self.wallet.storage.get("current_account", None)):
383             tx_hash, conf, is_mine, value, fee, balance, timestamp = item
384             tx_list[tx_hash] = {'value': value, 'timestamp': timestamp, 'balance': balance}
385
386         self.tx_list = tx_list
387
388
389     def requires_settings(self):
390         return True
391
392
393     def toggle(self):
394         out = BasePlugin.toggle(self)
395         self.win.update_status()
396         if self.config.get('use_exchange_rate'):
397             try:
398                 self.fiat_button
399             except:
400                 self.gui.main_window.show_message(_("To see fiat amount when sending bitcoin, please restart Electrum to activate the new GUI settings."))
401         return out
402
403
404     def close(self):
405         self.exchanger.stop()
406
407     def history_tab_update(self):
408         if self.config.get('history_rates', 'unchecked') == "checked":
409             cur_exchange = self.config.get('use_exchange', "Blockchain")
410             try:
411                 tx_list = self.tx_list
412             except Exception:
413                 return
414
415             try:
416                 mintimestr = datetime.datetime.fromtimestamp(int(min(tx_list.items(), key=lambda x: x[1]['timestamp'])[1]['timestamp'])).strftime('%Y-%m-%d')
417             except Exception:
418                 return
419             maxtimestr = datetime.datetime.now().strftime('%Y-%m-%d')
420
421             if cur_exchange == "CoinDesk":
422                 try:
423                     resp_hist = self.exchanger.get_json('api.coindesk.com', "/v1/bpi/historical/close.json?start=" + mintimestr + "&end=" + maxtimestr)
424                 except Exception:
425                     return
426             elif cur_exchange == "Winkdex":
427                 try:
428                     resp_hist = self.exchanger.get_json('winkdex.com', "/static/data/0_86400_730.json")['prices']
429                 except Exception:
430                     return
431             elif cur_exchange == "BitcoinVenezuela":
432                 cur_currency = self.config.get('currency', "EUR")
433                 if cur_currency == "VEF":
434                     try:
435                         resp_hist = self.exchanger.get_json('api.bitcoinvenezuela.com', "/historical/index.php")['VEF_BTC']
436                     except Exception:
437                         return
438                 elif cur_currency == "ARS":
439                     try:
440                         resp_hist = self.exchanger.get_json('api.bitcoinvenezuela.com', "/historical/index.php")['ARS_BTC']
441                     except Exception:
442                         return
443                 else:
444                     return
445
446             self.gui.main_window.is_edit = True
447             self.gui.main_window.history_list.setColumnCount(6)
448             self.gui.main_window.history_list.setHeaderLabels( [ '', _('Date'), _('Description') , _('Amount'), _('Balance'), _('Fiat Amount')] )
449             root = self.gui.main_window.history_list.invisibleRootItem()
450             childcount = root.childCount()
451             for i in range(childcount):
452                 item = root.child(i)
453                 try:
454                     tx_info = tx_list[str(item.data(0, Qt.UserRole).toPyObject())]
455                 except Exception:
456                     newtx = self.wallet.get_tx_history()
457                     v = newtx[[x[0] for x in newtx].index(str(item.data(0, Qt.UserRole).toPyObject()))][3]
458
459                     tx_info = {'timestamp':int(time.time()), 'value': v }
460                     pass
461                 tx_time = int(tx_info['timestamp'])
462                 if cur_exchange == "CoinDesk":
463                     tx_time_str = datetime.datetime.fromtimestamp(tx_time).strftime('%Y-%m-%d')
464                     try:
465                         tx_USD_val = "%.2f %s" % (Decimal(str(tx_info['value'])) / 100000000 * Decimal(resp_hist['bpi'][tx_time_str]), "USD")
466                     except KeyError:
467                         tx_USD_val = "%.2f %s" % (self.btc_rate * Decimal(str(tx_info['value']))/100000000 , "USD")
468                 elif cur_exchange == "Winkdex":
469                     tx_time_str = int(tx_time) - (int(tx_time) % (60 * 60 * 24))
470                     try:
471                         tx_rate = resp_hist[[x['x'] for x in resp_hist].index(tx_time_str)]['y']
472                         tx_USD_val = "%.2f %s" % (Decimal(tx_info['value']) / 100000000 * Decimal(tx_rate), "USD")
473                     except ValueError:
474                         tx_USD_val = "%.2f %s" % (self.btc_rate * Decimal(tx_info['value'])/100000000 , "USD")
475                 elif cur_exchange == "BitcoinVenezuela":
476                     tx_time_str = datetime.datetime.fromtimestamp(tx_time).strftime('%Y-%m-%d')
477                     try:
478                         num = resp_hist[tx_time_str].replace(',','')
479                         tx_BTCVEN_val = "%.2f %s" % (Decimal(str(tx_info['value'])) / 100000000 * Decimal(num), cur_currency)
480                     except KeyError:
481                         tx_BTCVEN_val = _("No data")
482
483                 if cur_exchange == "CoinDesk" or cur_exchange == "Winkdex":
484                     item.setText(5, tx_USD_val)
485                 elif cur_exchange == "BitcoinVenezuela":
486                     item.setText(5, tx_BTCVEN_val)
487                 if Decimal(str(tx_info['value'])) < 0:
488                     item.setForeground(5, QBrush(QColor("#BC1E1E")))
489
490             for i, width in enumerate(self.gui.main_window.column_widths['history']):
491                 self.gui.main_window.history_list.setColumnWidth(i, width)
492             self.gui.main_window.history_list.setColumnWidth(4, 140)
493             self.gui.main_window.history_list.setColumnWidth(5, 120)
494             self.gui.main_window.is_edit = False
495
496
497     def settings_widget(self, window):
498         return EnterButton(_('Settings'), self.settings_dialog)
499
500     def settings_dialog(self):
501         d = QDialog()
502         d.setWindowTitle("Settings")
503         layout = QGridLayout(d)
504         layout.addWidget(QLabel(_('Exchange rate API: ')), 0, 0)
505         layout.addWidget(QLabel(_('Currency: ')), 1, 0)
506         layout.addWidget(QLabel(_('History Rates: ')), 2, 0)
507         combo = QComboBox()
508         combo_ex = QComboBox()
509         hist_checkbox = QCheckBox()
510         hist_checkbox.setEnabled(False)
511         if self.config.get('history_rates', 'unchecked') == 'unchecked':
512             hist_checkbox.setChecked(False)
513         else:
514             hist_checkbox.setChecked(True)
515         ok_button = QPushButton(_("OK"))
516
517         def on_change(x):
518             try:
519                 cur_request = str(self.currencies[x])
520             except Exception:
521                 return
522             if cur_request != self.config.get('currency', "EUR"):
523                 self.config.set_key('currency', cur_request, True)
524                 cur_exchange = self.config.get('use_exchange', "Blockchain")
525                 if cur_request == "USD" and (cur_exchange == "CoinDesk" or cur_exchange == "Winkdex"):
526                     hist_checkbox.setEnabled(True)
527                 elif cur_request == "VEF" and (cur_exchange == "BitcoinVenezuela"):
528                     hist_checkbox.setEnabled(True)
529                 elif cur_request == "ARS" and (cur_exchange == "BitcoinVenezuela"):
530                     hist_checkbox.setEnabled(True)
531                 else:
532                     hist_checkbox.setChecked(False)
533                     hist_checkbox.setEnabled(False)
534                 self.win.update_status()
535                 try:
536                     self.fiat_button
537                 except:
538                     pass
539                 else:
540                     self.fiat_button.setText(cur_request)
541
542         def disable_check():
543             hist_checkbox.setChecked(False)
544             hist_checkbox.setEnabled(False)
545
546         def on_change_ex(x):
547             cur_request = str(self.exchanges[x])
548             if cur_request != self.config.get('use_exchange', "Blockchain"):
549                 self.config.set_key('use_exchange', cur_request, True)
550                 self.currencies = []
551                 combo.clear()
552                 self.exchanger.query_rates.set()
553                 cur_currency = self.config.get('currency', "EUR")
554                 if cur_request == "CoinDesk" or cur_request == "Winkdex":
555                     if cur_currency == "USD":
556                         hist_checkbox.setEnabled(True)
557                     else:
558                         disable_check()
559                 elif cur_request == "BitcoinVenezuela":
560                     if cur_currency == "VEF" or cur_currency == "ARS":
561                         hist_checkbox.setEnabled(True)
562                     else:
563                         disable_check()
564                 else:
565                     disable_check()
566                 set_currencies(combo)
567                 self.win.update_status()
568
569         def on_change_hist(checked):
570             if checked:
571                 self.config.set_key('history_rates', 'checked')
572                 self.history_tab_update()
573             else:
574                 self.config.set_key('history_rates', 'unchecked')
575                 self.gui.main_window.history_list.setHeaderLabels( [ '', _('Date'), _('Description') , _('Amount'), _('Balance')] )
576                 self.gui.main_window.history_list.setColumnCount(5)
577                 for i,width in enumerate(self.gui.main_window.column_widths['history']):
578                     self.gui.main_window.history_list.setColumnWidth(i, width)
579
580         def set_hist_check(hist_checkbox):
581             cur_exchange = self.config.get('use_exchange', "Blockchain")
582             if cur_exchange == "CoinDesk" or cur_exchange == "Winkdex":
583                 hist_checkbox.setEnabled(True)
584             elif cur_exchange == "BitcoinVenezuela":
585                 hist_checkbox.setEnabled(True)
586             else:
587                 hist_checkbox.setEnabled(False)
588
589         def set_currencies(combo):
590             current_currency = self.config.get('currency', "EUR")
591             try:
592                 combo.clear()
593             except Exception:
594                 return
595             combo.addItems(self.currencies)
596             try:
597                 index = self.currencies.index(current_currency)
598             except Exception:
599                 index = 0
600             combo.setCurrentIndex(index)
601
602         def set_exchanges(combo_ex):
603             try:
604                 combo_ex.clear()
605             except Exception:
606                 return
607             combo_ex.addItems(self.exchanges)
608             try:
609                 index = self.exchanges.index(self.config.get('use_exchange', "Blockchain"))
610             except Exception:
611                 index = 0
612             combo_ex.setCurrentIndex(index)
613
614         def ok_clicked():
615             d.accept();
616
617         set_exchanges(combo_ex)
618         set_currencies(combo)
619         set_hist_check(hist_checkbox)
620         combo.currentIndexChanged.connect(on_change)
621         combo_ex.currentIndexChanged.connect(on_change_ex)
622         hist_checkbox.stateChanged.connect(on_change_hist)
623         combo.connect(self.win, SIGNAL('refresh_currencies_combo()'), lambda: set_currencies(combo))
624         combo_ex.connect(d, SIGNAL('refresh_exchanges_combo()'), lambda: set_exchanges(combo_ex))
625         ok_button.clicked.connect(lambda: ok_clicked())
626         layout.addWidget(combo,1,1)
627         layout.addWidget(combo_ex,0,1)
628         layout.addWidget(hist_checkbox,2,1)
629         layout.addWidget(ok_button,3,1)
630
631         if d.exec_():
632             return True
633         else:
634             return False
635
636     def fiat_unit(self):
637         quote_currency = self.config.get("currency", "???")
638         return quote_currency
639
640     def fiat_dialog(self):
641         if not self.config.get('use_exchange_rate'):
642           self.gui.main_window.show_message(_("To use this feature, first enable the exchange rate plugin."))
643           return
644
645         if not self.gui.main_window.network.is_connected():
646           self.gui.main_window.show_message(_("To use this feature, you must have a network connection."))
647           return
648
649         quote_currency = self.fiat_unit()
650
651         d = QDialog(self.gui.main_window)
652         d.setWindowTitle("Fiat")
653         vbox = QVBoxLayout(d)
654         text = "Amount to Send in " + quote_currency
655         vbox.addWidget(QLabel(_(text)+':'))
656
657         grid = QGridLayout()
658         fiat_e = AmountEdit(self.fiat_unit)
659         grid.addWidget(fiat_e, 1, 0)
660
661         r = {}
662         self.get_fiat_price_text(r)
663         quote = r.get(0)
664         if quote:
665           text = "1 BTC~%s"%quote
666           grid.addWidget(QLabel(_(text)), 4, 0, 3, 0)
667         else:
668             self.gui.main_window.show_message(_("Exchange rate not available.  Please check your network connection."))
669             return
670
671         vbox.addLayout(grid)
672         vbox.addLayout(ok_cancel_buttons(d))
673
674         if not d.exec_():
675             return
676
677         fiat = str(fiat_e.text())
678
679         if str(fiat) == "" or str(fiat) == ".":
680             fiat = "0"
681
682         quote = quote[:-4]
683         btcamount = Decimal(fiat) / Decimal(quote)
684         if str(self.gui.main_window.base_unit()) == "mBTC":
685             btcamount = btcamount * 1000
686         quote = "%.8f"%btcamount
687         self.gui.main_window.amount_e.setText( quote )
688
689     def exchange_rate_button(self, grid):
690         quote_currency = self.config.get("currency", "EUR")
691         self.fiat_button = EnterButton(_(quote_currency), self.fiat_dialog)
692         grid.addWidget(self.fiat_button, 4, 3, Qt.AlignHCenter)