Files
pay/PortfolioDataProvider.cpp
T

153 lines
4.6 KiB
C++
Raw Permalink Normal View History

2020-05-24 13:20:03 +02:00
/*
* This file is part of the Flowee project
2021-01-05 14:03:30 +01:00
* Copyright (C) 2020 Tom Zander <tom@flowee.org>
2020-05-24 13:20:03 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "PortfolioDataProvider.h"
2020-10-14 15:12:33 +02:00
#include "AccountInfo.h"
#include "BitcoinValue.h"
2020-06-08 14:37:29 +02:00
#include "FloweePay.h"
2020-10-14 15:12:33 +02:00
#include "Payment.h"
#include "Wallet.h"
2020-06-08 14:37:29 +02:00
#include <QSettings>
2020-05-24 13:20:03 +02:00
PortfolioDataProvider::PortfolioDataProvider(QObject *parent) : QObject(parent)
{
2021-04-21 15:17:08 +02:00
connect (FloweePay::instance(), &FloweePay::walletsChanged, this, [=]() {
2020-12-25 23:27:14 +01:00
for (auto &w : FloweePay::instance()->wallets()) {
2020-05-24 13:20:03 +02:00
if (!m_accounts.contains(w)) {
addWalletAccount(w);
}
}
});
}
QList<AccountInfo *> PortfolioDataProvider::accounts() const
{
return m_accountInfos;
}
AccountInfo *PortfolioDataProvider::current() const
{
if (m_currentAccount >= 0 && m_currentAccount < m_accountInfos.size())
return m_accountInfos.at(m_currentAccount);
return nullptr;
}
2020-06-08 14:26:13 +02:00
void PortfolioDataProvider::setCurrent(AccountInfo *item)
2020-05-24 13:20:03 +02:00
{
if (item) {
int index = m_accountInfos.indexOf(item);
if (index == m_currentAccount)
return;
m_currentAccount = index;
}
else {
2020-06-08 14:26:13 +02:00
m_currentAccount = -1;
2020-05-24 13:20:03 +02:00
}
emit currentChanged();
}
2020-10-23 22:34:34 +02:00
Payment *PortfolioDataProvider::createPaymentObject(const QString &address, double value)
{
auto p = new Payment(m_accounts.at(m_currentAccount), value);
2021-05-04 17:41:49 +02:00
p->setPreferSchnorr(FloweePay::instance()->preferSchnorr());
2020-10-23 22:34:34 +02:00
try {
p->setTargetAddress(address);
return p;
} catch (...) {
logFatal() << "Got a non-validating address, can't start payment";
delete p;
return nullptr;
}
}
2020-06-12 23:37:32 +02:00
QObject *PortfolioDataProvider::startPayToAddress(const QString &address, BitcoinValue *bitcoinValue)
2020-06-08 14:37:29 +02:00
{
2020-06-12 23:37:32 +02:00
assert(bitcoinValue);
if (m_currentAccount == -1 || bitcoinValue == nullptr)
2020-06-08 14:37:29 +02:00
return nullptr;
2020-10-23 22:34:34 +02:00
return createPaymentObject(address, bitcoinValue->value());
}
QObject *PortfolioDataProvider::startPayAllToAddress(const QString &address)
{
// -1 is defined by the Payment::setPaymentAmount() to be the 'send all' indicator.
return createPaymentObject(address, -1);
2020-06-08 14:37:29 +02:00
}
void PortfolioDataProvider::selectDefaultWallet()
{
2021-04-21 15:17:08 +02:00
int fallback = -1;
for (int i = 0; i < m_accounts.size(); ++i) {
auto wallet = m_accounts.at(i);
2020-10-19 14:05:38 +02:00
if (wallet->segment()->priority() == PrivacySegment::First) {
m_currentAccount = i;
emit currentChanged();
break;
}
2021-04-21 15:17:08 +02:00
if (!wallet->userOwnedWallet()) {
fallback = i;
}
}
// when the app went and made an empty wallet, select that so we have at least something selected.
if (m_currentAccount == -1 && fallback >= 0) {
m_currentAccount = fallback;
emit currentChanged();
}
}
2021-04-29 12:26:02 +02:00
double PortfolioDataProvider::totalBalance() const
{
double rc = 0;
for (int i = 0; i < m_accounts.size(); ++i) {
auto wallet = m_accounts.at(i);
rc += wallet->balanceConfirmed();
rc += wallet->balanceImmature();
rc += wallet->balanceUnconfirmed();
}
return rc;
}
2020-05-24 13:20:03 +02:00
void PortfolioDataProvider::addWalletAccount(Wallet *wallet)
{
if (m_accounts.contains(wallet))
return;
m_accounts.append(wallet);
2020-10-19 16:50:36 +02:00
auto info = new AccountInfo(wallet, this);
m_accountInfos.append(info);
connect (info, SIGNAL(isDefaultWalletChanged()), this, SLOT(walletChangedPriority()));
2021-04-29 12:26:02 +02:00
connect (info, SIGNAL(balanceChanged()), this, SIGNAL(totalBalanceChanged()));
2020-12-25 23:27:14 +01:00
emit accountsChanged();
2020-05-24 13:20:03 +02:00
}
2020-10-19 16:50:36 +02:00
void PortfolioDataProvider::walletChangedPriority()
{
AccountInfo *wallet = qobject_cast<AccountInfo*>(sender());
if (!wallet)
return;
if (wallet->isDefaultWallet()) {
// as this just changed, through the UI, we have to mark any other
// wallet that was a default wallet as no longer being one.
2020-12-25 23:27:14 +01:00
for (auto &info : m_accountInfos) {
2020-10-19 16:50:36 +02:00
if (info != wallet && info->isDefaultWallet())
info->setDefaultWallet(false);
}
}
}