tracker-config.c 10.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 23 24 25
#define G_SETTINGS_ENABLE_BACKEND
#include <gio/gsettingsbackend.h>

#include <libtracker-common/tracker-common.h>
26 27 28

#include "tracker-config.h"

29 30 31 32 33 34 35
#define CONFIG_SCHEME "org.freedesktop.Tracker.Extract"
#define CONFIG_PATH   "/org/freedesktop/tracker/extract/"

typedef struct {
	GSettings *settings;
} TrackerConfigPrivate;

36
static void     config_set_property         (GObject       *object,
Martyn Russell's avatar
Martyn Russell committed
37 38 39
                                             guint          param_id,
                                             const GValue  *value,
                                             GParamSpec    *pspec);
40
static void     config_get_property         (GObject       *object,
Martyn Russell's avatar
Martyn Russell committed
41 42 43
                                             guint          param_id,
                                             GValue        *value,
                                             GParamSpec    *pspec);
44 45
static void     config_finalize             (GObject       *object);
static void     config_constructed          (GObject       *object);
46 47 48

enum {
	PROP_0,
49
	PROP_SETTINGS,
50
	PROP_VERBOSITY,
51
	PROP_SCHED_IDLE,
52
	PROP_MAX_BYTES,
53 54
	PROP_MAX_MEDIA_ART_WIDTH,
	PROP_WAIT_FOR_MINER_FS,
55 56
};

57
G_DEFINE_TYPE (TrackerConfig, tracker_config, G_TYPE_OBJECT);
58 59 60 61 62 63 64

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

	object_class->set_property = config_set_property;
65
	object_class->get_property = config_get_property;
Martyn Russell's avatar
Martyn Russell committed
66
	object_class->finalize     = config_finalize;
67
	object_class->constructed  = config_constructed;
68

69 70 71 72 73 74 75 76
	g_object_class_install_property (object_class,
	                                 PROP_SETTINGS,
	                                 g_param_spec_object ("settings",
	                                                      "GSettings",
	                                                      "GSettings GObject used",
	                                                      G_TYPE_SETTINGS,
	                                                      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));

77 78
	/* General */
	g_object_class_install_property (object_class,
Martyn Russell's avatar
Martyn Russell committed
79
	                                 PROP_VERBOSITY,
80
	                                 g_param_spec_enum ("verbosity",
81 82 83 84 85
	                                                    "Log verbosity",
	                                                    "Log verbosity (0=errors, 1=minimal, 2=detailed, 3=debug)",
	                                                    TRACKER_TYPE_VERBOSITY,
	                                                    TRACKER_VERBOSITY_ERRORS,
	                                                    G_PARAM_READWRITE));
86 87 88 89 90 91 92 93
	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));
94

95
	g_object_class_install_property (object_class,
96
	                                 PROP_MAX_BYTES,
97
	                                 g_param_spec_int ("max-bytes",
98
	                                                   "Max Bytes",
99 100
	                                                   "Maximum number of UTF-8 bytes to extract per file [0->10485760]",
	                                                   0, 1024 * 1024 * 10,
101
	                                                   1024 * 1024,
102
	                                                   G_PARAM_READWRITE));
103 104 105 106 107 108 109 110 111 112

	g_object_class_install_property (object_class,
	                                 PROP_MAX_MEDIA_ART_WIDTH,
	                                 g_param_spec_int ("max-media-art-width",
	                                                   "Max Media Art Width",
	                                                   " Maximum width of the Media Art to be generated (-1=disable, 0=original width, 1->2048=max pixel width)",
	                                                   -1,
	                                                   2048,
	                                                   0,
	                                                   G_PARAM_READWRITE));
113 114 115 116 117 118 119 120

	g_object_class_install_property (object_class,
	                                 PROP_WAIT_FOR_MINER_FS,
	                                 g_param_spec_boolean ("wait-for-miner-fs",
	                                                       "Wait for FS miner to be done before extracting",
	                                                       "%TRUE to wait for tracker-miner-fs is done before extracting. %FAlSE otherwise",
	                                                       FALSE,
	                                                       G_PARAM_READWRITE));
121 122

	g_type_class_add_private (object_class, sizeof (TrackerConfigPrivate));
123 124 125 126 127
}

static void
tracker_config_init (TrackerConfig *object)
{
128
	object->priv = G_TYPE_INSTANCE_GET_PRIVATE (object, TRACKER_TYPE_CONFIG, TrackerConfigPrivate);
129 130 131
}

static void
Martyn Russell's avatar
Martyn Russell committed
132 133 134
config_set_property (GObject      *object,
                     guint         param_id,
                     const GValue *value,
135
                     GParamSpec   *pspec)
