From c4ff129f861e3d5fae5551dc319640794b9ca4f3 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Wed, 14 Dec 2016 14:45:33 +1300 Subject: [PATCH] Inform user if can't do Clara.io download because can't write to ATP --- interface/src/Application.cpp | 28 +++++++++----- scripts/system/html/js/marketplacesInject.js | 30 +++++++++++++-- scripts/system/marketplaces/marketplaces.js | 40 ++++++++++++++++++-- 3 files changed, 81 insertions(+), 17 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 5ae57affef..c193a74cb1 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -5582,17 +5582,24 @@ void Application::showAssetServerWidget(QString filePath) { void Application::addAssetToWorldFromURL(QString url) { qInfo(interfaceapp) << "Download asset and add to world from" << url; + if (!_addAssetToWorldMessageBox) { + _addAssetToWorldMessageBox = DependencyManager::get()->createMessageBox(OffscreenUi::ICON_INFORMATION, + "Downloading Asset", "Downloading asset file " + url.section("filename=", 1, 1), + QMessageBox::Cancel, QMessageBox::NoButton); + connect(_addAssetToWorldMessageBox, SIGNAL(destroyed()), this, SLOT(onAssetToWorldMessageBoxClosed())); + } + + if (!DependencyManager::get()->getThisNodeCanWriteAssets()) { + QString errorInfo = "You do not have permissions to write to the Asset Server."; + qWarning(interfaceapp) << "Error downloading asset: " + errorInfo; + addAssetToWorldError(errorInfo); + return; + } + QUrl urlURL = QUrl(url); auto request = ResourceManager::createResourceRequest(nullptr, urlURL); connect(request, &ResourceRequest::finished, this, &Application::addAssetToWorldFromURLRequestFinished); request->send(); - - if (!_addAssetToWorldMessageBox) { - _addAssetToWorldMessageBox = DependencyManager::get()->createMessageBox(OffscreenUi::ICON_INFORMATION, - "Downloading Asset", "Downloading asset file " + url.section("filename=", 1, 1), - QMessageBox::Cancel, QMessageBox::NoButton); - } - connect(_addAssetToWorldMessageBox, SIGNAL(destroyed()), this, SLOT(onAssetToWorldMessageBoxClosed())); } void Application::addAssetToWorldFromURLRequestFinished() { @@ -5650,8 +5657,9 @@ void Application::addAssetToWorld(QString filePath) { return; } + // Test repeated because possibly different code paths. if (!DependencyManager::get()->getThisNodeCanWriteAssets()) { - QString errorInfo = "Do not have permissions to write to asset server."; + QString errorInfo = "You do not have permissions to write to the Asset Server."; qWarning(interfaceapp) << "Error downloading asset: " + errorInfo; addAssetToWorldError(errorInfo); return; @@ -5660,7 +5668,7 @@ void Application::addAssetToWorld(QString filePath) { QString path = QUrl(filePath).toLocalFile(); QString mapping = path.right(path.length() - path.lastIndexOf("/")); - _addAssetToWorldMessageBox->setProperty("text", "Adding " + mapping.mid(1) + " to Asset Server."); + _addAssetToWorldMessageBox->setProperty("text", "Adding " + mapping.mid(1) + " to the Asset Server."); addAssetToWorldWithNewMapping(path, mapping, 0); } @@ -5708,7 +5716,7 @@ void Application::addAssetToWorldUpload(QString path, QString mapping) { auto upload = DependencyManager::get()->createUpload(path); QObject::connect(upload, &AssetUpload::finished, this, [=](AssetUpload* upload, const QString& hash) mutable { if (upload->getError() != AssetUpload::NoError) { - QString errorInfo = "Could not upload asset to asset server."; + QString errorInfo = "Could not upload asset to the Asset Server."; qWarning(interfaceapp) << "Error downloading asset: " + errorInfo; addAssetToWorldError(errorInfo); } else { diff --git a/scripts/system/html/js/marketplacesInject.js b/scripts/system/html/js/marketplacesInject.js index b41f2c4825..1a64668c0e 100644 --- a/scripts/system/html/js/marketplacesInject.js +++ b/scripts/system/html/js/marketplacesInject.js @@ -12,6 +12,15 @@ (function () { + // Event bridge messages. + var CLARA_IO_DOWNLOAD = "CLARA.IO DOWNLOAD"; + var GOTO_DIRECTORY = "GOTO_DIRECTORY"; + var QUERY_CAN_WRITE_ASSETS = "QUERY_CAN_WRITE_ASSETS"; + var CAN_WRITE_ASSETS = "CAN_WRITE_ASSETS"; + var WARN_USER_NO_PERMISSIONS = "WARN_USER_NO_PERMISSIONS"; + + var canWriteAssets = false; + function injectCommonCode(isDirectoryPage) { // Supporting styles from marketplaces.css. @@ -54,7 +63,7 @@ window.history.back(); }); $("#all-markets").on("click", function () { - EventBridge.emitWebEvent("GOTO_DIRECTORY"); + EventBridge.emitWebEvent(GOTO_DIRECTORY); }); } @@ -118,7 +127,13 @@ // Automatic download to High Fidelity. var downloadTimer; - function startAutoDownload() { + function startAutoDownload(event) { + if (!canWriteAssets) { + console.log("Clara.io FBX file download cancelled because no permissions to write to Asset Server"); + EventBridge.emitWebEvent(WARN_USER_NO_PERMISSIONS); + event.stopPropagation(); + } + window.scrollTo(0, 0); // Scroll to top ready for history.back(). if (!downloadTimer) { downloadTimer = setInterval(autoDownload, 1000); @@ -131,7 +146,7 @@ clearInterval(downloadTimer); downloadTimer = null; var href = downloadButton[0].href; - EventBridge.emitWebEvent("CLARA.IO DOWNLOAD " + href); + EventBridge.emitWebEvent(CLARA_IO_DOWNLOAD + " " + href); console.log("Clara.io FBX file download initiated for " + href); $("a.btn.cancel").click(); history.back(); // Remove history item created by clicking "download". @@ -170,9 +185,18 @@ checkLocationInterval = undefined; currentLocation = ""; }); + + EventBridge.emitWebEvent(QUERY_CAN_WRITE_ASSETS); } function onLoad() { + + EventBridge.scriptEventReceived.connect(function (message) { + if (message.slice(0, CAN_WRITE_ASSETS.length) === CAN_WRITE_ASSETS) { + canWriteAssets = message.slice(-4) === "true"; + } + }); + var DIRECTORY = 0; var HIFI = 1; var CLARA = 2; diff --git a/scripts/system/marketplaces/marketplaces.js b/scripts/system/marketplaces/marketplaces.js index 8a61915bee..41fb590830 100644 --- a/scripts/system/marketplaces/marketplaces.js +++ b/scripts/system/marketplaces/marketplaces.js @@ -20,6 +20,14 @@ var MARKETPLACE_URL_INITIAL = MARKETPLACE_URL + "?"; // Append "?" to signal in var MARKETPLACES_URL = Script.resolvePath("../html/marketplaces.html"); var MARKETPLACES_INJECT_SCRIPT_URL = Script.resolvePath("../html/js/marketplacesInject.js"); +// Event bridge messages. +var CLARA_IO_DOWNLOAD = "CLARA.IO DOWNLOAD"; +var GOTO_DIRECTORY = "GOTO_DIRECTORY"; +var QUERY_CAN_WRITE_ASSETS = "QUERY_CAN_WRITE_ASSETS"; +var CAN_WRITE_ASSETS = "CAN_WRITE_ASSETS"; +var WARN_USER_NO_PERMISSIONS = "WARN_USER_NO_PERMISSIONS"; +var NO_PERMISSIONS_ERROR_MESSAGE = "Cannot download model because you can't write to \nthe domain's Asset Server."; + var marketplaceWindow = new OverlayWebWindow({ title: "Marketplace", source: "about:blank", @@ -28,10 +36,16 @@ var marketplaceWindow = new OverlayWebWindow({ visible: false }); marketplaceWindow.setScriptURL(MARKETPLACES_INJECT_SCRIPT_URL); -marketplaceWindow.webEventReceived.connect(function (data) { - if (data === "GOTO_DIRECTORY") { +marketplaceWindow.webEventReceived.connect(function (message) { + if (message === GOTO_DIRECTORY) { marketplaceWindow.setURL(MARKETPLACES_URL); } + if (message === QUERY_CAN_WRITE_ASSETS) { + marketplaceWindow.emitScriptEvent(CAN_WRITE_ASSETS + " " + Entities.canWriteAssets()); + } + if (message === WARN_USER_NO_PERMISSIONS) { + Window.alert(NO_PERMISSIONS_ERROR_MESSAGE); + } }); var toolHeight = 50; @@ -57,10 +71,16 @@ function showMarketplace() { marketplaceWebTablet = new WebTablet(MARKETPLACE_URL_INITIAL, null, null, true); Settings.setValue(persistenceKey, marketplaceWebTablet.pickle()); marketplaceWebTablet.setScriptURL(MARKETPLACES_INJECT_SCRIPT_URL); - marketplaceWebTablet.getOverlayObject().webEventReceived.connect(function (data) { - if (data === "GOTO_DIRECTORY") { + marketplaceWebTablet.getOverlayObject().webEventReceived.connect(function (message) { + if (message === GOTO_DIRECTORY) { marketplaceWebTablet.setURL(MARKETPLACES_URL); } + if (message === QUERY_CAN_WRITE_ASSETS) { + marketplaceWebTablet.getOverlayObject().emitScriptEvent(CAN_WRITE_ASSETS + " " + Entities.canWriteAssets()); + } + if (message === WARN_USER_NO_PERMISSIONS) { + Window.alert(NO_PERMISSIONS_ERROR_MESSAGE); + } }); } else { marketplaceWindow.setURL(MARKETPLACE_URL_INITIAL); @@ -123,12 +143,23 @@ function onMarketplaceWindowVisibilityChanged() { marketplaceVisible = marketplaceWindow.visible; } +function onCanWriteAssetsChanged() { + var message = CAN_WRITE_ASSETS + " " + Entities.canWriteAssets(); + if (marketplaceWindow.visible) { + marketplaceWindow.emitScriptEvent(message); + } + if (marketplaceWebTablet) { + marketplaceWebTablet.getOverlayObject().emitScriptEvent(message); + } +} + function onClick() { toggleMarketplace(); } browseExamplesButton.clicked.connect(onClick); marketplaceWindow.visibleChanged.connect(onMarketplaceWindowVisibilityChanged); +Entities.canWriteAssetsChanged.connect(onCanWriteAssetsChanged); clearOldTablet(); // Run once at startup, in case there's anything laying around from a crash. // We could also optionally do something like Window.domainChanged.connect(function () {Script.setTimeout(clearOldTablet, 2000)}), @@ -138,6 +169,7 @@ Script.scriptEnding.connect(function () { toolBar.removeButton("marketplace"); browseExamplesButton.clicked.disconnect(onClick); marketplaceWindow.visibleChanged.disconnect(onMarketplaceWindowVisibilityChanged); + Entities.canWriteAssetsChanged.disconnect(onCanWriteAssetsChanged); }); }()); // END LOCAL_SCOPE