Showing the changes to interface/src/scripting/AssetMappingsScriptingInterface.cpp (1 of 6 files). Show all
Unified · Split
| @@ -0,0 +1,288 @@ | ||
| 1 | +// | |
| 2 | +// AssetMappingsScriptingInterface.cpp | |
| 3 | +// libraries/script-engine/src | |
| 4 | +// | |
| 5 | +// Created by Ryan Huffman on 2016-03-09. | |
| 6 | +// Copyright 2016 High Fidelity, Inc. | |
| 7 | +// | |
| 8 | +// Distributed under the Apache License, Version 2.0. | |
| 9 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 10 | +// | |
| 11 | + | |
| 12 | +#include "AssetMappingsScriptingInterface.h" | |
| 13 | + | |
| 14 | +#include <QtScript/QScriptEngine> | |
| 15 | +#include <QtCore/QFile> | |
| 16 | + | |
| 17 | +#include <AssetRequest.h> | |
| 18 | +#include <AssetUpload.h> | |
| 19 | +#include <MappingRequest.h> | |
| 20 | +#include <NetworkLogging.h> | |
| 21 | +#include <OffscreenUi.h> | |
| 22 | + | |
| 23 | +AssetMappingsScriptingInterface::AssetMappingsScriptingInterface() { | |
| 24 | + _proxyModel.setSourceModel(&_assetMappingModel); | |
| 25 | + _proxyModel.setSortRole(Qt::DisplayRole); | |
| 26 | + _proxyModel.setDynamicSortFilter(true); | |
| 27 | + _proxyModel.sort(0); | |
| 28 | +} | |
| 29 | + | |
| 30 | +QString AssetMappingsScriptingInterface::getErrorString(int errorCode) const { | |
| 31 | + switch (errorCode) { | |
| 32 | + case MappingRequest::NoError: | |
| 33 | + return "No error"; | |
| 34 | + case MappingRequest::NotFound: | |
| 35 | + return "Asset not found"; | |
| 36 | + case MappingRequest::NetworkError: | |
| 37 | + return "Unable to communicate with Asset Server"; | |
| 38 | + case MappingRequest::PermissionDenied: | |
| 39 | + return "Permission denied"; | |
| 40 | + case MappingRequest::InvalidPath: | |
| 41 | + return "Path is invalid"; | |
| 42 | + case MappingRequest::InvalidHash: | |
| 43 | + return "Hash is invalid"; | |
| 44 | + case MappingRequest::UnknownError: | |
| 45 | + return "Asset Server internal error"; | |
| 46 | + default: | |
| 47 | + return QString(); | |
| 48 | + } | |
| 49 | +} | |
| 50 | + | |
| 51 | +void AssetMappingsScriptingInterface::setMapping(QString path, QString hash, QJSValue callback) { | |
| 52 | + auto assetClient = DependencyManager::get<AssetClient>(); | |
| 53 | + auto request = assetClient->createSetMappingRequest(path, hash); | |
| 54 | + | |
| 55 | + connect(request, &SetMappingRequest::finished, this, [this, callback](SetMappingRequest* request) mutable { | |
| 56 | + if (callback.isCallable()) { | |
| 57 | + QJSValueList args { uint8_t(request->getError()) }; | |
| 58 | + callback.call(args); | |
| 59 | + } | |
| 60 | + | |
| 61 | + request->deleteLater(); | |
| 62 | + }); | |
| 63 | + | |
| 64 | + request->start(); | |
| 65 | +} | |
| 66 | + | |
| 67 | +void AssetMappingsScriptingInterface::getMapping(QString path, QJSValue callback) { | |
| 68 | + auto assetClient = DependencyManager::get<AssetClient>(); | |
| 69 | + auto request = assetClient->createGetMappingRequest(path); | |
| 70 | + | |
| 71 | + connect(request, &GetMappingRequest::finished, this, [this, callback](GetMappingRequest* request) mutable { | |
| 72 | + if (callback.isCallable()) { | |
| 73 | + QJSValueList args { uint8_t(request->getError()) }; | |
| 74 | + callback.call(args); | |
| 75 | + } | |
| 76 | + | |
| 77 | + request->deleteLater(); | |
| 78 | + }); | |
| 79 | + | |
| 80 | + request->start(); | |
| 81 | +} | |
| 82 | + | |
| 83 | +void AssetMappingsScriptingInterface::uploadFile(QString path, QString mapping, QJSValue callback) { | |
| 84 | + QFile file(path); | |
| 85 | + if (!file.open(QFile::ReadOnly)) { | |
| 86 | + qCWarning(asset_client) << "Error uploading file to asset server:\n" | |
| 87 | + << "Could not open" << qPrintable(path); | |
| 88 | + OffscreenUi::warning("File Error", "Could not open file: " + path, QMessageBox::Ok); | |
| 89 | + return; | |
| 90 | + } | |
| 91 | + | |
| 92 | + auto offscreenUi = DependencyManager::get<OffscreenUi>(); | |
| 93 | + auto result = offscreenUi->inputDialog(OffscreenUi::ICON_NONE, "Enter asset path:", "", mapping); | |
| 94 | + if (!result.isValid()) { | |
| 95 | + return; | |
| 96 | + } | |
| 97 | + | |
| 98 | + // Check for override | |
| 99 | + if (isKnownMapping(result.toString())) { | |
| 100 | + auto message = "The following file already exists:\n" + path + "\nDo you want to override it?"; | |
| 101 | + auto button = offscreenUi->messageBox(OffscreenUi::ICON_QUESTION, "", message, | |
| 102 | + QMessageBox::Yes | QMessageBox::No, QMessageBox::No); | |
| 103 | + if (button == QMessageBox::No) { | |
| 104 | + return; | |
| 105 | + } | |
| 106 | + } | |
| 107 | + | |
| 108 | + auto upload = DependencyManager::get<AssetClient>()->createUpload(file.readAll()); | |
| 109 | + QObject::connect(upload, &AssetUpload::finished, this, [=](AssetUpload* upload, const QString& hash) mutable { | |
| 110 | + if (upload->getError() != AssetUpload::NoError) { | |
| 111 | + if (callback.isCallable()) { | |
| 112 | + QJSValueList args { uint8_t(upload->getError()) }; | |
| 113 | + callback.call(args); | |
| 114 | + } | |
| 115 | + } else { | |
| 116 | + setMapping(mapping, hash, callback); | |
| 117 | + } | |
| 118 | + | |
| 119 | + upload->deleteLater(); | |
| 120 | + }); | |
| 121 | + | |
| 122 | + upload->start(); | |
| 123 | +} | |
| 124 | + | |
| 125 | +void AssetMappingsScriptingInterface::deleteMappings(QStringList paths, QJSValue callback) { | |
| 126 | + auto assetClient = DependencyManager::get<AssetClient>(); | |
| 127 | + auto request = assetClient->createDeleteMappingsRequest(paths); | |
| 128 | + | |
| 129 | + connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable { | |
| 130 | + if (callback.isCallable()) { | |
| 131 | + QJSValueList args { uint8_t(request->getError()) }; | |
| 132 | + callback.call(args); | |
| 133 | + } | |
| 134 | + | |
| 135 | + request->deleteLater(); | |
| 136 | + }); | |
| 137 | + | |
| 138 | + request->start(); | |
| 139 | +} | |
| 140 | + | |
| 141 | +void AssetMappingsScriptingInterface::getAllMappings(QJSValue callback) { | |
| 142 | + auto assetClient = DependencyManager::get<AssetClient>(); | |
| 143 | + auto request = assetClient->createGetAllMappingsRequest(); | |
| 144 | + | |
| 145 | + connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable { | |
| 146 | + auto mappings = request->getMappings(); | |
| 147 | + auto map = callback.engine()->newObject(); | |
| 148 | + | |
| 149 | + for (auto& kv : mappings ) { | |
| 150 | + map.setProperty(kv.first, kv.second); | |
| 151 | + } | |
| 152 | + | |
| 153 | + if (callback.isCallable()) { | |
| 154 | + QJSValueList args { uint8_t(request->getError()) }; | |
| 155 | + callback.call(args); | |
| 156 | + } | |
| 157 | + | |
| 158 | + request->deleteLater(); | |
| 159 | + }); | |
| 160 | + | |
| 161 | + request->start(); | |
| 162 | +} | |
| 163 | + | |
| 164 | +void AssetMappingsScriptingInterface::renameMapping(QString oldPath, QString newPath, QJSValue callback) { | |
| 165 | + auto assetClient = DependencyManager::get<AssetClient>(); | |
| 166 | + auto request = assetClient->createRenameMappingRequest(oldPath, newPath); | |
| 167 | + | |
| 168 | + connect(request, &RenameMappingRequest::finished, this, [this, callback](RenameMappingRequest* request) mutable { | |
| 169 | + if (callback.isCallable()) { | |
| 170 | + QJSValueList args { uint8_t(request->getError()) }; | |
| 171 | + callback.call(args); | |
| 172 | + } | |
| 173 | + | |
| 174 | + request->deleteLater(); | |
| 175 | + }); | |
| 176 | + | |
| 177 | + request->start(); | |
| 178 | +} | |
| 179 | + | |
| 180 | + | |
| 181 | +AssetMappingItem::AssetMappingItem(const QString& name, const QString& fullPath, bool isFolder) : | |
| 182 | + name(name), | |
| 183 | + fullPath(fullPath), | |
| 184 | + isFolder(isFolder) | |
| 185 | +{ | |
| 186 | + | |
| 187 | +} | |
| 188 | + | |
| 189 | +static int assetMappingModelMetatypeId = qRegisterMetaType<AssetMappingModel*>("AssetMappingModel*"); | |
| 190 | + | |
| 191 | +void AssetMappingModel::refresh() { | |
| 192 | + qDebug() << "Refreshing asset mapping model"; | |
| 193 | + auto assetClient = DependencyManager::get<AssetClient>(); | |
| 194 | + auto request = assetClient->createGetAllMappingsRequest(); | |
| 195 | + | |
| 196 | + connect(request, &GetAllMappingsRequest::finished, this, [this](GetAllMappingsRequest* request) mutable { | |
| 197 | + if (request->getError() == MappingRequest::NoError) { | |
| 198 | + auto mappings = request->getMappings(); | |
| 199 | + auto existingPaths = _pathToItemMap.keys(); | |
| 200 | + for (auto& mapping : mappings) { | |
| 201 | + auto& path = mapping.first; | |
| 202 | + auto parts = path.split("/"); | |
| 203 | + auto length = parts.length(); | |
| 204 | + | |
| 205 | + existingPaths.removeOne(mapping.first); | |
| 206 | + | |
| 207 | + QString fullPath = "/"; | |
| 208 | + | |
| 209 | + QStandardItem* lastItem = nullptr; | |
| 210 | + | |
| 211 | + // start index at 1 to avoid empty string from leading slash | |
| 212 | + for (int i = 1; i < length; ++i) { | |
| 213 | + fullPath += (i == 1 ? "" : "/") + parts[i]; | |
| 214 | + | |
| 215 | + auto it = _pathToItemMap.find(fullPath); | |
| 216 | + if (it == _pathToItemMap.end()) { | |
| 217 | + qDebug() << "prefix not found: " << fullPath; | |
| 218 | + auto item = new QStandardItem(parts[i]); | |
| 219 | + bool isFolder = i < length - 1; | |
| 220 | + item->setData(isFolder ? fullPath + "/" : fullPath, Qt::UserRole); | |
| 221 | + item->setData(isFolder, Qt::UserRole + 1); | |
| 222 | + item->setData(parts[i], Qt::UserRole + 2); | |
| 223 | + item->setData("atp:" + fullPath, Qt::UserRole + 3); | |
| 224 | + if (lastItem) { | |
| 225 | + lastItem->setChild(lastItem->rowCount(), 0, item); | |
| 226 | + } else { | |
| 227 | + appendRow(item); | |
| 228 | + } | |
| 229 | + | |
| 230 | + lastItem = item; | |
| 231 | + _pathToItemMap[fullPath] = lastItem; | |
| 232 | + } | |
| 233 | + else { | |
| 234 | + lastItem = it.value(); | |
| 235 | + } | |
| 236 | + } | |
| 237 | + | |
| 238 | + Q_ASSERT(fullPath == path); | |
| 239 | + } | |
| 240 | + | |
| 241 | + // Remove folders from list | |
| 242 | + auto it = existingPaths.begin(); | |
| 243 | + while (it != existingPaths.end()) { | |
| 244 | + auto item = _pathToItemMap[*it]; | |
| 245 | + if (item->data(Qt::UserRole + 1).toBool()) { | |
| 246 | + it = existingPaths.erase(it); | |
| 247 | + } else { | |
| 248 | + ++it; | |
| 249 | + } | |
| 250 | + } | |
| 251 | + | |
| 252 | + for (auto& path : existingPaths) { | |
| 253 | + Q_ASSERT(_pathToItemMap.contains(path)); | |
| 254 | + qDebug() << "removing existing: " << path; | |
| 255 | + | |
| 256 | + auto item = _pathToItemMap[path]; | |
| 257 | + | |
| 258 | + while (item) { | |
| 259 | + // During each iteration, delete item | |
| 260 | + QStandardItem* nextItem = nullptr; | |
| 261 | + | |
| 262 | + auto parent = item->parent(); | |
| 263 | + if (parent) { | |
| 264 | + parent->removeRow(item->row()); | |
| 265 | + if (parent->rowCount() > 0) { | |
| 266 | + // The parent still contains children, set the nextItem to null so we stop processing | |
| 267 | + nextItem = nullptr; | |
| 268 | + } else { | |
| 269 | + nextItem = parent; | |
| 270 | + } | |
| 271 | + } else { | |
| 272 | + removeRow(item->row()); | |
| 273 | + } | |
| 274 | + | |
| 275 | + _pathToItemMap.remove(path); | |
| 276 | + //delete item; | |
| 277 | + | |
| 278 | + item = nextItem; | |
| 279 | + } | |
| 280 | + //removeitem->index(); | |
| 281 | + } | |
| 282 | + } else { | |
| 283 | + emit errorGettingMappings(static_cast<int>(request->getError())); | |
| 284 | + } | |
| 285 | + }); | |
| 286 | + | |
| 287 | + request->start(); | |
| 288 | +} | |