Line data Source code
1 : // Copyright (c) 2011-2014 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 :
5 : #include "recentrequeststablemodel.h"
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 :
16 0 : RecentRequestsTableModel::RecentRequestsTableModel(CWallet *wallet, WalletModel *parent) :
17 0 : walletModel(parent)
18 : {
19 : Q_UNUSED(wallet);
20 0 : nReceiveRequestsMaxId = 0;
21 :
22 : // Load entries from wallet
23 : std::vector<std::string> vReceiveRequests;
24 0 : parent->loadReceiveRequests(vReceiveRequests);
25 0 : BOOST_FOREACH(const std::string& request, vReceiveRequests)
26 0 : addNewRequest(request);
27 :
28 : /* These columns must match the indices in the ColumnIndex enumeration */
29 0 : columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
30 :
31 0 : connect(walletModel->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
32 0 : }
33 :
34 0 : RecentRequestsTableModel::~RecentRequestsTableModel()
35 : {
36 : /* Intentionally left empty */
37 0 : }
38 :
39 0 : int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
40 : {
41 : Q_UNUSED(parent);
42 :
43 0 : return list.length();
44 : }
45 :
46 0 : int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const
47 : {
48 : Q_UNUSED(parent);
49 :
50 0 : return columns.length();
51 : }
52 :
53 0 : QVariant RecentRequestsTableModel::data(const QModelIndex &index, int role) const
54 : {
55 0 : if(!index.isValid() || index.row() >= list.length())
56 : return QVariant();
57 :
58 0 : const RecentRequestEntry *rec = &list[index.row()];
59 :
60 0 : if(role == Qt::DisplayRole || role == Qt::EditRole)
61 : {
62 0 : switch(index.column())
63 : {
64 : case Date:
65 0 : return GUIUtil::dateTimeStr(rec->date);
66 : case Label:
67 0 : if(rec->recipient.label.isEmpty() && role == Qt::DisplayRole)
68 : {
69 0 : return tr("(no label)");
70 : }
71 : else
72 : {
73 0 : return rec->recipient.label;
74 : }
75 : case Message:
76 0 : if(rec->recipient.message.isEmpty() && role == Qt::DisplayRole)
77 : {
78 0 : return tr("(no message)");
79 : }
80 : else
81 : {
82 0 : return rec->recipient.message;
83 : }
84 : case Amount:
85 0 : if (rec->recipient.amount == 0 && role == Qt::DisplayRole)
86 0 : return tr("(no amount)");
87 0 : else if (role == Qt::EditRole)
88 0 : return BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), rec->recipient.amount, false, BitcoinUnits::separatorNever);
89 : else
90 0 : return BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), rec->recipient.amount);
91 : }
92 : }
93 0 : else if (role == Qt::TextAlignmentRole)
94 : {
95 0 : if (index.column() == Amount)
96 0 : return (int)(Qt::AlignRight|Qt::AlignVCenter);
97 : }
98 : return QVariant();
99 : }
100 :
101 0 : bool RecentRequestsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
102 : {
103 0 : return true;
104 : }
105 :
106 0 : QVariant RecentRequestsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
107 : {
108 0 : if(orientation == Qt::Horizontal)
109 : {
110 0 : if(role == Qt::DisplayRole && section < columns.size())
111 : {
112 0 : return columns[section];
113 : }
114 : }
115 : return QVariant();
116 : }
117 :
118 : /** Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table headers to react. */
119 0 : void RecentRequestsTableModel::updateAmountColumnTitle()
120 : {
121 0 : columns[Amount] = getAmountTitle();
122 0 : Q_EMIT headerDataChanged(Qt::Horizontal,Amount,Amount);
123 0 : }
124 :
125 : /** Gets title for amount column including current display unit if optionsModel reference available. */
126 0 : QString RecentRequestsTableModel::getAmountTitle()
127 : {
128 : QString amountTitle = tr("Amount");
129 0 : if (this->walletModel->getOptionsModel() != NULL)
130 : {
131 0 : amountTitle += " ("+BitcoinUnits::name(this->walletModel->getOptionsModel()->getDisplayUnit()) + ")";
132 : }
133 0 : return amountTitle;
134 : }
135 :
136 0 : QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const
137 : {
138 : Q_UNUSED(parent);
139 :
140 0 : return createIndex(row, column);
141 : }
142 :
143 0 : bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex &parent)
144 : {
145 : Q_UNUSED(parent);
146 :
147 0 : if(count > 0 && row >= 0 && (row+count) <= list.size())
148 : {
149 : const RecentRequestEntry *rec;
150 0 : for (int i = 0; i < count; ++i)
151 : {
152 0 : rec = &list[row+i];
153 0 : if (!walletModel->saveReceiveRequest(rec->recipient.address.toStdString(), rec->id, ""))
154 : return false;
155 : }
156 :
157 0 : beginRemoveRows(parent, row, row + count - 1);
158 0 : list.erase(list.begin() + row, list.begin() + row + count);
159 0 : endRemoveRows();
160 0 : return true;
161 : } else {
162 : return false;
163 : }
164 : }
165 :
166 0 : Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const
167 : {
168 0 : return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
169 : }
170 :
171 : // called when adding a request from the GUI
172 0 : void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient &recipient)
173 : {
174 0 : RecentRequestEntry newEntry;
175 0 : newEntry.id = ++nReceiveRequestsMaxId;
176 0 : newEntry.date = QDateTime::currentDateTime();
177 0 : newEntry.recipient = recipient;
178 :
179 : CDataStream ss(SER_DISK, CLIENT_VERSION);
180 : ss << newEntry;
181 :
182 0 : if (!walletModel->saveReceiveRequest(recipient.address.toStdString(), newEntry.id, ss.str()))
183 0 : return;
184 :
185 0 : addNewRequest(newEntry);
186 : }
187 :
188 : // called from ctor when loading from wallet
189 0 : void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
190 : {
191 0 : std::vector<char> data(recipient.begin(), recipient.end());
192 : CDataStream ss(data, SER_DISK, CLIENT_VERSION);
193 :
194 0 : RecentRequestEntry entry;
195 : ss >> entry;
196 :
197 0 : if (entry.id == 0) // should not happen
198 0 : return;
199 :
200 0 : if (entry.id > nReceiveRequestsMaxId)
201 0 : nReceiveRequestsMaxId = entry.id;
202 :
203 0 : addNewRequest(entry);
204 : }
205 :
206 : // actually add to table in GUI
207 0 : void RecentRequestsTableModel::addNewRequest(RecentRequestEntry &recipient)
208 : {
209 0 : beginInsertRows(QModelIndex(), 0, 0);
210 0 : list.prepend(recipient);
211 0 : endInsertRows();
212 0 : }
213 :
214 0 : void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
215 : {
216 0 : qSort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
217 0 : Q_EMIT dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
218 0 : }
219 :
220 0 : void RecentRequestsTableModel::updateDisplayUnit()
221 : {
222 0 : updateAmountColumnTitle();
223 0 : }
224 :
225 0 : bool RecentRequestEntryLessThan::operator()(RecentRequestEntry &left, RecentRequestEntry &right) const
226 : {
227 0 : RecentRequestEntry *pLeft = &left;
228 0 : RecentRequestEntry *pRight = &right;
229 0 : if (order == Qt::DescendingOrder)
230 : std::swap(pLeft, pRight);
231 :
232 0 : switch(column)
233 : {
234 : case RecentRequestsTableModel::Date:
235 0 : return pLeft->date.toTime_t() < pRight->date.toTime_t();
236 : case RecentRequestsTableModel::Label:
237 0 : return pLeft->recipient.label < pRight->recipient.label;
238 : case RecentRequestsTableModel::Message:
239 0 : return pLeft->recipient.message < pRight->recipient.message;
240 : case RecentRequestsTableModel::Amount:
241 0 : return pLeft->recipient.amount < pRight->recipient.amount;
242 : default:
243 0 : return pLeft->id < pRight->id;
244 : }
245 0 : }
|