Showing the changes to interface/src/scripting/AccountServicesScriptingInterface.cpp (1 of 6 files). Show all
Unified · Split
| @@ -0,0 +1,161 @@ | ||
| 1 | +// | |
| 2 | +// AccountServicesScriptingInterface.cpp | |
| 3 | +// interface/src/scripting | |
| 4 | +// | |
| 5 | +// Created by Thijs Wenker on 9/10/14. | |
| 6 | +// Copyright 2014 High Fidelity, Inc. | |
| 7 | +// | |
| 8 | +// Distributed under the Apache License, Version 2.0. | |
| 9 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 10 | +// | |
| 11 | + | |
| 12 | +#include "AccountManager.h" | |
| 13 | +#include "Application.h" | |
| 14 | +#include "DiscoverabilityManager.h" | |
| 15 | +#include "ResourceCache.h" | |
| 16 | + | |
| 17 | +#include "AccountServicesScriptingInterface.h" | |
| 18 | + | |
| 19 | +AccountServicesScriptingInterface::AccountServicesScriptingInterface() { | |
| 20 | + auto accountManager = DependencyManager::get<AccountManager>(); | |
| 21 | + connect(accountManager.data(), &AccountManager::usernameChanged, this, &AccountServicesScriptingInterface::onUsernameChanged); | |
| 22 | + connect(accountManager.data(), &AccountManager::logoutComplete, this, &AccountServicesScriptingInterface::loggedOut); | |
| 23 | + connect(accountManager.data(), &AccountManager::loginComplete, this, &AccountServicesScriptingInterface::connected); | |
| 24 | + | |
| 25 | + _downloading = false; | |
| 26 | + QTimer* checkDownloadTimer = new QTimer(this); | |
| 27 | + connect(checkDownloadTimer, &QTimer::timeout, this, &AccountServicesScriptingInterface::checkDownloadInfo); | |
| 28 | + const int CHECK_DOWNLOAD_INTERVAL = MSECS_PER_SECOND / 2; | |
| 29 | + checkDownloadTimer->start(CHECK_DOWNLOAD_INTERVAL); | |
| 30 | + | |
| 31 | + auto discoverabilityManager = DependencyManager::get<DiscoverabilityManager>(); | |
| 32 | + connect(discoverabilityManager.data(), &DiscoverabilityManager::discoverabilityModeChanged, | |
| 33 | + this, &AccountServicesScriptingInterface::discoverabilityModeChanged); | |
| 34 | + | |
| 35 | + _loggedIn = isLoggedIn(); | |
| 36 | + emit loggedInChanged(_loggedIn); | |
| 37 | +} | |
| 38 | + | |
| 39 | +AccountServicesScriptingInterface::~AccountServicesScriptingInterface() { | |
| 40 | + auto accountManager = DependencyManager::get<AccountManager>(); | |
| 41 | + disconnect(accountManager.data(), &AccountManager::usernameChanged, this, &AccountServicesScriptingInterface::onUsernameChanged); | |
| 42 | + disconnect(accountManager.data(), &AccountManager::logoutComplete, this, &AccountServicesScriptingInterface::loggedOut); | |
| 43 | + disconnect(accountManager.data(), &AccountManager::loginComplete, this, &AccountServicesScriptingInterface::connected); | |
| 44 | +} | |
| 45 | + | |
| 46 | +AccountServicesScriptingInterface* AccountServicesScriptingInterface::getInstance() { | |
| 47 | + static AccountServicesScriptingInterface sharedInstance; | |
| 48 | + return &sharedInstance; | |
| 49 | +} | |
| 50 | + | |
| 51 | +const QString AccountServicesScriptingInterface::getUsername() const { | |
| 52 | + auto accountManager = DependencyManager::get<AccountManager>(); | |
| 53 | + if (accountManager->isLoggedIn()) { | |
| 54 | + return accountManager->getAccountInfo().getUsername(); | |
| 55 | + } else { | |
| 56 | + return "Unknown user"; | |
| 57 | + } | |
| 58 | +} | |
| 59 | + | |
| 60 | +bool AccountServicesScriptingInterface::isLoggedIn() { | |
| 61 | + auto accountManager = DependencyManager::get<AccountManager>(); | |
| 62 | + return accountManager->isLoggedIn(); | |
| 63 | +} | |
| 64 | + | |
| 65 | +bool AccountServicesScriptingInterface::checkAndSignalForAccessToken() { | |
| 66 | + auto accountManager = DependencyManager::get<AccountManager>(); | |
| 67 | + return accountManager->checkAndSignalForAccessToken(); | |
| 68 | +} | |
| 69 | + | |
| 70 | +void AccountServicesScriptingInterface::logOut() { | |
| 71 | + auto accountManager = DependencyManager::get<AccountManager>(); | |
| 72 | + return accountManager->logout(); | |
| 73 | +} | |
| 74 | + | |
| 75 | +void AccountServicesScriptingInterface::loggedOut() { | |
| 76 | + emit AccountServicesScriptingInterface::disconnected(QString("logout")); | |
| 77 | +} | |
| 78 | + | |
| 79 | +QString AccountServicesScriptingInterface::getFindableBy() const { | |
| 80 | + auto discoverabilityManager = DependencyManager::get<DiscoverabilityManager>(); | |
| 81 | + return DiscoverabilityManager::findableByString(discoverabilityManager->getDiscoverabilityMode()); | |
| 82 | +} | |
| 83 | + | |
| 84 | +void AccountServicesScriptingInterface::setFindableBy(const QString& discoverabilityMode) { | |
| 85 | + auto discoverabilityManager = DependencyManager::get<DiscoverabilityManager>(); | |
| 86 | + if (discoverabilityMode.toLower() == "none") { | |
| 87 | + discoverabilityManager->setDiscoverabilityMode(Discoverability::None); | |
| 88 | + } else if (discoverabilityMode.toLower() == "friends") { | |
| 89 | + discoverabilityManager->setDiscoverabilityMode(Discoverability::Friends); | |
| 90 | + } else if (discoverabilityMode.toLower() == "connections") { | |
| 91 | + discoverabilityManager->setDiscoverabilityMode(Discoverability::Connections); | |
| 92 | + } else if (discoverabilityMode.toLower() == "all") { | |
| 93 | + discoverabilityManager->setDiscoverabilityMode(Discoverability::All); | |
| 94 | + } else { | |
| 95 | + qDebug() << "GlobalServices setFindableBy called with an unrecognized value. Did not change discoverability."; | |
| 96 | + } | |
| 97 | +} | |
| 98 | + | |
| 99 | +void AccountServicesScriptingInterface::discoverabilityModeChanged(Discoverability::Mode discoverabilityMode) { | |
| 100 | + emit findableByChanged(DiscoverabilityManager::findableByString(discoverabilityMode)); | |
| 101 | +} | |
| 102 | + | |
| 103 | +void AccountServicesScriptingInterface::onUsernameChanged(const QString& username) { | |
| 104 | + _loggedIn = (username != QString()); | |
| 105 | + emit myUsernameChanged(username); | |
| 106 | + emit loggedInChanged(_loggedIn); | |
| 107 | +} | |
| 108 | + | |
| 109 | +DownloadInfoResult::DownloadInfoResult() : | |
| 110 | + downloading(QList<float>()), | |
| 111 | + pending(0.0f) | |
| 112 | +{ | |
| 113 | +} | |
| 114 | + | |
| 115 | +QScriptValue DownloadInfoResultToScriptValue(QScriptEngine* engine, const DownloadInfoResult& result) { | |
| 116 | + QScriptValue object = engine->newObject(); | |
| 117 | + | |
| 118 | + QScriptValue array = engine->newArray(result.downloading.count()); | |
| 119 | + for (int i = 0; i < result.downloading.count(); i += 1) { | |
| 120 | + array.setProperty(i, result.downloading[i]); | |
| 121 | + } | |
| 122 | + | |
| 123 | + object.setProperty("downloading", array); | |
| 124 | + object.setProperty("pending", result.pending); | |
| 125 | + return object; | |
| 126 | +} | |
| 127 | + | |
| 128 | +void DownloadInfoResultFromScriptValue(const QScriptValue& object, DownloadInfoResult& result) { | |
| 129 | + QList<QVariant> downloading = object.property("downloading").toVariant().toList(); | |
| 130 | + result.downloading.clear(); | |
| 131 | + for (int i = 0; i < downloading.count(); i += 1) { | |
| 132 | + result.downloading.append(downloading[i].toFloat()); | |
| 133 | + } | |
| 134 | + | |
| 135 | + result.pending = object.property("pending").toVariant().toFloat(); | |
| 136 | +} | |
| 137 | + | |
| 138 | +DownloadInfoResult AccountServicesScriptingInterface::getDownloadInfo() { | |
| 139 | + DownloadInfoResult result; | |
| 140 | + foreach(const auto& resource, ResourceCache::getLoadingRequests()) { | |
| 141 | + result.downloading.append(resource->getProgress() * 100.0f); | |
| 142 | + } | |
| 143 | + result.pending = ResourceCache::getPendingRequestCount(); | |
| 144 | + return result; | |
| 145 | +} | |
| 146 | + | |
| 147 | +void AccountServicesScriptingInterface::checkDownloadInfo() { | |
| 148 | + DownloadInfoResult downloadInfo = getDownloadInfo(); | |
| 149 | + bool downloading = downloadInfo.downloading.count() > 0 || downloadInfo.pending > 0; | |
| 150 | + | |
| 151 | + // Emit signal if downloading or have just finished. | |
| 152 | + if (downloading || _downloading) { | |
| 153 | + _downloading = downloading; | |
| 154 | + emit downloadInfoChanged(downloadInfo); | |
| 155 | + } | |
| 156 | +} | |
| 157 | + | |
| 158 | +void AccountServicesScriptingInterface::updateDownloadInfo() { | |
| 159 | + emit downloadInfoChanged(getDownloadInfo()); | |
| 160 | +} | |
| 161 | + | |