• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 Canonical Limited
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  *
17  * Authors: James Henstridge <james.henstridge@canonical.com>
18  */
19 
20 /* A stub implementation of xdg-document-portal covering enough to
21  * support g_document_portal_add_documents */
22 
23 #include <glib.h>
24 #include <gio/gio.h>
25 #include <gio/gunixfdlist.h>
26 
27 #include "fake-document-portal-generated.h"
28 
29 static gboolean
on_handle_get_mount_point(FakeDocuments * object,GDBusMethodInvocation * invocation,gpointer user_data)30 on_handle_get_mount_point (FakeDocuments         *object,
31                            GDBusMethodInvocation *invocation,
32                            gpointer               user_data)
33 {
34   fake_documents_complete_get_mount_point (object,
35                                            invocation,
36                                            "/document-portal");
37   return TRUE;
38 }
39 
40 static gboolean
on_handle_add_full(FakeDocuments * object,GDBusMethodInvocation * invocation,GUnixFDList * o_path_fds,guint flags,const gchar * app_id,const gchar * const * permissions,gpointer user_data)41 on_handle_add_full (FakeDocuments         *object,
42                     GDBusMethodInvocation *invocation,
43                     GUnixFDList           *o_path_fds,
44                     guint                  flags,
45                     const gchar           *app_id,
46                     const gchar * const   *permissions,
47                     gpointer               user_data)
48 {
49   const gchar **doc_ids = NULL;
50   GVariant *extra_out = NULL;
51   gsize length, i;
52 
53   if (o_path_fds != NULL)
54     length = g_unix_fd_list_get_length (o_path_fds);
55   else
56     length = 0;
57 
58   doc_ids = g_new0 (const gchar *, length + 1  /* NULL terminator */);
59   for (i = 0; i < length; i++)
60     {
61       doc_ids[i] = "document-id";
62     }
63   extra_out = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
64 
65   fake_documents_complete_add_full (object,
66                                     invocation,
67                                     NULL,
68                                     doc_ids,
69                                     extra_out);
70 
71   g_free (doc_ids);
72 
73   return TRUE;
74 }
75 
76 static void
on_bus_acquired(GDBusConnection * connection,const gchar * name,gpointer user_data)77 on_bus_acquired (GDBusConnection *connection,
78                  const gchar     *name,
79                  gpointer         user_data)
80 {
81   FakeDocuments *interface;
82   GError *error = NULL;
83 
84   g_test_message ("Acquired a message bus connection");
85 
86   interface = fake_documents_skeleton_new ();
87   g_signal_connect (interface,
88                     "handle-get-mount-point",
89                     G_CALLBACK (on_handle_get_mount_point),
90                     NULL);
91   g_signal_connect (interface,
92                     "handle-add-full",
93                     G_CALLBACK (on_handle_add_full),
94                     NULL);
95 
96   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (interface),
97                                     connection,
98                                     "/org/freedesktop/portal/documents",
99                                     &error);
100   g_assert_no_error (error);
101 }
102 
103 static void
on_name_acquired(GDBusConnection * connection,const gchar * name,gpointer user_data)104 on_name_acquired (GDBusConnection *connection,
105                   const gchar     *name,
106                   gpointer         user_data)
107 {
108   g_test_message ("Acquired the name %s", name);
109 }
110 
111 static void
on_name_lost(GDBusConnection * connection,const gchar * name,gpointer user_data)112 on_name_lost (GDBusConnection *connection,
113               const gchar     *name,
114               gpointer         user_data)
115 {
116   g_test_message ("Lost the name %s", name);
117 }
118 
119 
120 gint
main(gint argc,gchar * argv[])121 main (gint argc, gchar *argv[])
122 {
123   GMainLoop *loop;
124   guint id;
125 
126   loop = g_main_loop_new (NULL, FALSE);
127 
128   id = g_bus_own_name (G_BUS_TYPE_SESSION,
129                        "org.freedesktop.portal.Documents",
130                        G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
131                        G_BUS_NAME_OWNER_FLAGS_REPLACE,
132                        on_bus_acquired,
133                        on_name_acquired,
134                        on_name_lost,
135                        loop,
136                        NULL);
137 
138   g_main_loop_run (loop);
139 
140   g_bus_unown_name (id);
141   g_main_loop_unref (loop);
142 
143   return 0;
144 }
145