tracker-config.c 8.66 KB
Newer Older
1
/*
2
 * Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
3 4 5 6 7 8 9 10
 *
 * This library 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 library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Martyn Russell's avatar
Martyn Russell committed
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.          See the GNU
12 13 14 15 16 17 18 19 20 21
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "config.h"

22
#include <libtracker-common/tracker-keyfile-object.h>
23 24 25 26 27 28

#include "tracker-config.h"

#define TRACKER_CONFIG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TRACKER_TYPE_CONFIG, TrackerConfigPrivate))

/* GKeyFile defines */
Martyn Russell's avatar
Martyn Russell committed
29
#define GROUP_GENERAL     "General"
30 31

/* Default values */
32
#define DEFAULT_VERBOSITY 0
33

34
typedef struct {
35
	/* General */
36 37 38 39 40
	gint verbosity;
} TrackerConfigPrivate;

typedef struct {
	GType  type;
41 42 43
	const gchar *property;
	const gchar *group;
	const gchar *key;
44
} ObjectToKeyFile;
45 46

static void     config_set_property         (GObject       *object,
Martyn Russell's avatar
Martyn Russell committed
47 48 49
                                             guint          param_id,
                                             const GValue  *value,
                                             GParamSpec    *pspec);
50
static void     config_get_property         (GObject       *object,
Martyn Russell's avatar
Martyn Russell committed
51 52 53
                                             guint          param_id,
                                             GValue        *value,
                                             GParamSpec    *pspec);
54 55
static void     config_finalize             (GObject       *object);
static void     config_constructed          (GObject       *object);
56 57
static void     config_load                 (TrackerConfig *config);
static gboolean config_save                 (TrackerConfig *config);
58
static void     config_create_with_defaults (TrackerConfig *config,
Martyn Russell's avatar
Martyn Russell committed
59 60
                                             GKeyFile      *key_file,
                                             gboolean       overwrite);
61 62 63 64 65

enum {
	PROP_0,

	/* General */
66 67 68 69 70
	PROP_VERBOSITY
};

static ObjectToKeyFile conversions[] = {
	{ G_TYPE_INT,     "verbosity",          GROUP_GENERAL,  "Verbosity"       },
71 72
};

73
G_DEFINE_TYPE (TrackerConfig, tracker_config, TRACKER_TYPE_CONFIG_FILE);
74 75 76 77 78 79 80

