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):
50 b = QPushButton(_("Show QR code"))
51 b.clicked.connect(lambda: self.show_raw_qr(dialog.tx))
52 buttons.insertWidget(1,b)
54 def is_available(self):
55 return self._is_available
57 def scan_qr_hook(self, func):
64 proc = zbar.Processor()
66 proc.init(video_device=self.video_device())
67 except zbar.SystemError, e:
68 QMessageBox.warning(self.win, _('Error'), _(e), _('OK'))
77 # User closed the preview window
80 for r in proc.results:
81 if str(r.type) != 'QRCODE':
85 def show_raw_qr(self, tx):
87 json_text = json.dumps(tx.as_dict()).replace(' ', '')
88 self.win.show_qrcode(json_text, 'Transaction')
89 except Exception as e:
90 self.win.show_message(str(e))
93 def read_raw_qr(self):
94 qrcode = self.scan_qr()
97 tx = self.win.tx_from_text(qrcode)
100 self.win.show_transaction(tx)
102 def video_device(self):
103 device = self.config.get("video_device", "default")
104 if device == 'default':
108 def requires_settings(self):
111 def settings_widget(self, window):
112 return EnterButton(_('Settings'), self.settings_dialog)
114 def _find_system_cameras(self):
115 device_root = "/sys/class/video4linux"
116 devices = {} # Name -> device
117 if os.path.exists(device_root):
118 for device in os.listdir(device_root):
119 name = open(os.path.join(device_root, device, 'name')).read()
120 devices[name] = os.path.join("/dev",device)
123 def settings_dialog(self):
124 system_cameras = self._find_system_cameras()
127 layout = QGridLayout(d)
128 layout.addWidget(QLabel("Choose a video device:"),0,0)
130 # Create a combo box with the available video devices:
133 # on change trigger for video device selection, makes the
134 # manual device selection only appear when needed:
136 combo_text = str(combo.itemText(x))
137 combo_data = combo.itemData(x)
138 if combo_text == "Manually specify a device":
139 custom_device_label.setVisible(True)
140 self.video_device_edit.setVisible(True)
141 if self.config.get("video_device") == "default":
142 self.video_device_edit.setText("")
144 self.video_device_edit.setText(self.config.get("video_device"))
146 custom_device_label.setVisible(False)
147 self.video_device_edit.setVisible(False)
148 self.video_device_edit.setText(combo_data.toString())
150 # on save trigger for the video device selection window,
151 # stores the chosen video device on close.
153 device = str(self.video_device_edit.text())
154 self.config.set_key("video_device", device)
157 custom_device_label = QLabel("Video device: ")
158 custom_device_label.setVisible(False)
159 layout.addWidget(custom_device_label,1,0)
160 self.video_device_edit = QLineEdit()
161 self.video_device_edit.setVisible(False)
162 layout.addWidget(self.video_device_edit, 1,1,2,2)
163 combo.currentIndexChanged.connect(on_change)
165 combo.addItem("Default","default")
166 for camera, device in system_cameras.items():
167 combo.addItem(camera, device)
168 combo.addItem("Manually specify a device",self.config.get("video_device"))
170 # Populate the previously chosen device:
171 index = combo.findData(self.config.get("video_device"))
172 combo.setCurrentIndex(index)
174 layout.addWidget(combo,0,1)
176 self.accept = QPushButton(_("Done"))
177 self.accept.clicked.connect(on_save)
178 layout.addWidget(self.accept,4,2)