Files
pay/src/PriceDataProvider.h
T
tomFlowee 1deccd9226 Make fiat mode an actual bool on BitcoinValue
The CPP now does more of the (heavy) lifting and the UI layer can
ignore
most of the details with regards to there being digits behind the
separator for fiat at all.

The internal change is that the fiat based values are always processed
in cents, even if the cents are not displayed. This solves incorrect
display and generally removes special cases.
2023-10-18 11:43:23 +02:00

143 lines
4.8 KiB
C++

/*
* This file is part of the Flowee project
* Copyright (C) 2021-2023 Tom Zander <tom@flowee.org>
*
* 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/>.
*/
#ifndef PRICEDATAPROVIDER_H
#define PRICEDATAPROVIDER_H
#include <QObject>
#include "PriceHistoryDataProvider.h"
#include <QNetworkAccessManager>
#include <QTimer>
class PriceDataProvider : public QObject
{
Q_OBJECT
Q_PROPERTY(int price READ price NOTIFY priceChanged)
Q_PROPERTY(QString currencyName READ currencyName NOTIFY currencySymbolChanged)
Q_PROPERTY(bool displayCents READ displayCents NOTIFY currencySymbolChanged)
Q_PROPERTY(QString currencySymbolPrefix READ currencySymbolPrefix NOTIFY currencySymbolChanged)
Q_PROPERTY(QString currencySymbolPost READ currencySymbolPost NOTIFY currencySymbolChanged)
public:
explicit PriceDataProvider(const QString &countryCode = QString(), QObject *parent = nullptr);
void start();
QString currencyName() const {
return m_currency;
}
// current price in cents
int price() const {
return m_currentPrice.price;
}
void setCurrency(const QLocale &countryLocale);
void setCountry(const QString &countrycode);
/**
* Return a formatted string with the locale-defined price of the amount of \a sats.
* This takes the BCH amount (in sats) and renders it into a price as the current locale defines,
* based on \a price.
*/
Q_INVOKABLE QString formattedPrice(double amountSats, int64_t price) const;
/// Return the price as int (in cents) for the number of sats and the given price.
Q_INVOKABLE int64_t priceFor(double amountSats, int64_t price) const;
/**
* Return a formatted string with the locale-defined price of a fiat price from \a cents.
*/
Q_INVOKABLE QString formattedPrice(int64_t cents) const;
/**
* Return the price at a certain time in the past.
*/
Q_INVOKABLE int historicalPrice(const QDateTime &timestamp) const;
/**
* Return the price at a certain time in the past, or 0 if none are on record.
*/
Q_INVOKABLE int historicalPriceAccurate(const QDateTime &timestamp) const;
/**
* Return the price a certain amount of days in the past, or 0 if none are on record.
*/
Q_INVOKABLE int historicalPriceAccurate(int days) const;
/// return a string with the given price and needed decimal separator.
/// Note: the currency indicators are not included, unlike in formattedPrice()
/// \see currencySymbolPrefix()
/// \see currencySymbolPost()
Q_INVOKABLE QString priceToStringSimple(int64_t price) const;
/**
* Returns if the locale defined currency wants to display cents.
*
* Note: The typical 2 digits behind the separator are lazily called 'cents' here, even
* though the actual naming is differnt in different currencies.
*/
bool displayCents() const;
/**
* The string to show in front of a fiat price.
* Fiat prices need some unit, or currency-symbol, which we split into the prefix and post
* strings to help display in QML.
*/
QString currencySymbolPrefix() const;
/**
* The string to show after a fiat price.
* Fiat prices need some unit, or currency-symbol, which we split into the prefix and post
* strings to help display in QML.
*/
QString currencySymbolPost() const;
void loadPriceHistory(const QString &basedir);
signals:
void priceChanged(int32_t price);
void currencySymbolChanged();
private slots:
void fetch();
void finishedDownload();
void finishedYadioDownload();
private:
struct Price {
int32_t price = 0;
uint32_t timestamp = 0;
};
Price m_currentPrice;
QNetworkAccessManager m_network;
QNetworkReply *m_reply = nullptr;
QString m_currency;
QString m_currencySymbolPrefix, m_currencySymbolPost;
bool m_displayCents = true; // if true, display 2 digits behind the unit-separator
int m_failedCount = 0;
QTimer m_timer;
// Prices converted via USD
bool m_yadioViaUSD = false;
QMap<QString, double> m_currencyConversions;
QString m_basedir; // where the priceHistory is stored.
std::unique_ptr<PriceHistoryDataProvider> m_priceHistory;
};
#endif