Bitcoin Core  22.99.0
P2P Digital Currency
recentrequeststablemodel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
6 
7 #include <qt/bitcoinunits.h>
8 #include <qt/guiutil.h>
9 #include <qt/optionsmodel.h>
10 #include <qt/walletmodel.h>
11 
12 #include <clientversion.h>
13 #include <interfaces/wallet.h>
14 #include <key_io.h>
15 #include <streams.h>
16 #include <util/string.h>
17 
18 #include <utility>
19 
20 #include <QLatin1Char>
21 #include <QLatin1String>
22 
24  QAbstractTableModel(parent), walletModel(parent)
25 {
26  // Load entries from wallet
27  for (const std::string& request : parent->wallet().getAddressReceiveRequests()) {
28  addNewRequest(request);
29  }
30 
31  /* These columns must match the indices in the ColumnIndex enumeration */
32  columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
33 
35 }
36 
38 {
39  /* Intentionally left empty */
40 }
41 
42 int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
43 {
44  if (parent.isValid()) {
45  return 0;
46  }
47  return list.length();
48 }
49 
50 int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const
51 {
52  if (parent.isValid()) {
53  return 0;
54  }
55  return columns.length();
56 }
57 
58 QVariant RecentRequestsTableModel::data(const QModelIndex &index, int role) const
59 {
60  if(!index.isValid() || index.row() >= list.length())
61  return QVariant();
62 
63  if(role == Qt::DisplayRole || role == Qt::EditRole)
64  {
65  const RecentRequestEntry *rec = &list[index.row()];
66  switch(index.column())
67  {
68  case Date:
69  return GUIUtil::dateTimeStr(rec->date);
70  case Label:
71  if(rec->recipient.label.isEmpty() && role == Qt::DisplayRole)
72  {
73  return tr("(no label)");
74  }
75  else
76  {
77  return rec->recipient.label;
78  }
79  case Message:
80  if(rec->recipient.message.isEmpty() && role == Qt::DisplayRole)
81  {
82  return tr("(no message)");
83  }
84  else
85  {
86  return rec->recipient.message;
87  }
88  case Amount:
89  if (rec->recipient.amount == 0 && role == Qt::DisplayRole)
90  return tr("(no amount requested)");
91  else if (role == Qt::EditRole)
93  else
95  }
96  }
97  else if (role == Qt::TextAlignmentRole)
98  {
99  if (index.column() == Amount)
100  return (int)(Qt::AlignRight|Qt::AlignVCenter);
101  }
102  return QVariant();
103 }
104 
105 bool RecentRequestsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
106 {
107  return true;
108 }
109 
110 QVariant RecentRequestsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
111 {
112  if(orientation == Qt::Horizontal)
113  {
114  if(role == Qt::DisplayRole && section < columns.size())
115  {
116  return columns[section];
117  }
118  }
119  return QVariant();
120 }
121 
124 {
126  Q_EMIT headerDataChanged(Qt::Horizontal,Amount,Amount);
127 }
128 
131 {
132  if (!walletModel->getOptionsModel()) return {};
133  return tr("Requested") +
134  QLatin1String(" (") +
136  QLatin1Char(')');
137 }
138 
139 QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const
140 {
141  Q_UNUSED(parent);
142 
143  return createIndex(row, column);
144 }
145 
146 bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex &parent)
147 {
148  Q_UNUSED(parent);
149 
150  if(count > 0 && row >= 0 && (row+count) <= list.size())
151  {
152  for (int i = 0; i < count; ++i)
153  {
154  const RecentRequestEntry* rec = &list[row+i];
156  return false;
157  }
158 
159  beginRemoveRows(parent, row, row + count - 1);
160  list.erase(list.begin() + row, list.begin() + row + count);
161  endRemoveRows();
162  return true;
163  } else {
164  return false;
165  }
166 }
167 
168 Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const
169 {
170  return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
171 }
172 
173 // called when adding a request from the GUI
175 {
176  RecentRequestEntry newEntry;
177  newEntry.id = ++nReceiveRequestsMaxId;
178  newEntry.date = QDateTime::currentDateTime();
179  newEntry.recipient = recipient;
180 
182  ss << newEntry;
183 
184  if (!walletModel->wallet().setAddressReceiveRequest(DecodeDestination(recipient.address.toStdString()), ToString(newEntry.id), ss.str()))
185  return;
186 
187  addNewRequest(newEntry);
188 }
189 
190 // called from ctor when loading from wallet
191 void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
192 {
193  std::vector<uint8_t> data(recipient.begin(), recipient.end());
195 
197  ss >> entry;
198 
199  if (entry.id == 0) // should not happen
200  return;
201 
204 
206 }
207 
208 // actually add to table in GUI
210 {
211  beginInsertRows(QModelIndex(), 0, 0);
212  list.prepend(recipient);
213  endInsertRows();
214 }
215 
216 void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
217 {
218  std::sort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
219  Q_EMIT dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
220 }
221 
223 {
225 }
226 
228 {
229  const RecentRequestEntry* pLeft = &left;
230  const RecentRequestEntry* pRight = &right;
231  if (order == Qt::DescendingOrder)
232  std::swap(pLeft, pRight);
233 
234  switch(column)
235  {
237  return pLeft->date.toTime_t() < pRight->date.toTime_t();
239  return pLeft->recipient.label < pRight->recipient.label;
241  return pLeft->recipient.message < pRight->recipient.message;
243  return pLeft->recipient.amount < pRight->recipient.amount;
244  default:
245  return pLeft->id < pRight->id;
246  }
247 }
SendCoinsRecipient::amount
CAmount amount
Definition: sendcoinsrecipient.h:33
RecentRequestsTableModel::list
QList< RecentRequestEntry > list
Definition: recentrequeststablemodel.h:90
WalletModel::getOptionsModel
OptionsModel * getOptionsModel()
Definition: walletmodel.cpp:284
RecentRequestEntry::recipient
SendCoinsRecipient recipient
Definition: recentrequeststablemodel.h:25
BitcoinUnits::SeparatorStyle::NEVER
@ NEVER
RecentRequestEntryLessThan
Definition: recentrequeststablemodel.h:35
ToString
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:79
count
static int count
Definition: tests.c:41
RecentRequestEntry::date
QDateTime date
Definition: recentrequeststablemodel.h:24
SER_DISK
@ SER_DISK
Definition: serialize.h:139
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:51
RecentRequestsTableModel::Amount
@ Amount
Definition: recentrequeststablemodel.h:62
key_io.h
streams.h
RecentRequestsTableModel::entry
const RecentRequestEntry & entry(int row) const
Definition: recentrequeststablemodel.h:79
walletmodel.h
string.h
interfaces::Wallet::setAddressReceiveRequest
virtual bool setAddressReceiveRequest(const CTxDestination &dest, const std::string &id, const std::string &value)=0
Save or remove receive request.
SendCoinsRecipient::label
QString label
Definition: sendcoinsrecipient.h:32
RecentRequestsTableModel::columnCount
int columnCount(const QModelIndex &parent) const override
Definition: recentrequeststablemodel.cpp:50
RecentRequestsTableModel::rowCount
int rowCount(const QModelIndex &parent) const override
Definition: recentrequeststablemodel.cpp:42
RecentRequestsTableModel::updateAmountColumnTitle
void updateAmountColumnTitle()
Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table hea...
Definition: recentrequeststablemodel.cpp:123
OptionsModel::displayUnitChanged
void displayUnitChanged(int unit)
clientversion.h
RecentRequestsTableModel::walletModel
WalletModel * walletModel
Definition: recentrequeststablemodel.h:88
RecentRequestsTableModel::updateDisplayUnit
void updateDisplayUnit()
Definition: recentrequeststablemodel.cpp:222
interfaces::Wallet::getAddressReceiveRequests
virtual std::vector< std::string > getAddressReceiveRequests()=0
Get receive requests.
RecentRequestsTableModel::columns
QStringList columns
Definition: recentrequeststablemodel.h:89
RecentRequestsTableModel::removeRows
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Definition: recentrequeststablemodel.cpp:146
SendCoinsRecipient
Definition: sendcoinsrecipient.h:19
WalletModel::wallet
interfaces::Wallet & wallet() const
Definition: walletmodel.h:144
RecentRequestsTableModel::RecentRequestsTableModel
RecentRequestsTableModel(WalletModel *parent)
Definition: recentrequeststablemodel.cpp:23
RecentRequestsTableModel::getAmountTitle
QString getAmountTitle()
Gets title for amount column including current display unit if optionsModel reference available.
Definition: recentrequeststablemodel.cpp:130
recentrequeststablemodel.h
RecentRequestsTableModel::NUMBER_OF_COLUMNS
@ NUMBER_OF_COLUMNS
Definition: recentrequeststablemodel.h:63
RecentRequestsTableModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Definition: recentrequeststablemodel.cpp:105
RecentRequestEntryLessThan::order
Qt::SortOrder order
Definition: recentrequeststablemodel.h:44
guiutil.h
RecentRequestsTableModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: recentrequeststablemodel.cpp:168
DecodeDestination
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg)
Definition: key_io.cpp:261
RecentRequestsTableModel::addNewRequest
void addNewRequest(const SendCoinsRecipient &recipient)
Definition: recentrequeststablemodel.cpp:174
RecentRequestsTableModel::nReceiveRequestsMaxId
int64_t nReceiveRequestsMaxId
Definition: recentrequeststablemodel.h:91
RecentRequestsTableModel::~RecentRequestsTableModel
~RecentRequestsTableModel()
Definition: recentrequeststablemodel.cpp:37
RecentRequestsTableModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: recentrequeststablemodel.cpp:139
RecentRequestsTableModel::Date
@ Date
Definition: recentrequeststablemodel.h:59
RecentRequestEntry::id
int64_t id
Definition: recentrequeststablemodel.h:23
OptionsModel::getDisplayUnit
int getDisplayUnit() const
Definition: optionsmodel.h:87
SendCoinsRecipient::address
QString address
Definition: sendcoinsrecipient.h:31
RecentRequestsTableModel::Label
@ Label
Definition: recentrequeststablemodel.h:60
CLIENT_VERSION
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
RecentRequestEntryLessThan::operator()
bool operator()(const RecentRequestEntry &left, const RecentRequestEntry &right) const
Definition: recentrequeststablemodel.cpp:227
bitcoinunits.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
RecentRequestsTableModel::sort
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
Definition: recentrequeststablemodel.cpp:216
wallet.h
RecentRequestsTableModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: recentrequeststablemodel.cpp:58
RecentRequestsTableModel::Message
@ Message
Definition: recentrequeststablemodel.h:61
GUIUtil::dateTimeStr
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:77
BitcoinUnits::shortName
static QString shortName(int unit)
Short name.
Definition: bitcoinunits.cpp:55
optionsmodel.h
RecentRequestsTableModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Definition: recentrequeststablemodel.cpp:110
CDataStream::str
std::string str() const
Definition: streams.h:242
RecentRequestEntryLessThan::column
int column
Definition: recentrequeststablemodel.h:43
RecentRequestEntry
Definition: recentrequeststablemodel.h:16
BitcoinUnits::format
static QString format(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
Definition: bitcoinunits.cpp:101
SendCoinsRecipient::message
QString message
Definition: sendcoinsrecipient.h:35