Dogecoin Core  1.14.2
P2P Digital Currency
recentrequeststablemodel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 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 "bitcoinunits.h"
8 #include "guiutil.h"
9 #include "optionsmodel.h"
10 
11 #include "clientversion.h"
12 #include "streams.h"
13 
14 #include <boost/foreach.hpp>
15 
17  QAbstractTableModel(parent), walletModel(parent)
18 {
19  Q_UNUSED(wallet);
21 
22  // Load entries from wallet
23  std::vector<std::string> vReceiveRequests;
24  parent->loadReceiveRequests(vReceiveRequests);
25  BOOST_FOREACH(const std::string& request, vReceiveRequests)
26  addNewRequest(request);
27 
28  /* These columns must match the indices in the ColumnIndex enumeration */
29  columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
30 
31  connect(walletModel->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
32 }
33 
35 {
36  /* Intentionally left empty */
37 }
38 
39 int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
40 {
41  Q_UNUSED(parent);
42 
43  return list.length();
44 }
45 
46 int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const
47 {
48  Q_UNUSED(parent);
49 
50  return columns.length();
51 }
52 
53 QVariant RecentRequestsTableModel::data(const QModelIndex &index, int role) const
54 {
55  if(!index.isValid() || index.row() >= list.length())
56  return QVariant();
57 
58  const RecentRequestEntry *rec = &list[index.row()];
59 
60  if(role == Qt::DisplayRole || role == Qt::EditRole)
61  {
62  switch(index.column())
63  {
64  case Date:
65  return GUIUtil::dateTimeStr(rec->date);
66  case Label:
67  if(rec->recipient.label.isEmpty() && role == Qt::DisplayRole)
68  {
69  return tr("(no label)");
70  }
71  else
72  {
73  return rec->recipient.label;
74  }
75  case Message:
76  if(rec->recipient.message.isEmpty() && role == Qt::DisplayRole)
77  {
78  return tr("(no message)");
79  }
80  else
81  {
82  return rec->recipient.message;
83  }
84  case Amount:
85  if (rec->recipient.amount == 0 && role == Qt::DisplayRole)
86  return tr("(no amount requested)");
87  else if (role == Qt::EditRole)
89  else
91  }
92  }
93  else if (role == Qt::TextAlignmentRole)
94  {
95  if (index.column() == Amount)
96  return (int)(Qt::AlignRight|Qt::AlignVCenter);
97  }
98  return QVariant();
99 }
100 
101 bool RecentRequestsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
102 {
103  return true;
104 }
105 
106 QVariant RecentRequestsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
107 {
108  if(orientation == Qt::Horizontal)
109  {
110  if(role == Qt::DisplayRole && section < columns.size())
111  {
112  return columns[section];
113  }
114  }
115  return QVariant();
116 }
117 
120 {
122  Q_EMIT headerDataChanged(Qt::Horizontal,Amount,Amount);
123 }
124 
127 {
128  return (this->walletModel->getOptionsModel() != NULL) ? tr("Requested") + " ("+BitcoinUnits::name(this->walletModel->getOptionsModel()->getDisplayUnit()) + ")" : "";
129 }
130 
131 QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const
132 {
133  Q_UNUSED(parent);
134 
135  return createIndex(row, column);
136 }
137 
138 bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex &parent)
139 {
140  Q_UNUSED(parent);
141 
142  if(count > 0 && row >= 0 && (row+count) <= list.size())
143  {
144  const RecentRequestEntry *rec;
145  for (int i = 0; i < count; ++i)
146  {
147  rec = &list[row+i];
148  if (!walletModel->saveReceiveRequest(rec->recipient.address.toStdString(), rec->id, ""))
149  return false;
150  }
151 
152  beginRemoveRows(parent, row, row + count - 1);
153  list.erase(list.begin() + row, list.begin() + row + count);
154  endRemoveRows();
155  return true;
156  } else {
157  return false;
158  }
159 }
160 
161 Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const
162 {
163  return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
164 }
165 
166 // called when adding a request from the GUI
168 {
169  RecentRequestEntry newEntry;
170  newEntry.id = ++nReceiveRequestsMaxId;
171  newEntry.date = QDateTime::currentDateTime();
172  newEntry.recipient = recipient;
173 
174  CDataStream ss(SER_DISK, CLIENT_VERSION);
175  ss << newEntry;
176 
177  if (!walletModel->saveReceiveRequest(recipient.address.toStdString(), newEntry.id, ss.str()))
178  return;
179 
180  addNewRequest(newEntry);
181 }
182 
183 // called from ctor when loading from wallet
184 void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
185 {
186  std::vector<char> data(recipient.begin(), recipient.end());
187  CDataStream ss(data, SER_DISK, CLIENT_VERSION);
188 
190  ss >> entry;
191 
192  if (entry.id == 0) // should not happen
193  return;
194 
197 
199 }
200 
201 // actually add to table in GUI
203 {
204  beginInsertRows(QModelIndex(), 0, 0);
205  list.prepend(recipient);
206  endInsertRows();
207 }
208 
209 void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
210 {
211  qSort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
212  Q_EMIT dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
213 }
214 
216 {
218 }
219 
221 {
222  RecentRequestEntry *pLeft = &left;
223  RecentRequestEntry *pRight = &right;
224  if (order == Qt::DescendingOrder)
225  std::swap(pLeft, pRight);
226 
227  switch(column)
228  {
230  return pLeft->date.toTime_t() < pRight->date.toTime_t();
232  return pLeft->recipient.label < pRight->recipient.label;
234  return pLeft->recipient.message < pRight->recipient.message;
236  return pLeft->recipient.amount < pRight->recipient.amount;
237  default:
238  return pLeft->id < pRight->id;
239  }
240 }
static QString name(int unit)
Short name.
static QString format(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as string.
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
std::string str() const
Definition: streams.h:224
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,...
Definition: wallet.h:493
int getDisplayUnit()
Definition: optionsmodel.h:65
int64_t id
SendCoinsRecipient recipient
QDateTime date
Qt::SortOrder order
int column
bool operator()(RecentRequestEntry &left, RecentRequestEntry &right) const
int rowCount(const QModelIndex &parent) const
QVariant headerData(int section, Qt::Orientation orientation, int role) const
bool setData(const QModelIndex &index, const QVariant &value, int role)
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
Qt::ItemFlags flags(const QModelIndex &index) const
int columnCount(const QModelIndex &parent) const
const RecentRequestEntry & entry(int row) const
QModelIndex index(int row, int column, const QModelIndex &parent) const
QList< RecentRequestEntry > list
void updateAmountColumnTitle()
Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table hea...
QString getAmountTitle()
Gets title for amount column including current display unit if optionsModel reference available.
RecentRequestsTableModel(CWallet *wallet, WalletModel *parent)
QVariant data(const QModelIndex &index, int role) const
void addNewRequest(const SendCoinsRecipient &recipient)
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:99
void loadReceiveRequests(std::vector< std::string > &vReceiveRequests)
bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest)
OptionsModel * getOptionsModel()
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:86
@ SER_DISK
Definition: serialize.h:147