2020-05-24 13:20:03 +02:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
2021-10-27 18:31:49 +02:00
|
|
|
* Copyright (C) 2020-2021 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
|
|
|
|
2020-06-11 17:41:18 +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, [=]() {
|
2021-10-27 18:31:49 +02:00
|
|
|
const auto &wallets = FloweePay::instance()->wallets();
|
|
|
|
|
if (wallets.isEmpty()) {
|
|
|
|
|
m_accounts.clear();
|
|
|
|
|
for (auto ai : m_accountInfos) {
|
|
|
|
|
ai->deleteLater();
|
2020-05-24 13:20:03 +02:00
|
|
|
}
|
2021-10-27 18:31:49 +02:00
|
|
|
m_accountInfos.clear();
|
2020-05-24 13:20:03 +02:00
|
|
|
}
|
2021-10-27 18:31:49 +02:00
|
|
|
for (auto &w : wallets) {
|
|
|
|
|
addWalletAccount(w);
|
|
|
|
|
}
|
|
|
|
|
emit accountsChanged(); // not strictly needed, but safe to have.
|
2020-05-24 13:20:03 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<AccountInfo *> PortfolioDataProvider::accounts() const
|
|
|
|
|
{
|
2021-07-14 15:55:30 +02:00
|
|
|
QList<AccountInfo *> answer;
|
|
|
|
|
// we filter out the wallets that are NOT user-owned. Which is essentially the main initial
|
|
|
|
|
// wallet created to allow people to deposit instantly.
|
|
|
|
|
for (auto *account : m_accountInfos) {
|
|
|
|
|
if (account->userOwnedWallet())
|
|
|
|
|
answer.append(account);
|
|
|
|
|
}
|
|
|
|
|
// if the only wallet(s) are not user owned, share those with the GUI.
|
|
|
|
|
if (answer.isEmpty() && !m_accountInfos.isEmpty())
|
|
|
|
|
return m_accountInfos;
|
|
|
|
|
return answer;
|
2020-05-24 13:20:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2020-06-11 17:41:18 +02:00
|
|
|
void PortfolioDataProvider::selectDefaultWallet()
|
|
|
|
|
{
|
2021-04-21 15:17:08 +02:00
|
|
|
int fallback = -1;
|
2021-10-27 19:02:43 +02:00
|
|
|
int current = -1;
|
|
|
|
|
PrivacySegment::Priority selectedPriority = PrivacySegment::OnlyManual;
|
2020-06-11 17:41:18 +02:00
|
|
|
for (int i = 0; i < m_accounts.size(); ++i) {
|
|
|
|
|
auto wallet = m_accounts.at(i);
|
2021-10-27 19:02:43 +02:00
|
|
|
const auto prio = wallet->segment()->priority();
|
2021-04-21 15:17:08 +02:00
|
|
|
if (!wallet->userOwnedWallet()) {
|
|
|
|
|
fallback = i;
|
2021-10-27 19:02:43 +02:00
|
|
|
} else if (prio < selectedPriority) {
|
|
|
|
|
current = i;
|
|
|
|
|
selectedPriority = prio;
|
2021-04-21 15:17:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// when the app went and made an empty wallet, select that so we have at least something selected.
|
2021-10-27 19:02:43 +02:00
|
|
|
if (current == -1)
|
|
|
|
|
current = fallback;
|
|
|
|
|
|
|
|
|
|
if (current != m_currentAccount) { // changed?
|
|
|
|
|
m_currentAccount = current;
|
2021-04-21 15:17:08 +02:00
|
|
|
emit currentChanged();
|
2020-06-11 17:41:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|