slush

Electrum QR scanner

Jul 25th, 2012
273
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. diff --git a/lib/gui_qt.py b/lib/gui_qt.py
  2. index 6784481..f0e58f9 100644
  3. --- a/lib/gui_qt.py
  4. +++ b/lib/gui_qt.py
  5. @@ -27,6 +27,12 @@ except:
  6.      print_error("on Linux systems, you may try 'sudo apt-get install python-qt4'")
  7.      sys.exit(1)
  8.  
  9. +try:
  10. +    import zbar
  11. +except ImportError:
  12. +    print_error("Install zbar package to enable QR scans")
  13. +    zbar = None
  14. +
  15.  from PyQt4.QtGui import *
  16.  from PyQt4.QtCore import *
  17.  import PyQt4.QtCore as QtCore
  18. @@ -432,6 +438,36 @@ class ElectrumWindow(QMainWindow):
  19.          self.history_list.setCurrentItem(self.history_list.topLevelItem(0))
  20.  
  21.  
  22. +    def scan_qr(self):
  23. +        from urlparse import urlparse, parse_qs
  24. +
  25. +        proc = zbar.Processor()
  26. +        proc.init()
  27. +        proc.visible = True
  28. +    
  29. +        while True:
  30. +            try:
  31. +                proc.process_one()
  32. +            except:
  33. +                # User closed the preview window
  34. +                return {}
  35. +
  36. +            for r in proc.results:
  37. +                if str(r.type) != 'QRCODE':
  38. +                    continue
  39.  
  40. +
  41. +                qrcode = r.data
  42. +                if ':' not in qrcode:
  43. +                    # It's just an address (not BIP21)
  44. +                    return {'address': qrcode}
  45. +
  46. +                if '//' not in qrcode:
  47. +                    # Workaround for urlparse, it don't handle bitcoin: URI properly
  48. +                    qrcode = qrcode.replace(':', '://')
  49. +                uri = urlparse(qrcode)
  50. +                print uri
  51. +                return {'address': uri.netloc}#, 'amount': None, 'label': None}
  52. +
  53.      def create_send_tab(self):
  54.          w = QWidget()
  55.  
  56. @@ -443,6 +479,21 @@ class ElectrumWindow(QMainWindow):
  57.          self.payto_e = QLineEdit()
  58.          grid.addWidget(QLabel(_('Pay to')), 1, 0)
  59.          grid.addWidget(self.payto_e, 1, 1, 1, 3)
  60. +        
  61. +        def fill_from_qr():
  62. +            qrcode = self.scan_qr()
  63. +            if 'address' in qrcode:
  64. +                self.payto_e.setText(qrcode['address'])
  65. +            if 'amount' in qrcode:
  66. +                self.amount_e.setText(str(qrcode['amount']))
  67. +            if 'label' in qrcode:
  68. +                self.message_e.setText(qrcode['label'])
  69. +
  70. +        if zbar:
  71. +            b = QPushButton(_("Scan QR code"))
  72. +            b.clicked.connect(fill_from_qr)
  73. +            grid.addWidget(b, 1, 5)
  74. +    
  75.          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)
RAW Paste Data