Compute lucene-style scores for our hits.
[beagle.git] / libbeagle / beagle / beagle-hits-added-response.c
blob358b7f59dc4f368813b96fa7769a42abf3e5ca96
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 } BeagleHitsAddedResponsePrivate;
44 #define BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), BEAGLE_TYPE_HITS_ADDED_RESPONSE, BeagleHitsAddedResponsePrivate))
46 static BeagleResponseClass *parent_class = NULL;
48 G_DEFINE_TYPE (BeagleHitsAddedResponse, beagle_hits_added_response, BEAGLE_TYPE_RESPONSE)
50 static void
51 beagle_hits_added_response_finalize (GObject *obj)
53 BeagleHitsAddedResponsePrivate *priv = BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (obj);
55 _beagle_hit_list_free (priv->hits);
57 if (G_OBJECT_CLASS (parent_class)->finalize)
58 G_OBJECT_CLASS (parent_class)->finalize (obj);
61 static void
62 start_hit (BeagleParserContext *ctx, const char **attrs)
64 BeagleHitsAddedResponse *response = BEAGLE_HITS_ADDED_RESPONSE (_beagle_parser_context_get_response (ctx));
65 BeagleHitsAddedResponsePrivate *priv = BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response);
67 int i;
69 priv->hit = _beagle_hit_new ();
71 for (i = 0; attrs[i] != NULL; i += 2) {
72 if (strcmp (attrs[i], "Uri") == 0)
73 priv->hit->uri = g_strdup (attrs[i + 1]);
74 else if (strcmp (attrs[i], "ParentUri") == 0)
75 priv->hit->parent_uri = g_strdup (attrs[i + 1]);
76 else if (strcmp (attrs[i], "Timestamp") == 0)
77 priv->hit->timestamp = beagle_timestamp_new_from_string (attrs[i + 1]);
78 else if (strcmp (attrs[i], "Type") == 0)
79 priv->hit->type = g_strdup (attrs[i + 1]);
80 else if (strcmp (attrs[i], "MimeType") == 0)
81 priv->hit->mime_type = g_strdup (attrs[i + 1]);
82 else if (strcmp (attrs[i], "Source") == 0)
83 priv->hit->source = g_strdup (attrs[i + 1]);
84 else if (strcmp (attrs[i], "SourceObjectName") == 0)
85 priv->hit->source_object_name = g_strdup (attrs[i + 1]);
86 else if (strcmp (attrs[i], "Score") == 0)
87 priv->hit->score = g_ascii_strtod (attrs[i + 1], NULL);
88 else
89 g_warning ("unknown attribute \"%s\" with value \"%s\"", attrs[i], attrs[i + 1]);
93 static void
94 end_hit (BeagleParserContext *ctx)
96 BeagleHitsAddedResponse *response = BEAGLE_HITS_ADDED_RESPONSE (_beagle_parser_context_get_response (ctx));
97 BeagleHitsAddedResponsePrivate *priv = BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response);
99 priv->hits = g_slist_prepend (priv->hits, priv->hit);
100 priv->hit = NULL;
103 static void
104 start_property (BeagleParserContext *ctx, const char **attrs)
106 BeagleHitsAddedResponse *response = BEAGLE_HITS_ADDED_RESPONSE (_beagle_parser_context_get_response (ctx));
107 BeagleHitsAddedResponsePrivate *priv = BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response);
108 BeaglePropertyType type = BEAGLE_PROPERTY_TYPE_UNKNOWN;
109 const char *key = NULL, *value = NULL;
110 gboolean is_mutable = FALSE, is_searched = FALSE;
111 int i;
113 for (i = 0; attrs[i] != NULL; i += 2) {
114 if (strcmp (attrs[i], "Key") == 0)
115 key = attrs[i + 1];
116 else if (strcmp (attrs[i], "Value") == 0)
117 value = attrs[i + 1];
118 else if (strcmp (attrs[i], "IsMutable") == 0)
119 is_mutable = strcmp (attrs[i + 1], "true") == 0;
120 else if (strcmp (attrs[i], "IsSearched") == 0)
121 is_searched = strcmp (attrs[i + 1], "true") == 0;
122 else if (strcmp (attrs[i], "Type") == 0)
123 if (strcmp (attrs [i + 1], "Text") == 0)
124 type = BEAGLE_PROPERTY_TYPE_TEXT;
125 else if (strcmp (attrs [i + 1], "Keyword") == 0)
126 type = BEAGLE_PROPERTY_TYPE_KEYWORD;
127 else if (strcmp (attrs [i + 1], "Date") == 0)
128 type = BEAGLE_PROPERTY_TYPE_DATE;
129 else
130 g_warning ("could not handle %s", attrs[i]);
133 if (!key || !value) {
134 g_warning ("key or value was null");
135 return;
138 priv->prop = beagle_property_new (type, key, value);
139 priv->prop->is_mutable = is_mutable;
140 priv->prop->is_searched = is_searched;
143 static void
144 end_property (BeagleParserContext *ctx)
146 BeagleHitsAddedResponse *response = BEAGLE_HITS_ADDED_RESPONSE (_beagle_parser_context_get_response (ctx));
147 BeagleHitsAddedResponsePrivate *priv = BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response);
149 _beagle_hit_add_property (priv->hit, priv->prop);
150 priv->prop = NULL;
153 enum {
154 PARSER_STATE_HITS,
155 PARSER_STATE_HIT,
156 PARSER_STATE_PROPERTIES,
157 PARSER_STATE_PROPERTY
160 static BeagleParserHandler parser_handlers[] = {
161 { "Hits",
163 PARSER_STATE_HITS,
164 NULL,
165 NULL },
167 { "Hit",
168 PARSER_STATE_HITS,
169 PARSER_STATE_HIT,
170 start_hit,
171 end_hit },
173 { "Properties",
174 PARSER_STATE_HIT,
175 PARSER_STATE_PROPERTIES,
176 NULL,
177 NULL },
178 { "Property",
179 PARSER_STATE_PROPERTIES,
180 PARSER_STATE_PROPERTY,
181 start_property,
182 end_property },
184 { 0 }
187 static void
188 beagle_hits_added_response_class_init (BeagleHitsAddedResponseClass *klass)
190 GObjectClass *obj_class = G_OBJECT_CLASS (klass);
192 parent_class = g_type_class_peek_parent (klass);
194 obj_class->finalize = beagle_hits_added_response_finalize;
196 _beagle_response_class_set_parser_handlers (BEAGLE_RESPONSE_CLASS (klass),
197 parser_handlers);
199 g_type_class_add_private (klass, sizeof (BeagleHitsAddedResponsePrivate));
202 static void
203 beagle_hits_added_response_init (BeagleHitsAddedResponse *response)
208 * beagle_hits_added_response_get_hits:
209 * @response: a #BeagleHitsAddedResponse
211 * Fetches the hits from the given #BeagleHitsAddedResponse. The list should not be modified or freed.
213 * Return value: A list of #BeagleHit.
215 GSList *
216 beagle_hits_added_response_get_hits (BeagleHitsAddedResponse *response)
218 BeagleHitsAddedResponsePrivate *priv;
220 g_return_val_if_fail (BEAGLE_IS_HITS_ADDED_RESPONSE (response), NULL);
222 priv = BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response);
224 return priv->hits;