39d0aed8dc09c1c6d39793763b99b5cba45c9c16
[platform/upstream/glib.git] / gio / tests / gdbus-example-watch-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 gchar *opt_name         = NULL;
17 static gboolean opt_system_bus = FALSE;
18 static gboolean opt_auto_start = FALSE;
19
20 static GOptionEntry opt_entries[] =
21 {
22   { "name", 'n', 0, G_OPTION_ARG_STRING, &opt_name, "Name to watch", NULL },
23   { "system-bus", 's', 0, G_OPTION_ARG_NONE, &opt_system_bus, "Use the system-bus instead of the session-bus", NULL },
24   { "auto-start", 'a', 0, G_OPTION_ARG_NONE, &opt_auto_start, "Instruct the bus to launch an owner for the name", NULL},
25   { NULL}
26 };
27
28 static void
29 on_name_appeared (GDBusConnection *connection,
30                   const gchar     *name,
31                   const gchar     *name_owner,
32                   gpointer         user_data)
33 {
34   g_print ("Name %s on %s is owned by %s\n",
35            name,
36            opt_system_bus ? "the system bus" : "the session bus",
37            name_owner);
38 }
39
40 static void
41 on_name_vanished (GDBusConnection *connection,
42                   const gchar     *name,
43                   gpointer         user_data)
44 {
45   g_print ("Name %s does not exist on %s\n",
46            name,
47            opt_system_bus ? "the system bus" : "the session bus");
48 }
49
50 int
51 main (int argc, char *argv[])
52 {
53   guint watcher_id;
54   GMainLoop *loop;
55   GOptionContext *opt_context;
56   GError *error;
57   GBusNameWatcherFlags flags;
58
59   g_type_init ();
60
61   error = NULL;
62   opt_context = g_option_context_new ("g_bus_watch_name() example");
63   g_option_context_set_summary (opt_context,
64                                 "Example: to watch the power manager on the session bus, use:\n"
65                                 "\n"
66                                 "  ./example-watch-name -n org.gnome.PowerManager");
67   g_option_context_add_main_entries (opt_context, opt_entries, NULL);
68   if (!g_option_context_parse (opt_context, &argc, &argv, &error))
69     {
70       g_printerr ("Error parsing options: %s", error->message);
71       goto out;
72     }
73   if (opt_name == NULL)
74     {
75       g_printerr ("Incorrect usage, try --help.\n");
76       goto out;
77     }
78
79   flags = G_BUS_NAME_WATCHER_FLAGS_NONE;
80   if (opt_auto_start)
81     flags |= G_BUS_NAME_WATCHER_FLAGS_AUTO_START;
82
83   watcher_id = g_bus_watch_name (opt_system_bus ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
84                                  opt_name,
85                                  flags,
86                                  on_name_appeared,
87                                  on_name_vanished,
88                                  NULL,
89                                  NULL);
90
91   loop = g_main_loop_new (NULL, FALSE);
92   g_main_loop_run (loop);
93
94   g_bus_unwatch_name (watcher_id);
95
96  out:
97   g_option_context_free (opt_context);
98   g_free (opt_name);
99
100   return 0;
101 }