Add new AssetMappingsScriptingInterface and add mappings model

This commit is contained in:
Ryan Huffman 2016-03-09 13:55:33 -08:00
parent 64bd41c37f
commit f5497aa1e7
9 changed files with 298 additions and 552 deletions

View file

@ -72,6 +72,7 @@ Window {
}
function deleteFile() {
var path = scriptsModel.data(treeView.currentIndex, 0x100);
print(path);
if (!path) {
return;
}
@ -89,6 +90,10 @@ Window {
if (button === OriginalDialogs.StandardButton.Yes) {
console.log("Deleting " + path);
Assets.deleteMappings([path], function(err) {
print("Finished deleting path: ", path, err);
reload();
});
}

View file

@ -66,6 +66,7 @@
#include <BuildInfo.h>
#include <AssetClient.h>
#include <AssetUpload.h>
#include <AssetMappingsScriptingInterface.h>
#include <AutoUpdater.h>
#include <AudioInjectorManager.h>
#include <CursorManager.h>
@ -413,6 +414,7 @@ bool setupEssentials(int& argc, char** argv) {
DependencyManager::set<MessagesClient>();
DependencyManager::set<UserInputMapper>();
DependencyManager::set<controller::ScriptingInterface, ControllerScriptingInterface>();
DependencyManager::set<AssetMappingsScriptingInterface>();
DependencyManager::set<InterfaceParentFinder>();
DependencyManager::set<EntityTreeRenderer>(true, qApp, qApp);
DependencyManager::set<CompositorHelper>();
@ -1267,7 +1269,7 @@ void Application::initializeUi() {
rootContext->setContextProperty("Quat", new Quat());
rootContext->setContextProperty("Vec3", new Vec3());
rootContext->setContextProperty("Uuid", new ScriptUUID());
rootContext->setContextProperty("Assets", new AssetMappingsScriptingInterface(engine));
rootContext->setContextProperty("Assets", DependencyManager::get<AssetMappingsScriptingInterface>().data());
rootContext->setContextProperty("AvatarList", DependencyManager::get<AvatarManager>().data());
@ -4288,8 +4290,8 @@ bool Application::askToSetAvatarUrl(const QString& url) {
case FSTReader::HEAD_AND_BODY_MODEL:
ok = QMessageBox::Ok == OffscreenUi::question("Set Avatar",
"Would you like to use '" + modelName + "' for your avatar?",
QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok);
"Would you like to use '" + modelName + "' for your avatar?",
QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok);
break;
default:

View file

@ -31,150 +31,6 @@
MessageID AssetClient::_currentID = 0;
void MappingRequest::start() {
if (QThread::currentThread() != thread()) {
QMetaObject::invokeMethod(this, "start", Qt::AutoConnection);
return;
}
doStart();
};
GetMappingRequest::GetMappingRequest(const AssetPath& path) : _path(path) {
};
void GetMappingRequest::doStart() {
auto assetClient = DependencyManager::get<AssetClient>();
// Check cache
auto it = assetClient->_mappingCache.constFind(_path);
if (it != assetClient->_mappingCache.constEnd()) {
_hash = it.value();
emit finished(this);
return;
}
assetClient->getAssetMapping(_path, [this, assetClient](bool responseReceived, AssetServerError error, QSharedPointer<ReceivedMessage> message) {
if (!responseReceived) {
_error = NetworkError;
} else {
switch (error) {
case AssetServerError::NoError:
_error = NoError;
break;
case AssetServerError::AssetNotFound:
_error = NotFound;
break;
default:
_error = UnknownError;
break;
}
}
if (!_error) {
_hash = message->read(SHA256_HASH_LENGTH).toHex();
assetClient->_mappingCache[_path] = _hash;
}
emit finished(this);
});
};
GetAllMappingsRequest::GetAllMappingsRequest() {
};
void GetAllMappingsRequest::doStart() {
auto assetClient = DependencyManager::get<AssetClient>();
assetClient->getAllAssetMappings([this, assetClient](bool responseReceived, AssetServerError error, QSharedPointer<ReceivedMessage> message) {
if (!responseReceived) {
_error = NetworkError;
} else {
switch (error) {
case AssetServerError::NoError:
_error = NoError;
break;
default:
_error = UnknownError;
break;
}
}
if (!error) {
int numberOfMappings;
message->readPrimitive(&numberOfMappings);
assetClient->_mappingCache.clear();
for (auto i = 0; i < numberOfMappings; ++i) {
auto path = message->readString();
auto hash = message->read(SHA256_HASH_LENGTH).toHex();
_mappings[path] = hash;
assetClient->_mappingCache[path] = hash;
}
}
emit finished(this);
});
};
SetMappingRequest::SetMappingRequest(const AssetPath& path, const AssetHash& hash) : _path(path), _hash(hash) {
};
void SetMappingRequest::doStart() {
auto assetClient = DependencyManager::get<AssetClient>();
assetClient->setAssetMapping(_path, _hash, [this, assetClient](bool responseReceived, AssetServerError error, QSharedPointer<ReceivedMessage> message) {
if (!responseReceived) {
_error = NetworkError;
} else {
switch (error) {
case AssetServerError::NoError:
_error = NoError;
break;
case AssetServerError::PermissionDenied:
_error = PermissionDenied;
break;
default:
_error = UnknownError;
break;
}
}
if (!error) {
assetClient->_mappingCache[_path] = _hash;
}
emit finished(this);
});
};
DeleteMappingsRequest::DeleteMappingsRequest(const AssetPathList& paths) : _paths(paths) {
};
void DeleteMappingsRequest::doStart() {
auto assetClient = DependencyManager::get<AssetClient>();
assetClient->deleteAssetMappings(_paths, [this, assetClient](bool responseReceived, AssetServerError error, QSharedPointer<ReceivedMessage> message) {
if (!responseReceived) {
_error = NetworkError;
} else {
switch (error) {
case AssetServerError::NoError:
_error = NoError;
break;
case AssetServerError::PermissionDenied:
_error = PermissionDenied;
break;
default:
_error = UnknownError;
break;
}
}
if (!error) {
// enumerate the paths and remove them from the cache
for (auto& path : _paths) {
assetClient->_mappingCache.remove(path);
}
}
emit finished(this);
});
};
AssetClient::AssetClient() {
setCustomDeleter([](Dependency* dependency){
@ -725,252 +581,3 @@ void AssetClient::handleNodeKilled(SharedNodePointer node) {
_mappingCache.clear();
}
void AssetScriptingInterface::uploadData(QString data, QScriptValue callback) {
QByteArray dataByteArray = data.toUtf8();
auto upload = DependencyManager::get<AssetClient>()->createUpload(dataByteArray);
if (!upload) {
qCWarning(asset_client) << "Error uploading file to asset server";
return;
}
QObject::connect(upload, &AssetUpload::finished, this, [this, callback](AssetUpload* upload, const QString& hash) mutable {
if (callback.isFunction()) {
QString url = "atp://" + hash;
QScriptValueList args { url };
callback.call(_engine->currentContext()->thisObject(), args);
}
});
upload->start();
}
AssetScriptingInterface::AssetScriptingInterface(QScriptEngine* engine) :
_engine(engine)
{
}
void AssetScriptingInterface::downloadData(QString urlString, QScriptValue callback) {
const QString ATP_SCHEME { "atp://" };
if (!urlString.startsWith(ATP_SCHEME)) {
return;
}
// Make request to atp
auto path = urlString.right(urlString.length() - ATP_SCHEME.length());
auto parts = path.split(".", QString::SkipEmptyParts);
auto hash = parts.length() > 0 ? parts[0] : "";
if (hash.length() != SHA256_HASH_HEX_LENGTH) {
return;
}
auto assetClient = DependencyManager::get<AssetClient>();
auto assetRequest = assetClient->createRequest(hash);
if (!assetRequest) {
return;
}
_pendingRequests << assetRequest;
connect(assetRequest, &AssetRequest::finished, this, [this, callback](AssetRequest* request) mutable {
Q_ASSERT(request->getState() == AssetRequest::Finished);
if (request->getError() == AssetRequest::Error::NoError) {
if (callback.isFunction()) {
QString data = QString::fromUtf8(request->getData());
QScriptValueList args { data };
callback.call(_engine->currentContext()->thisObject(), args);
}
}
request->deleteLater();
_pendingRequests.remove(request);
});
assetRequest->start();
}
static int standardItemModelMetaTypeId = qRegisterMetaType<AssetMappingModel*>("AssetMappingModel*");
AssetMappingsScriptingInterface::AssetMappingsScriptingInterface(QJSEngine* engine) :
_engine(engine),
_assetMappingModel(this)
{
}
void AssetMappingsScriptingInterface::setMapping(QString path, QString hash, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createSetMappingRequest(path, hash);
connect(request, &SetMappingRequest::finished, this, [this, callback](SetMappingRequest* request) mutable {
QJSValueList args { uint8_t(request->getError()) };
callback.call(args);
request->deleteLater();
});
request->start();
}
void AssetMappingsScriptingInterface::getMapping(QString path, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetMappingRequest(path);
connect(request, &GetMappingRequest::finished, this, [this, callback](GetMappingRequest* request) mutable {
QJSValueList args { uint8_t(request->getError()), request->getHash() };
callback.call(args);
request->deleteLater();
});
request->start();
}
void AssetMappingsScriptingInterface::deleteMappings(QStringList paths, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createDeleteMappingsRequest(paths);
connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable {
QJSValueList args { uint8_t(request->getError()) };
callback.call(args);
request->deleteLater();
});
request->start();
}
void AssetMappingsScriptingInterface::getAllMappings(QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetAllMappingsRequest();
connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable {
auto mappings = request->getMappings();
auto map = callback.engine()->newObject();
for (auto& kv : mappings ) {
map.setProperty(kv.first, kv.second);
}
QJSValueList args { uint8_t(request->getError()), map };
callback.call(args);
request->deleteLater();
});
request->start();
}
AssetMappingItem::AssetMappingItem(const QString& name, const QString& fullPath, bool isFolder)
: name(name),
fullPath(fullPath),
isFolder(isFolder) {
}
AssetMappingModel::AssetMappingModel(QObject* parent) {
}
void AssetMappingModel::refresh() {
qDebug() << "Refreshing asset mapping model";
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetAllMappingsRequest();
connect(request, &GetAllMappingsRequest::finished, this, [this](GetAllMappingsRequest* request) mutable {
auto mappings = request->getMappings();
for (auto& mapping : mappings) {
auto& path = mapping.first;
auto parts = path.split("/");
auto length = parts.length();
QString prefix = parts[0];
QStandardItem* lastItem = nullptr;
auto it = _pathToItemMap.find(prefix);
if (it == _pathToItemMap.end()) {
lastItem = new QStandardItem(parts[0]);
lastItem->setData(parts[0], Qt::UserRole + 1);
lastItem->setData(prefix, Qt::UserRole + 2);
_pathToItemMap[prefix] = lastItem;
appendRow(lastItem);
} else {
lastItem = it.value();
}
if (length > 1) {
for (int i = 1; i < length; ++i) {
prefix += "/" + parts[i];
auto it = _pathToItemMap.find(prefix);
if (it == _pathToItemMap.end()) {
qDebug() << "prefix not found: " << prefix;
auto item = new QStandardItem(parts[i]);
item->setData(parts[i], Qt::UserRole + 1);
item->setData(prefix, Qt::UserRole + 2);
lastItem->setChild(lastItem->rowCount(), 0, item);
lastItem = item;
_pathToItemMap[prefix] = lastItem;
} else {
lastItem = it.value();
}
}
}
Q_ASSERT(prefix == path);
}
});
request->start();
}
// QModelIndex AssetMappingModel::index(int row, int column, const QModelIndex& parent) const {
// if (row < 0 || column < 0) {
// return QModelIndex();
// }
// if (parent.isValid()) {
// auto item = static_cast<AssetMappingItem*>(parent.internalPointer());
// return createIndex(row, column, )
// }
// return createIndex(row, column, getFolderNodes(
// static_cast<AssetMappingItem*>(getTreeNodeFromIndex(parent))).at(row));
// }
// QModelIndex AssetMappingModel::parent(const QModelIndex& child) const {
// AssetMappingItem* parent = (static_cast<AssetMappingItem*>(child.internalPointer()))->getParent();
// if (!parent) {
// return QModelIndex();
// }
// AssetMappingItem* grandParent = parent->getParent();
// int row = getFolderNodes(grandParent).indexOf(parent);
// return createIndex(row, 0, parent);
// }
// QVariant AssetMappingModel::data(const QModelIndex& index, int role) const {
// if (index.isValid()) {
// AssetMappingItem* item = (static_cast<AssetMappingItem*>(index.internalPointer()));
// if (item) {
// return item->name;
// }
// }
// return QVariant();
// }
//
// int AssetMappingModel::rowCount(const QModelIndex& parent) const {
// return 1;
// }
// int AssetMappingModel::columnCount(const QModelIndex& parent) const {
// return 1;
// }

View file

@ -108,62 +108,4 @@ private:
friend class RenameMappingRequest;
};
class AssetScriptingInterface : public QObject {
Q_OBJECT
public:
AssetScriptingInterface(QScriptEngine* engine);
Q_INVOKABLE void uploadData(QString data, QScriptValue callback);
Q_INVOKABLE void downloadData(QString url, QScriptValue downloadComplete);
// Q_INVOKABLE void setMapping(QString path, QString hash, QScriptValue callback);
// Q_INVOKABLE void getMapping(QString path, QScriptValue callback);
// Q_INVOKABLE void deleteMappings(QStringList paths, QScriptValue callback);
// Q_INVOKABLE void getAllMappings(QScriptValue callback);
protected:
QSet<AssetRequest*> _pendingRequests;
QScriptEngine* _engine;
};
class AssetMappingItem : public QStandardItem {
public:
AssetMappingItem(const QString& name, const QString& fullPath, bool isFolder);
QString name;
QString fullPath;
bool isFolder;
};
class AssetMappingModel : public QStandardItemModel {
Q_OBJECT
public:
AssetMappingModel(QObject* parent = nullptr);
QVariant AssetMappingModel::data(const QModelIndex& index, int role) const;
Q_INVOKABLE void refresh();
private:
QHash<QString, AssetMappingItem*> _pathToItemMap;
};
class AssetMappingsScriptingInterface : public QObject {
Q_OBJECT
Q_PROPERTY(AssetMappingModel* mappingModel READ getAssetMappingModel CONSTANT)
public:
AssetMappingsScriptingInterface(QJSEngine* engine);
Q_INVOKABLE AssetMappingModel* getAssetMappingModel() { return &_assetMappingModel; };
Q_INVOKABLE void setMapping(QString path, QString hash, QJSValue callback);
Q_INVOKABLE void getMapping(QString path, QJSValue callback);
Q_INVOKABLE void deleteMappings(QStringList paths, QJSValue callback);
Q_INVOKABLE void getAllMappings(QJSValue callback);
protected:
QSet<AssetRequest*> _pendingRequests;
QJSEngine* _engine;
AssetMappingModel _assetMappingModel;
};
#endif

View file

@ -50,7 +50,7 @@ void GetMappingRequest::doStart() {
break;
case AssetServerError::AssetNotFound:
_error = NotFound;
break;
break
default:
_error = UnknownError;
break;
@ -85,13 +85,13 @@ void GetAllMappingsRequest::doStart() {
}
if (!error) {
if (!_error) {
int numberOfMappings;
message->readPrimitive(&numberOfMappings);
assetClient->_mappingCache.clear();
for (auto i = 0; i < numberOfMappings; ++i) {
auto path = message->readString();
auto hash = message->readString();
auto hash = message->read(SHA256_HASH_LENGTH).toHex();
_mappings[path] = hash;
assetClient->_mappingCache[path] = hash;
}
@ -122,7 +122,7 @@ void SetMappingRequest::doStart() {
}
}
if (!error) {
if (!_error) {
assetClient->_mappingCache[_path] = _hash;
}
emit finished(this);
@ -151,7 +151,7 @@ void DeleteMappingsRequest::doStart() {
}
}
if (!error) {
if (!_error) {
// enumerate the paths and remove them from the cache
for (auto& path : _paths) {
assetClient->_mappingCache.remove(path);
@ -189,7 +189,7 @@ void RenameMappingRequest::doStart() {
}
}
if (!error) {
if (!_error) {
// take the hash mapped for the old path from the cache
auto hash = assetClient->_mappingCache.take(_oldPath);
if (!hash.isEmpty()) {

View file

@ -0,0 +1,218 @@
//
// AssetMappingsScriptingInterface.cpp
// libraries/script-engine/src
//
// Created by Ryan Huffman on 2016-03-09.
// Copyright 2016 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "AssetMappingsScriptingInterface.h"
#include <QtScript/QScriptEngine>
#include <AssetRequest.h>
#include <AssetUpload.h>
#include <MappingRequest.h>
#include <NetworkLogging.h>
AssetMappingsScriptingInterface::AssetMappingsScriptingInterface() {
}
void AssetMappingsScriptingInterface::setMapping(QString path, QString hash, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createSetMappingRequest(path, hash);
connect(request, &SetMappingRequest::finished, this, [this, callback](SetMappingRequest* request) mutable {
QJSValueList args { uint8_t(request->getError()) };
callback.call(args);
request->deleteLater();
});
request->start();
}
void AssetMappingsScriptingInterface::getMapping(QString path, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetMappingRequest(path);
connect(request, &GetMappingRequest::finished, this, [this, callback](GetMappingRequest* request) mutable {
QJSValueList args { uint8_t(request->getError()), request->getHash() };
callback.call(args);
request->deleteLater();
});
request->start();
}
void AssetMappingsScriptingInterface::deleteMappings(QStringList paths, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createDeleteMappingsRequest(paths);
connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable {
QJSValueList args { uint8_t(request->getError()) };
callback.call(args);
request->deleteLater();
});
request->start();
}
void AssetMappingsScriptingInterface::getAllMappings(QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetAllMappingsRequest();
connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable {
auto mappings = request->getMappings();
auto map = callback.engine()->newObject();
for (auto& kv : mappings ) {
map.setProperty(kv.first, kv.second);
}
QJSValueList args { uint8_t(request->getError()), map };
callback.call(args);
request->deleteLater();
});
request->start();
}
void AssetMappingsScriptingInterface::renameMapping(QString oldPath, QString newPath, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createRenameMappingRequest(oldPath, newPath);
connect(request, &RenameMappingRequest::finished, this, [this, callback](RenameMappingRequest* request) mutable {
QJSValueList args { uint8_t(request->getError()) };
callback.call(args);
request->deleteLater();
});
request->start();
}
AssetMappingItem::AssetMappingItem(const QString& name, const QString& fullPath, bool isFolder)
: name(name),
fullPath(fullPath),
isFolder(isFolder) {
}
static int assetMappingModelMetatypeId = qRegisterMetaType<AssetMappingModel*>("AssetMappingModel*");
AssetMappingModel::AssetMappingModel(QObject* parent) {
}
void AssetMappingModel::refresh() {
qDebug() << "Refreshing asset mapping model";
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetAllMappingsRequest();
connect(request, &GetAllMappingsRequest::finished, this, [this](GetAllMappingsRequest* request) mutable {
auto mappings = request->getMappings();
for (auto& mapping : mappings) {
auto& path = mapping.first;
auto parts = path.split("/");
auto length = parts.length();
QString fullPath = parts[0];
QStandardItem* lastItem = nullptr;
auto it = _pathToItemMap.find(fullPath);
if (it == _pathToItemMap.end()) {
lastItem = new QStandardItem(parts[0]);
lastItem->setData(parts[0], Qt::UserRole + 1);
lastItem->setData(fullPath, Qt::UserRole + 2);
_pathToItemMap[fullPath] = lastItem;
appendRow(lastItem);
}
else {
lastItem = it.value();
}
if (length > 1) {
for (int i = 1; i < length; ++i) {
fullPath += "/" + parts[i];
auto it = _pathToItemMap.find(fullPath);
if (it == _pathToItemMap.end()) {
qDebug() << "prefix not found: " << fullPath;
auto item = new QStandardItem(parts[i]);
bool isFolder = i < length - 1;
item->setData(isFolder ? fullPath + "/" : fullPath, Qt::UserRole);
lastItem->setChild(lastItem->rowCount(), 0, item);
lastItem = item;
_pathToItemMap[fullPath] = lastItem;
}
else {
lastItem = it.value();
}
}
}
Q_ASSERT(fullPath == path);
}
});
request->start();
}
// QModelIndex AssetMappingModel::index(int row, int column, const QModelIndex& parent) const {
// if (row < 0 || column < 0) {
// return QModelIndex();
// }
// if (parent.isValid()) {
// auto item = static_cast<AssetMappingItem*>(parent.internalPointer());
// return createIndex(row, column, )
// }
// return createIndex(row, column, getFolderNodes(
// static_cast<AssetMappingItem*>(getTreeNodeFromIndex(parent))).at(row));
// }
// QModelIndex AssetMappingModel::parent(const QModelIndex& child) const {
// AssetMappingItem* parent = (static_cast<AssetMappingItem*>(child.internalPointer()))->getParent();
// if (!parent) {
// return QModelIndex();
// }
// AssetMappingItem* grandParent = parent->getParent();
// int row = getFolderNodes(grandParent).indexOf(parent);
// return createIndex(row, 0, parent);
// }
// QVariant AssetMappingModel::data(const QModelIndex& index, int role) const {
// if (index.isValid()) {
// AssetMappingItem* item = (static_cast<AssetMappingItem*>(index.internalPointer()));
// if (item) {
// return item->name;
// }
// }
// return QVariant();
// }
//
// int AssetMappingModel::rowCount(const QModelIndex& parent) const {
// return 1;
// }
// int AssetMappingModel::columnCount(const QModelIndex& parent) const {
// return 1;
// }

View file

@ -0,0 +1,64 @@
//
// AssetScriptingInterface.h
// libraries/script-engine/src
//
// Created by Ryan Huffman on 2016-03-09.
// Copyright 2016 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#pragma once
#ifndef hifi_AssetMappingsScriptingInterface_h
#define hifi_AssetMappingsScriptingInterface_h
#include <QtCore/QObject>
#include <QtScript/QScriptValue>
#include <AssetClient.h>
class AssetMappingItem : public QStandardItem {
public:
AssetMappingItem(const QString& name, const QString& fullPath, bool isFolder);
QString name;
QString fullPath;
bool isFolder;
};
class AssetMappingModel : public QStandardItemModel {
Q_OBJECT
public:
AssetMappingModel(QObject* parent = nullptr);
// QVariant AssetMappingModel::data(const QModelIndex& index, int role) const;
Q_INVOKABLE void refresh();
private:
QHash<QString, QStandardItem*> _pathToItemMap;
};
class AssetMappingsScriptingInterface : public QObject, public Dependency {
Q_OBJECT
public:
AssetMappingsScriptingInterface();
Q_INVOKABLE AssetMappingModel* getAssetMappingModel() { return &_assetMappingModel; }
Q_INVOKABLE void setMapping(QString path, QString hash, QJSValue callback);
Q_INVOKABLE void getMapping(QString path, QJSValue callback);
Q_INVOKABLE void deleteMappings(QStringList paths, QJSValue callback);
Q_INVOKABLE void getAllMappings(QJSValue callback);
Q_INVOKABLE void renameMapping(QString oldPath, QString newPath, QJSValue callback);
protected:
QSet<AssetRequest*> _pendingRequests;
AssetMappingModel _assetMappingModel;
};
#endif // hifi_AssetMappingsScriptingInterface_h

View file

@ -84,90 +84,3 @@ void AssetScriptingInterface::downloadData(QString urlString, QScriptValue callb
assetRequest->start();
}
void AssetScriptingInterface::setMapping(QString path, QString hash, QScriptValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createSetMappingRequest(path, hash);
connect(request, &SetMappingRequest::finished, this, [this, callback](SetMappingRequest* request) mutable {
QScriptValueList args { uint8_t(request->getError()) };
callback.call(_engine->currentContext()->thisObject(), args);
request->deleteLater();
});
request->start();
}
void AssetScriptingInterface::getMapping(QString path, QScriptValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetMappingRequest(path);
connect(request, &GetMappingRequest::finished, this, [this, callback](GetMappingRequest* request) mutable {
QScriptValueList args { uint8_t(request->getError()), request->getHash() };
callback.call(_engine->currentContext()->thisObject(), args);
request->deleteLater();
});
request->start();
}
void AssetScriptingInterface::deleteMappings(QStringList paths, QScriptValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createDeleteMappingsRequest(paths);
connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable {
QScriptValueList args { uint8_t(request->getError()) };
callback.call(_engine->currentContext()->thisObject(), args);
request->deleteLater();
});
request->start();
}
void AssetScriptingInterface::getAllMappings(QScriptValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetAllMappingsRequest();
connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable {
auto mappings = request->getMappings();
auto map = callback.engine()->newObject();
for (auto& kv : mappings ) {
map.setProperty(kv.first, kv.second);
}
QScriptValueList args { uint8_t(request->getError()), map };
callback.call(_engine->currentContext()->thisObject(), args);
request->deleteLater();
});
request->start();
}
void AssetScriptingInterface::renameMapping(QString oldPath, QString newPath, QScriptValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createRenameMappingRequest(oldPath, newPath);
connect(request, &RenameMappingRequest::finished, this, [this, callback](RenameMappingRequest* request) mutable {
QScriptValueList args { uint8_t(request->getError()) };
callback.call(_engine->currentContext()->thisObject(), args);
request->deleteLater();
});
request->start();
}

View file

@ -26,11 +26,6 @@ public:
Q_INVOKABLE void uploadData(QString data, QScriptValue callback);
Q_INVOKABLE void downloadData(QString url, QScriptValue downloadComplete);
Q_INVOKABLE void setMapping(QString path, QString hash, QScriptValue callback);
Q_INVOKABLE void getMapping(QString path, QScriptValue callback);
Q_INVOKABLE void deleteMappings(QStringList paths, QScriptValue callback);
Q_INVOKABLE void getAllMappings(QScriptValue callback);
Q_INVOKABLE void renameMapping(QString oldPath, QString newPath, QScriptValue callback);
protected:
QSet<AssetRequest*> _pendingRequests;
QScriptEngine* _engine;