X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=plugins%2Fqrscanner.py;h=0ff1fe936c7c83959800a0e3594f3853b547000a;hb=81d1e67253c8ca581e821a6cd9e5ee1502fffd08;hp=a7e41f8b7f88bb92132b05c3251b4ab67e24b777;hpb=8685d749a3b7329ca318aa1ce1d2c9c440b26853;p=electrum-nvc.git diff --git a/plugins/qrscanner.py b/plugins/qrscanner.py index a7e41f8..0ff1fe9 100644 --- a/plugins/qrscanner.py +++ b/plugins/qrscanner.py @@ -35,6 +35,8 @@ class Plugin(BasePlugin): try: proc = zbar.Processor() proc.init(video_device=self.video_device()) + except zbar.UnsupportedError: + return False except zbar.SystemError: # Cannot open video device pass @@ -44,22 +46,14 @@ class Plugin(BasePlugin): def init(self): self.win = self.gui.main_window - self.win.raw_transaction_menu.addAction(_("&From QR code"), self.read_raw_qr) - b = QPushButton(_("Scan QR code")) - b.clicked.connect(self.fill_from_qr) - self.win.send_grid.addWidget(b, 1, 5) - self.win.send_grid.setColumnStretch(5, 0) - self.win.send_grid.setColumnStretch(6, 1) - - def init_transaction_dialog(self, dialog, buttons): - b = QPushButton(_("Show QR code")) - b.clicked.connect(self.show_raw_qr) - buttons.insertWidget(1,b) def is_available(self): return self._is_available - def scan_qr(self): + def is_enabled(self): + return True + + def scan_qr_hook(self): proc = zbar.Processor() try: proc.init(video_device=self.video_device()) @@ -81,93 +75,7 @@ class Plugin(BasePlugin): continue return r.data - def show_raw_qr(self): - r = self.win.read_send_tab() - if not r: - return - - outputs, fee, label, coins = r - try: - tx = self.win.wallet.make_unsigned_transaction(outputs, fee, None, None, coins) - except Exception as e: - self.win.show_message(str(e)) - return - - if tx.requires_fee(self.win.wallet.verifier) and fee < MIN_RELAY_TX_FEE: - QMessageBox.warning(self.win, _('Error'), _("This transaction requires a higher fee, or it will not be propagated by the network."), _('OK')) - return - - try: - out = { - "hex" : tx.hash(), - "complete" : "false" - } - - input_info = [] - - except Exception as e: - self.win.show_message(str(e)) - - try: - json_text = json.dumps(tx.as_dict()).replace(' ', '') - self.show_tx_qrcode(json_text, 'Unsigned Transaction') - except Exception as e: - self.win.show_message(str(e)) - - def show_tx_qrcode(self, data, title): - if not data: return - d = QDialog(self.win) - d.setModal(1) - d.setWindowTitle(title) - d.setMinimumSize(250, 525) - vbox = QVBoxLayout() - qrw = QRCodeWidget(data) - vbox.addWidget(qrw, 0) - hbox = QHBoxLayout() - hbox.addStretch(1) - - def print_qr(self): - filename = "qrcode.bmp" - electrum_gui.bmp.save_qrcode(qrw.qr, filename) - QMessageBox.information(None, _('Message'), _("QR code saved to file") + " " + filename, _('OK')) - - b = QPushButton(_("Save")) - hbox.addWidget(b) - b.clicked.connect(print_qr) - - b = QPushButton(_("Close")) - hbox.addWidget(b) - b.clicked.connect(d.accept) - b.setDefault(True) - - vbox.addLayout(hbox, 1) - d.setLayout(vbox) - d.exec_() - - def read_raw_qr(self): - qrcode = self.scan_qr() - if not qrcode: - return - tx = self.win.tx_from_text(qrcode) - if not tx: - return - self.win.show_transaction(tx) - - def fill_from_qr(self): - qrcode = parse_uri(self.scan_qr()) - if not qrcode: - return - - if 'address' in qrcode: - self.win.payto_e.setText(qrcode['address']) - if 'amount' in qrcode: - self.win.amount_e.setText(str(qrcode['amount'])) - if 'label' in qrcode: - self.win.message_e.setText(qrcode['label']) - if 'message' in qrcode: - self.win.message_e.setText("%s (%s)" % (self.win.message_e.text(), qrcode['message'])) - def video_device(self): device = self.config.get("video_device", "default") if device == 'default': @@ -210,7 +118,7 @@ class Plugin(BasePlugin): if self.config.get("video_device") == "default": self.video_device_edit.setText("") else: - self.video_device_edit.setText(self.config.get("video_device")) + self.video_device_edit.setText(self.config.get("video_device",'')) else: custom_device_label.setVisible(False) self.video_device_edit.setVisible(False) @@ -250,55 +158,3 @@ class Plugin(BasePlugin): return True else: return False - - - -def parse_uri(uri): - if not uri: - return {} - - if ':' not in uri: - # It's just an address (not BIP21) - return {'address': uri} - - if '//' not in uri: - # Workaround for urlparse, it don't handle bitcoin: URI properly - uri = uri.replace(':', '://') - - uri = urlparse(uri) - result = {'address': uri.netloc} - - if uri.query.startswith('?'): - params = parse_qs(uri.query[1:]) - else: - params = parse_qs(uri.query) - - for k,v in params.items(): - if k in ('amount', 'label', 'message'): - result[k] = v[0] - - return result - - - - - -if __name__ == '__main__': - # Run some tests - - assert(parse_uri('1Marek48fwU7mugmSe186do2QpUkBnpzSN') == - {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'}) - - assert(parse_uri('bitcoin://1Marek48fwU7mugmSe186do2QpUkBnpzSN') == - {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'}) - - assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN') == - {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'}) - - assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10') == - {'amount': '10', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'}) - - assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10&label=slush&message=Small%20tip%20to%20slush') == - {'amount': '10', 'label': 'slush', 'message': 'Small tip to slush', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'}) - -