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.UnsupportedError:
40 except zbar.SystemError:
41 # Cannot open video device
48 self.win = self.gui.main_window
49 self.win.raw_transaction_menu.addAction(_("&From QR code"), self.read_raw_qr)
51 def is_available(self):
52 return self._is_available
54 def scan_qr_hook(self, func):
61 proc = zbar.Processor()
63 proc.init(video_device=self.video_device())
64 except zbar.SystemError, e:
65 QMessageBox.warning(self.win, _('Error'), _(e), _('OK'))
74 # User closed the preview window
77 for r in proc.results:
78 if str(r.type) != 'QRCODE':
83 def read_raw_qr(self):
84 qrcode = self.scan_qr()
89 # transactions are binary, but qrcode seems to return utf8...
90 z = data.decode('utf8')
94 data = s.encode('hex')
95 tx = self.win.tx_from_text(data)
98 self.win.show_transaction(tx)
100 def video_device(self):
101 device = self.config.get("video_device", "default")
102 if device == 'default':
106 def requires_settings(self):
109 def settings_widget(self, window):
110 return EnterButton(_('Settings'), self.settings_dialog)
112 def _find_system_cameras(self):
113 device_root = "/sys/class/video4linux"
114 devices = {} # Name -> device
115 if os.path.exists(device_root):
116 for device in os.listdir(device_root):
117 name = open(os.path.join(device_root, device, 'name')).read()
118 devices[name] = os.path.join("/dev",device)
121 def settings_dialog(self):
122 system_cameras = self._find_system_cameras()
125 layout = QGridLayout(d)
126 layout.addWidget(QLabel("Choose a video device:"),0,0)
128 # Create a combo box with the available video devices:
131 # on change trigger for video device selection, makes the
132 # manual device selection only appear when needed:
134 combo_text = str(combo.itemText(x))
135 combo_data = combo.itemData(x)
136 if combo_text == "Manually specify a device":
137 custom_device_label.setVisible(True)
138 self.video_device_edit.setVisible(True)
139 if self.config.get("video_device") == "default":
140 self.video_device_edit.setText("")
142 self.video_device_edit.setText(self.config.get("video_device",''))
144 custom_device_label.setVisible(False)
145 self.video_device_edit.setVisible(False)
146 self.video_device_edit.setText(combo_data.toString())
148 # on save trigger for the video device selection window,
149 # stores the chosen video device on close.
151 device = str(self.video_device_edit.text())
152 self.config.set_key("video_device", device)
155 custom_device_label = QLabel("Video device: ")
156 custom_device_label.setVisible(False)
157 layout.addWidget(custom_device_label,1,0)
158 self.video_device_edit = QLineEdit()
159 self.video_device_edit.setVisible(False)
160 layout.addWidget(self.video_device_edit, 1,1,2,2)
161 combo.currentIndexChanged.connect(on_change)
163 combo.addItem("Default","default")
164 for camera, device in system_cameras.items():
165 combo.addItem(camera, device)
166 combo.addItem("Manually specify a device",self.config.get("video_device"))
168 # Populate the previously chosen device:
169 index = combo.findData(self.config.get("video_device"))
170 combo.setCurrentIndex(index)
172 layout.addWidget(combo,0,1)
174 self.accept = QPushButton(_("Done"))
175 self.accept.clicked.connect(on_save)
176 layout.addWidget(self.accept,4,2)