From 369e7572bac94bb5b83ef881012fdc4a86829496 Mon Sep 17 00:00:00 2001 From: Jan Grulich Date: Thu, 12 Apr 2018 10:56:47 +0200 Subject: Add support for opening files in OpenURI flatpak portal Adds support for opening files in applications outside sandbox. To prove we have access to given file, we have to pass it as file descriptor. The user then gets a dialog with list of application which can be used to open given file. Change-Id: Ifb4cf3dece15d0792b8eb1b90a240da2d3f3c89e Reviewed-by: Thiago Macieira --- .../services/genericunix/qgenericunixservices.cpp | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/platformsupport/services/genericunix') diff --git a/src/platformsupport/services/genericunix/qgenericunixservices.cpp b/src/platformsupport/services/genericunix/qgenericunixservices.cpp index cb1e367b9f..adfa3121e0 100644 --- a/src/platformsupport/services/genericunix/qgenericunixservices.cpp +++ b/src/platformsupport/services/genericunix/qgenericunixservices.cpp @@ -177,6 +177,39 @@ static inline bool checkRunningUnderFlatpak() return !QStandardPaths::locate(QStandardPaths::RuntimeLocation, QLatin1String("flatpak-info")).isEmpty(); } +static inline bool flatpakOpenFile(const QUrl &url) +{ + // DBus signature: + // OpenFile (IN s parent_window, + // IN h fd, + // IN a{sv} options, + // OUT o handle) + // Options: + // handle_token (s) - A string that will be used as the last element of the @handle. + // writable (b) - Whether to allow the chosen application to write to the file. + +#ifdef O_PATH + const int fd = qt_safe_open(QFile::encodeName(url.toLocalFile()), O_PATH); + if (fd != -1) { + QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.portal.Desktop"), + QLatin1String("/org/freedesktop/portal/desktop"), + QLatin1String("org.freedesktop.portal.OpenURI"), + QLatin1String("OpenFile")); + + QDBusUnixFileDescriptor descriptor(fd); + qt_safe_close(fd); + + // FIXME parent_window_id and handle writable option + message << QString() << QVariant::fromValue(descriptor) << QVariantMap(); + + QDBusPendingReply reply = QDBusConnection::sessionBus().call(message); + return !reply.isError(); + } +#endif + + return false; +} + static inline bool flatpakOpenUrl(const QUrl &url) { // DBus signature: @@ -185,6 +218,7 @@ static inline bool flatpakOpenUrl(const QUrl &url) // IN a{sv} options, // OUT o handle) // Options: + // handle_token (s) - A string that will be used as the last element of the @handle. // writable (b) - Whether to allow the chosen application to write to the file. // This key only takes effect the uri points to a local file that is exported in the document portal, // and the chosen application is sandboxed itself. @@ -278,6 +312,11 @@ bool QGenericUnixServices::openUrl(const QUrl &url) bool QGenericUnixServices::openDocument(const QUrl &url) { +#if QT_CONFIG(dbus) + if (checkRunningUnderFlatpak()) + return flatpakOpenFile(url); +#endif + if (m_documentLauncher.isEmpty() && !detectWebBrowser(desktopEnvironment(), false, &m_documentLauncher)) { qWarning("Unable to detect a launcher for '%s'", qPrintable(url.toString())); return false; -- cgit v1.2.3