Skip to content

Commit e4901e7

Browse files
committed
portal-test: Register application IDs
1 parent ceaf045 commit e4901e7

11 files changed

+98
-11
lines changed

portal-test/gtk3/portal-test-win.c

+35-1
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,28 @@ settings_changed (XdpSettings* settings, const gchar *namespace, const gchar *ke
299299
}
300300

301301
static void
302-
portal_test_win_init (PortalTestWin *win)
302+
register_cb (GObject *source_object,
303+
GAsyncResult *result,
304+
gpointer user_data)
305+
{
306+
XdpPortal *portal = XDP_PORTAL (source_object);
307+
g_autoptr(GError) error = NULL;
308+
309+
if (!xdp_portal_register_finish (portal, result, &error))
310+
{
311+
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
312+
g_warning ("Failed to register application ID: %s", error->message);
313+
return;
314+
}
315+
316+
g_debug ("Registered application ID");
317+
}
318+
319+
static void
320+
portal_test_win_realize (GtkWidget *widget)
303321
{
322+
PortalTestWin *win = (PortalTestWin *) widget;
323+
GtkApplication *app = gtk_window_get_application (GTK_WINDOW (win));
304324
const char *status;
305325
g_auto(GStrv) proxies = NULL;
306326
g_autofree char *proxy = NULL;
@@ -312,6 +332,11 @@ portal_test_win_init (PortalTestWin *win)
312332
g_autoptr(GFile) src = NULL;
313333

314334
win->portal = xdp_portal_new ();
335+
xdp_portal_register (win->portal,
336+
g_application_get_application_id (G_APPLICATION (app)),
337+
NULL,
338+
register_cb,
339+
NULL);
315340

316341
gtk_widget_init_template (GTK_WIDGET (win));
317342

@@ -357,6 +382,13 @@ portal_test_win_init (PortalTestWin *win)
357382

358383
win->settings = xdp_portal_get_settings (win->portal);
359384
g_signal_connect (win->settings, "changed", G_CALLBACK (settings_changed), win);
385+
386+
GTK_WIDGET_CLASS (portal_test_win_parent_class)->realize (widget);
387+
}
388+
389+
static void
390+
portal_test_win_init (PortalTestWin *win)
391+
{
360392
}
361393

362394
static void
@@ -1394,6 +1426,8 @@ portal_test_win_class_init (PortalTestWinClass *class)
13941426
{
13951427
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
13961428

1429+
widget_class->realize = portal_test_win_realize;
1430+
13971431
gtk_widget_class_set_template_from_resource (widget_class,
13981432
"/org/gtk/portal-test/portal-test-win.ui");
13991433

portal-test/gtk4/application.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Gio, GLib, GObject, Gtk } = imports.gi;
1+
const { Gio, GLib, GObject, Gtk, Xdp } = imports.gi;
22

