1 // Copyright (c) 2011-2015 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.
5 #include "recentrequeststablemodel.h"
7 #include "bitcoinunits.h"
9 #include "optionsmodel.h"
11 #include "clientversion.h"
14 #include <boost/foreach.hpp>
16 RecentRequestsTableModel::RecentRequestsTableModel(CWallet
*wallet
, WalletModel
*parent
) :
17 QAbstractTableModel(parent
), walletModel(parent
)
20 nReceiveRequestsMaxId
= 0;
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
);
28 /* These columns must match the indices in the ColumnIndex enumeration */
29 columns
<< tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
31 connect(walletModel
->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
34 RecentRequestsTableModel::~RecentRequestsTableModel()
36 /* Intentionally left empty */
39 int RecentRequestsTableModel::rowCount(const QModelIndex
&parent
) const
46 int RecentRequestsTableModel::columnCount(const QModelIndex
&parent
) const
50 return columns
.length();
53 QVariant
RecentRequestsTableModel::data(const QModelIndex
&index
, int role
) const
55 if(!index
.isValid() || index
.row() >= list
.length())
58 const RecentRequestEntry
*rec
= &list
[index
.row()];
60 if(role
== Qt::DisplayRole
|| role
== Qt::EditRole
)
62 switch(index
.column())
65 return GUIUtil::dateTimeStr(rec
->date
);
67 if(rec
->recipient
.label
.isEmpty() && role
== Qt::DisplayRole
)
69 return tr("(no label)");
73 return rec
->recipient
.label
;
76 if(rec
->recipient
.message
.isEmpty() && role
== Qt::DisplayRole
)
78 return tr("(no message)");
82 return rec
->recipient
.message
;
85 if (rec
->recipient
.amount
== 0 && role
== Qt::DisplayRole
)
86 return tr("(no amount requested)");
87 else if (role
== Qt::EditRole
)
88 return BitcoinUnits::format(walletModel
->getOptionsModel()->getDisplayUnit(), rec
->recipient
.amount
, false, BitcoinUnits::separatorNever
);
90 return BitcoinUnits::format(walletModel
->getOptionsModel()->getDisplayUnit(), rec
->recipient
.amount
);
93 else if (role
== Qt::TextAlignmentRole
)
95 if (index
.column() == Amount
)
96 return (int)(Qt::AlignRight
|Qt::AlignVCenter
);
101 bool RecentRequestsTableModel::setData(const QModelIndex
&index
, const QVariant
&value
, int role
)
106 QVariant
RecentRequestsTableModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
108 if(orientation
== Qt::Horizontal
)
110 if(role
== Qt::DisplayRole
&& section
< columns
.size())
112 return columns
[section
];
118 /** Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table headers to react. */
119 void RecentRequestsTableModel::updateAmountColumnTitle()
121 columns
[Amount
] = getAmountTitle();
122 Q_EMIT
headerDataChanged(Qt::Horizontal
,Amount
,Amount
);
125 /** Gets title for amount column including current display unit if optionsModel reference available. */
126 QString
RecentRequestsTableModel::getAmountTitle()
128 return (this->walletModel
->getOptionsModel() != NULL
) ? tr("Requested") + " ("+BitcoinUnits::name(this->walletModel
->getOptionsModel()->getDisplayUnit()) + ")" : "";
131 QModelIndex
RecentRequestsTableModel::index(int row
, int column
, const QModelIndex
&parent
) const
135 return createIndex(row
, column
);
138 bool RecentRequestsTableModel::removeRows(int row
, int count
, const QModelIndex
&parent
)
142 if(count
> 0 && row
>= 0 && (row
+count
) <= list
.size())
144 const RecentRequestEntry
*rec
;
145 for (int i
= 0; i
< count
; ++i
)
148 if (!walletModel
->saveReceiveRequest(rec
->recipient
.address
.toStdString(), rec
->id
, ""))
152 beginRemoveRows(parent
, row
, row
+ count
- 1);
153 list
.erase(list
.begin() + row
, list
.begin() + row
+ count
);
161 Qt::ItemFlags
RecentRequestsTableModel::flags(const QModelIndex
&index
) const
163 return Qt::ItemIsSelectable
| Qt::ItemIsEnabled
;
166 // called when adding a request from the GUI
167 void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient
&recipient
)
169 RecentRequestEntry newEntry
;
170 newEntry
.id
= ++nReceiveRequestsMaxId
;
171 newEntry
.date
= QDateTime::currentDateTime();
172 newEntry
.recipient
= recipient
;
174 CDataStream
ss(SER_DISK
, CLIENT_VERSION
);
177 if (!walletModel
->saveReceiveRequest(recipient
.address
.toStdString(), newEntry
.id
, ss
.str()))
180 addNewRequest(newEntry
);
183 // called from ctor when loading from wallet
184 void RecentRequestsTableModel::addNewRequest(const std::string
&recipient
)
186 std::vector
<char> data(recipient
.begin(), recipient
.end());
187 CDataStream
ss(data
, SER_DISK
, CLIENT_VERSION
);
189 RecentRequestEntry entry
;
192 if (entry
.id
== 0) // should not happen
195 if (entry
.id
> nReceiveRequestsMaxId
)
196 nReceiveRequestsMaxId
= entry
.id
;
198 addNewRequest(entry
);
201 // actually add to table in GUI
202 void RecentRequestsTableModel::addNewRequest(RecentRequestEntry
&recipient
)
204 beginInsertRows(QModelIndex(), 0, 0);
205 list
.prepend(recipient
);
209 void RecentRequestsTableModel::sort(int column
, Qt::SortOrder order
)
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()));
215 void RecentRequestsTableModel::updateDisplayUnit()
217 updateAmountColumnTitle();
220 bool RecentRequestEntryLessThan::operator()(RecentRequestEntry
&left
, RecentRequestEntry
&right
) const
222 RecentRequestEntry
*pLeft
= &left
;
223 RecentRequestEntry
*pRight
= &right
;
224 if (order
== Qt::DescendingOrder
)
225 std::swap(pLeft
, pRight
);
229 case RecentRequestsTableModel::Date
:
230 return pLeft
->date
.toTime_t() < pRight
->date
.toTime_t();
231 case RecentRequestsTableModel::Label
:
232 return pLeft
->recipient
.label
< pRight
->recipient
.label
;
233 case RecentRequestsTableModel::Message
:
234 return pLeft
->recipient
.message
< pRight
->recipient
.message
;
235 case RecentRequestsTableModel::Amount
:
236 return pLeft
->recipient
.amount
< pRight
->recipient
.amount
;
238 return pLeft
->id
< pRight
->id
;