Add some more cases to the app-id unit tests
[glib.git] / gio / tests / gdbus-example-own-name.c
blob74d39208fdaaa53129a55bafc577208e79377c24
1 #include <gio/gio.h>
3 static void
4 on_bus_acquired (GDBusConnection *connection,
5 const gchar *name,
6 gpointer user_data)
8 /* This is where we'd export some objects on the bus */
11 static void
12 on_name_acquired (GDBusConnection *connection,
13 const gchar *name,
14 gpointer user_data)
16 g_print ("Acquired the name %s on the session bus\n", name);
19 static void
20 on_name_lost (GDBusConnection *connection,
21 const gchar *name,
22 gpointer user_data)
24 g_print ("Lost the name %s on the session bus\n", name);
27 int
28 main (int argc, char *argv[])
30 guint owner_id;
31 GMainLoop *loop;
32 GBusNameOwnerFlags flags;
33 gboolean opt_replace;
34 gboolean opt_allow_replacement;
35 gchar *opt_name;
36 GOptionContext *opt_context;
37 GError *error;
38 GOptionEntry opt_entries[] =
40 { "replace", 'r', 0, G_OPTION_ARG_NONE, &opt_replace, "Replace existing name if possible", NULL },
41 { "allow-replacement", 'a', 0, G_OPTION_ARG_NONE, &opt_allow_replacement, "Allow replacement", NULL },
42 { "name", 'n', 0, G_OPTION_ARG_STRING, &opt_name, "Name to acquire", NULL },
43 { NULL}
46 error = NULL;
47 opt_name = NULL;
48 opt_replace = FALSE;
49 opt_allow_replacement = FALSE;
50 opt_context = g_option_context_new ("g_bus_own_name() example");
51 g_option_context_add_main_entries (opt_context, opt_entries, NULL);
52 if (!g_option_context_parse (opt_context, &argc, &argv, &error))
54 g_printerr ("Error parsing options: %s", error->message);
55 return 1;
57 if (opt_name == NULL)
59 g_printerr ("Incorrect usage, try --help.\n");
60 return 1;
63 flags = G_BUS_NAME_OWNER_FLAGS_NONE;
64 if (opt_replace)
65 flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;
66 if (opt_allow_replacement)
67 flags |= G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
69 owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
70 opt_name,
71 flags,
72 on_bus_acquired,
73 on_name_acquired,
74 on_name_lost,
75 NULL,
76 NULL);
78 loop = g_main_loop_new (NULL, FALSE);
79 g_main_loop_run (loop);
81 g_bus_unown_name (owner_id);
83 return 0;