From 8b0d859686e0f61589eae5db6f2c7cecc76094f2 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Tue, 6 Jan 2015 15:54:07 -0800 Subject: [PATCH 1/3] 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(); From 40274df2791689eeaf956673fcebb1e8262b62bd Mon Sep 17 00:00:00 2001 From: David Rowe Date: Wed, 7 Jan 2015 11:20:46 -0800 Subject: [PATCH 2/3] Add downloads information changed event for JavaScript --- examples/example/downloadInfoExample.js | 57 ++++++++++++++++--- .../GlobalServicesScriptingInterface.cpp | 40 +++++++++---- .../GlobalServicesScriptingInterface.h | 6 ++ 3 files changed, 84 insertions(+), 19 deletions(-) diff --git a/examples/example/downloadInfoExample.js b/examples/example/downloadInfoExample.js index 399c83b5cf..19995503bc 100644 --- a/examples/example/downloadInfoExample.js +++ b/examples/example/downloadInfoExample.js @@ -11,15 +11,56 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +var downloadInfo, + downloadInfoOverlay; + +function formatInfo(info) { + var string = "Downloads: ", + i; + + for (i = 0; i < info.downloading.length; i += 1) { + string += info.downloading[i].toFixed(0) + "% "; + } + + string += "(" + info.pending.toFixed(0) + " pending)"; + + return string; +} + + // Get and log the current downloads info ... -var downloadInfo, - downloadInfoString; - downloadInfo = GlobalServices.getDownloadInfo(); -downloadInfoString = "Downloads: "; -if (downloadInfo.downloading.length > 0) { - downloadInfoString += downloadInfo.downloading.join("% ") + "% "; +print(formatInfo(downloadInfo)); + + +// Display and update the downloads info in an overlay ... + +function setUp() { + downloadInfoOverlay = Overlays.addOverlay("text", { + x: 300, + y: 200, + width: 300, + height: 50, + color: { red: 255, green: 255, blue: 255 }, + alpha: 1.0, + backgroundColor: { red: 127, green: 127, blue: 127 }, + backgroundAlpha: 0.5, + topMargin: 15, + leftMargin: 20, + text: "" + }); } -downloadInfoString += "(" + downloadInfo.pending.toFixed(0) + " pending)"; -print(downloadInfoString); + +function updateInfo(info) { + Overlays.editOverlay(downloadInfoOverlay, { text: formatInfo(info) }); +} + +function tearDown() { + Overlays.deleteOverlay(downloadInfoOverlay); +} + +setUp(); +GlobalServices.downloadInfoChanged.connect(updateInfo); +GlobalServices.updateDownloadInfo(); +Script.scriptEnding.connect(tearDown); diff --git a/interface/src/scripting/GlobalServicesScriptingInterface.cpp b/interface/src/scripting/GlobalServicesScriptingInterface.cpp index 24c2008603..94777acbc0 100644 --- a/interface/src/scripting/GlobalServicesScriptingInterface.cpp +++ b/interface/src/scripting/GlobalServicesScriptingInterface.cpp @@ -10,8 +10,9 @@ // #include "AccountManager.h" -#include "XmppClient.h" +#include "Application.h" #include "ResourceCache.h" +#include "XmppClient.h" #include "GlobalServicesScriptingInterface.h" @@ -26,6 +27,9 @@ GlobalServicesScriptingInterface::GlobalServicesScriptingInterface() { const QXmppClient& qxmppClient = XmppClient::getInstance().getXMPPClient(); connect(&qxmppClient, &QXmppClient::messageReceived, this, &GlobalServicesScriptingInterface::messageReceived); #endif // HAVE_QXMPP + + _downloading = false; + connect(Application::getInstance(), &Application::renderingInWorldInterface, this, &GlobalServicesScriptingInterface::checkDownloadInfo); } GlobalServicesScriptingInterface::~GlobalServicesScriptingInterface() { @@ -115,16 +119,6 @@ void GlobalServicesScriptingInterface::messageReceived(const QXmppMessage& messa } #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()), @@ -154,3 +148,27 @@ void DownloadInfoResultFromScriptValue(const QScriptValue& object, DownloadInfoR result.pending = object.property("pending").toVariant().toFloat(); } + +DownloadInfoResult GlobalServicesScriptingInterface::getDownloadInfo() { + DownloadInfoResult result; + foreach(Resource* resource, ResourceCache::getLoadingRequests()) { + result.downloading.append(resource->getProgress() * 100.0f); + } + result.pending = ResourceCache::getPendingRequestCount(); + return result; +} + +void GlobalServicesScriptingInterface::checkDownloadInfo() { + DownloadInfoResult downloadInfo = getDownloadInfo(); + bool downloading = downloadInfo.downloading.count() > 0 || downloadInfo.pending > 0; + + // Emit signal if downloading or have just finished. + if (downloading || _downloading) { + _downloading = downloading; + emit downloadInfoChanged(downloadInfo); + } +} + +void GlobalServicesScriptingInterface::updateDownloadInfo() { + emit downloadInfoChanged(getDownloadInfo()); +} diff --git a/interface/src/scripting/GlobalServicesScriptingInterface.h b/interface/src/scripting/GlobalServicesScriptingInterface.h index 2e189a9a69..657cb945c5 100644 --- a/interface/src/scripting/GlobalServicesScriptingInterface.h +++ b/interface/src/scripting/GlobalServicesScriptingInterface.h @@ -56,6 +56,7 @@ public: public slots: QScriptValue chat(const QString& message); DownloadInfoResult getDownloadInfo(); + void updateDownloadInfo(); private slots: void loggedOut(); @@ -64,6 +65,7 @@ private slots: #ifdef HAVE_QXMPP void messageReceived(const QXmppMessage& message); #endif // HAVE_QXMPP + void checkDownloadInfo(); signals: void connected(); @@ -71,6 +73,10 @@ signals: void incomingMessage(const QString& username, const QString& message); void onlineUsersChanged(const QStringList& usernames); void myUsernameChanged(const QString& username); + void downloadInfoChanged(DownloadInfoResult info); + +private: + bool _downloading; }; #endif // hifi_GlobalServicesScriptingInterface_h From 6c4932f9d0034c210a379d99cb8f631204650377 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Wed, 7 Jan 2015 11:24:10 -0800 Subject: [PATCH 3/3] Coding standard --- .../scripting/GlobalServicesScriptingInterface.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/interface/src/scripting/GlobalServicesScriptingInterface.cpp b/interface/src/scripting/GlobalServicesScriptingInterface.cpp index 94777acbc0..1cceec0e01 100644 --- a/interface/src/scripting/GlobalServicesScriptingInterface.cpp +++ b/interface/src/scripting/GlobalServicesScriptingInterface.cpp @@ -29,7 +29,8 @@ GlobalServicesScriptingInterface::GlobalServicesScriptingInterface() { #endif // HAVE_QXMPP _downloading = false; - connect(Application::getInstance(), &Application::renderingInWorldInterface, this, &GlobalServicesScriptingInterface::checkDownloadInfo); + connect(Application::getInstance(), &Application::renderingInWorldInterface, + this, &GlobalServicesScriptingInterface::checkDownloadInfo); } GlobalServicesScriptingInterface::~GlobalServicesScriptingInterface() { @@ -43,14 +44,16 @@ GlobalServicesScriptingInterface::~GlobalServicesScriptingInterface() { const QXmppClient& qxmppClient = XmppClient::getInstance().getXMPPClient(); disconnect(&qxmppClient, &QXmppClient::messageReceived, this, &GlobalServicesScriptingInterface::messageReceived); const QXmppMucRoom* publicChatRoom = XmppClient::getInstance().getPublicChatRoom(); - disconnect(publicChatRoom, &QXmppMucRoom::participantsChanged, this, &GlobalServicesScriptingInterface::participantsChanged); + disconnect(publicChatRoom, &QXmppMucRoom::participantsChanged, + this, &GlobalServicesScriptingInterface::participantsChanged); #endif // HAVE_QXMPP } void GlobalServicesScriptingInterface::onConnected() { #ifdef HAVE_QXMPP const QXmppMucRoom* publicChatRoom = XmppClient::getInstance().getPublicChatRoom(); - connect(publicChatRoom, &QXmppMucRoom::participantsChanged, this, &GlobalServicesScriptingInterface::participantsChanged, Qt::UniqueConnection); + connect(publicChatRoom, &QXmppMucRoom::participantsChanged, + this, &GlobalServicesScriptingInterface::participantsChanged, Qt::UniqueConnection); #endif // HAVE_QXMPP } @@ -115,7 +118,8 @@ void GlobalServicesScriptingInterface::messageReceived(const QXmppMessage& messa return; } const QXmppMucRoom* publicChatRoom = XmppClient::getInstance().getPublicChatRoom(); - emit GlobalServicesScriptingInterface::incomingMessage(message.from().right(message.from().count() - 1 - publicChatRoom->jid().count()), message.body()); + QString username = message.from().right(message.from().count() - 1 - publicChatRoom->jid().count()); + emit GlobalServicesScriptingInterface::incomingMessage(username, message.body()); } #endif // HAVE_QXMPP