mirror of
https://github.com/overte-org/overte.git
synced 2025-08-16 07:11:37 +02:00
Zip download works
This commit is contained in:
parent
ab02d16eb7
commit
c36fc29363
5 changed files with 82 additions and 37 deletions
|
@ -140,6 +140,7 @@ ScrollingWindow {
|
|||
}
|
||||
}
|
||||
|
||||
<<<<<<< ab02d16eb7656ea0a37785a398716fad62dff045
|
||||
Rectangle {
|
||||
id:permissionsContainer
|
||||
visible:false
|
||||
|
@ -196,6 +197,8 @@ ScrollingWindow {
|
|||
}
|
||||
}
|
||||
}
|
||||
=======
|
||||
>>>>>>> Zip download works
|
||||
|
||||
WebEngineView {
|
||||
id: webview
|
||||
|
@ -220,35 +223,15 @@ ScrollingWindow {
|
|||
}
|
||||
|
||||
Component.onCompleted: {
|
||||
webview.profile.downloadRequested.connect(function(download){
|
||||
if (download.state === WebEngineDownloadItem.DownloadRequested) {
|
||||
console.log("Download start: " + download.state)
|
||||
download.accept()
|
||||
console.log("Download accept: " + download.state)
|
||||
if (download.state === WebEngineDownloadItem.DownloadInterrupted) {
|
||||
console.log("Download? " + download.state)
|
||||
console.log("download failed to complete")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
webview.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)
|
||||
} else {
|
||||
console.log("The download was corrupted")
|
||||
}
|
||||
})
|
||||
desktop.initWebviewProfileHandlers(webview.profile)
|
||||
}
|
||||
|
||||
|
||||
//profile: desktop.browserProfile
|
||||
}
|
||||
|
||||
} // item
|
||||
|
||||
|
||||
|
||||
Keys.onPressed: {
|
||||
switch(event.key) {
|
||||
case Qt.Key_L:
|
||||
|
|
|
@ -69,9 +69,14 @@ WebEngineView {
|
|||
}
|
||||
|
||||
onLinkHovered: {
|
||||
console.log(hoveredUrl);
|
||||
desktop.currentUrl = hoveredUrl
|
||||
console.log("my url in WebView: " + desktop.currentUrl)
|
||||
}
|
||||
|
||||
//onLinkHovered: {
|
||||
// console.log(hoveredUrl);
|
||||
//}
|
||||
|
||||
// This breaks the webchannel used for passing messages. Fixed in Qt 5.6
|
||||
// See https://bugreports.qt.io/browse/QTBUG-49521
|
||||
//profile: desktop.browserProfile
|
||||
|
|
|
@ -71,6 +71,45 @@ 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;
|
||||
|
||||
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")
|
||||
}
|
||||
})
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Create or fetch a toolbar with the given name
|
||||
function getToolbar(name) {
|
||||
var result = toolbars[name];
|
||||
|
|
|
@ -31,9 +31,15 @@ FileScriptingInterface::FileScriptingInterface(QObject* parent) : QObject(parent
|
|||
// nothing for now
|
||||
}
|
||||
|
||||
void FileScriptingInterface::runUnzip(QString path) {
|
||||
//downloadZip(path, importURL);
|
||||
void FileScriptingInterface::runUnzip(QString path, QUrl url) {
|
||||
qDebug() << "Url that was downloaded: " + url.toString();
|
||||
qDebug() << "Path where download is saved: " + path;
|
||||
unzipFile(path);
|
||||
}
|
||||
|
||||
bool FileScriptingInterface::testUrl(QUrl url) {
|
||||
if (url.toString().contains(".zip")) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
QString FileScriptingInterface::getTempDir() {
|
||||
|
@ -43,6 +49,14 @@ QString FileScriptingInterface::getTempDir() {
|
|||
// remember I must 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;
|
||||
}
|
||||
|
||||
void FileScriptingInterface::downloadZip(QString path, const QString link) {
|
||||
QUrl url = QUrl(link);
|
||||
auto request = ResourceManager::createResourceRequest(nullptr, url);
|
||||
|
@ -54,15 +68,15 @@ void FileScriptingInterface::downloadZip(QString path, const QString link) {
|
|||
|
||||
// clement's help :D
|
||||
void FileScriptingInterface::unzipFile(QString path) {
|
||||
ResourceRequest* request = qobject_cast<ResourceRequest*>(sender());
|
||||
QUrl url = request->getUrl();
|
||||
//ResourceRequest* request = qobject_cast<ResourceRequest*>(sender());
|
||||
//QUrl url = request->getUrl();
|
||||
|
||||
if (request->getResult() == ResourceRequest::Success) {
|
||||
//if (request->getResult() == ResourceRequest::Success) {
|
||||
qDebug() << "Zip file was downloaded";
|
||||
QDir dir(path);
|
||||
QByteArray compressedFileContent = request->getData(); // <-- Downloaded file is in here
|
||||
QBuffer buffer(&compressedFileContent);
|
||||
buffer.open(QIODevice::ReadOnly);
|
||||
//QByteArray compressedFileContent = request->getData(); // <-- Downloaded file is in here
|
||||
//QBuffer buffer(&compressedFileContent);
|
||||
//buffer.open(QIODevice::ReadOnly);
|
||||
|
||||
//QString zipFileName = QFile::decodeName(compressedFileContent);
|
||||
QString dirName = dir.path();
|
||||
|
@ -104,10 +118,10 @@ void FileScriptingInterface::unzipFile(QString path) {
|
|||
}*/
|
||||
|
||||
|
||||
buffer.close();
|
||||
} else {
|
||||
qDebug() << "Could not download the zip file";
|
||||
}
|
||||
//buffer.close();
|
||||
//} else {
|
||||
// qDebug() << "Could not download the zip file";
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -21,12 +21,16 @@ class FileScriptingInterface : public QObject {
|
|||
|
||||
public:
|
||||
FileScriptingInterface(QObject* parent);
|
||||
|
||||
//void runUnzip(QString path, QString importURL);
|
||||
QString getTempDir();
|
||||
|
||||
|
||||
public slots:
|
||||
//void unzipFile(QString path);
|
||||
void runUnzip(QString path);
|
||||
bool testUrl(QUrl url);
|
||||
QString convertUrlToPath(QUrl url);
|
||||
void runUnzip(QString path, QUrl url);
|
||||
|
||||
signals:
|
||||
|
||||
|
|
Loading…
Reference in a new issue