tracker-config.c 7.41 KB
Newer Older
1 2
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
3
 * Copyright (C) 2009, Nokia (urho.konttori@nokia.com)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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
 * 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 library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "config.h"

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

#include "tracker-config.h"

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

/* GKeyFile defines */
30
#define GROUP_GENERAL	  "General"
31 32

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

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

typedef struct {
	GType  type;
42 43 44
	const gchar *property;
	const gchar *group;
	const gchar *key;
45
} ObjectToKeyFile;
46 47 48 49 50

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

enum {
	PROP_0,

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

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

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

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

	object_class->set_property = config_set_property;
82 83 84
	object_class->get_property = config_get_property;
	object_class->finalize	   = config_finalize;
	object_class->constructed  = config_constructed;
85 86 87 88 89 90

	/* General */
	g_object_class_install_property (object_class,
					 PROP_VERBOSITY,
					 g_param_spec_int ("verbosity",
							   "Log verbosity",
91
							   " Log verbosity (0=errors, 1=minimal, 2=detailed, 3=debug)",
92 93 94 95 96 97 98 99 100 101 102 103 104 105
							   0,
							   3,
							   DEFAULT_VERBOSITY,
							   G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

	g_type_class_add_private (object_class, sizeof (TrackerConfigPrivate));
}

static void
tracker_config_init (TrackerConfig *object)
{
}

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

118 119 120 121
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
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
}

static void
config_get_property (GObject	*object,
		     guint	 param_id,
		     GValue	*value,
		     GParamSpec *pspec)
{
	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
147
config_finalize (GObject *object)
148
{
149 150 151
	/* For now we do nothing here, we left this override in for
	 * future expansion.
	 */
152

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

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

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

static void
165 166 167
config_create_with_defaults (TrackerConfig *config,
			     GKeyFile      *key_file, 
			     gboolean       overwrite)
168
{
169
	gint i;
170

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	g_message ("Loading defaults into GKeyFile...");
	
	for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
		gboolean has_key;
		
		has_key = g_key_file_has_key (key_file, 
					      conversions[i].group, 
					      conversions[i].key, 
					      NULL);
		if (!overwrite && has_key) {
			continue;
		}
		
		switch (conversions[i].type) {
		case G_TYPE_INT:
			g_key_file_set_integer (key_file, 
						conversions[i].group, 
						conversions[i].key, 
189 190
						tracker_keyfile_object_default_int (config, 
										    conversions[i].property));
191 192 193 194 195
			break;

		default:
			g_assert_not_reached ();
		}
196

197 198 199
		g_key_file_set_comment (key_file, 
					conversions[i].group, 
					conversions[i].key, 
200 201
					tracker_keyfile_object_blurb (config,
								      conversions[i].property), 
202
					NULL);
203 204 205 206 207 208
	}
}

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

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

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

219 220 221
	for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
		gboolean has_key;
		
222
		has_key = g_key_file_has_key (file->key_file, 
223 224 225 226 227 228
					      conversions[i].group, 
					      conversions[i].key, 
					      NULL);
	
		switch (conversions[i].type) {
		case G_TYPE_INT:
229 230 231 232 233
			tracker_keyfile_object_load_int (G_OBJECT (file), 
							 conversions[i].property,
							 file->key_file,
							 conversions[i].group, 
							 conversions[i].key);
234 235 236 237 238 239
			break;

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

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

249
	file = TRACKER_CONFIG_FILE (config);
250

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

		return FALSE;
	}

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

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

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

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

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

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,
			      gint	     value)
{
	TrackerConfigPrivate *priv;

	g_return_if_fail (TRACKER_IS_CONFIG (config));

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

	priv = TRACKER_CONFIG_GET_PRIVATE (config);

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