summaryrefslogtreecommitdiff
path: root/defaults/gconf-defaults-main.c
blob: b09711e48d12c54317fa6bfb3a33ef1efd5d8783 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 Matthias Clasen <mclasen@redhat.com>
 * Copyright © 2010 Christian Persch
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>

#include <stdlib.h>

#include <glib.h>
#include <gio/gio.h>

#include "gconf-defaults.h"

#define BUS_NAME "org.gnome.GConf.Defaults"

extern gboolean disable_killtimer;
static gboolean debug = FALSE;

static const GOptionEntry entries [] = {
	{ "debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Emit debug output", NULL },
	{ "no-kill", 0, 0, G_OPTION_ARG_NONE, &disable_killtimer, "Don't exit when idle", NULL },
	{ NULL, }
};

static gint log_levels = (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING);

static void
log_default_handler (const gchar   *log_domain,
                     GLogLevelFlags log_level,
                     const gchar   *message,
                     gpointer       unused_data)
{
	if ((log_level & log_levels) != 0) {
		g_log_default_handler (log_domain, log_level, message, unused_data);
	}
}

static GMainLoop *loop = NULL;
static GConfDefaults *mechanism = NULL;

static void
bus_acquired_cb (GDBusConnection *connection,
                 const gchar     *name,
                 gpointer         user_data)
{
        mechanism = gconf_defaults_new (connection);
        if (mechanism == NULL && g_main_loop_is_running (loop))
                g_main_loop_quit (loop);
}

static void
name_acquired_cb (GDBusConnection *connection,
                  const gchar     *name,
                  gpointer         user_data)
{
        g_debug ("Name '%s' acquired\n", name);
}

static void
name_lost_cb (GDBusConnection *connection,
              const gchar     *name,
              gpointer         user_data)
{
        g_debug ("Name '%s' lost \n", name);

        if (mechanism) {
                g_object_unref (mechanism);
                mechanism = NULL;
        }

        if (g_main_loop_is_running (loop))
                g_main_loop_quit (loop);
}

int
main (int argc, char **argv)
{
        GOptionContext *options;
        guint           owner_id;
        GError         *error = NULL;

        g_type_init ();

	options = g_option_context_new (NULL);
	g_option_context_add_main_entries (options, entries, NULL);
	if (!g_option_context_parse (options, &argc, &argv, &error)) {
		g_printerr ("Failed to parse options: %s\n", error->message);
		g_error_free (error);
                g_option_context_free (options);
                return 1;
	}
	g_option_context_free (options);

	g_log_set_default_handler (log_default_handler, NULL);
	if (debug) {
		log_levels = log_levels | G_LOG_LEVEL_DEBUG;
	}

        loop = g_main_loop_new (NULL, FALSE);

        owner_id = g_bus_own_name (G_BUS_TYPE_STARTER,
                                   BUS_NAME,
                                   G_BUS_NAME_OWNER_FLAGS_NONE,
                                   bus_acquired_cb,
                                   name_acquired_cb,
                                   name_lost_cb,
                                   NULL, NULL);

        g_main_loop_run (loop);

        g_bus_unown_name (owner_id);
        g_main_loop_unref (loop);

        return 0;
}