Dont add null mimetypes. Fixes bgo# 337431. The patch hasnt been officially accepted...
[beagle.git] / libbeagle / beagle / beagle-hit.c
blobb88ca4a8f86a820ddde6383c2dcfe62b70565e78
1 /*
2 * beagle-hit.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 <string.h>
30 #include "beagle-hit.h"
31 #include "beagle-private.h"
32 #include "beagle-property.h"
34 /**
35 * beagle_hit_get_uri:
36 * @hit: a #BeagleHit
38 * Fetches the URI of the given #BeagleHit.
40 * Return value: the URI of the #BeagleHit.
41 **/
42 G_CONST_RETURN char *
43 beagle_hit_get_uri (BeagleHit *hit)
45 g_return_val_if_fail (hit != NULL, NULL);
47 return hit->uri;
50 /**
51 * beagle_hit_get_parent_uri:
52 * @hit: a #BeagleHit
54 * Fetches the parent URI of the given #BeagleHit.
56 * Return value: the parent URI of the #BeagleHit.
57 **/
58 G_CONST_RETURN char *
59 beagle_hit_get_parent_uri (BeagleHit *hit)
61 g_return_val_if_fail (hit != NULL, NULL);
63 return hit->parent_uri;
66 /**
67 * beagle_hit_get_timestamp:
68 * @hit: a #BeagleHit
70 * Fetches the timestamp of the given #BeagleHit.
72 * Return value: the timestamp as a string of the #BeagleHit.
73 **/
74 BeagleTimestamp *
75 beagle_hit_get_timestamp (BeagleHit *hit)
77 g_return_val_if_fail (hit != NULL, NULL);
79 return hit->timestamp;
82 /**
83 * beagle_hit_get_type:
84 * @hit: a #BeagleHit
86 * Fetches the type of the given #BeagleHit.
88 * Return value: the type of the #BeagleHit.
89 **/
90 G_CONST_RETURN char *
91 beagle_hit_get_type (BeagleHit *hit)
93 g_return_val_if_fail (hit != NULL, NULL);
95 return hit->type;
98 /**
99 * beagle_hit_get_mime_type:
100 * @hit: a #BeagleHit
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:
116 * @hit: a #BeagleHit
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);
127 return hit->source;
131 * beagle_hit_get_score:
132 * @hit: a #BeagleHit
134 * Fetches the score of the given #BeagleHit.
136 * Return value: the score of the #BeagleHit.
139 double
140 beagle_hit_get_score (BeagleHit *hit)
142 g_return_val_if_fail (hit != NULL, -1);
144 return hit->score;
147 BeagleHit *
148 _beagle_hit_new (void)
150 BeagleHit *hit = g_new0 (BeagleHit, 1);
152 hit->ref_count = 1;
154 hit->uri = NULL;
155 hit->timestamp = NULL;
156 hit->type = NULL;
157 hit->mime_type = NULL;
158 hit->source = NULL;
160 hit->properties = NULL;
162 return hit;
165 void
166 _beagle_hit_add_property (BeagleHit *hit, BeagleProperty *prop)
168 hit->properties = g_slist_insert_sorted (hit->properties, prop, (GCompareFunc) _beagle_property_compare);
171 void
172 _beagle_hit_list_free (GSList *list)
174 g_slist_foreach (list, (GFunc)beagle_hit_unref, NULL);
176 g_slist_free (list);
180 * beagle_hit_ref:
181 * @hit: a #BeagleHit
183 * Increases the reference count of the #BeagleHit.
185 * Return value: the #BeagleHit.
187 BeagleHit *
188 beagle_hit_ref (BeagleHit *hit)
190 g_return_val_if_fail (hit != NULL, NULL);
192 hit->ref_count ++;
194 return hit;
198 * beagle_hit_unref:
199 * @hit: a #BeagleHit.
201 * Decreases the reference count of the #BeagleHit. When its reference count drops to 0, it is freed.
203 void
204 beagle_hit_unref (BeagleHit *hit)
206 g_return_if_fail (hit != NULL);
207 g_return_if_fail (hit->ref_count > 0);
209 hit->ref_count--;
211 if (hit->ref_count == 0) {
212 g_free (hit->uri);
213 g_free (hit->parent_uri);
214 g_free (hit->type);
215 g_free (hit->mime_type);
216 g_free (hit->source);
218 if (hit->timestamp)
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);
226 g_free (hit);
231 * beagle_hit_get_one_property:
232 * @hit: a #BeagleHit
233 * @key: a string
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.
244 **/
245 gboolean
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);
255 *value = NULL;
257 if (! hit->properties)
258 return FALSE;
260 pointer_first_property = g_slist_find_custom (hit->properties, key, (GCompareFunc) _beagle_property_key_compare);
262 if (pointer_first_property == NULL)
263 return FALSE;
265 next = g_slist_next (pointer_first_property);
267 if (next != NULL) {
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)
272 return FALSE;
275 property = (BeagleProperty *) pointer_first_property->data;
276 *value = beagle_property_get_value (property);
278 return TRUE;
282 * beagle_hit_get_properties:
283 * @hit: a #BeagleHit
284 * @key: a string
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.
290 **/
291 GSList *
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)
301 return NULL;
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)
310 break;
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:
322 * @hit: a #BeagleHit
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.
328 **/
329 GSList *
330 beagle_hit_get_all_properties (BeagleHit *hit)
332 g_return_val_if_fail (hit != NULL, NULL);
334 return g_slist_copy (hit->properties);
337 void
338 _beagle_hit_to_xml (BeagleHit *hit, GString *data)
340 char *tmp;
342 if (hit->timestamp)
343 tmp = _beagle_timestamp_to_string (hit->timestamp);
344 else
345 tmp = _beagle_timestamp_get_start ();
347 g_string_append_printf (data, "<Hit Timestamp=\"%s\"",
348 tmp);
350 g_free (tmp);
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\"",
356 hit->source);
358 g_string_append_printf (data, " Score=\"%f\"",
359 hit->score);
361 g_string_append (data, ">");
363 _beagle_properties_to_xml (hit->properties, data);
365 g_string_append (data, "</Hit>");