Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / servers / groupwise / e-gw-filter.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* 
3  * Authors : 
4  * Sivaiah Nallagatla <snallagatla@novell.com> 
5  * 
6  *
7  * Copyright 2003, Novell, Inc.
8  *
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.
12  *
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.
17  *
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
21  * USA
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "e-gw-filter.h"
29 #include "e-gw-message.h"
30
31 static GObjectClass *parent_class = NULL;
32
33 struct _FilterComponent {
34         int operation;
35         char *field_name;
36         char *field_value;
37         int num_of_conditions;
38 };
39
40 typedef struct _FilterComponent  FilterComponent;
41
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*/
45   
46 };
47
48
49
50 void
51 e_gw_filter_add_filter_component (EGwFilter *filter, EGwFilterOpType operation, const char *field_name, const char *field_value)
52 {
53         FilterComponent *component;
54   
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);
58   
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);
64
65 }
66
67 void 
68 e_gw_filter_group_conditions (EGwFilter *filter, EGwFilterOpType operation, int num_of_condtions)
69 {
70         FilterComponent *component;
71   
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);
79 }
80
81 static void 
82 append_child_component (FilterComponent* filter_component, SoupSoapMessage *msg)
83 {
84
85         char *operation_name;
86
87         g_return_if_fail (SOUP_IS_SOAP_MESSAGE (msg));
88         soup_soap_message_start_element (msg, "element", NULL, NULL);
89         operation_name = NULL;
90   
91         switch (filter_component->operation) {
92     
93                 case E_GW_FILTER_OP_EQUAL :
94                         operation_name = "eq";
95                         break;
96                 case E_GW_FILTER_OP_NOTEQUAL :
97                         operation_name = "ne";
98                         break;
99                 case E_GW_FILTER_OP_GREATERTHAN :
100                         operation_name = "gt";
101                         break;
102                 case E_GW_FILTER_OP_LESSTHAN :
103                         operation_name = "lt";
104                         break;
105                 case E_GW_FILTER_OP_GREATERTHAN_OR_EQUAL :
106                         operation_name = "gte";
107                         break;
108                 case E_GW_FILTER_OP_LESSTHAN_OR_EQUAL :
109                         operation_name = "lte";
110                         break;
111                 case E_GW_FILTER_OP_CONTAINS :
112                         operation_name = "contains";
113                         break;
114                 case E_GW_FILTER_OP_CONTAINSWORD :
115                         operation_name = "containsWord";
116                         break;
117                 case E_GW_FILTER_OP_BEGINS :
118                         operation_name = "begins";
119                         break;
120                 case E_GW_FILTER_OP_EXISTS :
121                         operation_name = "exists";
122                         break;
123                 case E_GW_FILTER_OP_NOTEXISTS :
124                         operation_name = "notExist";
125                         break;
126                                  
127         }
128         
129                 
130         if (operation_name != NULL) {
131                 
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);
135         }
136
137         soup_soap_message_end_element (msg);
138 }
139
140
141
142 static GSList* 
143 append_complex_component (GSList *component_list, SoupSoapMessage *msg)
144 {
145         FilterComponent *filter_component;
146         int num_of_condtions;
147         int i;
148
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 ) {
152                 
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");
158                 else
159                         e_gw_message_write_string_parameter (msg, "op", NULL, "not");
160         }
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);
168                 }
169                 else 
170                         append_child_component (filter_component, msg);
171                 
172
173         }
174         soup_soap_message_end_element (msg);
175
176         return component_list;
177 }
178
179 void 
180 e_gw_filter_append_to_soap_message (EGwFilter *filter, SoupSoapMessage *msg)
181 {
182         EGwFilterPrivate *priv;
183         GSList *component_list;
184         FilterComponent *filter_component;
185
186         g_return_if_fail (E_IS_GW_FILTER (filter));
187         g_return_if_fail (SOUP_IS_SOAP_MESSAGE (msg));
188  
189         priv = filter->priv;
190         component_list = priv->component_list;
191
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);
198                 }
199                 else 
200                         append_child_component (filter_component, msg);
201         soup_soap_message_end_element (msg); //end filter
202     
203         }
204 }
205
206 static void
207 e_gw_filter_finalize (GObject *object)
208 {
209         EGwFilter *filter;
210         EGwFilterPrivate *priv;
211         GSList *filter_components;
212         FilterComponent *component;
213
214         filter = E_GW_FILTER (object);
215         priv = filter->priv;
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);
222         }
223
224         g_slist_free (priv->component_list);
225         g_free (priv);
226         filter->priv = NULL;
227         if (parent_class->finalize)
228                 (* parent_class->finalize) (object);
229 }
230
231 static void 
232 e_gw_filter_dispose (GObject *object)
233 {
234   
235         if (parent_class->dispose)
236                 (* parent_class->dispose) (object);
237
238 }
239
240 static void
241 e_gw_filter_init (EGwFilter *filter, EGwFilterClass *klass)
242 {
243         EGwFilterPrivate *priv;
244
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;
248         filter->priv = priv;
249 }
250
251
252 static void
253 e_gw_filter_class_init (EGwFilterClass *klass)
254 {
255         GObjectClass *object_class = G_OBJECT_CLASS (klass);
256   
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;
260 }
261
262 GType
263 e_gw_filter_get_type (void)
264 {
265         static GType type = 0;
266   
267         if (!type) {
268                 static GTypeInfo info = {
269                         sizeof (EGwFilterClass),
270                         (GBaseInitFunc) NULL,
271                         (GBaseFinalizeFunc) NULL,
272                         (GClassInitFunc) e_gw_filter_class_init,
273                         NULL, NULL,
274                         sizeof (EGwFilter),
275                         0,
276                         (GInstanceInitFunc) e_gw_filter_init
277                 };
278                 type = g_type_register_static (G_TYPE_OBJECT, "EGwFilter", &info, 0);
279         }
280
281         return type;
282 }
283
284 EGwFilter *
285 e_gw_filter_new (void)
286 {
287         return g_object_new (E_TYPE_GW_FILTER, NULL);
288 }