peerplays_migrated/programs/light_client/ClientDataModel.hpp

158 lines
3.6 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
#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
2015-07-13 18:14:58 +00:00
Q_DECLARE_METATYPE(std::function<void()>)
2015-07-13 17:56:30 +00:00
2015-07-10 21:37:22 +00:00
class Asset : public QObject {
Q_OBJECT
Q_PROPERTY(QString symbol MEMBER symbol)
2015-07-13 19:41:06 +00:00
Q_PROPERTY(qint64 id MEMBER id)
2015-07-10 21:37:22 +00:00
Q_PROPERTY(quint8 precision MEMBER precision)
QString symbol;
2015-07-13 19:41:06 +00:00
qint64 id;
2015-07-10 21:37:22 +00:00
quint8 precision;
};
class Balance : public QObject {
Q_OBJECT
Q_PROPERTY(Asset* type MEMBER type)
Q_PROPERTY(qint64 amount MEMBER amount)
2015-07-13 19:41:06 +00:00
Q_PROPERTY(qint64 id MEMBER id)
2015-07-10 21:37:22 +00:00
Asset* type;
qint64 amount;
2015-07-13 19:41:06 +00:00
qint64 id;
2015-07-10 21:37:22 +00:00
};
class Account : public QObject {
Q_OBJECT
Q_PROPERTY(QString name MEMBER name NOTIFY nameChanged)
Q_PROPERTY(qint64 id MEMBER id NOTIFY idChanged)
2015-07-10 21:37:22 +00:00
Q_PROPERTY(QQmlListProperty<Balance> balances READ balances)
QString name;
2015-07-13 19:41:06 +00:00
qint64 id;
2015-07-10 21:37:22 +00:00
QList<Balance*> m_balances;
public:
Account(QObject* parent = nullptr)
: QObject(parent){}
2015-07-13 19:41:06 +00:00
const QString& getName()const { return name; }
qint64 getId()const { return id; }
2015-07-10 21:37:22 +00:00
QQmlListProperty<Balance> balances();
signals:
void nameChanged();
void idChanged();
2015-07-10 21:37:22 +00:00
};
struct by_id;
2015-07-13 19:41:06 +00:00
struct by_name;
/**
* @ingroup object_index
*/
typedef multi_index_container<
Account*,
indexed_by<
hashed_unique< tag<by_id>, const_mem_fun<Account, qint64, &Account::getId > >,
ordered_non_unique< tag<by_name>, const_mem_fun<Account, const QString&, &Account::getName> >
>
> 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
public:
2015-07-13 19:41:06 +00:00
Q_INVOKABLE Account* getAccount(qint64 id);
Q_INVOKABLE Account* getAccount(QString name);
2015-07-13 17:56:30 +00:00
ChainDataModel(){}
ChainDataModel( fc::thread& t, QObject* parent = nullptr );
2015-07-13 19:41:06 +00:00
void setDatabaseAPI( fc::api<graphene::app::database_api> dbapi ){ m_db_api = dbapi; }
Q_SIGNALS:
void queueExecute( const std::function<void()>& );
void exceptionThrown( QString message );
2015-07-13 17:56:30 +00:00
private:
2015-07-13 19:41:06 +00:00
fc::thread* m_thread = nullptr;
std::string m_api_url;
fc::api<graphene::app::database_api> m_db_api;
qint64 m_account_query_num = -1;
account_multi_index_type m_accounts;
2015-07-10 21:37:22 +00:00
};
2015-07-13 17:19:07 +00:00
2015-07-13 19:41:06 +00:00
2015-07-13 17:19:07 +00:00
class GrapheneApplication : public QObject {
Q_OBJECT
Q_PROPERTY(ChainDataModel* model READ model CONSTANT)
Q_PROPERTY(bool isConnected READ isConnected NOTIFY isConnectedChanged)
2015-07-13 17:56:30 +00:00
fc::thread m_thread;
ChainDataModel* m_model = nullptr;
bool m_isConnected = false;
fc::http::websocket_client m_client;
2015-07-13 18:14:58 +00:00
fc::future<void> m_done;
void setIsConnected( bool v );
Q_SLOT void execute( const std::function<void()>& )const;
2015-07-13 17:19:07 +00:00
public:
2015-07-13 17:56:30 +00:00
GrapheneApplication( QObject* parent = nullptr );
~GrapheneApplication();
2015-07-13 17:19:07 +00:00
ChainDataModel* model() const
{
return m_model;
}
Q_INVOKABLE void start(QString apiUrl,
QString user,
2015-07-13 18:14:58 +00:00
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
};