99a494ceeb1fc5f84f281e87834aaf6ccc1d709d
[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 = self.btc_rate
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         self.win.tabs.removeTab(1)
397         new_send_tab = self.gui.main_window.create_send_tab()
398         self.win.tabs.insertTab(1, new_send_tab, _('Send'))
399         return out
400
401
402     def close(self):
403         self.exchanger.stop()
404
405     def history_tab_update(self):
406         if self.config.get('history_rates', 'unchecked') == "checked":
407             cur_exchange = self.config.get('use_exchange', "Blockchain")
408             try:
409                 tx_list = self.tx_list
410             except Exception:
411                 return
412
413             try:
414                 mintimestr = datetime.datetime.fromtimestamp(int(min(tx_list.items(), key=lambda x: x[1]['timestamp'])[1]['timestamp'])).strftime('%Y-%m-%d')
415             except Exception:
416                 return
417             maxtimestr = datetime.datetime.now().strftime('%Y-%m-%d')
418
419             if cur_exchange == "CoinDesk":
420                 try:
421                     resp_hist = self.exchanger.get_json('api.coindesk.com', "/v1/bpi/historical/close.json?start=" + mintimestr + "&end=" + maxtimestr)
422                 except Exception:
423                     return
424             elif cur_exchange == "Winkdex":
425                 try:
426                     resp_hist = self.exchanger.get_json('winkdex.com', "/static/data/0_86400_730.json")['prices']
427                 except Exception:
428                     return
429             elif cur_exchange == "BitcoinVenezuela":
430                 cur_currency = self.config.get('currency', "EUR")
431                 if cur_currency == "VEF":
432                     try:
433                         resp_hist = self.exchanger.get_json('api.bitcoinvenezuela.com', "/historical/index.php?coin=BTC")['VEF_BTC']
434                     except Exception:
435                         return
436                 elif cur_currency == "ARS":
437                     try:
438                         resp_hist = self.exchanger.get_json('api.bitcoinvenezuela.com', "/historical/index.php?coin=BTC")['ARS_BTC']
439                     except Exception:
440                         return
441                 else:
442                     return
443
444             self.gui.main_window.is_edit = True
445             self.gui.main_window.history_list.setColumnCount(6)
446             self.gui.main_window.history_list.setHeaderLabels( [ '', _('Date'), _('Description') , _('Amount'), _('Balance'), _('Fiat Amount')] )
447             root = self.gui.main_window.history_list.invisibleRootItem()
448             childcount = root.childCount()
449             for i in range(childcount):
450                 item = root.child(i)
451                 try:
452                     tx_info = tx_list[str(item.data(0, Qt.UserRole).toPyObject())]
453                 except Exception:
454                     newtx = self.wallet.get_tx_history()
455                     v = newtx[[x[0] for x in newtx].index(str(item.data(0, Qt.UserRole).toPyObject()))][3]
456
457                     tx_info = {'timestamp':int(time.time()), 'value': v }
458                     pass
459                 tx_time = int(tx_info['timestamp'])
460                 if cur_exchange == "CoinDesk":
461                     tx_time_str = datetime.datetime.fromtimestamp(tx_time).strftime('%Y-%m-%d')
462                     try:
463                         tx_USD_val = "%.2f %s" % (Decimal(str(tx_info['value'])) / 100000000 * Decimal(resp_hist['bpi'][tx_time_str]), "USD")
464                     except KeyError:
465                         tx_USD_val = "%.2f %s" % (self.btc_rate * Decimal(str(tx_info['value']))/100000000 , "USD")
466                 elif cur_exchange == "Winkdex":
467                     tx_time_str = int(tx_time) - (int(tx_time) % (60 * 60 * 24))
468                     try:
469                         tx_rate = resp_hist[[x['x'] for x in resp_hist].index(tx_time_str)]['y']
470                         tx_USD_val = "%.2f %s" % (Decimal(tx_info['value']) / 100000000 * Decimal(tx_rate), "USD")
471                     except ValueError:
472                         tx_USD_val = "%.2f %s" % (self.btc_rate * Decimal(tx_info['value'])/100000000 , "USD")
473                 elif cur_exchange == "BitcoinVenezuela":
474                     tx_time_str = datetime.datetime.fromtimestamp(tx_time).strftime('%Y-%m-%d')
475                     try:
476                         num = resp_hist[tx_time_str].replace(',','')
477                         tx_BTCVEN_val = "%.2f %s" % (Decimal(str(tx_info['value'])) / 100000000 * Decimal(num), cur_currency)
478                     except KeyError:
479                         tx_BTCVEN_val = _("No data")
480
481                 if cur_exchange == "CoinDesk" or cur_exchange == "Winkdex":
482                     item.setText(5, tx_USD_val)
483                 elif cur_exchange == "BitcoinVenezuela":
484                     item.setText(5, tx_BTCVEN_val)
485                 if Decimal(str(tx_info['value'])) < 0:
486                     item.setForeground(5, QBrush(QColor("#BC1E1E")))
487
488             for i, width in enumerate(self.gui.main_window.column_widths['history']):
489                 self.gui.main_window.history_list.setColumnWidth(i, width)
490             self.gui.main_window.history_list.setColumnWidth(4, 140)
491             self.gui.main_window.history_list.setColumnWidth(5, 120)
492             self.gui.main_window.is_edit = False
493
494
495     def settings_widget(self, window):
496         return EnterButton(_('Settings'), self.settings_dialog)
497
498     def settings_dialog(self):
499         d = QDialog()
500         d.setWindowTitle("Settings")
501         layout = QGridLayout(d)
502         layout.addWidget(QLabel(_('Exchange rate API: ')), 0, 0)
503         layout.addWidget(QLabel(_('Currency: ')), 1, 0)
504         layout.addWidget(QLabel(_('History Rates: ')), 2, 0)
505         combo = QComboBox()
506         combo_ex = QComboBox()
507         hist_checkbox = QCheckBox()
508         hist_checkbox.setEnabled(False)
509         if self.config.get('history_rates', 'unchecked') == 'unchecked':
510             hist_checkbox.setChecked(False)
511         else:
512             hist_checkbox.setChecked(True)
513         ok_button = QPushButton(_("OK"))
514
515         def on_change(x):
516             try:
517                 cur_request = str(self.currencies[x])
518             except Exception:
519                 return
520             if cur_request != self.config.get('currency', "EUR"):
521                 self.config.set_key('currency', cur_request, True)
522                 cur_exchange = self.config.get('use_exchange', "Blockchain")
523                 if cur_request == "USD" and (cur_exchange == "CoinDesk" or cur_exchange == "Winkdex"):
524                     hist_checkbox.setEnabled(True)
525                 elif cur_request == "VEF" and (cur_exchange == "BitcoinVenezuela"):
526                     hist_checkbox.setEnabled(True)
527                 elif cur_request == "ARS" and (cur_exchange == "BitcoinVenezuela"):
528                     hist_checkbox.setEnabled(True)
529                 else:
530                     hist_checkbox.setChecked(False)
531                     hist_checkbox.setEnabled(False)
532                 self.win.update_status()
533                 try:
534                     self.fiat_button
535                 except:
536                     pass
537                 else:
538                     self.fiat_button.setText(cur_request)
539
540         def disable_check():
541             hist_checkbox.setChecked(False)
542             hist_checkbox.setEnabled(False)
543
544         def on_change_ex(x):
545             cur_request = str(self.exchanges[x])
546             if cur_request != self.config.get('use_exchange', "Blockchain"):
547                 self.config.set_key('use_exchange', cur_request, True)
548                 self.currencies = []
549                 combo.clear()
550                 self.exchanger.query_rates.set()
551                 cur_currency = self.config.get('currency', "EUR")
552                 if cur_request == "CoinDesk" or cur_request == "Winkdex":
553                     if cur_currency == "USD":
554                         hist_checkbox.setEnabled(True)
555                     else:
556                         disable_check()
557                 elif cur_request == "BitcoinVenezuela":
558                     if cur_currency == "VEF" or cur_currency == "ARS":
559                         hist_checkbox.setEnabled(True)
560                     else:
561                         disable_check()
562                 else:
563                     disable_check()
564                 set_currencies(combo)
565                 self.win.update_status()
566
567         def on_change_hist(checked):
568             if checked:
569                 self.config.set_key('history_rates', 'checked')
570                 self.history_tab_update()
571             else:
572                 self.config.set_key('history_rates', 'unchecked')
573                 self.gui.main_window.history_list.setHeaderLabels( [ '', _('Date'), _('Description') , _('Amount'), _('Balance')] )
574                 self.gui.main_window.history_list.setColumnCount(5)
575                 for i,width in enumerate(self.gui.main_window.column_widths['history']):
576                     self.gui.main_window.history_list.setColumnWidth(i, width)
577
578         def set_hist_check(hist_checkbox):
579             cur_exchange = self.config.get('use_exchange', "Blockchain")
580             if cur_exchange == "CoinDesk" or cur_exchange == "Winkdex":
581                 hist_checkbox.setEnabled(True)
582             elif cur_exchange == "BitcoinVenezuela":
583                 hist_checkbox.setEnabled(True)
584             else:
585                 hist_checkbox.setEnabled(False)
586
587         def set_currencies(combo):
588             current_currency = self.config.get('currency', "EUR")
589             try:
590                 combo.clear()
591             except Exception:
592                 return
593             combo.addItems(self.currencies)
594             try:
595                 index = self.currencies.index(current_currency)
596             except Exception:
597                 index = 0
598             combo.setCurrentIndex(index)
599
600         def set_exchanges(combo_ex):
601             try:
602                 combo_ex.clear()
603             except Exception:
604                 return
605             combo_ex.addItems(self.exchanges)
606             try:
607                 index = self.exchanges.index(self.config.get('use_exchange', "Blockchain"))
608             except Exception:
609                 index = 0
610             combo_ex.setCurrentIndex(index)
611
612         def ok_clicked():
613             d.accept();
614
615         set_exchanges(combo_ex)
616         set_currencies(combo)
617         set_hist_check(hist_checkbox)
618         combo.currentIndexChanged.connect(on_change)
619         combo_ex.currentIndexChanged.connect(on_change_ex)
620         hist_checkbox.stateChanged.connect(on_change_hist)
621         combo.connect(self.win, SIGNAL('refresh_currencies_combo()'), lambda: set_currencies(combo))
622         combo_ex.connect(d, SIGNAL('refresh_exchanges_combo()'), lambda: set_exchanges(combo_ex))
623         ok_button.clicked.connect(lambda: ok_clicked())
624         layout.addWidget(combo,1,1)
625         layout.addWidget(combo_ex,0,1)
626         layout.addWidget(hist_checkbox,2,1)
627         layout.addWidget(ok_button,3,1)
628
629         if d.exec_():
630             return True
631         else:
632             return False
633
634     def fiat_unit(self):
635         quote_currency = self.config.get("currency", "???")
636         return quote_currency
637
638     def fiat_dialog(self):
639         if not self.config.get('use_exchange_rate'):
640           self.gui.main_window.show_message(_("To use this feature, first enable the exchange rate plugin."))
641           return
642
643         if not self.gui.main_window.network.is_connected():
644           self.gui.main_window.show_message(_("To use this feature, you must have a network connection."))
645           return
646
647         quote_currency = self.fiat_unit()
648
649         d = QDialog(self.gui.main_window)
650         d.setWindowTitle("Fiat")
651         vbox = QVBoxLayout(d)
652         text = "Amount to Send in " + quote_currency
653         vbox.addWidget(QLabel(_(text)+':'))
654
655         grid = QGridLayout()
656         fiat_e = AmountEdit(self.fiat_unit)
657         grid.addWidget(fiat_e, 1, 0)
658
659         r = {}
660         self.get_fiat_price_text(r)
661         quote = r.get(0)
662         if quote:
663           text = "1 BTC~%s"%quote
664           grid.addWidget(QLabel(_(text)), 4, 0, 3, 0)
665         else:
666             self.gui.main_window.show_message(_("Exchange rate not available.  Please check your network connection."))
667             return
668
669         vbox.addLayout(grid)
670         vbox.addLayout(ok_cancel_buttons(d))
671
672         if not d.exec_():
673             return
674
675         fiat = str(fiat_e.text())
676
677         if str(fiat) == "" or str(fiat) == ".":
678             fiat = "0"
679
680         quote = quote[:-4]
681         btcamount = Decimal(fiat) / Decimal(quote)
682         if str(self.gui.main_window.base_unit()) == "mBTC":
683             btcamount = btcamount * 1000
684         quote = "%.8f"%btcamount
685         self.gui.main_window.amount_e.setText( quote )
686
687     def exchange_rate_button(self, grid):
688         quote_currency = self.config.get("currency", "EUR")
689         self.fiat_button = EnterButton(_(quote_currency), self.fiat_dialog)
690         grid.addWidget(self.fiat_button, 4, 3, Qt.AlignHCenter)