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 _
15 gui.set_hook('create_send_tab', create_send_tab)
17 gui.unset_hook('create_send_tab', create_send_tab)
20 return 'QR scans', "QR Scans.\nInstall the zbar package to enable this plugin"
34 proc = zbar.Processor()
36 except zbar.SystemError:
37 # Cannot open video device
43 proc = zbar.Processor()
51 # User closed the preview window
54 for r in proc.results:
55 if str(r.type) != 'QRCODE':
58 return parse_uri(r.data)
62 # It's just an address (not BIP21)
63 return {'address': uri}
66 # Workaround for urlparse, it don't handle bitcoin: URI properly
67 uri = uri.replace(':', '://')
70 result = {'address': uri.netloc}
72 if uri.path.startswith('?'):
73 params = parse_qs(uri.path[1:])
75 params = parse_qs(uri.path)
77 for k,v in params.items():
78 if k in ('amount', 'label', 'message'):
85 def fill_from_qr(self):
87 if 'address' in qrcode:
88 self.payto_e.setText(qrcode['address'])
89 if 'amount' in qrcode:
90 self.amount_e.setText(str(qrcode['amount']))
92 self.message_e.setText(qrcode['label'])
93 if 'message' in qrcode:
94 self.message_e.setText("%s (%s)" % (self.message_e.text(), qrcode['message']))
97 def create_send_tab(gui, grid):
99 b = QPushButton(_("Scan QR code"))
100 b.clicked.connect(lambda: fill_from_qr(gui))
101 grid.addWidget(b, 1, 5)
105 if __name__ == '__main__':
108 assert(parse_uri('1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
109 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
111 assert(parse_uri('bitcoin://1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
112 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
114 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
115 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
117 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10') ==
118 {'amount': '10', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
120 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10&label=slush&message=Small%20tip%20to%20slush') ==
121 {'amount': '10', 'label': 'slush', 'message': 'Small tip to slush', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})