transaction show_qr
[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.SystemError:
39             # Cannot open video device
40             pass
41             #return False
42
43         return True
44
45     def init(self):
46         self.win = self.gui.main_window
47         self.win.raw_transaction_menu.addAction(_("&From QR code"), self.read_raw_qr)
48
49     def is_available(self):
50         return self._is_available
51
52     def scan_qr_hook(self, func):
53         data = self.scan_qr()
54         if type(data) != str:
55             return
56         func(data)
57
58     def scan_qr(self):
59         proc = zbar.Processor()
60         try:
61             proc.init(video_device=self.video_device())
62         except zbar.SystemError, e:
63             QMessageBox.warning(self.win, _('Error'), _(e), _('OK'))
64             return
65
66         proc.visible = True
67
68         while True:
69             try:
70                 proc.process_one()
71             except Exception:
72                 # User closed the preview window
73                 return {}
74
75             for r in proc.results:
76                 if str(r.type) != 'QRCODE':
77                     continue
78                 return r.data
79         
80
81     def read_raw_qr(self):
82         qrcode = self.scan_qr()
83         if not qrcode:
84             return
85         tx = self.win.tx_from_text(qrcode)
86         if not tx:
87             return
88         self.win.show_transaction(tx)
89
90     def video_device(self):
91         device = self.config.get("video_device", "default")
92         if device == 'default':
93             device = ''
94         return device
95
96     def requires_settings(self):
97         return True
98
99     def settings_widget(self, window):
100         return EnterButton(_('Settings'), self.settings_dialog)
101     
102     def _find_system_cameras(self):
103         device_root = "/sys/class/video4linux"
104         devices = {} # Name -> device
105         if os.path.exists(device_root):
106             for device in os.listdir(device_root):
107                 name = open(os.path.join(device_root, device, 'name')).read()
108                 devices[name] = os.path.join("/dev",device)
109         return devices
110
111     def settings_dialog(self):
112         system_cameras = self._find_system_cameras()
113
114         d = QDialog()
115         layout = QGridLayout(d)
116         layout.addWidget(QLabel("Choose a video device:"),0,0)
117
118         # Create a combo box with the available video devices:
119         combo = QComboBox()
120
121         # on change trigger for video device selection, makes the
122         # manual device selection only appear when needed:
123         def on_change(x):
124             combo_text = str(combo.itemText(x))
125             combo_data = combo.itemData(x)
126             if combo_text == "Manually specify a device":
127                 custom_device_label.setVisible(True)
128                 self.video_device_edit.setVisible(True)
129                 if self.config.get("video_device") == "default":
130                     self.video_device_edit.setText("")
131                 else:
132                     self.video_device_edit.setText(self.config.get("video_device"))
133             else:
134                 custom_device_label.setVisible(False)
135                 self.video_device_edit.setVisible(False)
136                 self.video_device_edit.setText(combo_data.toString())
137
138         # on save trigger for the video device selection window,
139         # stores the chosen video device on close.
140         def on_save():
141             device = str(self.video_device_edit.text())
142             self.config.set_key("video_device", device)
143             d.accept()
144
145         custom_device_label = QLabel("Video device: ")
146         custom_device_label.setVisible(False)
147         layout.addWidget(custom_device_label,1,0)
148         self.video_device_edit = QLineEdit()
149         self.video_device_edit.setVisible(False)
150         layout.addWidget(self.video_device_edit, 1,1,2,2)
151         combo.currentIndexChanged.connect(on_change)
152
153         combo.addItem("Default","default")
154         for camera, device in system_cameras.items():
155             combo.addItem(camera, device)
156         combo.addItem("Manually specify a device",self.config.get("video_device"))
157
158         # Populate the previously chosen device:
159         index = combo.findData(self.config.get("video_device"))
160         combo.setCurrentIndex(index)
161
162         layout.addWidget(combo,0,1)
163
164         self.accept = QPushButton(_("Done"))
165         self.accept.clicked.connect(on_save)
166         layout.addWidget(self.accept,4,2)
167
168         if d.exec_():
169           return True
170         else:
171           return False