History

Handle Clara marketplace downloads

David Rowe committed 9 years ago

Commit 5fbdd641 parent 0ef972c8 files · +111 −13Browse files

Showing the changes to interface/src/Application.cpp (1 of 8 files). Show all

Unified · Split

interface/src/Application.cpp+46−8
@@ -42,6 +42,7 @@ · 41 unmodified lines
4242 #include <QtMultimedia/QMediaPlayer>
4343
4444 #include <QProcessEnvironment>
45+#include <QTemporaryDir>
4546
4647 #include <gl/QOpenGLContextWrapper.h>
4748
@@ -1859,9 +1860,9 @@ void Application::initializeUi() { · 1811 unmodified lines
18591860 rootContext->setContextProperty("AudioStats", DependencyManager::get<AudioClient>()->getStats().data());
18601861 rootContext->setContextProperty("Controller", DependencyManager::get<controller::ScriptingInterface>().data());
18611862 rootContext->setContextProperty("Entities", DependencyManager::get<EntityScriptingInterface>().data());
1862− FileScriptingInterface* fileDownload = new FileScriptingInterface(engine);
1863− rootContext->setContextProperty("File", fileDownload);
1864− connect(fileDownload, &FileScriptingInterface::unzipSuccess, this, &Application::handleUnzip);
1863+ _fileDownload = new FileScriptingInterface(engine);
1864+ rootContext->setContextProperty("File", _fileDownload);
1865+ connect(_fileDownload, &FileScriptingInterface::unzipSuccess, this, &Application::handleUnzip);
18651866 rootContext->setContextProperty("MyAvatar", getMyAvatar().get());
18661867 rootContext->setContextProperty("Messages", DependencyManager::get<MessagesClient>().data());
18671868 rootContext->setContextProperty("Recording", DependencyManager::get<RecordingScriptingInterface>().data());
@@ -5554,17 +5555,54 @@ void Application::showAssetServerWidget(QString filePath) { · 3686 unmodified lines
55545555 startUpload(nullptr, nullptr);
55555556 }
55565557
5557−void Application::addAssetToWorldInitiate() {
5558− qCDebug(interfaceapp) << "Start downloading asset file";
5558+void Application::addAssetToWorldFromURL(QString url) {
5559+ qInfo(interfaceapp) << "Download asset and add to world from" << url;
5560+
5561+ QUrl urlURL = QUrl(url);
5562+ auto request = ResourceManager::createResourceRequest(nullptr, urlURL);
5563+ connect(request, &ResourceRequest::finished, this, &Application::addAssetToWorldFromURLRequestFinished);
5564+ request->send();
55595565
55605566 if (!_addAssetToWorldMessageBox) {
5561− _addAssetToWorldMessageBox = DependencyManager::get<OffscreenUi>()->createMessageBox(OffscreenUi::ICON_INFORMATION,
5562− "Downloading Asset", "Downloading asset file.", QMessageBox::Cancel, QMessageBox::NoButton);
5567+ _addAssetToWorldMessageBox = DependencyManager::get<OffscreenUi>()->createMessageBox(OffscreenUi::ICON_INFORMATION,
5568+ "Downloading Asset", "Downloading asset file " + url.section("filename=", 1, 1),
5569+ QMessageBox::Cancel, QMessageBox::NoButton);
55635570 }
5564−
55655571 connect(_addAssetToWorldMessageBox, SIGNAL(destroyed()), this, SLOT(onAssetToWorldMessageBoxClosed()));
55665572 }
55675573
5574+void Application::addAssetToWorldFromURLRequestFinished() {
5575+ auto request = qobject_cast<ResourceRequest*>(sender());
5576+ auto url = request->getUrl().toString();
5577+ auto result = request->getResult();
5578+
5579+ if (result == ResourceRequest::Success) {
5580+ qInfo(interfaceapp) << "Downloaded asset from" << url;
5581+
5582+ QTemporaryDir temporaryDir;
5583+ temporaryDir.setAutoRemove(false);
5584+ QString temporaryDirPath = temporaryDir.path();
5585+ QString filename = url.section("filename=", 1, 1);
5586+ QString downloadPath = temporaryDirPath + "/" + filename;
5587+ qInfo() << "Download path:" << downloadPath;
5588+
5589+ QFile tempFile(downloadPath);
5590+ if (tempFile.open(QIODevice::WriteOnly)) {
5591+ tempFile.write(request->getData());
5592+ qApp->getFileDownloadInterface()->runUnzip(downloadPath, url, true);
5593+ } else {
5594+ QString errorInfo = "Couldn't open temporary file for writing";
5595+ qWarning(interfaceapp) << errorInfo;
5596+ addAssetToWorldError(errorInfo);
5597+ }
5598+ } else {
5599+ qWarning(interfaceapp) << "Error downloading" << url << ":" << request->getResultString();
5600+ addAssetToWorldError("Error downloading " + url.section("filename=", 1, 1) + " : " + request->getResultString());
5601+ }
5602+
5603+ request->deleteLater();
5604+}
5605+
55685606 void Application::onAssetToWorldMessageBoxClosed() {
55695607 disconnect(_addAssetToWorldMessageBox);
55705608 _addAssetToWorldMessageBox = nullptr;