Merge pull request #4091 from ctrlaltdavid/20229

CR for Job #20229 - Expose the downloads section for models and metavoxels to javascript
This commit is contained in:
Clément Brisset 2015-01-13 17:07:17 -08:00
commit 3034b8f733
4 changed files with 153 additions and 3 deletions

View file

@ -0,0 +1,66 @@
//
// 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
//
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 ...
downloadInfo = GlobalServices.getDownloadInfo();
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: ""
});
}
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);

View file

@ -3437,6 +3437,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

@ -10,6 +10,8 @@
//
#include "AccountManager.h"
#include "Application.h"
#include "ResourceCache.h"
#include "XmppClient.h"
#include "GlobalServicesScriptingInterface.h"
@ -25,6 +27,10 @@ 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() {
@ -38,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
}
@ -110,6 +118,61 @@ 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
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();
}
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());
}

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,8 @@ public:
public slots:
QScriptValue chat(const QString& message);
DownloadInfoResult getDownloadInfo();
void updateDownloadInfo();
private slots:
void loggedOut();
@ -50,6 +65,7 @@ private slots:
#ifdef HAVE_QXMPP
void messageReceived(const QXmppMessage& message);
#endif // HAVE_QXMPP
void checkDownloadInfo();
signals:
void connected();
@ -57,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