From 79f09605d561a342371f857b4fb66d249f017bbb Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 22 May 2014 14:55:43 -0700 Subject: [PATCH] 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