From a988930dbccd7bb122cbed51190cf1134aade4ca Mon Sep 17 00:00:00 2001 From: Levi Neely <141506390+lneely@users.noreply.github.com> Date: Wed, 8 Jul 2026 19:33:48 +0200 Subject: [PATCH] fix: use xdg-desktop-portal for folder dialog in plasmoid plasmashell is QGuiApplication so QFileDialog (QtWidgets) cannot work. Use org.freedesktop.portal.FileChooser.OpenFile via D-Bus instead. Also fix StandardPaths ReferenceError in main.qml. --- CMakeLists.txt | 2 -- plasmoid/contents/ui/main.qml | 2 +- plasmoid/plugin/olliedbusclient.cpp | 55 +++++++++++++++++++++++++---- plasmoid/plugin/olliedbusclient.h | 1 + 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 93715e3..2a65764 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,7 +74,6 @@ if(OLLIE_KF5) target_link_libraries(ollie-plasmoid-plugin Qt5::Core Qt5::DBus - Qt5::Widgets Qt5::Quick Qt5::Qml ) @@ -148,7 +147,6 @@ else() target_link_libraries(ollie-plasmoid-plugin PRIVATE Qt6::Core Qt6::DBus - Qt6::Widgets Qt6::Quick ) target_include_directories(ollie-plasmoid-plugin PRIVATE diff --git a/plasmoid/contents/ui/main.qml b/plasmoid/contents/ui/main.qml index 8424cb9..fd493bd 100644 --- a/plasmoid/contents/ui/main.qml +++ b/plasmoid/contents/ui/main.qml @@ -17,7 +17,7 @@ PlasmoidItem { FolderDialog { id: folderDialog title: "Choose working directory" - currentFolder: dbus.activeCwd ? "file://" + dbus.activeCwd : StandardPaths.writableLocation(StandardPaths.HomeLocation) + currentFolder: dbus.activeCwd ? "file://" + dbus.activeCwd : "" onAccepted: { let path = selectedFolder.toString().replace("file://", "") dbus.setConfig(dbus.activeSessionId, "cwd", path) diff --git a/plasmoid/plugin/olliedbusclient.cpp b/plasmoid/plugin/olliedbusclient.cpp index 0e30090..7d9b70e 100644 --- a/plasmoid/plugin/olliedbusclient.cpp +++ b/plasmoid/plugin/olliedbusclient.cpp @@ -5,7 +5,8 @@ #include #include #include -#include +#include + static const char *SERVICE = "org.ollie.SessionManager"; static const char *OBJ_PATH = "/org/ollie/SessionManager"; @@ -230,12 +231,52 @@ QString OllieDBusClient::createSession(const QString &cwd) void OllieDBusClient::createSessionWithDialog() { - QString startDir = m_activeCwd.isEmpty() ? QDir::homePath() : m_activeCwd; - QString dir = QFileDialog::getExistingDirectory(nullptr, - tr("Choose working directory for new session"), startDir, - QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); - if (!dir.isEmpty()) { - createSession(dir); + // Use xdg-desktop-portal FileChooser — plasmashell is QGuiApplication, + // so QFileDialog (QtWidgets) cannot be used. + QDBusMessage msg = QDBusMessage::createMethodCall( + "org.freedesktop.portal.Desktop", + "/org/freedesktop/portal/desktop", + "org.freedesktop.portal.FileChooser", + "OpenFile"); + + QString parentWindow; // empty — no parent window handle in plasmoid context + QString title = "Choose working directory for new session"; + QVariantMap options; + options["directory"] = true; + options["modal"] = true; + + msg << parentWindow << title << options; + + QDBusPendingCall pending = QDBusConnection::sessionBus().asyncCall(msg); + auto *watcher = new QDBusPendingCallWatcher(pending, this); + connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *w) { + w->deleteLater(); + QDBusPendingReply reply = *w; + if (reply.isError()) return; + + // Connect to the Response signal on the request object + QString requestPath = reply.value().path(); + QDBusConnection::sessionBus().connect( + "org.freedesktop.portal.Desktop", + requestPath, + "org.freedesktop.portal.Request", + "Response", + this, + SLOT(onPortalFolderResponse(uint,QVariantMap))); + }); +} + +void OllieDBusClient::onPortalFolderResponse(uint response, const QVariantMap &results) +{ + if (response != 0) return; // 0 = success, 1 = cancelled, 2 = other + + QStringList uris = results.value("uris").toStringList(); + if (uris.isEmpty()) return; + + // Portal returns file:// URIs + QString path = QUrl(uris.first()).toLocalFile(); + if (!path.isEmpty()) { + createSession(path); } } diff --git a/plasmoid/plugin/olliedbusclient.h b/plasmoid/plugin/olliedbusclient.h index 73921c7..fc9eba0 100644 --- a/plasmoid/plugin/olliedbusclient.h +++ b/plasmoid/plugin/olliedbusclient.h @@ -63,6 +63,7 @@ private slots: void onRefreshSessionsFinished(QDBusPendingCallWatcher *watcher); void onFetchChatFinished(QDBusPendingCallWatcher *watcher); void onFetchConfigFinished(QDBusPendingCallWatcher *watcher); + void onPortalFolderResponse(uint response, const QVariantMap &results); private: QDBusInterface *m_iface;