Files
pay/PriceDataProvider.h
T

80 lines
2.3 KiB
C++
Raw Permalink Normal View History

2021-05-07 19:47:11 +02:00
/*
* This file is part of the Flowee project
* Copyright (C) 2021 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 <QNetworkAccessManager>
#include <QObject>
#include <QTimer>
class PriceDataProvider : public QObject
{
Q_OBJECT
Q_PROPERTY(int price READ price NOTIFY priceChanged)
2021-05-08 00:03:07 +02:00
Q_PROPERTY(QString currencyName READ currencyName NOTIFY currencySymbolChanged)
2021-11-03 18:30:24 +01:00
Q_PROPERTY(bool dispayCents READ dispayCents NOTIFY dispayCentsChanged)
2021-05-07 19:47:11 +02:00
public:
explicit PriceDataProvider(QObject *parent = nullptr);
void start();
2021-05-08 00:03:07 +02:00
QString currencyName() const {
return m_currency;
}
2021-05-07 19:47:11 +02:00
// current price in cents
int price() const {
return m_currentPrice.price;
}
2021-05-08 00:03:07 +02:00
/**
* 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, int price) const;
Q_INVOKABLE QString formattedPrice(int cents) const;
2021-05-08 00:03:07 +02:00
2021-11-03 18:30:24 +01:00
bool dispayCents() const;
2021-05-07 19:47:11 +02:00
signals:
void priceChanged();
void currencySymbolChanged();
2021-11-03 18:30:24 +01:00
void dispayCentsChanged();
2021-05-07 19:47:11 +02:00
private slots:
void fetch();
void finishedDownload();
private:
struct Price {
int32_t price = 0;
uint32_t timestamp = 0;
};
Price m_currentPrice;
QNetworkAccessManager m_network;
QNetworkReply *m_reply = nullptr;
2021-05-08 12:22:44 +02:00
QString m_currency;
QString m_currencySymbolPrefix, m_currencySymbolPost;
2021-06-07 11:35:54 +02:00
bool m_dispayCents = true; // if true, display 2 digits behind the unit-separator
2021-05-07 19:47:11 +02:00
QTimer m_timer;
};
#endif