Showing the changes to libraries/script-engine/src/AssetMappingsScriptingInterface.cpp (1 of 8 files). Show all
Unified · Split
| @@ -29,87 +29,117 @@ void AssetMappingsScriptingInterface::setMapping(QString path, QString hash, QJS · 28 unmodified lines | ||
| 29 | 29 | auto assetClient = DependencyManager::get<AssetClient>(); |
| 30 | 30 | auto request = assetClient->createSetMappingRequest(path, hash); |
| 31 | 31 | |
| 32 | − connect(request, &SetMappingRequest::finished, this, [this, callback](SetMappingRequest* request) mutable { | |
| 33 | − QJSValueList args { uint8_t(request->getError()) }; | |
| 32 | + if (request) { | |
| 33 | + connect(request, &SetMappingRequest::finished, this, [this, callback](SetMappingRequest* request) mutable { | |
| 34 | + QJSValueList args { uint8_t(request->getError()) }; | |
| 34 | 35 | |
| 35 | − callback.call(args); | |
| 36 | + callback.call(args); | |
| 36 | 37 | |
| 37 | − request->deleteLater(); | |
| 38 | − | |
| 39 | − }); | |
| 38 | + request->deleteLater(); | |
| 40 | 39 | |
| 41 | − request->start(); | |
| 40 | + }); | |
| 41 | + | |
| 42 | + request->start(); | |
| 43 | + } else { | |
| 44 | + // not connected to an Asset Server, return network error | |
| 45 | + QJSValueList args { uint8_t(MappingRequest::NetworkError) }; | |
| 46 | + callback.call(args); | |
| 47 | + } | |
| 42 | 48 | } |
| 43 | 49 | |
| 44 | 50 | void AssetMappingsScriptingInterface::getMapping(QString path, QJSValue callback) { |
| 45 | 51 | auto assetClient = DependencyManager::get<AssetClient>(); |
| 46 | 52 | auto request = assetClient->createGetMappingRequest(path); |
| 47 | 53 | |
| 48 | − connect(request, &GetMappingRequest::finished, this, [this, callback](GetMappingRequest* request) mutable { | |
| 49 | − QJSValueList args { uint8_t(request->getError()), request->getHash() }; | |
| 54 | + if (request) { | |
| 55 | + connect(request, &GetMappingRequest::finished, this, [this, callback](GetMappingRequest* request) mutable { | |
| 56 | + QJSValueList args { uint8_t(request->getError()), request->getHash() }; | |
| 50 | 57 | |
| 51 | − callback.call(args); | |
| 58 | + callback.call(args); | |
| 52 | 59 | |
| 53 | − request->deleteLater(); | |
| 54 | − | |
| 55 | − }); | |
| 60 | + request->deleteLater(); | |
| 56 | 61 | |
| 57 | − request->start(); | |
| 62 | + }); | |
| 63 | + | |
| 64 | + request->start(); | |
| 65 | + } else { | |
| 66 | + // not connected to an Asset Server, return network error | |
| 67 | + QJSValueList args { uint8_t(MappingRequest::NetworkError) }; | |
| 68 | + callback.call(args); | |
| 69 | + } | |
| 58 | 70 | } |
| 59 | 71 | |
| 60 | 72 | void AssetMappingsScriptingInterface::deleteMappings(QStringList paths, QJSValue callback) { |
| 61 | 73 | auto assetClient = DependencyManager::get<AssetClient>(); |
| 62 | 74 | auto request = assetClient->createDeleteMappingsRequest(paths); |
| 63 | 75 | |
| 64 | − connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable { | |
| 65 | − QJSValueList args { uint8_t(request->getError()) }; | |
| 76 | + if (request) { | |
| 77 | + connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable { | |
| 78 | + QJSValueList args { uint8_t(request->getError()) }; | |
| 66 | 79 | |
| 67 | − callback.call(args); | |
| 80 | + callback.call(args); | |
| 68 | 81 | |
| 69 | − request->deleteLater(); | |
| 70 | − | |
| 71 | − }); | |
| 82 | + request->deleteLater(); | |
| 72 | 83 | |
| 73 | − request->start(); | |
| 84 | + }); | |
| 85 | + | |
| 86 | + request->start(); | |
| 87 | + } else { | |
| 88 | + // not connected to an Asset Server, return network error | |
| 89 | + QJSValueList args { uint8_t(MappingRequest::NetworkError) }; | |
| 90 | + callback.call(args); | |
| 91 | + } | |
| 74 | 92 | } |
| 75 | 93 | |
| 76 | 94 | void AssetMappingsScriptingInterface::getAllMappings(QJSValue callback) { |
| 77 | 95 | auto assetClient = DependencyManager::get<AssetClient>(); |
| 78 | 96 | auto request = assetClient->createGetAllMappingsRequest(); |
| 79 | 97 | |
| 80 | − connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable { | |
| 81 | − auto mappings = request->getMappings(); | |
| 82 | − auto map = callback.engine()->newObject(); | |
| 98 | + if (request) { | |
| 99 | + connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable { | |
| 100 | + auto mappings = request->getMappings(); | |
| 101 | + auto map = callback.engine()->newObject(); | |
| 83 | 102 | |
| 84 | − for (auto& kv : mappings ) { | |
| 85 | − map.setProperty(kv.first, kv.second); | |
| 86 | − } | |
| 87 | − | |
| 88 | − QJSValueList args { uint8_t(request->getError()), map }; | |
| 103 | + for (auto& kv : mappings ) { | |
| 104 | + map.setProperty(kv.first, kv.second); | |
| 105 | + } | |
| 89 | 106 | |
| 90 | − callback.call(args); | |
| 107 | + QJSValueList args { uint8_t(request->getError()), map }; | |
| 91 | 108 | |
| 92 | − request->deleteLater(); | |
| 93 | − | |
| 94 | − }); | |
| 109 | + callback.call(args); | |
| 95 | 110 | |
| 96 | − request->start(); | |
| 111 | + request->deleteLater(); | |
| 112 | + | |
| 113 | + }); | |
| 114 | + | |
| 115 | + request->start(); | |
| 116 | + } else { | |
| 117 | + // not connected to an Asset Server, return network error | |
| 118 | + QJSValueList args { uint8_t(MappingRequest::NetworkError) }; | |
| 119 | + callback.call(args); | |
| 120 | + } | |
| 97 | 121 | } |
| 98 | 122 | |
| 99 | 123 | void AssetMappingsScriptingInterface::renameMapping(QString oldPath, QString newPath, QJSValue callback) { |
| 100 | 124 | auto assetClient = DependencyManager::get<AssetClient>(); |
| 101 | 125 | auto request = assetClient->createRenameMappingRequest(oldPath, newPath); |
| 102 | 126 | |
| 103 | − connect(request, &RenameMappingRequest::finished, this, [this, callback](RenameMappingRequest* request) mutable { | |
| 104 | − QJSValueList args { uint8_t(request->getError()) }; | |
| 127 | + if (request) { | |
| 128 | + connect(request, &RenameMappingRequest::finished, this, [this, callback](RenameMappingRequest* request) mutable { | |
| 129 | + QJSValueList args { uint8_t(request->getError()) }; | |
| 130 | + | |
| 131 | + callback.call(args); | |
| 132 | + | |
| 133 | + request->deleteLater(); | |
| 134 | + | |
| 135 | + }); | |
| 105 | 136 | |
| 137 | + request->start(); | |
| 138 | + } else { | |
| 139 | + // not connected to an Asset Server, return network error | |
| 140 | + QJSValueList args { uint8_t(MappingRequest::NetworkError) }; | |
| 106 | 141 | callback.call(args); |
| 107 | − | |
| 108 | − request->deleteLater(); | |
| 109 | − | |
| 110 | − }); | |
| 111 | − | |
| 112 | − request->start(); | |
| 142 | + } | |
| 113 | 143 | } |
| 114 | 144 | |
| 115 | 145 | |
| @@ -134,92 +164,102 @@ void AssetMappingModel::refresh() { · 18 unmodified lines | ||
| 134 | 164 | auto assetClient = DependencyManager::get<AssetClient>(); |
| 135 | 165 | auto request = assetClient->createGetAllMappingsRequest(); |
| 136 | 166 | |
| 137 | − connect(request, &GetAllMappingsRequest::finished, this, [this](GetAllMappingsRequest* request) mutable { | |
| 138 | − auto mappings = request->getMappings(); | |
| 139 | − auto existingPaths = _pathToItemMap.keys(); | |
| 140 | − for (auto& mapping : mappings) { | |
| 141 | − auto& path = mapping.first; | |
| 142 | − auto parts = path.split("/"); | |
| 143 | − auto length = parts.length(); | |
| 144 | − | |
| 145 | − existingPaths.removeOne(mapping.first); | |
| 146 | − | |
| 147 | − QString fullPath = "/"; | |
| 148 | − | |
| 149 | − QStandardItem* lastItem = nullptr; | |
| 150 | − | |
| 151 | − // start index at 1 to avoid empty string from leading slash | |
| 152 | − for (int i = 1; i < length; ++i) { | |
| 153 | − fullPath += (i == 1 ? "" : "/") + parts[i]; | |
| 154 | − | |
| 155 | − auto it = _pathToItemMap.find(fullPath); | |
| 156 | − if (it == _pathToItemMap.end()) { | |
| 157 | − qDebug() << "prefix not found: " << fullPath; | |
| 158 | − auto item = new QStandardItem(parts[i]); | |
| 159 | − bool isFolder = i < length - 1; | |
| 160 | − item->setData(isFolder ? fullPath + "/" : fullPath, Qt::UserRole); | |
| 161 | − item->setData(isFolder, Qt::UserRole + 1); | |
| 162 | − item->setData(parts[i], Qt::UserRole + 2); | |
| 163 | − item->setData("atp:" + fullPath, Qt::UserRole + 3); | |
| 164 | − if (lastItem) { | |
| 165 | − lastItem->setChild(lastItem->rowCount(), 0, item); | |
| 166 | − } else { | |
| 167 | − appendRow(item); | |
| 167 | + if (request) { | |
| 168 | + connect(request, &GetAllMappingsRequest::finished, this, [this](GetAllMappingsRequest* request) mutable { | |
| 169 | + if (request->getError() == MappingRequest::NoError) { | |
| 170 | + auto mappings = request->getMappings(); | |
| 171 | + auto existingPaths = _pathToItemMap.keys(); | |
| 172 | + for (auto& mapping : mappings) { | |
| 173 | + auto& path = mapping.first; | |
| 174 | + auto parts = path.split("/"); | |
| 175 | + auto length = parts.length(); | |
| 176 | + | |
| 177 | + existingPaths.removeOne(mapping.first); | |
| 178 | + | |
| 179 | + QString fullPath = "/"; | |
| 180 | + | |
| 181 | + QStandardItem* lastItem = nullptr; | |
| 182 | + | |
| 183 | + // start index at 1 to avoid empty string from leading slash | |
| 184 | + for (int i = 1; i < length; ++i) { | |
| 185 | + fullPath += (i == 1 ? "" : "/") + parts[i]; | |
| 186 | + | |
| 187 | + auto it = _pathToItemMap.find(fullPath); | |
| 188 | + if (it == _pathToItemMap.end()) { | |
| 189 | + qDebug() << "prefix not found: " << fullPath; | |
| 190 | + auto item = new QStandardItem(parts[i]); | |
| 191 | + bool isFolder = i < length - 1; | |
| 192 | + item->setData(isFolder ? fullPath + "/" : fullPath, Qt::UserRole); | |
| 193 | + item->setData(isFolder, Qt::UserRole + 1); | |
| 194 | + item->setData(parts[i], Qt::UserRole + 2); | |
| 195 | + item->setData("atp:" + fullPath, Qt::UserRole + 3); | |
| 196 | + if (lastItem) { | |
| 197 | + lastItem->setChild(lastItem->rowCount(), 0, item); | |
| 198 | + } else { | |
| 199 | + appendRow(item); | |
| 200 | + } | |
| 201 | + | |
| 202 | + lastItem = item; | |
| 203 | + _pathToItemMap[fullPath] = lastItem; | |
| 204 | + } | |
| 205 | + else { | |
| 206 | + lastItem = it.value(); | |
| 207 | + } | |
| 168 | 208 | } |
| 169 | 209 | |
| 170 | − lastItem = item; | |
| 171 | − _pathToItemMap[fullPath] = lastItem; | |
| 210 | + Q_ASSERT(fullPath == path); | |
| 172 | 211 | } |
| 173 | − else { | |
| 174 | − lastItem = it.value(); | |
| 175 | − } | |
| 176 | − } | |
| 177 | − | |
| 178 | − Q_ASSERT(fullPath == path); | |
| 179 | − } | |
| 180 | − | |
| 181 | − // Remove folders from list | |
| 182 | − auto it = existingPaths.begin(); | |
| 183 | − while (it != existingPaths.end()) { | |
| 184 | − auto item = _pathToItemMap[*it]; | |
| 185 | − if (item->data(Qt::UserRole + 1).toBool()) { | |
| 186 | − it = existingPaths.erase(it); | |
| 187 | − } else { | |
| 188 | − ++it; | |
| 189 | − } | |
| 190 | − } | |
| 191 | 212 | |
| 192 | − for (auto& path : existingPaths) { | |
| 193 | − Q_ASSERT(_pathToItemMap.contains(path)); | |
| 194 | − qDebug() << "removing existing: " << path; | |
| 195 | − | |
| 196 | − auto item = _pathToItemMap[path]; | |
| 197 | − | |
| 198 | − while (item) { | |
| 199 | − // During each iteration, delete item | |
| 200 | − QStandardItem* nextItem = nullptr; | |
| 201 | − | |
| 202 | − auto parent = item->parent(); | |
| 203 | − if (parent) { | |
| 204 | − parent->removeRow(item->row()); | |
| 205 | − if (parent->rowCount() > 0) { | |
| 206 | − // The parent still contains children, set the nextItem to null so we stop processing | |
| 207 | − nextItem = nullptr; | |
| 213 | + // Remove folders from list | |
| 214 | + auto it = existingPaths.begin(); | |
| 215 | + while (it != existingPaths.end()) { | |
| 216 | + auto item = _pathToItemMap[*it]; | |
| 217 | + if (item->data(Qt::UserRole + 1).toBool()) { | |
| 218 | + it = existingPaths.erase(it); | |
| 208 | 219 | } else { |
| 209 | − nextItem = parent; | |
| 220 | + ++it; | |
| 210 | 221 | } |
| 211 | − } else { | |
| 212 | − removeRow(item->row()); | |
| 213 | 222 | } |
| 214 | 223 | |
| 215 | − _pathToItemMap.remove(path); | |
| 216 | − //delete item; | |
| 217 | − | |
| 218 | − item = nextItem; | |
| 224 | + for (auto& path : existingPaths) { | |
| 225 | + Q_ASSERT(_pathToItemMap.contains(path)); | |
| 226 | + qDebug() << "removing existing: " << path; | |
| 227 | + | |
| 228 | + auto item = _pathToItemMap[path]; | |
| 229 | + | |
| 230 | + while (item) { | |
| 231 | + // During each iteration, delete item | |
| 232 | + QStandardItem* nextItem = nullptr; | |
| 233 | + | |
| 234 | + auto parent = item->parent(); | |
| 235 | + if (parent) { | |
| 236 | + parent->removeRow(item->row()); | |
| 237 | + if (parent->rowCount() > 0) { | |
| 238 | + // The parent still contains children, set the nextItem to null so we stop processing | |
| 239 | + nextItem = nullptr; | |
| 240 | + } else { | |
| 241 | + nextItem = parent; | |
| 242 | + } | |
| 243 | + } else { | |
| 244 | + removeRow(item->row()); | |
| 245 | + } | |
| 246 | + | |
| 247 | + _pathToItemMap.remove(path); | |
| 248 | + //delete item; | |
| 249 | + | |
| 250 | + item = nextItem; | |
| 251 | + } | |
| 252 | + //removeitem->index(); | |
| 253 | + } | |
| 254 | + } else { | |
| 255 | + emit errorGettingMappings(uint8_t(request->getError())); | |
| 219 | 256 | } |
| 220 | − //removeitem->index(); | |
| 221 | − } | |
| 222 | − }); | |
| 223 | − | |
| 224 | − request->start(); | |
| 257 | + }); | |
| 258 | + | |
| 259 | + request->start(); | |
| 260 | + } else { | |
| 261 | + qDebug() << "NO CONNECTED ASSET SERVER"; | |
| 262 | + // not connected to an Asset Server, emit network error | |
| 263 | + emit errorGettingMappings(uint8_t(MappingRequest::NetworkError)); | |
| 264 | + } | |
| 225 | 265 | } |