1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
4 * Sivaiah Nallagatla <snallagatla@novell.com>
7 * Copyright 2003, Novell, Inc.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU Lesser General Public
11 * License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
28 #include "e-gw-filter.h"
29 #include "e-gw-message.h"
31 static GObjectClass *parent_class = NULL;
33 struct _FilterComponent {
37 int num_of_conditions;
40 typedef struct _FilterComponent FilterComponent;
42 struct _EGwFilterPrivate {
43 GSList *component_list;
44 int filter_group_type; /* stores, whether all condtions are to be met or any one of them*/
51 e_gw_filter_add_filter_component (EGwFilter *filter, EGwFilterOpType operation, const char *field_name, const char *field_value)
53 FilterComponent *component;
55 g_return_if_fail (E_IS_GW_FILTER (filter));
56 g_return_if_fail (field_name != NULL);
57 g_return_if_fail (field_value != NULL);
59 component = g_new0 (FilterComponent, 1);
60 component->operation = operation;
61 component->field_name = g_strdup (field_name);
62 component->field_value = g_strdup (field_value);
63 filter->priv->component_list = g_slist_prepend (filter->priv->component_list, component);
68 e_gw_filter_group_conditions (EGwFilter *filter, EGwFilterOpType operation, int num_of_condtions)
70 FilterComponent *component;
72 g_return_if_fail (E_IS_GW_FILTER (filter));
73 g_return_if_fail ( operation == E_GW_FILTER_OP_AND || operation == E_GW_FILTER_OP_OR ||
74 operation == E_GW_FILTER_OP_NOT);
75 component = g_new0 (FilterComponent, 1);
76 component->operation = operation;
77 component->num_of_conditions = num_of_condtions;
78 filter->priv->component_list = g_slist_prepend (filter->priv->component_list, component);
82 append_child_component (FilterComponent* filter_component, SoupSoapMessage *msg)
87 g_return_if_fail (SOUP_IS_SOAP_MESSAGE (msg));
88 soup_soap_message_start_element (msg, "element", NULL, NULL);
89 operation_name = NULL;
91 switch (filter_component->operation) {
93 case E_GW_FILTER_OP_EQUAL :
94 operation_name = "eq";
96 case E_GW_FILTER_OP_NOTEQUAL :
97 operation_name = "ne";
99 case E_GW_FILTER_OP_GREATERTHAN :
100 operation_name = "gt";
102 case E_GW_FILTER_OP_LESSTHAN :
103 operation_name = "lt";
105 case E_GW_FILTER_OP_GREATERTHAN_OR_EQUAL :
106 operation_name = "gte";
108 case E_GW_FILTER_OP_LESSTHAN_OR_EQUAL :
109 operation_name = "lte";
111 case E_GW_FILTER_OP_CONTAINS :
112 operation_name = "contains";
114 case E_GW_FILTER_OP_CONTAINSWORD :
115 operation_name = "containsWord";
117 case E_GW_FILTER_OP_BEGINS :
118 operation_name = "begins";
120 case E_GW_FILTER_OP_EXISTS :
121 operation_name = "exists";
123 case E_GW_FILTER_OP_NOTEXISTS :
124 operation_name = "notExist";
130 if (operation_name != NULL) {
132 e_gw_message_write_string_parameter (msg, "op", NULL, operation_name);
133 e_gw_message_write_string_parameter (msg, "field", NULL, filter_component->field_name);
134 e_gw_message_write_string_parameter (msg, "value", NULL, filter_component->field_value);
137 soup_soap_message_end_element (msg);
143 append_complex_component (GSList *component_list, SoupSoapMessage *msg)
145 FilterComponent *filter_component;
146 int num_of_condtions;
149 filter_component = (FilterComponent* )component_list->data;
150 if (filter_component->operation == E_GW_FILTER_OP_AND || filter_component->operation == E_GW_FILTER_OP_OR
151 || filter_component->operation == E_GW_FILTER_OP_NOT ) {
153 soup_soap_message_start_element (msg, "group", NULL, NULL);
154 if (filter_component->operation == E_GW_FILTER_OP_AND)
155 e_gw_message_write_string_parameter (msg, "op", NULL, "and");
156 else if (filter_component->operation == E_GW_FILTER_OP_OR)
157 e_gw_message_write_string_parameter (msg, "op", NULL, "or");
159 e_gw_message_write_string_parameter (msg, "op", NULL, "not");
161 num_of_condtions = filter_component->num_of_conditions;
162 for ( i = 0; i < num_of_condtions && component_list; i++) {
163 component_list = g_slist_next (component_list);
164 filter_component = (FilterComponent *)component_list->data;
165 if (filter_component->operation == E_GW_FILTER_OP_AND || filter_component->operation == E_GW_FILTER_OP_OR
166 || filter_component->operation == E_GW_FILTER_OP_NOT ) {
167 component_list = append_complex_component (component_list, msg);
170 append_child_component (filter_component, msg);
174 soup_soap_message_end_element (msg);
176 return component_list;
180 e_gw_filter_append_to_soap_message (EGwFilter *filter, SoupSoapMessage *msg)
182 EGwFilterPrivate *priv;
183 GSList *component_list;
184 FilterComponent *filter_component;
186 g_return_if_fail (E_IS_GW_FILTER (filter));
187 g_return_if_fail (SOUP_IS_SOAP_MESSAGE (msg));
190 component_list = priv->component_list;
192 soup_soap_message_start_element (msg, "filter", NULL, NULL);
193 for (; component_list != NULL; component_list = g_slist_next (component_list)) {
194 filter_component = (FilterComponent *) (component_list->data);
195 if (filter_component->operation == E_GW_FILTER_OP_AND || filter_component->operation == E_GW_FILTER_OP_OR
196 || filter_component->operation == E_GW_FILTER_OP_NOT) {
197 component_list = append_complex_component (component_list, msg);
200 append_child_component (filter_component, msg);
201 soup_soap_message_end_element (msg); //end filter
207 e_gw_filter_finalize (GObject *object)
210 EGwFilterPrivate *priv;
211 GSList *filter_components;
212 FilterComponent *component;
214 filter = E_GW_FILTER (object);
216 filter_components = priv->component_list;
217 for (; filter_components != NULL; filter_components = g_slist_next (filter_components)) {
218 component = filter_components->data;
219 g_free (component->field_name);
220 g_free (component->field_value);
221 g_free (filter_components->data);
224 g_slist_free (priv->component_list);
227 if (parent_class->finalize)
228 (* parent_class->finalize) (object);
232 e_gw_filter_dispose (GObject *object)
235 if (parent_class->dispose)
236 (* parent_class->dispose) (object);
241 e_gw_filter_init (EGwFilter *filter, EGwFilterClass *klass)
243 EGwFilterPrivate *priv;
245 priv = g_new0(EGwFilterPrivate, 1);
246 priv->filter_group_type = E_GW_FILTER_OP_AND; /*by default all condtions are to be met*/
247 priv->component_list = NULL;
253 e_gw_filter_class_init (EGwFilterClass *klass)
255 GObjectClass *object_class = G_OBJECT_CLASS (klass);
257 parent_class = g_type_class_peek_parent (klass);
258 object_class->dispose = e_gw_filter_dispose;
259 object_class->finalize = e_gw_filter_finalize;
263 e_gw_filter_get_type (void)
265 static GType type = 0;
268 static GTypeInfo info = {
269 sizeof (EGwFilterClass),
270 (GBaseInitFunc) NULL,
271 (GBaseFinalizeFunc) NULL,
272 (GClassInitFunc) e_gw_filter_class_init,
276 (GInstanceInitFunc) e_gw_filter_init
278 type = g_type_register_static (G_TYPE_OBJECT, "EGwFilter", &info, 0);
285 e_gw_filter_new (void)
287 return g_object_new (E_TYPE_GW_FILTER, NULL);