You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

transactiontablemodel.cpp 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. // Copyright (c) 2011-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "transactiontablemodel.h"
  5. #include "addresstablemodel.h"
  6. #include "guiconstants.h"
  7. #include "guiutil.h"
  8. #include "optionsmodel.h"
  9. #include "platformstyle.h"
  10. #include "transactiondesc.h"
  11. #include "transactionrecord.h"
  12. #include "walletmodel.h"
  13. #include "core_io.h"
  14. #include "validation.h"
  15. #include "sync.h"
  16. #include "uint256.h"
  17. #include "util.h"
  18. #include "wallet/wallet.h"
  19. #include <QColor>
  20. #include <QDateTime>
  21. #include <QDebug>
  22. #include <QIcon>
  23. #include <QList>
  24. // Amount column is right-aligned it contains numbers
  25. static int column_alignments[] = {
  26. Qt::AlignLeft|Qt::AlignVCenter, /* status */
  27. Qt::AlignLeft|Qt::AlignVCenter, /* watchonly */
  28. Qt::AlignLeft|Qt::AlignVCenter, /* date */
  29. Qt::AlignLeft|Qt::AlignVCenter, /* type */
  30. Qt::AlignLeft|Qt::AlignVCenter, /* address */
  31. Qt::AlignRight|Qt::AlignVCenter /* amount */
  32. };
  33. // Comparison operator for sort/binary search of model tx list
  34. struct TxLessThan
  35. {
  36. bool operator()(const TransactionRecord &a, const TransactionRecord &b) const
  37. {
  38. return a.hash < b.hash;
  39. }
  40. bool operator()(const TransactionRecord &a, const uint256 &b) const
  41. {
  42. return a.hash < b;
  43. }
  44. bool operator()(const uint256 &a, const TransactionRecord &b) const
  45. {
  46. return a < b.hash;
  47. }
  48. };
  49. // Private implementation
  50. class TransactionTablePriv
  51. {
  52. public:
  53. TransactionTablePriv(CWallet *_wallet, TransactionTableModel *_parent) :
  54. wallet(_wallet),
  55. parent(_parent)
  56. {
  57. }
  58. CWallet *wallet;
  59. TransactionTableModel *parent;
  60. /* Local cache of wallet.
  61. * As it is in the same order as the CWallet, by definition
  62. * this is sorted by sha256.
  63. */
  64. QList<TransactionRecord> cachedWallet;
  65. /* Query entire wallet anew from core.
  66. */
  67. void refreshWallet()
  68. {
  69. qDebug() << "TransactionTablePriv::refreshWallet";
  70. cachedWallet.clear();
  71. {
  72. LOCK2(cs_main, wallet->cs_wallet);
  73. for(std::map<uint256, CWalletTx>::iterator it = wallet->mapWallet.begin(); it != wallet->mapWallet.end(); ++it)
  74. {
  75. if(TransactionRecord::showTransaction(it->second))
  76. cachedWallet.append(TransactionRecord::decomposeTransaction(wallet, it->second));
  77. }
  78. }
  79. }
  80. /* Update our model of the wallet incrementally, to synchronize our model of the wallet
  81. with that of the core.
  82. Call with transaction that was added, removed or changed.
  83. */
  84. void updateWallet(const uint256 &hash, int status, bool showTransaction)
  85. {
  86. qDebug() << "TransactionTablePriv::updateWallet: " + QString::fromStdString(hash.ToString()) + " " + QString::number(status);
  87. // Find bounds of this transaction in model
  88. QList<TransactionRecord>::iterator lower = qLowerBound(
  89. cachedWallet.begin(), cachedWallet.end(), hash, TxLessThan());
  90. QList<TransactionRecord>::iterator upper = qUpperBound(
  91. cachedWallet.begin(), cachedWallet.end(), hash, TxLessThan());
  92. int lowerIndex = (lower - cachedWallet.begin());
  93. int upperIndex = (upper - cachedWallet.begin());
  94. bool inModel = (lower != upper);
  95. if(status == CT_UPDATED)
  96. {
  97. if(showTransaction && !inModel)
  98. status = CT_NEW; /* Not in model, but want to show, treat as new */
  99. if(!showTransaction && inModel)
  100. status = CT_DELETED; /* In model, but want to hide, treat as deleted */
  101. }
  102. qDebug() << " inModel=" + QString::number(inModel) +
  103. " Index=" + QString::number(lowerIndex) + "-" + QString::number(upperIndex) +
  104. " showTransaction=" + QString::number(showTransaction) + " derivedStatus=" + QString::number(status);
  105. switch(status)
  106. {
  107. case CT_NEW:
  108. if(inModel)
  109. {
  110. qWarning() << "TransactionTablePriv::updateWallet: Warning: Got CT_NEW, but transaction is already in model";
  111. break;
  112. }
  113. if(showTransaction)
  114. {
  115. LOCK2(cs_main, wallet->cs_wallet);
  116. // Find transaction in wallet
  117. std::map<uint256, CWalletTx>::iterator mi = wallet->mapWallet.find(hash);
  118. if(mi == wallet->mapWallet.end())
  119. {
  120. qWarning() << "TransactionTablePriv::updateWallet: Warning: Got CT_NEW, but transaction is not in wallet";
  121. break;
  122. }
  123. // Added -- insert at the right position
  124. QList<TransactionRecord> toInsert =
  125. TransactionRecord::decomposeTransaction(wallet, mi->second);
  126. if(!toInsert.isEmpty()) /* only if something to insert */
  127. {
  128. parent->beginInsertRows(QModelIndex(), lowerIndex, lowerIndex+toInsert.size()-1);
  129. int insert_idx = lowerIndex;
  130. for (const TransactionRecord &rec : toInsert)
  131. {
  132. cachedWallet.insert(insert_idx, rec);
  133. insert_idx += 1;
  134. }
  135. parent->endInsertRows();
  136. }
  137. }
  138. break;
  139. case CT_DELETED:
  140. if(!inModel)
  141. {
  142. qWarning() << "TransactionTablePriv::updateWallet: Warning: Got CT_DELETED, but transaction is not in model";
  143. break;
  144. }
  145. // Removed -- remove entire transaction from table
  146. parent->beginRemoveRows(QModelIndex(), lowerIndex, upperIndex-1);
  147. cachedWallet.erase(lower, upper);
  148. parent->endRemoveRows();
  149. break;
  150. case CT_UPDATED:
  151. // Miscellaneous updates -- nothing to do, status update will take care of this, and is only computed for
  152. // visible transactions.
  153. for (int i = lowerIndex; i < upperIndex; i++) {
  154. TransactionRecord *rec = &cachedWallet[i];
  155. rec->status.needsUpdate = true;
  156. }
  157. break;
  158. }
  159. }
  160. int size()
  161. {
  162. return cachedWallet.size();
  163. }
  164. TransactionRecord *index(int idx)
  165. {
  166. if(idx >= 0 && idx < cachedWallet.size())
  167. {
  168. TransactionRecord *rec = &cachedWallet[idx];
  169. // Get required locks upfront. This avoids the GUI from getting
  170. // stuck if the core is holding the locks for a longer time - for
  171. // example, during a wallet rescan.
  172. //
  173. // If a status update is needed (blocks came in since last check),
  174. // update the status of this transaction from the wallet. Otherwise,
  175. // simply re-use the cached status.
  176. TRY_LOCK(cs_main, lockMain);
  177. if(lockMain)
  178. {
  179. TRY_LOCK(wallet->cs_wallet, lockWallet);
  180. if(lockWallet && rec->statusUpdateNeeded())
  181. {
  182. std::map<uint256, CWalletTx>::iterator mi = wallet->mapWallet.find(rec->hash);
  183. if(mi != wallet->mapWallet.end())
  184. {
  185. rec->updateStatus(mi->second);
  186. }
  187. }
  188. }
  189. return rec;
  190. }
  191. return 0;
  192. }
  193. QString describe(TransactionRecord *rec, int unit)
  194. {
  195. {
  196. LOCK2(cs_main, wallet->cs_wallet);
  197. std::map<uint256, CWalletTx>::iterator mi = wallet->mapWallet.find(rec->hash);
  198. if(mi != wallet->mapWallet.end())
  199. {
  200. return TransactionDesc::toHTML(wallet, mi->second, rec, unit);
  201. }
  202. }
  203. return QString();
  204. }
  205. QString getTxHex(TransactionRecord *rec)
  206. {
  207. LOCK2(cs_main, wallet->cs_wallet);
  208. std::map<uint256, CWalletTx>::iterator mi = wallet->mapWallet.find(rec->hash);
  209. if(mi != wallet->mapWallet.end())
  210. {
  211. std::string strHex = EncodeHexTx(static_cast<CTransaction>(mi->second));
  212. return QString::fromStdString(strHex);
  213. }
  214. return QString();
  215. }
  216. };
  217. TransactionTableModel::TransactionTableModel(const PlatformStyle *_platformStyle, CWallet* _wallet, WalletModel *parent):
  218. QAbstractTableModel(parent),
  219. wallet(_wallet),
  220. walletModel(parent),
  221. priv(new TransactionTablePriv(_wallet, this)),
  222. fProcessingQueuedTransactions(false),
  223. platformStyle(_platformStyle)
  224. {
  225. columns << QString() << QString() << tr("Date") << tr("Type") << tr("Label") << StarwelsUnits::getAmountColumnTitle(walletModel->getOptionsModel()->getDisplayUnit());
  226. priv->refreshWallet();
  227. connect(walletModel->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
  228. subscribeToCoreSignals();
  229. }
  230. TransactionTableModel::~TransactionTableModel()
  231. {
  232. unsubscribeFromCoreSignals();
  233. delete priv;
  234. }
  235. /** Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table headers to react. */
  236. void TransactionTableModel::updateAmountColumnTitle()
  237. {
  238. columns[Amount] = StarwelsUnits::getAmountColumnTitle(walletModel->getOptionsModel()->getDisplayUnit());
  239. Q_EMIT headerDataChanged(Qt::Horizontal,Amount,Amount);
  240. }
  241. void TransactionTableModel::updateTransaction(const QString &hash, int status, bool showTransaction)
  242. {
  243. uint256 updated;
  244. updated.SetHex(hash.toStdString());
  245. priv->updateWallet(updated, status, showTransaction);
  246. }
  247. void TransactionTableModel::updateConfirmations()
  248. {
  249. // Blocks came in since last poll.
  250. // Invalidate status (number of confirmations) and (possibly) description
  251. // for all rows. Qt is smart enough to only actually request the data for the
  252. // visible rows.
  253. Q_EMIT dataChanged(index(0, Status), index(priv->size()-1, Status));
  254. Q_EMIT dataChanged(index(0, ToAddress), index(priv->size()-1, ToAddress));
  255. }
  256. int TransactionTableModel::rowCount(const QModelIndex &parent) const
  257. {
  258. Q_UNUSED(parent);
  259. return priv->size();
  260. }
  261. int TransactionTableModel::columnCount(const QModelIndex &parent) const
  262. {
  263. Q_UNUSED(parent);
  264. return columns.length();
  265. }
  266. QString TransactionTableModel::formatTxStatus(const TransactionRecord *wtx) const
  267. {
  268. QString status;
  269. switch(wtx->status.status)
  270. {
  271. case TransactionStatus::OpenUntilBlock:
  272. status = tr("Open for %n more block(s)","",wtx->status.open_for);
  273. break;
  274. case TransactionStatus::OpenUntilDate:
  275. status = tr("Open until %1").arg(GUIUtil::dateTimeStr(wtx->status.open_for));
  276. break;
  277. case TransactionStatus::Offline:
  278. status = tr("Offline");
  279. break;
  280. case TransactionStatus::Unconfirmed:
  281. status = tr("Unconfirmed");
  282. break;
  283. case TransactionStatus::Abandoned:
  284. status = tr("Abandoned");
  285. break;
  286. case TransactionStatus::Confirming:
  287. status = tr("Confirming (%1 of %2 recommended confirmations)").arg(wtx->status.depth).arg(TransactionRecord::RecommendedNumConfirmations);
  288. break;
  289. case TransactionStatus::Confirmed:
  290. status = tr("Confirmed (%1 confirmations)").arg(wtx->status.depth);
  291. break;
  292. case TransactionStatus::Conflicted:
  293. status = tr("Conflicted");
  294. break;
  295. case TransactionStatus::Immature:
  296. status = tr("Immature (%1 confirmations, will be available after %2)").arg(wtx->status.depth).arg(wtx->status.depth + wtx->status.matures_in);
  297. break;
  298. case TransactionStatus::MaturesWarning:
  299. status = tr("This block was not received by any other nodes and will probably not be accepted!");
  300. break;
  301. case TransactionStatus::NotAccepted:
  302. status = tr("Generated but not accepted");
  303. break;
  304. }
  305. return status;
  306. }
  307. QString TransactionTableModel::formatTxDate(const TransactionRecord *wtx) const
  308. {
  309. if(wtx->time)
  310. {
  311. return GUIUtil::dateTimeStr(wtx->time);
  312. }
  313. return QString();
  314. }
  315. /* Look up address in address book, if found return label (address)
  316. otherwise just return (address)
  317. */
  318. QString TransactionTableModel::lookupAddress(const std::string &address, bool tooltip) const
  319. {
  320. QString label = walletModel->getAddressTableModel()->labelForAddress(QString::fromStdString(address));
  321. QString description;
  322. if(!label.isEmpty())
  323. {
  324. description += label;
  325. }
  326. if(label.isEmpty() || tooltip)
  327. {
  328. description += QString(" (") + QString::fromStdString(address) + QString(")");
  329. }
  330. return description;
  331. }
  332. QString TransactionTableModel::formatTxType(const TransactionRecord *wtx) const
  333. {
  334. switch(wtx->type)
  335. {
  336. case TransactionRecord::RecvWithAddress:
  337. return tr("Received with");
  338. case TransactionRecord::RecvFromOther:
  339. return tr("Received from");
  340. case TransactionRecord::SendToAddress:
  341. case TransactionRecord::SendToOther:
  342. return tr("Sent to");
  343. case TransactionRecord::SendToSelf:
  344. return tr("Payment to yourself");
  345. case TransactionRecord::Generated:
  346. return tr("Mined");
  347. default:
  348. return QString();
  349. }
  350. }
  351. QVariant TransactionTableModel::txAddressDecoration(const TransactionRecord *wtx) const
  352. {
  353. switch(wtx->type)
  354. {
  355. case TransactionRecord::Generated:
  356. return QIcon(":/icons/tx_mined");
  357. case TransactionRecord::RecvWithAddress:
  358. case TransactionRecord::RecvFromOther:
  359. return QIcon(":/icons/tx_input");
  360. case TransactionRecord::SendToAddress:
  361. case TransactionRecord::SendToOther:
  362. return QIcon(":/icons/tx_output");
  363. default:
  364. return QIcon(":/icons/tx_inout");
  365. }
  366. }
  367. QString TransactionTableModel::formatTxToAddress(const TransactionRecord *wtx, bool tooltip) const
  368. {
  369. QString watchAddress;
  370. if (tooltip) {
  371. // Mark transactions involving watch-only addresses by adding " (watch-only)"
  372. watchAddress = wtx->involvesWatchAddress ? QString(" (") + tr("watch-only") + QString(")") : "";
  373. }
  374. switch(wtx->type)
  375. {
  376. case TransactionRecord::RecvFromOther:
  377. return QString::fromStdString(wtx->address) + watchAddress;
  378. case TransactionRecord::RecvWithAddress:
  379. case TransactionRecord::SendToAddress:
  380. case TransactionRecord::Generated:
  381. return lookupAddress(wtx->address, tooltip) + watchAddress;
  382. case TransactionRecord::SendToOther:
  383. return QString::fromStdString(wtx->address) + watchAddress;
  384. case TransactionRecord::SendToSelf:
  385. default:
  386. return tr("(n/a)") + watchAddress;
  387. }
  388. }
  389. QVariant TransactionTableModel::addressColor(const TransactionRecord *wtx) const
  390. {
  391. // Show addresses without label in a less visible color
  392. switch(wtx->type)
  393. {
  394. case TransactionRecord::RecvWithAddress:
  395. case TransactionRecord::SendToAddress:
  396. case TransactionRecord::Generated:
  397. {
  398. QString label = walletModel->getAddressTableModel()->labelForAddress(QString::fromStdString(wtx->address));
  399. if(label.isEmpty())
  400. return COLOR_BAREADDRESS;
  401. } break;
  402. case TransactionRecord::SendToSelf:
  403. return COLOR_BAREADDRESS;
  404. default:
  405. break;
  406. }
  407. return QVariant();
  408. }
  409. QString TransactionTableModel::formatTxAmount(const TransactionRecord *wtx, bool showUnconfirmed, StarwelsUnits::SeparatorStyle separators) const
  410. {
  411. QString str = StarwelsUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), wtx->credit + wtx->debit, false, separators);
  412. if(showUnconfirmed)
  413. {
  414. if(!wtx->status.countsForBalance)
  415. {
  416. str = QString("[") + str + QString("]");
  417. }
  418. }
  419. return QString(str);
  420. }
  421. QVariant TransactionTableModel::txStatusDecoration(const TransactionRecord *wtx) const
  422. {
  423. switch(wtx->status.status)
  424. {
  425. case TransactionStatus::OpenUntilBlock:
  426. case TransactionStatus::OpenUntilDate:
  427. return COLOR_TX_STATUS_OPENUNTILDATE;
  428. case TransactionStatus::Offline:
  429. return COLOR_TX_STATUS_OFFLINE;
  430. case TransactionStatus::Unconfirmed:
  431. return QIcon(":/icons/transaction_0");
  432. case TransactionStatus::Abandoned:
  433. return QIcon(":/icons/transaction_abandoned");
  434. case TransactionStatus::Confirming:
  435. switch(wtx->status.depth)
  436. {
  437. case 1: return QIcon(":/icons/transaction_1");
  438. case 2: return QIcon(":/icons/transaction_2");
  439. case 3: return QIcon(":/icons/transaction_3");
  440. case 4: return QIcon(":/icons/transaction_4");
  441. default: return QIcon(":/icons/transaction_5");
  442. };
  443. case TransactionStatus::Confirmed:
  444. return QIcon(":/icons/transaction_confirmed");
  445. case TransactionStatus::Conflicted:
  446. return QIcon(":/icons/transaction_conflicted");
  447. case TransactionStatus::Immature: {
  448. int total = wtx->status.depth + wtx->status.matures_in;
  449. int part = (wtx->status.depth * 4 / total) + 1;
  450. return QIcon(QString(":/icons/transaction_%1").arg(part));
  451. }
  452. case TransactionStatus::MaturesWarning:
  453. case TransactionStatus::NotAccepted:
  454. return QIcon(":/icons/transaction_0");
  455. default:
  456. return COLOR_BLACK;
  457. }
  458. }
  459. QVariant TransactionTableModel::txWatchonlyDecoration(const TransactionRecord *wtx) const
  460. {
  461. if (wtx->involvesWatchAddress)
  462. return QIcon(":/icons/eye");
  463. else
  464. return QVariant();
  465. }
  466. QString TransactionTableModel::formatTooltip(const TransactionRecord *rec) const
  467. {
  468. QString tooltip = formatTxStatus(rec) + QString("\n") + formatTxType(rec);
  469. if(rec->type==TransactionRecord::RecvFromOther || rec->type==TransactionRecord::SendToOther ||
  470. rec->type==TransactionRecord::SendToAddress || rec->type==TransactionRecord::RecvWithAddress)
  471. {
  472. tooltip += QString(" ") + formatTxToAddress(rec, true);
  473. }
  474. return tooltip;
  475. }
  476. QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
  477. {
  478. if(!index.isValid())
  479. return QVariant();
  480. TransactionRecord *rec = static_cast<TransactionRecord*>(index.internalPointer());
  481. switch(role)
  482. {
  483. case RawDecorationRole:
  484. switch(index.column())
  485. {
  486. case Status:
  487. return txStatusDecoration(rec);
  488. case Watchonly:
  489. return txWatchonlyDecoration(rec);
  490. case ToAddress:
  491. return txAddressDecoration(rec);
  492. }
  493. break;
  494. case Qt::DecorationRole:
  495. {
  496. QIcon icon = qvariant_cast<QIcon>(index.data(RawDecorationRole));
  497. return platformStyle->TextColorIcon(icon);
  498. }
  499. case Qt::DisplayRole:
  500. switch(index.column())
  501. {
  502. case Date:
  503. return formatTxDate(rec);
  504. case Type:
  505. return formatTxType(rec);
  506. case ToAddress:
  507. return formatTxToAddress(rec, false);
  508. case Amount:
  509. return formatTxAmount(rec, true, StarwelsUnits::separatorAlways);
  510. }
  511. break;
  512. case Qt::EditRole:
  513. // Edit role is used for sorting, so return the unformatted values
  514. switch(index.column())
  515. {
  516. case Status:
  517. return QString::fromStdString(rec->status.sortKey);
  518. case Date:
  519. return rec->time;
  520. case Type:
  521. return formatTxType(rec);
  522. case Watchonly:
  523. return (rec->involvesWatchAddress ? 1 : 0);
  524. case ToAddress:
  525. return formatTxToAddress(rec, true);
  526. case Amount:
  527. return qint64(rec->credit + rec->debit);
  528. }
  529. break;
  530. case Qt::ToolTipRole:
  531. return formatTooltip(rec);
  532. case Qt::TextAlignmentRole:
  533. return column_alignments[index.column()];
  534. case Qt::ForegroundRole:
  535. // Use the "danger" color for abandoned transactions
  536. if(rec->status.status == TransactionStatus::Abandoned)
  537. {
  538. return COLOR_TX_STATUS_DANGER;
  539. }
  540. // Non-confirmed (but not immature) as transactions are grey
  541. if(!rec->status.countsForBalance && rec->status.status != TransactionStatus::Immature)
  542. {
  543. return COLOR_UNCONFIRMED;
  544. }
  545. if(index.column() == Amount && (rec->credit+rec->debit) < 0)
  546. {
  547. return COLOR_NEGATIVE;
  548. }
  549. if(index.column() == ToAddress)
  550. {
  551. return addressColor(rec);
  552. }
  553. break;
  554. case TypeRole:
  555. return rec->type;
  556. case DateRole:
  557. return QDateTime::fromTime_t(static_cast<uint>(rec->time));
  558. case WatchonlyRole:
  559. return rec->involvesWatchAddress;
  560. case WatchonlyDecorationRole:
  561. return txWatchonlyDecoration(rec);
  562. case LongDescriptionRole:
  563. return priv->describe(rec, walletModel->getOptionsModel()->getDisplayUnit());
  564. case AddressRole:
  565. return QString::fromStdString(rec->address);
  566. case LabelRole:
  567. return walletModel->getAddressTableModel()->labelForAddress(QString::fromStdString(rec->address));
  568. case AmountRole:
  569. return qint64(rec->credit + rec->debit);
  570. case TxIDRole:
  571. return rec->getTxID();
  572. case TxHashRole:
  573. return QString::fromStdString(rec->hash.ToString());
  574. case TxHexRole:
  575. return priv->getTxHex(rec);
  576. case TxPlainTextRole:
  577. {
  578. QString details;
  579. QDateTime date = QDateTime::fromTime_t(static_cast<uint>(rec->time));
  580. QString txLabel = walletModel->getAddressTableModel()->labelForAddress(QString::fromStdString(rec->address));
  581. details.append(date.toString("M/d/yy HH:mm"));
  582. details.append(" ");
  583. details.append(formatTxStatus(rec));
  584. details.append(". ");
  585. if(!formatTxType(rec).isEmpty()) {
  586. details.append(formatTxType(rec));
  587. details.append(" ");
  588. }
  589. if(!rec->address.empty()) {
  590. if(txLabel.isEmpty())
  591. details.append(tr("(no label)") + " ");
  592. else {
  593. details.append("(");
  594. details.append(txLabel);
  595. details.append(") ");
  596. }
  597. details.append(QString::fromStdString(rec->address));
  598. details.append(" ");
  599. }
  600. details.append(formatTxAmount(rec, false, StarwelsUnits::separatorNever));
  601. return details;
  602. }
  603. case ConfirmedRole:
  604. return rec->status.countsForBalance;
  605. case FormattedAmountRole:
  606. // Used for copy/export, so don't include separators
  607. return formatTxAmount(rec, false, StarwelsUnits::separatorNever);
  608. case StatusRole:
  609. return rec->status.status;
  610. }
  611. return QVariant();
  612. }
  613. QVariant TransactionTableModel::headerData(int section, Qt::Orientation orientation, int role) const
  614. {
  615. if(orientation == Qt::Horizontal)
  616. {
  617. if(role == Qt::DisplayRole)
  618. {
  619. return columns[section];
  620. }
  621. else if (role == Qt::TextAlignmentRole)
  622. {
  623. return column_alignments[section];
  624. } else if (role == Qt::ToolTipRole)
  625. {
  626. switch(section)
  627. {
  628. case Status:
  629. return tr("Transaction status. Hover over this field to show number of confirmations.");
  630. case Date:
  631. return tr("Date and time that the transaction was received.");
  632. case Type:
  633. return tr("Type of transaction.");
  634. case Watchonly:
  635. return tr("Whether or not a watch-only address is involved in this transaction.");
  636. case ToAddress:
  637. return tr("User-defined intent/purpose of the transaction.");
  638. case Amount:
  639. return tr("Amount removed from or added to balance.");
  640. }
  641. }
  642. }
  643. return QVariant();
  644. }
  645. QModelIndex TransactionTableModel::index(int row, int column, const QModelIndex &parent) const
  646. {
  647. Q_UNUSED(parent);
  648. TransactionRecord *data = priv->index(row);
  649. if(data)
  650. {
  651. return createIndex(row, column, priv->index(row));
  652. }
  653. return QModelIndex();
  654. }
  655. void TransactionTableModel::updateDisplayUnit()
  656. {
  657. // emit dataChanged to update Amount column with the current unit
  658. updateAmountColumnTitle();
  659. Q_EMIT dataChanged(index(0, Amount), index(priv->size()-1, Amount));
  660. }
  661. // queue notifications to show a non freezing progress dialog e.g. for rescan
  662. struct TransactionNotification
  663. {
  664. public:
  665. TransactionNotification() {}
  666. TransactionNotification(uint256 _hash, ChangeType _status, bool _showTransaction):
  667. hash(_hash), status(_status), showTransaction(_showTransaction) {}
  668. void invoke(QObject *ttm)
  669. {
  670. QString strHash = QString::fromStdString(hash.GetHex());
  671. qDebug() << "NotifyTransactionChanged: " + strHash + " status= " + QString::number(status);
  672. QMetaObject::invokeMethod(ttm, "updateTransaction", Qt::QueuedConnection,
  673. Q_ARG(QString, strHash),
  674. Q_ARG(int, status),
  675. Q_ARG(bool, showTransaction));
  676. }
  677. private:
  678. uint256 hash;
  679. ChangeType status;
  680. bool showTransaction;
  681. };
  682. static bool fQueueNotifications = false;
  683. static std::vector< TransactionNotification > vQueueNotifications;
  684. static void NotifyTransactionChanged(TransactionTableModel *ttm, CWallet *wallet, const uint256 &hash, ChangeType status)
  685. {
  686. // Find transaction in wallet
  687. std::map<uint256, CWalletTx>::iterator mi = wallet->mapWallet.find(hash);
  688. // Determine whether to show transaction or not (determine this here so that no relocking is needed in GUI thread)
  689. bool inWallet = mi != wallet->mapWallet.end();
  690. bool showTransaction = (inWallet && TransactionRecord::showTransaction(mi->second));
  691. TransactionNotification notification(hash, status, showTransaction);
  692. if (fQueueNotifications)
  693. {
  694. vQueueNotifications.push_back(notification);
  695. return;
  696. }
  697. notification.invoke(ttm);
  698. }
  699. static void ShowProgress(TransactionTableModel *ttm, const std::string &title, int nProgress)
  700. {
  701. if (nProgress == 0)
  702. fQueueNotifications = true;
  703. if (nProgress == 100)
  704. {
  705. fQueueNotifications = false;
  706. if (vQueueNotifications.size() > 10) // prevent balloon spam, show maximum 10 balloons
  707. QMetaObject::invokeMethod(ttm, "setProcessingQueuedTransactions", Qt::QueuedConnection, Q_ARG(bool, true));
  708. for (unsigned int i = 0; i < vQueueNotifications.size(); ++i)
  709. {
  710. if (vQueueNotifications.size() - i <= 10)
  711. QMetaObject::invokeMethod(ttm, "setProcessingQueuedTransactions", Qt::QueuedConnection, Q_ARG(bool, false));
  712. vQueueNotifications[i].invoke(ttm);
  713. }
  714. std::vector<TransactionNotification >().swap(vQueueNotifications); // clear
  715. }
  716. }
  717. void TransactionTableModel::subscribeToCoreSignals()
  718. {
  719. // Connect signals to wallet
  720. wallet->NotifyTransactionChanged.connect(boost::bind(NotifyTransactionChanged, this, _1, _2, _3));
  721. wallet->ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
  722. }
  723. void TransactionTableModel::unsubscribeFromCoreSignals()
  724. {
  725. // Disconnect signals from wallet
  726. wallet->NotifyTransactionChanged.disconnect(boost::bind(NotifyTransactionChanged, this, _1, _2, _3));
  727. wallet->ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2));
  728. }