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 prop
->is_searched
= TRUE
;
54 prop
->is_stored
= TRUE
;
60 * beagle_property_free:
61 * @prop: a #BeagleProperty
63 * Frees the memory allocated for the #BeagleProperty.
66 beagle_property_free (BeagleProperty
*prop
)
68 g_return_if_fail (prop
!= NULL
);
76 * beagle_property_get_type:
77 * @prop: a #BeagleProperty
79 * Fetches the type of the #BeagleProperty.
81 * Return value: the #BeaglePropertyType of the #BeagleProperty.
84 beagle_property_get_type (BeagleProperty
*prop
)
86 g_return_val_if_fail (prop
!= NULL
, BEAGLE_PROPERTY_TYPE_UNKNOWN
);
92 * beagle_property_set_type:
93 * @prop: a #BeagleProperty
94 * @type: a #BeaglePropertyType
96 * Sets the type of the given #BeagleProperty to @type.
99 beagle_property_set_type (BeagleProperty
*prop
, BeaglePropertyType type
)
101 g_return_if_fail (prop
!= NULL
);
107 * beagle_property_get_key:
108 * @prop: a #BeagleProperty
110 * Fetches the key of the #BeagleProperty.
112 * Return value: the key name of the #BeagleProperty.
114 G_CONST_RETURN
char *
115 beagle_property_get_key (BeagleProperty
*prop
)
117 g_return_val_if_fail (prop
!= NULL
, NULL
);
123 * beagle_property_set_key:
124 * @prop: a #BeagleProperty
127 * Sets the key of the given #BeagleProperty to @key.
130 beagle_property_set_key (BeagleProperty
*prop
, const char *key
)
132 g_return_if_fail (prop
!= NULL
);
135 prop
->key
= g_strdup (key
);
139 * beagle_property_get_value:
140 * @prop: a #BeagleProperty
142 * Fetches the value of the given #BeagleProperty.
144 * Return Value: the value of the #BeagleProperty.
146 G_CONST_RETURN
char *
147 beagle_property_get_value (BeagleProperty
*prop
)
149 g_return_val_if_fail (prop
!= NULL
, NULL
);
155 * beagle_property_set_value:
156 * @prop: a #BeagleProperty
159 * Sets the value of the given #BeagleProperty to @value.
162 beagle_property_set_value (BeagleProperty
*prop
, const char *value
)
164 g_return_if_fail (prop
!= NULL
);
166 g_free (prop
->value
);
167 prop
->key
= g_strdup (value
);
171 * beagle_property_get_is_searched:
172 * @prop: a #BeagleProperty
174 * Fetches whether the given #BeagleProperty is searched.
176 * Return value: whether the #BeagleProperty is searched.
179 beagle_property_get_is_searched (BeagleProperty
*prop
)
181 g_return_val_if_fail (prop
!= NULL
, FALSE
);
183 return prop
->is_searched
;
187 * beagle_property_set_is_searched:
188 * @prop: a #BeagleProperty
189 * @is_searched: a boolean
191 * Sets whether the given #BeagleProperty is searched. By default, properties
195 beagle_property_set_is_searched (BeagleProperty
*prop
, gboolean is_searched
)
197 g_return_if_fail (prop
!= NULL
);
199 prop
->is_searched
= is_searched
!= FALSE
;
203 * beagle_property_get_is_mutable:
204 * @prop: a #BeagleProperty
206 * Fetches whether the given #BeagleProperty is mutable.
208 * Return value: whether the #BeagleProperty is mutable.
211 beagle_property_get_is_mutable (BeagleProperty
*prop
)
213 g_return_val_if_fail (prop
!= NULL
, FALSE
);
215 return prop
->is_mutable
;
219 * beagle_property_set_is_mutable:
220 * @prop: a #BeagleProperty
221 * @is_mutable: a boolean
223 * Sets whether the given #BeagleProperty is mutable.
226 beagle_property_set_is_mutable (BeagleProperty
*prop
, gboolean is_mutable
)
228 g_return_if_fail (prop
!= NULL
);
230 prop
->is_mutable
= is_mutable
!= FALSE
;
234 * beagle_property_get_is_stored:
235 * @prop: a #BeagleProperty
237 * Fetches whether the given #BeagleProperty is stored in the index, or just a
240 * Return value: whether the #BeagleProperty is stored.
243 beagle_property_get_is_stored (BeagleProperty
*prop
)
245 g_return_val_if_fail (prop
!= NULL
, FALSE
);
247 return prop
->is_stored
;
251 * beagle_property_set_is_stored:
252 * @prop: a #BeagleProperty
253 * @is_stored: a boolean
255 * Sets whether the given #BeagleProperty is stored in the index, or just a
256 * hint to filters. By default, properties are stored.
259 beagle_property_set_is_stored (BeagleProperty
*prop
, gboolean is_stored
)
261 g_return_if_fail (prop
!= NULL
);
263 prop
->is_stored
= is_stored
!= FALSE
;
267 * Compares two BeagleProperty based on their keys.
270 _beagle_property_compare (BeagleProperty
*prop_a
, BeagleProperty
*prop_b
)
272 return strcmp (prop_a
->key
, prop_b
->key
);
276 * Compares a BeagleProperty (wrt its key) and another given key.
277 * Useful when trying to search for a property with a given key
278 * in a list of BeagleProperty elements.
280 int _beagle_property_key_compare (BeagleProperty
*prop_a
, char *key
)
282 return strcmp (prop_a
->key
, key
);
285 static const char * const property_types
[] = {
293 prop_to_xml (gpointer value
, gpointer user_data
)
295 BeagleProperty
*prop
= value
;
297 GString
*data
= user_data
;
299 if (prop
->type
<= BEAGLE_PROPERTY_TYPE_UNKNOWN
||
300 prop
->type
>= BEAGLE_PROPERTY_TYPE_LAST
)
303 g_string_append (data
, "<Property ");
305 tmp
= g_markup_printf_escaped ("Type=\"%s\" isSearched=\"%s\" isMutable=\"%s\" "
306 "Key=\"%s\" Value=\"%s\"/>",
307 property_types
[prop
->type
],
308 prop
->is_searched
? "true" : "false",
309 prop
->is_mutable
? "true" : "false",
310 prop
->key
, prop
->value
);
312 g_string_append (data
, tmp
);
317 _beagle_properties_to_xml (GSList
*properties
, GString
*data
)
319 g_string_append (data
, "<Properties>");
321 if (properties
!= NULL
)
322 g_slist_foreach (properties
, prop_to_xml
, data
);
324 g_string_append (data
, "</Properties>");