peerplays_migrated/programs/light_client/ClientDataModel.hpp

254 lines
6.3 KiB
C++
Raw Normal View History

2015-07-10 21:37:22 +00:00
#pragma once
2015-07-13 17:56:30 +00:00
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
2015-07-13 19:41:06 +00:00
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
2015-07-13 17:56:30 +00:00
#include <fc/network/http/websocket.hpp>
#include <fc/thread/thread.hpp>
2015-07-13 19:41:06 +00:00
#include <graphene/app/api.hpp>
2015-07-10 21:37:22 +00:00
2015-07-14 17:06:32 +00:00
#include <QtQml>
2015-07-10 21:37:22 +00:00
#include <QObject>
#include <QQmlListProperty>
2015-07-13 19:41:06 +00:00
using boost::multi_index_container;
using namespace boost::multi_index;
2015-07-13 17:56:30 +00:00
using graphene::chain::by_id;
2015-07-14 17:06:32 +00:00
using ObjectId = qint64;
2015-07-14 19:06:00 +00:00
Q_DECLARE_METATYPE(ObjectId)
2015-07-14 17:06:32 +00:00
2015-07-13 18:14:58 +00:00
Q_DECLARE_METATYPE(std::function<void()>)
2015-07-13 17:56:30 +00:00
2015-07-14 21:30:15 +00:00
class GrapheneObject : public QObject
{
Q_OBJECT
Q_PROPERTY(ObjectId id MEMBER m_id READ id NOTIFY idChanged)
2015-07-14 21:30:15 +00:00
ObjectId m_id;
2015-07-14 21:30:15 +00:00
public:
GrapheneObject(ObjectId id = -1, QObject* parent = nullptr)
: QObject(parent), m_id(id)
{}
ObjectId id() const {
return m_id;
}
Q_SIGNALS:
void idChanged();
2015-07-14 21:30:15 +00:00
};
2015-07-14 17:06:32 +00:00
class Crypto {
Q_GADGET
public:
Q_INVOKABLE QString sha256(QByteArray data) {
return QCryptographicHash::hash(data, QCryptographicHash::Sha256).toHex();
}
};
QML_DECLARE_TYPE(Crypto)
2015-07-13 17:56:30 +00:00
2015-07-14 21:30:15 +00:00
class Asset : public GrapheneObject {
2015-07-10 21:37:22 +00:00
Q_OBJECT
Q_PROPERTY(QString symbol MEMBER m_symbol READ symbol NOTIFY symbolChanged)
Q_PROPERTY(quint32 precision MEMBER m_precision NOTIFY precisionChanged)
2015-07-14 21:30:15 +00:00
QString m_symbol;
quint32 m_precision;
graphene::chain::price coreExchangeRate;
public:
Asset(ObjectId id = -1, QString symbol = QString(), quint32 precision = 0, QObject* parent = nullptr)
: GrapheneObject(id, parent), m_symbol(symbol), m_precision(precision)
{}
QString symbol() const {
return m_symbol;
}
2015-07-10 21:37:22 +00:00
quint64 precisionPower() const {
quint64 power = 1;
for (int i = 0; i < m_precision; ++i)
power *= 10;
return power;
}
2015-07-14 21:30:15 +00:00
void update(const graphene::chain::asset_object& asset);
Q_SIGNALS:
void symbolChanged();
void precisionChanged();
2015-07-10 21:37:22 +00:00
};
2015-07-14 21:30:15 +00:00
struct by_symbol_name;
typedef multi_index_container<
Asset*,
indexed_by<
hashed_unique< tag<by_id>, const_mem_fun<GrapheneObject, ObjectId, &GrapheneObject::id > >,
ordered_unique< tag<by_symbol_name>, const_mem_fun<Asset, QString, &Asset::symbol> >
2015-07-14 21:30:15 +00:00
>
> asset_multi_index_type;
class Balance : public GrapheneObject {
2015-07-10 21:37:22 +00:00
Q_OBJECT
Q_PROPERTY(Asset* type MEMBER m_type READ type NOTIFY typeChanged)
Q_PROPERTY(qint64 amount MEMBER amount NOTIFY amountChanged)
2015-07-10 21:37:22 +00:00
Asset* m_type;
2015-07-10 21:37:22 +00:00
qint64 amount;
public:
// This ultimately needs to be replaced with a string equivalent
Q_INVOKABLE qreal amountReal() const {
return amount / qreal(m_type->precisionPower());
}
Asset* type()const {
return m_type;
}
void update(const graphene::app::account_balance_object& update);
Q_SIGNALS:
void typeChanged();
void amountChanged();
2015-07-10 21:37:22 +00:00
};
2015-07-14 21:30:15 +00:00
class Account : public GrapheneObject {
2015-07-10 21:37:22 +00:00
Q_OBJECT
Q_PROPERTY(QString name MEMBER m_name READ name NOTIFY nameChanged)
Q_PROPERTY(QQmlListProperty<Balance> balances READ balances NOTIFY balancesChanged)
2015-07-10 21:37:22 +00:00
QString m_name;
2015-07-10 21:37:22 +00:00
QList<Balance*> m_balances;
public:
Account(ObjectId id = -1, QString name = QString(), QObject* parent = nullptr)
: GrapheneObject(id, parent), m_name(name)
{}
QString name()const { return m_name; }
QQmlListProperty<Balance> balances();
2015-07-14 13:46:38 +00:00
void setBalances(QList<Balance*> balances) {
if (balances != m_balances) {
m_balances = balances;
Q_EMIT balancesChanged();
}
}
void update(const graphene::app::account_balance_object& balance);
Q_SIGNALS:
void nameChanged();
void balancesChanged();
2015-07-10 21:37:22 +00:00
};
2015-07-13 21:24:25 +00:00
struct by_account_name;
2015-07-13 19:41:06 +00:00
typedef multi_index_container<
Account*,
indexed_by<
hashed_unique< tag<by_id>, const_mem_fun<GrapheneObject, ObjectId, &GrapheneObject::id > >,
ordered_unique< tag<by_account_name>, const_mem_fun<Account, QString, &Account::name> >
2015-07-13 19:41:06 +00:00
>
> account_multi_index_type;
2015-07-13 17:19:07 +00:00
class ChainDataModel : public QObject {
2015-07-10 21:37:22 +00:00
Q_OBJECT
void processUpdatedObject(const fc::variant& update);
void getAssetImpl(QString assetIdentifier, Asset* const * assetInContainer);
void getAccountImpl(QString accountIdentifier, Account* const * accountInContainer);
2015-07-10 21:37:22 +00:00
public:
2015-07-14 17:06:32 +00:00
Q_INVOKABLE Account* getAccount(ObjectId id);
2015-07-13 19:41:06 +00:00
Q_INVOKABLE Account* getAccount(QString name);
Q_INVOKABLE Asset* getAsset(ObjectId id);
2015-07-14 21:30:15 +00:00
Q_INVOKABLE Asset* getAsset(QString symbol);
2015-07-13 17:56:30 +00:00
ChainDataModel(){}
ChainDataModel( fc::thread& t, QObject* parent = nullptr );
void setDatabaseAPI(fc::api<graphene::app::database_api> dbapi);
const graphene::chain::global_property_object& global_properties() const { return m_global_properties; }
2015-07-13 19:41:06 +00:00
Q_SIGNALS:
void queueExecute( const std::function<void()>& );
void exceptionThrown( QString message );
2015-07-13 17:56:30 +00:00
private:
fc::thread* m_rpc_thread = nullptr;
std::string m_api_url;
fc::api<graphene::app::database_api> m_db_api;
2015-07-13 19:41:06 +00:00
graphene::chain::global_property_object m_global_properties;
ObjectId m_account_query_num = -1;
account_multi_index_type m_accounts;
asset_multi_index_type m_assets;
2015-07-10 21:37:22 +00:00
};
2015-07-13 17:19:07 +00:00
class OperationBuilder;
2015-07-13 17:19:07 +00:00
class GrapheneApplication : public QObject {
Q_OBJECT
Q_PROPERTY(ChainDataModel* model READ model CONSTANT)
Q_PROPERTY(OperationBuilder* operationBuilder READ operationBuilder CONSTANT)
2015-07-13 17:19:07 +00:00
Q_PROPERTY(bool isConnected READ isConnected NOTIFY isConnectedChanged)
fc::thread m_thread;
ChainDataModel* m_model = nullptr;
OperationBuilder* m_operationBuilder = nullptr;
bool m_isConnected = false;
2015-07-13 17:56:30 +00:00
boost::signals2::scoped_connection m_connectionClosed;
std::shared_ptr<fc::http::websocket_client> m_client;
fc::future<void> m_done;
void setIsConnected(bool v);
2015-07-13 18:14:58 +00:00
protected Q_SLOTS:
void execute(const std::function<void()>&)const;
2015-07-13 18:14:58 +00:00
2015-07-13 17:19:07 +00:00
public:
GrapheneApplication(QObject* parent = nullptr);
2015-07-13 17:56:30 +00:00
~GrapheneApplication();
ChainDataModel* model() const {
2015-07-13 17:19:07 +00:00
return m_model;
}
OperationBuilder* operationBuilder() const {
return m_operationBuilder;
}
2015-07-13 17:19:07 +00:00
Q_INVOKABLE void start(QString apiUrl,
QString user,
QString pass);
2015-07-13 17:19:07 +00:00
bool isConnected() const
{
return m_isConnected;
}
2015-07-13 19:41:06 +00:00
Q_SIGNALS:
void exceptionThrown( QString message );
void loginFailed();
2015-07-13 17:19:07 +00:00
void isConnectedChanged(bool isConnected);
2015-07-13 18:14:58 +00:00
void queueExecute( const std::function<void()>& );
2015-07-13 17:19:07 +00:00
};