AmountEdit:get_amount
[electrum-nvc.git] / plugins / exchange_rate.py
index 2a0c22a..f8713a7 100644 (file)
@@ -17,6 +17,8 @@ from electrum_gui.qt.amountedit import AmountEdit
 
 EXCHANGES = ["BitcoinAverage",
              "BitcoinVenezuela",
+             "Bitcurex",
+             "Bitmarket",
              "BitPay",
              "Blockchain",
              "BTCChina",
@@ -38,7 +40,7 @@ class Exchanger(threading.Thread):
         self.query_rates = threading.Event()
         self.use_exchange = self.parent.config.get('use_exchange', "Blockchain")
         self.parent.exchanges = EXCHANGES
-        self.parent.currencies = ["EUR","GBP","USD"]
+        self.parent.currencies = ["EUR","GBP","USD","PLN"]
         self.parent.win.emit(SIGNAL("refresh_exchanges_combo()"))
         self.parent.win.emit(SIGNAL("refresh_currencies_combo()"))
         self.is_running = False
@@ -82,6 +84,8 @@ class Exchanger(threading.Thread):
         update_rates = {
             "BitcoinAverage": self.update_ba,
             "BitcoinVenezuela": self.update_bv,
+            "Bitcurex": self.update_bx,
+            "Bitmarket": self.update_bm,
             "BitPay": self.update_bp,
             "Blockchain": self.update_bc,
             "BTCChina": self.update_CNY,
@@ -150,6 +154,36 @@ class Exchanger(threading.Thread):
             pass
         self.parent.set_currencies(quote_currencies)
 
+    def update_bm(self):
+        try:
+            jsonresp = self.get_json('www.bitmarket.pl', "/json/BTCPLN/ticker.json")
+        except Exception:
+            return
+        quote_currencies = {"PLN": 0.0}
+        pln_price = jsonresp["last"]
+        try:
+            quote_currencies["PLN"] = decimal.Decimal(str(pln_price))
+            with self.lock:
+                self.quote_currencies = quote_currencies
+        except KeyError:
+            pass
+        self.parent.set_currencies(quote_currencies)
+
+    def update_bx(self):
+        try:
+            jsonresp = self.get_json('pln.bitcurex.com', "/data/ticker.json")
+        except Exception:
+            return
+        quote_currencies = {"PLN": 0.0}
+        pln_price = jsonresp["last"]
+        try:
+            quote_currencies["PLN"] = decimal.Decimal(str(pln_price))
+            with self.lock:
+                self.quote_currencies = quote_currencies
+        except KeyError:
+            pass
+        self.parent.set_currencies(quote_currencies)
+
     def update_CNY(self):
         try:
             jsonresp = self.get_json('data.btcchina.com', "/data/ticker")
@@ -287,7 +321,7 @@ class Plugin(BasePlugin):
 
     def __init__(self,a,b):
         BasePlugin.__init__(self,a,b)
-        self.currencies = [self.config.get('currency', "EUR")]
+        self.currencies = [self.fiat_unit()]
         self.exchanges = [self.config.get('use_exchange', "Blockchain")]
 
     def init(self):
@@ -298,6 +332,7 @@ class Plugin(BasePlugin):
         self.exchanger = Exchanger(self)
         self.exchanger.start()
         self.gui.exchanger = self.exchanger #
+        self.add_fiat_edit()
 
     def set_currencies(self, currency_options):
         self.currencies = sorted(currency_options)
@@ -324,14 +359,14 @@ class Plugin(BasePlugin):
         if quote:
             price_text = "1 BTC~%s"%quote
             fiat_currency = quote[-3:]
-            btc_price = quote[:-4]
+            btc_price = self.btc_rate
             fiat_balance = Decimal(btc_price) * (Decimal(btc_balance)/100000000)
             balance_text = "(%.2f %s)" % (fiat_balance,fiat_currency)
             text = "  " + balance_text + "     " + price_text + " "
         r2[0] = text
 
     def create_fiat_balance_text(self, btc_balance):
-        quote_currency = self.config.get("currency", "EUR")
+        quote_currency = self.fiat_unit()
         self.exchanger.use_exchange = self.config.get("use_exchange", "Blockchain")
         cur_rate = self.exchanger.exchange(Decimal("1.0"), quote_currency)
         if cur_rate is None:
@@ -357,14 +392,14 @@ class Plugin(BasePlugin):
 
 
     def toggle(self):
-        out = BasePlugin.toggle(self)
+        enabled = BasePlugin.toggle(self)
         self.win.update_status()
-        if self.config.get('use_exchange_rate'):
-            try:
-                self.fiat_button
-            except:
-                self.gui.main_window.show_message(_("To see fiat amount when sending bitcoin, please restart Electrum to activate the new GUI settings."))
-        return out
+        self.win.tabs.removeTab(1)
+        new_send_tab = self.gui.main_window.create_send_tab()
+        self.win.tabs.insertTab(1, new_send_tab, _('Send'))
+        if enabled:
+            self.add_fiat_edit()
+        return enabled
 
 
     def close(self):
@@ -395,15 +430,15 @@ class Plugin(BasePlugin):
                 except Exception:
                     return
             elif cur_exchange == "BitcoinVenezuela":
-                cur_currency = self.config.get('currency', "EUR")
+                cur_currency = self.fiat_unit()
                 if cur_currency == "VEF":
                     try:
-                        resp_hist = self.exchanger.get_json('api.bitcoinvenezuela.com', "/historical/index.php")['VEF_BTC']
+                        resp_hist = self.exchanger.get_json('api.bitcoinvenezuela.com', "/historical/index.php?coin=BTC")['VEF_BTC']
                     except Exception:
                         return
                 elif cur_currency == "ARS":
                     try:
-                        resp_hist = self.exchanger.get_json('api.bitcoinvenezuela.com', "/historical/index.php")['ARS_BTC']
+                        resp_hist = self.exchanger.get_json('api.bitcoinvenezuela.com', "/historical/index.php?coin=BTC")['ARS_BTC']
                     except Exception:
                         return
                 else:
@@ -485,7 +520,7 @@ class Plugin(BasePlugin):
                 cur_request = str(self.currencies[x])
             except Exception:
                 return
-            if cur_request != self.config.get('currency', "EUR"):
+            if cur_request != self.fiat_unit():
                 self.config.set_key('currency', cur_request, True)
                 cur_exchange = self.config.get('use_exchange', "Blockchain")
                 if cur_request == "USD" and (cur_exchange == "CoinDesk" or cur_exchange == "Winkdex"):
@@ -516,7 +551,7 @@ class Plugin(BasePlugin):
                 self.currencies = []
                 combo.clear()
                 self.exchanger.query_rates.set()
-                cur_currency = self.config.get('currency', "EUR")
+                cur_currency = self.fiat_unit()
                 if cur_request == "CoinDesk" or cur_request == "Winkdex":
                     if cur_currency == "USD":
                         hist_checkbox.setEnabled(True)
@@ -553,7 +588,7 @@ class Plugin(BasePlugin):
                 hist_checkbox.setEnabled(False)
 
         def set_currencies(combo):
-            current_currency = self.config.get('currency', "EUR")
+            current_currency = self.fiat_unit()
             try:
                 combo.clear()
             except Exception:
@@ -600,59 +635,31 @@ class Plugin(BasePlugin):
             return False
 
     def fiat_unit(self):
-        quote_currency = self.config.get("currency", "???")
-        return quote_currency
+        return self.config.get("currency", "EUR")
 
-    def fiat_dialog(self):
-        if not self.config.get('use_exchange_rate'):
-          self.gui.main_window.show_message(_("To use this feature, first enable the exchange rate plugin."))
-          return
-
-        if not self.gui.main_window.network.is_connected():
-          self.gui.main_window.show_message(_("To use this feature, you must have a network connection."))
-          return
-
-        quote_currency = self.fiat_unit()
-
-        d = QDialog(self.gui.main_window)
-        d.setWindowTitle("Fiat")
-        vbox = QVBoxLayout(d)
-        text = "Amount to Send in " + quote_currency
-        vbox.addWidget(QLabel(_(text)+':'))
-
-        grid = QGridLayout()
-        fiat_e = AmountEdit(self.fiat_unit)
-        grid.addWidget(fiat_e, 1, 0)
-
-        r = {}
-        self.get_fiat_price_text(r)
-        quote = r.get(0)
-        if quote:
-          text = "1 BTC~%s"%quote
-          grid.addWidget(QLabel(_(text)), 4, 0, 3, 0)
-        else:
-            self.gui.main_window.show_message(_("Exchange rate not available.  Please check your network connection."))
-            return
-
-        vbox.addLayout(grid)
-        vbox.addLayout(ok_cancel_buttons(d))
-
-        if not d.exec_():
-            return
-
-        fiat = str(fiat_e.text())
-
-        if str(fiat) == "" or str(fiat) == ".":
-            fiat = "0"
-
-        quote = quote[:-4]
-        btcamount = Decimal(fiat) / Decimal(quote)
-        if str(self.gui.main_window.base_unit()) == "mBTC":
-            btcamount = btcamount * 1000
-        quote = "%.8f"%btcamount
-        self.gui.main_window.amount_e.setText( quote )
-
-    def exchange_rate_button(self, grid):
-        quote_currency = self.config.get("currency", "EUR")
-        self.fiat_button = EnterButton(_(quote_currency), self.fiat_dialog)
-        grid.addWidget(self.fiat_button, 4, 3, Qt.AlignHCenter)
+    def add_fiat_edit(self):
+        self.fiat_e = AmountEdit(self.fiat_unit)
+        self.btc_e = self.win.amount_e
+        grid = self.btc_e.parent()
+        def fiat_changed():
+            try:
+                fiat_amount = Decimal(str(self.fiat_e.text()))
+            except:
+                self.btc_e.setText("")
+                return
+            exchange_rate = self.exchanger.exchange(Decimal("1.0"), self.fiat_unit())
+            if exchange_rate is not None:
+                btc_amount = fiat_amount/exchange_rate
+                self.btc_e.setAmount(int(btc_amount*Decimal(100000000)))
+        self.fiat_e.textEdited.connect(fiat_changed)
+        def btc_changed():
+            btc_amount = self.btc_e.get_amount()
+            if btc_amount is None:
+                self.fiat_e.setText("")
+                return
+            fiat_amount = self.exchanger.exchange(Decimal(btc_amount)/Decimal(100000000), self.fiat_unit())
+            if fiat_amount is not None:
+                self.fiat_e.setText("%.2f"%fiat_amount)
+        self.btc_e.textEdited.connect(btc_changed)
+        self.btc_e.frozen.connect(lambda: self.fiat_e.setFrozen(self.btc_e.isReadOnly()))
+        self.win.send_grid.addWidget(self.fiat_e, 4, 3, Qt.AlignHCenter)