2006-12-16 Gabor Kelemen <kelemeng@gnome.hu>
[beagle.git] / libbeagle / beagle / beagle-search-term-response.c
blob675edf92892176bc17b21d86223cdf4b744c2cbd
1 /*
2 * beagle-search-term-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-search-term-response.h"
34 #include "beagle-private.h"
35 #include "beagle-property.h"
37 typedef struct {
38 GSList *exact_text; /* of string */
39 GSList *stemmed_text; /* of string */
41 GSList **current_list; /* points to one of the two above */
42 } BeagleSearchTermResponsePrivate;
44 #define BEAGLE_SEARCH_TERM_RESPONSE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), BEAGLE_TYPE_SEARCH_TERM_RESPONSE, BeagleSearchTermResponsePrivate))
46 static BeagleResponseClass *parent_class = NULL;
48 G_DEFINE_TYPE (BeagleSearchTermResponse, beagle_search_term_response, BEAGLE_TYPE_RESPONSE)
50 static void
51 beagle_search_term_response_finalize (GObject *obj)
53 BeagleSearchTermResponsePrivate *priv = BEAGLE_SEARCH_TERM_RESPONSE_GET_PRIVATE (obj);
56 * Note, we don't free the lists or their contents here! This is
57 * because the BeagleSearchTermResponse is internal to BeagleQuery.
58 * This response never happens in reply to any other request, and
59 * the BeagleQuery gets these lists through the get_exact_text()
60 * and get_stemmed_text() functions below. They take ownership
61 * of them.
64 if (G_OBJECT_CLASS (parent_class)->finalize)
65 G_OBJECT_CLASS (parent_class)->finalize (obj);
68 static void
69 start_exact (BeagleParserContext *ctx, const char **attrs)
71 BeagleSearchTermResponse *response = BEAGLE_SEARCH_TERM_RESPONSE (_beagle_parser_context_get_response (ctx));
72 BeagleSearchTermResponsePrivate *priv = BEAGLE_SEARCH_TERM_RESPONSE_GET_PRIVATE (response);
74 priv->current_list = &priv->exact_text;
77 static void
78 start_stemmed (BeagleParserContext *ctx, const char **attrs)
80 BeagleSearchTermResponse *response = BEAGLE_SEARCH_TERM_RESPONSE (_beagle_parser_context_get_response (ctx));
81 BeagleSearchTermResponsePrivate *priv = BEAGLE_SEARCH_TERM_RESPONSE_GET_PRIVATE (response);
83 priv->current_list = &priv->stemmed_text;
86 static void
87 end_text (BeagleParserContext *ctx)
89 BeagleSearchTermResponse *response = BEAGLE_SEARCH_TERM_RESPONSE (_beagle_parser_context_get_response (ctx));
90 BeagleSearchTermResponsePrivate *priv = BEAGLE_SEARCH_TERM_RESPONSE_GET_PRIVATE (response);
92 *priv->current_list = g_slist_append (*priv->current_list, _beagle_parser_context_get_text_buffer (ctx));
95 enum {
96 PARSER_STATE_EXACT,
97 PARSER_STATE_STEMMED,
98 PARSER_STATE_TEXT,
101 static BeagleParserHandler parser_handlers[] = {
102 { "Exact",
104 PARSER_STATE_EXACT,
105 start_exact,
106 NULL },
108 { "Stemmed",
110 PARSER_STATE_STEMMED,
111 start_stemmed,
112 NULL },
114 { "Text",
115 PARSER_STATE_EXACT,
116 PARSER_STATE_TEXT,
117 NULL,
118 end_text },
120 { "Text",
121 PARSER_STATE_STEMMED,
122 PARSER_STATE_TEXT,
123 NULL,
124 end_text },
126 { 0 }
129 static void
130 beagle_search_term_response_class_init (BeagleSearchTermResponseClass *klass)
132 GObjectClass *obj_class = G_OBJECT_CLASS (klass);
134 parent_class = g_type_class_peek_parent (klass);
136 obj_class->finalize = beagle_search_term_response_finalize;
138 _beagle_response_class_set_parser_handlers (BEAGLE_RESPONSE_CLASS (klass),
139 parser_handlers);
141 g_type_class_add_private (klass, sizeof (BeagleSearchTermResponsePrivate));
144 static void
145 beagle_search_term_response_init (BeagleSearchTermResponse *response)
149 GSList *
150 _beagle_search_term_response_get_exact_text (BeagleSearchTermResponse *response)
152 BeagleSearchTermResponsePrivate *priv;
154 g_return_val_if_fail (BEAGLE_IS_SEARCH_TERM_RESPONSE (response), NULL);
156 priv = BEAGLE_SEARCH_TERM_RESPONSE_GET_PRIVATE (response);
158 return priv->exact_text;
161 GSList *
162 _beagle_search_term_response_get_stemmed_text (BeagleSearchTermResponse *response)
164 BeagleSearchTermResponsePrivate *priv;
166 g_return_val_if_fail (BEAGLE_IS_SEARCH_TERM_RESPONSE (response), NULL);
168 priv = BEAGLE_SEARCH_TERM_RESPONSE_GET_PRIVATE (response);
170 return priv->stemmed_text;