History

Add method to access downloads information in JavaScript

David Rowe committed 11 years ago

Commit 8b0d8591 parent 98079fa4 files · +81 −0Browse files

Showing the changes to interface/src/scripting/GlobalServicesScriptingInterface.cpp (1 of 4 files). Show all

Unified · Split

interface/src/scripting/GlobalServicesScriptingInterface.cpp+41−0
@@ -11,6 +11,7 @@ · 10 unmodified lines
11 11
12#include "AccountManager.h"12#include "AccountManager.h"
13#include "XmppClient.h"13#include "XmppClient.h"
14#include "ResourceCache.h"
14 15
15#include "GlobalServicesScriptingInterface.h"16#include "GlobalServicesScriptingInterface.h"
16 17
@@ -113,3 +114,43 @@ void GlobalServicesScriptingInterface::messageReceived(const QXmppMessage& messa · 96 unmodified lines
113 emit GlobalServicesScriptingInterface::incomingMessage(message.from().right(message.from().count() - 1 - publicChatRoom->jid().count()), message.body());114 emit GlobalServicesScriptingInterface::incomingMessage(message.from().right(message.from().count() - 1 - publicChatRoom->jid().count()), message.body());
114}115}
115#endif // HAVE_QXMPP116#endif // HAVE_QXMPP
117
118DownloadInfoResult GlobalServicesScriptingInterface::getDownloadInfo() {
119 DownloadInfoResult result;
120 foreach(Resource* resource, ResourceCache::getLoadingRequests()) {
121 result.downloading.append(resource->getProgress() * 100.0f);
122 }
123 result.pending = ResourceCache::getPendingRequestCount();
124
125 return result;
126}
127
128
129DownloadInfoResult::DownloadInfoResult() :
130 downloading(QList<float>()),
131 pending(0.0f)
132{
133}
134
135QScriptValue DownloadInfoResultToScriptValue(QScriptEngine* engine, const DownloadInfoResult& result) {
136 QScriptValue object = engine->newObject();
137
138 QScriptValue array = engine->newArray(result.downloading.count());
139 for (int i = 0; i < result.downloading.count(); i += 1) {
140 array.setProperty(i, result.downloading[i]);
141 }
142
143 object.setProperty("downloading", array);
144 object.setProperty("pending", result.pending);
145 return object;
146}
147
148void DownloadInfoResultFromScriptValue(const QScriptValue& object, DownloadInfoResult& result) {
149 QList<QVariant> downloading = object.property("downloading").toVariant().toList();
150 result.downloading.clear();
151 for (int i = 0; i < downloading.count(); i += 1) {
152 result.downloading.append(downloading[i].toFloat());
153 }
154
155 result.pending = object.property("pending").toVariant().toFloat();
156}