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-13 21:22:06 +00:00
|
|
|
#include <QCryptographicHash>
|
2015-07-10 21:37:22 +00:00
|
|
|
#include <QObject>
|
|
|
|
|
#include <QQmlListProperty>
|
2015-07-13 21:22:06 +00:00
|
|
|
#include <QtQml>
|
2015-07-10 21:37:22 +00:00
|
|
|
|
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-13 21:22:06 +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-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
|
|
|
|
|
|
2015-07-13 21:24:25 +00:00
|
|
|
Q_PROPERTY(QString accountName MEMBER accountName NOTIFY accountNameChanged)
|
2015-07-13 20:21:01 +00:00
|
|
|
Q_PROPERTY(qint64 id MEMBER id NOTIFY idChanged)
|
2015-07-10 21:37:22 +00:00
|
|
|
Q_PROPERTY(QQmlListProperty<Balance> balances READ balances)
|
|
|
|
|
|
2015-07-13 21:24:25 +00:00
|
|
|
QString accountName;
|
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 21:24:25 +00:00
|
|
|
const QString& getAccountName()const { return accountName; }
|
2015-07-13 19:41:06 +00:00
|
|
|
qint64 getId()const { return id; }
|
2015-07-13 21:24:25 +00:00
|
|
|
std::string name()const { return accountName.toStdString(); }
|
2015-07-13 19:41:06 +00:00
|
|
|
|
2015-07-10 21:37:22 +00:00
|
|
|
QQmlListProperty<Balance> balances();
|
2015-07-13 20:21:01 +00:00
|
|
|
|
|
|
|
|
signals:
|
2015-07-13 21:24:25 +00:00
|
|
|
void accountNameChanged();
|
2015-07-13 20:21:01 +00:00
|
|
|
void idChanged();
|
2015-07-10 21:37:22 +00:00
|
|
|
};
|
|
|
|
|
|
2015-07-13 20:21:01 +00:00
|
|
|
struct by_id;
|
2015-07-13 21:24:25 +00:00
|
|
|
struct by_account_name;
|
2015-07-13 19:41:06 +00:00
|
|
|
/**
|
|
|
|
|
* @ingroup object_index
|
|
|
|
|
*/
|
|
|
|
|
typedef multi_index_container<
|
|
|
|
|
Account*,
|
|
|
|
|
indexed_by<
|
|
|
|
|
hashed_unique< tag<by_id>, const_mem_fun<Account, qint64, &Account::getId > >,
|
2015-07-13 21:24:25 +00:00
|
|
|
hashed_unique< tag<by_account_name>, const_mem_fun<Account, std::string, &Account::name> >
|
|
|
|
|
// ordered_non_unique< tag<by_account_name>, const_mem_fun<Account, std::string, &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
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2015-07-13 21:24:25 +00:00
|
|
|
std::shared_ptr<fc::http::websocket_client> m_client;
|
|
|
|
|
fc::future<void> m_done;
|
2015-07-13 18:14:58 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-13 18:35:17 +00:00
|
|
|
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
|
|
|
};
|