Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
recentrequeststablemodel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2014 The Bitcoin developers
2 // Distributed under the MIT/X11 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 
12  walletModel(parent)
13 {
14  Q_UNUSED(wallet);
16 
17  // Load entries from wallet
18  std::vector<std::string> vReceiveRequests;
19  parent->loadReceiveRequests(vReceiveRequests);
20  BOOST_FOREACH(const std::string& request, vReceiveRequests)
21  addNewRequest(request);
22 
23  /* These columns must match the indices in the ColumnIndex enumeration */
24  columns << tr("Date") << tr("Label") << tr("Message") << tr("Amount");
25 }
26 
28 {
29  /* Intentionally left empty */
30 }
31 
32 int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
33 {
34  Q_UNUSED(parent);
35 
36  return list.length();
37 }
38 
39 int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const
40 {
41  Q_UNUSED(parent);
42 
43  return columns.length();
44 }
45 
46 QVariant RecentRequestsTableModel::data(const QModelIndex &index, int role) const
47 {
48  if(!index.isValid() || index.row() >= list.length())
49  return QVariant();
50 
51  const RecentRequestEntry *rec = &list[index.row()];
52 
53  if(role == Qt::DisplayRole || role == Qt::EditRole)
54  {
55  switch(index.column())
56  {
57  case Date:
58  return GUIUtil::dateTimeStr(rec->date);
59  case Label:
60  if(rec->recipient.label.isEmpty() && role == Qt::DisplayRole)
61  {
62  return tr("(no label)");
63  }
64  else
65  {
66  return rec->recipient.label;
67  }
68  case Message:
69  if(rec->recipient.message.isEmpty() && role == Qt::DisplayRole)
70  {
71  return tr("(no message)");
72  }
73  else
74  {
75  return rec->recipient.message;
76  }
77  case Amount:
78  if (rec->recipient.amount == 0 && role == Qt::DisplayRole)
79  return tr("(no amount)");
80  else
82  }
83  }
84  return QVariant();
85 }
86 
87 bool RecentRequestsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
88 {
89  return true;
90 }
91 
92 QVariant RecentRequestsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
93 {
94  if(orientation == Qt::Horizontal)
95  {
96  if(role == Qt::DisplayRole && section < columns.size())
97  {
98  return columns[section];
99  }
100  }
101  return QVariant();
102 }
103 
104 QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const
105 {
106  Q_UNUSED(parent);
107 
108  return createIndex(row, column);
109 }
110 
111 bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex &parent)
112 {
113  Q_UNUSED(parent);
114 
115  if(count > 0 && row >= 0 && (row+count) <= list.size())
116  {
117  const RecentRequestEntry *rec;
118  for (int i = 0; i < count; ++i)
119  {
120  rec = &list[row+i];
121  if (!walletModel->saveReceiveRequest(rec->recipient.address.toStdString(), rec->id, ""))
122  return false;
123  }
124 
125  beginRemoveRows(parent, row, row + count - 1);
126  list.erase(list.begin() + row, list.begin() + row + count);
127  endRemoveRows();
128  return true;
129  } else {
130  return false;
131  }
132 }
133 
134 Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const
135 {
136  return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
137 }
138 
139 // called when adding a request from the GUI
141 {
142  RecentRequestEntry newEntry;
143  newEntry.id = ++nReceiveRequestsMaxId;
144  newEntry.date = QDateTime::currentDateTime();
145  newEntry.recipient = recipient;
146 
148  ss << newEntry;
149 
150  if (!walletModel->saveReceiveRequest(recipient.address.toStdString(), newEntry.id, ss.str()))
151  return;
152 
153  addNewRequest(newEntry);
154 }
155 
156 // called from ctor when loading from wallet
157 void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
158 {
159  std::vector<char> data(recipient.begin(), recipient.end());
161 
163  ss >> entry;
164 
165  if (entry.id == 0) // should not happen
166  return;
167 
168  if (entry.id > nReceiveRequestsMaxId)
169  nReceiveRequestsMaxId = entry.id;
170 
171  addNewRequest(entry);
172 }
173 
174 // actually add to table in GUI
176 {
177  beginInsertRows(QModelIndex(), 0, 0);
178  list.prepend(recipient);
179  endInsertRows();
180 }
181 
182 void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
183 {
184  qSort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
185  emit dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
186 }
187 
189 {
190  RecentRequestEntry *pLeft = &left;
191  RecentRequestEntry *pRight = &right;
192  if (order == Qt::DescendingOrder)
193  std::swap(pLeft, pRight);
194 
195  switch(column)
196  {
198  return pLeft->date.toTime_t() < pRight->date.toTime_t();
200  return pLeft->recipient.label < pRight->recipient.label;
202  return pLeft->recipient.message < pRight->recipient.message;
204  return pLeft->recipient.amount < pRight->recipient.amount;
205  default:
206  return pLeft->id < pRight->id;
207  }
208 }
void loadReceiveRequests(std::vector< std::string > &vReceiveRequests)
bool setData(const QModelIndex &index, const QVariant &value, int role)
void addNewRequest(const SendCoinsRecipient &recipient)
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
QModelIndex index(int row, int column, const QModelIndex &parent) const
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
static const int CLIENT_VERSION
Definition: version.h:15
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:73
Double ended buffer combining vector and stream-like interfaces.
Definition: serialize.h:839
RecentRequestsTableModel(CWallet *wallet, WalletModel *parent)
const RecentRequestEntry & entry(int row) const
Qt::ItemFlags flags(const QModelIndex &index) const
int64_t id
QDateTime date
std::string str() const
Definition: serialize.h:915
QVariant data(const QModelIndex &index, int role) const
QList< RecentRequestEntry > list
int rowCount(const QModelIndex &parent) const
SendCoinsRecipient recipient
Qt::SortOrder order
int getDisplayUnit()
Definition: optionsmodel.h:58
bool operator()(RecentRequestEntry &left, RecentRequestEntry &right) const
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:96
QVariant headerData(int section, Qt::Orientation orientation, int role) const
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:100
int columnCount(const QModelIndex &parent) const
int column
bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest)
static QString format(int unit, qint64 amount, bool plussign=false)
Format as string.
OptionsModel * getOptionsModel()