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 is_available(self):
50 return self._is_available
52 def scan_qr_hook(self, func):
59 proc = zbar.Processor()
61 proc.init(video_device=self.video_device())
62 except zbar.SystemError, e:
63 QMessageBox.warning(self.win, _('Error'), _(e), _('OK'))
72 # User closed the preview window
75 for r in proc.results:
76 if str(r.type) != 'QRCODE':
81 def read_raw_qr(self):
82 qrcode = self.scan_qr()
87 # transactions are binary, but qrcode seems to return utf8...
88 z = data.decode('utf8')
92 data = s.encode('hex')
93 tx = self.win.tx_from_text(data)
96 self.win.show_transaction(tx)
98 def video_device(self):
99 device = self.config.get("video_device", "default")
100 if device == 'default':
104 def requires_settings(self):
107 def settings_widget(self, window):
108 return EnterButton(_('Settings'), self.settings_dialog)
110 def _find_system_cameras(self):
111 device_root = "/sys/class/video4linux"
112 devices = {} # Name -> device
113 if os.path.exists(device_root):
114 for device in os.listdir(device_root):
115 name = open(os.path.join(device_root, device, 'name')).read()
116 devices[name] = os.path.join("/dev",device)
119 def settings_dialog(self):
120 system_cameras = self._find_system_cameras()
123 layout = QGridLayout(d)
124 layout.addWidget(QLabel("Choose a video device:"),0,0)
126 # Create a combo box with the available video devices:
129 # on change trigger for video device selection, makes the
130 # manual device selection only appear when needed:
132 combo_text = str(combo.itemText(x))
133 combo_data = combo.itemData(x)
134 if combo_text == "Manually specify a device":
135 custom_device_label.setVisible(True)
136 self.video_device_edit.setVisible(True)
137 if self.config.get("video_device") == "default":
138 self.video_device_edit.setText("")
140 self.video_device_edit.setText(self.config.get("video_device",''))
142 custom_device_label.setVisible(False)
143 self.video_device_edit.setVisible(False)
144 self.video_device_edit.setText(combo_data.toString())
146 # on save trigger for the video device selection window,
147 # stores the chosen video device on close.
149 device = str(self.video_device_edit.text())
150 self.config.set_key("video_device", device)
153 custom_device_label = QLabel("Video device: ")
154 custom_device_label.setVisible(False)
155 layout.addWidget(custom_device_label,1,0)
156 self.video_device_edit = QLineEdit()
157 self.video_device_edit.setVisible(False)
158 layout.addWidget(self.video_device_edit, 1,1,2,2)
159 combo.currentIndexChanged.connect(on_change)
161 combo.addItem("Default","default")
162 for camera, device in system_cameras.items():
163 combo.addItem(camera, device)
164 combo.addItem("Manually specify a device",self.config.get("video_device"))
166 # Populate the previously chosen device:
167 index = combo.findData(self.config.get("video_device"))
168 combo.setCurrentIndex(index)
170 layout.addWidget(combo,0,1)
172 self.accept = QPushButton(_("Done"))
173 self.accept.clicked.connect(on_save)
174 layout.addWidget(self.accept,4,2)