1 from electrum.util import print_error
2 from urlparse import urlparse, parse_qs
13 gui.set_hook('create_send_tab', create_send_tab)
15 gui.unset_hook('create_send_tab', create_send_tab)
18 return 'QR scans', "QR Scans.\nInstall the zbar package to enable this plugin"
32 proc = zbar.Processor()
34 except zbar.SystemError:
35 # Cannot open video device
41 proc = zbar.Processor()
49 # User closed the preview window
52 for r in proc.results:
53 if str(r.type) != 'QRCODE':
56 return parse_uri(r.data)
60 # It's just an address (not BIP21)
61 return {'address': uri}
64 # Workaround for urlparse, it don't handle bitcoin: URI properly
65 uri = uri.replace(':', '://')
68 result = {'address': uri.netloc}
70 if uri.path.startswith('?'):
71 params = parse_qs(uri.path[1:])
73 params = parse_qs(uri.path)
75 for k,v in params.items():
76 if k in ('amount', 'label', 'message'):
83 def fill_from_qr(self):
85 if 'address' in qrcode:
86 self.payto_e.setText(qrcode['address'])
87 if 'amount' in qrcode:
88 self.amount_e.setText(str(qrcode['amount']))
90 self.message_e.setText(qrcode['label'])
91 if 'message' in qrcode:
92 self.message_e.setText("%s (%s)" % (self.message_e.text(), qrcode['message']))
95 def create_send_tab(gui, grid):
97 b = QPushButton(_("Scan QR code"))
98 b.clicked.connect(lambda: fill_from_qr(gui))
99 grid.addWidget(b, 1, 5)
103 if __name__ == '__main__':
106 assert(parse_uri('1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
107 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
109 assert(parse_uri('bitcoin://1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
110 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
112 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
113 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
115 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10') ==
116 {'amount': '10', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
118 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10&label=slush&message=Small%20tip%20to%20slush') ==
119 {'amount': '10', 'label': 'slush', 'message': 'Small tip to slush', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})