1 from electrum_nvc.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_nvc.i18n import _
9 from electrum_nvc import Transaction
10 from electrum_nvc.bitcoin import MIN_RELAY_TX_FEE, is_valid
11 from electrum_nvc_gui.qt.qrcodewidget import QRCodeWidget
12 from electrum_nvc import bmp
13 from electrum_nvc_gui.qt import HelpButton, EnterButton
21 from electrum_nvc 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
47 self.win = self.gui.main_window
49 def is_available(self):
50 return self._is_available
55 def scan_qr_hook(self):
56 proc = zbar.Processor()
58 proc.init(video_device=self.video_device())
59 except zbar.SystemError, e:
60 QMessageBox.warning(self.win, _('Error'), _(e), _('OK'))
69 # User closed the preview window
72 for r in proc.results:
73 if str(r.type) != 'QRCODE':
78 def video_device(self):
79 device = self.config.get("video_device", "default")
80 if device == 'default':
84 def requires_settings(self):
87 def settings_widget(self, window):
88 return EnterButton(_('Settings'), self.settings_dialog)
90 def _find_system_cameras(self):
91 device_root = "/sys/class/video4linux"
92 devices = {} # Name -> device
93 if os.path.exists(device_root):
94 for device in os.listdir(device_root):
95 name = open(os.path.join(device_root, device, 'name')).read()
96 devices[name] = os.path.join("/dev",device)
99 def settings_dialog(self):
100 system_cameras = self._find_system_cameras()
103 layout = QGridLayout(d)
104 layout.addWidget(QLabel("Choose a video device:"),0,0)
106 # Create a combo box with the available video devices:
109 # on change trigger for video device selection, makes the
110 # manual device selection only appear when needed:
112 combo_text = str(combo.itemText(x))
113 combo_data = combo.itemData(x)
114 if combo_text == "Manually specify a device":
115 custom_device_label.setVisible(True)
116 self.video_device_edit.setVisible(True)
117 if self.config.get("video_device") == "default":
118 self.video_device_edit.setText("")
120 self.video_device_edit.setText(self.config.get("video_device",''))
122 custom_device_label.setVisible(False)
123 self.video_device_edit.setVisible(False)
124 self.video_device_edit.setText(combo_data.toString())
126 # on save trigger for the video device selection window,
127 # stores the chosen video device on close.
129 device = str(self.video_device_edit.text())
130 self.config.set_key("video_device", device)
133 custom_device_label = QLabel("Video device: ")
134 custom_device_label.setVisible(False)
135 layout.addWidget(custom_device_label,1,0)
136 self.video_device_edit = QLineEdit()
137 self.video_device_edit.setVisible(False)
138 layout.addWidget(self.video_device_edit, 1,1,2,2)
139 combo.currentIndexChanged.connect(on_change)
141 combo.addItem("Default","default")
142 for camera, device in system_cameras.items():
143 combo.addItem(camera, device)
144 combo.addItem("Manually specify a device",self.config.get("video_device"))
146 # Populate the previously chosen device:
147 index = combo.findData(self.config.get("video_device"))
148 combo.setCurrentIndex(index)
150 layout.addWidget(combo,0,1)
152 self.accept = QPushButton(_("Done"))
153 self.accept.clicked.connect(on_save)
154 layout.addWidget(self.accept,4,2)