Add downloads information changed event for JavaScript

This commit is contained in:
David Rowe 2015-01-07 11:20:46 -08:00
parent 8b0d859686
commit 40274df279
3 changed files with 84 additions and 19 deletions

View file

@ -11,15 +11,56 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // 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 ... // Get and log the current downloads info ...
var downloadInfo,
downloadInfoString;
downloadInfo = GlobalServices.getDownloadInfo(); downloadInfo = GlobalServices.getDownloadInfo();
downloadInfoString = "Downloads: "; print(formatInfo(downloadInfo));
if (downloadInfo.downloading.length > 0) {
downloadInfoString += downloadInfo.downloading.join("% ") + "% ";
// 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);

View file

@ -10,8 +10,9 @@
// //
#include "AccountManager.h" #include "AccountManager.h"
#include "XmppClient.h" #include "Application.h"
#include "ResourceCache.h" #include "ResourceCache.h"
#include "XmppClient.h"
#include "GlobalServicesScriptingInterface.h" #include "GlobalServicesScriptingInterface.h"
@ -26,6 +27,9 @@ GlobalServicesScriptingInterface::GlobalServicesScriptingInterface() {
const QXmppClient& qxmppClient = XmppClient::getInstance().getXMPPClient(); const QXmppClient& qxmppClient = XmppClient::getInstance().getXMPPClient();
connect(&qxmppClient, &QXmppClient::messageReceived, this, &GlobalServicesScriptingInterface::messageReceived); connect(&qxmppClient, &QXmppClient::messageReceived, this, &GlobalServicesScriptingInterface::messageReceived);
#endif // HAVE_QXMPP #endif // HAVE_QXMPP
_downloading = false;
connect(Application::getInstance(), &Application::renderingInWorldInterface, this, &GlobalServicesScriptingInterface::checkDownloadInfo);
} }
GlobalServicesScriptingInterface::~GlobalServicesScriptingInterface() { GlobalServicesScriptingInterface::~GlobalServicesScriptingInterface() {
@ -115,16 +119,6 @@ void GlobalServicesScriptingInterface::messageReceived(const QXmppMessage& messa
} }
#endif // HAVE_QXMPP #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() : DownloadInfoResult::DownloadInfoResult() :
downloading(QList<float>()), downloading(QList<float>()),
@ -154,3 +148,27 @@ void DownloadInfoResultFromScriptValue(const QScriptValue& object, DownloadInfoR
result.pending = object.property("pending").toVariant().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

@ -56,6 +56,7 @@ public:
public slots: public slots:
QScriptValue chat(const QString& message); QScriptValue chat(const QString& message);
DownloadInfoResult getDownloadInfo(); DownloadInfoResult getDownloadInfo();
void updateDownloadInfo();
private slots: private slots:
void loggedOut(); void loggedOut();
@ -64,6 +65,7 @@ private slots:
#ifdef HAVE_QXMPP #ifdef HAVE_QXMPP
void messageReceived(const QXmppMessage& message); void messageReceived(const QXmppMessage& message);
#endif // HAVE_QXMPP #endif // HAVE_QXMPP
void checkDownloadInfo();
signals: signals:
void connected(); void connected();
@ -71,6 +73,10 @@ signals:
void incomingMessage(const QString& username, const QString& message); void incomingMessage(const QString& username, const QString& message);
void onlineUsersChanged(const QStringList& usernames); void onlineUsersChanged(const QStringList& usernames);
void myUsernameChanged(const QString& username); void myUsernameChanged(const QString& username);
void downloadInfoChanged(DownloadInfoResult info);
private:
bool _downloading;
}; };
#endif // hifi_GlobalServicesScriptingInterface_h #endif // hifi_GlobalServicesScriptingInterface_h