a bit of refactor
This commit is contained in:
19 files changed
+546
-232
No files matched your search
@@ -0,0 +1,155 @@
|
||||
#ifdef HAS_QT_DBUS
|
||||
|
||||
#include "PortalFileDialogService.h"
|
||||
#include <QDBusConnection>
|
||||
#include <QDBusMessage>
|
||||
#include <QDBusArgument>
|
||||
#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()
|
||||
{
|
||||
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 char *ext : {"*.txt", "*.fb2", "*.epub", "*.pdf", "*.doc",
|
||||
"*.rtf", "*.html", "*.htm", "*.docx",
|
||||
"*.djvu", "*.odt", "*.chm"}) {
|
||||
bookFilter.globs.append({0, QString::fromLatin1(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
|
||||
Reference in new issue
Block a user