1 from electrum.util import print_error
2 from urlparse import urlparse, parse_qs
3 from PyQt4.QtGui import QPushButton, QMessageBox, QDialog, QVBoxLayout, QHBoxLayout, QGridLayout, QLabel, QLineEdit, QComboBox
4 from PyQt4.QtCore import Qt
6 from electrum.i18n import _
9 from electrum import Transaction
10 from electrum.bitcoin import MIN_RELAY_TX_FEE, is_valid
11 from electrum_gui.qt.qrcodewidget import QRCodeWidget
12 from electrum import bmp
13 from electrum_gui.qt import HelpButton, EnterButton
21 from electrum import BasePlugin
22 class Plugin(BasePlugin):
24 def fullname(self): return 'QR scans'
26 def description(self): return "QR Scans.\nInstall the zbar package to enable this plugin.\nOn linux, type: 'apt-get install python-zbar'"
28 def __init__(self, gui, name):
29 BasePlugin.__init__(self, gui, name)
30 self._is_available = self._init()
36 proc = zbar.Processor()
37 proc.init(video_device=self.video_device())
38 except zbar.SystemError:
39 # Cannot open video device
46 self.win = self.gui.main_window
47 self.win.raw_transaction_menu.addAction(_("&From QR code"), self.read_raw_qr)
48 b = QPushButton(_("Scan QR code"))
49 b.clicked.connect(self.fill_from_qr)
50 self.win.send_grid.addWidget(b, 1, 5)
51 self.win.send_grid.setColumnStretch(5, 0)
52 self.win.send_grid.setColumnStretch(6, 1)
54 def init_transaction_dialog(self, dialog, buttons):
55 b = QPushButton(_("Show QR code"))
56 b.clicked.connect(lambda: self.show_raw_qr(dialog.tx))
57 buttons.insertWidget(1,b)
59 def is_available(self):
60 return self._is_available
63 proc = zbar.Processor()
65 proc.init(video_device=self.video_device())
66 except zbar.SystemError, e:
67 QMessageBox.warning(self.win, _('Error'), _(e), _('OK'))
76 # User closed the preview window
79 for r in proc.results:
80 if str(r.type) != 'QRCODE':
84 def show_raw_qr(self, tx):
86 json_text = json.dumps(tx.as_dict()).replace(' ', '')
87 self.show_tx_qrcode(json_text, 'Unsigned Transaction')
88 except Exception as e:
89 self.win.show_message(str(e))
91 def show_tx_qrcode(self, data, title):
95 d.setWindowTitle(title)
96 d.setMinimumSize(250, 525)
98 qrw = QRCodeWidget(data)
99 vbox.addWidget(qrw, 0)
104 filename = "qrcode.bmp"
105 electrum_gui.bmp.save_qrcode(qrw.qr, filename)
106 QMessageBox.information(None, _('Message'), _("QR code saved to file") + " " + filename, _('OK'))
108 b = QPushButton(_("Save"))
110 b.clicked.connect(print_qr)
112 b = QPushButton(_("Close"))
114 b.clicked.connect(d.accept)
117 vbox.addLayout(hbox, 1)
121 def read_raw_qr(self):
122 qrcode = self.scan_qr()
125 tx = self.win.tx_from_text(qrcode)
128 self.win.show_transaction(tx)
131 def fill_from_qr(self):
132 qrcode = parse_uri(self.scan_qr())
136 if 'address' in qrcode:
137 self.win.payto_e.setText(qrcode['address'])
138 if 'amount' in qrcode:
139 self.win.amount_e.setText(str(qrcode['amount']))
140 if 'label' in qrcode:
141 self.win.message_e.setText(qrcode['label'])
142 if 'message' in qrcode:
143 self.win.message_e.setText("%s (%s)" % (self.win.message_e.text(), qrcode['message']))
145 def video_device(self):
146 device = self.config.get("video_device", "default")
147 if device == 'default':
151 def requires_settings(self):
154 def settings_widget(self, window):
155 return EnterButton(_('Settings'), self.settings_dialog)
157 def _find_system_cameras(self):
158 device_root = "/sys/class/video4linux"
159 devices = {} # Name -> device
160 if os.path.exists(device_root):
161 for device in os.listdir(device_root):
162 name = open(os.path.join(device_root, device, 'name')).read()
163 devices[name] = os.path.join("/dev",device)
166 def settings_dialog(self):
167 system_cameras = self._find_system_cameras()
170 layout = QGridLayout(d)
171 layout.addWidget(QLabel("Choose a video device:"),0,0)
173 # Create a combo box with the available video devices:
176 # on change trigger for video device selection, makes the
177 # manual device selection only appear when needed:
179 combo_text = str(combo.itemText(x))
180 combo_data = combo.itemData(x)
181 if combo_text == "Manually specify a device":
182 custom_device_label.setVisible(True)
183 self.video_device_edit.setVisible(True)
184 if self.config.get("video_device") == "default":
185 self.video_device_edit.setText("")
187 self.video_device_edit.setText(self.config.get("video_device"))
189 custom_device_label.setVisible(False)
190 self.video_device_edit.setVisible(False)
191 self.video_device_edit.setText(combo_data.toString())
193 # on save trigger for the video device selection window,
194 # stores the chosen video device on close.
196 device = str(self.video_device_edit.text())
197 self.config.set_key("video_device", device)
200 custom_device_label = QLabel("Video device: ")
201 custom_device_label.setVisible(False)
202 layout.addWidget(custom_device_label,1,0)
203 self.video_device_edit = QLineEdit()
204 self.video_device_edit.setVisible(False)
205 layout.addWidget(self.video_device_edit, 1,1,2,2)
206 combo.currentIndexChanged.connect(on_change)
208 combo.addItem("Default","default")
209 for camera, device in system_cameras.items():
210 combo.addItem(camera, device)
211 combo.addItem("Manually specify a device",self.config.get("video_device"))
213 # Populate the previously chosen device:
214 index = combo.findData(self.config.get("video_device"))
215 combo.setCurrentIndex(index)
217 layout.addWidget(combo,0,1)
219 self.accept = QPushButton(_("Done"))
220 self.accept.clicked.connect(on_save)
221 layout.addWidget(self.accept,4,2)
235 # It's just an address (not BIP21)
236 return {'address': uri}
239 # Workaround for urlparse, it don't handle bitcoin: URI properly
240 uri = uri.replace(':', '://')
243 result = {'address': uri.netloc}
245 if uri.query.startswith('?'):
246 params = parse_qs(uri.query[1:])
248 params = parse_qs(uri.query)
250 for k,v in params.items():
251 if k in ('amount', 'label', 'message'):
260 if __name__ == '__main__':
263 assert(parse_uri('1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
264 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
266 assert(parse_uri('bitcoin://1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
267 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
269 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
270 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
272 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10') ==
273 {'amount': '10', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
275 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10&label=slush&message=Small%20tip%20to%20slush') ==
276 {'amount': '10', 'label': 'slush', 'message': 'Small tip to slush', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})