X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=plugins%2Fqrscanner.py;h=0ff1fe936c7c83959800a0e3594f3853b547000a;hb=81d1e67253c8ca581e821a6cd9e5ee1502fffd08;hp=c3044642d445fe3992094456612560f46fa9b593;hpb=111611a08b5fd13f2e858495632cf0eff65f634b;p=electrum-nvc.git diff --git a/plugins/qrscanner.py b/plugins/qrscanner.py index c304464..0ff1fe9 100644 --- a/plugins/qrscanner.py +++ b/plugins/qrscanner.py @@ -1,13 +1,16 @@ from electrum.util import print_error from urlparse import urlparse, parse_qs -from PyQt4.QtGui import QPushButton, QMessageBox, QDialog, QVBoxLayout, QHBoxLayout, QGridLayout, QLabel +from PyQt4.QtGui import QPushButton, QMessageBox, QDialog, QVBoxLayout, QHBoxLayout, QGridLayout, QLabel, QLineEdit, QComboBox from PyQt4.QtCore import Qt -from electrum_gui.i18n import _ +from electrum.i18n import _ import re -from electrum.bitcoin import MIN_RELAY_TX_FEE, Transaction, is_valid -from electrum_gui.qrcodewidget import QRCodeWidget -import electrum_gui.bmp +import os +from electrum import Transaction +from electrum.bitcoin import MIN_RELAY_TX_FEE, is_valid +from electrum_gui.qt.qrcodewidget import QRCodeWidget +from electrum import bmp +from electrum_gui.qt import HelpButton, EnterButton import json try: @@ -15,12 +18,12 @@ try: except ImportError: zbar = None -from electrum_gui import BasePlugin +from electrum import BasePlugin class Plugin(BasePlugin): def fullname(self): return 'QR scans' - def description(self): return "QR Scans.\nInstall the zbar package (http://zbar.sourceforge.net/download.html) to enable this plugin" + def description(self): return "QR Scans.\nInstall the zbar package to enable this plugin.\nOn linux, type: 'apt-get install python-zbar'" def __init__(self, gui, name): BasePlugin.__init__(self, gui, name) @@ -31,41 +34,39 @@ class Plugin(BasePlugin): return False try: proc = zbar.Processor() - proc.init() + proc.init(video_device=self.video_device()) + except zbar.UnsupportedError: + return False except zbar.SystemError: # Cannot open video device - return False + pass + #return False return True + def init(self): + self.win = self.gui.main_window + def is_available(self): return self._is_available - def create_send_tab(self, grid): - b = QPushButton(_("Scan QR code")) - b.clicked.connect(self.fill_from_qr) - grid.addWidget(b, 1, 5) - b2 = QPushButton(_("Scan TxQR")) - b2.clicked.connect(self.read_raw_qr) - - if not self.gui.wallet.seed: - b3 = QPushButton(_("Show unsigned TxQR")) - b3.clicked.connect(self.show_raw_qr) - grid.addWidget(b3, 7, 1) - grid.addWidget(b2, 7, 2) - else: - grid.addWidget(b2, 7, 1) - + def is_enabled(self): + return True - def scan_qr(self): + def scan_qr_hook(self): proc = zbar.Processor() - proc.init() + try: + proc.init(video_device=self.video_device()) + except zbar.SystemError, e: + QMessageBox.warning(self.win, _('Error'), _(e), _('OK')) + return + proc.visible = True while True: try: proc.process_one() - except: + except Exception: # User closed the preview window return {} @@ -74,217 +75,86 @@ class Plugin(BasePlugin): continue return r.data - def show_raw_qr(self): - r = unicode( self.gui.payto_e.text() ) - r = r.strip() - - # label or alias, with address in brackets - m = re.match('(.*?)\s*\<([1-9A-HJ-NP-Za-km-z]{26,})\>', r) - to_address = m.group(2) if m else r - if not is_valid(to_address): - QMessageBox.warning(self.gui, _('Error'), _('Invalid Bitcoin Address') + ':\n' + to_address, _('OK')) - return + def video_device(self): + device = self.config.get("video_device", "default") + if device == 'default': + device = '' + return device - try: - amount = self.gui.read_amount(unicode( self.gui.amount_e.text())) - except: - QMessageBox.warning(self.gui, _('Error'), _('Invalid Amount'), _('OK')) - return - try: - fee = self.gui.read_amount(unicode( self.gui.fee_e.text())) - except: - QMessageBox.warning(self.gui, _('Error'), _('Invalid Fee'), _('OK')) - return - - try: - tx = self.gui.wallet.mktx( [(to_address, amount)], None, fee, account=self.gui.current_account) - except BaseException, e: - self.gui.show_message(str(e)) - return - - if tx.requires_fee(self.gui.wallet.verifier) and fee < MIN_RELAY_TX_FEE: - QMessageBox.warning(self.gui, _('Error'), _("This transaction requires a higher fee, or it will not be propagated by the network."), _('OK')) - return + def requires_settings(self): + return True - try: - out = { - "hex" : tx.hash(), - "complete" : "false" - } + def settings_widget(self, window): + return EnterButton(_('Settings'), self.settings_dialog) - input_info = [] - - except BaseException, e: - self.gui.show_message(str(e)) - - try: - json_text = json.dumps(tx.as_dict()).replace(' ', '') - self.show_tx_qrcode(json_text, 'Unsigned Transaction') - except BaseException, e: - self.gui.show_message(str(e)) - - def show_tx_qrcode(self, data, title): - if not data: return - d = QDialog(self.gui) - 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 qrcode: - tx_dict = self.gui.tx_dict_from_text(qrcode) - if tx_dict: - self.create_transaction_details_window(tx_dict) - - - def create_transaction_details_window(self, tx_dict): - tx = Transaction(tx_dict["hex"]) - - dialog = QDialog(self.gui) - dialog.setMinimumWidth(500) - dialog.setWindowTitle(_('Process Offline transaction')) - dialog.setModal(1) - - l = QGridLayout() - dialog.setLayout(l) - - l.addWidget(QLabel(_("Transaction status:")), 3,0) - l.addWidget(QLabel(_("Actions")), 4,0) - - if tx_dict["complete"] == False: - l.addWidget(QLabel(_("Unsigned")), 3,1) - if self.gui.wallet.seed : - b = QPushButton("Sign transaction") - input_info = json.loads(tx_dict["input_info"]) - b.clicked.connect(lambda: self.sign_raw_transaction(tx, input_info, dialog)) - l.addWidget(b, 4, 1) + def _find_system_cameras(self): + device_root = "/sys/class/video4linux" + devices = {} # Name -> device + if os.path.exists(device_root): + for device in os.listdir(device_root): + name = open(os.path.join(device_root, device, 'name')).read() + devices[name] = os.path.join("/dev",device) + return devices + + def settings_dialog(self): + system_cameras = self._find_system_cameras() + + d = QDialog() + layout = QGridLayout(d) + layout.addWidget(QLabel("Choose a video device:"),0,0) + + # Create a combo box with the available video devices: + combo = QComboBox() + + # on change trigger for video device selection, makes the + # manual device selection only appear when needed: + def on_change(x): + combo_text = str(combo.itemText(x)) + combo_data = combo.itemData(x) + if combo_text == "Manually specify a device": + custom_device_label.setVisible(True) + self.video_device_edit.setVisible(True) + if self.config.get("video_device") == "default": + self.video_device_edit.setText("") + else: + self.video_device_edit.setText(self.config.get("video_device",'')) else: - l.addWidget(QLabel(_("Wallet is de-seeded, can't sign.")), 4,1) + custom_device_label.setVisible(False) + self.video_device_edit.setVisible(False) + self.video_device_edit.setText(combo_data.toString()) + + # on save trigger for the video device selection window, + # stores the chosen video device on close. + def on_save(): + device = str(self.video_device_edit.text()) + self.config.set_key("video_device", device) + d.accept() + + custom_device_label = QLabel("Video device: ") + custom_device_label.setVisible(False) + layout.addWidget(custom_device_label,1,0) + self.video_device_edit = QLineEdit() + self.video_device_edit.setVisible(False) + layout.addWidget(self.video_device_edit, 1,1,2,2) + combo.currentIndexChanged.connect(on_change) + + combo.addItem("Default","default") + for camera, device in system_cameras.items(): + combo.addItem(camera, device) + combo.addItem("Manually specify a device",self.config.get("video_device")) + + # Populate the previously chosen device: + index = combo.findData(self.config.get("video_device")) + combo.setCurrentIndex(index) + + layout.addWidget(combo,0,1) + + self.accept = QPushButton(_("Done")) + self.accept.clicked.connect(on_save) + layout.addWidget(self.accept,4,2) + + if d.exec_(): + return True else: - l.addWidget(QLabel(_("Signed")), 3,1) - b = QPushButton("Broadcast transaction") - b.clicked.connect(lambda: self.gui.send_raw_transaction(tx, dialog)) - l.addWidget(b,4,1) - - l.addWidget( self.gui.generate_transaction_information_widget(tx), 0,0,2,3) - closeButton = QPushButton(_("Close")) - closeButton.clicked.connect(lambda: dialog.done(0)) - l.addWidget(closeButton, 4,2) - - dialog.exec_() - - def do_protect(self, func, args): - if self.gui.wallet.use_encryption: - password = self.gui.password_dialog() - if not password: - return - else: - password = None - - if args != (False,): - args = (self,) + args + (password,) - else: - args = (self,password) - apply( func, args) - - def protected(func): - return lambda s, *args: s.do_protect(func, args) - - @protected - def sign_raw_transaction(self, tx, input_info, dialog ="", password = ""): - try: - self.gui.wallet.signrawtransaction(tx, input_info, [], password) - txtext = json.dumps(tx.as_dict()).replace(' ', '') - self.show_tx_qrcode(txtext, 'Signed Transaction') - except BaseException, e: - self.gui.show_message(str(e)) - - - def fill_from_qr(self): - qrcode = parse_uri(self.scan_qr()) - if not qrcode: - return - - if 'address' in qrcode: - self.gui.payto_e.setText(qrcode['address']) - if 'amount' in qrcode: - self.gui.amount_e.setText(str(qrcode['amount'])) - if 'label' in qrcode: - self.gui.message_e.setText(qrcode['label']) - if 'message' in qrcode: - self.gui.message_e.setText("%s (%s)" % (self.gui.message_e.text(), qrcode['message'])) - - - - -def parse_uri(uri): - 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.path.startswith('?'): - params = parse_qs(uri.path[1:]) - else: - params = parse_qs(uri.path) - - 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'}) - - + return False