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.
5 #include "recentrequeststablemodel.h"
7 #include "bitcoinunits.h"
8 #include "clientversion.h"
10 #include "optionsmodel.h"
13 #include <boost/foreach.hpp>
15 RecentRequestsTableModel::RecentRequestsTableModel(CWallet *wallet, WalletModel *parent) :
19 nReceiveRequestsMaxId = 0;
21 // Load entries from wallet
22 std::vector<std::string> vReceiveRequests;
23 parent->loadReceiveRequests(vReceiveRequests);
24 BOOST_FOREACH(const std::string& request, vReceiveRequests)
25 addNewRequest(request);
27 /* These columns must match the indices in the ColumnIndex enumeration */
28 columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
30 connect(walletModel->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
33 RecentRequestsTableModel::~RecentRequestsTableModel()
35 /* Intentionally left empty */
38 int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
45 int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const
49 return columns.length();
52 QVariant RecentRequestsTableModel::data(const QModelIndex &index, int role) const
54 if(!index.isValid() || index.row() >= list.length())
57 const RecentRequestEntry *rec = &list[index.row()];
59 if(role == Qt::DisplayRole || role == Qt::EditRole)
61 switch(index.column())
64 return GUIUtil::dateTimeStr(rec->date);
66 if(rec->recipient.label.isEmpty() && role == Qt::DisplayRole)
68 return tr("(no label)");
72 return rec->recipient.label;
75 if(rec->recipient.message.isEmpty() && role == Qt::DisplayRole)
77 return tr("(no message)");
81 return rec->recipient.message;
84 if (rec->recipient.amount == 0 && role == Qt::DisplayRole)
85 return tr("(no amount)");
86 else if (role == Qt::EditRole)
87 return BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), rec->recipient.amount, false, BitcoinUnits::separatorNever);
89 return BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), rec->recipient.amount);
92 else if (role == Qt::TextAlignmentRole)
94 if (index.column() == Amount)
95 return (int)(Qt::AlignRight|Qt::AlignVCenter);
100 bool RecentRequestsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
105 QVariant RecentRequestsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
107 if(orientation == Qt::Horizontal)
109 if(role == Qt::DisplayRole && section < columns.size())
111 return columns[section];
117 /** Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table headers to react. */
118 void RecentRequestsTableModel::updateAmountColumnTitle()
120 columns[Amount] = getAmountTitle();
121 emit headerDataChanged(Qt::Horizontal,Amount,Amount);
124 /** Gets title for amount column including current display unit if optionsModel reference available. */
125 QString RecentRequestsTableModel::getAmountTitle()
127 QString amountTitle = tr("Amount");
128 if (this->walletModel->getOptionsModel() != NULL)
130 amountTitle += " ("+BitcoinUnits::name(this->walletModel->getOptionsModel()->getDisplayUnit()) + ")";
135 QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const
139 return createIndex(row, column);
142 bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex &parent)
146 if(count > 0 && row >= 0 && (row+count) <= list.size())
148 const RecentRequestEntry *rec;
149 for (int i = 0; i < count; ++i)
152 if (!walletModel->saveReceiveRequest(rec->recipient.address.toStdString(), rec->id, ""))
156 beginRemoveRows(parent, row, row + count - 1);
157 list.erase(list.begin() + row, list.begin() + row + count);
165 Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const
167 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
170 // called when adding a request from the GUI
171 void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient &recipient)
173 RecentRequestEntry newEntry;
174 newEntry.id = ++nReceiveRequestsMaxId;
175 newEntry.date = QDateTime::currentDateTime();
176 newEntry.recipient = recipient;
178 CDataStream ss(SER_DISK, CLIENT_VERSION);
181 if (!walletModel->saveReceiveRequest(recipient.address.toStdString(), newEntry.id, ss.str()))
184 addNewRequest(newEntry);
187 // called from ctor when loading from wallet
188 void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
190 std::vector<char> data(recipient.begin(), recipient.end());
191 CDataStream ss(data, SER_DISK, CLIENT_VERSION);
193 RecentRequestEntry entry;
196 if (entry.id == 0) // should not happen
199 if (entry.id > nReceiveRequestsMaxId)
200 nReceiveRequestsMaxId = entry.id;
202 addNewRequest(entry);
205 // actually add to table in GUI
206 void RecentRequestsTableModel::addNewRequest(RecentRequestEntry &recipient)
208 beginInsertRows(QModelIndex(), 0, 0);
209 list.prepend(recipient);
213 void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
215 qSort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
216 emit dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
219 void RecentRequestsTableModel::updateDisplayUnit()
221 updateAmountColumnTitle();
224 bool RecentRequestEntryLessThan::operator()(RecentRequestEntry &left, RecentRequestEntry &right) const
226 RecentRequestEntry *pLeft = &left;
227 RecentRequestEntry *pRight = &right;
228 if (order == Qt::DescendingOrder)
229 std::swap(pLeft, pRight);
233 case RecentRequestsTableModel::Date:
234 return pLeft->date.toTime_t() < pRight->date.toTime_t();
235 case RecentRequestsTableModel::Label:
236 return pLeft->recipient.label < pRight->recipient.label;
237 case RecentRequestsTableModel::Message:
238 return pLeft->recipient.message < pRight->recipient.message;
239 case RecentRequestsTableModel::Amount:
240 return pLeft->recipient.amount < pRight->recipient.amount;
242 return pLeft->id < pRight->id;