help using KIO to download files in Konqueror

Stefano Crocco stefano.crocco at alice.it
Tue Aug 22 16:21:31 BST 2017


Hello to everyone,
I'd need a bit of help to solve an issue I run into while attempting to fix 
Konqueror so that downloads work correctly using QWebEngine.

The issue regards a situation when you try to download a file which is created 
by a server-side script. It doesn't happen for all such downloads; actually, I 
only found one site causing this behaviour: unfortunately, it's the web site of 
my bank so, of course I can't give you the password to try my code.

The issue is the following: I use KParts::BrowserOpenOrSaveQuestion to ask the 
user whether he wants to save or open the file, then use either KIO::copy or 
KRun, according to the user choice, to download or open the file. In both 
cases, the code is mostly copied from the KDEWebKit framework (in particular, 
from kwebpage.cpp, lines ...). In both cases, however, what is downloaded is not 
the file produced by the script on the server (in this case, a PDF) but the 
script itself. What I can't understand is that using KWebKitPart (which means 
the KDEWebKit framework, whose code I almost copied) produces the correct 
result, downloading or opening the PDF file. 

I tried looking at differences between my code and KWebPage and reading all 
related documentation, but I couldn't find anything which could explain the 
different behaviours. Can someone point me in the right direction?

Here are the relevant lines of code (when saving the file)

//From kwebenginepartdownloadmanager.cpp (my code):
bool WebEnginePartDownloadManager::downloadAndSave(QWebEngineDownloadItem* it, WebEnginePage* page)
{
    QString fileName = QDir(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)).filePath(it->path());
    QUrl destUrl = QUrl::fromLocalFile(QFileDialog::getSaveFileName(page->view(), QString(),fileName));
    if(!destUrl.isValid()) return false;

    KIO::Job *job = KIO::copy(it->url(), destUrl);

    job->addMetaData(QLatin1String("MaxCacheSize"), QLatin1String("0")); //Don't store in http cache.
    job->addMetaData(QLatin1String("cache"), QLatin1String("cache")); // Use entry from cache if available.
    KJobWidgets::setWindow(job, page->view());
    job->uiDelegate()->setAutoErrorHandlingEnabled(true);
    return true; 
}

//From kwebpage.cpp (from KDEWebKit framework)
static bool downloadResource(const QUrl &srcUrl, const QString &suggestedName = QString(), QWidget *parent = nullptr, const KIO::MetaData &metaData = KIO::MetaData())
{
    const QString fileName = suggestedName.isEmpty() ? srcUrl.fileName() : suggestedName;
    // convert filename to URL using fromPath to avoid trouble with ':' in filenames (#184202)
    QUrl destUrl = QUrl::fromLocalFile(QFileDialog::getSaveFileName(parent, QString(), fileName));
    if (!destUrl.isValid()) {
        return false;
    }

    // Using KIO::copy rather than file_copy, to benefit from "dest already exists" dialogs.
    KIO::Job *job = KIO::copy(srcUrl, destUrl);

    if (!metaData.isEmpty()) {
        job->setMetaData(metaData);
    }

    job->addMetaData(QL1S("MaxCacheSize"), QL1S("0")); // Don't store in http cache.
    job->addMetaData(QL1S("cache"), QL1S("cache")); // Use entry from cache if available.
    KJobWidgets::setWindow(job, parent ? parent->window() : nullptr);
    job->ui()->setAutoErrorHandlingEnabled(true);
    return true;
}

In case they're useful, I attach both files. The functions I copied above are 
called respectively from line 97 of webenginepartdownloadmanager.cpp and line 
608 of kwebpage.cpp.

Thanks in advance for any help

Stefano
-------------- next part --------------
A non-text attachment was scrubbed...
Name: webenginepartdownloadmanager.cpp
Type: text/x-c++src
Size: 6253 bytes
Desc: not available
URL: <https://mail.kde.org/mailman/private/kfm-devel/attachments/20170822/2bb5c2f0/attachment.cpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: kwebpage.cpp
Type: text/x-c++src
Size: 23309 bytes
Desc: not available
URL: <https://mail.kde.org/mailman/private/kfm-devel/attachments/20170822/2bb5c2f0/attachment-0001.cpp>


More information about the kfm-devel mailing list