tracker-config.c 5.99 KB
Newer Older
1
/*
2
 * Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
3 4
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8 9 10
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17 18 19 20 21
 * 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
#include <libtracker-common/tracker-enum-types.h>
#include <libtracker-common/tracker-enums.h>
25 26 27 28

#include "tracker-config.h"

static void     config_set_property         (GObject       *object,
Martyn Russell's avatar
Martyn Russell committed
29 30 31
                                             guint          param_id,
                                             const GValue  *value,
                                             GParamSpec    *pspec);
32
static void     config_get_property         (GObject       *object,
Martyn Russell's avatar
Martyn Russell committed
33 34 35
                                             guint          param_id,
                                             GValue        *value,
                                             GParamSpec    *pspec);
36 37
static void     config_finalize             (GObject       *object);
static void     config_constructed          (GObject       *object);
38 39 40

enum {
	PROP_0,
41 42
	PROP_VERBOSITY,
	PROP_MAX_BYTES
43 44
};

45 46 47 48
static TrackerConfigMigrationEntry migration[] = {
	{ G_TYPE_ENUM, "General", "Verbosity", "verbosity" },
	{ G_TYPE_INT, "General", "MaxBytes", "max-bytes" },
	{ 0 }
49 50
};

51
G_DEFINE_TYPE (TrackerConfig, tracker_config, G_TYPE_SETTINGS);
52 53 54 55 56 57 58

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

	object_class->set_property = config_set_property;
59
	object_class->get_property = config_get_property;
Martyn Russell's avatar
Martyn Russell committed
60
	object_class->finalize     = config_finalize;
61
	object_class->constructed  = config_constructed;
62 63 64

	/* General */
	g_object_class_install_property (object_class,
Martyn Russell's avatar
Martyn Russell committed
65
	                                 PROP_VERBOSITY,
66 67 68 69 70 71
	                                 g_param_spec_enum ("verbosity",
							    "Log verbosity",
							    "Log verbosity (0=errors, 1=minimal, 2=detailed, 3=debug)",
							    TRACKER_TYPE_VERBOSITY,
							    TRACKER_VERBOSITY_ERRORS,
							    G_PARAM_READWRITE));
72

73
	g_object_class_install_property (object_class,
74
	                                 PROP_MAX_BYTES,
75
	                                 g_param_spec_int ("max-bytes",
76
	                                                   "Max Bytes",
77 78 79 80
	                                                   "Maximum number of UTF-8 bytes to extract per file [0->10485760]",
	                                                   0, 1024 * 1024 * 10,
							   1024 * 1024,
	                                                   G_PARAM_READWRITE));
81 82 83 84 85 86 87 88
}

static void
tracker_config_init (TrackerConfig *object)
{
}

static void
Martyn Russell's avatar
Martyn Russell committed
89 90 91
config_set_property (GObject      *object,
                     guint         param_id,
                     const GValue *value,
92
                     GParamSpec   *pspec)
93
{
94 95
	switch (param_id) {
	case PROP_VERBOSITY:
96 97
		g_settings_set_enum (G_SETTINGS (object), "verbosity",
		                     g_value_get_enum (value));
98
		break;
99

100
	case PROP_MAX_BYTES:
101 102
		g_settings_set_int (G_SETTINGS (object), "max-bytes",
		                    g_value_get_int (value));
103 104
		break;

105 106 107 108
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
109 110 111
}

static void
Martyn Russell's avatar
Martyn Russell committed
112 113 114 115
config_get_property (GObject    *object,
                     guint       param_id,
                     GValue     *value,
                     GParamSpec *pspec)
116 117 118
{
	switch (param_id) {
	case PROP_VERBOSITY:
119 120
		g_value_set_enum (value,
		                  g_settings_get_enum (G_SETTINGS (object), "verbosity"));
121 122
		break;

123
	case PROP_MAX_BYTES:
124 125
		g_value_set_int (value,
		                 g_settings_get_int (G_SETTINGS (object), "max-bytes"));
126 127
		break;

128 129 130 131 132 133 134
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
}

static void
135
config_finalize (GObject *object)
136
{
137 138 139
	/* For now we do nothing here, we left this override in for
	 * future expansion.
	 */
140

141
	(G_OBJECT_CLASS (tracker_config_parent_class)->finalize) (object);
142 143 144
}

static void
145
config_constructed (GObject *object)
146
{
147
	TrackerConfigFile *config_file;
148

149
	(G_OBJECT_CLASS (tracker_config_parent_class)->constructed) (object);
150

151
	g_settings_delay (G_SETTINGS (object));
152

153 154
	/* Migrate keyfile-based configuration */
	config_file = tracker_config_file_new ();
155

156 157 158
	if (config_file) {
		tracker_config_file_migrate (config_file, G_SETTINGS (object), migration);
		g_object_unref (config_file);
159 160 161 162 163 164
	}
}

TrackerConfig *
tracker_config_new (void)
{
165
	return g_object_new (TRACKER_TYPE_CONFIG,
166 167
			     "schema", "org.freedesktop.Tracker.Extract",
			     "path", "/org/freedesktop/tracker/extract/",
168
			     NULL);
169 170 171 172 173
}

gint
tracker_config_get_verbosity (TrackerConfig *config)
{
174
	gint verbosity;
175

176
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), TRACKER_VERBOSITY_ERRORS);
177

178 179
	g_object_get (config, "verbosity", &verbosity, NULL);
	return verbosity;
180 181 182 183
}

void
tracker_config_set_verbosity (TrackerConfig *config,
Martyn Russell's avatar
Martyn Russell committed
184
                              gint           value)
185 186 187
{
	g_return_if_fail (TRACKER_IS_CONFIG (config));

188
	g_object_set (G_OBJECT (config), "verbosity", value, NULL);
189
}
190 191 192 193 194


gint
tracker_config_get_max_bytes (TrackerConfig *config)
{
195
	gint max_bytes;
196

197
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), 0);
198

199 200
	g_object_get (config, "max-bytes", &max_bytes, NULL);
	return max_bytes;
201 202 203 204 205 206 207 208
}

void
tracker_config_set_max_bytes (TrackerConfig *config,
                              gint           value)
{
	g_return_if_fail (TRACKER_IS_CONFIG (config));

209
	g_object_set (G_OBJECT (config), "max-bytes", value, NULL);
210
}