04717082cad87356818a837d69c1b99a54229ef7
[platform/upstream/glib.git] / gio / tests / gdbus-daemon.c
1 #include "config.h"
2
3 #include "gdbusdaemon.h"
4 #include <glib/gi18n.h>
5
6 int
7 main (int argc, char *argv[])
8 {
9   GDBusDaemon *daemon;
10   GMainLoop *loop;
11   const char *address = NULL;
12   const char *config_file = NULL;
13   GError *error = NULL;
14   gboolean print_address = FALSE;
15   gboolean print_env = FALSE;
16   GOptionContext *context;
17   GOptionEntry entries[] = {
18     { "address", 0, 0, G_OPTION_ARG_STRING, &address, N_("Address to listen on"), NULL },
19     { "config-file", 0, 0, G_OPTION_ARG_STRING, &config_file, N_("Ignored, for compat with GTestDbus"), NULL },
20     { "print-address", 0, 0, G_OPTION_ARG_NONE, &print_address, N_("Print address"), NULL },
21     { "print-env", 0, 0, G_OPTION_ARG_NONE, &print_env, N_("Print address in shell mode"), NULL },
22     { NULL }
23   };
24
25   g_type_init ();
26
27   context = g_option_context_new ("");
28   g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
29   g_option_context_set_summary (context,
30     N_("Run a dbus service"));
31   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
32
33   error = NULL;
34   if (!g_option_context_parse (context, &argc, &argv, &error))
35     {
36       g_printerr ("%s\n", error->message);
37       return 1;
38     }
39
40   g_option_context_free (context);
41
42   if (argc != 1)
43     {
44       g_printerr (_("Wrong args\n"));
45       return 1;
46     }
47
48
49   loop = g_main_loop_new (NULL, FALSE);
50
51   if (argc >= 2)
52     address = argv[1];
53
54   daemon = _g_dbus_daemon_new (address, NULL, &error);
55   if (daemon == NULL)
56     {
57       g_printerr ("Can't init bus: %s\n", error->message);
58       return 1;
59     }
60
61   if (print_env)
62     g_print ("export DBUS_SESSION_BUS_ADDRESS=\"%s\"\n", _g_dbus_daemon_get_address (daemon));
63
64   if (print_address)
65     g_print ("%s\n", _g_dbus_daemon_get_address (daemon));
66
67   g_main_loop_run (loop);
68
69   g_main_loop_unref (loop);
70
71   return 0;
72 }