Add methods and messages to retrieve specific information from beagle-info. Clients...
[beagle.git] / libbeagle / beagle / beagle-indexable.c
blobfa4469110be7c52a14a47322b195eb54deeed636
1 /*
2 * beagle-indexable.c
4 * Copyright (C) 2005 Novell, Inc.
6 */
8 /*
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
28 #include "beagle-indexable.h"
29 #include "beagle-private.h"
30 #include "beagle-timestamp.h"
32 struct _BeagleIndexable {
33 BeagleIndexableType type;
35 char *uri;
36 char *parent_uri;
37 char *content_uri;
38 char *hot_content_uri;
40 BeagleTimestamp *timestamp;
42 gboolean delete_content;
43 gboolean crawled;
44 gboolean no_content;
45 gboolean cache_content;
47 BeagleIndexableFiltering filtering;
49 char *hit_type;
50 char *mime_type;
51 char *source;
53 GSList *properties;
56 /**
57 * beagle_indexable_new:
58 * @uri: a string
60 * Creates a new #BeagleIndexable for the given @uri.
62 * Return value: the newly created #BeagleIndexable.
63 **/
64 BeagleIndexable *
65 beagle_indexable_new (const char *uri)
67 BeagleIndexable *indexable;
69 g_return_val_if_fail (uri != NULL, NULL);
71 indexable = g_new0 (BeagleIndexable, 1);
72 indexable->uri = g_strdup (uri);
74 // Use current time as the indexable timestamp, similar to C# API
75 indexable->timestamp = beagle_timestamp_new_from_unix_time (time (NULL));
77 indexable->delete_content = FALSE;
78 indexable->crawled = TRUE;
79 indexable->no_content = FALSE;
80 indexable->cache_content = TRUE;
82 indexable->properties = NULL;
84 indexable->hit_type = g_strdup ("File");
85 indexable->type = BEAGLE_INDEXABLE_TYPE_ADD;
86 indexable->filtering = BEAGLE_INDEXABLE_FILTERING_AUTOMATIC;
88 return indexable;
91 /**
92 * beagle_indexable_free:
93 * @indexable: a #BeagleIndexable
95 * Frees the memory allocated by the #BeagleIndexable.
96 **/
97 void
98 beagle_indexable_free (BeagleIndexable *indexable)
100 g_return_if_fail (indexable != NULL);
102 if (indexable->timestamp)
103 beagle_timestamp_free (indexable->timestamp);
105 g_free (indexable->uri);
106 g_free (indexable->parent_uri);
107 g_free (indexable->content_uri);
108 g_free (indexable->hot_content_uri);
110 g_free (indexable->hit_type);
111 g_free (indexable->mime_type);
112 g_free (indexable->source);
114 if (indexable->properties) {
115 g_slist_foreach (indexable->properties, (GFunc) beagle_property_free, NULL);
116 g_slist_free (indexable->properties);
119 g_free (indexable);
123 * beagle_indexable_add_property:
124 * @indexable: a #BeagleIndexable
125 * @prop: a #BeagleProperty
127 * Adds the #BeagleProperty to the given #BeagleIndexable.
129 void
130 beagle_indexable_add_property (BeagleIndexable *indexable, BeagleProperty *prop)
132 g_return_if_fail (indexable != NULL);
133 g_return_if_fail (prop != NULL);
135 indexable->properties = g_slist_insert_sorted (indexable->properties, prop, (GCompareFunc) _beagle_property_compare);
139 * beagle_indexable_get_uri:
140 * @indexable: a #BeagleIndexable
142 * Fetches the URI for the given #BeagleIndexable.
144 * Return value: the URI of the #BeagleIndexable.
146 G_CONST_RETURN char *
147 beagle_indexable_get_uri (BeagleIndexable *indexable)
149 g_return_val_if_fail (indexable != NULL, NULL);
151 return indexable->uri;
155 * beagle_indexable_set_uri:
156 * @indexable: a #BeagleIndexable
157 * @uri: a string
159 * Sets the URI of the #BeagleIndexable to @uri.
161 void
162 beagle_indexable_set_uri (BeagleIndexable *indexable, const char *uri)
164 g_return_if_fail (indexable != NULL);
165 g_return_if_fail (uri != NULL);
167 g_free (indexable->uri);
168 indexable->uri = g_strdup (uri);
172 * beagle_indexable_get_parent_uri:
173 * @indexable: a #BeagleIndexable
175 * Fetches the URI for the given #BeagleIndexable.
177 * Return value: the parent URI of the #BeagleIndexable.
179 G_CONST_RETURN char *
180 beagle_indexable_get_parent_uri (BeagleIndexable *indexable)
182 g_return_val_if_fail (indexable != NULL, NULL);
184 return indexable->parent_uri;
188 * beagle_indexable_set_parent_uri:
189 * @indexable: a #BeagleIndexable
190 * @parent_uri: a string
192 * Sets the parent URI of the #BeagleIndexable to @parent_uri.
194 void
195 beagle_indexable_set_parent_uri (BeagleIndexable *indexable, const char *parent_uri)
197 g_return_if_fail (indexable != NULL);
198 g_return_if_fail (parent_uri != NULL);
200 g_free (indexable->parent_uri);
201 indexable->parent_uri = g_strdup (parent_uri);
205 * beagle_indexable_get_content_uri:
206 * @indexable: a #BeagleIndexable
208 * Fetches the content URI for the given #BeagleIndexable.
210 * Return value: the content URI of the #BeagleIndexable.
212 G_CONST_RETURN char *
213 beagle_indexable_get_content_uri (BeagleIndexable *indexable)
215 g_return_val_if_fail (indexable != NULL, NULL);
217 return indexable->content_uri;
221 * beagle_indexable_set_content_uri:
222 * @indexable: a #BeagleIndexable
223 * @content_uri: a string
225 * Sets the content URI of the given #BeagleIndexable to @content_uri.
227 void
228 beagle_indexable_set_content_uri (BeagleIndexable *indexable, const char *content_uri)
230 g_return_if_fail (indexable != NULL);
232 g_free (indexable->content_uri);
233 indexable->content_uri = g_strdup (content_uri);
237 * beagle_indexable_get_hot_content_uri:
238 * @indexable: a #BeagleIndexable
240 * Fetches the hot content URI of the given #BeagleIndexable.
242 * Return value: the hot content URI of the #BeagleIndexable.
244 G_CONST_RETURN char *
245 beagle_indexable_get_hot_content_uri (BeagleIndexable *indexable)
247 g_return_val_if_fail (indexable != NULL, NULL);
249 return indexable->hot_content_uri;
253 * beagle_indexable_set_hot_content_uri:
254 * @indexable: a #BeagleIndexable
255 * @hot_content_uri: a string
257 * Sets the hot content URI of the given #BeagleIndexable to @hot_content_uri.
259 void
260 beagle_indexable_set_hot_content_uri (BeagleIndexable *indexable, const char *hot_content_uri)
262 g_return_if_fail (indexable != NULL);
264 g_free (indexable->hot_content_uri);
265 indexable->hot_content_uri = g_strdup (hot_content_uri);
269 * beagle_indexable_get_delete_content:
270 * @indexable: a #BeagleIndexable
272 * Fetches whether content of the given #BeagleIndexable should be deleted after it has been indexed.
274 * Return value: whether content should be deleted for the #BeagleIndexable.
276 gboolean
277 beagle_indexable_get_delete_content (BeagleIndexable *indexable)
279 g_return_val_if_fail (indexable != NULL, FALSE);
281 return indexable->delete_content;
285 * beagle_indexable_set_delete_content:
286 * @indexable: a #BeagleIndexable
287 * @delete_content: a boolean
289 * Sets whether content of the given #BeagleIndexable should be deleted after it has been indexed.
291 void
292 beagle_indexable_set_delete_content (BeagleIndexable *indexable, gboolean delete_content)
294 g_return_if_fail (indexable != NULL);
296 indexable->delete_content = delete_content != FALSE;
300 * beagle_indexable_get_crawled:
301 * @indexable: a #BeagleIndexable
303 * Fetches whether the given #BeagleIndexable is in crawl mode.
305 * Return value: whether the #BeagleIndexable is crawled.
307 gboolean
308 beagle_indexable_get_crawled (BeagleIndexable *indexable)
310 g_return_val_if_fail (indexable != NULL, FALSE);
312 return indexable->crawled;
316 * beagle_indexable_set_crawled:
317 * @indexable: a #BeagleIndexable
318 * @crawled: a boolean
320 * Sets whether the given #BeagleIndexable is in crawl mode.
322 void
323 beagle_indexable_set_crawled (BeagleIndexable *indexable, gboolean crawled)
325 g_return_if_fail (indexable != NULL);
327 indexable->crawled = crawled != FALSE;
331 * beagle_indexable_get_no_content:
332 * @indexable: a #BeagleIndexable
334 * Fetches whether the given #BeagleIndexable has no content.
336 * Return value: whether the #BeagleIndexable has no content.
338 gboolean
339 beagle_indexable_get_no_content (BeagleIndexable *indexable)
341 g_return_val_if_fail (indexable != NULL, FALSE);
343 return indexable->no_content;
347 * beagle_indexable_set_no_content:
348 * @indexable: a #BeagleIndexable
349 * @no_content: a boolean
351 * Sets whether the given #BeagleIndexable has no content.
353 void
354 beagle_indexable_set_no_content (BeagleIndexable *indexable,
355 gboolean no_content)
357 g_return_if_fail (indexable != NULL);
359 indexable->no_content = no_content != FALSE;
364 * beagle_indexable_get_cache_content:
365 * @indexable: a #BeagleIndexable
367 * Fetches whether the given #BeagleIndexable consists of cached contents.
369 * Return value: whether the #BeagleIndexable contains cached contents.
371 gboolean
372 beagle_indexable_get_cache_content (BeagleIndexable *indexable)
374 g_return_val_if_fail (indexable != NULL, FALSE);
376 return indexable->cache_content;
380 * beagle_indexable_set_cache_content:
381 * @indexable: a #BeagleIndexable
382 * @cache_content: a boolean
384 * Sets whether the given #BeagleIndexable consists of cached contents.
386 void
387 beagle_indexable_set_cache_content (BeagleIndexable *indexable, gboolean cache_content)
389 g_return_if_fail (indexable != NULL);
391 indexable->cache_content = cache_content != FALSE;
395 * beagle_indexable_get_type:
396 * @indexable: a #BeagleIndexable
398 * Fetches the #BeagleIndexableType of the given #BeagleIndexable.
400 * Return value: Fetches the #BeagleIndexableType of the #BeagleIndexable.
402 BeagleIndexableType
403 beagle_indexable_get_type (BeagleIndexable *indexable)
405 g_return_val_if_fail (indexable != NULL, BEAGLE_INDEXABLE_TYPE_ADD);
407 return indexable->type;
411 * beagle_indexable_set_type:
412 * @indexable: a #BeagleIndexable
413 * @filtering: a #BeagleIndexableType
415 * Sets the #BeagleIndexableType of the given #BeagleIndexable.
417 void
418 beagle_indexable_set_type (BeagleIndexable *indexable, BeagleIndexableType type)
420 g_return_if_fail (indexable != NULL);
421 g_return_if_fail (type >= BEAGLE_INDEXABLE_TYPE_ADD && type <= BEAGLE_INDEXABLE_TYPE_PROPERTY_CHANGE);
423 indexable->type = type;
428 * beagle_indexable_get_filtering:
429 * @indexable: a #BeagleIndexable
431 * Fetches the #BeagleIndexableFiltering of the given #BeagleIndexable.
433 * Return value: Fetches the #BeagleIndexableFiltering of the #BeagleIndexable.
435 BeagleIndexableFiltering
436 beagle_indexable_get_filtering (BeagleIndexable *indexable)
438 g_return_val_if_fail (indexable != NULL, BEAGLE_INDEXABLE_FILTERING_AUTOMATIC);
440 return indexable->filtering;
444 * beagle_indexable_set_filtering:
445 * @indexable: a #BeagleIndexable
446 * @filtering: a #BeagleIndexableFiltering
448 * Sets the #BeagleIndexableFiltering of the given #BeagleIndexable.
450 void
451 beagle_indexable_set_filtering (BeagleIndexable *indexable, BeagleIndexableFiltering filtering)
453 g_return_if_fail (indexable != NULL);
454 g_return_if_fail (filtering >= BEAGLE_INDEXABLE_FILTERING_AUTOMATIC && filtering <= BEAGLE_INDEXABLE_FILTERING_NEVER);
456 indexable->filtering = filtering;
460 * beagle_indexable_get_hit_type:
461 * @indexable: a #BeagleIndexable
463 * Fetches the hit type of the given #BeagleIndexable.
465 * Return value: the hit type of the #BeagleIndexable.
468 G_CONST_RETURN char *
469 beagle_indexable_get_hit_type (BeagleIndexable *indexable)
471 g_return_val_if_fail (indexable != NULL, NULL);
473 return indexable->hit_type;
477 * beagle_indexable_set_hit_type:
478 * @indexable: a #BeagleIndexable
479 * @hit_type: a string
481 * Sets the hit type of the given #BeagleIndexable to @hit_type.
483 void
484 beagle_indexable_set_hit_type (BeagleIndexable *indexable, const char *hit_type)
486 g_return_if_fail (indexable != NULL);
488 g_free (indexable->hit_type);
489 indexable->hit_type = g_strdup (hit_type);
494 * beagle_indexable_get_mime_type:
495 * @indexable: a #BeagleIndexable
497 * Fetches the mime type of the given #BeagleIndexable.
499 * Return value: the mime type of the #BeagleIndexable.
501 G_CONST_RETURN char *
502 beagle_indexable_get_mime_type (BeagleIndexable *indexable)
504 g_return_val_if_fail (indexable != NULL, NULL);
506 return indexable->mime_type;
510 * beagle_indexable_set_mime_type:
511 * @indexable: a #BeagleIndexable
512 * @mime_type: a string
514 * Sets the mime type of the given #BeagleIndexable to @mime_type.
516 void
517 beagle_indexable_set_mime_type (BeagleIndexable *indexable, const char *mime_type)
519 g_return_if_fail (indexable != NULL);
521 g_free (indexable->mime_type);
522 indexable->mime_type = g_strdup (mime_type);
526 * beagle_indexable_get_source:
527 * @indexable: a #BeagleIndexable
529 * Fetches the source of the given #BeagleIndexable.
531 * Return value: the source of the #BeagleIndexable.
533 G_CONST_RETURN char *
534 beagle_indexable_get_source (BeagleIndexable *indexable)
536 g_return_val_if_fail (indexable != NULL, NULL);
538 return indexable->source;
542 * beagle_indexable_set_source:
543 * @indexable: a #BeagleIndexable
544 * @source: a string
546 * Sets the source of the given #BeagleIndexable to @source.
548 void
549 beagle_indexable_set_source (BeagleIndexable *indexable, const char *source)
551 g_return_if_fail (indexable != NULL);
553 g_free (indexable->source);
554 indexable->source = g_strdup (source);
558 BeagleTimestamp *
559 beagle_indexable_get_timestamp (BeagleIndexable *indexable)
561 g_return_val_if_fail (indexable != NULL, NULL);
563 return indexable->timestamp;
566 void
567 beagle_indexable_set_timestamp (BeagleIndexable *indexable,
568 BeagleTimestamp *timestamp)
570 g_return_if_fail (indexable != NULL);
571 g_return_if_fail (timestamp != NULL);
573 if (indexable->timestamp)
574 beagle_timestamp_free (indexable->timestamp);
576 indexable->timestamp = timestamp;
579 void
580 _beagle_indexable_to_xml (BeagleIndexable *indexable, GString *data)
582 char *tmp;
584 g_string_append_printf (data, "<Indexable");
586 if (indexable->timestamp) {
587 tmp = _beagle_timestamp_to_string (indexable->timestamp);
588 g_string_append_printf (data, " Timestamp=\"%s\"", tmp);
589 g_free (tmp);
592 g_string_append_printf (data, " Uri=\"%s\"", indexable->uri);
594 if (indexable->parent_uri)
595 g_string_append_printf (data, " ParentUri=\"%s\"",
596 indexable->parent_uri);
598 g_string_append_printf (data, " ContentUri=\"%s\" HotContentUri=\"%s\"",
599 indexable->content_uri ? indexable->content_uri : indexable->uri,
600 indexable->hot_content_uri ? indexable->hot_content_uri : "");
602 g_string_append_printf (data, " DeleteContent=\"%s\" Crawled=\"%s\" NoContent=\"%s\" "
603 "CacheContent=\"%s\"",
604 indexable->delete_content ? "true" : "false",
605 indexable->crawled ? "true" : "false",
606 indexable->no_content ? "true" : "false",
607 indexable->cache_content ? "true" : "false");
609 switch (indexable->type) {
610 case BEAGLE_INDEXABLE_TYPE_ADD:
611 tmp = "Add";
612 break;
613 case BEAGLE_INDEXABLE_TYPE_REMOVE:
614 tmp = "Remove";
615 break;
616 case BEAGLE_INDEXABLE_TYPE_PROPERTY_CHANGE:
617 tmp = "PropertyChange";
618 break;
619 default:
620 g_assert_not_reached ();
623 g_string_append_printf (data, " Type=\"%s\"", tmp);
625 switch (indexable->filtering) {
626 case BEAGLE_INDEXABLE_FILTERING_ALWAYS:
627 tmp = "Always";
628 break;
629 case BEAGLE_INDEXABLE_FILTERING_ALREADY_FILTERED:
630 tmp = "AlreadyFiltered";
631 break;
632 case BEAGLE_INDEXABLE_FILTERING_AUTOMATIC:
633 tmp = "Automatic";
634 break;
635 case BEAGLE_INDEXABLE_FILTERING_NEVER:
636 tmp = "Never";
637 break;
638 default:
639 g_assert_not_reached ();
642 g_string_append_printf (data, " Filtering=\"%s\"", tmp);
644 if (indexable->hit_type)
645 g_string_append_printf (data, " HitType=\"%s\"", indexable->hit_type);
647 if (indexable->mime_type)
648 g_string_append_printf (data, " MimeType=\"%s\"", indexable->mime_type);
650 if (indexable->source)
651 g_string_append_printf (data, " Source=\"%s\"", indexable->source);
653 g_string_append (data, ">");
655 _beagle_properties_to_xml (indexable->properties, data);
657 g_string_append (data, "</Indexable>");