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
50 def is_available(self):
51 return self._is_available
56 def scan_qr_hook(self):
57 proc = zbar.Processor()
59 proc.init(video_device=self.video_device())
60 except zbar.SystemError, e:
61 QMessageBox.warning(self.win, _('Error'), _(e), _('OK'))
70 # User closed the preview window
73 for r in proc.results:
74 if str(r.type) != 'QRCODE':
79 def video_device(self):
80 device = self.config.get("video_device", "default")
81 if device == 'default':
85 def requires_settings(self):
88 def settings_widget(self, window):
89 return EnterButton(_('Settings'), self.settings_dialog)
91 def _find_system_cameras(self):
92 device_root = "/sys/class/video4linux"
93 devices = {} # Name -> device
94 if os.path.exists(device_root):
95 for device in os.listdir(device_root):
96 name = open(os.path.join(device_root, device, 'name')).read()
97 devices[name] = os.path.join("/dev",device)
100 def settings_dialog(self):
101 system_cameras = self._find_system_cameras()
104 layout = QGridLayout(d)
105 layout.addWidget(QLabel("Choose a video device:"),0,0)
107 # Create a combo box with the available video devices:
110 # on change trigger for video device selection, makes the
111 # manual device selection only appear when needed:
113 combo_text = str(combo.itemText(x))
114 combo_data = combo.itemData(x)
115 if combo_text == "Manually specify a device":
116 custom_device_label.setVisible(True)
117 self.video_device_edit.setVisible(True)
118 if self.config.get("video_device") == "default":
119 self.video_device_edit.setText("")
121 self.video_device_edit.setText(self.config.get("video_device",''))
123 custom_device_label.setVisible(False)
124 self.video_device_edit.setVisible(False)
125 self.video_device_edit.setText(combo_data.toString())
127 # on save trigger for the video device selection window,
128 # stores the chosen video device on close.
130 device = str(self.video_device_edit.text())
131 self.config.set_key("video_device", device)
134 custom_device_label = QLabel("Video device: ")
135 custom_device_label.setVisible(False)
136 layout.addWidget(custom_device_label,1,0)
137 self.video_device_edit = QLineEdit()
138 self.video_device_edit.setVisible(False)
139 layout.addWidget(self.video_device_edit, 1,1,2,2)
140 combo.currentIndexChanged.connect(on_change)
142 combo.addItem("Default","default")
143 for camera, device in system_cameras.items():
144 combo.addItem(camera, device)
145 combo.addItem("Manually specify a device",self.config.get("video_device"))
147 # Populate the previously chosen device:
148 index = combo.findData(self.config.get("video_device"))
149 combo.setCurrentIndex(index)
151 layout.addWidget(combo,0,1)
153 self.accept = QPushButton(_("Done"))
154 self.accept.clicked.connect(on_save)
155 layout.addWidget(self.accept,4,2)