2016-05-27 09:56:53 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
#
|
|
|
|
|
# Electrum - lightweight Bitcoin client
|
|
|
|
|
# Copyright (C) 2015 Thomas Voegtlin
|
|
|
|
|
#
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person
|
|
|
|
|
# obtaining a copy of this software and associated documentation files
|
|
|
|
|
# (the "Software"), to deal in the Software without restriction,
|
|
|
|
|
# including without limitation the rights to use, copy, modify, merge,
|
|
|
|
|
# publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
|
# and to permit persons to whom the Software is furnished to do so,
|
|
|
|
|
# subject to the following conditions:
|
|
|
|
|
#
|
|
|
|
|
# The above copyright notice and this permission notice shall be
|
|
|
|
|
# included in all copies or substantial portions of the Software.
|
|
|
|
|
#
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
|
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
|
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
|
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
# SOFTWARE.
|
|
|
|
|
|
2019-02-10 21:00:08 +01:00
|
|
|
from enum import IntEnum
|
2020-02-23 21:23:56 +01:00
|
|
|
from typing import Optional
|
2019-02-10 21:00:08 +01:00
|
|
|
|
2018-11-27 21:32:55 +01:00
|
|
|
from PyQt5.QtGui import QStandardItemModel, QStandardItem
|
2020-05-15 15:17:47 +02:00
|
|
|
from PyQt5.QtWidgets import QMenu, QAbstractItemView
|
2020-02-23 21:18:46 +01:00
|
|
|
from PyQt5.QtCore import Qt, QItemSelectionModel, QModelIndex
|
2018-09-25 18:15:28 +02:00
|
|
|
|
2016-05-27 09:56:53 +02:00
|
|
|
from electrum.i18n import _
|
2020-05-31 12:49:49 +02:00
|
|
|
from electrum.util import format_time
|
2020-06-22 22:37:58 +02:00
|
|
|
from electrum.invoices import PR_TYPE_ONCHAIN, PR_TYPE_LN, LNInvoice, OnchainInvoice
|
2018-07-11 17:38:47 +02:00
|
|
|
from electrum.plugin import run_hook
|
2018-09-25 18:15:28 +02:00
|
|
|
|
2020-05-15 15:17:47 +02:00
|
|
|
from .util import MyTreeView, pr_icons, read_QIcon, webopen, MySortModel
|
2016-05-27 09:56:53 +02:00
|
|
|
|
2019-02-10 21:00:08 +01:00
|
|
|
|
2018-12-04 18:56:30 +01:00
|
|
|
ROLE_REQUEST_TYPE = Qt.UserRole
|
2019-09-03 14:44:33 +02:00
|
|
|
ROLE_KEY = Qt.UserRole + 1
|
2020-05-15 15:17:47 +02:00
|
|
|
ROLE_SORT_ORDER = Qt.UserRole + 2
|
|
|
|
|
|
2018-12-04 18:56:30 +01:00
|
|
|
|
2018-11-27 21:32:55 +01:00
|
|
|
class RequestList(MyTreeView):
|
2017-02-16 17:10:02 +01:00
|
|
|
|
2019-02-10 21:00:08 +01:00
|
|
|
class Columns(IntEnum):
|
|
|
|
|
DATE = 0
|
2019-01-30 11:10:11 +01:00
|
|
|
DESCRIPTION = 1
|
|
|
|
|
AMOUNT = 2
|
|
|
|
|
STATUS = 3
|
2019-02-10 21:00:08 +01:00
|
|
|
|
2019-02-10 21:13:53 +01:00
|
|
|
headers = {
|
|
|
|
|
Columns.DATE: _('Date'),
|
|
|
|
|
Columns.DESCRIPTION: _('Description'),
|
|
|
|
|
Columns.AMOUNT: _('Amount'),
|
|
|
|
|
Columns.STATUS: _('Status'),
|
|
|
|
|
}
|
2019-02-11 22:23:30 +01:00
|
|
|
filter_columns = [Columns.DATE, Columns.DESCRIPTION, Columns.AMOUNT]
|
2016-05-27 09:56:53 +02:00
|
|
|
|
|
|
|
|
def __init__(self, parent):
|
2019-02-10 21:00:08 +01:00
|
|
|
super().__init__(parent, self.create_menu,
|
|
|
|
|
stretch_column=self.Columns.DESCRIPTION,
|
2019-02-20 20:05:28 +01:00
|
|
|
editable_columns=[])
|
2020-01-19 07:02:48 +01:00
|
|
|
self.wallet = self.parent.wallet
|
2020-05-15 15:17:47 +02:00
|
|
|
self.std_model = QStandardItemModel(self)
|
|
|
|
|
self.proxy = MySortModel(self, sort_role=ROLE_SORT_ORDER)
|
|
|
|
|
self.proxy.setSourceModel(self.std_model)
|
|
|
|
|
self.setModel(self.proxy)
|
2016-05-27 09:56:53 +02:00
|
|
|
self.setSortingEnabled(True)
|
2018-11-27 21:32:55 +01:00
|
|
|
self.selectionModel().currentRowChanged.connect(self.item_changed)
|
2020-03-03 12:56:44 +01:00
|
|
|
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
|
|
|
|
|
self.update()
|
2016-05-27 09:56:53 +02:00
|
|
|
|
2019-01-17 15:29:21 +01:00
|
|
|
def select_key(self, key):
|
|
|
|
|
for i in range(self.model().rowCount()):
|
2019-01-30 11:10:11 +01:00
|
|
|
item = self.model().index(i, self.Columns.DATE)
|
2019-09-03 14:44:33 +02:00
|
|
|
row_key = item.data(ROLE_KEY)
|
2019-01-17 15:29:21 +01:00
|
|
|
if key == row_key:
|
|
|
|
|
self.selectionModel().setCurrentIndex(item, QItemSelectionModel.SelectCurrent | QItemSelectionModel.Rows)
|
|
|
|
|
break
|
|
|
|
|
|
2020-02-23 21:23:56 +01:00
|
|
|
def item_changed(self, idx: Optional[QModelIndex]):
|
|
|
|
|
if idx is None:
|
|
|
|
|
self.parent.receive_payreq_e.setText('')
|
|
|
|
|
self.parent.receive_address_e.setText('')
|
|
|
|
|
return
|
2020-02-23 21:18:46 +01:00
|
|
|
if not idx.isValid():
|
|
|
|
|
return
|
2018-11-27 21:32:55 +01:00
|
|
|
# TODO use siblingAtColumn when min Qt version is >=5.11
|
2020-05-15 15:17:47 +02:00
|
|
|
item = self.item_from_index(idx.sibling(idx.row(), self.Columns.DATE))
|
2019-09-03 14:44:33 +02:00
|
|
|
key = item.data(ROLE_KEY)
|
|
|
|
|
req = self.wallet.get_request(key)
|
2019-08-21 18:25:36 +02:00
|
|
|
if req is None:
|
|
|
|
|
self.update()
|
|
|
|
|
return
|
2020-05-31 12:49:49 +02:00
|
|
|
if req.is_lightning():
|
2020-07-08 04:16:30 +02:00
|
|
|
self.parent.receive_payreq_e.setText(req.invoice) # TODO maybe prepend "lightning:" ??
|
2020-05-31 12:49:49 +02:00
|
|
|
self.parent.receive_address_e.setText(req.invoice)
|
2019-12-07 06:06:36 +01:00
|
|
|
else:
|
2020-05-31 12:49:49 +02:00
|
|
|
self.parent.receive_payreq_e.setText(self.parent.wallet.get_request_URI(req))
|
|
|
|
|
self.parent.receive_address_e.setText(req.get_address())
|
2020-05-14 20:16:01 +02:00
|
|
|
self.parent.receive_payreq_e.repaint() # macOS hack (similar to #4777)
|
|
|
|
|
self.parent.receive_address_e.repaint() # macOS hack (similar to #4777)
|
2019-08-21 18:25:36 +02:00
|
|
|
|
2020-02-23 21:18:46 +01:00
|
|
|
def clearSelection(self):
|
|
|
|
|
super().clearSelection()
|
|
|
|
|
self.selectionModel().clearCurrentIndex()
|
|
|
|
|
|
2019-08-21 18:25:36 +02:00
|
|
|
def refresh_status(self):
|
2020-05-15 15:17:47 +02:00
|
|
|
m = self.std_model
|
2019-08-21 18:25:36 +02:00
|
|
|
for r in range(m.rowCount()):
|
|
|
|
|
idx = m.index(r, self.Columns.STATUS)
|
|
|
|
|
date_idx = idx.sibling(idx.row(), self.Columns.DATE)
|
|
|
|
|
date_item = m.itemFromIndex(date_idx)
|
|
|
|
|
status_item = m.itemFromIndex(idx)
|
2019-09-03 14:44:33 +02:00
|
|
|
key = date_item.data(ROLE_KEY)
|
|
|
|
|
req = self.wallet.get_request(key)
|
2019-08-21 18:25:36 +02:00
|
|
|
if req:
|
2020-05-31 12:49:49 +02:00
|
|
|
status = self.parent.wallet.get_request_status(key)
|
|
|
|
|
status_str = req.get_status_str(status)
|
2019-08-21 18:25:36 +02:00
|
|
|
status_item.setText(status_str)
|
2019-08-11 14:47:06 +02:00
|
|
|
status_item.setIcon(read_QIcon(pr_icons.get(status)))
|
2016-05-27 09:56:53 +02:00
|
|
|
|
2018-11-27 21:32:55 +01:00
|
|
|
def update(self):
|
2020-01-21 11:51:02 +01:00
|
|
|
# not calling maybe_defer_update() as it interferes with conditional-visibility
|
2019-05-27 20:24:09 +02:00
|
|
|
self.parent.update_receive_address_styling()
|
2020-05-15 15:17:47 +02:00
|
|
|
self.proxy.setDynamicSortFilter(False) # temp. disable re-sorting after every change
|
|
|
|
|
self.std_model.clear()
|
2019-02-10 21:13:53 +01:00
|
|
|
self.update_headers(self.__class__.headers)
|
2019-09-10 08:57:40 +02:00
|
|
|
for req in self.wallet.get_sorted_requests():
|
2020-06-22 22:37:58 +02:00
|
|
|
if req.is_lightning():
|
|
|
|
|
assert isinstance(req, LNInvoice)
|
|
|
|
|
key = req.rhash
|
|
|
|
|
else:
|
|
|
|
|
assert isinstance(req, OnchainInvoice)
|
|
|
|
|
key = req.id
|
2020-05-31 12:49:49 +02:00
|
|
|
status = self.parent.wallet.get_request_status(key)
|
|
|
|
|
status_str = req.get_status_str(status)
|
|
|
|
|
request_type = req.type
|
|
|
|
|
timestamp = req.time
|
2020-06-22 22:37:58 +02:00
|
|
|
amount = req.get_amount_sat()
|
2020-05-31 12:49:49 +02:00
|
|
|
message = req.message
|
2016-05-27 09:56:53 +02:00
|
|
|
date = format_time(timestamp)
|
|
|
|
|
amount_str = self.parent.format_amount(amount) if amount else ""
|
2019-08-21 18:25:36 +02:00
|
|
|
labels = [date, message, amount_str, status_str]
|
2020-05-31 12:49:49 +02:00
|
|
|
if req.is_lightning():
|
2020-06-22 22:37:58 +02:00
|
|
|
assert isinstance(req, LNInvoice)
|
2020-05-31 12:49:49 +02:00
|
|
|
key = req.rhash
|
2019-09-08 11:59:03 +02:00
|
|
|
icon = read_QIcon("lightning.png")
|
|
|
|
|
tooltip = 'lightning request'
|
2020-05-31 12:49:49 +02:00
|
|
|
else:
|
2020-06-22 22:37:58 +02:00
|
|
|
assert isinstance(req, OnchainInvoice)
|
2020-05-31 12:49:49 +02:00
|
|
|
key = req.get_address()
|
2019-09-08 11:59:03 +02:00
|
|
|
icon = read_QIcon("bitcoin.png")
|
|
|
|
|
tooltip = 'onchain request'
|
2018-11-27 21:32:55 +01:00
|
|
|
items = [QStandardItem(e) for e in labels]
|
|
|
|
|
self.set_editability(items)
|
2019-06-10 14:05:02 +02:00
|
|
|
items[self.Columns.DATE].setData(request_type, ROLE_REQUEST_TYPE)
|
2019-09-08 11:59:03 +02:00
|
|
|
items[self.Columns.DATE].setData(key, ROLE_KEY)
|
2020-05-15 15:17:47 +02:00
|
|
|
items[self.Columns.DATE].setData(timestamp, ROLE_SORT_ORDER)
|
2019-09-08 11:59:03 +02:00
|
|
|
items[self.Columns.DATE].setIcon(icon)
|
2019-01-30 11:10:11 +01:00
|
|
|
items[self.Columns.STATUS].setIcon(read_QIcon(pr_icons.get(status)))
|
2019-09-08 11:59:03 +02:00
|
|
|
items[self.Columns.DATE].setToolTip(tooltip)
|
2020-05-15 15:17:47 +02:00
|
|
|
self.std_model.insertRow(self.std_model.rowCount(), items)
|
2019-04-19 19:12:42 +02:00
|
|
|
self.filter()
|
2020-05-15 15:17:47 +02:00
|
|
|
self.proxy.setDynamicSortFilter(True)
|
2019-01-23 17:22:58 +01:00
|
|
|
# sort requests by date
|
2020-03-03 11:25:54 +01:00
|
|
|
self.sortByColumn(self.Columns.DATE, Qt.DescendingOrder)
|
2019-01-30 11:10:11 +01:00
|
|
|
# hide list if empty
|
|
|
|
|
if self.parent.isVisible():
|
2020-05-15 15:17:47 +02:00
|
|
|
b = self.std_model.rowCount() > 0
|
2019-01-30 11:10:11 +01:00
|
|
|
self.setVisible(b)
|
|
|
|
|
self.parent.receive_requests_label.setVisible(b)
|
2020-02-23 21:23:56 +01:00
|
|
|
if not b:
|
|
|
|
|
# list got hidden, so selected item should also be cleared:
|
|
|
|
|
self.item_changed(None)
|
2016-05-27 09:56:53 +02:00
|
|
|
|
|
|
|
|
def create_menu(self, position):
|
2020-03-03 12:56:44 +01:00
|
|
|
items = self.selected_in_column(0)
|
|
|
|
|
if len(items)>1:
|
|
|
|
|
keys = [ item.data(ROLE_KEY) for item in items]
|
|
|
|
|
menu = QMenu(self)
|
|
|
|
|
menu.addAction(_("Delete requests"), lambda: self.parent.delete_requests(keys))
|
|
|
|
|
menu.exec_(self.viewport().mapToGlobal(position))
|
|
|
|
|
return
|
2018-11-27 21:32:55 +01:00
|
|
|
idx = self.indexAt(position)
|
|
|
|
|
# TODO use siblingAtColumn when min Qt version is >=5.11
|
2020-05-15 15:17:47 +02:00
|
|
|
item = self.item_from_index(idx.sibling(idx.row(), self.Columns.DATE))
|
2018-12-04 18:56:30 +01:00
|
|
|
if not item:
|
2016-05-28 07:43:01 +02:00
|
|
|
return
|
2019-09-03 14:44:33 +02:00
|
|
|
key = item.data(ROLE_KEY)
|
|
|
|
|
req = self.wallet.get_request(key)
|
2018-05-02 11:56:03 +02:00
|
|
|
if req is None:
|
|
|
|
|
self.update()
|
|
|
|
|
return
|
2016-05-27 09:56:53 +02:00
|
|
|
menu = QMenu(self)
|
2019-10-16 15:50:18 +02:00
|
|
|
self.add_copy_menu(menu, idx)
|
2020-05-31 12:49:49 +02:00
|
|
|
if req.is_lightning():
|
|
|
|
|
menu.addAction(_("Copy Request"), lambda: self.parent.do_copy(req.invoice, title='Lightning Request'))
|
2019-09-04 12:52:32 +02:00
|
|
|
else:
|
2020-05-31 12:49:49 +02:00
|
|
|
URI = self.wallet.get_request_URI(req)
|
|
|
|
|
menu.addAction(_("Copy Request"), lambda: self.parent.do_copy(URI, title='Bitcoin URI'))
|
|
|
|
|
menu.addAction(_("Copy Address"), lambda: self.parent.do_copy(req.get_address(), title='Bitcoin Address'))
|
|
|
|
|
#if 'view_url' in req:
|
|
|
|
|
# menu.addAction(_("View in web browser"), lambda: webopen(req['view_url']))
|
2020-03-03 12:56:44 +01:00
|
|
|
menu.addAction(_("Delete"), lambda: self.parent.delete_requests([key]))
|
2019-09-03 14:44:33 +02:00
|
|
|
run_hook('receive_list_menu', menu, key)
|
|
|
|
|
menu.exec_(self.viewport().mapToGlobal(position))
|