tracker-config.c 8.66 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 44
	PROP_MAX_BYTES,
	PROP_MAX_MEDIA_ART_WIDTH
45 46
};

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

55
G_DEFINE_TYPE (TrackerConfig, tracker_config, G_TYPE_SETTINGS);
56 57 58 59 60 61 62

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

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

	/* General */
	g_object_class_install_property (object_class,
Martyn Russell's avatar
Martyn Russell committed
69
	                                 PROP_VERBOSITY,
70
	                                 g_param_spec_enum ("verbosity",
71 72 73 74 75
	                                                    "Log verbosity",
	                                                    "Log verbosity (0=errors, 1=minimal, 2=detailed, 3=debug)",
	                                                    TRACKER_TYPE_VERBOSITY,
	                                                    TRACKER_VERBOSITY_ERRORS,
	                                                    G_PARAM_READWRITE));
76 77 78 79 80 81 82 83
	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));
84

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

	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));
103 104 105 106 107 108 109 110
}

static void
tracker_config_init (TrackerConfig *object)
{
}

static void
Martyn Russell's avatar
Martyn Russell committed
111 112 113
config_set_property (GObject      *object,
                     guint         param_id,
                     const GValue *value,
114
                     GParamSpec   *pspec)
115
{
116 117
	TrackerConfig *config = TRACKER_CONFIG (object);

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

127
	/* We don't care about the others... we don't save anyway. */
128
	case PROP_SCHED_IDLE:
129
	case PROP_MAX_BYTES:
130 131 132
	case PROP_MAX_MEDIA_ART_WIDTH:
		break;

133 134 135 136
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
137 138 139
}

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

147 148
	switch (param_id) {
	case PROP_VERBOSITY:
149
		g_value_set_enum (value,
150
		                  tracker_config_get_verbosity (config));
151 152
		break;

153 154
	case PROP_SCHED_IDLE:
		g_value_set_enum (value,
155
		                  tracker_config_get_sched_idle (config));
156 157
		break;

158
	case PROP_MAX_BYTES:
159
		g_value_set_int (value,
160
		                 tracker_config_get_max_bytes (config));
161 162
		break;

163 164
	case PROP_MAX_MEDIA_ART_WIDTH:
		g_value_set_int (value,
165
		                 tracker_config_get_max_media_art_width (config));
166 167
		break;

168 169 170 171 172 173 174
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
}

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

181
	(G_OBJECT_CLASS (tracker_config_parent_class)->finalize) (object);
182 183 184
}

static void
185
config_constructed (GObject *object)
186
{
187
	TrackerConfigFile *config_file;
188
	GSettings *settings;
189

190
	(G_OBJECT_CLASS (tracker_config_parent_class)->constructed) (object);
191

192 193 194 195 196 197 198 199 200
	settings = G_SETTINGS (object);

	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);
201

202 203
	/* Migrate keyfile-based configuration */
	config_file = tracker_config_file_new ();
204

205 206 207
	if (config_file) {
		tracker_config_file_migrate (config_file, G_SETTINGS (object), migration);
		g_object_unref (config_file);
208 209 210 211 212 213
	}
}

TrackerConfig *
tracker_config_new (void)
{
214
	return g_object_new (TRACKER_TYPE_CONFIG,
215 216 217
	                     "schema", "org.freedesktop.Tracker.Extract",
	                     "path", "/org/freedesktop/tracker/extract/",
	                     NULL);
218 219 220 221 222
}

gint
tracker_config_get_verbosity (TrackerConfig *config)
{
223
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), TRACKER_VERBOSITY_ERRORS);
224

225
	return g_settings_get_enum (G_SETTINGS (config), "verbosity");
226 227 228 229
}

void
tracker_config_set_verbosity (TrackerConfig *config,
Martyn Russell's avatar
Martyn Russell committed
230
                              gint           value)
231 232 233
{
	g_return_if_fail (TRACKER_IS_CONFIG (config));

234
	g_settings_set_enum (G_SETTINGS (config), "verbosity", value);
235
}
236

237 238 239 240 241 242 243 244
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");
}

245 246 247
gint
tracker_config_get_max_bytes (TrackerConfig *config)
{
248
	g_return_val_if_fail (TRACKER_IS_CONFIG (config), 0);
249

250
	return g_settings_get_int (G_SETTINGS (config), "max-bytes");
251
}
252 253 254 255 256 257

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

258
	return g_settings_get_int (G_SETTINGS (config), "max-media-art-width");
259
}