diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 48cea1e..d88c768 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -51,6 +51,9 @@ MainWindow::MainWindow(QWidget *parent) : // Backup wallet.dat QObject::connect(ui->actionBackup_wallet_dat, &QAction::triggered, this, &MainWindow::backupWalletDat); + // Export transactions + QObject::connect(ui->actionExport_transactions, &QAction::triggered, this, &MainWindow::exportTransactions); + // z-Board.net QObject::connect(ui->actionz_board_net, &QAction::triggered, this, &MainWindow::postToZBoard); @@ -649,6 +652,25 @@ void MainWindow::importPrivKey() { } } +/** + * Export transaction history into a CSV file + */ +void MainWindow::exportTransactions() { + // First, get the export file name + QString exportName = "zcash-transactions-" + QDateTime::currentDateTime().toString("yyyyMMdd") + ".csv"; + + QUrl csvName = QFileDialog::getSaveFileUrl(this, + tr("Export transactions"), exportName, "CSV file (*.csv)"); + + if (csvName.isEmpty()) + return; + + if (!rpc->getTransactionsModel()->exportToCsv(csvName.toLocalFile())) { + QMessageBox::critical(this, tr("Error"), + tr("Error exporting transactions, file was not saved"), QMessageBox::Ok); + } +} + /** * Backup the wallet.dat file. This is kind of a hack, since it has to read from the filesystem rather than an RPC call * This might fail for various reasons - Remote zcashd, non-standard locations, custom params passed to zcashd, many others diff --git a/src/mainwindow.h b/src/mainwindow.h index 382b86e..6d4c03b 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -94,6 +94,7 @@ private: void exportAllKeys(); void exportKeys(QString addr = ""); void backupWalletDat(); + void exportTransactions(); void doImport(QList* keys); diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 5682ae2..ea17051 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -346,8 +346,8 @@ 0 0 - 928 - 380 + 920 + 334 @@ -978,7 +978,7 @@ 0 0 968 - 19 + 22 @@ -989,6 +989,8 @@ + + @@ -1086,6 +1088,11 @@ &Backup wallet.dat + + + Export transactions + + diff --git a/src/rpc.h b/src/rpc.h index dcb340d..88b5bd5 100644 --- a/src/rpc.h +++ b/src/rpc.h @@ -46,10 +46,10 @@ public: void watchTxStatus(); void addNewTxToWatch(Tx tx, const QString& newOpid); - BalancesTableModel* getBalancesModel() { return balancesTableModel; } - const QList* getAllZAddresses() { return zaddresses; } - const QList* getUTXOs() { return utxos; } - const QMap* getAllBalances() { return allBalances; } + const TxTableModel* getTransactionsModel() { return transactionsTableModel; } + const QList* getAllZAddresses() { return zaddresses; } + const QList* getUTXOs() { return utxos; } + const QMap* getAllBalances() { return allBalances; } void newZaddr(bool sapling, const std::function& cb); void newTaddr(const std::function& cb); diff --git a/src/txtablemodel.cpp b/src/txtablemodel.cpp index d610b47..297301e 100644 --- a/src/txtablemodel.cpp +++ b/src/txtablemodel.cpp @@ -39,6 +39,37 @@ void TxTableModel::addTData(const QList& data) { updateAllData(); } +bool TxTableModel::exportToCsv(QString fileName) const { + if (!modeldata) + return false; + + QFile file(fileName); + if (!file.open(QIODevice::ReadWrite | QIODevice::Truncate)) + return false; + + QTextStream out(&file); // we will serialize the data into the file + + // Write headers + for (int i = 0; i < headers.length(); i++) { + out << "\"" << headers[i] << "\""; + } + out << "\"Memo\""; + out << endl; + + // Write out each row + for (int row = 0; row < modeldata->length(); row++) { + for (int col = 0; col < headers.length(); col++) { + out << "\"" << data(index(row, col), Qt::DisplayRole).toString() << "\","; + } + // Memo + out << "\"" << modeldata->at(row).memo << "\""; + out << endl; + } + + file.close(); + return true; +} + void TxTableModel::updateAllData() { auto newmodeldata = new QList(); diff --git a/src/txtablemodel.h b/src/txtablemodel.h index fb149cf..a2e3e91 100644 --- a/src/txtablemodel.h +++ b/src/txtablemodel.h @@ -19,6 +19,8 @@ public: QString getMemo(int row); QString getAddr(int row); + bool exportToCsv(QString fileName) const; + int rowCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const; QVariant data(const QModelIndex &index, int role) const;