plugin hook for load_wallet
[electrum-nvc.git] / plugins / pointofsale.py
1 import re
2 import platform
3 from decimal import Decimal
4
5 from PyQt4.QtGui import *
6 from PyQt4.QtCore import *
7 import PyQt4.QtCore as QtCore
8 import PyQt4.QtGui as QtGui
9
10 from electrum_gui.qrcodewidget import QRCodeWidget
11 from electrum_gui import bmp, pyqrnative, BasePlugin
12 from electrum_gui.i18n import _
13
14
15 if platform.system() == 'Windows':
16     MONOSPACE_FONT = 'Lucida Console'
17 elif platform.system() == 'Darwin':
18     MONOSPACE_FONT = 'Monaco'
19 else:
20     MONOSPACE_FONT = 'monospace'
21
22 column_index = 3
23
24 class QR_Window(QWidget):
25
26     def __init__(self, exchanger):
27         QWidget.__init__(self)
28         self.exchanger = exchanger
29         self.setWindowTitle('Electrum - '+_('Invoice'))
30         self.setMinimumSize(800, 250)
31         self.address = ''
32         self.label = ''
33         self.amount = 0
34         self.setFocusPolicy(QtCore.Qt.NoFocus)
35
36         main_box = QHBoxLayout()
37         
38         self.qrw = QRCodeWidget()
39         main_box.addWidget(self.qrw, 1)
40
41         vbox = QVBoxLayout()
42         main_box.addLayout(vbox)
43
44         self.address_label = QLabel("")
45         #self.address_label.setFont(QFont(MONOSPACE_FONT))
46         vbox.addWidget(self.address_label)
47
48         self.label_label = QLabel("")
49         vbox.addWidget(self.label_label)
50
51         self.amount_label = QLabel("")
52         vbox.addWidget(self.amount_label)
53
54         vbox.addStretch(1)
55         self.setLayout(main_box)
56
57
58     def set_content(self, addr, label, amount, currency):
59         self.address = addr
60         address_text = "<span style='font-size: 18pt'>%s</span>" % addr if addr else ""
61         self.address_label.setText(address_text)
62
63         if currency == 'BTC': currency = None
64         amount_text = ''
65         if amount:
66             if currency:
67                 self.amount = Decimal(amount) / self.exchanger.exchange(1, currency) if currency else amount
68             else:
69                 self.amount = Decimal(amount)
70             self.amount = self.amount.quantize(Decimal('1.0000'))
71
72             if currency:
73                 amount_text += "<span style='font-size: 18pt'>%s %s</span><br/>" % (amount, currency)
74             amount_text += "<span style='font-size: 21pt'>%s</span> <span style='font-size: 16pt'>BTC</span> " % str(self.amount) 
75         self.amount_label.setText(amount_text)
76
77         self.label = label
78         label_text = "<span style='font-size: 21pt'>%s</span>" % label if label else ""
79         self.label_label.setText(label_text)
80
81         msg = 'bitcoin:'+self.address
82         if self.amount is not None:
83             msg += '?amount=%s'%(str( self.amount))
84             if self.label is not None:
85                 msg += '&label=%s'%(self.label)
86         elif self.label is not None:
87             msg += '?label=%s'%(self.label)
88             
89         self.qrw.set_addr( msg )
90
91
92
93
94 class Plugin(BasePlugin):
95
96     def fullname(self):
97         return 'Point of Sale'
98
99     def description(self):
100         return _('Show QR code window and amounts requested for each address. Add menu item to request amount.')
101
102     def init(self):
103         self.qr_window = None
104         self.merchant_name = self.config.get('merchant_name', 'Invoice')
105
106         self.gui.expert_mode = True
107         self.gui.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Request')])
108         self.toggle_QR_window(True)
109         self.requested_amounts = {}
110
111     def load_wallet(self):
112         self.requested_amounts = self.gui.wallet.storage.get('requested_amounts',{}) 
113
114     def close(self):
115         self.gui.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Tx')])
116         self.toggle_QR_window(False)
117     
118
119     def close_main_window(self):
120         if self.qr_window: 
121             self.qr_window.close()
122             self.qr_window = None
123
124     
125     def timer_actions(self):
126         if self.qr_window:
127             self.qr_window.qrw.update_qr()
128
129
130     def toggle_QR_window(self, show):
131         if show and not self.qr_window:
132             self.qr_window = QR_Window(self.gui.exchanger)
133             self.qr_window.setVisible(True)
134             self.qr_window_geometry = self.qr_window.geometry()
135             item = self.gui.receive_list.currentItem()
136             if item:
137                 address = str(item.text(1))
138                 label = self.gui.wallet.labels.get(address)
139                 amount, currency = self.requested_amounts.get(address, (None, None))
140                 self.qr_window.set_content( address, label, amount, currency )
141
142         elif show and self.qr_window and not self.qr_window.isVisible():
143             self.qr_window.setVisible(True)
144             self.qr_window.setGeometry(self.qr_window_geometry)
145
146         elif not show and self.qr_window and self.qr_window.isVisible():
147             self.qr_window_geometry = self.qr_window.geometry()
148             self.qr_window.setVisible(False)
149
150
151     
152     def update_receive_item(self, address, item):
153         try:
154             amount, currency = self.requested_amounts.get(address, (None, None))
155         except:
156             print "cannot get requested amount", address, self.requested_amounts.get(address)
157             amount, currency = None, None
158             self.requested_amounts.pop(address)
159
160         amount_str = amount + (' ' + currency if currency else '') if amount is not None  else ''
161         item.setData(column_index,0,amount_str)
162
163
164     
165     def current_item_changed(self, a):
166         if a is not None and self.qr_window and self.qr_window.isVisible():
167             address = str(a.text(0))
168             label = self.gui.wallet.labels.get(address)
169             try:
170                 amount, currency = self.requested_amounts.get(address, (None, None))
171             except:
172                 amount, currency = None, None
173             self.qr_window.set_content( address, label, amount, currency )
174
175
176     
177     def item_changed(self, item, column):
178         if column != column_index:
179             return
180         address = str( item.text(0) )
181         text = str( item.text(column) )
182         try:
183             seq = self.gui.wallet.get_address_index(address)
184             index = seq[1][1]
185         except:
186             print "cannot get index"
187             return
188
189         text = text.strip().upper()
190         print text
191         m = re.match('^(\d*(|\.\d*))\s*(|BTC|EUR|USD|GBP|CNY|JPY|RUB|BRL)$', text)
192         if m and m.group(1) and m.group(1)!='.':
193             amount = m.group(1)
194             currency = m.group(3)
195             if not currency:
196                 currency = 'BTC'
197             else:
198                 currency = currency.upper()
199                     
200             self.requested_amounts[address] = (amount, currency)
201             self.gui.wallet.storage.put('requested_amounts', self.requested_amounts, True)
202
203             label = self.gui.wallet.labels.get(address)
204             if label is None:
205                 label = self.merchant_name + ' - %04d'%(index+1)
206                 self.gui.wallet.labels[address] = label
207
208             if self.qr_window:
209                 self.qr_window.set_content( address, label, amount, currency )
210
211         else:
212             item.setText(column,'')
213             if address in self.requested_amounts:
214                 self.requested_amounts.pop(address)
215             
216         self.gui.update_receive_item(self.gui.receive_list.currentItem())
217
218
219
220
221     def edit_amount(self):
222         l = self.gui.receive_list
223         item = l.currentItem()
224         item.setFlags(Qt.ItemIsEditable|Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
225         l.editItem( item, column_index )
226         item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
227
228     
229     def receive_menu(self, menu):
230         menu.addAction(_("Request amount"), self.edit_amount)
231
232