always enable qr scanner plugin
[electrum-nvc.git] / plugins / qrscanner.py
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
5
6 from electrum.i18n import _
7 import re
8 import os
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
14 import json
15
16 try:
17     import zbar
18 except ImportError:
19     zbar = None
20
21 from electrum import BasePlugin
22 class Plugin(BasePlugin):
23
24     def fullname(self): return 'QR scans'
25
26     def description(self): return "QR Scans.\nInstall the zbar package to enable this plugin.\nOn linux, type: 'apt-get install python-zbar'"
27
28     def __init__(self, gui, name):
29         BasePlugin.__init__(self, gui, name)
30         self._is_available = self._init()
31
32     def _init(self):
33         if not zbar:
34             return False
35         try:
36             proc = zbar.Processor()
37             proc.init(video_device=self.video_device())
38         except zbar.UnsupportedError:
39             return False
40         except zbar.SystemError:
41             # Cannot open video device
42             pass
43             #return False
44
45         return True
46
47     def init(self):
48         self.win = self.gui.main_window
49
50     def is_available(self):
51         return self._is_available
52
53     def is_enabled(self):
54         return True
55
56     def scan_qr_hook(self):
57         proc = zbar.Processor()
58         try:
59             proc.init(video_device=self.video_device())
60         except zbar.SystemError, e:
61             QMessageBox.warning(self.win, _('Error'), _(e), _('OK'))
62             return
63
64         proc.visible = True
65
66         while True:
67             try:
68                 proc.process_one()
69             except Exception:
70                 # User closed the preview window
71                 return {}
72
73             for r in proc.results:
74                 if str(r.type) != 'QRCODE':
75                     continue
76                 return r.data
77         
78
79     def video_device(self):
80         device = self.config.get("video_device", "default")
81         if device == 'default':
82             device = ''
83         return device
84
85     def requires_settings(self):
86         return True
87
88     def settings_widget(self, window):
89         return EnterButton(_('Settings'), self.settings_dialog)
90     
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)
98         return devices
99
100     def settings_dialog(self):
101         system_cameras = self._find_system_cameras()
102
103         d = QDialog()
104         layout = QGridLayout(d)
105         layout.addWidget(QLabel("Choose a video device:"),0,0)
106
107         # Create a combo box with the available video devices:
108         combo = QComboBox()
109
110         # on change trigger for video device selection, makes the
111         # manual device selection only appear when needed:
112         def on_change(x):
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("")
120                 else:
121                     self.video_device_edit.setText(self.config.get("video_device",''))
122             else:
123                 custom_device_label.setVisible(False)
124                 self.video_device_edit.setVisible(False)
125                 self.video_device_edit.setText(combo_data.toString())
126
127         # on save trigger for the video device selection window,
128         # stores the chosen video device on close.
129         def on_save():
130             device = str(self.video_device_edit.text())
131             self.config.set_key("video_device", device)
132             d.accept()
133
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)
141
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"))
146
147         # Populate the previously chosen device:
148         index = combo.findData(self.config.get("video_device"))
149         combo.setCurrentIndex(index)
150
151         layout.addWidget(combo,0,1)
152
153         self.accept = QPushButton(_("Done"))
154         self.accept.clicked.connect(on_save)
155         layout.addWidget(self.accept,4,2)
156
157         if d.exec_():
158           return True
159         else:
160           return False