summaryrefslogtreecommitdiff
path: root/src/up-config.c
blob: 703774a342a4bee50bdc37424037554c86d2d3e0 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2011 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * 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 <glib-object.h>
#include <gio/gio.h>

#include "up-config.h"

static void     up_config_finalize	(GObject     *object);

#define UP_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_CONFIG, UpConfigPrivate))

/**
 * UpConfigPrivate:
 *
 * Private #UpConfig data
 **/
struct _UpConfigPrivate
{
	GKeyFile			*keyfile;
};

G_DEFINE_TYPE (UpConfig, up_config, G_TYPE_OBJECT)

static gpointer up_config_object = NULL;

/**
 * up_config_get_boolean:
 **/
gboolean
up_config_get_boolean (UpConfig *config, const gchar *key)
{
	return g_key_file_get_boolean (config->priv->keyfile,
				       "UPower", key, NULL);
}

/**
 * up_config_get_uint:
 **/
guint
up_config_get_uint (UpConfig *config, const gchar *key)
{
	int val;

	val = g_key_file_get_integer (config->priv->keyfile,
				      "UPower", key, NULL);
	if (val < 0)
		return 0;

	return val;
}

/**
 * up_config_get_string:
 **/
gchar *
up_config_get_string (UpConfig *config, const gchar *key)
{
	return g_key_file_get_string (config->priv->keyfile,
				      "UPower", key, NULL);
}

/**
 * up_config_class_init:
 **/
static void
up_config_class_init (UpConfigClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = up_config_finalize;
	g_type_class_add_private (klass, sizeof (UpConfigPrivate));
}

/**
 * up_config_init:
 **/
static void
up_config_init (UpConfig *config)
{
	gboolean ret;
	GError *error = NULL;
	gchar *filename;

	config->priv = UP_CONFIG_GET_PRIVATE (config);
	config->priv->keyfile = g_key_file_new ();

	filename = g_strdup (g_getenv ("UPOWER_CONF_FILE_NAME"));
	if (filename == NULL)
		filename = g_build_filename (PACKAGE_SYSCONF_DIR,"UPower", "UPower.conf", NULL);

	/* load */
	ret = g_key_file_load_from_file (config->priv->keyfile,
					 filename,
					 G_KEY_FILE_NONE,
					 &error);

	g_free (filename);

	if (!ret) {
		g_warning ("failed to load config file: %s",
			   error->message);
		g_error_free (error);
	}
}

/**
 * up_config_finalize:
 **/
static void
up_config_finalize (GObject *object)
{
	UpConfig *config = UP_CONFIG (object);
	UpConfigPrivate *priv = config->priv;

	g_key_file_free (priv->keyfile);

	G_OBJECT_CLASS (up_config_parent_class)->finalize (object);
}

/**
 * up_config_new:
 **/
UpConfig *
up_config_new (void)
{
	if (up_config_object != NULL) {
		g_object_ref (up_config_object);
	} else {
		up_config_object = g_object_new (UP_TYPE_CONFIG, NULL);
		g_object_add_weak_pointer (up_config_object, &up_config_object);
	}
	return UP_CONFIG (up_config_object);
}