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)
49 def init_transaction_dialog(self, dialog, buttons):
52 b = QPushButton(_("Show QR code"))
53 b.clicked.connect(self.show_raw_qr)
54 buttons.insertWidget(1,b)
56 def load_wallet(self, wallet):
57 b = QPushButton(_("Scan QR code"))
58 b.clicked.connect(self.fill_from_qr)
59 self.send_tab_grid.addWidget(b, 1, 5)
60 self.send_tab_grid.setColumnStretch(5, 0)
61 self.send_tab_grid.setColumnStretch(6, 1)
63 def is_available(self):
64 return self._is_available
66 def create_send_tab(self, grid):
67 self.send_tab_grid = grid
70 proc = zbar.Processor()
72 proc.init(video_device=self.video_device())
73 except zbar.SystemError, e:
74 QMessageBox.warning(self.win, _('Error'), _(e), _('OK'))
83 # User closed the preview window
86 for r in proc.results:
87 if str(r.type) != 'QRCODE':
91 def show_raw_qr(self):
92 r = self.win.read_send_tab()
96 outputs, fee, label, coins = r
98 tx = self.win.wallet.make_unsigned_transaction(outputs, fee, None, None, coins)
99 except Exception as e:
100 self.win.show_message(str(e))
103 if tx.requires_fee(self.win.wallet.verifier) and fee < MIN_RELAY_TX_FEE:
104 QMessageBox.warning(self.win, _('Error'), _("This transaction requires a higher fee, or it will not be propagated by the network."), _('OK'))
115 except Exception as e:
116 self.win.show_message(str(e))
119 json_text = json.dumps(tx.as_dict()).replace(' ', '')
120 self.show_tx_qrcode(json_text, 'Unsigned Transaction')
121 except Exception as e:
122 self.win.show_message(str(e))
124 def show_tx_qrcode(self, data, title):
126 d = QDialog(self.win)
128 d.setWindowTitle(title)
129 d.setMinimumSize(250, 525)
131 qrw = QRCodeWidget(data)
132 vbox.addWidget(qrw, 0)
137 filename = "qrcode.bmp"
138 electrum_gui.bmp.save_qrcode(qrw.qr, filename)
139 QMessageBox.information(None, _('Message'), _("QR code saved to file") + " " + filename, _('OK'))
141 b = QPushButton(_("Save"))
143 b.clicked.connect(print_qr)
145 b = QPushButton(_("Close"))
147 b.clicked.connect(d.accept)
150 vbox.addLayout(hbox, 1)
154 def read_raw_qr(self):
155 qrcode = self.scan_qr()
158 tx = self.win.tx_from_text(qrcode)
161 self.win.show_transaction(tx)
164 def fill_from_qr(self):
165 qrcode = parse_uri(self.scan_qr())
169 if 'address' in qrcode:
170 self.win.payto_e.setText(qrcode['address'])
171 if 'amount' in qrcode:
172 self.win.amount_e.setText(str(qrcode['amount']))
173 if 'label' in qrcode:
174 self.win.message_e.setText(qrcode['label'])
175 if 'message' in qrcode:
176 self.win.message_e.setText("%s (%s)" % (self.win.message_e.text(), qrcode['message']))
178 def video_device(self):
179 device = self.config.get("video_device", "default")
180 if device == 'default':
184 def requires_settings(self):
187 def settings_widget(self, window):
188 return EnterButton(_('Settings'), self.settings_dialog)
190 def _find_system_cameras(self):
191 device_root = "/sys/class/video4linux"
192 devices = {} # Name -> device
193 if os.path.exists(device_root):
194 for device in os.listdir(device_root):
195 name = open(os.path.join(device_root, device, 'name')).read()
196 devices[name] = os.path.join("/dev",device)
199 def settings_dialog(self):
200 system_cameras = self._find_system_cameras()
203 layout = QGridLayout(d)
204 layout.addWidget(QLabel("Choose a video device:"),0,0)
206 # Create a combo box with the available video devices:
209 # on change trigger for video device selection, makes the
210 # manual device selection only appear when needed:
212 combo_text = str(combo.itemText(x))
213 combo_data = combo.itemData(x)
214 if combo_text == "Manually specify a device":
215 custom_device_label.setVisible(True)
216 self.video_device_edit.setVisible(True)
217 if self.config.get("video_device") == "default":
218 self.video_device_edit.setText("")
220 self.video_device_edit.setText(self.config.get("video_device"))
222 custom_device_label.setVisible(False)
223 self.video_device_edit.setVisible(False)
224 self.video_device_edit.setText(combo_data.toString())
226 # on save trigger for the video device selection window,
227 # stores the chosen video device on close.
229 device = str(self.video_device_edit.text())
230 self.config.set_key("video_device", device)
233 custom_device_label = QLabel("Video device: ")
234 custom_device_label.setVisible(False)
235 layout.addWidget(custom_device_label,1,0)
236 self.video_device_edit = QLineEdit()
237 self.video_device_edit.setVisible(False)
238 layout.addWidget(self.video_device_edit, 1,1,2,2)
239 combo.currentIndexChanged.connect(on_change)
241 combo.addItem("Default","default")
242 for camera, device in system_cameras.items():
243 combo.addItem(camera, device)
244 combo.addItem("Manually specify a device",self.config.get("video_device"))
246 # Populate the previously chosen device:
247 index = combo.findData(self.config.get("video_device"))
248 combo.setCurrentIndex(index)
250 layout.addWidget(combo,0,1)
252 self.accept = QPushButton(_("Done"))
253 self.accept.clicked.connect(on_save)
254 layout.addWidget(self.accept,4,2)
268 # It's just an address (not BIP21)
269 return {'address': uri}
272 # Workaround for urlparse, it don't handle bitcoin: URI properly
273 uri = uri.replace(':', '://')
276 result = {'address': uri.netloc}
278 if uri.query.startswith('?'):
279 params = parse_qs(uri.query[1:])
281 params = parse_qs(uri.query)
283 for k,v in params.items():
284 if k in ('amount', 'label', 'message'):
293 if __name__ == '__main__':
296 assert(parse_uri('1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
297 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
299 assert(parse_uri('bitcoin://1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
300 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
302 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
303 {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
305 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10') ==
306 {'amount': '10', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
308 assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10&label=slush&message=Small%20tip%20to%20slush') ==
309 {'amount': '10', 'label': 'slush', 'message': 'Small tip to slush', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})