1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*- */
3 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5 * Authors: Michael Zucchi <notzed@ximian.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
33 #include "camel-index.h"
34 #include "camel-object.h"
38 #define d(x) /*(printf ("%s (%d): ", __FILE__, __LINE__),(x))*/
40 #define CAMEL_INDEX_VERSION (0x01)
42 #define CAMEL_INDEX_GET_PRIVATE(obj) \
43 (G_TYPE_INSTANCE_GET_PRIVATE \
44 ((obj), CAMEL_TYPE_INDEX, CamelIndexPrivate))
46 struct _CamelIndexPrivate {
50 /* ********************************************************************** */
52 /* ********************************************************************** */
54 G_DEFINE_TYPE (CamelIndex, camel_index, CAMEL_TYPE_OBJECT)
57 index_finalize (GObject *object)
59 CamelIndex *index = CAMEL_INDEX (object);
63 /* Chain up to parent's finalize () method. */
64 G_OBJECT_CLASS (camel_index_parent_class)->finalize (object);
68 camel_index_class_init (CamelIndexClass *class)
70 GObjectClass *object_class;
72 g_type_class_add_private (class, sizeof (CamelIndexPrivate));
74 object_class = G_OBJECT_CLASS (class);
75 object_class->finalize = index_finalize;
79 camel_index_init (CamelIndex *index)
81 index->priv = CAMEL_INDEX_GET_PRIVATE (index);
82 index->version = CAMEL_INDEX_VERSION;
86 camel_index_new (const gchar *path,
91 idx = g_object_new (CAMEL_TYPE_INDEX, NULL);
92 camel_index_construct (idx, path, flags);
98 camel_index_construct (CamelIndex *idx,
103 idx->path = g_strdup_printf ("%s.index", path);
108 camel_index_rename (CamelIndex *idx,
111 CamelIndexClass *class;
113 g_return_val_if_fail (CAMEL_IS_INDEX (idx), -1);
115 class = CAMEL_INDEX_GET_CLASS (idx);
116 g_return_val_if_fail (class->rename != NULL, -1);
118 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
119 return class->rename (idx, path);
127 * camel_index_set_normalize:
128 * @index: a #CamelIndex
129 * @func: normalization function
130 * @data: user data for @func
135 camel_index_set_normalize (CamelIndex *index,
139 g_return_if_fail (CAMEL_IS_INDEX (index));
141 index->normalize = func;
142 index->normalize_data = data;
146 camel_index_sync (CamelIndex *idx)
148 CamelIndexClass *class;
150 g_return_val_if_fail (CAMEL_IS_INDEX (idx), -1);
152 class = CAMEL_INDEX_GET_CLASS (idx);
153 g_return_val_if_fail (class->sync != NULL, -1);
155 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
156 return class->sync (idx);
164 camel_index_compress (CamelIndex *idx)
166 CamelIndexClass *class;
168 g_return_val_if_fail (CAMEL_IS_INDEX (idx), -1);
170 class = CAMEL_INDEX_GET_CLASS (idx);
171 g_return_val_if_fail (class->compress != NULL, -1);
173 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
174 return class->compress (idx);
182 camel_index_delete (CamelIndex *idx)
184 CamelIndexClass *class;
187 g_return_val_if_fail (CAMEL_IS_INDEX (idx), -1);
189 class = CAMEL_INDEX_GET_CLASS (idx);
190 g_return_val_if_fail (class->delete_ != NULL, -1);
192 if ((idx->state & CAMEL_INDEX_DELETED) == 0) {
193 ret = class->delete_ (idx);
194 idx->state |= CAMEL_INDEX_DELETED;
204 camel_index_has_name (CamelIndex *idx,
207 CamelIndexClass *class;
209 g_return_val_if_fail (CAMEL_IS_INDEX (idx), FALSE);
211 class = CAMEL_INDEX_GET_CLASS (idx);
212 g_return_val_if_fail (class->has_name != NULL, FALSE);
214 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
215 return class->has_name (idx, name);
221 camel_index_add_name (CamelIndex *idx,
224 CamelIndexClass *class;
226 g_return_val_if_fail (CAMEL_IS_INDEX (idx), NULL);
228 class = CAMEL_INDEX_GET_CLASS (idx);
229 g_return_val_if_fail (class->add_name != NULL, NULL);
231 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
232 return class->add_name (idx, name);
238 camel_index_write_name (CamelIndex *idx,
241 CamelIndexClass *class;
243 g_return_val_if_fail (CAMEL_IS_INDEX (idx), -1);
245 class = CAMEL_INDEX_GET_CLASS (idx);
246 g_return_val_if_fail (class->write_name != NULL, -1);
248 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
249 return class->write_name (idx, idn);
257 camel_index_find_name (CamelIndex *idx,
260 CamelIndexClass *class;
262 g_return_val_if_fail (CAMEL_IS_INDEX (idx), NULL);
264 class = CAMEL_INDEX_GET_CLASS (idx);
265 g_return_val_if_fail (class->find_name != NULL, NULL);
267 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
268 return class->find_name (idx, name);
274 camel_index_delete_name (CamelIndex *idx,
277 CamelIndexClass *class;
279 g_return_if_fail (CAMEL_IS_INDEX (idx));
281 class = CAMEL_INDEX_GET_CLASS (idx);
282 g_return_if_fail (class->delete_name != NULL);
284 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
285 class->delete_name (idx, name);
289 camel_index_find (CamelIndex *idx,
292 CamelIndexClass *class;
293 CamelIndexCursor *ret;
294 gchar *b = (gchar *) word;
296 g_return_val_if_fail (CAMEL_IS_INDEX (idx), NULL);
298 class = CAMEL_INDEX_GET_CLASS (idx);
299 g_return_val_if_fail (class->find != NULL, NULL);
301 if ((idx->state & CAMEL_INDEX_DELETED) != 0)
305 b = idx->normalize (idx, word, idx->normalize_data);
307 ret = class->find (idx, b);
316 camel_index_words (CamelIndex *idx)
318 CamelIndexClass *class;
320 g_return_val_if_fail (CAMEL_IS_INDEX (idx), NULL);
322 class = CAMEL_INDEX_GET_CLASS (idx);
323 g_return_val_if_fail (class->words != NULL, NULL);
325 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
326 return class->words (idx);
332 camel_index_names (CamelIndex *idx)
334 CamelIndexClass *class;
336 g_return_val_if_fail (CAMEL_IS_INDEX (idx), NULL);
338 class = CAMEL_INDEX_GET_CLASS (idx);
339 g_return_val_if_fail (class->names != NULL, NULL);
341 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
342 return class->names (idx);
347 /* ********************************************************************** */
349 /* ********************************************************************** */
351 G_DEFINE_TYPE (CamelIndexName, camel_index_name, CAMEL_TYPE_OBJECT)
354 index_name_dispose (GObject *object)
356 CamelIndexName *index_name = CAMEL_INDEX_NAME (object);
358 if (index_name->index != NULL) {
359 g_object_unref (index_name->index);
360 index_name->index = NULL;
363 /* Chain up to parent's dispose () method. */
364 G_OBJECT_CLASS (camel_index_name_parent_class)->dispose (object);
368 camel_index_name_class_init (CamelIndexNameClass *class)
370 GObjectClass *object_class;
372 object_class = G_OBJECT_CLASS (class);
373 object_class->dispose = index_name_dispose;
377 camel_index_name_init (CamelIndexName *index_name)
382 camel_index_name_new (CamelIndex *idx,
387 idn = g_object_new (CAMEL_TYPE_INDEX_NAME, NULL);
388 idn->index = g_object_ref (idx);
394 camel_index_name_add_word (CamelIndexName *idn,
397 CamelIndexNameClass *class;
398 gchar *b = (gchar *) word;
400 g_return_if_fail (CAMEL_IS_INDEX_NAME (idn));
402 class = CAMEL_INDEX_NAME_GET_CLASS (idn);
403 g_return_if_fail (class->add_word != NULL);
405 if (idn->index->normalize)
406 b = idn->index->normalize (idn->index, word, idn->index->normalize_data);
408 class->add_word (idn, b);
415 camel_index_name_add_buffer (CamelIndexName *idn,
419 CamelIndexNameClass *class;
421 g_return_val_if_fail (CAMEL_IS_INDEX_NAME (idn), 0);
423 class = CAMEL_INDEX_NAME_GET_CLASS (idn);
424 g_return_val_if_fail (class->add_buffer != NULL, 0);
426 return class->add_buffer (idn, buffer, len);
429 /* ********************************************************************** */
430 /* CamelIndexCursor */
431 /* ********************************************************************** */
433 G_DEFINE_TYPE (CamelIndexCursor, camel_index_cursor, CAMEL_TYPE_OBJECT)
436 index_cursor_dispose (GObject *object)
438 CamelIndexCursor *index_cursor = CAMEL_INDEX_CURSOR (object);
440 if (index_cursor->index != NULL) {
441 g_object_unref (index_cursor->index);
442 index_cursor->index = NULL;
445 /* Chain up to parent's dispose () method. */
446 G_OBJECT_CLASS (camel_index_cursor_parent_class)->dispose (object);
450 camel_index_cursor_class_init (CamelIndexCursorClass *class)
452 GObjectClass *object_class;
454 object_class = G_OBJECT_CLASS (class);
455 object_class->dispose = index_cursor_dispose;
459 camel_index_cursor_init (CamelIndexCursor *index_cursor)
464 camel_index_cursor_new (CamelIndex *idx,
467 CamelIndexCursor *idc;
469 idc = g_object_new (CAMEL_TYPE_INDEX_CURSOR, NULL);
470 idc->index = g_object_ref (idx);
476 camel_index_cursor_next (CamelIndexCursor *idc)
478 CamelIndexCursorClass *class;
480 g_return_val_if_fail (CAMEL_IS_INDEX_CURSOR (idc), NULL);
482 class = CAMEL_INDEX_CURSOR_GET_CLASS (idc);
483 g_return_val_if_fail (class->next != NULL, NULL);
485 return class->next (idc);
489 camel_index_cursor_reset (CamelIndexCursor *idc)
491 CamelIndexCursorClass *class;
493 g_return_if_fail (CAMEL_IS_INDEX_CURSOR (idc));
495 class = CAMEL_INDEX_CURSOR_GET_CLASS (idc);
496 g_return_if_fail (class->reset != NULL);