static void
tracker_config_class_init (TrackerConfigClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->set_property = config_set_property;
81
	object_class->get_property = config_get_property;
Martyn Russell's avatar
Martyn Russell committed
82
	object_class->finalize     = config_finalize;
83
	object_class->constructed  = config_constructed;
84 85 86

	/* General */
	g_object_class_install_property (object_class,
Martyn Russell's avatar
Martyn Russell committed
87 88 89 90 91 92 93 94
	                                 PROP_VERBOSITY,
	                                 g_param_spec_int ("verbosity",
	                                                   "Log verbosity",
	                                                   " Log verbosity (0=errors, 1=minimal, 2=detailed, 3=debug)",
	                                                   0,
	                                                   3,
	                                                   DEFAULT_VERBOSITY,
	                                                   G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
95 96 97 98 99 100 101 102 103 104

	g_type_class_add_private (object_class, sizeof (TrackerConfigPrivate));
}

static void
tracker_config_init (TrackerConfig *object)
{
}

static void
Martyn Russell's avatar
Martyn Russell committed
105 106 107 108
config_set_property (GObject      *object,
                     guint         param_id,
                     const GValue *value,
                     GParamSpec           *pspec)
109
{
110 111 112 113
	switch (param_id) {
		/* General */
	case PROP_VERBOSITY:
		tracker_config_set_verbosity (TRACKER_CONFIG (object),
Martyn Russell's avatar
Martyn Russell committed
114
		                              g_value_get_int (value));
115
		break;
116

117 118 119 120
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
121 122 123
}

static void
Martyn Russell's avatar
Martyn Russell committed
124 125 126 127
config_get_property (GObject    *object,
                     guint       param_id,
                     GValue     *value,
                     GParamSpec *pspec)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
{
	TrackerConfigPrivate *priv;

	priv = TRACKER_CONFIG_GET_PRIVATE (object);

	switch (param_id) {
		/* General */
	case PROP_VERBOSITY:
		g_value_set_int (value, priv->verbosity);
		break;

	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
}

static void
146
config_finalize (GObject *object)
147
{
148 149 150
	/* For now we do nothing here, we left this override in for
	 * future expansion.
	 */
151

152
	(G_OBJECT_CLASS (tracker_config_parent_class)->finalize) (object);
153 154 155
}

static void
156
config_constructed (GObject *object)
157
{
158
	(G_OBJECT_CLASS (tracker_config_parent_class)->constructed) (object);
159

160
	config_load (TRACKER_CONFIG (object));
161 162 163
}

static void
164
config_create_with_defaults (TrackerConfig *config,
Martyn Russell's avatar
Martyn Russell committed
165 166
                             GKeyFile      *key_file,
                             gboolean       overwrite)
167
{
168
	gint i;
169

170
	g_message ("Loading defaults into GKeyFile...");
Martyn Russell's avatar
Martyn Russell committed
171

172 173
	for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
		gboolean has_key;
Martyn Russell's avatar
Martyn Russell committed
174 175 176 177 178

		has_key = g_key_file_has_key (key_file,
		                              conversions[i].group,
		                              conversions[i].key,
		                              NULL);
179 180 181
		if (!overwrite && has_key) {
			continue;
		}
Martyn Russell's avatar
Martyn Russell committed
182

183 184
		switch (conversions[i].type) {
		case G_TYPE_INT:
Martyn Russell's avatar
Martyn Russell committed
185 186 187 188 189
			g_key_file_set_integer (key_file,
			                        conversions[i].group,
			                        conversions[i].key,
			                        tracker_keyfile_object_default_int (config,
			                                                            conversions[i].property));
190 191 192 193 194
			break;

		default:
			g_assert_not_reached ();
		}
195

Martyn Russell's avatar
Martyn Russell committed
196 197 198 199 200 201
		g_key_file_set_comment (key_file,
		                        conversions[i].group,
		                        conversions[i].key,
		                        tracker_keyfile_object_blurb (config,
		                                                      conversions[i].property),
		                        NULL);
202 203 204 205 206 207
	}
}

static void
config_load (TrackerConfig *config)
{
208
	TrackerConfigFile *file;
209
	gint i;
210

211 212
	file = TRACKER_CONFIG_FILE (config);
	config_create_with_defaults (config, file->key_file, FALSE);
213

214 215
	if (!file->file_exists) {
		tracker_config_file_save (file);
216 217
	}

218 219
	for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
		gboolean has_key;
Martyn Russell's avatar
Martyn Russell committed
220 221 222 223 224 225

		has_key = g_key_file_has_key (file->key_file,
		                              conversions[i].group,
		                              conversions[i].key,
		                              NULL);

226 227
		switch (conversions[i].type) {
		case G_TYPE_INT:
Martyn Russell's avatar
Martyn Russell committed
228 229 230 231 232
			tracker_keyfile_object_load_int (G_OBJECT (file),
			                                 conversions[i].property,
			                                 file->key_file,
			                                 conversions[i].group,
			                                 conversions[i].key);
233 234 235 236 237 238
			break;

		default:
			g_assert_not_reached ();
			break;
		}
239 240 241 242 243 244
	}
}

static gboolean
config_save (TrackerConfig *config)
{
245
	TrackerConfigFile *file;
246
	gint i;
247

248
	file = TRACKER_CONFIG_FILE (config);
249

250
	if (!file->key_file) {
251 252 253 254 255 256 257
		g_critical ("Could not save config, GKeyFile was NULL, has the config been loaded?");

		return FALSE;
	}

	g_message ("Setting details to GKeyFile object...");

258 259 260
	for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
		switch (conversions[i].type) {
		case G_TYPE_INT:
261
			tracker_keyfile_object_save_int (file,
Martyn Russell's avatar
Martyn Russell committed
262 263 264 265
			                                 conversions[i].property,
			                                 file->key_file,
			                                 conversions[i].group,
			                                 conversions[i].key);
266 267 268 269 270 271
			break;

		default:
			g_assert_not_reached ();
			break;
		}
272 273
	}

274
	return tracker_config_file_save (file);
275 276 277 278 279
}

TrackerConfig *
tracker_config_new (void)
{
280
	return g_object_new (TRACKER_TYPE_CONFIG, NULL);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
}

gboolean
tracker_config_save (TrackerConfig *config)
{
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), FALSE);

	return config_save (config);
}

gint
tracker_config_get_verbosity (TrackerConfig *config)
{
	TrackerConfigPrivate *priv;

	g_return_val_if_fail (TRACKER_IS_CONFIG (config), DEFAULT_VERBOSITY);

	priv = TRACKER_CONFIG_GET_PRIVATE (config);

	return priv->verbosity;
}

void
tracker_config_set_verbosity (TrackerConfig *config,
Martyn Russell's avatar
Martyn Russell committed
305
                              gint           value)
306 307 308 309 310
{
	TrackerConfigPrivate *priv;

	g_return_if_fail (TRACKER_IS_CONFIG (config));

311
	if (!tracker_keyfile_object_validate_int (config, "verbosity", value)) {
312 313 314 315 316 317 318 319
		return;
	}

	priv = TRACKER_CONFIG_GET_PRIVATE (config);

	priv->verbosity = value;
	g_object_notify (G_OBJECT (config), "verbosity");
}