Skip to content

Commit

Permalink
cosmetic updates
Browse files Browse the repository at this point in the history
  • Loading branch information
j4ys0n committed Dec 24, 2019
1 parent 6c0b55a commit e4eab63
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 104 deletions.
96 changes: 48 additions & 48 deletions src/recurring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ RecurringPaymentInfo RecurringPaymentInfo::fromJson(QJsonObject j) {
QString RecurringPaymentInfo::getHash() const {
auto val = getScheduleDescription() + fromAddr + toAddr;

return QString(QCryptographicHash::hash(val.toUtf8(),
return QString(QCryptographicHash::hash(val.toUtf8(),
QCryptographicHash::Sha256).toHex());
}

Expand Down Expand Up @@ -100,11 +100,11 @@ QDateTime RecurringPaymentInfo::getNextPayment() const {
}

return QDateTime::fromSecsSinceEpoch(0);
}
}

/**
* Counts the number of payments that haven't been started yet
*/
*/
int RecurringPaymentInfo::getNumPendingPayments() const {
int count = 0;
for (auto item : payments) {
Expand All @@ -116,7 +116,7 @@ int RecurringPaymentInfo::getNumPendingPayments() const {
return count;
}

// Returns a new Recurring payment info, created from the Tx.
// Returns a new Recurring payment info, created from the Tx.
// The caller needs to take ownership of the returned object.
RecurringPaymentInfo* Recurring::getNewRecurringFromTx(QWidget* parent, MainWindow*, Tx tx, RecurringPaymentInfo* rpi) {
Ui_newRecurringDialog ui;
Expand Down Expand Up @@ -171,24 +171,24 @@ RecurringPaymentInfo* Recurring::getNewRecurringFromTx(QWidget* parent, MainWind
ui.txtDesc->setText(rpi->desc);
ui.lblTo->setText(rpi->toAddr);
ui.txtMemo->setPlainText(rpi->memo);

ui.cmbCurrency->setCurrentText(rpi->currency);
ui.lblAmt->setText(rpi->getAmountPretty());
ui.lblAmt->setText(rpi->getAmountPretty());
ui.lblFrom->setText(rpi->fromAddr);
ui.txtNumPayments->setText(QString::number(rpi->payments.size()));
ui.cmbSchedule->setCurrentIndex(rpi->schedule - 1); // indexes start from 0
}

ui.txtDesc->setFocus();
if (d.exec() == QDialog::Accepted) {
// Construct a new Object and return it
auto numPayments = ui.txtNumPayments->text().toInt();
auto r = new RecurringPaymentInfo(numPayments);
r->desc = ui.txtDesc->text();
r->currency = ui.cmbCurrency->currentText();
r->currency = ui.cmbCurrency->currentText();
r->schedule = (Schedule)ui.cmbSchedule->currentData().toInt();
r->startDate = QDateTime::currentDateTime();

updateInfoWithTx(r, tx);
return r;
}
Expand Down Expand Up @@ -225,7 +225,7 @@ void Recurring::updateInfoWithTx(RecurringPaymentInfo* r, Tx tx) {
/**
* Given a schedule and an optional previous date, calculate the next payment date/
* If there is no previous date, it is assumed to be the current DateTime
*/
*/
QDateTime Recurring::getNextPaymentDate(Schedule s, QDateTime start) {
QDateTime nextDate = start;

Expand Down Expand Up @@ -260,9 +260,9 @@ void Recurring::addRecurringInfo(const RecurringPaymentInfo& rpi) {
if (payments.contains(rpi.getHash())) {
payments.remove(rpi.getHash());
}

payments.insert(rpi.getHash(), rpi);

writeToStorage();
}

Expand All @@ -271,9 +271,9 @@ void Recurring::removeRecurringInfo(QString hash) {
qDebug() << "Hash not found:" << hash << " in " << payments.keys();
return;
}

payments.remove(hash);

writeToStorage();
}

Expand Down Expand Up @@ -303,7 +303,7 @@ void Recurring::writeToStorage() {
arr.append(v.toJson());
}

QTextStream out(&file);
QTextStream out(&file);
out << QJsonDocument(arr).toJson();

file.close();
Expand All @@ -313,7 +313,7 @@ void Recurring::writeToStorage() {
* Lookup the recurring payment info with the given hash, and update
* a payment made
**/
bool Recurring::updatePaymentItem(QString hash, int paymentNumber,
bool Recurring::updatePaymentItem(QString hash, int paymentNumber,
QString txid, QString err, PaymentStatus status) {
if (!payments.contains(hash)) {
return false;
Expand All @@ -334,12 +334,12 @@ bool Recurring::updatePaymentItem(QString hash, int paymentNumber,
}

Recurring* Recurring::getInstance() {
if (!instance) {
instance = new Recurring();
if (!instance) {
instance = new Recurring();
instance->readFromStorage();
}
return instance;
}

return instance;
}

// Singleton
Expand All @@ -362,7 +362,7 @@ void Recurring::processPending(MainWindow* main) {
QList<RecurringPaymentInfo::PaymentItem> pending;

for (auto pi: rpi.payments) {
if (pi.status == PaymentStatus::NOT_STARTED &&
if (pi.status == PaymentStatus::NOT_STARTED &&
pi.date <= QDateTime::currentDateTime()) {
pending.append(pi);
}
Expand All @@ -385,7 +385,7 @@ void Recurring::processPending(MainWindow* main) {
/**
* Called when a particular RecurringPaymentInfo has more than one pending payment to be processed.
* We will ask the user what he wants to do.
*/
*/
void Recurring::processMultiplePending(RecurringPaymentInfo rpi, MainWindow* main) {
Ui_RecurringPending ui;
QDialog d(main);
Expand All @@ -399,7 +399,7 @@ void Recurring::processMultiplePending(RecurringPaymentInfo rpi, MainWindow* mai

// Mark all the outstanding ones as pending, so it shows in the table correctly.
for (auto& pi: rpi.payments) {
if (pi.status == PaymentStatus::NOT_STARTED &&
if (pi.status == PaymentStatus::NOT_STARTED &&
pi.date <= QDateTime::currentDateTime()) {
pi.status = PaymentStatus::PENDING;
}
Expand Down Expand Up @@ -455,7 +455,7 @@ void Recurring::processMultiplePending(RecurringPaymentInfo rpi, MainWindow* mai
}

// Save the table column layout
s.setValue("recurringmultipaymentstablevgeom", ui.tblPending->horizontalHeader()->saveState());
s.setValue("recurringmultipaymentstablevgeom", ui.tblPending->horizontalHeader()->saveState());
}

void Recurring::executeRecurringPayment(MainWindow* main, RecurringPaymentInfo rpi, QList<int> paymentNumbers) {
Expand All @@ -465,13 +465,13 @@ void Recurring::executeRecurringPayment(MainWindow* main, RecurringPaymentInfo r
// If there is no price, then fail the payment
if (Settings::getInstance()->getZECPrice() == 0) {
for (auto paymentNumber: paymentNumbers) {
updatePaymentItem(rpi.getHash(), paymentNumber,
updatePaymentItem(rpi.getHash(), paymentNumber,
"", QObject::tr("No ARW price was available to convert from USD"),
PaymentStatus::ERROR);
}
return;
}

// Translate it into ARW
amt = rpi.amt / Settings::getInstance()->getZECPrice();
}
Expand All @@ -480,27 +480,27 @@ void Recurring::executeRecurringPayment(MainWindow* main, RecurringPaymentInfo r
Tx tx;
tx.fromAddr = rpi.fromAddr;
tx.fee = Settings::getMinerFee();

// If this is a multiple payment, we'll add up all the amounts
if (paymentNumbers.size() > 1)
amt *= paymentNumbers.size();

tx.toAddrs.append(ToFields { rpi.toAddr, amt, rpi.memo, rpi.memo.toUtf8().toHex() });

// To prevent some weird race conditions, we immediately mark the payment as paid.
// If something goes wrong, we'll get the error callback below, and the status will be
// If something goes wrong, we'll get the error callback below, and the status will be
// updated. If it succeeds, we'll once again update the status with the txid
for (int paymentNumber: paymentNumbers) {
updatePaymentItem(rpi.getHash(), paymentNumber, "", "", PaymentStatus::COMPLETED);
}
}

// Send it off to the RPC
doSendTx(main, tx, [=] (QString txid, QString err) {
if (err.isEmpty()) {
// Success, update the rpi
for (int paymentNumber: paymentNumbers) {
updatePaymentItem(rpi.getHash(), paymentNumber, txid, "", PaymentStatus::COMPLETED);
}
}
} else {
// Errored out. Bummer.
for (int paymentNumber: paymentNumbers) {
Expand All @@ -512,25 +512,25 @@ void Recurring::executeRecurringPayment(MainWindow* main, RecurringPaymentInfo r

/**
* Execute a send Tx
*/
*/
void Recurring::doSendTx(MainWindow* mainwindow, Tx tx, std::function<void(QString, QString)> cb) {
mainwindow->getRPC()->executeTransaction(tx, [=] (QString opid) {
mainwindow->ui->statusBar->showMessage(QObject::tr("Computing Recurring Tx: ") % opid);
},
[=] (QString /*opid*/, QString txid) {
[=] (QString /*opid*/, QString txid) {
mainwindow->ui->statusBar->showMessage(Settings::txidStatusMessage + " " + txid);
cb(txid, "");
},
[=] (QString opid, QString errStr) {
mainwindow->ui->statusBar->showMessage(QObject::tr(" Tx ") % opid % QObject::tr(" failed"), 15 * 1000);
cb("", errStr);
});

}

/**
* Show the list of configured recurring payments
*/
*/
void Recurring::showRecurringDialog(MainWindow* parent) {
// Make sure only 1 is showing at a time
static bool isDialogOpen = false;
Expand All @@ -540,7 +540,7 @@ void Recurring::showRecurringDialog(MainWindow* parent) {

Ui_RecurringDialog rd;
QDialog d(parent);

rd.setupUi(&d);
Settings::saveRestore(&d);

Expand Down Expand Up @@ -577,7 +577,7 @@ void Recurring::showRecurringDialog(MainWindow* parent) {
if (Settings::getInstance()->isTestnet()) {
url = "https://explorer.testnet.z.cash/tx/" + txid;
} else {
url = "https://explorer.zcha.in/transactions/" + txid;
url = "https://explorer.arrowchain.io/#/tx/" + txid;
}
QDesktopServices::openUrl(QUrl(url));
});
Expand All @@ -596,35 +596,35 @@ void Recurring::showRecurringDialog(MainWindow* parent) {
// Restore the table column layout
QSettings s;
p.tableView->horizontalHeader()->restoreState(s.value("recurringpaymentstablevgeom").toByteArray());

pd.exec();

// Save the table column layout
s.setValue("recurringpaymentstablevgeom", p.tableView->horizontalHeader()->saveState());
s.setValue("recurringpaymentstablevgeom", p.tableView->horizontalHeader()->saveState());
};

// View Button
QObject::connect(rd.btnView, &QPushButton::clicked, [=] () {
auto selectedRows = rd.tableView->selectionModel()->selectedRows();
if (selectedRows.size() == 1) {
auto rpi = Recurring::getInstance()->getAsList()[selectedRows[0].row()];
showPayments(rpi);
showPayments(rpi);
}
});

// Double Click
QObject::connect(rd.tableView, &QTableView::doubleClicked, [=] (auto index) {
auto rpi = Recurring::getInstance()->getAsList()[index.row()];
showPayments(rpi);
showPayments(rpi);
});

// Delete button
QObject::connect(rd.btnDelete, &QPushButton::clicked, [=, &d]() {
auto selectedRows = rd.tableView->selectionModel()->selectedRows();
if (selectedRows.size() == 1) {
auto rpi = Recurring::getInstance()->getAsList()[selectedRows[0].row()];
if (QMessageBox::warning(&d, QObject::tr("Are you sure you want to delete the recurring payment?"),
QObject::tr("Are you sure you want to delete the recurring payment?") + "\n" +
if (QMessageBox::warning(&d, QObject::tr("Are you sure you want to delete the recurring payment?"),
QObject::tr("Are you sure you want to delete the recurring payment?") + "\n" +
QObject::tr("All future payments will be cancelled."),
QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
Recurring::getInstance()->removeRecurringInfo(rpi.getHash());
Expand All @@ -644,10 +644,10 @@ void Recurring::showRecurringDialog(MainWindow* parent) {

/**
* Model for List of recurring payments
*/
*/
RecurringListViewModel::RecurringListViewModel(QTableView* parent) {
this->parent = parent;
headers << tr("Amount") << tr("Schedule") << tr("Payments Left")
headers << tr("Amount") << tr("Schedule") << tr("Payments Left")
<< tr("Next Payment") << tr("To");
}

Expand All @@ -667,11 +667,11 @@ QVariant RecurringListViewModel::data(const QModelIndex &index, int role) const
case 0: return rpi.getAmountPretty();
case 1: return tr("Every ") + schedule_desc(rpi.schedule);
case 2: return QString::number(rpi.getNumPendingPayments()) + " of " + QString::number(rpi.payments.size());
case 3: {
case 3: {
auto n = rpi.getNextPayment();
if (n.toSecsSinceEpoch() == 0) return tr("None"); else return n;
}
case 4: return rpi.toAddr;
case 4: return rpi.toAddr;
}
}

Expand Down Expand Up @@ -749,4 +749,4 @@ QVariant RecurringPaymentsListViewModel::headerData(int section, Qt::Orientation
}

return QVariant();
}
}
Loading

0 comments on commit e4eab63

Please sign in to comment.