1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; -*- */
6 * Copyright (C) 2005 Novell, Inc.
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
32 #include "beagle-property.h"
33 #include "beagle-private.h"
36 * beagle_property_new:
40 * Creates a new #BeagleProperty for the key and value.
42 * Return value: a newly allocated #BeagleProperty.
45 beagle_property_new (BeaglePropertyType type
, const char *key
, const char *value
)
47 BeagleProperty
*prop
= g_new0 (BeagleProperty
, 1);
50 prop
->key
= g_strdup (key
);
51 prop
->value
= g_strdup (value
);
53 if (type
== BEAGLE_PROPERTY_TYPE_TEXT
)
54 prop
->is_searched
= TRUE
;
56 prop
->is_searched
= FALSE
;
58 prop
->is_stored
= TRUE
;
64 * beagle_property_free:
65 * @prop: a #BeagleProperty
67 * Frees the memory allocated for the #BeagleProperty.
70 beagle_property_free (BeagleProperty
*prop
)
72 g_return_if_fail (prop
!= NULL
);
80 * beagle_property_get_type:
81 * @prop: a #BeagleProperty
83 * Fetches the type of the #BeagleProperty.
85 * Return value: the #BeaglePropertyType of the #BeagleProperty.
88 beagle_property_get_type (BeagleProperty
*prop
)
90 g_return_val_if_fail (prop
!= NULL
, BEAGLE_PROPERTY_TYPE_UNKNOWN
);
96 * beagle_property_set_type:
97 * @prop: a #BeagleProperty
98 * @type: a #BeaglePropertyType
100 * Sets the type of the given #BeagleProperty to @type.
103 beagle_property_set_type (BeagleProperty
*prop
, BeaglePropertyType type
)
105 g_return_if_fail (prop
!= NULL
);
111 * beagle_property_get_key:
112 * @prop: a #BeagleProperty
114 * Fetches the key of the #BeagleProperty.
116 * Return value: the key name of the #BeagleProperty.
118 G_CONST_RETURN
char *
119 beagle_property_get_key (BeagleProperty
*prop
)
121 g_return_val_if_fail (prop
!= NULL
, NULL
);
127 * beagle_property_set_key:
128 * @prop: a #BeagleProperty
131 * Sets the key of the given #BeagleProperty to @key.
134 beagle_property_set_key (BeagleProperty
*prop
, const char *key
)
136 g_return_if_fail (prop
!= NULL
);
139 prop
->key
= g_strdup (key
);
143 * beagle_property_get_value:
144 * @prop: a #BeagleProperty
146 * Fetches the value of the given #BeagleProperty.
148 * Return Value: the value of the #BeagleProperty.
150 G_CONST_RETURN
char *
151 beagle_property_get_value (BeagleProperty
*prop
)
153 g_return_val_if_fail (prop
!= NULL
, NULL
);
159 * beagle_property_set_value:
160 * @prop: a #BeagleProperty
163 * Sets the value of the given #BeagleProperty to @value.
166 beagle_property_set_value (BeagleProperty
*prop
, const char *value
)
168 g_return_if_fail (prop
!= NULL
);
170 g_free (prop
->value
);
171 prop
->key
= g_strdup (value
);
175 * beagle_property_get_is_searched:
176 * @prop: a #BeagleProperty
178 * Fetches whether the given #BeagleProperty is searched.
180 * Return value: whether the #BeagleProperty is searched.
183 beagle_property_get_is_searched (BeagleProperty
*prop
)
185 g_return_val_if_fail (prop
!= NULL
, FALSE
);
187 return prop
->is_searched
;
191 * beagle_property_set_is_searched:
192 * @prop: a #BeagleProperty
193 * @is_searched: a boolean
195 * Sets whether the given #BeagleProperty is searched. By default, text properties
196 * are searched and keyword properties are not searched.
199 beagle_property_set_is_searched (BeagleProperty
*prop
, gboolean is_searched
)
201 g_return_if_fail (prop
!= NULL
);
203 prop
->is_searched
= is_searched
!= FALSE
;
207 * beagle_property_get_is_mutable:
208 * @prop: a #BeagleProperty
210 * Fetches whether the given #BeagleProperty is mutable.
212 * Return value: whether the #BeagleProperty is mutable.
215 beagle_property_get_is_mutable (BeagleProperty
*prop
)
217 g_return_val_if_fail (prop
!= NULL
, FALSE
);
219 return prop
->is_mutable
;
223 * beagle_property_set_is_mutable:
224 * @prop: a #BeagleProperty
225 * @is_mutable: a boolean
227 * Sets whether the given #BeagleProperty is mutable.
230 beagle_property_set_is_mutable (BeagleProperty
*prop
, gboolean is_mutable
)
232 g_return_if_fail (prop
!= NULL
);
234 prop
->is_mutable
= is_mutable
!= FALSE
;
238 * beagle_property_get_is_stored:
239 * @prop: a #BeagleProperty
241 * Fetches whether the given #BeagleProperty is stored in the index, or just a
244 * Return value: whether the #BeagleProperty is stored.
247 beagle_property_get_is_stored (BeagleProperty
*prop
)
249 g_return_val_if_fail (prop
!= NULL
, FALSE
);
251 return prop
->is_stored
;
255 * beagle_property_set_is_stored:
256 * @prop: a #BeagleProperty
257 * @is_stored: a boolean
259 * Sets whether the given #BeagleProperty is stored in the index, or just a
260 * hint to filters. By default, properties are stored.
263 beagle_property_set_is_stored (BeagleProperty
*prop
, gboolean is_stored
)
265 g_return_if_fail (prop
!= NULL
);
267 prop
->is_stored
= is_stored
!= FALSE
;
271 * Compares two BeagleProperty based on their keys.
274 _beagle_property_compare (BeagleProperty
*prop_a
, BeagleProperty
*prop_b
)
276 return strcmp (prop_a
->key
, prop_b
->key
);
280 * Compares a BeagleProperty (wrt its key) and another given key.
281 * Useful when trying to search for a property with a given key
282 * in a list of BeagleProperty elements.
284 int _beagle_property_key_compare (BeagleProperty
*prop_a
, char *key
)
286 return strcmp (prop_a
->key
, key
);
289 static const char * const property_types
[] = {
297 prop_to_xml (gpointer value
, gpointer user_data
)
299 BeagleProperty
*prop
= value
;
301 GString
*data
= user_data
;
303 if (prop
->type
<= BEAGLE_PROPERTY_TYPE_UNKNOWN
||
304 prop
->type
>= BEAGLE_PROPERTY_TYPE_LAST
)
307 g_string_append (data
, "<Property ");
309 tmp
= g_markup_printf_escaped ("Type=\"%s\" IsSearched=\"%s\" IsMutable=\"%s\" "
310 "IsStored=\"%s\" Key=\"%s\" Value=\"%s\"/>",
311 property_types
[prop
->type
],
312 prop
->is_searched
? "true" : "false",
313 prop
->is_mutable
? "true" : "false",
314 prop
->is_stored
? "true" : "false",
315 prop
->key
, prop
->value
);
317 g_string_append (data
, tmp
);
322 _beagle_properties_to_xml (GSList
*properties
, GString
*data
)
324 g_string_append (data
, "<Properties>");
326 if (properties
!= NULL
)
327 g_slist_foreach (properties
, prop_to_xml
, data
);
329 g_string_append (data
, "</Properties>");