2 * Copyright © 2010 Red Hat, Inc.
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.
9 * See the included COPYING file for more information.
11 * Author: David Zeuthen <davidz@redhat.com>
17 on_bus_acquired (GDBusConnection *connection,
21 /* This is where we'd export some objects on the bus */
25 on_name_acquired (GDBusConnection *connection,
29 g_print ("Acquired the name %s on the session bus\n", name);
33 on_name_lost (GDBusConnection *connection,
37 g_print ("Lost the name %s on the session bus\n", name);
41 main (int argc, char *argv[])
45 GBusNameOwnerFlags flags;
47 gboolean opt_allow_replacement;
49 GOptionContext *opt_context;
51 GOptionEntry opt_entries[] =
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 },
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))
69 g_printerr ("Error parsing options: %s", error->message);
74 g_printerr ("Incorrect usage, try --help.\n");
78 flags = G_BUS_NAME_OWNER_FLAGS_NONE;
80 flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;
81 if (opt_allow_replacement)
82 flags |= G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
84 owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
93 loop = g_main_loop_new (NULL, FALSE);
94 g_main_loop_run (loop);
96 g_bus_unown_name (owner_id);