733c29ea4606b199e82dc9f6452000b16435312b
[platform/upstream/glib.git] / gio / tests / gdbus-example-own-name.c
1 /*
2  * Copyright © 2010 Red Hat, Inc.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the licence or (at
7  * your option) any later version.
8  *
9  * See the included COPYING file for more information.
10  *
11  * Author: David Zeuthen <davidz@redhat.com>
12  */
13
14 #include <gio/gio.h>
15
16 static void
17 on_bus_acquired (GDBusConnection *connection,
18                  const gchar     *name,
19                  gpointer         user_data)
20 {
21   /* This is where we'd export some objects on the bus */
22 }
23
24 static void
25 on_name_acquired (GDBusConnection *connection,
26                   const gchar     *name,
27                   gpointer         user_data)
28 {
29   g_print ("Acquired the name %s on the session bus\n", name);
30 }
31
32 static void
33 on_name_lost (GDBusConnection *connection,
34               const gchar     *name,
35               gpointer         user_data)
36 {
37   g_print ("Lost the name %s on the session bus\n", name);
38 }
39
40 int
41 main (int argc, char *argv[])
42 {
43   guint owner_id;
44   GMainLoop *loop;
45   GBusNameOwnerFlags flags;
46   gboolean opt_replace;
47   gboolean opt_allow_replacement;
48   gchar *opt_name;
49   GOptionContext *opt_context;
50   GError *error;
51   GOptionEntry opt_entries[] =
52     {
53       { "replace", 'r', 0, G_OPTION_ARG_NONE, &opt_replace, "Replace existing name if possible", NULL },
54       { "allow-replacement", 'a', 0, G_OPTION_ARG_NONE, &opt_allow_replacement, "Allow replacement", NULL },
55       { "name", 'n', 0, G_OPTION_ARG_STRING, &opt_name, "Name to acquire", NULL },
56       { NULL}
57     };
58
59   g_type_init ();
60
61   error = NULL;
62   opt_name = NULL;
63   opt_replace = FALSE;
64   opt_allow_replacement = FALSE;
65   opt_context = g_option_context_new ("g_bus_own_name() example");
66   g_option_context_add_main_entries (opt_context, opt_entries, NULL);
67   if (!g_option_context_parse (opt_context, &argc, &argv, &error))
68     {
69       g_printerr ("Error parsing options: %s", error->message);
70       return 1;
71     }
72   if (opt_name == NULL)
73     {
74       g_printerr ("Incorrect usage, try --help.\n");
75       return 1;
76     }
77
78   flags = G_BUS_NAME_OWNER_FLAGS_NONE;
79   if (opt_replace)
80     flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;
81   if (opt_allow_replacement)
82     flags |= G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
83
84   owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
85                              opt_name,
86                              flags,
87                              on_bus_acquired,
88                              on_name_acquired,
89                              on_name_lost,
90                              NULL,
91                              NULL);
92
93   loop = g_main_loop_new (NULL, FALSE);
94   g_main_loop_run (loop);
95
96   g_bus_unown_name (owner_id);
97
98   return 0;
99 }