Files
pay/PortfolioDataProvider.cpp
T

184 lines
6.1 KiB
C++
Raw Permalink Normal View History

2020-05-24 13:20:03 +02:00
/*
* This file is part of the Flowee project
* Copyright (C) 2020-2022 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, [=]() {
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
}
m_accountInfos.clear();
2020-05-24 13:20:03 +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
});
}
2021-11-29 20:57:41 +01:00
QList<QObject *> PortfolioDataProvider::accounts() const
2020-05-24 13:20:03 +02:00
{
2021-11-29 20:57:41 +01:00
QList<QObject *> answer;
if (m_accountInfos.size() == 1) {
// We have a 'mode' where having exactly one wallet will
// hide the wallets-list to avoid confusing the user with multi wallets.
return answer;
}
2021-07-14 15:55:30 +02:00
// we filter out the wallets that are NOT user-owned. Which is essentially the main initial
// wallet created to allow people to deposit instantly.
// Such a wallet is migrated to user-owned the moment a deposit is detected.
2021-07-14 15:55:30 +02:00
for (auto *account : m_accountInfos) {
2022-04-05 16:37:09 +02:00
if (account->userOwnedWallet() && !account->isArchived())
2021-07-14 15:55:30 +02:00
answer.append(account);
}
// if the only wallet(s) are not user owned, share those with the GUI.
2021-11-29 20:57:41 +01:00
if (answer.isEmpty() && !m_accountInfos.isEmpty()) {
2021-12-04 11:25:27 +01:00
for (auto *account : m_accountInfos) {
if (account->isArchived())
answer.append(account);
}
}
return answer;
}
QList<QObject *> PortfolioDataProvider::archivedAccounts() const
{
QList<QObject *> 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->isArchived() && account->userOwnedWallet())
2021-11-29 20:57:41 +01:00
answer.append(account);
}
2021-07-14 15:55:30 +02:00
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
{
auto *old = current();
// when changing, assume that the user noticed the transactions in the account.
if (old) old->setHasFreshTransactions(false);
2020-05-24 13:20:03 +02:00
if (item) {
item->setHasFreshTransactions(false);
2020-05-24 13:20:03 +02:00
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();
}
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;
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();
}
}
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);
2022-04-06 16:14:48 +02:00
// skip archived wallet balances.
if (!wallet->segment() || wallet->segment()->priority() == PrivacySegment::OnlyManual)
continue;
2021-04-29 12:26:02 +02:00
rc += wallet->balanceConfirmed();
rc += wallet->balanceImmature();
rc += wallet->balanceUnconfirmed();
}
return rc;
}
bool PortfolioDataProvider::isSingleAccount() const
{
return m_accounts.size() == 1;
}
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-12-04 11:25:27 +01:00
connect (info, SIGNAL(isArchivedChanged()), 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);
}
}
if (wallet == current() && wallet->isArchived()) {
selectDefaultWallet();
}
2021-12-04 11:25:27 +01:00
emit accountsChanged();
2022-04-06 16:14:48 +02:00
emit totalBalanceChanged(); // maybe one got (un) archived which changes the total balance
2020-10-19 16:50:36 +02:00
}