1 from electrum.util import print_error
2 from urlparse import urlparse, parse_qs
3 from PyQt4.QtGui import QPushButton
4 from electrum_gui.i18n import _
11 from electrum_gui import BasePlugin
12 class Plugin(BasePlugin):
14 def __init__(self, gui):
15 BasePlugin.__init__(self, gui, 'qrscans', 'QR scans', "QR Scans.\nInstall the zbar package to enable this plugin")
17 def is_available(self):
21 proc = zbar.Processor()
23 except zbar.SystemError:
24 # Cannot open video device
29 def create_send_tab(self, grid):
30 b = QPushButton(_("Scan QR code"))
31 b.clicked.connect(self.fill_from_qr)
32 grid.addWidget(b, 1, 5)
36 proc = zbar.Processor()
44 # User closed the preview window
47 for r in proc.results:
48 if str(r.type) != 'QRCODE':
50 return parse_uri(r.data)
53 def fill_from_qr(self):
54 qrcode = self.scan_qr()
55 if 'address' in qrcode:
56 self.gui.payto_e.setText(qrcode['address'])
57 if 'amount' in qrcode:
58 self.gui.amount_e.setText(str(qrcode['amount']))
60 self.gui.message_e.setText(qrcode['label'])
61 if 'message' in qrcode:
62 self.gui.message_e.setText("%s (%s)" % (self.gui.message_e.text(), qrcode['message']))
69 # It's just an address (not BIP21)
70 return {'address': uri}
73 # Workaround for urlparse, it don't handle bitcoin: URI properly
74 uri = uri.replace(':', '://')
77 result = {'address': uri.netloc}
79 if uri.path.startswith('?'):
80 params = parse_qs(uri.path[1:])
82 params = parse_qs(uri.path)
84 for k,v in params.items():
85 if k in ('amount', 'label', 'message'):
94 if __name__ == '__main__':
97 assert(parse_uri('1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
98 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
100 assert(parse_uri('bitcoin://1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
101 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
103 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
104 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
106 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10') ==
107 {'amount': '10', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
109 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10&label=slush&message=Small%20tip%20to%20slush') ==
110 {'amount': '10', 'label': 'slush', 'message': 'Small tip to slush', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})