derive plugins from BasePlugin class
[electrum-nvc.git] / plugins / qrscanner.py
index cc16484..f66e60b 100644 (file)
@@ -8,55 +8,62 @@ try:
 except ImportError:
     zbar = None
 
+from electrum_gui import BasePlugin
+class Plugin(BasePlugin):
 
+    def __init__(self, gui):
+        BasePlugin.__init__(self, gui, 'qrscans', 'QR scans', "QR Scans.\nInstall the zbar package to enable this plugin")
+        
+    def is_available(self):
+        if not zbar:
+            return False
+        try:
+            proc = zbar.Processor()
+            proc.init()
+        except zbar.SystemError:
+            # Cannot open video device
+            return False
+        return True
 
-def init(gui):
-    if is_enabled():
-        gui.set_hook('create_send_tab', create_send_tab)
-    else:
-        gui.unset_hook('create_send_tab', create_send_tab)
-
-def get_info():
-    return 'QR scans', "QR Scans.\nInstall the zbar package to enable this plugin"
-
-def is_enabled():
-    return is_available()
-
-def toggle(gui):
-    return is_enabled()
 
+    def create_send_tab(self, grid):
+        b = QPushButton(_("Scan QR code"))
+        b.clicked.connect(self.fill_from_qr)
+        grid.addWidget(b, 1, 5)
 
-def is_available():
-    if not zbar:
-        return False
 
-    try:
+    def scan_qr(self):
         proc = zbar.Processor()
         proc.init()
-    except zbar.SystemError:
-        # Cannot open video device
-        return False
-
-    return True
+        proc.visible = True
+
+        while True:
+            try:
+                proc.process_one()
+            except:
+                # User closed the preview window
+                return {}
+
+            for r in proc.results:
+                if str(r.type) != 'QRCODE':
+                    continue
+                return parse_uri(r.data)
+        
 
-def scan_qr():
-    proc = zbar.Processor()
-    proc.init()
-    proc.visible = True
+    def fill_from_qr(self):
+        qrcode = self.scan_qr()
+        if 'address' in qrcode:
+            self.gui.payto_e.setText(qrcode['address'])
+        if 'amount' in qrcode:
+            self.gui.amount_e.setText(str(qrcode['amount']))
+        if 'label' in qrcode:
+            self.gui.message_e.setText(qrcode['label'])
+        if 'message' in qrcode:
+            self.gui.message_e.setText("%s (%s)" % (self.gui.message_e.text(), qrcode['message']))
+                
 
-    while True:
-        try:
-            proc.process_one()
-        except:
-            # User closed the preview window
-            return {}
 
-        for r in proc.results:
-            if str(r.type) != 'QRCODE':
-                continue
 
-            return parse_uri(r.data)
-        
 def parse_uri(uri):
     if ':' not in uri:
         # It's just an address (not BIP21)
@@ -82,24 +89,6 @@ def parse_uri(uri):
 
 
 
-def fill_from_qr(self):
-    qrcode = scan_qr()
-    if 'address' in qrcode:
-        self.payto_e.setText(qrcode['address'])
-    if 'amount' in qrcode:
-        self.amount_e.setText(str(qrcode['amount']))
-    if 'label' in qrcode:
-        self.message_e.setText(qrcode['label'])
-    if 'message' in qrcode:
-        self.message_e.setText("%s (%s)" % (self.message_e.text(), qrcode['message']))
-                
-
-def create_send_tab(gui, grid):
-    if is_available():
-        b = QPushButton(_("Scan QR code"))
-        b.clicked.connect(lambda: fill_from_qr(gui))
-        grid.addWidget(b, 1, 5)
-
 
 
 if __name__ == '__main__':