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