New port numbers
[electrum-nvc.git] / plugins / qrscanner.py
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
5
6 from electrum_nvc.i18n import _
7 import re
8 import os
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
14 import json
15
16 try:
17     import zbar
18 except ImportError:
19     zbar = None
20
21 from electrum_nvc 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
44         return True
45
46     def init(self):
47         self.win = self.gui.main_window
48
49     def is_available(self):
50         return self._is_available
51
52     def is_enabled(self):
53         return True
54
55     def scan_qr_hook(self):
56         proc = zbar.Processor()
57         try:
58             proc.init(video_device=self.video_device())
59         except zbar.SystemError, e:
60             QMessageBox.warning(self.win, _('Error'), _(e), _('OK'))
61             return
62
63         proc.visible = True
64
65         while True:
66             try:
67                 proc.process_one()
68             except Exception:
69                 # User closed the preview window
70                 return {}
71
72             for r in proc.results:
73                 if str(r.type) != 'QRCODE':
74                     continue
75                 return r.data
76         
77
78     def video_device(self):
79         device = self.config.get("video_device", "default")
80         if device == 'default':
81             device = ''
82         return device
83
84     def requires_settings(self):
85         return True
86
87     def settings_widget(self, window):
88         return EnterButton(_('Settings'), self.settings_dialog)
89     
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)
97         return devices
98
99     def settings_dialog(self):
100         system_cameras = self._find_system_cameras()
101
102         d = QDialog()
103         layout = QGridLayout(d)
104         layout.addWidget(QLabel("Choose a video device:"),0,0)
105
106         # Create a combo box with the available video devices:
107         combo = QComboBox()
108
109         # on change trigger for video device selection, makes the
110         # manual device selection only appear when needed:
111         def on_change(x):
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("")
119                 else:
120                     self.video_device_edit.setText(self.config.get("video_device",''))
121             else:
122                 custom_device_label.setVisible(False)
123                 self.video_device_edit.setVisible(False)
124                 self.video_device_edit.setText(combo_data.toString())
125
126         # on save trigger for the video device selection window,
127         # stores the chosen video device on close.
128         def on_save():
129             device = str(self.video_device_edit.text())
130             self.config.set_key("video_device", device)
131             d.accept()
132
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)
140
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"))
145
146         # Populate the previously chosen device:
147         index = combo.findData(self.config.get("video_device"))
148         combo.setCurrentIndex(index)
149
150         layout.addWidget(combo,0,1)
151
152         self.accept = QPushButton(_("Done"))
153         self.accept.clicked.connect(on_save)
154         layout.addWidget(self.accept,4,2)
155
156         if d.exec_():
157           return True
158         else:
159           return False