tracker-config.c 10.3 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
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
#define DEFAULT_MAX_BYTES 1048576   /* 1Mbyte */
#define ABSOLUTE_MAX_BYTES 10485760 /* 10 Mbytes (GB#616845) */
35

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

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

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

enum {
	PROP_0,

	/* General */
69 70
	PROP_VERBOSITY,
	PROP_MAX_BYTES
71 72 73 74
};

static ObjectToKeyFile conversions[] = {
	{ G_TYPE_INT,     "verbosity",          GROUP_GENERAL,  "Verbosity"       },
75
	{ G_TYPE_INT,     "max-bytes",          GROUP_GENERAL,  "MaxBytes"        },
76 77
};

78
G_DEFINE_TYPE (TrackerConfig, tracker_config, TRACKER_TYPE_CONFIG_FILE);
79 80 81 82 83 84 85

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

	object_class->set_property = config_set_property;
86
	object_class->get_property = config_get_property;
Martyn Russell's avatar
Martyn Russell committed
87
	object_class->finalize     = config_finalize;
88
	object_class->constructed  = config_constructed;
89 90 91

	/* General */
	g_object_class_install_property (object_class,
Martyn Russell's avatar
Martyn Russell committed
92 93 94 95 96 97 98 99
	                                 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));
100

101
	g_object_class_install_property (object_class,
102
	                                 PROP_MAX_BYTES,
103
	                                 g_param_spec_int ("max-bytes",
104
	                                                   "Max Bytes",
105
	                                                   " Maximum number of UTF-8 bytes to extract per file [0->10485760]",
106
	                                                   0,
107
	                                                   ABSOLUTE_MAX_BYTES,
108 109 110
	                                                   DEFAULT_MAX_BYTES,
	                                                   G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

111 112 113 114 115 116 117 118 119
	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
120 121 122
config_set_property (GObject      *object,
                     guint         param_id,
                     const GValue *value,
123
                     GParamSpec   *pspec)
124
{
125 126 127 128
	switch (param_id) {
		/* General */
	case PROP_VERBOSITY:
		tracker_config_set_verbosity (TRACKER_CONFIG (object),
Martyn Russell's avatar
Martyn Russell committed
129
		                              g_value_get_int (value));
130
		break;
131

132 133 134 135 136
	case PROP_MAX_BYTES:
		tracker_config_set_max_bytes (TRACKER_CONFIG (object),
		                              g_value_get_int (value));
		break;

137 138 139 140
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
141 142 143
}

static void
Martyn Russell's avatar
Martyn Russell committed
144 145 146 147
config_get_property (GObject    *object,
                     guint       param_id,
                     GValue     *value,
                     GParamSpec *pspec)
148 149 150 151 152 153 154 155 156 157 158
{
	TrackerConfigPrivate *priv;

	priv = TRACKER_CONFIG_GET_PRIVATE (object);

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

159 160 161 162
	case PROP_MAX_BYTES:
		g_value_set_int (value, priv->max_bytes);
		break;

163 164 165 166 167 168 169
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
}

static void
170
config_finalize (GObject *object)
171
{
172 173 174
	/* For now we do nothing here, we left this override in for
	 * future expansion.
	 */
175

176
	(G_OBJECT_CLASS (tracker_config_parent_class)->finalize) (object);
177 178 179
}

static void
180
config_constructed (GObject *object)
181
{
182
	(G_OBJECT_CLASS (tracker_config_parent_class)->constructed) (object);
183

184
	config_load (TRACKER_CONFIG (object));
185 186 187
}

static void
188
config_create_with_defaults (TrackerConfig *config,
Martyn Russell's avatar
Martyn Russell committed
189 190
                             GKeyFile      *key_file,
                             gboolean       overwrite)
191
{
192
	gint i;
193

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

196 197
	for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
		gboolean has_key;
Martyn Russell's avatar
Martyn Russell committed
198 199 200 201 202

		has_key = g_key_file_has_key (key_file,
		                              conversions[i].group,
		                              conversions[i].key,
		                              NULL);
203 204 205
		if (!overwrite && has_key) {
			continue;
		}
Martyn Russell's avatar
Martyn Russell committed
206

207 208
		switch (conversions[i].type) {
		case G_TYPE_INT:
Martyn Russell's avatar
Martyn Russell committed
209 210 211 212 213
			g_key_file_set_integer (key_file,
			                        conversions[i].group,
			                        conversions[i].key,
			                        tracker_keyfile_object_default_int (config,
			                                                            conversions[i].property));
214 215 216 217 218
			break;

		default:
			g_assert_not_reached ();
		}
219

Martyn Russell's avatar
Martyn Russell committed
220 221 222 223 224 225
		g_key_file_set_comment (key_file,
		                        conversions[i].group,
		                        conversions[i].key,
		                        tracker_keyfile_object_blurb (config,
		                                                      conversions[i].property),
		                        NULL);
226 227 228 229 230 231
	}
}

static void
config_load (TrackerConfig *config)
{
232
	TrackerConfigFile *file;
233
	gint i;
234

235 236
	file = TRACKER_CONFIG_FILE (config);
	config_create_with_defaults (config, file->key_file, FALSE);
237

238 239
	if (!file->file_exists) {
		tracker_config_file_save (file);
240 241
	}

242 243
	for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
		gboolean has_key;
Martyn Russell's avatar
Martyn Russell committed
244 245 246 247 248 249

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

250 251
		switch (conversions[i].type) {
		case G_TYPE_INT:
Martyn Russell's avatar
Martyn Russell committed
252 253 254 255 256
			tracker_keyfile_object_load_int (G_OBJECT (file),
			                                 conversions[i].property,
			                                 file->key_file,
			                                 conversions[i].group,
			                                 conversions[i].key);
257 258 259 260 261 262
			break;

		default:
			g_assert_not_reached ();
			break;
		}
263 264 265 266 267 268
	}
}

static gboolean
config_save (TrackerConfig *config)
{
269
	TrackerConfigFile *file;
270
	gint i;
271

272
	file = TRACKER_CONFIG_FILE (config);
273

274
	if (!file->key_file) {
275 276 277 278 279 280 281
		g_critical ("Could not save config, GKeyFile was NULL, has the config been loaded?");

		return FALSE;
	}

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

282 283 284
	for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
		switch (conversions[i].type) {
		case G_TYPE_INT:
285
			tracker_keyfile_object_save_int (file,
Martyn Russell's avatar
Martyn Russell committed
286 287 288 289
			                                 conversions[i].property,
			                                 file->key_file,
			                                 conversions[i].group,
			                                 conversions[i].key);
290 291 292 293 294 295
			break;

		default:
			g_assert_not_reached ();
			break;
		}
296 297
	}

298
	return tracker_config_file_save (file);
299 300 301 302 303
}