136
{
137 138
	TrackerConfig *config = TRACKER_CONFIG (object);

139
	switch (param_id) {
140 141 142 143 144 145 146 147
	case PROP_SETTINGS: {
		TrackerConfigPrivate *priv;

		priv = config->priv;
		priv->settings = g_value_get_object (value);
		break;
	}

148 149 150 151
	/* General */
	/* NOTE: We handle these because we have to be able
	 * to save these based on command line overrides.
	 */
152
	case PROP_VERBOSITY:
153
		tracker_config_set_verbosity (config, g_value_get_enum (value));
154
		break;
155

156
	/* We don't care about the others... we don't save anyway. */
157
	case PROP_SCHED_IDLE:
158
	case PROP_MAX_BYTES:
159
	case PROP_MAX_MEDIA_ART_WIDTH:
160
	case PROP_WAIT_FOR_MINER_FS:
161 162
		break;

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

static void
Martyn Russell's avatar
Martyn Russell committed
170 171 172 173
config_get_property (GObject    *object,
                     guint       param_id,
                     GValue     *value,
                     GParamSpec *pspec)
174
{
175 176
	TrackerConfig *config = TRACKER_CONFIG (object);

177 178
	switch (param_id) {
	case PROP_VERBOSITY:
179
		g_value_set_enum (value,
180
		                  tracker_config_get_verbosity (config));
181 182
		break;

183 184
	case PROP_SCHED_IDLE:
		g_value_set_enum (value,
185
		                  tracker_config_get_sched_idle (config));
186 187
		break;

188
	case PROP_MAX_BYTES:
189
		g_value_set_int (value,
190
		                 tracker_config_get_max_bytes (config));
191 192
		break;

193 194
	case PROP_MAX_MEDIA_ART_WIDTH:
		g_value_set_int (value,
195
		                 tracker_config_get_max_media_art_width (config));
196 197
		break;

198 199 200 201 202
	case PROP_WAIT_FOR_MINER_FS:
		g_value_set_boolean (value,
		                     tracker_config_get_wait_for_miner_fs (config));
		break;

203 204 205 206 207 208 209
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
}

static void
210
config_finalize (GObject *object)
211
{
212 213 214
	/* For now we do nothing here, we left this override in for
	 * future expansion.
	 */
215

216
	(G_OBJECT_CLASS (tracker_config_parent_class)->finalize) (object);
217 218 219
}

static void
220
config_constructed (GObject *object)
221
{
222
	TrackerConfigPrivate *priv;
223
	GSettings *settings;
224

225
	(G_OBJECT_CLASS (tracker_config_parent_class)->constructed) (object);
226

227 228 229 230 231 232
	priv = TRACKER_CONFIG (object)->priv;
	settings = priv->settings;

	if (!settings) {
		priv->settings = settings = g_settings_new_with_path (CONFIG_SCHEME, CONFIG_PATH);
	}
233 234 235 236 237 238 239 240

	g_settings_delay (settings);

	/* Set up bindings */
	g_settings_bind (settings, "verbosity", object, "verbosity", G_SETTINGS_BIND_GET_NO_CHANGES);
	g_settings_bind (settings, "sched-idle", object, "sched-idle", G_SETTINGS_BIND_GET);
	g_settings_bind (settings, "max-bytes", object, "max-bytes", G_SETTINGS_BIND_GET);
	g_settings_bind (settings, "max-media-art-width", object, "max-media-art-width", G_SETTINGS_BIND_GET);
241
	g_settings_bind (settings, "wait-for-miner-fs", object, "wait-for-miner-fs", G_SETTINGS_BIND_GET);
242 243 244 245 246
}

TrackerConfig *
tracker_config_new (void)
{
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	GSettings *settings = NULL;

	if (G_UNLIKELY (g_getenv ("TRACKER_USE_CONFIG_FILES"))) {
		/* FIXME: should we unset GSETTINGS_BACKEND env var? */
		GSettingsBackend *backend;
		gchar *filename, *basename;

		basename = g_strdup_printf ("%s.cfg", g_get_prgname ());
		filename = g_build_filename (g_get_user_config_dir (), "tracker", basename, NULL);
		g_free (basename);

		backend = g_keyfile_settings_backend_new (filename, CONFIG_PATH, NULL);
		g_free (filename);

		settings = g_settings_new_with_backend (CONFIG_SCHEME, backend);
		g_object_unref (backend);
	}

	return g_object_new (TRACKER_TYPE_CONFIG, "settings", settings, NULL);
266 267 268 269 270
}

gint
tracker_config_get_verbosity (TrackerConfig *config)
{
271
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), TRACKER_VERBOSITY_ERRORS);
272

273
	return g_settings_get_enum (G_SETTINGS (config), "verbosity");
274 275 276 277
}

void
tracker_config_set_verbosity (TrackerConfig *config,
Martyn Russell's avatar
Martyn Russell committed
278
                              gint           value)
279 280 281
{
	g_return_if_fail (TRACKER_IS_CONFIG (config));

282
	g_settings_set_enum (G_SETTINGS (config), "verbosity", value);
283
}
284

285 286 287 288 289 290 291 292
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");
}

293 294 295
gint
tracker_config_get_max_bytes (TrackerConfig *config)
{
296
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), 0);
297

298
	return g_settings_get_int (G_SETTINGS (config), "max-bytes");
299
}
300 301 302 303 304 305

gint
tracker_config_get_max_media_art_width (TrackerConfig *config)
{
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), 0);

306
	return g_settings_get_int (G_SETTINGS (config), "max-media-art-width");
307
}
308 309 310 311 312 313 314 315

gboolean
tracker_config_get_wait_for_miner_fs (TrackerConfig *config)
{
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), FALSE);

	return g_settings_get_boolean (G_SETTINGS (config), "wait-for-miner-fs");
}