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
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
#define DEFAULT_MAX_BYTES 1048576 /* 1Mbyte */
34

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

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

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

enum {
	PROP_0,

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

static ObjectToKeyFile conversions[] = {
	{ G_TYPE_INT,     "verbosity",          GROUP_GENERAL,  "Verbosity"       },
74
	{ G_TYPE_INT,     "max_bytes",          GROUP_GENERAL,  "Max Bytes"       },
75 76
};

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

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

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

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

100 101 102 103 104 105 106 107 108 109
	g_object_class_install_property (object_class,
	                                 PROP_VERBOSITY,
	                                 g_param_spec_int ("max_bytes",
	                                                   "Max Bytes",
	                                                   " Maximum number of UTF-8 bytes to extract [0,G_MAXINT]",
	                                                   0,
	                                                   G_MAXINT,
	                                                   DEFAULT_MAX_BYTES,
	                                                   G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

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

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

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

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

	priv = TRACKER_CONFIG_GET_PRIVATE (object);

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

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

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

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

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

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

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

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

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

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

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

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

		default:
			g_assert_not_reached ();
		}
218

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

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

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

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

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

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

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

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

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

271
	file = TRACKER_CONFIG_FILE (config);
272

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

		return FALSE;
	}

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

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

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

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

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

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

	g_return_if_fail (TRACKER_IS_CONFIG (config));

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

	priv = TRACKER_CONFIG_GET_PRIVATE (config);

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


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));

	if (!tracker_keyfile_object_validate_int (config, "max_bytes", value)) {
		return;
	}

	priv = TRACKER_CONFIG_GET_PRIVATE (config);

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