From 8b0d859686e0f61589eae5db6f2c7cecc76094f2 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Tue, 6 Jan 2015 15:54:07 -0800 Subject: [PATCH] Add method to access downloads information in JavaScript --- examples/example/downloadInfoExample.js | 25 +++++++++++ interface/src/Application.cpp | 1 + .../GlobalServicesScriptingInterface.cpp | 41 +++++++++++++++++++ .../GlobalServicesScriptingInterface.h | 14 +++++++ 4 files changed, 81 insertions(+) create mode 100644 examples/example/downloadInfoExample.js diff --git a/examples/example/downloadInfoExample.js b/examples/example/downloadInfoExample.js new file mode 100644 index 0000000000..399c83b5cf --- /dev/null +++ b/examples/example/downloadInfoExample.js @@ -0,0 +1,25 @@ +// +// downloadInfoExample.js +// examples/example +// +// Created by David Rowe on 5 Jan 2015 +// Copyright 2015 High Fidelity, Inc. +// +// Display downloads information the same as in the stats. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// Get and log the current downloads info ... + +var downloadInfo, + downloadInfoString; + +downloadInfo = GlobalServices.getDownloadInfo(); +downloadInfoString = "Downloads: "; +if (downloadInfo.downloading.length > 0) { + downloadInfoString += downloadInfo.downloading.join("% ") + "% "; +} +downloadInfoString += "(" + downloadInfo.pending.toFixed(0) + " pending)"; +print(downloadInfoString); diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index bbcf90312e..776fef71ff 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3669,6 +3669,7 @@ void Application::registerScriptEngineWithApplicationServices(ScriptEngine* scri scriptEngine->registerGlobalObject("Metavoxels", &_metavoxels); scriptEngine->registerGlobalObject("GlobalServices", GlobalServicesScriptingInterface::getInstance()); + qScriptRegisterMetaType(scriptEngine, DownloadInfoResultToScriptValue, DownloadInfoResultFromScriptValue); scriptEngine->registerGlobalObject("AvatarManager", &_avatarManager); diff --git a/interface/src/scripting/GlobalServicesScriptingInterface.cpp b/interface/src/scripting/GlobalServicesScriptingInterface.cpp index 26fa7c564b..24c2008603 100644 --- a/interface/src/scripting/GlobalServicesScriptingInterface.cpp +++ b/interface/src/scripting/GlobalServicesScriptingInterface.cpp @@ -11,6 +11,7 @@ #include "AccountManager.h" #include "XmppClient.h" +#include "ResourceCache.h" #include "GlobalServicesScriptingInterface.h" @@ -113,3 +114,43 @@ void GlobalServicesScriptingInterface::messageReceived(const QXmppMessage& messa emit GlobalServicesScriptingInterface::incomingMessage(message.from().right(message.from().count() - 1 - publicChatRoom->jid().count()), message.body()); } #endif // HAVE_QXMPP + +DownloadInfoResult GlobalServicesScriptingInterface::getDownloadInfo() { + DownloadInfoResult result; + foreach(Resource* resource, ResourceCache::getLoadingRequests()) { + result.downloading.append(resource->getProgress() * 100.0f); + } + result.pending = ResourceCache::getPendingRequestCount(); + + return result; +} + + +DownloadInfoResult::DownloadInfoResult() : + downloading(QList()), + pending(0.0f) +{ +} + +QScriptValue DownloadInfoResultToScriptValue(QScriptEngine* engine, const DownloadInfoResult& result) { + QScriptValue object = engine->newObject(); + + QScriptValue array = engine->newArray(result.downloading.count()); + for (int i = 0; i < result.downloading.count(); i += 1) { + array.setProperty(i, result.downloading[i]); + } + + object.setProperty("downloading", array); + object.setProperty("pending", result.pending); + return object; +} + +void DownloadInfoResultFromScriptValue(const QScriptValue& object, DownloadInfoResult& result) { + QList downloading = object.property("downloading").toVariant().toList(); + result.downloading.clear(); + for (int i = 0; i < downloading.count(); i += 1) { + result.downloading.append(downloading[i].toFloat()); + } + + result.pending = object.property("pending").toVariant().toFloat(); +} diff --git a/interface/src/scripting/GlobalServicesScriptingInterface.h b/interface/src/scripting/GlobalServicesScriptingInterface.h index 44b2f7c1bf..2e189a9a69 100644 --- a/interface/src/scripting/GlobalServicesScriptingInterface.h +++ b/interface/src/scripting/GlobalServicesScriptingInterface.h @@ -26,6 +26,19 @@ #endif // HAVE_QXMPP +class DownloadInfoResult { +public: + DownloadInfoResult(); + QList downloading; // List of percentages + float pending; +}; + +Q_DECLARE_METATYPE(DownloadInfoResult) + +QScriptValue DownloadInfoResultToScriptValue(QScriptEngine* engine, const DownloadInfoResult& result); +void DownloadInfoResultFromScriptValue(const QScriptValue& object, DownloadInfoResult& result); + + class GlobalServicesScriptingInterface : public QObject { Q_OBJECT Q_PROPERTY(bool isConnected READ isConnected) @@ -42,6 +55,7 @@ public: public slots: QScriptValue chat(const QString& message); + DownloadInfoResult getDownloadInfo(); private slots: void loggedOut();