Debian Codegraph @

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

raw
1
2 #include "portal-test-qt.h"
3 #include "ui_portal-test-qt.h"
4
5 #include <QStringLiteral>
6
7 PortalTestQt::PortalTestQt(QWidget *parent, Qt::WindowFlags f)
8 : QMainWindow(parent, f)
9 , m_mainWindow(new Ui_PortalTestQt)
10 , m_portal(xdp_portal_new())
11 {
12 m_mainWindow->setupUi(this);
13
14 connect(m_mainWindow->openFileButton, &QPushButton::clicked, [=] (G_GNUC_UNUSED bool clicked) {
15 XdpParent *parent;
16 XdpOpenFileFlags flags = XDP_OPEN_FILE_FLAG_NONE;
17
18 parent = xdp_parent_new_qt(windowHandle());
19 xdp_portal_open_file (m_portal, parent, "Portal Test Qt", nullptr /*filters*/, nullptr /*current_filters*/,
20 nullptr /*choices*/, flags, nullptr /*cancellable*/, openedFile, this);
21 xdp_parent_free (parent);
22 });
23 }
24
25 PortalTestQt::~PortalTestQt()
26 {
27 delete m_mainWindow;
28 g_object_unref( m_portal);
29 }
30
31 void PortalTestQt::updateLastOpenedFile(const QString &file)
32 {
33 if (!file.isEmpty()) {
34 m_mainWindow->openedFileLabel->setText(QStringLiteral("Opened file: %1").arg(file));
35 } else {
36 m_mainWindow->openedFileLabel->setText(QStringLiteral("Failed to open a file!!!"));
37 }
38 }
39
40 void PortalTestQt::openedFile(GObject *object, GAsyncResult *result, gpointer data)
41 {
42 Q_UNUSED(data);
43 XdpPortal *portal = XDP_PORTAL (object);
44 PortalTestQt *win = static_cast<PortalTestQt*>(data);
45 g_autoptr(GError) error = nullptr;
46 g_autoptr(GVariant) ret = nullptr;
47
48 ret = xdp_portal_open_file_finish(portal, result, &error);
49
50 if (ret) {
51 const char **uris;
52 if (g_variant_lookup(ret, "uris", "^a&s", &uris)) {
53 win->updateLastOpenedFile(uris[0]);
54 }
55 } else {
56 win->updateLastOpenedFile(QString());
57 }
58 }