2015-07-10 21:37:22 +00:00
|
|
|
#include "ClientDataModel.hpp"
|
|
|
|
|
|
2015-07-13 17:56:30 +00:00
|
|
|
#include <graphene/app/api.hpp>
|
|
|
|
|
#include <graphene/chain/protocol/protocol.hpp>
|
|
|
|
|
|
|
|
|
|
#include <fc/rpc/websocket_api.hpp>
|
|
|
|
|
|
2015-07-13 18:14:58 +00:00
|
|
|
using namespace graphene::app;
|
|
|
|
|
|
2015-07-13 17:56:30 +00:00
|
|
|
ChainDataModel::ChainDataModel( fc::thread& t, QObject* parent )
|
|
|
|
|
:QObject(parent),m_thread(&t){}
|
2015-07-10 21:37:22 +00:00
|
|
|
|
2015-07-13 20:21:01 +00:00
|
|
|
Account* ChainDataModel::getAccount(qint64 id)
|
2015-07-10 21:37:22 +00:00
|
|
|
{
|
2015-07-13 19:41:06 +00:00
|
|
|
auto itr = m_accounts.find( id );
|
|
|
|
|
if( itr != m_accounts.end() )
|
|
|
|
|
return *itr;
|
|
|
|
|
|
|
|
|
|
auto acct = new Account(this);
|
2015-07-10 21:37:22 +00:00
|
|
|
acct->setProperty("id", id);
|
2015-07-13 21:24:25 +00:00
|
|
|
acct->setProperty("accountName", "LOADING");
|
2015-07-13 19:41:06 +00:00
|
|
|
auto insert_result = m_accounts.insert( acct );
|
|
|
|
|
|
|
|
|
|
/** execute in app thread */
|
2015-07-13 20:21:01 +00:00
|
|
|
m_thread->async( [=](){
|
2015-07-13 19:41:06 +00:00
|
|
|
try {
|
|
|
|
|
auto result = m_db_api->get_accounts( {account_id_type(id)} );
|
|
|
|
|
if( result.size() && result.front().valid() )
|
|
|
|
|
{
|
|
|
|
|
QString name = QString::fromStdString( result.front()->name );
|
|
|
|
|
/** execute in main */
|
|
|
|
|
Q_EMIT queueExecute( [=](){
|
2015-07-13 20:21:01 +00:00
|
|
|
this->m_accounts.modify( insert_result.first,
|
2015-07-13 21:24:25 +00:00
|
|
|
[=]( Account* a ){ a->setProperty("accountName", name ); }
|
2015-07-13 19:41:06 +00:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/** execute in main */
|
|
|
|
|
Q_EMIT queueExecute( [=](){
|
|
|
|
|
acct->deleteLater();
|
|
|
|
|
m_accounts.erase( insert_result.first );
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-07-13 20:21:01 +00:00
|
|
|
}
|
2015-07-13 19:41:06 +00:00
|
|
|
catch ( const fc::exception& e )
|
|
|
|
|
{
|
2015-07-13 20:21:01 +00:00
|
|
|
edump((e.to_detail_string()));
|
2015-07-13 19:41:06 +00:00
|
|
|
Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) );
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-10 21:37:22 +00:00
|
|
|
return acct;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-13 20:21:01 +00:00
|
|
|
Account* ChainDataModel::getAccount(QString name)
|
2015-07-10 21:37:22 +00:00
|
|
|
{
|
2015-07-13 21:24:25 +00:00
|
|
|
{
|
|
|
|
|
auto itr = m_accounts.get<by_account_name>().begin();
|
|
|
|
|
while( itr != m_accounts.get<by_account_name>().end() )
|
|
|
|
|
{
|
|
|
|
|
edump(( (*itr)->getAccountName().toStdString()));
|
|
|
|
|
if((*itr)->name() == name.toStdString() )
|
|
|
|
|
wlog( "THEY ARE THE SAME" );
|
|
|
|
|
++itr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
auto& by_name_index = m_accounts.get<by_account_name>();
|
|
|
|
|
{
|
|
|
|
|
for( auto itr = by_name_index.begin(); itr != by_name_index.end(); ++itr )
|
|
|
|
|
wdump(( (*itr)->getAccountName().toStdString()));
|
|
|
|
|
}
|
|
|
|
|
auto itr = by_name_index.find(name.toStdString());
|
|
|
|
|
idump((itr != by_name_index.end()));
|
|
|
|
|
if( itr != by_name_index.end() )
|
2015-07-13 19:41:06 +00:00
|
|
|
{
|
|
|
|
|
return *itr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto acct = new Account(this);
|
|
|
|
|
acct->setProperty("id", --m_account_query_num );
|
2015-07-13 21:24:25 +00:00
|
|
|
acct->setProperty("accountName", name);
|
2015-07-13 19:41:06 +00:00
|
|
|
auto insert_result = m_accounts.insert( acct );
|
2015-07-13 21:24:25 +00:00
|
|
|
edump( (insert_result.second) );
|
|
|
|
|
edump( (int64_t(*insert_result.first))(int64_t(acct)) );
|
|
|
|
|
auto itr2 = by_name_index.find(name.toStdString());
|
|
|
|
|
assert( itr2 != by_name_index.end() );
|
2015-07-13 19:41:06 +00:00
|
|
|
|
|
|
|
|
/** execute in app thread */
|
2015-07-13 20:21:01 +00:00
|
|
|
m_thread->async( [=](){
|
2015-07-13 19:41:06 +00:00
|
|
|
try {
|
2015-07-13 21:24:25 +00:00
|
|
|
ilog( "look up names.." );
|
2015-07-13 19:41:06 +00:00
|
|
|
auto result = m_db_api->lookup_account_names( {name.toStdString()} );
|
2015-07-13 21:24:25 +00:00
|
|
|
idump((result));
|
2015-07-13 19:41:06 +00:00
|
|
|
if( result.size() && result.front().valid() )
|
|
|
|
|
{
|
|
|
|
|
/** execute in main */
|
|
|
|
|
Q_EMIT queueExecute( [=](){
|
2015-07-13 20:21:01 +00:00
|
|
|
this->m_accounts.modify( insert_result.first,
|
|
|
|
|
[=]( Account* a ){
|
2015-07-13 21:24:25 +00:00
|
|
|
ilog( "setting id ${i}", ("i",result.front()->id.instance()) );
|
|
|
|
|
idump((int64_t(a)));
|
2015-07-13 20:21:01 +00:00
|
|
|
a->setProperty("id", result.front()->id.instance() );
|
2015-07-13 19:41:06 +00:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/** execute in main */
|
|
|
|
|
Q_EMIT queueExecute( [=](){
|
|
|
|
|
acct->deleteLater();
|
2015-07-13 21:24:25 +00:00
|
|
|
this->m_accounts.erase( insert_result.first );
|
2015-07-13 19:41:06 +00:00
|
|
|
});
|
|
|
|
|
}
|
2015-07-13 20:21:01 +00:00
|
|
|
}
|
2015-07-13 19:41:06 +00:00
|
|
|
catch ( const fc::exception& e )
|
|
|
|
|
{
|
2015-07-13 20:21:01 +00:00
|
|
|
edump((e.to_detail_string()));
|
2015-07-13 19:41:06 +00:00
|
|
|
Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) );
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-13 21:24:25 +00:00
|
|
|
idump((int64_t(acct)));
|
2015-07-13 20:21:01 +00:00
|
|
|
return acct;
|
2015-07-10 21:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QQmlListProperty<Balance> Account::balances()
|
|
|
|
|
{
|
|
|
|
|
return QQmlListProperty<Balance>(this, m_balances);
|
|
|
|
|
}
|
2015-07-13 17:24:30 +00:00
|
|
|
|
2015-07-13 17:56:30 +00:00
|
|
|
GrapheneApplication::GrapheneApplication( QObject* parent )
|
|
|
|
|
:QObject( parent ),m_thread("app")
|
|
|
|
|
{
|
2015-07-13 18:14:58 +00:00
|
|
|
connect( this, &GrapheneApplication::queueExecute,
|
|
|
|
|
this, &GrapheneApplication::execute );
|
|
|
|
|
|
2015-07-13 17:56:30 +00:00
|
|
|
m_model = new ChainDataModel( m_thread, this );
|
2015-07-13 19:41:06 +00:00
|
|
|
|
|
|
|
|
connect( m_model, &ChainDataModel::queueExecute,
|
|
|
|
|
this, &GrapheneApplication::execute );
|
|
|
|
|
|
|
|
|
|
connect( m_model, &ChainDataModel::exceptionThrown,
|
|
|
|
|
this, &GrapheneApplication::exceptionThrown );
|
2015-07-13 17:56:30 +00:00
|
|
|
}
|
2015-07-13 18:14:58 +00:00
|
|
|
|
2015-07-13 17:56:30 +00:00
|
|
|
GrapheneApplication::~GrapheneApplication()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-13 18:14:58 +00:00
|
|
|
void GrapheneApplication::setIsConnected( bool v )
|
|
|
|
|
{
|
|
|
|
|
if( v != m_isConnected )
|
|
|
|
|
{
|
|
|
|
|
m_isConnected = v;
|
|
|
|
|
Q_EMIT isConnectedChanged( m_isConnected );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-13 18:35:17 +00:00
|
|
|
void GrapheneApplication::start( QString apiurl, QString user, QString pass )
|
2015-07-13 17:24:30 +00:00
|
|
|
{
|
2015-07-13 17:56:30 +00:00
|
|
|
if( !m_thread.is_current() )
|
|
|
|
|
{
|
2015-07-13 18:35:17 +00:00
|
|
|
m_done = m_thread.async( [=](){ return start( apiurl, user, pass ); } );
|
2015-07-13 18:14:58 +00:00
|
|
|
return;
|
2015-07-13 17:56:30 +00:00
|
|
|
}
|
|
|
|
|
try {
|
2015-07-13 21:24:25 +00:00
|
|
|
m_client = std::make_shared<fc::http::websocket_client>();
|
|
|
|
|
ilog( "connecting...${s}", ("s",apiurl.toStdString()) );
|
|
|
|
|
auto con = m_client->connect( apiurl.toStdString() );
|
2015-07-13 17:56:30 +00:00
|
|
|
auto apic = std::make_shared<fc::rpc::websocket_api_connection>(*con);
|
2015-07-13 18:14:58 +00:00
|
|
|
auto remote_api = apic->get_remote_api< login_api >(1);
|
2015-07-13 19:41:06 +00:00
|
|
|
auto db_api = apic->get_remote_api< database_api >(0);
|
2015-07-13 18:14:58 +00:00
|
|
|
if( !remote_api->login( user.toStdString(), pass.toStdString() ) )
|
|
|
|
|
{
|
2015-07-13 21:24:25 +00:00
|
|
|
elog( "login failed" );
|
2015-07-13 19:41:06 +00:00
|
|
|
Q_EMIT loginFailed();
|
2015-07-13 18:14:58 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2015-07-13 17:56:30 +00:00
|
|
|
|
2015-07-13 21:24:25 +00:00
|
|
|
ilog( "connecting..." );
|
2015-07-13 19:41:06 +00:00
|
|
|
queueExecute( [=](){
|
|
|
|
|
m_model->setDatabaseAPI( db_api );
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-13 18:14:58 +00:00
|
|
|
queueExecute( [=](){setIsConnected( true );} );
|
2015-07-13 17:56:30 +00:00
|
|
|
} catch ( const fc::exception& e )
|
|
|
|
|
{
|
2015-07-13 19:41:06 +00:00
|
|
|
Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) );
|
2015-07-13 17:56:30 +00:00
|
|
|
}
|
2015-07-13 18:14:58 +00:00
|
|
|
}
|
|
|
|
|
Q_SLOT void GrapheneApplication::execute( const std::function<void()>& func )const
|
|
|
|
|
{
|
|
|
|
|
func();
|
2015-07-13 17:24:30 +00:00
|
|
|
}
|