Add method to access downloads information in JavaScript

This commit is contained in:
David Rowe 2015-01-06 15:54:07 -08:00
parent 98079faafd
commit 8b0d859686
4 changed files with 81 additions and 0 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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<float>()),
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<QVariant> 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();
}

View file

@ -26,6 +26,19 @@
#endif // HAVE_QXMPP
class DownloadInfoResult {
public:
DownloadInfoResult();
QList<float> 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();