From 79f09605d561a342371f857b4fb66d249f017bbb Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 22 May 2014 14:55:43 -0700 Subject: [PATCH 1/6] show credit balance in window title --- interface/src/Application.cpp | 26 ++++++++++++++++--- libraries/networking/src/AccountManager.cpp | 20 ++++++++++++++ libraries/networking/src/AccountManager.h | 5 +++- .../networking/src/DataServerAccountInfo.cpp | 20 +++++++++++++- .../networking/src/DataServerAccountInfo.h | 9 +++++++ 5 files changed, 75 insertions(+), 5 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index fd09e530c1..c6704bcd80 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -237,12 +237,12 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : connect(&nodeList->getDomainHandler(), SIGNAL(connectedToDomain(const QString&)), SLOT(connectedToDomain(const QString&))); // update our location every 5 seconds in the data-server, assuming that we are authenticated with one - const float DATA_SERVER_LOCATION_CHANGE_UPDATE_MSECS = 5.0f * 1000.0f; + const qint64 DATA_SERVER_LOCATION_CHANGE_UPDATE_MSECS = 5 * 1000; QTimer* locationUpdateTimer = new QTimer(this); connect(locationUpdateTimer, &QTimer::timeout, this, &Application::updateLocationInServer); locationUpdateTimer->start(DATA_SERVER_LOCATION_CHANGE_UPDATE_MSECS); - + connect(nodeList, &NodeList::nodeAdded, this, &Application::nodeAdded); connect(nodeList, &NodeList::nodeKilled, this, &Application::nodeKilled); connect(nodeList, SIGNAL(nodeKilled(SharedNodePointer)), SLOT(nodeKilled(SharedNodePointer))); @@ -251,9 +251,18 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : connect(nodeList, &NodeList::uuidChanged, this, &Application::updateWindowTitle); connect(nodeList, SIGNAL(uuidChanged(const QUuid&)), _myAvatar, SLOT(setSessionUUID(const QUuid&))); connect(nodeList, &NodeList::limitOfSilentDomainCheckInsReached, nodeList, &NodeList::reset); - + // connect to appropriate slots on AccountManager AccountManager& accountManager = AccountManager::getInstance(); + + const qint64 BALANCE_UPDATE_INTERVAL_MSECS = 5 * 1000; + + QTimer* balanceUpdateTimer = new QTimer(this); + connect(balanceUpdateTimer, &QTimer::timeout, &accountManager, &AccountManager::updateBalance); + balanceUpdateTimer->start(BALANCE_UPDATE_INTERVAL_MSECS); + + connect(&accountManager, &AccountManager::balanceChanged, this, &Application::updateWindowTitle); + connect(&accountManager, &AccountManager::authRequired, Menu::getInstance(), &Menu::loginForCurrentDomain); connect(&accountManager, &AccountManager::usernameChanged, this, &Application::updateWindowTitle); @@ -3095,6 +3104,17 @@ void Application::updateWindowTitle(){ QString title = QString() + (!username.isEmpty() ? username + " @ " : QString()) + nodeList->getDomainHandler().getHostname() + buildVersion; qDebug("Application title set to: %s", title.toStdString().c_str()); + + AccountManager& accountManager = AccountManager::getInstance(); + if (accountManager.getAccountInfo().hasBalance()) { + float creditBalance = accountManager.getAccountInfo().getBalance() * pow(10.0f, -8.0f); + + QString creditBalanceString; + creditBalanceString.sprintf("%.8f", creditBalance); + + title += " - ₵" + creditBalanceString; + } + _window->setWindowTitle(title); } diff --git a/libraries/networking/src/AccountManager.cpp b/libraries/networking/src/AccountManager.cpp index aad2cfb386..7d27332a57 100644 --- a/libraries/networking/src/AccountManager.cpp +++ b/libraries/networking/src/AccountManager.cpp @@ -61,6 +61,8 @@ AccountManager::AccountManager() : qRegisterMetaType("QNetworkAccessManager::Operation"); qRegisterMetaType("JSONCallbackParameters"); + + connect(&_accountInfo, &DataServerAccountInfo::balanceChanged, this, &AccountManager::accountInfoBalanceChanged); } const QString DOUBLE_SLASH_SUBSTITUTE = "slashslash"; @@ -69,6 +71,9 @@ void AccountManager::logout() { // a logout means we want to delete the DataServerAccountInfo we currently have for this URL, in-memory and in file _accountInfo = DataServerAccountInfo(); + emit balanceChanged(0); + connect(&_accountInfo, &DataServerAccountInfo::balanceChanged, this, &AccountManager::accountInfoBalanceChanged); + QSettings settings; settings.beginGroup(ACCOUNTS_GROUP); @@ -82,6 +87,21 @@ void AccountManager::logout() { emit usernameChanged(QString()); } +void AccountManager::updateBalance() { + if (hasValidAccessToken()) { + // ask our auth endpoint for our balance + JSONCallbackParameters callbackParameters; + callbackParameters.jsonCallbackReceiver = &_accountInfo; + callbackParameters.jsonCallbackMethod = "setBalanceFromJSON"; + + authenticatedRequest("/api/v1/wallets/mine", QNetworkAccessManager::GetOperation, callbackParameters); + } +} + +void AccountManager::accountInfoBalanceChanged(qint64 newBalance) { + emit balanceChanged(newBalance); +} + void AccountManager::setAuthURL(const QUrl& authURL) { if (_authURL != authURL) { _authURL = authURL; diff --git a/libraries/networking/src/AccountManager.h b/libraries/networking/src/AccountManager.h index 6bdf5d76d8..f32ff75f51 100644 --- a/libraries/networking/src/AccountManager.h +++ b/libraries/networking/src/AccountManager.h @@ -34,7 +34,7 @@ public: QString updateSlot; }; -class AccountManager : public QObject { +class AccountManager: public QObject { Q_OBJECT public: static AccountManager& getInstance(); @@ -63,6 +63,8 @@ public slots: void requestFinished(); void requestError(QNetworkReply::NetworkError error); void logout(); + void updateBalance(); + void accountInfoBalanceChanged(qint64 newBalance); signals: void authRequired(); void authEndpointChanged(); @@ -71,6 +73,7 @@ signals: void loginComplete(const QUrl& authURL); void loginFailed(); void logoutComplete(); + void balanceChanged(qint64 newBalance); private slots: void processReply(); private: diff --git a/libraries/networking/src/DataServerAccountInfo.cpp b/libraries/networking/src/DataServerAccountInfo.cpp index 0fdb5ff4b1..ac664bca7c 100644 --- a/libraries/networking/src/DataServerAccountInfo.cpp +++ b/libraries/networking/src/DataServerAccountInfo.cpp @@ -17,7 +17,9 @@ DataServerAccountInfo::DataServerAccountInfo() : _accessToken(), _username(), _xmppPassword(), - _discourseApiKey() + _discourseApiKey(), + _balance(0), + _hasBalance(false) { } @@ -75,6 +77,22 @@ void DataServerAccountInfo::setDiscourseApiKey(const QString& discourseApiKey) { } } +void DataServerAccountInfo::setBalance(quint64 balance) { + if (!_hasBalance || _balance != balance) { + _balance = balance; + _hasBalance = true; + + emit balanceChanged(_balance); + } +} + +void DataServerAccountInfo::setBalanceFromJSON(const QJsonObject& jsonObject) { + if (jsonObject["status"].toString() == "success") { + qint64 balanceInSatoshis = jsonObject["data"].toObject()["wallet"].toObject()["balance"].toInt(); + setBalance(balanceInSatoshis); + } +} + QDataStream& operator<<(QDataStream &out, const DataServerAccountInfo& info) { out << info._accessToken << info._username << info._xmppPassword << info._discourseApiKey; return out; diff --git a/libraries/networking/src/DataServerAccountInfo.h b/libraries/networking/src/DataServerAccountInfo.h index a7d1fa9cb0..f3fe2401fe 100644 --- a/libraries/networking/src/DataServerAccountInfo.h +++ b/libraries/networking/src/DataServerAccountInfo.h @@ -34,9 +34,16 @@ public: const QString& getDiscourseApiKey() const { return _discourseApiKey; } void setDiscourseApiKey(const QString& discourseApiKey); + + quint64 getBalance() const { return _balance; } + void setBalance(quint64 balance); + bool hasBalance() const { return _hasBalance; } + Q_INVOKABLE void setBalanceFromJSON(const QJsonObject& jsonObject); friend QDataStream& operator<<(QDataStream &out, const DataServerAccountInfo& info); friend QDataStream& operator>>(QDataStream &in, DataServerAccountInfo& info); +signals: + qint64 balanceChanged(qint64 newBalance); private: void swap(DataServerAccountInfo& otherInfo); @@ -44,6 +51,8 @@ private: QString _username; QString _xmppPassword; QString _discourseApiKey; + quint64 _balance; + bool _hasBalance; }; #endif // hifi_DataServerAccountInfo_h From 164d3c5fb3aea2ff3c523efc19260ebb569115b9 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 22 May 2014 15:06:16 -0700 Subject: [PATCH 2/6] make sure balance is cleared on logout and re-launch --- interface/src/Application.cpp | 2 +- libraries/networking/src/DataServerAccountInfo.cpp | 8 +++++++- libraries/networking/src/DataServerAccountInfo.h | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index c6704bcd80..07309fab85 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3103,7 +3103,6 @@ void Application::updateWindowTitle(){ QString username = AccountManager::getInstance().getAccountInfo().getUsername(); QString title = QString() + (!username.isEmpty() ? username + " @ " : QString()) + nodeList->getDomainHandler().getHostname() + buildVersion; - qDebug("Application title set to: %s", title.toStdString().c_str()); AccountManager& accountManager = AccountManager::getInstance(); if (accountManager.getAccountInfo().hasBalance()) { @@ -3115,6 +3114,7 @@ void Application::updateWindowTitle(){ title += " - ₵" + creditBalanceString; } + qDebug("Application title set to: %s", title.toStdString().c_str()); _window->setWindowTitle(title); } diff --git a/libraries/networking/src/DataServerAccountInfo.cpp b/libraries/networking/src/DataServerAccountInfo.cpp index ac664bca7c..b3607200fe 100644 --- a/libraries/networking/src/DataServerAccountInfo.cpp +++ b/libraries/networking/src/DataServerAccountInfo.cpp @@ -27,7 +27,9 @@ DataServerAccountInfo::DataServerAccountInfo() : DataServerAccountInfo::DataServerAccountInfo(const QJsonObject& jsonObject) : _accessToken(jsonObject), _username(), - _xmppPassword() + _xmppPassword(), + _balance(0), + _hasBalance(false) { QJsonObject userJSONObject = jsonObject["user"].toObject(); setUsername(userJSONObject["username"].toString()); @@ -40,6 +42,8 @@ DataServerAccountInfo::DataServerAccountInfo(const DataServerAccountInfo& otherI _username = otherInfo._username; _xmppPassword = otherInfo._xmppPassword; _discourseApiKey = otherInfo._discourseApiKey; + _balance = otherInfo._balance; + _hasBalance = otherInfo._hasBalance; } DataServerAccountInfo& DataServerAccountInfo::operator=(const DataServerAccountInfo& otherInfo) { @@ -55,6 +59,8 @@ void DataServerAccountInfo::swap(DataServerAccountInfo& otherInfo) { swap(_username, otherInfo._username); swap(_xmppPassword, otherInfo._xmppPassword); swap(_discourseApiKey, otherInfo._discourseApiKey); + swap(_balance, otherInfo._balance); + swap(_hasBalance, otherInfo._hasBalance); } void DataServerAccountInfo::setUsername(const QString& username) { diff --git a/libraries/networking/src/DataServerAccountInfo.h b/libraries/networking/src/DataServerAccountInfo.h index f3fe2401fe..fd135f922b 100644 --- a/libraries/networking/src/DataServerAccountInfo.h +++ b/libraries/networking/src/DataServerAccountInfo.h @@ -38,6 +38,7 @@ public: quint64 getBalance() const { return _balance; } void setBalance(quint64 balance); bool hasBalance() const { return _hasBalance; } + void setHasBalance(bool hasBalance) { _hasBalance = hasBalance; } Q_INVOKABLE void setBalanceFromJSON(const QJsonObject& jsonObject); friend QDataStream& operator<<(QDataStream &out, const DataServerAccountInfo& info); From 40c59a35890eba2c7b3424f81d667dd0fce0aa2f Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 22 May 2014 15:10:41 -0700 Subject: [PATCH 3/6] fix rate of payment for assignments --- domain-server/src/DomainServer.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 9ad36e6956..20e34744db 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -731,10 +731,11 @@ void DomainServer::setupPendingAssignmentCredits() { qint64 elapsedMsecsSinceLastPayment = nodeData->getPaymentIntervalTimer().elapsed(); nodeData->getPaymentIntervalTimer().restart(); - const float CREDITS_PER_HOUR = 3; + const float CREDITS_PER_HOUR = 0.10; const float CREDITS_PER_MSEC = CREDITS_PER_HOUR / (60 * 60 * 1000); + const int SATOSHIS_PER_MSEC = CREDITS_PER_MSEC * powf(10, 8); - float pendingCredits = elapsedMsecsSinceLastPayment * CREDITS_PER_MSEC; + float pendingCredits = elapsedMsecsSinceLastPayment * SATOSHIS_PER_MSEC; if (existingTransaction) { existingTransaction->incrementAmount(pendingCredits); From fc9d850c40399bcdf20e6fcbcc6a24357dda83c1 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 22 May 2014 15:33:06 -0700 Subject: [PATCH 4/6] output when pay-for-assignments is on --- domain-server/src/DomainServer.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 20e34744db..cd8e66a2b5 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -218,6 +218,8 @@ bool DomainServer::optionallySetupAssignmentPayment() { } } + qDebug() << "Assignments will be paid for via" << qPrintable(_oauthProviderURL.toString()); + // assume that the fact we are authing against HF data server means we will pay for assignments // setup a timer to send transactions to pay assigned nodes every 30 seconds QTimer* creditSetupTimer = new QTimer(this); From 6d500d64d12bb4d75cb817208e7f13e086ec6deb Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 22 May 2014 17:12:22 -0700 Subject: [PATCH 5/6] fix floats that aren't actually floats --- domain-server/src/DomainServer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index cd8e66a2b5..f979de64c0 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -733,9 +733,9 @@ void DomainServer::setupPendingAssignmentCredits() { qint64 elapsedMsecsSinceLastPayment = nodeData->getPaymentIntervalTimer().elapsed(); nodeData->getPaymentIntervalTimer().restart(); - const float CREDITS_PER_HOUR = 0.10; + const float CREDITS_PER_HOUR = 0.10f; const float CREDITS_PER_MSEC = CREDITS_PER_HOUR / (60 * 60 * 1000); - const int SATOSHIS_PER_MSEC = CREDITS_PER_MSEC * powf(10, 8); + const int SATOSHIS_PER_MSEC = CREDITS_PER_MSEC * powf(10.0f, 8.0f); float pendingCredits = elapsedMsecsSinceLastPayment * SATOSHIS_PER_MSEC; From 4f81bac742a2bf54db72e78f8cc1760c00d6438d Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 22 May 2014 17:16:42 -0700 Subject: [PATCH 6/6] add back a missing space in AccountManager --- libraries/networking/src/AccountManager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/networking/src/AccountManager.h b/libraries/networking/src/AccountManager.h index f32ff75f51..628b084ea8 100644 --- a/libraries/networking/src/AccountManager.h +++ b/libraries/networking/src/AccountManager.h @@ -34,7 +34,7 @@ public: QString updateSlot; }; -class AccountManager: public QObject { +class AccountManager : public QObject { Q_OBJECT public: static AccountManager& getInstance();