Files
asteria-qt/src/backend/PortalFileDialogService.cpp
T

152 lines
4.7 KiB
C++

#ifdef HAS_QT_DBUS
#include "PortalFileDialogService.h"
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusMetaType>
#include <QRandomGenerator>
#include <QUrl>
// Typed structs so Qt can derive D-Bus signatures without reading a
// write-only QDBusArgument (which causes a SIGSEGV).
struct PortalGlob {
uint type; // 0 = glob pattern
QString pattern;
};
Q_DECLARE_METATYPE(PortalGlob)
QDBusArgument &operator<<(QDBusArgument &a, const PortalGlob &g)
{
a.beginStructure();
a << g.type << g.pattern;
a.endStructure();
return a;
}
const QDBusArgument &operator>>(const QDBusArgument &a, PortalGlob &g)
{
a.beginStructure();
a >> g.type >> g.pattern;
a.endStructure();
return a;
}
struct PortalFilter {
QString name;
QList<PortalGlob> globs;
};
Q_DECLARE_METATYPE(PortalFilter)
QDBusArgument &operator<<(QDBusArgument &a, const PortalFilter &f)
{
a.beginStructure();
a << f.name << f.globs;
a.endStructure();
return a;
}
const QDBusArgument &operator>>(const QDBusArgument &a, PortalFilter &f)
{
a.beginStructure();
a >> f.name >> f.globs;
a.endStructure();
return a;
}
static void registerPortalTypes()
{
static bool done = false;
if (done) return;
done = true;
qDBusRegisterMetaType<PortalGlob>();
qDBusRegisterMetaType<QList<PortalGlob>>();
qDBusRegisterMetaType<PortalFilter>();
qDBusRegisterMetaType<QList<PortalFilter>>();
}
PortalFileDialogService::PortalFileDialogService(QObject *parent)
: IFileDialogService(parent)
{
}
void PortalFileDialogService::open(const QStringList &extensions)
{
auto bus = QDBusConnection::sessionBus();
// Generate a unique handle token and compute the expected response path
QString token = QStringLiteral("asteria_")
+ QString::number(QRandomGenerator::global()->generate());
QString sender = bus.baseService();
sender.remove(0, 1); // strip leading ':'
sender.replace(QLatin1Char('.'), QLatin1Char('_'));
m_portalHandlePath = QStringLiteral("/org/freedesktop/portal/desktop/request/%1/%2")
.arg(sender, token);
// Connect to the Response signal BEFORE making the call (avoids race)
bus.connect(QStringLiteral("org.freedesktop.portal.Desktop"),
m_portalHandlePath,
QStringLiteral("org.freedesktop.portal.Request"),
QStringLiteral("Response"),
this,
SLOT(handlePortalResponse(uint,QVariantMap)));
registerPortalTypes();
// Build file filters using registered types (no raw QDBusArgument in QVariant)
PortalFilter bookFilter;
bookFilter.name = QStringLiteral("Book files");
for (const QString &ext : extensions)
bookFilter.globs.append({0, QStringLiteral("*.") + ext});
PortalFilter allFilter;
allFilter.name = QStringLiteral("All files");
allFilter.globs.append({0, QStringLiteral("*")});
QList<PortalFilter> filters{bookFilter, allFilter};
QVariantMap options;
options[QStringLiteral("handle_token")] = token;
options[QStringLiteral("filters")] = QVariant::fromValue(filters);
// Build the OpenFile call
QDBusMessage msg = QDBusMessage::createMethodCall(
QStringLiteral("org.freedesktop.portal.Desktop"),
QStringLiteral("/org/freedesktop/portal/desktop"),
QStringLiteral("org.freedesktop.portal.FileChooser"),
QStringLiteral("OpenFile"));
msg << QString(); // parent_window (empty)
msg << QStringLiteral("Open Book"); // title
msg << options;
QDBusMessage reply = bus.call(msg);
if (reply.type() == QDBusMessage::ErrorMessage) {
bus.disconnect(QStringLiteral("org.freedesktop.portal.Desktop"),
m_portalHandlePath,
QStringLiteral("org.freedesktop.portal.Request"),
QStringLiteral("Response"),
this,
SLOT(handlePortalResponse(uint,QVariantMap)));
}
}
void PortalFileDialogService::handlePortalResponse(uint response,
const QVariantMap &results)
{
QDBusConnection::sessionBus().disconnect(
QStringLiteral("org.freedesktop.portal.Desktop"),
m_portalHandlePath,
QStringLiteral("org.freedesktop.portal.Request"),
QStringLiteral("Response"),
this,
SLOT(handlePortalResponse(uint,QVariantMap)));
if (response != 0)
return; // user cancelled or error
QStringList uris = results.value(QStringLiteral("uris")).toStringList();
if (!uris.isEmpty())
emit fileSelected(QUrl(uris.first()).toLocalFile());
}
#endif // HAS_QT_DBUS