33
const { PortalTestWindow } = imports.window;
44
const ByteArray = imports.byteArray;
@@ -31,12 +31,21 @@ var Application = GObject.registerClass({
3131
}
3232

3333
vfunc_startup() {
34+
this._portal = new Xdp.Portal();
35+
this._portal.register(this.get_application_id(), null, (portal, result) => {
36+
this._portal.register_finish(result);
37+
});
38+
3439
super.vfunc_startup();
3540

3641
if (!this._window)
3742
this._window = new PortalTestWindow(this);
3843
}
3944

45+
getPortal() {
46+
return this._portal;
47+
}
48+
4049
restart() {
4150
const bus = this.get_dbus_connection();
4251

portal-test/gtk4/window.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var PortalTestWindow = GObject.registerClass({
3737
super._init({ application });
3838

3939
this._app = application;
40-
this._portal = new Xdp.Portal();
40+
this._portal = application.getPortal();
4141

4242
this._restoreToken = null;
4343

portal-test/qt5/main.cpp

+21-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,31 @@
33

44
#include "portal-test-qt.h"
55

6+
#define APP_ID "org.gnome.PortalTest.Qt5"
7+
8+
static void registerCb(GObject *source_object, GAsyncResult *result, gpointer user_data)
9+
{
10+
Q_UNUSED(user_data)
11+
12+
XdpPortal *portal = XDP_PORTAL (source_object);
13+
g_autoptr(GError) error = NULL;
14+
if (!xdp_portal_register_finish (portal, result, &error))
15+
{
16+
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
17+
qWarning ("Failed to register application ID: %s", error->message);
18+
return;
19+
}
20+
21+
qInfo ("Registered application ID");
22+
}
23+
624
int main(int argc, char *argv[])
725
{
826
QApplication a(argc, argv);
927

10-
PortalTestQt *portalTest = new PortalTestQt(nullptr);
28+
XdpPortal *portal = xdp_portal_new();
29+
xdp_portal_register(portal, APP_ID, nullptr /*cancellable*/, registerCb, &a);
30+
PortalTestQt *portalTest = new PortalTestQt(portal, nullptr);
1131
portalTest->show();
1232

1333
return a.exec();

portal-test/qt5/meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ prep = qt5.preprocess(
1414
dependencies: libportal_qt5_dep,
1515
)
1616

17+
install_data('org.gnome.PortalTest.Qt5.desktop', install_dir: 'share/applications')
18+
1719
executable('portal-test-qt5',
1820
[src, prep],
1921
include_directories: [top_inc, libportal_inc],

portal-test/qt5/portal-test-qt.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
#include <QStringLiteral>
66

7-
PortalTestQt::PortalTestQt(QWidget *parent, Qt::WindowFlags f)
7+
PortalTestQt::PortalTestQt(XdpPortal *portal, QWidget *parent, Qt::WindowFlags f)
88
: QMainWindow(parent, f)
99
, m_mainWindow(new Ui_PortalTestQt)
10-
, m_portal(xdp_portal_new())
10+
, m_portal(portal)
1111
{
1212
m_mainWindow->setupUi(this);
1313

portal-test/qt5/portal-test-qt.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class PortalTestQt : public QMainWindow
1414
{
1515
Q_OBJECT
1616
public:
17-
PortalTestQt(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
17+
PortalTestQt(XdpPortal *portal, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
1818
~PortalTestQt();
1919

2020
void updateLastOpenedFile(const QString &file);

portal-test/qt6/main.cpp

+21-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,31 @@
33

44
#include "portal-test-qt.h"
55

6+
#define APP_ID "org.gnome.PortalTest.Qt6"
7+
8+
static void registerCb(GObject *source_object, GAsyncResult *result, gpointer user_data)
9+
{
10+
Q_UNUSED(user_data)
11+
12+
XdpPortal *portal = XDP_PORTAL (source_object);
13+
g_autoptr(GError) error = NULL;
14+
if (!xdp_portal_register_finish (portal, result, &error))
15+
{
16+
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
17+
qWarning ("Failed to register application ID: %s", error->message);
18+
return;
19+
}
20+
21+
qInfo ("Registered application ID");
22+
}
23+
624
int main(int argc, char *argv[])
725
{
826
QApplication a(argc, argv);
927

10-
PortalTestQt *portalTest = new PortalTestQt(nullptr);
28+
XdpPortal *portal = xdp_portal_new();
29+
xdp_portal_register(portal, APP_ID, nullptr /*cancellable*/, registerCb, &a);
30+
PortalTestQt *portalTest = new PortalTestQt(portal, nullptr);
1131
portalTest->show();
1232

1333
return a.exec();

portal-test/qt6/meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ prep = qt6.preprocess(
1414
dependencies: libportal_qt6_dep,
1515
)
1616

17+
install_data('org.gnome.PortalTest.Qt6.desktop', install_dir: 'share/applications')
18+
1719
executable('portal-test-qt6',
1820
[src, prep],
1921
include_directories: [top_inc, libportal_inc],

portal-test/qt6/portal-test-qt.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
#include <QStringLiteral>
66

7-
PortalTestQt::PortalTestQt(QWidget *parent, Qt::WindowFlags f)
7+
PortalTestQt::PortalTestQt(XdpPortal *portal, QWidget *parent, Qt::WindowFlags f)
88
: QMainWindow(parent, f)
99
, m_mainWindow(new Ui_PortalTestQt)
10-
, m_portal(xdp_portal_new())
10+
, m_portal(portal)
1111
{
1212
m_mainWindow->setupUi(this);
1313

portal-test/qt6/portal-test-qt.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class PortalTestQt : public QMainWindow
1414
{
1515
Q_OBJECT
1616
public:
17-
PortalTestQt(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
17+
PortalTestQt(XdpPortal *portal, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
1818
~PortalTestQt();
1919

2020
void updateLastOpenedFile(const QString &file);

0 commit comments

Comments
 (0)