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