Browse Source

Add recurring payments to file

recurring
adityapk00 5 years ago
parent
commit
dd71b8e261
  1. 42
      src/recurring.cpp
  2. 13
      src/recurring.h
  3. 15
      src/sendtab.cpp

42
src/recurring.cpp

@ -121,6 +121,8 @@ void Recurring::updateInfoWithTx(RecurringPaymentInfo* r, Tx tx) {
r->currency = Settings::getTokenName();
r->amt = tx.toAddrs[0].amount;
}
r->updateHash();
}
QDateTime Recurring::getNextPaymentDate(Schedule s) {
@ -134,7 +136,47 @@ QDateTime Recurring::getNextPaymentDate(Schedule s) {
}
return nextDate;
}
QString Recurring::writeableFile() {
auto filename = QStringLiteral("recurringpayments.json");
auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
if (!dir.exists())
QDir().mkpath(dir.absolutePath());
if (Settings::getInstance()->isTestnet()) {
return dir.filePath("testnet-" % filename);
}
else {
return dir.filePath(filename);
}
}
void Recurring::addRecurringInfo(const RecurringPaymentInfo& rpi) {
if (payments.contains(rpi.hashid)) {
payments.remove(rpi.hashid);
}
payments.insert(rpi.hashid, rpi);
writeToStorage();
}
void Recurring::writeToStorage() {
QFile file(writeableFile());
file.open(QIODevice::ReadWrite | QIODevice::Truncate);
QJsonArray arr;
for (auto k : payments.keys()) {
arr.append(payments[k].toJson());
}
QTextStream out(&file);
out << QJsonDocument(arr).toJson();
file.close();
}
// Singleton

13
src/recurring.h

@ -70,7 +70,7 @@ struct RecurringPaymentInfo {
void updateHash() {
auto val = getScheduleDescription() + fromAddr + toAddr;
hashid = QCryptographicHash::hash(val.toUtf8(), QCryptographicHash::Sha256);
hashid = QString(QCryptographicHash::hash(val.toUtf8(), QCryptographicHash::Sha256).toHex());
}
QJsonObject toJson() {
@ -116,10 +116,17 @@ public:
RecurringPaymentInfo* getNewRecurringFromTx(QWidget* parent, MainWindow* main, Tx tx, RecurringPaymentInfo* rpi);
QDateTime getNextPaymentDate(Schedule s);
void updateInfoWithTx(RecurringPaymentInfo* r, Tx tx);
QDateTime getNextPaymentDate(Schedule s);
void updateInfoWithTx(RecurringPaymentInfo* r, Tx tx);
QString writeableFile();
void addRecurringInfo(const RecurringPaymentInfo& rpi);
void writeToStorage();
private:
Recurring() = default;
QMap<QString, RecurringPaymentInfo> payments;
static Recurring* instance;
};

15
src/sendtab.cpp

@ -521,6 +521,7 @@ bool MainWindow::confirmTx(Tx tx, RecurringPaymentInfo* rpi) {
// Update the recurring info with the latest Tx
if (rpi != nullptr) {
Recurring::getInstance()->updateInfoWithTx(rpi, tx);
rpi->updateHash();
}
// Show a confirmation dialog
@ -661,13 +662,7 @@ bool MainWindow::confirmTx(Tx tx, RecurringPaymentInfo* rpi) {
confirm.sendFrom->setToolTip(tooltip);
// Show the dialog and submit it if the user confirms
if (d.exec() == QDialog::Accepted) {
// Then delete the additional fields from the sendTo tab
clearSendForm();
return true;
} else {
return false;
}
return d.exec() == QDialog::Accepted;
}
// Send button clicked
@ -688,6 +683,12 @@ void MainWindow::sendButton() {
// Show a dialog to confirm the Tx
if (confirmTx(tx, sendTxRecurringInfo)) {
// Add it to the list
Recurring::getInstance()->addRecurringInfo(*sendTxRecurringInfo);
// Then delete the additional fields from the sendTo tab
clearSendForm();
// And send the Tx
rpc->executeTransaction(tx,
// Submitted

Loading…
Cancel
Save