Showing the changes to interface/src/scripting/AssetMappingsScriptingInterface.cpp (1 of 13 files). Show all
Unified · Split
| @@ -0,0 +1,283 @@ | |||
| 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 | auto offscreenUi = DependencyManager::get<OffscreenUi>(); | ||
| 85 | auto result = offscreenUi->inputDialog(OffscreenUi::ICON_NONE, "Enter asset path:", "", mapping); | ||
| 86 | |||
| 87 | if (!result.isValid()) { | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | mapping = result.toString(); | ||
| 91 | |||
| 92 | // Check for override | ||
| 93 | if (isKnownMapping(mapping)) { | ||
| 94 | auto message = path + "\n" + "This file already exists. Do you want to overwrite it?"; | ||
| 95 | auto button = offscreenUi->messageBox(OffscreenUi::ICON_QUESTION, "Overwrite File", message, | ||
| 96 | QMessageBox::Yes | QMessageBox::No, QMessageBox::No); | ||
| 97 | if (button == QMessageBox::No) { | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | auto upload = DependencyManager::get<AssetClient>()->createUpload(path); | ||
| 103 | QObject::connect(upload, &AssetUpload::finished, this, [=](AssetUpload* upload, const QString& hash) mutable { | ||
| 104 | if (upload->getError() != AssetUpload::NoError) { | ||
| 105 | if (callback.isCallable()) { | ||
| 106 | QJSValueList args { uint8_t(upload->getError()) }; | ||
| 107 | callback.call(args); | ||
| 108 | } | ||
| 109 | } else { | ||
| 110 | setMapping(mapping, hash, callback); | ||
| 111 | } | ||
| 112 | |||
| 113 | upload->deleteLater(); | ||
| 114 | }); | ||
| 115 | |||
| 116 | upload->start(); | ||
| 117 | } | ||
| 118 | |||
| 119 | void AssetMappingsScriptingInterface::deleteMappings(QStringList paths, QJSValue callback) { | ||
| 120 | auto assetClient = DependencyManager::get<AssetClient>(); | ||
| 121 | auto request = assetClient->createDeleteMappingsRequest(paths); | ||
| 122 | |||
| 123 | connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable { | ||
| 124 | if (callback.isCallable()) { | ||
| 125 | QJSValueList args { uint8_t(request->getError()) }; | ||
| 126 | callback.call(args); | ||
| 127 | } | ||
| 128 | |||
| 129 | request->deleteLater(); | ||
| 130 | }); | ||
| 131 | |||
| 132 | request->start(); | ||
| 133 | } | ||
| 134 | |||
| 135 | void AssetMappingsScriptingInterface::getAllMappings(QJSValue callback) { | ||
| 136 | auto assetClient = DependencyManager::get<AssetClient>(); | ||
| 137 | auto request = assetClient->createGetAllMappingsRequest(); | ||
| 138 | |||
| 139 | connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable { | ||
| 140 | auto mappings = request->getMappings(); | ||
| 141 | auto map = callback.engine()->newObject(); | ||
| 142 | |||
| 143 | for (auto& kv : mappings ) { | ||
| 144 | map.setProperty(kv.first, kv.second); | ||
| 145 | } | ||
| 146 | |||
| 147 | if (callback.isCallable()) { | ||
| 148 | QJSValueList args { uint8_t(request->getError()) }; | ||
| 149 | callback.call(args); | ||
| 150 | } | ||
| 151 | |||
| 152 | request->deleteLater(); | ||
| 153 | }); | ||
| 154 | |||
| 155 | request->start(); | ||
| 156 | } | ||
| 157 | |||
| 158 | void AssetMappingsScriptingInterface::renameMapping(QString oldPath, QString newPath, QJSValue callback) { | ||
| 159 | auto assetClient = DependencyManager::get<AssetClient>(); | ||
| 160 | auto request = assetClient->createRenameMappingRequest(oldPath, newPath); | ||
| 161 | |||
| 162 | connect(request, &RenameMappingRequest::finished, this, [this, callback](RenameMappingRequest* request) mutable { | ||
| 163 | if (callback.isCallable()) { | ||
| 164 | QJSValueList args { uint8_t(request->getError()) }; | ||
| 165 | callback.call(args); | ||
| 166 | } | ||
| 167 | |||
| 168 | request->deleteLater(); | ||
| 169 | }); | ||
| 170 | |||
| 171 | request->start(); | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | AssetMappingItem::AssetMappingItem(const QString& name, const QString& fullPath, bool isFolder) : | ||
| 176 | name(name), | ||
| 177 | fullPath(fullPath), | ||
| 178 | isFolder(isFolder) | ||
| 179 | { | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | static int assetMappingModelMetatypeId = qRegisterMetaType<AssetMappingModel*>("AssetMappingModel*"); | ||
| 184 | |||
| 185 | void AssetMappingModel::refresh() { | ||
| 186 | qDebug() << "Refreshing asset mapping model"; | ||
| 187 | auto assetClient = DependencyManager::get<AssetClient>(); | ||
| 188 | auto request = assetClient->createGetAllMappingsRequest(); | ||
| 189 | |||
| 190 | connect(request, &GetAllMappingsRequest::finished, this, [this](GetAllMappingsRequest* request) mutable { | ||
| 191 | if (request->getError() == MappingRequest::NoError) { | ||
| 192 | auto mappings = request->getMappings(); | ||
| 193 | auto existingPaths = _pathToItemMap.keys(); | ||
| 194 | for (auto& mapping : mappings) { | ||
| 195 | auto& path = mapping.first; | ||
| 196 | auto parts = path.split("/"); | ||
| 197 | auto length = parts.length(); | ||
| 198 | |||
| 199 | existingPaths.removeOne(mapping.first); | ||
| 200 | |||
| 201 | QString fullPath = "/"; | ||
| 202 | |||
| 203 | QStandardItem* lastItem = nullptr; | ||
| 204 | |||
| 205 | // start index at 1 to avoid empty string from leading slash | ||
| 206 | for (int i = 1; i < length; ++i) { | ||
| 207 | fullPath += (i == 1 ? "" : "/") + parts[i]; | ||
| 208 | |||
| 209 | auto it = _pathToItemMap.find(fullPath); | ||
| 210 | if (it == _pathToItemMap.end()) { | ||
| 211 | auto item = new QStandardItem(parts[i]); | ||
| 212 | bool isFolder = i < length - 1; | ||
| 213 | item->setData(isFolder ? fullPath + "/" : fullPath, Qt::UserRole); | ||
| 214 | item->setData(isFolder, Qt::UserRole + 1); | ||
| 215 | item->setData(parts[i], Qt::UserRole + 2); | ||
| 216 | item->setData("atp:" + fullPath, Qt::UserRole + 3); | ||
| 217 | item->setData(fullPath, Qt::UserRole + 4); | ||
| 218 | if (lastItem) { | ||
| 219 | lastItem->setChild(lastItem->rowCount(), 0, item); | ||
| 220 | } else { | ||
| 221 | appendRow(item); | ||
| 222 | } | ||
| 223 | |||
| 224 | lastItem = item; | ||
| 225 | _pathToItemMap[fullPath] = lastItem; | ||
| 226 | } | ||
| 227 | else { | ||
| 228 | lastItem = it.value(); | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | Q_ASSERT(fullPath == path); | ||
| 233 | } | ||
| 234 | |||
| 235 | // Remove folders from list | ||
| 236 | auto it = existingPaths.begin(); | ||
| 237 | while (it != existingPaths.end()) { | ||
| 238 | Q_ASSERT(_pathToItemMap.contains(*it)); | ||
| 239 | auto item = _pathToItemMap[*it]; | ||
| 240 | if (item && item->data(Qt::UserRole + 1).toBool()) { | ||
| 241 | it = existingPaths.erase(it); | ||
| 242 | } else { | ||
| 243 | ++it; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | for (auto& path : existingPaths) { | ||
| 248 | Q_ASSERT(_pathToItemMap.contains(path)); | ||
| 249 | |||
| 250 | auto item = _pathToItemMap[path]; | ||
| 251 | |||
| 252 | while (item) { | ||
| 253 | // During each iteration, delete item | ||
| 254 | QStandardItem* nextItem = nullptr; | ||
| 255 | |||
| 256 | auto fullPath = item->data(Qt::UserRole + 4).toString(); | ||
| 257 | auto parent = item->parent(); | ||
| 258 | if (parent) { | ||
| 259 | parent->removeRow(item->row()); | ||
| 260 | if (parent->rowCount() > 0) { | ||
| 261 | // The parent still contains children, set the nextItem to null so we stop processing | ||
| 262 | nextItem = nullptr; | ||
| 263 | } else { | ||
| 264 | nextItem = parent; | ||
| 265 | } | ||
| 266 | } else { | ||
| 267 | auto removed = removeRow(item->row()); | ||
| 268 | Q_ASSERT(removed); | ||
| 269 | } | ||
| 270 | |||
| 271 | Q_ASSERT(_pathToItemMap.contains(fullPath)); | ||
| 272 | _pathToItemMap.remove(fullPath); | ||
| 273 | |||
| 274 | item = nextItem; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | } else { | ||
| 278 | emit errorGettingMappings(static_cast<int>(request->getError())); | ||
| 279 | } | ||
| 280 | }); | ||
| 281 | |||
| 282 | request->start(); | ||
| 283 | } | ||