diff --git a/programs/light_client/ClientDataModel.cpp b/programs/light_client/ClientDataModel.cpp index 3a207de4..6b45b9e4 100644 --- a/programs/light_client/ClientDataModel.cpp +++ b/programs/light_client/ClientDataModel.cpp @@ -12,118 +12,102 @@ ChainDataModel::ChainDataModel( fc::thread& t, QObject* parent ) Account* ChainDataModel::getAccount(qint64 id) { - auto itr = m_accounts.find( id ); - if( itr != m_accounts.end() ) - return *itr; + auto& by_id_idx = m_accounts.get<::by_id>(); + auto itr = by_id_idx.find(id); + if( itr == by_id_idx.end() ) + { + auto tmp = new Account; + tmp->id = id; --m_account_query_num; + tmp->name = QString::number( --m_account_query_num); + auto result = m_accounts.insert( tmp ); + assert( result.second ); - auto acct = new Account(this); - acct->setProperty("id", id); - acct->setProperty("accountName", "LOADING"); - auto insert_result = m_accounts.insert( acct ); + /** execute in app thread */ + m_thread->async( [this,id](){ + try { + ilog( "look up names.." ); + auto result = m_db_api->get_accounts( {account_id_type(id)} ); + /** execute in main */ + Q_EMIT queueExecute( [this,result,id](){ + wlog( "process result" ); + auto& by_id_idx = this->m_accounts.get<::by_id>(); + auto itr = by_id_idx.find(id); + assert( itr != by_id_idx.end() ); - /** execute in app thread */ - m_thread->async( [=](){ - 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( [=](){ - this->m_accounts.modify( insert_result.first, - [=]( Account* a ){ a->setProperty("accountName", name ); } - ); - }); - } - else - { - /** execute in main */ - Q_EMIT queueExecute( [=](){ - acct->deleteLater(); - m_accounts.erase( insert_result.first ); - }); - } - } - catch ( const fc::exception& e ) - { - edump((e.to_detail_string())); - Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) ); - } - }); - - return acct; + if( result.size() == 0 || !result.front() ) + { + elog( "delete later" ); + (*itr)->deleteLater(); + by_id_idx.erase( itr ); + } + else + { + by_id_idx.modify( itr, + [=]( Account* a ){ + a->setProperty("name", QString::fromStdString(result.front()->name) ); + } + ); + } + }); + } + catch ( const fc::exception& e ) + { + Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) ); + } + }); + return *result.first; + } + return *itr; } Account* ChainDataModel::getAccount(QString name) { + auto& by_name_idx = m_accounts.get(); + auto itr = by_name_idx.find(name); + if( itr == by_name_idx.end() ) { - auto itr = m_accounts.get().begin(); - while( itr != m_accounts.get().end() ) - { - edump(( (*itr)->getAccountName().toStdString())); - if((*itr)->name() == name.toStdString() ) - wlog( "THEY ARE THE SAME" ); - ++itr; - } - } - auto& by_name_index = m_accounts.get(); - { - 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() ) - { - return *itr; - } + auto tmp = new Account; + tmp->id = --m_account_query_num; + tmp->name = name; + auto result = m_accounts.insert( tmp ); + assert( result.second ); - auto acct = new Account(this); - acct->setProperty("id", --m_account_query_num ); - acct->setProperty("accountName", name); - auto insert_result = m_accounts.insert( acct ); - 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() ); + /** execute in app thread */ + m_thread->async( [this,name](){ + try { + ilog( "look up names.." ); + auto result = m_db_api->lookup_account_names( {name.toStdString()} ); + /** execute in main */ + Q_EMIT queueExecute( [this,result,name](){ + wlog( "process result" ); + auto& by_name_idx = this->m_accounts.get(); + auto itr = by_name_idx.find(name); + assert( itr != by_name_idx.end() ); - /** execute in app thread */ - m_thread->async( [=](){ - try { - ilog( "look up names.." ); - auto result = m_db_api->lookup_account_names( {name.toStdString()} ); - idump((result)); - if( result.size() && result.front().valid() ) - { - /** execute in main */ - Q_EMIT queueExecute( [=](){ - this->m_accounts.modify( insert_result.first, - [=]( Account* a ){ - ilog( "setting id ${i}", ("i",result.front()->id.instance()) ); - idump((int64_t(a))); - a->setProperty("id", result.front()->id.instance() ); - } - ); - }); - } - else - { - /** execute in main */ - Q_EMIT queueExecute( [=](){ - acct->deleteLater(); - this->m_accounts.erase( insert_result.first ); - }); - } - } - catch ( const fc::exception& e ) - { - edump((e.to_detail_string())); - Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) ); - } - }); - - idump((int64_t(acct))); - return acct; + if( result.size() == 0 || !result.front() ) + { + elog( "delete later" ); + (*itr)->deleteLater(); + by_name_idx.erase( itr ); + } + else + { + by_name_idx.modify( itr, + [=]( Account* a ){ + a->setProperty("id", result.front()->id.instance() ); + } + ); + } + }); + } + catch ( const fc::exception& e ) + { + Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) ); + } + }); + return *result.first; + } + return *itr; } QQmlListProperty Account::balances() diff --git a/programs/light_client/ClientDataModel.hpp b/programs/light_client/ClientDataModel.hpp index 2b1811b5..dd258e4c 100644 --- a/programs/light_client/ClientDataModel.hpp +++ b/programs/light_client/ClientDataModel.hpp @@ -11,25 +11,14 @@ #include #include -#include #include #include -#include using boost::multi_index_container; using namespace boost::multi_index; Q_DECLARE_METATYPE(std::function) -class Crypto { - Q_GADGET - -public: - Q_INVOKABLE QString sha256(QByteArray data) { - return QCryptographicHash::hash(data, QCryptographicHash::Sha256).toHex(); - } -}; -QML_DECLARE_TYPE(Crypto) class Asset : public QObject { Q_OBJECT @@ -58,26 +47,26 @@ class Balance : public QObject { class Account : public QObject { Q_OBJECT - Q_PROPERTY(QString accountName MEMBER accountName NOTIFY accountNameChanged) + Q_PROPERTY(QString name MEMBER name NOTIFY nameChanged) Q_PROPERTY(qint64 id MEMBER id NOTIFY idChanged) Q_PROPERTY(QQmlListProperty balances READ balances) - QString accountName; - qint64 id; QList m_balances; public: - Account(QObject* parent = nullptr) - : QObject(parent){} + // Account(QObject* parent = nullptr) + // : QObject(parent){} - const QString& getAccountName()const { return accountName; } + const QString& getName()const { return name; } qint64 getId()const { return id; } - std::string name()const { return accountName.toStdString(); } QQmlListProperty balances(); + QString name; + qint64 id; + signals: - void accountNameChanged(); + void nameChanged(); void idChanged(); }; @@ -90,8 +79,7 @@ typedef multi_index_container< Account*, indexed_by< hashed_unique< tag, const_mem_fun >, - hashed_unique< tag, const_mem_fun > -// ordered_non_unique< tag, const_mem_fun > + ordered_unique< tag, const_mem_fun > > > account_multi_index_type; diff --git a/programs/light_client/main.cpp b/programs/light_client/main.cpp index 2c733daa..a761fbea 100644 --- a/programs/light_client/main.cpp +++ b/programs/light_client/main.cpp @@ -21,9 +21,11 @@ int main(int argc, char *argv[]) qmlRegisterType("Graphene.Client", 0, 1, "GrapheneApplication"); QQmlApplicationEngine engine; + /* QVariant crypto; crypto.setValue(Crypto()); engine.rootContext()->setContextProperty("Crypto", crypto); + */ #ifdef NDEBUG engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); #else diff --git a/programs/light_client/qml/main.qml b/programs/light_client/qml/main.qml index 9e713872..ef5f0c84 100644 --- a/programs/light_client/qml/main.qml +++ b/programs/light_client/qml/main.qml @@ -55,12 +55,12 @@ ApplicationWindow { } TextField { id: nameField - onAccepted: lookupButton.clicked() + onAccepted: lookupNameButton.clicked() focus: true } Button { - id: lookupButton - text: "Lookup" + id: lookupNameButton + text: "Lookup Name" onClicked: { var acct = app.model.getAccount(nameField.text) console.log(JSON.stringify(acct)) @@ -83,6 +83,37 @@ ApplicationWindow { } } + TextField { + id: idField + onAccepted: lookupIdButton.clicked() + focus: true + } + Button { + id: lookupIdButton + text: "Lookup ID" + onClicked: { + var acct = app.model.getAccount(parseInt(idField.text)) + console.log(JSON.stringify(acct)) + // @disable-check M126 + if (acct == null) + console.log("Got back null account") + else if ( !(parseInt(acct.name) <= 0) ) + { + console.log("NAME ALREADY SET" ); + console.log(JSON.stringify(acct)) + } + else + { + console.log("Waiting for result...") + acct.nameChanged.connect(function(loadedAcct) { + console.log( "NAME CHANGED" ); + console.log(JSON.stringify(acct)) + }) + } + } + } + + } FormBox {