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 (http://zbar.sourceforge.net/download.html) to enable this plugin")
16 self._is_available = self._init()
22 proc = zbar.Processor()
24 except zbar.SystemError:
25 # Cannot open video device
30 def is_available(self):
31 return self._is_available
33 def create_send_tab(self, grid):
34 b = QPushButton(_("Scan QR code"))
35 b.clicked.connect(self.fill_from_qr)
36 grid.addWidget(b, 1, 5)
40 proc = zbar.Processor()
48 # User closed the preview window
51 for r in proc.results:
52 if str(r.type) != 'QRCODE':
54 return parse_uri(r.data)
57 def fill_from_qr(self):
58 qrcode = self.scan_qr()
59 if 'address' in qrcode:
60 self.gui.payto_e.setText(qrcode['address'])
61 if 'amount' in qrcode:
62 self.gui.amount_e.setText(str(qrcode['amount']))
64 self.gui.message_e.setText(qrcode['label'])
65 if 'message' in qrcode:
66 self.gui.message_e.setText("%s (%s)" % (self.gui.message_e.text(), qrcode['message']))
73 # It's just an address (not BIP21)
74 return {'address': uri}
77 # Workaround for urlparse, it don't handle bitcoin: URI properly
78 uri = uri.replace(':', '://')
81 result = {'address': uri.netloc}
83 if uri.path.startswith('?'):
84 params = parse_qs(uri.path[1:])
86 params = parse_qs(uri.path)
88 for k,v in params.items():
89 if k in ('amount', 'label', 'message'):
98 if __name__ == '__main__':
101 assert(parse_uri('1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
102 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
104 assert(parse_uri('bitcoin://1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
105 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
107 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
108 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
110 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10') ==
111 {'amount': '10', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
113 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10&label=slush&message=Small%20tip%20to%20slush') ==
114 {'amount': '10', 'label': 'slush', 'message': 'Small tip to slush', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})