TrackerConfig *
tracker_config_new (void)
{
304
	return g_object_new (TRACKER_TYPE_CONFIG, NULL);
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
}

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
329
                              gint           value)
330 331 332 333 334
{
	TrackerConfigPrivate *priv;

	g_return_if_fail (TRACKER_IS_CONFIG (config));

335
	if (!tracker_keyfile_object_validate_int (config, "verbosity", value)) {
336 337 338 339 340 341 342 343
		return;
	}

	priv = TRACKER_CONFIG_GET_PRIVATE (config);

	priv->verbosity = value;
	g_object_notify (G_OBJECT (config), "verbosity");
}
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365


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

	g_return_val_if_fail (TRACKER_IS_CONFIG (config), DEFAULT_MAX_BYTES);

	priv = TRACKER_CONFIG_GET_PRIVATE (config);

	return priv->max_bytes;
}

void
tracker_config_set_max_bytes (TrackerConfig *config,
                              gint           value)
{
	TrackerConfigPrivate *priv;

	g_return_if_fail (TRACKER_IS_CONFIG (config));

366
	if (!tracker_keyfile_object_validate_int (config, "max-bytes", value)) {
367 368 369 370 371 372
		return;
	}

	priv = TRACKER_CONFIG_GET_PRIVATE (config);

	priv->max_bytes = value;
373
	g_object_notify (G_OBJECT (config), "max-bytes");
374
}