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