tracker-config.c 7.7 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
	PROP_VERBOSITY,
42
	PROP_SCHED_IDLE,
43
	PROP_MAX_BYTES
44 45
};

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

53
G_DEFINE_TYPE (TrackerConfig, tracker_config, G_TYPE_SETTINGS);
54 55 56 57 58 59 60

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

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

	/* General */
	g_object_class_install_property (object_class,
Martyn Russell's avatar
Martyn Russell committed
67
	                                 PROP_VERBOSITY,
68
	                                 g_param_spec_enum ("verbosity",
69 70 71 72 73
	                                                    "Log verbosity",
	                                                    "Log verbosity (0=errors, 1=minimal, 2=detailed, 3=debug)",
	                                                    TRACKER_TYPE_VERBOSITY,
	                                                    TRACKER_VERBOSITY_ERRORS,
	                                                    G_PARAM_READWRITE));
74 75 76 77 78 79 80 81
	g_object_class_install_property (object_class,
	                                 PROP_SCHED_IDLE,
	                                 g_param_spec_enum ("sched-idle",
	                                                    "Scheduler priority when idle",
	                                                    "Scheduler priority when idle (0=always, 1=first-index, 2=never)",
	                                                    TRACKER_TYPE_SCHED_IDLE,
	                                                    TRACKER_SCHED_IDLE_FIRST_INDEX,
	                                                    G_PARAM_READWRITE));
82

83
	g_object_class_install_property (object_class,
84
	                                 PROP_MAX_BYTES,
85
	                                 g_param_spec_int ("max-bytes",
86
	                                                   "Max Bytes",
87 88
	                                                   "Maximum number of UTF-8 bytes to extract per file [0->10485760]",
	                                                   0, 1024 * 1024 * 10,
89
	                                                   1024 * 1024,
90
	                                                   G_PARAM_READWRITE));
91 92 93 94 95 96 97 98
}

static void
tracker_config_init (TrackerConfig *object)
{
}

static void
Martyn Russell's avatar
Martyn Russell committed
99 100 101
config_set_property (GObject      *object,
                     guint         param_id,
                     const GValue *value,
102
                     GParamSpec   *pspec)
103
{
104 105
	switch (param_id) {
	case PROP_VERBOSITY:
106 107
		g_settings_set_enum (G_SETTINGS (object), "verbosity",
		                     g_value_get_enum (value));
108
		break;
109

110 111 112 113 114
	case PROP_SCHED_IDLE:
		g_settings_set_enum (G_SETTINGS (object), "sched-idle",
		                     g_value_get_enum (value));
		break;

115
	case PROP_MAX_BYTES:
116 117
		g_settings_set_int (G_SETTINGS (object), "max-bytes",
		                    g_value_get_int (value));
118 119
		break;

120 121 122 123
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
124 125 126
}

static void
Martyn Russell's avatar
Martyn Russell committed
127 128 129 130
config_get_property (GObject    *object,
                     guint       param_id,
                     GValue     *value,
                     GParamSpec *pspec)
131 132 133
{
	switch (param_id) {
	case PROP_VERBOSITY:
134 135
		g_value_set_enum (value,
		                  g_settings_get_enum (G_SETTINGS (object), "verbosity"));
136 137
		break;

138 139 140 141 142
	case PROP_SCHED_IDLE:
		g_value_set_enum (value,
		                  g_settings_get_enum (G_SETTINGS (object), "sched-idle"));
		break;

143
	case PROP_MAX_BYTES:
144 145
		g_value_set_int (value,
		                 g_settings_get_int (G_SETTINGS (object), "max-bytes"));
146 147
		break;

148 149 150 151 152 153 154
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
}

static void
155
config_finalize (GObject *object)
156
{
157 158 159
	/* For now we do nothing here, we left this override in for
	 * future expansion.
	 */
160

161
	(G_OBJECT_CLASS (tracker_config_parent_class)->finalize) (object);
162 163 164
}

static void
165
config_constructed (GObject *object)
166
{
167
	TrackerConfigFile *config_file;
168

169
	(G_OBJECT_CLASS (tracker_config_parent_class)->constructed) (object);
170

171
	g_settings_delay (G_SETTINGS (object));
172

173 174
	/* Migrate keyfile-based configuration */
	config_file = tracker_config_file_new ();
175

176 177 178
	if (config_file) {
		tracker_config_file_migrate (config_file, G_SETTINGS (object), migration);
		g_object_unref (config_file);
179 180 181 182 183 184
	}
}

TrackerConfig *
tracker_config_new (void)
{
185
	return g_object_new (TRACKER_TYPE_CONFIG,
186 187 188
	                     "schema", "org.freedesktop.Tracker.Extract",
	                     "path", "/org/freedesktop/tracker/extract/",
	                     NULL);
189 190 191 192 193
}

gint
tracker_config_get_verbosity (TrackerConfig *config)
{
194
	gint verbosity;
195

196
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), TRACKER_VERBOSITY_ERRORS);
197

198
	g_object_get (config, "verbosity", &verbosity, NULL);
199

200
	return verbosity;
201 202 203 204
}

void
tracker_config_set_verbosity (TrackerConfig *config,
Martyn Russell's avatar
Martyn Russell committed
205
                              gint           value)
206 207 208
{
	g_return_if_fail (TRACKER_IS_CONFIG (config));

209
	g_object_set (G_OBJECT (config), "verbosity", value, NULL);
210
}
211

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
gint
tracker_config_get_sched_idle (TrackerConfig *config)
{
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), TRACKER_SCHED_IDLE_FIRST_INDEX);

	return g_settings_get_enum (G_SETTINGS (config), "sched-idle");
}

void
tracker_config_set_sched_idle (TrackerConfig *config,
                               gint           value)
{

	g_return_if_fail (TRACKER_IS_CONFIG (config));

	g_settings_set_enum (G_SETTINGS (config), "sched-idle", value);
	g_object_notify (G_OBJECT (config), "sched-idle");
}
230 231 232 233

gint
tracker_config_get_max_bytes (TrackerConfig *config)
{
234
	gint max_bytes;
235

236
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), 0);
237

238
	g_object_get (config, "max-bytes", &max_bytes, NULL);
239

240
	return max_bytes;
241 242 243 244 245 246 247 248
}

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

249
	g_object_set (G_OBJECT (config), "max-bytes", value, NULL);
250
}