Commit 4f02e435 authored by Stefano Crocco's avatar Stefano Crocco
Browse files

Attempt to enable embedding/opening blob URL

parent e66e1d6b
......@@ -35,6 +35,8 @@
#include <KLocalizedString>
#include <KNotificationJobUiDelegate>
#include <KParts/BrowserOpenOrSaveQuestion>
#include <KIO/OpenUrlJob>
WebEnginePartDownloadManager::WebEnginePartDownloadManager()
: QObject()
......@@ -100,6 +102,28 @@ void WebEnginePartDownloadManager::performDownload(QWebEngineDownloadItem* it)
}
void WebEnginePartDownloadManager::downloadBlob(QWebEngineDownloadItem* it)
{
QWidget *w = it->page() ? it->page()->view() : nullptr;
KParts::BrowserOpenOrSaveQuestion askDlg(w, it->url(), it->mimeType());
KParts::BrowserOpenOrSaveQuestion::Result ans = askDlg.askEmbedOrSave();
qDebug() << "ANSWER:" << ans;
switch (ans) {
case KParts::BrowserOpenOrSaveQuestion::Cancel:
qDebug() << "CANCEL";
it->cancel();
return;
case KParts::BrowserOpenOrSaveQuestion::Save:
qDebug() << "SAVE";
saveBlob(it);
break;
case KParts::BrowserOpenOrSaveQuestion::Embed:
case KParts::BrowserOpenOrSaveQuestion::Open:
openBlob(it);
break;
}
}
void WebEnginePartDownloadManager::saveBlob(QWebEngineDownloadItem* it)
{
QWidget *w = it->page() ? it->page()->view() : nullptr;
QString downloadDir = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
......@@ -131,6 +155,46 @@ void WebEnginePartDownloadManager::downloadBlob(QWebEngineDownloadItem* it)
j->start();
}
void WebEnginePartDownloadManager::embedBlob(QWebEngineDownloadItem* it)
{
//TODO: implement me
}
void WebEnginePartDownloadManager::openBlob(QWebEngineDownloadItem* it)
{
qDebug() << "OPEN BLOB";
QMimeDatabase db;
QMimeType type = db.mimeTypeForName(it->mimeType());
int i = 0;
QDir tmpDir(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
QString nameTemplate = "konqueror-%1-%2.%3";
QString fileName;
bool validName = false;
while (!validName) {
++i;
fileName = nameTemplate.arg(QTime::currentTime().msecsSinceStartOfDay()).arg(i).arg(type.preferredSuffix());
validName = !tmpDir.exists(fileName);
}
qDebug() << "TEMP FILE NAME" << fileName;
it->setDownloadDirectory(tmpDir.path());
it->setDownloadFileName(fileName);
connect(it, &QWebEngineDownloadItem::finished, this, [this, it](){blobDownloadedToFile(it);});
qDebug() << "ACCEPTING DOWNLOAD";
it->accept();
}
void WebEnginePartDownloadManager::blobDownloadedToFile(QWebEngineDownloadItem *it)
{
qDebug() << "DOWNLOAD FINISHED. ABOUT TO START JOB";
QString file = QDir(it->downloadDirectory()).filePath(it->downloadFileName());
KIO::OpenUrlJob *j = new KIO::OpenUrlJob(QUrl::fromLocalFile(file), it->mimeType(), this);
j->start();
j->setDeleteTemporaryFile(true);
qDebug() << "JOB STARTED";
}
#ifndef DOWNLOADITEM_KNOWS_PAGE
void WebEnginePartDownloadManager::recordNavigationRequest(WebEnginePage *page, const QUrl& url)
......
......@@ -29,6 +29,7 @@
#include <KJob>
class WebEnginePage;
class QFile;
class WebEnginePartDownloadManager : public QObject
{
......@@ -49,6 +50,10 @@ public Q_SLOTS:
private Q_SLOTS:
void performDownload(QWebEngineDownloadItem *it);
void saveBlob(QWebEngineDownloadItem *it);
void embedBlob(QWebEngineDownloadItem *it);
void openBlob(QWebEngineDownloadItem *it);
void blobDownloadedToFile(QWebEngineDownloadItem *it);
#ifndef DOWNLOADITEM_KNOWS_PAGE
private:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment