tracker-0.16.2/src/libtracker-data/tracker-db-config.c

No issues found

  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  * This library is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with this library; if not, write to the Free Software
 16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 17  * 02110-1301  USA
 18  */
 19 
 20 #include "config.h"
 21 
 22 #include <string.h>
 23 #include <stdlib.h>
 24 
 25 #include <glib.h>
 26 #include <gio/gio.h>
 27 
 28 #include <libtracker-common/tracker-keyfile-object.h>
 29 
 30 #include "tracker-db-config.h"
 31 
 32 /* GKeyFile defines */
 33 #define GROUP_JOURNAL     "Journal"
 34 
 35 /* Default values */
 36 #define DEFAULT_JOURNAL_CHUNK_SIZE           50
 37 #define DEFAULT_JOURNAL_ROTATE_DESTINATION   ""
 38 
 39 static void config_set_property (GObject      *object,
 40                                  guint         param_id,
 41                                  const GValue *value,
 42                                  GParamSpec   *pspec);
 43 static void config_get_property (GObject      *object,
 44                                  guint         param_id,
 45                                  GValue       *value,
 46                                  GParamSpec   *pspec);
 47 static void config_finalize     (GObject      *object);
 48 static void config_constructed  (GObject      *object);
 49 
 50 enum {
 51 	PROP_0,
 52 
 53 	/* Journal */
 54 	PROP_JOURNAL_CHUNK_SIZE,
 55 	PROP_JOURNAL_ROTATE_DESTINATION
 56 };
 57 
 58 static TrackerConfigMigrationEntry migration[] = {
 59 	{ G_TYPE_INT, GROUP_JOURNAL, "JournalChunkSize", "journal-chunk-size" },
 60 	{ G_TYPE_STRING, GROUP_JOURNAL, "JournalRotateDestination", "journal-rotate-destination" },
 61 };
 62 
 63 G_DEFINE_TYPE (TrackerDBConfig, tracker_db_config, G_TYPE_SETTINGS);
 64 
 65 static void
 66 tracker_db_config_class_init (TrackerDBConfigClass *klass)
 67 {
 68 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
 69 
 70 	object_class->set_property = config_set_property;
 71 	object_class->get_property = config_get_property;
 72 	object_class->finalize     = config_finalize;
 73 	object_class->constructed  = config_constructed;
 74 
 75 	g_object_class_install_property (object_class,
 76 	                                 PROP_JOURNAL_CHUNK_SIZE,
 77 	                                 g_param_spec_int ("journal-chunk-size",
 78 	                                                   "Journal chunk size",
 79 	                                                   " Size of the journal at rotation in MB. Use -1 to disable rotating",
 80 	                                                   -1,
 81 	                                                   G_MAXINT,
 82 	                                                   DEFAULT_JOURNAL_CHUNK_SIZE,
 83 	                                                   G_PARAM_READWRITE));
 84 
 85 	g_object_class_install_property (object_class,
 86 	                                 PROP_JOURNAL_ROTATE_DESTINATION,
 87 	                                 g_param_spec_string ("journal-rotate-destination",
 88 	                                                      "Journal rotate destination",
 89 	                                                      " Destination to rotate journal chunks to",
 90 	                                                      DEFAULT_JOURNAL_ROTATE_DESTINATION,
 91 	                                                      G_PARAM_READWRITE));
 92 
 93 }
 94 
 95 static void
 96 tracker_db_config_init (TrackerDBConfig *object)
 97 {
 98 }
 99 
