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

26
#include <libtracker-miners-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
	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));
99 100 101 102 103 104 105 106
}

static void
tracker_config_init (TrackerConfig *object)
{
}

static void
Martyn Russell's avatar
Martyn Russell committed
107 108 109
config_set_property (GObject      *object,
                     guint         param_id,
                     const GValue *value,
110
                     GParamSpec   *pspec)
111
{
112 113
	TrackerConfig *config = TRACKER_CONFIG (object);

114
	switch (param_id) {
115 116 117 118
	/* General */
	/* NOTE: We handle these because we have to be able
	 * to save these based on command line overrides.
	 */
119
	case PROP_VERBOSITY:
120
		tracker_config_set_verbosity (config, g_value_get_enum (value));
121
		break;
122

123
	/* We don't care about the others... we don't save anyway. */
124
	case PROP_SCHED_IDLE:
125
	case PROP_MAX_BYTES:
126
	case PROP_MAX_MEDIA_ART_WIDTH:
127
	case PROP_WAIT_FOR_MINER_FS:
128 129
		break;

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

static void
Martyn Russell's avatar
Martyn Russell committed
137 138 139 140
config_get_property (GObject    *object,
                     guint       param_id,
                     GValue     *value,
                     GParamSpec *pspec)
141
{
142 143
	TrackerConfig *config = TRACKER_CONFIG (object);

144 145
	switch (param_id) {
	case PROP_VERBOSITY:
146
		g_value_set_enum (value,
147
		                  tracker_config_get_verbosity (config));
148 149
		break;

150 151
	case PROP_SCHED_IDLE:
		g_value_set_enum (value,
152
		                  tracker_config_get_sched_idle (config));
153 154
		break;

155
	case PROP_MAX_BYTES:
156
		g_value_set_int (value,
157
		                 tracker_config_get_max_bytes (config));
158 159
		break;

160 161 162 163 164
	case PROP_WAIT_FOR_MINER_FS:
		g_value_set_boolean (value,
		                     tracker_config_get_wait_for_miner_fs (config));
		break;

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

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

178
	(G_OBJECT_CLASS (tracker_config_parent_class)->finalize) (object);
179 180 181
}

static void
182
config_constructed (GObject *object)
183
{
184
	GSettings *settings;
185

186
	(G_OBJECT_CLASS (tracker_config_parent_class)->constructed) (object);
187

188
	settings = G_SETTINGS (object);
189

190 191
	if (G_LIKELY (!g_getenv ("TRACKER_USE_CONFIG_FILES"))) {
		g_settings_delay (settings);
192
	}
193

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	/* 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);
213
	g_settings_bind (settings, "sched-idle", object, "sched-idle", G_SETTINGS_BIND_GET);
214
	g_settings_bind (settings, "wait-for-miner-fs", object, "wait-for-miner-fs", G_SETTINGS_BIND_GET);
215 216 217 218 219 220

	/* Cache settings accessed from extractor modules, we don't want
	 * the GSettings object accessed within these as it may trigger
	 * unintended open() calls.
	 */
	TRACKER_CONFIG (settings)->max_bytes = g_settings_get_int (settings, "max-bytes");
221 222 223 224 225
}

TrackerConfig *
tracker_config_new (void)
{
226 227 228
	TrackerConfig *config = NULL;

	/* FIXME: should we unset GSETTINGS_BACKEND env var? */
229 230 231 232

	if (G_UNLIKELY (g_getenv ("TRACKER_USE_CONFIG_FILES"))) {
		GSettingsBackend *backend;
		gchar *filename, *basename;
233
		gboolean need_to_save;
234 235 236 237 238

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

239 240 241 242
		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);
243 244
		g_free (filename);

245 246 247 248 249
		config = g_object_new (TRACKER_TYPE_CONFIG,
		                       "backend", backend,
		                       "schema-id", CONFIG_SCHEMA,
		                       "path", CONFIG_PATH,
		                       NULL);
250
		g_object_unref (backend);
251 252 253 254 255 256 257 258 259

		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);
260 261
	}

262
	return config;
263 264 265 266 267
}

gint
tracker_config_get_verbosity (TrackerConfig *config)
{
268
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), TRACKER_VERBOSITY_ERRORS);
269

270
	return g_settings_get_enum (G_SETTINGS (config), "verbosity");
271 272 273 274
}

void
tracker_config_set_verbosity (TrackerConfig *config,
Martyn Russell's avatar
Martyn Russell committed
275
                              gint           value)
276 277 278
{
	g_return_if_fail (TRACKER_IS_CONFIG (config));

279
	g_settings_set_enum (G_SETTINGS (config), "verbosity", value);
280
}
281

282 283 284 285 286 287 288 289
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");
}

290 291 292
gint
tracker_config_get_max_bytes (TrackerConfig *config)
{
293
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), 0);
294

295
	return config->max_bytes;
296
}
297

298 299 300 301 302 303 304
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");
}