From bc9f3cf33912e3f71fb221d0671b1b579ffad8b5 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Mon, 13 May 2024 07:32:39 +0300 Subject: [PATCH] kinit: download remote URLs for services lacking support for such now klauncher has all required features (by the spec) and then some Signed-off-by: Ivailo Monev --- kinit/klauncher_adaptor.cpp | 42 +++++++++++++++++++++++++------------ kio/kio/krun.cpp | 1 - kio/kio/krun.h | 1 + 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/kinit/klauncher_adaptor.cpp b/kinit/klauncher_adaptor.cpp index eff83b19..5824b238 100644 --- a/kinit/klauncher_adaptor.cpp +++ b/kinit/klauncher_adaptor.cpp @@ -309,17 +309,6 @@ bool KLauncherAdaptor::start_service_by_storage_id(const QString &serviceName, removeTemp(temp, urls); return false; } - const QString kserviceexec = kservice->exec(); - if (!kserviceexec.contains(QLatin1String("%u")) && !kserviceexec.contains(QLatin1String("%U"))) { - foreach (const QString &url, urls) { - if (!KUrl(url).isLocalFile()) { - kError() << "service does not support remote" << serviceName; - showError(i18n("Service does not support remote URLs: %1", serviceName), window); - removeTemp(temp, urls); - return false; - } - } - } if (urls.size() > 1 && !kservice->allowMultipleFiles()) { kWarning() << "service does not support multiple files" << serviceName; bool result = true; @@ -331,7 +320,6 @@ bool KLauncherAdaptor::start_service_by_storage_id(const QString &serviceName, } return result; } - // TODO: for applications which do not support URLs - download QStringList programandargs = KRun::processDesktopExec(*kservice, urls); if (programandargs.isEmpty()) { kError() << "could not process service" << kservice->entryPath(); @@ -343,8 +331,36 @@ bool KLauncherAdaptor::start_service_by_storage_id(const QString &serviceName, if (programworkdir.isEmpty()) { programworkdir = QDir::homePath(); } - kDebug() << "starting" << kservice->entryPath() << urls; const QString program = programandargs.takeFirst(); + const QString kserviceexec = kservice->exec(); + if (!kserviceexec.contains(QLatin1String("%u")) && !kserviceexec.contains(QLatin1String("%U"))) { + kDebug() << "service does not support remote" << serviceName; + QStringList downloaded; + for (int i = 0; i < programandargs.size(); i++) { + const QString url = programandargs.at(i); + const KUrl realurl = KUrl(url); + if (!realurl.isLocalFile()) { + // remote URLs should not be passed along with temporary files + Q_ASSERT(!temp); + kDebug() << "downloading" << url; + QString urldestination; + const QString prettyurl = realurl.prettyUrl(); + if (!KIO::NetAccess::download(realurl, urldestination, findWindow(window))) { + kError() << "could not download" << prettyurl; + showError(i18n("Could not download URL: %1", Qt::escape(prettyurl)), window); + removeTemp(temp, urls); + removeTemp(true, downloaded); + return false; + } + kDebug() << "downloaded" << prettyurl << "to" << urldestination; + downloaded.append(urldestination); + programandargs[i] = urldestination; + } + } + kDebug() << "starting" << kservice->entryPath() << urls; + return startProgram(program, programandargs, envs, window, true, programworkdir, m_startuptimeout, kservice); + } + kDebug() << "starting" << kservice->entryPath() << urls; return startProgram(program, programandargs, envs, window, temp, programworkdir, m_startuptimeout, kservice); } diff --git a/kio/kio/krun.cpp b/kio/kio/krun.cpp index 55491e1c..2e7c6581 100644 --- a/kio/kio/krun.cpp +++ b/kio/kio/krun.cpp @@ -45,7 +45,6 @@ bool KRun::displayOpenWithDialog(const KUrl::List &urls, QWidget *window, bool t return false; } -// TODO: this needs a complete rewrite to handle remote URLs QStringList KRun::processDesktopExec(const KService &service, const QStringList &urls) { QStringList args = KShell::splitArgs(service.exec()); diff --git a/kio/kio/krun.h b/kio/kio/krun.h index adfb5133..7bbd7eb0 100644 --- a/kio/kio/krun.h +++ b/kio/kio/krun.h @@ -50,6 +50,7 @@ public: * @param service the service to extract information from * @param urls the urls the service should open * @return a list of arguments suitable for KProcess::setProgram() + * @warning caller is responsible for handling services which do not support remote URLs */ static QStringList processDesktopExec(const KService &service, const QStringList &urls);