100 static void
101 config_set_property (GObject      *object,
102                      guint         param_id,
103                      const GValue *value,
104                      GParamSpec           *pspec)
105 {
106 	switch (param_id) {
107 		/* Journal */
108 	case PROP_JOURNAL_CHUNK_SIZE:
109 		tracker_db_config_set_journal_chunk_size (TRACKER_DB_CONFIG (object),
110 		                                          g_value_get_int(value));
111 		break;
112 	case PROP_JOURNAL_ROTATE_DESTINATION:
113 		tracker_db_config_set_journal_rotate_destination (TRACKER_DB_CONFIG (object),
114 		                                                  g_value_get_string(value));
115 		break;
116 	default:
117 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
118 		break;
119 	};
120 }
121 
122 static void
123 config_get_property (GObject    *object,
124                      guint       param_id,
125                      GValue     *value,
126                      GParamSpec *pspec)
127 {
128 	TrackerDBConfig *config = TRACKER_DB_CONFIG (object);
129 
130 	switch (param_id) {
131 	case PROP_JOURNAL_CHUNK_SIZE:
132 		g_value_set_int (value, tracker_db_config_get_journal_chunk_size (config));
133 		break;
134 	case PROP_JOURNAL_ROTATE_DESTINATION:
135 		g_value_take_string (value, tracker_db_config_get_journal_rotate_destination (config));
136 		break;
137 	default:
138 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
139 		break;
140 	};
141 }
142 
143 static void
144 config_finalize (GObject *object)
145 {
146 	(G_OBJECT_CLASS (tracker_db_config_parent_class)->finalize) (object);
147 }
148 
149 
150 static void
151 config_constructed (GObject *object)
152 {
153 	TrackerConfigFile *config_file;
154 
155 	(G_OBJECT_CLASS (tracker_db_config_parent_class)->constructed) (object);
156 
157 	g_settings_delay (G_SETTINGS (object));
158 
159 	/* Migrate keyfile-based configuration */
160 	config_file = tracker_config_file_new ();
161 	if (config_file) {
162 		tracker_config_file_migrate (config_file,
163 		                             G_SETTINGS (object), migration);
164 		g_object_unref (config_file);
165 	}
166 }
167 
168 TrackerDBConfig *
169 tracker_db_config_new (void)
170 {
171 	return g_object_new (TRACKER_TYPE_DB_CONFIG,
172 	                     "schema", "org.freedesktop.Tracker.DB",
173 	                     "path", "/org/freedesktop/tracker/db/",
174 	                     NULL);
175 }
176 
177 gboolean
178 tracker_db_config_save (TrackerDBConfig *config)
179 {
180 	g_return_val_if_fail (TRACKER_IS_DB_CONFIG (config), FALSE);
181 
182 	g_settings_apply (G_SETTINGS (config));
183 
184 	return TRUE;
185 }
186 
187 
188 gint
189 tracker_db_config_get_journal_chunk_size (TrackerDBConfig *config)
190 {
191 	g_return_val_if_fail (TRACKER_IS_DB_CONFIG (config), DEFAULT_JOURNAL_CHUNK_SIZE);
192 
193 	return g_settings_get_int (G_SETTINGS (config), "journal-chunk-size");
194 }
195 
196 gchar *
197 tracker_db_config_get_journal_rotate_destination (TrackerDBConfig *config)
198 {
199 	g_return_val_if_fail (TRACKER_IS_DB_CONFIG (config), g_strdup (DEFAULT_JOURNAL_ROTATE_DESTINATION));
200 
201 	return g_settings_get_string (G_SETTINGS (config), "journal-rotate-destination");
202 }
203 
204 void
205 tracker_db_config_set_journal_chunk_size (TrackerDBConfig *config,
206                                           gint             value)
207 {
208 	g_return_if_fail (TRACKER_IS_DB_CONFIG (config));
209 
210 	g_settings_set_int (G_SETTINGS (config), "journal-chunk-size", value);
211 	g_object_notify (G_OBJECT (config), "journal-chunk-size");
212 }
213 
214 void
215 tracker_db_config_set_journal_rotate_destination (TrackerDBConfig *config,
216                                                   const gchar     *value)
217 {
218 	g_return_if_fail (TRACKER_IS_DB_CONFIG (config));
219 
220 	g_settings_set_string (G_SETTINGS (config), "journal-rotate-destination", value);
221 	g_object_notify (G_OBJECT (config), "journal-rotate-destination");
222 }