cvsimport
[beagle.git] / libbeagle / beagle / beagle-hits-added-response.c
blobeb802474f0e8cbcb9a3cafb463677cdec987a91f
1 /*
2 * beagle-hits-added-response.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 <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
33 #include "beagle-hits-added-response.h"
34 #include "beagle-private.h"
35 #include "beagle-property.h"
37 typedef struct {
38 BeagleHit *hit; /* Current hit */
39 BeagleProperty *prop; /* Current property; */
41 GSList *hits; /* of BeagleHit */
42 int num_matches; /* Actual number of matches in index */
43 } BeagleHitsAddedResponsePrivate;
45 #define BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), BEAGLE_TYPE_HITS_ADDED_RESPONSE, BeagleHitsAddedResponsePrivate))
47 static BeagleResponseClass *parent_class = NULL;
49 G_DEFINE_TYPE (BeagleHitsAddedResponse, beagle_hits_added_response, BEAGLE_TYPE_RESPONSE)
51 static void
52 beagle_hits_added_response_finalize (GObject *obj)
54 BeagleHitsAddedResponsePrivate *priv = BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (obj);
56 _beagle_hit_list_free (priv->hits);
58 if (G_OBJECT_CLASS (parent_class)->finalize)
59 G_OBJECT_CLASS (parent_class)->finalize (obj);
62 static void
63 end_num_matches (BeagleParserContext *ctx)
65 BeagleHitsAddedResponse *response = BEAGLE_HITS_ADDED_RESPONSE (_beagle_parser_context_get_response (ctx));
66 BeagleHitsAddedResponsePrivate *priv = BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response);
68 char *buf;
69 buf = _beagle_parser_context_get_text_buffer (ctx);
71 priv->num_matches = (int) g_ascii_strtod (buf, NULL);
73 g_free (buf);
76 static void
77 start_hit (BeagleParserContext *ctx, const char **attrs)
79 BeagleHitsAddedResponse *response = BEAGLE_HITS_ADDED_RESPONSE (_beagle_parser_context_get_response (ctx));
80 BeagleHitsAddedResponsePrivate *priv = BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response);
82 int i;
84 priv->hit = _beagle_hit_new ();
86 for (i = 0; attrs[i] != NULL; i += 2) {
87 if (strcmp (attrs[i], "Uri") == 0)
88 priv->hit->uri = g_strdup (attrs[i + 1]);
89 else if (strcmp (attrs[i], "ParentUri") == 0)
90 priv->hit->parent_uri = g_strdup (attrs[i + 1]);
91 else if (strcmp (attrs[i], "Timestamp") == 0)
92 priv->hit->timestamp = beagle_timestamp_new_from_string (attrs[i + 1]);
93 else if (strcmp (attrs[i], "Type") == 0)
94 priv->hit->type = g_strdup (attrs[i + 1]);
95 else if (strcmp (attrs[i], "MimeType") == 0)
96 priv->hit->mime_type = g_strdup (attrs[i + 1]);
97 else if (strcmp (attrs[i], "Source") == 0)
98 priv->hit->source = g_strdup (attrs[i + 1]);
99 else if (strcmp (attrs[i], "Score") == 0)
100 priv->hit->score = g_ascii_strtod (attrs[i + 1], NULL);
102 else
103 g_warning ("unknown attribute \"%s\" with value \"%s\"", attrs[i], attrs[i + 1]);
107 static void
108 end_hit (BeagleParserContext *ctx)
110 BeagleHitsAddedResponse *response = BEAGLE_HITS_ADDED_RESPONSE (_beagle_parser_context_get_response (ctx));
111 BeagleHitsAddedResponsePrivate *priv = BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response);
113 priv->hits = g_slist_prepend (priv->hits, priv->hit);
114 priv->hit = NULL;
117 static void
118 start_property (BeagleParserContext *ctx, const char **attrs)
120 BeagleHitsAddedResponse *response = BEAGLE_HITS_ADDED_RESPONSE (_beagle_parser_context_get_response (ctx));
121 BeagleHitsAddedResponsePrivate *priv = BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response);
122 BeaglePropertyType type = BEAGLE_PROPERTY_TYPE_UNKNOWN;
123 const char *key = NULL, *value = NULL;
124 gboolean is_mutable = FALSE, is_searched = FALSE, is_stored = FALSE;
125 int i;
127 for (i = 0; attrs[i] != NULL; i += 2) {
128 if (strcmp (attrs[i], "Key") == 0)
129 key = attrs[i + 1];
130 else if (strcmp (attrs[i], "Value") == 0)
131 value = attrs[i + 1];
132 else if (strcmp (attrs[i], "IsMutable") == 0)
133 is_mutable = strcmp (attrs[i + 1], "true") == 0;
134 else if (strcmp (attrs[i], "IsSearched") == 0)
135 is_searched = strcmp (attrs[i + 1], "true") == 0;
136 else if (strcmp (attrs[i], "IsStored") == 0)
137 is_stored = strcmp (attrs[i + 1], "true") == 0;
138 else if (strcmp (attrs[i], "Type") == 0) {
139 if (strcmp (attrs [i + 1], "Text") == 0)
140 type = BEAGLE_PROPERTY_TYPE_TEXT;
141 else if (strcmp (attrs [i + 1], "Keyword") == 0)
142 type = BEAGLE_PROPERTY_TYPE_KEYWORD;
143 else if (strcmp (attrs [i + 1], "Date") == 0)
144 type = BEAGLE_PROPERTY_TYPE_DATE;
145 } else
146 g_warning ("could not handle %s", attrs[i]);
149 if (!key || !value) {
150 g_warning ("key or value was null");
151 return;
154 priv->prop = beagle_property_new (type, key, value);
155 priv->prop->is_mutable = is_mutable;
156 priv->prop->is_searched = is_searched;
159 static void
160 end_property (BeagleParserContext *ctx)
162 BeagleHitsAddedResponse *response = BEAGLE_HITS_ADDED_RESPONSE (_beagle_parser_context_get_response (ctx));
163 BeagleHitsAddedResponsePrivate *priv = BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response);
165 _beagle_hit_add_property (priv->hit, priv->prop);
166 priv->prop = NULL;
169 enum {
170 PARSER_STATE_NUM_MATCHES,
171 PARSER_STATE_HITS,
172 PARSER_STATE_HIT,
173 PARSER_STATE_PROPERTIES,
174 PARSER_STATE_PROPERTY
177 static BeagleParserHandler parser_handlers[] = {
178 { "NumMatches",
180 PARSER_STATE_NUM_MATCHES,
181 NULL,
182 end_num_matches},
184 { "Hits",
186 PARSER_STATE_HITS,
187 NULL,
188 NULL },
190 { "Hit",
191 PARSER_STATE_HITS,
192 PARSER_STATE_HIT,
193 start_hit,
194 end_hit },
196 { "Properties",
197 PARSER_STATE_HIT,
198 PARSER_STATE_PROPERTIES,
199 NULL,
200 NULL },
202 { "Property",
203 PARSER_STATE_PROPERTIES,
204 PARSER_STATE_PROPERTY,
205 start_property,
206 end_property },
208 { 0 }
211 static void
212 beagle_hits_added_response_class_init (BeagleHitsAddedResponseClass *klass)
214 GObjectClass *obj_class = G_OBJECT_CLASS (klass);
216 parent_class = g_type_class_peek_parent (klass);
218 obj_class->finalize = beagle_hits_added_response_finalize;
220 _beagle_response_class_set_parser_handlers (BEAGLE_RESPONSE_CLASS (klass),
221 parser_handlers);
223 g_type_class_add_private (klass, sizeof (BeagleHitsAddedResponsePrivate));
226 static void
227 beagle_hits_added_response_init (BeagleHitsAddedResponse *response)
232 * beagle_hits_added_response_get_hits:
233 * @response: a #BeagleHitsAddedResponse
235 * Fetches the hits from the given #BeagleHitsAddedResponse. The list should not be modified or freed.
237 * Return value: A list of #BeagleHit.
239 GSList *
240 beagle_hits_added_response_get_hits (BeagleHitsAddedResponse *response)
242 BeagleHitsAddedResponsePrivate *priv;
244 g_return_val_if_fail (BEAGLE_IS_HITS_ADDED_RESPONSE (response), NULL);
246 priv = BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response);
248 return priv->hits;
252 * beagle_hits_added_response_get_num_matches
253 * @response: a #BeagleHitsAddedResponse
255 * Fetches the total of matches for this query from the given #BeagleHitsAddedResponse. The actual number of results returned is set by max-hits in #BeagleQuery.
257 * Return value: Total number of actual matches.
260 beagle_hits_added_response_get_num_matches (BeagleHitsAddedResponse *response)
262 BeagleHitsAddedResponsePrivate *priv;
264 g_return_val_if_fail (BEAGLE_IS_HITS_ADDED_RESPONSE (response), -1);
265 priv = BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response);
266 g_return_val_if_fail (priv != NULL, -1);
268 return priv->num_matches;