4 * Copyright (C) 2005 Novell, Inc.
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.
30 #include "beagle-hit.h"
31 #include "beagle-private.h"
32 #include "beagle-property.h"
38 * Fetches the URI of the given #BeagleHit.
40 * Return value: the URI of the #BeagleHit.
43 beagle_hit_get_uri (BeagleHit
*hit
)
45 g_return_val_if_fail (hit
!= NULL
, NULL
);
51 * beagle_hit_get_parent_uri:
54 * Fetches the parent URI of the given #BeagleHit.
56 * Return value: the parent URI of the #BeagleHit.
59 beagle_hit_get_parent_uri (BeagleHit
*hit
)
61 g_return_val_if_fail (hit
!= NULL
, NULL
);
63 return hit
->parent_uri
;
67 * beagle_hit_get_timestamp:
70 * Fetches the timestamp of the given #BeagleHit.
72 * Return value: the timestamp as a string of the #BeagleHit.
75 beagle_hit_get_timestamp (BeagleHit
*hit
)
77 g_return_val_if_fail (hit
!= NULL
, NULL
);
79 return hit
->timestamp
;
83 * beagle_hit_get_type:
86 * Fetches the type of the given #BeagleHit.
88 * Return value: the type of the #BeagleHit.
91 beagle_hit_get_type (BeagleHit
*hit
)
93 g_return_val_if_fail (hit
!= NULL
, NULL
);
99 * beagle_hit_get_mime_type:
102 * Fetches the mime type of the given #BeagleHit.
104 * Return value: the mime type of the #BeagleHit.
106 G_CONST_RETURN
char *
107 beagle_hit_get_mime_type (BeagleHit
*hit
)
109 g_return_val_if_fail (hit
!= NULL
, NULL
);
111 return hit
->mime_type
;
115 * beagle_hit_get_source:
118 * Fetches the source of the given #BeagleHit.
120 * Return value: the source of the #BeagleHit.
122 G_CONST_RETURN
char *
123 beagle_hit_get_source (BeagleHit
*hit
)
125 g_return_val_if_fail (hit
!= NULL
, NULL
);
131 * beagle_hit_get_score:
134 * Fetches the score of the given #BeagleHit.
136 * Return value: the score of the #BeagleHit.
140 beagle_hit_get_score (BeagleHit
*hit
)
142 g_return_val_if_fail (hit
!= NULL
, -1);
148 _beagle_hit_new (void)
150 BeagleHit
*hit
= g_new0 (BeagleHit
, 1);
155 hit
->timestamp
= NULL
;
157 hit
->mime_type
= NULL
;
160 hit
->properties
= NULL
;
166 _beagle_hit_add_property (BeagleHit
*hit
, BeagleProperty
*prop
)
168 hit
->properties
= g_slist_insert_sorted (hit
->properties
, prop
, (GCompareFunc
) _beagle_property_compare
);
172 _beagle_hit_list_free (GSList
*list
)
174 g_slist_foreach (list
, (GFunc
)beagle_hit_unref
, NULL
);
183 * Increases the reference count of the #BeagleHit.
185 * Return value: the #BeagleHit.
188 beagle_hit_ref (BeagleHit
*hit
)
190 g_return_val_if_fail (hit
!= NULL
, NULL
);
199 * @hit: a #BeagleHit.
201 * Decreases the reference count of the #BeagleHit. When its reference count drops to 0, it is freed.
204 beagle_hit_unref (BeagleHit
*hit
)
206 g_return_if_fail (hit
!= NULL
);
207 g_return_if_fail (hit
->ref_count
> 0);
211 if (hit
->ref_count
== 0) {
213 g_free (hit
->parent_uri
);
215 g_free (hit
->mime_type
);
216 g_free (hit
->source
);
219 beagle_timestamp_free (hit
->timestamp
);
221 if (hit
->properties
) {
222 g_slist_foreach (hit
->properties
, (GFunc
) beagle_property_free
, NULL
);
223 g_slist_free (hit
->properties
);
231 * beagle_hit_get_one_property:
234 * @value: pointer to a string where value is stored
236 * Puts the value of the property @key of the given #BeagleHit in the string pointed to by @value.
237 * The value of @value is set to NULL if FALSE is returned.
239 * This is a shortcut method for getting the value of a property when you know ahead of time that
240 * only one property for a given key exists. This function will fail if the key isn't found or
241 * if there is more than one value for a given key.
243 * Return value: TRUE if exactly one property with @key was found, else FALSE.
246 beagle_hit_get_one_property (BeagleHit
*hit
, const char *key
, const char **value
)
248 BeagleProperty
*property
;
249 GSList
*pointer_first_property
, *next
;
251 g_return_val_if_fail (hit
!= NULL
, FALSE
);
252 g_return_val_if_fail (key
!= NULL
, FALSE
);
253 g_return_val_if_fail (value
!= NULL
, FALSE
);
257 if (! hit
->properties
)
260 pointer_first_property
= g_slist_find_custom (hit
->properties
, key
, (GCompareFunc
) _beagle_property_key_compare
);
262 if (pointer_first_property
== NULL
)
265 next
= g_slist_next (pointer_first_property
);
268 BeagleProperty
*next_prop
= (BeagleProperty
*) next
->data
;
269 const char *next_key
= beagle_property_get_key (next_prop
);
271 if (strcmp (key
, next_key
) == 0)
275 property
= (BeagleProperty
*) pointer_first_property
->data
;
276 *value
= beagle_property_get_value (property
);
282 * beagle_hit_get_properties:
286 * Fetches all values of the property @key of the given #BeagleHit.
288 * Return value: A list of values (char *) of the of property @key. The values
289 * contained within the list should not be freed.
292 beagle_hit_get_properties (BeagleHit
*hit
, const char *key
)
294 GSList
*property_list
= NULL
;
295 GSList
*iterator_properties
;
297 g_return_val_if_fail (hit
!= NULL
, NULL
);
298 g_return_val_if_fail (key
!= NULL
, NULL
);
300 if (! hit
->properties
)
303 iterator_properties
= g_slist_find_custom (hit
->properties
, key
, (GCompareFunc
) _beagle_property_key_compare
);
305 while (iterator_properties
!= NULL
) {
306 BeagleProperty
*property
= (BeagleProperty
*) iterator_properties
->data
;
307 const char *prop_key
= beagle_property_get_key (property
);
309 if (strcmp (prop_key
, key
) != 0)
312 property_list
= g_slist_prepend (property_list
, (gpointer
) beagle_property_get_value (property
));
314 iterator_properties
= g_slist_next (iterator_properties
);
317 return property_list
;
321 * beagle_hit_get_all_properties:
324 * Fetches all properties of the given #BeagleHit
326 * Return value: A list of all properties (BeagleProperty *) of @hit. The values
327 * contained within the list should not be freed.
330 beagle_hit_get_all_properties (BeagleHit
*hit
)
332 g_return_val_if_fail (hit
!= NULL
, NULL
);
334 return g_slist_copy (hit
->properties
);
338 _beagle_hit_to_xml (BeagleHit
*hit
, GString
*data
)
343 tmp
= _beagle_timestamp_to_string (hit
->timestamp
);
345 tmp
= _beagle_timestamp_get_start ();
347 g_string_append_printf (data
, "<Hit Timestamp=\"%s\"",
352 g_string_append_printf (data
, " Uri=\"%s\" Type=\"%s\" MimeType=\"%s\"",
353 hit
->uri
, hit
->type
, hit
->mime_type
);
355 g_string_append_printf (data
, " Source=\"%s\"",
358 g_string_append_printf (data
, " Score=\"%f\"",
361 g_string_append (data
, ">");
363 _beagle_properties_to_xml (hit
->properties
, data
);
365 g_string_append (data
, "</Hit>");