Xdg-open or gvfs-open [QDesktopServices::openUrl("file:///somelocation/file.txt")] wont open the file

I had a go at building Qt 5.15.2 from source with the O_RDONLY change applied as a patch and to be bundled into the yuzu snap. Yuzu has a menu item to “Open yuzu folder”, which behind the scenes makes use of the QDesktopServices::openUrl(QUrl url) function with the argument passed in being a file:// URI pointing to a directory. I didn’t have any luck with getting the portal to open a folder for me with just the O_RDONLY change. I managed to get the functionality I expected by altering the writable option to be set to false :

diff --git a/src/platformsupport/services/genericunix/qgenericunixservices.cpp b/src/platformsupport/services/genericunix/qgenericunixservices.cpp
index 1a3cab275d..80c12159fc 100644
--- a/src/platformsupport/services/genericunix/qgenericunixservices.cpp
+++ b/src/platformsupport/services/genericunix/qgenericunixservices.cpp
@@ -203,8 +203,8 @@ static inline QDBusMessage xdgDesktopPortalOpenFile(const QUrl &url)
     // 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);
+#ifdef O_RDONLY
+    const int fd = qt_safe_open(QFile::encodeName(url.toLocalFile()), O_RDONLY);
     if (fd != -1) {
         QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.portal.Desktop"),
                                                               QLatin1String("/org/freedesktop/portal/desktop"),
@@ -214,7 +214,7 @@ static inline QDBusMessage xdgDesktopPortalOpenFile(const QUrl &url)
         QDBusUnixFileDescriptor descriptor;
         descriptor.giveFileDescriptor(fd);
 
-        const QVariantMap options = {{QLatin1String("writable"), true}};
+        const QVariantMap options = {{QLatin1String("writable"), false}};
 
         // FIXME parent_window_id
         message << QString() << QVariant::fromValue(descriptor) << options;

which had the result of opening a nautilus window at the correct directory. I also used this patch on another snap (modified source code from the application on this forum post QT File Dialog (open folder) and KDE-NEON Extension made available on the store under test-openfolder-qt-2 if anyone wants to test) which used a file URI pointing to an image file, which resulted in having to pick from a list of suitable applications.

Alternatively I also experimented with the following patch, which changes the writable option to be ask:

diff --git a/src/platformsupport/services/genericunix/qgenericunixservices.cpp b/src/platformsupport/services/genericunix/qgenericunixservices.cpp
index 1a3cab275d..64bf2d3f3b 100644
--- a/src/platformsupport/services/genericunix/qgenericunixservices.cpp
+++ b/src/platformsupport/services/genericunix/qgenericunixservices.cpp
@@ -203,8 +203,8 @@ static inline QDBusMessage xdgDesktopPortalOpenFile(const QUrl &url)
     // 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);
+#ifdef O_RDONLY
+    const int fd = qt_safe_open(QFile::encodeName(url.toLocalFile()), O_RDONLY);
     if (fd != -1) {
         QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.portal.Desktop"),
                                                               QLatin1String("/org/freedesktop/portal/desktop"),
@@ -214,7 +214,7 @@ static inline QDBusMessage xdgDesktopPortalOpenFile(const QUrl &url)
         QDBusUnixFileDescriptor descriptor;
         descriptor.giveFileDescriptor(fd);
 
-        const QVariantMap options = {{QLatin1String("writable"), true}};
+        const QVariantMap options = {{QLatin1String("ask"), true}};
 
         // FIXME parent_window_id
         message << QString() << QVariant::fromValue(descriptor) << options;

This patch also allowed me to open the directory but presented me with the choice to pick from a list of suitable applications beforehand.

I won’t pretend to have in depth knowledge into portal and it’s OpenURI API, so I don’t know if writable = true is meant be the correct way to handle opening file URIs, but there’s a bug with the Qt implementation? Hopefully someone with more knowledge on this can comment. I created my patches based on the portal documentation/API reference located here https://flatpak.github.io/xdg-desktop-portal/portal-docs.html#gdbus-method-org-freedesktop-portal-OpenURI.OpenFile

Would be nice to have this functionality fixed in Qt from the apt repos/kde-neon extension, at present I’m having to provide instructions to users to navigate to the directory manually which confuses non tech-savvy users unfamiliar with hidden directories, and the snap directories living in a different location from what they are used to.