Made code more QA; extra browser and download widget close upon download

This commit is contained in:
elisa-lj11 2016-07-26 16:33:06 -07:00
parent a94a304700
commit b1b2ea48b2
13 changed files with 144 additions and 196 deletions

View file

@ -320,7 +320,7 @@ ScrollingWindow {
id: timer
}
function uploadClicked(fileUrl) {
console.log("Upload clicked url: " + fileUrl);
console.log("Upload clicked url: " + fileUrl);
if (uploadOpen) {
return;
}

View file

@ -140,7 +140,6 @@ ScrollingWindow {
}
}
<<<<<<< ab02d16eb7656ea0a37785a398716fad62dff045
Rectangle {
id:permissionsContainer
visible:false
@ -197,8 +196,6 @@ ScrollingWindow {
}
}
}
=======
>>>>>>> Zip download works
WebEngineView {
id: webview
@ -221,15 +218,11 @@ ScrollingWindow {
onIconChanged: {
console.log("New icon: " + icon)
}
Component.onCompleted: {
desktop.initWebviewProfileHandlers(webview.profile)
}
//profile: desktop.browserProfile
}

View file

@ -25,7 +25,7 @@ WebEngineView {
});
}
// FIXME hack to get the URL with the auth token included. Remove when we move to Qt 5.6
Timer {
@ -51,7 +51,7 @@ WebEngineView {
onLoadingChanged: {
// Required to support clicking on "hifi://" links
console.log("loading change requested url");
console.log("loading change requested url");
if (WebEngineView.LoadStartedStatus == loadRequest.status) {
var url = loadRequest.url.toString();
if (urlHandler.canHandleUrl(url)) {
@ -62,26 +62,44 @@ WebEngineView {
}
}
onNewViewRequested: {
console.log("new view requested url");
//console.log("new view requested url" + request.url.toString());
var component = Qt.createComponent("../Browser.qml");
var newWindow = component.createObject(desktop);
request.openIn(newWindow.webView);
Timer {
id: zipTimer
running: false
repeat: false
interval: 1000
property var handler;
onTriggered: handler();
}
property var myScript: 'var element = $("a.download-file");
element.removeClass("download-file");
element.removeAttr("download _target");'
property var autoCancel: 'var element = $("a.btn.cancel");
element.click();'
onLinkHovered: {
desktop.currentUrl = hoveredUrl
console.log("my url in WebView: " + desktop.currentUrl)
if (File.testUrl(desktop.currentUrl)) {
runJavaScript(myScript, function() {console.log("ran the JS"); });
}
onNewViewRequested: {
console.log("new view requested url");
var component = Qt.createComponent("../Browser.qml");
var newWindow = component.createObject(desktop);
request.openIn(newWindow.webView);
if (File.testUrl(desktop.currentUrl)) {
zipTimer.handler = function() {
newWindow.destroy();
runJavaScript(autoCancel);
}
zipTimer.start();
}
}
}
property var simpleDownload: 'var element = $("a.download-file");
element.removeClass("download-file");
element.removeAttr("download");'
onLinkHovered: {
desktop.currentUrl = hoveredUrl
console.log("my url in WebView: " + desktop.currentUrl)
if (File.testUrl(desktop.currentUrl)) {
runJavaScript(simpleDownload, function(){console.log("ran the JS");});
}
}
// This breaks the webchannel used for passing messages. Fixed in Qt 5.6
// See https://bugreports.qt.io/browse/QTBUG-49521

View file

@ -19,7 +19,7 @@ OriginalDesktop.Desktop {
scrollGestureEnabled: false // we don't need/want these
onEntered: ApplicationCompositor.reticleOverDesktop = true
onExited: ApplicationCompositor.reticleOverDesktop = false
acceptedButtons: Qt.NoButtonMouseArea
acceptedButtons: Qt.NoButton
}
@ -73,44 +73,39 @@ OriginalDesktop.Desktop {
});
}
// Accept a download through the webview
property bool webViewProfileSetup: false
property string currentUrl: ""
property string adaptedPath: ""
function initWebviewProfileHandlers(profile) {
console.log("the webview url in desktop is: " + currentUrl)
if (webViewProfileSetup) return;
webViewProfileSetup = true;
// Accept a download through the webview
property bool webViewProfileSetup: false
property string currentUrl: ""
property string adaptedPath: ""
profile.downloadRequested.connect(function(download){
console.log("Download start: " + download.state)
//if (!File.testUrl(currentUrl)) {
//console.log("This file type is not accepted. Look for a zip file")
//download.cancel()
//return
//}
adaptedPath = File.convertUrlToPath(currentUrl)
download.path = "C:/Users/elisa/Downloads/" + adaptedPath
console.log("Path where it should download: " + download.path)
download.accept()
console.log("Download accept: " + download.state)
if (download.state === WebEngineDownloadItem.DownloadInterrupted) {
console.log("Download? " + download.state)
console.log("download failed to complete")
}
})
function initWebviewProfileHandlers(profile) {
console.log("the webview url in desktop is: " + currentUrl);
if (webViewProfileSetup) return;
webViewProfileSetup = true;
profile.downloadFinished.connect(function(download){
if (download.state === WebEngineDownloadItem.DownloadCompleted) {
console.log("Download Finished: " + download.state)
console.log("File object is: " + File)
File.runUnzip(download.path, currentUrl)
//download.cancel()
} else {
console.log("The download was corrupted, state: " + download.state)
}
})
}
profile.downloadRequested.connect(function(download){
console.log("Download start: " + download.state);
adaptedPath = File.convertUrlToPath(currentUrl);
download.path = "C:/Users/elisa/Downloads/" + adaptedPath;
console.log("Path where it should download: " + download.path);
download.accept();
console.log("Download accept: " + download.state);
if (download.state === WebEngineDownloadItem.DownloadInterrupted) {
console.log("Download? " + download.state);
console.log("download failed to complete");
}
})
profile.downloadFinished.connect(function(download){
if (download.state === WebEngineDownloadItem.DownloadCompleted) {
console.log("Download Finished: " + download.state);
console.log("File object is: " + File);
File.runUnzip(download.path, currentUrl);
} else {
console.log("The download was corrupted, state: " + download.state);
}
})
}
// Create or fetch a toolbar with the given name
function getToolbar(name) {

View file

@ -355,7 +355,6 @@ public:
if (message->message == WM_COPYDATA) {
COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)(message->lParam);
qDebug() << "os windows url for some reason";
QUrl url = QUrl((const char*)(pcds->lpData));
if (url.isValid() && url.scheme() == HIFI_URL_SCHEME) {
DependencyManager::get<AddressManager>()->handleLookupString(url.toString());
@ -1543,10 +1542,9 @@ void Application::initializeUi() {
rootContext->setContextProperty("Audio", &AudioScriptingInterface::getInstance());
rootContext->setContextProperty("Controller", DependencyManager::get<controller::ScriptingInterface>().data());
rootContext->setContextProperty("Entities", DependencyManager::get<EntityScriptingInterface>().data());
//rootContext->setContextProperty("File", new FileScriptingInterface(engine));
FileScriptingInterface* fileDownload = new FileScriptingInterface(engine);
rootContext->setContextProperty("File", fileDownload);
connect(fileDownload, &FileScriptingInterface::unzipSuccess, this, &Application::toggleAssetServerWidget);
FileScriptingInterface* fileDownload = new FileScriptingInterface(engine);
rootContext->setContextProperty("File", fileDownload);
connect(fileDownload, &FileScriptingInterface::unzipSuccess, this, &Application::toggleAssetServerWidget);
rootContext->setContextProperty("MyAvatar", getMyAvatar());
rootContext->setContextProperty("Messages", DependencyManager::get<MessagesClient>().data());
rootContext->setContextProperty("Recording", DependencyManager::get<RecordingScriptingInterface>().data());
@ -2020,14 +2018,6 @@ bool Application::importSVOFromURL(const QString& urlString) {
return true;
}
// attempt to start ZIP download project
bool Application::importZIPFromURL(const QString& urlString) {
qDebug() << "zip import request has been emitted";
emit zipImportRequested(urlString);
return true;
}
// end attempt
bool Application::event(QEvent* event) {
if (!Menu::getInstance()) {
@ -2147,26 +2137,21 @@ bool Application::event(QEvent* event) {
// handle custom URL
if (event->type() == QEvent::FileOpen) {
qDebug() << "we have received one url!: ";
QFileOpenEvent* fileEvent = static_cast<QFileOpenEvent*>(event);
QUrl url = fileEvent->url();
if (!url.isEmpty()) {
QString urlString = url.toString();
qDebug() << "we got a url!: " + urlString;
if (canAcceptURL(urlString)) {
qDebug() << "we got an ACCEPTED url!: " + urlString;
return acceptURL(urlString);
}
}
return false;
}
if (event->type() == QEvent::None) {
qDebug() << "this url just didn't click";
}
if (HFActionEvent::types().contains(event->type())) {
_controllerScriptingInterface->handleMetaEvent(static_cast<HFMetaEvent*>(event));
}
@ -3190,7 +3175,6 @@ void Application::saveSettings() const {
}
bool Application::importEntities(const QString& urlOrFilename) {
qDebug() << "import entities url";
bool success = false;
_entityClipboard->withWriteLock([&] {
_entityClipboard->eraseAllOctreeElements();
@ -4800,7 +4784,6 @@ void Application::registerScriptEngineWithApplicationServices(ScriptEngine* scri
}
bool Application::canAcceptURL(const QString& urlString) const {
qDebug() << "stepping through 'canAcceptURL'";
QUrl url(urlString);
if (urlString.startsWith(HIFI_URL_SCHEME)) {
return true;
@ -4817,7 +4800,6 @@ bool Application::canAcceptURL(const QString& urlString) const {
}
bool Application::acceptURL(const QString& urlString, bool defaultUpload) {
qDebug() << "stepping through 'acceptURL'";
if (urlString.startsWith(HIFI_URL_SCHEME)) {
// this is a hifi URL - have the AddressManager handle it
QMetaObject::invokeMethod(DependencyManager::get<AddressManager>().data(), "handleLookupString",
@ -4847,7 +4829,6 @@ void Application::setSessionUUID(const QUuid& sessionUUID) const {
}
bool Application::askToSetAvatarUrl(const QString& url) {
qDebug() << "setting avatar url";
QUrl realUrl(url);
if (realUrl.isLocalFile()) {
OffscreenUi::warning("", "You can not use local files for avatar components.");
@ -4904,7 +4885,6 @@ bool Application::askToSetAvatarUrl(const QString& url) {
bool Application::askToLoadScript(const QString& scriptFilenameOrURL) {
qDebug() << "setting script url";
QMessageBox::StandardButton reply;
QString shortName = scriptFilenameOrURL;
@ -4929,7 +4909,6 @@ bool Application::askToLoadScript(const QString& scriptFilenameOrURL) {
}
bool Application::askToWearAvatarAttachmentUrl(const QString& url) {
qDebug() << "setting avatar attachment url";
QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
QNetworkRequest networkRequest = QNetworkRequest(url);
networkRequest.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT);
@ -5011,7 +4990,6 @@ bool Application::displayAvatarAttachmentConfirmationDialog(const QString& name)
}
void Application::toggleRunningScriptsWidget() const {
qDebug() << "toggle running scripts url";
static const QUrl url("hifi/dialogs/RunningScripts.qml");
DependencyManager::get<OffscreenUi>()->show(url, "RunningScripts");
//if (_runningScriptsWidget->isVisible()) {
@ -5029,16 +5007,16 @@ void Application::toggleRunningScriptsWidget() const {
}
void Application::toggleAssetServerWidget(QString filePath) {
qDebug() << "toggle asset before if";
qDebug() << "toggle asset before if: " + filePath;
if (!DependencyManager::get<NodeList>()->getThisNodeCanWriteAssets()) {
return;
}
qDebug() << "toggle asset after if";
qDebug() << "toggle asset after if";
static const QUrl url { "AssetServer.qml" };
auto startUpload = [=](QQmlContext* context, QObject* newObject){
if (!filePath.isEmpty()) {
qDebug() << "file in toggle: " + filePath;
qDebug() << "file in toggle: " + filePath;
emit uploadRequest(filePath);
}
};
@ -5051,7 +5029,6 @@ void Application::packageModel() {
}
void Application::openUrl(const QUrl& url) const {
qDebug() << "open url";
if (!url.isEmpty()) {
if (url.scheme() == HIFI_URL_SCHEME) {
DependencyManager::get<AddressManager>()->handleLookupString(url.toString());
@ -5082,7 +5059,6 @@ void Application::setPreviousScriptLocation(const QString& location) {
}
void Application::loadScriptURLDialog() const {
qDebug() << "load script url dialog";
auto newScript = OffscreenUi::getText(nullptr, "Open and Run Script", "Script URL");
if (!newScript.isEmpty()) {
DependencyManager::get<ScriptEngines>()->loadScript(newScript);

View file

@ -250,7 +250,6 @@ public:
signals:
void svoImportRequested(const QString& url);
void zipImportRequested(const QString& url);
void fullAvatarURLChanged(const QString& newValue, const QString& modelName);
@ -386,7 +385,6 @@ private:
bool importJSONFromURL(const QString& urlString);
bool importSVOFromURL(const QString& urlString);
bool importZIPFromURL(const QString& urlString);
bool nearbyEntitiesAreReadyForPhysics();
int processOctreeStats(ReceivedMessage& message, SharedNodePointer sendingNode);

View file

@ -59,20 +59,6 @@ WindowScriptingInterface::WindowScriptingInterface() {
OffscreenUi::warning("Import SVO Error", "You need to be running edit.js to import entities.");
}
});
// attempt to start ZIP download project
connect(qApp, &Application::zipImportRequested, [this](const QString& urlString) {
qDebug() << "zip import has been requested";
static const QMetaMethod zipImportRequestedSignal =
QMetaMethod::fromSignal(&WindowScriptingInterface::zipImportRequested);
if (isSignalConnected(zipImportRequestedSignal)) {
QUrl url(urlString);
emit zipImportRequested(url.url());
} else {
OffscreenUi::warning("Import ZIP Error", "You need to be running edit.js to import entities.");
}
// end attempt
});
}
WebWindowClass* WindowScriptingInterface::doCreateWebWindow(const QString& title, const QString& url, int width, int height) {

View file

@ -58,7 +58,6 @@ public slots:
signals:
void domainChanged(const QString& domainHostname);
void svoImportRequested(const QString& url);
void zipImportRequested(const QString& url); // zip project
void domainConnectionRefused(const QString& reasonMessage, int reasonCode);
void snapshotTaken(const QString& path);

View file

@ -28,94 +28,97 @@
FileScriptingInterface::FileScriptingInterface(QObject* parent) : QObject(parent) {
// nothing for now
// nothing for now
}
void FileScriptingInterface::runUnzip(QString path, QUrl url) {
qDebug() << "Url that was downloaded: " + url.toString();
qDebug() << "Path where download is saved: " + path;
QString file = unzipFile(path);
if (file != "") {
qDebug() << "file to upload: " + file;
QUrl url = QUrl::fromLocalFile(file);
qDebug() << "url from local file: " + url.toString();
emit unzipSuccess(url.toString());
//Application::toggleAssetServerWidget(file);
} else {
qDebug() << "unzip failed";
}
qDebug() << "Url that was downloaded: " + url.toString();
qDebug() << "Path where download is saved: " + path;
QString file = unzipFile(path);
if (file != "") {
qDebug() << "file to upload: " + file;
QUrl url = QUrl::fromLocalFile(file);
qDebug() << "url from local file: " + url.toString();
emit unzipSuccess(url.toString());
} else {
qDebug() << "unzip failed";
}
}
bool FileScriptingInterface::testUrl(QUrl url) {
if (url.toString().contains(".zip") && url.toString().contains("fbx")) return true;
qDebug() << "This model is not a .fbx packaged in a .zip. Please try with another model.";
return false;
if (url.toString().contains(".zip") && url.toString().contains("fbx")) return true;
qDebug() << "This model is not a .fbx packaged in a .zip. Please try with another model.";
return false;
}
// this function is not in use
QString FileScriptingInterface::getTempDir() {
QTemporaryDir dir;
dir.setAutoRemove(false);
return dir.path();
// remember I must do something to delete this temp dir later
QTemporaryDir dir;
dir.setAutoRemove(false);
return dir.path();
// do something to delete this temp dir later
}
QString FileScriptingInterface::convertUrlToPath(QUrl url) {
QString newUrl;
QString oldUrl = url.toString();
newUrl = oldUrl.section("filename=", 1, 1);
qDebug() << "Filename should be: " + newUrl;
return newUrl;
QString newUrl;
QString oldUrl = url.toString();
newUrl = oldUrl.section("filename=", 1, 1);
qDebug() << "Filename should be: " + newUrl;
return newUrl;
}
// this function is not in use
void FileScriptingInterface::downloadZip(QString path, const QString link) {
QUrl url = QUrl(link);
auto request = ResourceManager::createResourceRequest(nullptr, url);
connect(request, &ResourceRequest::finished, this, [this, path]{
unzipFile(path);
});
QUrl url = QUrl(link);
auto request = ResourceManager::createResourceRequest(nullptr, url);
connect(request, &ResourceRequest::finished, this, [this, path]{
unzipFile(path);
});
request->send();
}
// clement's help :D
QString FileScriptingInterface::unzipFile(QString path) {
qDebug() << "Zip file was downloaded";
QDir dir(path);
QDir dir(path);
QString dirName = dir.path();
qDebug() << "Zip directory is stored at: " + dirName;
qDebug() << "Zip directory is stored at: " + dirName;
QString target = path.section("/", -1) + "/model_repo";
qDebug() << "Target path: " + target;
QStringList list = JlCompress::extractDir(dirName, "C:/Users/elisa/Downloads/test");
qDebug() << list;
qDebug() << list;
if (!list.isEmpty()) {
return list.front();
} else {
qDebug() << "Extraction failed";
return "";
}
if (!list.isEmpty()) {
return list.front();
} else {
qDebug() << "Extraction failed";
return "";
}
}
// this function is not in use
void FileScriptingInterface::recursiveFileScan(QFileInfo file, QString* dirName) {
/*if (!file.isDir()) {
qDebug() << "Regular file logged: " + file.fileName();
return;
}*/
/*if (!file.isDir()) {
qDebug() << "Regular file logged: " + file.fileName();
return;
}*/
QFileInfoList files;
if (file.fileName().contains(".zip")) {
qDebug() << "Extracting archive: " + file.fileName();
JlCompress::extractDir(file.fileName());
if (file.fileName().contains(".zip")) {
qDebug() << "Extracting archive: " + file.fileName();
JlCompress::extractDir(file.fileName());
}
files = file.dir().entryInfoList();
/*if (files.empty()) {
files = JlCompress::getFileList(file.fileName());
}*/
/*if (files.empty()) {
files = JlCompress::getFileList(file.fileName());
}*/
foreach (QFileInfo file, files) {
foreach (QFileInfo file, files) {
qDebug() << "Looking into file: " + file.fileName();
recursiveFileScan(file, dirName);
}
}
return;
}

View file

@ -22,24 +22,24 @@ class FileScriptingInterface : public QObject {
public:
FileScriptingInterface(QObject* parent);
//void runUnzip(QString path, QString importURL);
QString getTempDir();
//void runUnzip(QString path, QString importURL);
QString getTempDir();
public slots:
//void unzipFile(QString path);
bool testUrl(QUrl url);
QString convertUrlToPath(QUrl url);
void runUnzip(QString path, QUrl url);
//void unzipFile(QString path);
bool testUrl(QUrl url);
QString convertUrlToPath(QUrl url);
void runUnzip(QString path, QUrl url);
signals:
void unzipSuccess(QString url);
void unzipSuccess(QString url);
private:
//void downloadZip();
QString unzipFile(QString path);
void recursiveFileScan(QFileInfo file, QString* dirName);
void downloadZip(QString path, const QString link);
//void downloadZip();
QString unzipFile(QString path);
void recursiveFileScan(QFileInfo file, QString* dirName);
void downloadZip(QString path, const QString link);
};

View file

@ -503,7 +503,6 @@ void ScriptEngine::init() {
registerGlobalObject("Uuid", &_uuidLibrary);
registerGlobalObject("Messages", DependencyManager::get<MessagesClient>().data());
// unzip project
registerGlobalObject("File", new FileScriptingInterface(this));
qScriptRegisterMetaType(this, animVarMapToScriptValue, animVarMapFromScriptValue);

View file

@ -78,20 +78,17 @@ class UrlHandler : public QObject {
Q_OBJECT
public:
Q_INVOKABLE bool canHandleUrl(const QString& url) {
qDebug() << Q_FUNC_INFO;
static auto handler = dynamic_cast<AbstractUriHandler*>(qApp);
return handler->canAcceptURL(url);
}
Q_INVOKABLE bool handleUrl(const QString& url) {
qDebug() << Q_FUNC_INFO;
static auto handler = dynamic_cast<AbstractUriHandler*>(qApp);
return handler->acceptURL(url);
}
// FIXME hack for authentication, remove when we migrate to Qt 5.6
Q_INVOKABLE QString fixupUrl(const QString& originalUrl) {
qDebug() << Q_FUNC_INFO;
return fixupHifiUrl(originalUrl);
}
};

View file

@ -1205,22 +1205,6 @@ function importSVO(importURL) {
}
Window.svoImportRequested.connect(importSVO);
// attempt to start ZIP download project, GET RID OF THIS
function importZIP(importURL) {
print("Import ZIP requested: " + importURL);
/*if (!Entities.canAdjustLocks()) {
Window.alert(INSUFFICIENT_PERMISSIONS_IMPORT_ERROR_MSG);
return;
}*/
var path = File.getTempDir();
print("Temporary path to zip: " + path);
File.runUnzip(path, importURL);
}
Window.zipImportRequested.connect(importZIP);
//end attempt
Menu.menuItemEvent.connect(handeMenuEvent);
Controller.keyPressEvent.connect(function (event) {