glib/gio/tests/gdbus-example-own-name.c
David Zeuthen d0a14469d0 Initial GDBus code-drop from GDBus-standalone repo
Things compile and the test-suite passes. Still need to hook up
gio.symbols and docs. There are still a bunch of TODOs left in the
sources that needs to be addressed.

Signed-off-by: David Zeuthen <davidz@redhat.com>
2010-05-06 14:21:32 -04:00

100 lines
2.7 KiB
C

/*
* Copyright © 2010 Red Hat, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* See the included COPYING file for more information.
*
* Author: David Zeuthen <davidz@redhat.com>
*/
#include <gio/gio.h>
static void
on_bus_acquired (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
/* This is where we'd export some objects on the bus */
}
static void
on_name_acquired (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
g_print ("Acquired the name %s on the session bus\n", name);
}
static void
on_name_lost (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
g_print ("Lost the name %s on the session bus\n", name);
}
int
main (int argc, char *argv[])
{
guint owner_id;
GMainLoop *loop;
GBusNameOwnerFlags flags;
gboolean opt_replace;
gboolean opt_allow_replacement;
gchar *opt_name;
GOptionContext *opt_context;
GError *error;
GOptionEntry opt_entries[] =
{
{ "replace", 'r', 0, G_OPTION_ARG_NONE, &opt_replace, "Replace existing name if possible", NULL },
{ "allow-replacement", 'a', 0, G_OPTION_ARG_NONE, &opt_allow_replacement, "Allow replacement", NULL },
{ "name", 'n', 0, G_OPTION_ARG_STRING, &opt_name, "Name to acquire", NULL },
{ NULL}
};
g_type_init ();
error = NULL;
opt_name = NULL;
opt_replace = FALSE;
opt_allow_replacement = FALSE;
opt_context = g_option_context_new ("g_bus_own_name() example");
g_option_context_add_main_entries (opt_context, opt_entries, NULL);
if (!g_option_context_parse (opt_context, &argc, &argv, &error))
{
g_printerr ("Error parsing options: %s", error->message);
return 1;
}
if (opt_name == NULL)
{
g_printerr ("Incorrect usage, try --help.\n");
return 1;
}
flags = G_BUS_NAME_OWNER_FLAGS_NONE;
if (opt_replace)
flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;
if (opt_allow_replacement)
flags |= G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
opt_name,
flags,
on_bus_acquired,
on_name_acquired,
on_name_lost,
NULL,
NULL);
loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (loop);
g_bus_unown_name (owner_id);
return 0;
}