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

23 24 25 26
#define G_SETTINGS_ENABLE_BACKEND
#include <gio/gsettingsbackend.h>

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

#include "tracker-config.h"

30
#define CONFIG_SCHEMA "org.freedesktop.Tracker.Extract"
31 32
#define CONFIG_PATH   "/org/freedesktop/tracker/extract/"

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

enum {
	PROP_0,
46
	PROP_VERBOSITY,
47
	PROP_SCHED_IDLE,
48
	PROP_MAX_BYTES,
49 50
	PROP_MAX_MEDIA_ART_WIDTH,
	PROP_WAIT_FOR_MINER_FS,
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 99 100

	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));
101 102 103 104 105 106 107 108

	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));
109 110 111 112 113 114 115 116
}

static void
tracker_config_init (TrackerConfig *object)
{
}

static void
Martyn Russell's avatar
Martyn Russell committed
117 118 119
config_set_property (GObject      *object,
                     guint         param_id,
                     const GValue *value,
120
                     GParamSpec   *pspec)
121
{
122 123
	TrackerConfig *config = TRACKER_CONFIG (object);

124
	switch (param_id) {
125 126 127 128
	/* General */
	/* NOTE: We handle these because we have to be able
	 * to save these based on command line overrides.
	 */
129
	case PROP_VERBOSITY:
130
		tracker_config_set_verbosity (config, g_value_get_enum (value));
131
		break;
132

133
	/* We don't care about the others... we don't save anyway. */
134
	case PROP_SCHED_IDLE:
135
	case PROP_MAX_BYTES:
136
	case PROP_MAX_MEDIA_ART_WIDTH:
137
	case PROP_WAIT_FOR_MINER_FS:
138 139
		break;

140 141 142 143
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
144 145 146
}

static void
Martyn Russell's avatar
Martyn Russell committed
147 148 149 150
config_get_property (GObject    *object,
                     guint       param_id,
                     GValue     *value,
                     GParamSpec *pspec)
151
{
152 153
	TrackerConfig *config = TRACKER_CONFIG (object);

154 155
	switch (param_id) {
	case PROP_VERBOSITY:
156
		g_value_set_enum (value,
157
		                  tracker_config_get_verbosity (config));
158 159
		break;

160 161
	case PROP_SCHED_IDLE:
		g_value_set_enum (value,
162
		                  tracker_config_get_sched_idle (config));
163 164
		break;

165
	case PROP_MAX_BYTES:
166
		g_value_set_int (value,
167
		                 tracker_config_get_max_bytes (config));
168 169
		break;

170 171
	case PROP_MAX_MEDIA_ART_WIDTH:
		g_value_set_int (value,
172
		                 tracker_config_get_max_media_art_width (config));
173 174
		break;

175 176 177 178 179
	case PROP_WAIT_FOR_MINER_FS:
		g_value_set_boolean (value,
		                     tracker_config_get_wait_for_miner_fs (config));
		break;

180 181 182 183 184 185 186
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
}

static void
187
config_finalize (GObject *object)
188
{
189 190 191
	/* For now we do nothing here, we left this override in for
	 * future expansion.
	 */
192

193
	(G_OBJECT_CLASS (tracker_config_parent_class)->finalize) (object);
194 195 196
}

static void
197
config_constructed (GObject *object)
198
{
199
	GSettings *settings;
200

201
	(G_OBJECT_CLASS (tracker_config_parent_class)->constructed) (object);
202

203
	settings = G_SETTINGS (object);
204

205 206
	if (G_LIKELY (!g_getenv ("TRACKER_USE_CONFIG_FILES"))) {
		g_settings_delay (settings);
207
	}
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	/* Set up bindings:
	 *
	 * What's interesting here is that 'verbosity' and
	 * 'initial-sleep' are command line arguments that can be
	 * overridden, so we don't update the config when we set them
	 * from main() because it's a session configuration only, not
	 * a permanent one. To do this we use the flag
	 * G_SETTINGS_BIND_GET_NO_CHANGES.
	 *
	 * For the other settings, we don't bind the
	 * G_SETTINGS_BIND_SET because we don't want to save anything,
	 * ever, we only want to know about updates to the settings as
	 * they're changed externally. The only time this may be
	 * different is where we use the environment variable
	 * TRACKER_USE_CONFIG_FILES and we want to write a config
	 * file for convenience. But this is only necessary if the
	 * config is different to the default.
	 */
	g_settings_bind (settings, "verbosity", object, "verbosity", G_SETTINGS_BIND_GET | G_SETTINGS_BIND_GET_NO_CHANGES);
228 229 230
	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);
231
	g_settings_bind (settings, "wait-for-miner-fs", object, "wait-for-miner-fs", G_SETTINGS_BIND_GET);
232 233 234 235 236
}

TrackerConfig *
tracker_config_new (void)
{
237 238 239
	TrackerConfig *config = NULL;

	/* FIXME: should we unset GSETTINGS_BACKEND env var? */
240 241 242 243

	if (G_UNLIKELY (g_getenv ("TRACKER_USE_CONFIG_FILES"))) {
		GSettingsBackend *backend;
		gchar *filename, *basename;
244
		gboolean need_to_save;
245 246 247 248 249

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

250 251 252 253
		need_to_save = g_file_test (filename, G_FILE_TEST_EXISTS) == FALSE;

		backend = g_keyfile_settings_backend_new (filename, CONFIG_PATH, "General");
		g_info ("Using config file '%s'", filename);
254 255
		g_free (filename);

256 257 258 259 260
		config = g_object_new (TRACKER_TYPE_CONFIG,
		                       "backend", backend,
		                       "schema-id", CONFIG_SCHEMA,
		                       "path", CONFIG_PATH,
		                       NULL);
261
		g_object_unref (backend);
262 263 264 265 266 267 268 269 270

		if (need_to_save) {
			g_info ("  Config file does not exist, using default values...");
		}
	} else {
		config = g_object_new (TRACKER_TYPE_CONFIG,
		                       "schema-id", CONFIG_SCHEMA,
		                       "path", CONFIG_PATH,
		                       NULL);
271 272
	}

273
	return config;
274 275 276 277 278
}

gint
tracker_config_get_verbosity (TrackerConfig *config)
{
279
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), TRACKER_VERBOSITY_ERRORS);
280

281
	return g_settings_get_enum (G_SETTINGS (config), "verbosity");
282 283 284 285
}

void
tracker_config_set_verbosity (TrackerConfig *config,
Martyn Russell's avatar
Martyn Russell committed
286
                              gint           value)
287 288 289
{
	g_return_if_fail (TRACKER_IS_CONFIG (config));

290
	g_settings_set_enum (G_SETTINGS (config), "verbosity", value);
291
}
292

293 294 295 296 297 298 299 300
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");
}

301 302 303
gint
tracker_config_get_max_bytes (TrackerConfig *config)
{
304
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), 0);
305

306
	return g_settings_get_int (G_SETTINGS (config), "max-bytes");
307
}
308 309 310 311 312 313

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

314
	return g_settings_get_int (G_SETTINGS (config), "max-media-art-width");
315
}
316 317 318 319 320 321 322 323

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