URI: 
       tanother plugin for qrscanner - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit ba15b9eb6bcec956b2387320b94827176cf1a43f
   DIR parent cd9f3667352e4e97b345a9e967e428396dd47769
  HTML Author: thomasv <thomasv@gitorious>
       Date:   Sun,  3 Mar 2013 14:49:42 +0100
       
       another plugin for qrscanner
       
       Diffstat:
         M gui/gui_classic.py                  |      22 +++-------------------
         M gui/qrcodewidget.py                 |       2 +-
         D gui/qrscanner.py                    |      83 -------------------------------
         M plugins/pointofsale.py              |       2 +-
         A plugins/qrscanner.py                |     125 +++++++++++++++++++++++++++++++
       
       5 files changed, 130 insertions(+), 104 deletions(-)
       ---
   DIR diff --git a/gui/gui_classic.py b/gui/gui_classic.py
       t@@ -42,7 +42,7 @@ from electrum.wallet import format_satoshis
        from electrum.bitcoin import Transaction, is_valid
        from electrum import mnemonic
        
       -import bmp, pyqrnative, qrscanner
       +import bmp, pyqrnative
        import exchange_rate
        
        from decimal import Decimal
       t@@ -620,24 +620,7 @@ class ElectrumWindow(QMainWindow):
                self.payto_e = QLineEdit()
                grid.addWidget(QLabel(_('Pay to')), 1, 0)
                grid.addWidget(self.payto_e, 1, 1, 1, 3)
       -        
       -        def fill_from_qr():
       -            qrcode = qrscanner.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']))
       -                
       -
       -        if qrscanner.is_available():
       -            b = QPushButton(_("Scan QR code"))
       -            b.clicked.connect(fill_from_qr)
       -            grid.addWidget(b, 1, 5)
       -    
       +            
                grid.addWidget(HelpButton(_('Recipient of the funds.') + '\n\n' + _('You may enter a Bitcoin address, a label from your list of contacts (a list of completions will be proposed), or an alias (email-like address that forwards to a Bitcoin address)')), 1, 4)
        
                completer = QCompleter()
       t@@ -714,6 +697,7 @@ class ElectrumWindow(QMainWindow):
                self.amount_e.textChanged.connect(lambda: entry_changed(False) )
                self.fee_e.textChanged.connect(lambda: entry_changed(True) )
        
       +        self.wallet.run_hook('create_send_tab',(self,grid))
                return w2
        
        
   DIR diff --git a/gui/qrcodewidget.py b/gui/qrcodewidget.py
       t@@ -3,7 +3,7 @@ from PyQt4.QtCore import *
        import PyQt4.QtCore as QtCore
        import PyQt4.QtGui as QtGui
        
       -import bmp, pyqrnative, qrscanner
       +import bmp, pyqrnative
        
        
        class QRCodeWidget(QWidget):
   DIR diff --git a/gui/qrscanner.py b/gui/qrscanner.py
       t@@ -1,83 +0,0 @@
       -from electrum.util import print_error
       -
       -try:
       -    import zbar
       -except ImportError:    
       -    print_error("Install zbar package to enable QR scans")
       -    zbar = None
       -
       -from urlparse import urlparse, parse_qs
       -
       -def is_available():
       -    if not zbar:
       -        return False
       -
       -    try:
       -        proc = zbar.Processor()
       -        proc.init()
       -    except zbar.SystemError:
       -        # Cannot open video device
       -        return False
       -
       -    return True
       -
       -def scan_qr():
       -    proc = zbar.Processor()
       -    proc.init()
       -    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 parse_uri(uri):
       -    if ':' not in uri:
       -        # It's just an address (not BIP21)
       -        return {'address': uri}
       -
       -    if '//' not in uri:
       -        # Workaround for urlparse, it don't handle bitcoin: URI properly
       -        uri = uri.replace(':', '://')
       -        
       -    uri = urlparse(uri)
       -    result = {'address': uri.netloc} 
       -    
       -    if uri.path.startswith('?'):
       -        params = parse_qs(uri.path[1:])
       -    else:
       -        params = parse_qs(uri.path)    
       -
       -    for k,v in params.items():
       -        if k in ('amount', 'label', 'message'):
       -            result[k] = v[0]
       -        
       -    return result    
       -
       -if __name__ == '__main__':
       -    # Run some tests
       -    
       -    assert(parse_uri('1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
       -           {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
       -
       -    assert(parse_uri('bitcoin://1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
       -           {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
       -    
       -    assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
       -           {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
       -    
       -    assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10') ==
       -           {'amount': '10', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
       -    
       -    assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10&label=slush&message=Small%20tip%20to%20slush') ==
       -           {'amount': '10', 'label': 'slush', 'message': 'Small tip to slush', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
       -    
       -    
   DIR diff --git a/plugins/pointofsale.py b/plugins/pointofsale.py
       t@@ -8,7 +8,7 @@ import PyQt4.QtCore as QtCore
        import PyQt4.QtGui as QtGui
        
        from electrum_gui.qrcodewidget import QRCodeWidget
       -from electrum_gui import bmp, pyqrnative, qrscanner
       +from electrum_gui import bmp, pyqrnative
        
        from electrum_gui.i18n import _
        
   DIR diff --git a/plugins/qrscanner.py b/plugins/qrscanner.py
       t@@ -0,0 +1,125 @@
       +from electrum.util import print_error
       +
       +try:
       +    import zbar
       +except ImportError:    
       +    print_error("Install zbar package to enable QR scans")
       +    zbar = None
       +
       +from urlparse import urlparse, parse_qs
       +
       +
       +def init(wallet):
       +    pass
       +
       +def init_gui(gui):
       +    if is_enabled():
       +        gui.wallet.set_hook('create_send_tab', create_send_tab)
       +    else:
       +        gui.wallet.unset_hook('create_send_tab', create_send_tab)
       +
       +def get_info():
       +    return 'QR scans', "QR Scans"
       +
       +def is_enabled():
       +    return is_available()
       +
       +def toggle(gui):
       +    return is_enabled()
       +
       +
       +def is_available():
       +    if not zbar:
       +        return False
       +
       +    try:
       +        proc = zbar.Processor()
       +        proc.init()
       +    except zbar.SystemError:
       +        # Cannot open video device
       +        return False
       +
       +    return True
       +
       +def scan_qr():
       +    proc = zbar.Processor()
       +    proc.init()
       +    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 parse_uri(uri):
       +    if ':' not in uri:
       +        # It's just an address (not BIP21)
       +        return {'address': uri}
       +
       +    if '//' not in uri:
       +        # Workaround for urlparse, it don't handle bitcoin: URI properly
       +        uri = uri.replace(':', '://')
       +        
       +    uri = urlparse(uri)
       +    result = {'address': uri.netloc} 
       +    
       +    if uri.path.startswith('?'):
       +        params = parse_qs(uri.path[1:])
       +    else:
       +        params = parse_qs(uri.path)    
       +
       +    for k,v in params.items():
       +        if k in ('amount', 'label', 'message'):
       +            result[k] = v[0]
       +        
       +    return result    
       +
       +
       +
       +def fill_from_qr(self):
       +    qrcode = qrscanner.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 qrscanner.is_available():
       +        b = QPushButton(_("Scan QR code"))
       +        b.clicked.connect(lambda: fill_from_qr(gui))
       +        grid.addWidget(b, 1, 5)
       +
       +
       +
       +if __name__ == '__main__':
       +    # Run some tests
       +    
       +    assert(parse_uri('1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
       +           {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
       +
       +    assert(parse_uri('bitcoin://1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
       +           {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
       +    
       +    assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN') ==
       +           {'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
       +    
       +    assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10') ==
       +           {'amount': '10', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
       +    
       +    assert(parse_uri('bitcoin:1Marek48fwU7mugmSe186do2QpUkBnpzSN?amount=10&label=slush&message=Small%20tip%20to%20slush') ==
       +           {'amount': '10', 'label': 'slush', 'message': 'Small tip to slush', 'address': '1Marek48fwU7mugmSe186do2QpUkBnpzSN'})
       +    
       +