File: e-gw-filter.c

package info (click to toggle)
evolution-data-server 1.0.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 39,504 kB
  • ctags: 26,423
  • sloc: ansic: 175,347; tcl: 30,499; sh: 20,699; perl: 11,320; xml: 9,039; java: 7,653; cpp: 6,029; makefile: 4,866; awk: 1,338; yacc: 1,103; sed: 772; cs: 505; lex: 134; asm: 14
file content (288 lines) | stat: -rw-r--r-- 7,998 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * Authors : 
 * Sivaiah Nallagatla <snallagatla@novell.com> 
 * 
 *
 * Copyright 2003, Novell, Inc.
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of version 2 of the GNU General Public 
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-gw-filter.h"
#include "e-gw-message.h"

static GObjectClass *parent_class = NULL;

struct _FilterComponent {
	int operation;
	char *field_name;
	char *field_value;
	int num_of_conditions;
};

typedef struct _FilterComponent  FilterComponent;

struct _EGwFilterPrivate {
	GSList *component_list;
	int  filter_group_type; /* stores, whether all condtions are to be met or any one of them*/
  
};



void
e_gw_filter_add_filter_component (EGwFilter *filter, EGwFilterOpType operation, const char *field_name, const char *field_value)
{
	FilterComponent *component;
  
	g_return_if_fail (E_IS_GW_FILTER (filter));
	g_return_if_fail (field_name != NULL);
	g_return_if_fail (field_value != NULL);
  
	component = g_new0 (FilterComponent, 1);
	component->operation = operation;
	component->field_name = g_strdup (field_name);
	component->field_value = g_strdup (field_value);
	filter->priv->component_list = g_slist_prepend (filter->priv->component_list, component);

}

void 
e_gw_filter_group_conditions (EGwFilter *filter, EGwFilterOpType operation, int num_of_condtions)
{
	FilterComponent *component;
  
	g_return_if_fail (E_IS_GW_FILTER (filter));
	g_return_if_fail ( operation == E_GW_FILTER_OP_AND || operation == E_GW_FILTER_OP_OR ||
			  operation == E_GW_FILTER_OP_NOT);
	component = g_new0 (FilterComponent, 1);
	component->operation = operation;
	component->num_of_conditions =  num_of_condtions;
	filter->priv->component_list = g_slist_prepend (filter->priv->component_list, component);
}

static void 
append_child_component (FilterComponent* filter_component, SoupSoapMessage *msg)
{

	char *operation_name;

	g_return_if_fail (SOUP_IS_SOAP_MESSAGE (msg));
	soup_soap_message_start_element (msg, "element", NULL, NULL);
	operation_name = NULL;
  
	switch (filter_component->operation) {
    
	        case E_GW_FILTER_OP_EQUAL :
			operation_name = "eq";
			break;
		case E_GW_FILTER_OP_NOTEQUAL :
			operation_name = "ne";
			break;
		case E_GW_FILTER_OP_GREATERTHAN :
			operation_name = "gt";
			break;
		case E_GW_FILTER_OP_LESSTHAN :
			operation_name = "lt";
			break;
		case E_GW_FILTER_OP_GREATERTHAN_OR_EQUAL :
			operation_name = "gte";
			break;
		case E_GW_FILTER_OP_LESSTHAN_OR_EQUAL :
			operation_name = "lte";
			break;
		case E_GW_FILTER_OP_CONTAINS :
			operation_name = "contains";
			break;
		case E_GW_FILTER_OP_CONTAINSWORD :
			operation_name = "containsWord";
			break;
		case E_GW_FILTER_OP_BEGINS :
			operation_name = "begins";
			break;
		case E_GW_FILTER_OP_EXISTS :
			operation_name = "exists";
			break;
		case E_GW_FILTER_OP_NOTEXISTS :
			operation_name = "notExist";
			break;
				 
	}
	
		
	if (operation_name != NULL) {
		
			e_gw_message_write_string_parameter (msg, "op", NULL, operation_name);
			e_gw_message_write_string_parameter (msg, "field", NULL, filter_component->field_name);
			e_gw_message_write_string_parameter (msg, "value", NULL, filter_component->field_value);
	}

	soup_soap_message_end_element (msg);
}



static GSList* 
append_complex_component (GSList *component_list, SoupSoapMessage *msg)
{
	FilterComponent *filter_component;
	int num_of_condtions;
	int i;

	filter_component = (FilterComponent* )component_list->data;
	if (filter_component->operation == E_GW_FILTER_OP_AND || filter_component->operation == E_GW_FILTER_OP_OR
	    ||  filter_component->operation == E_GW_FILTER_OP_NOT ) {
		
		soup_soap_message_start_element (msg, "element", NULL, NULL);
		if (filter_component->operation == E_GW_FILTER_OP_AND)
			e_gw_message_write_string_parameter (msg, "op", NULL, "and");
		else if (filter_component->operation == E_GW_FILTER_OP_OR) 
			e_gw_message_write_string_parameter (msg, "op", NULL, "or");
		else
			e_gw_message_write_string_parameter (msg, "op", NULL, "not");
	}
	num_of_condtions = filter_component->num_of_conditions;
	for ( i = 0; i < num_of_condtions && component_list; i++) {
		component_list = g_slist_next (component_list);
		filter_component = (FilterComponent *)component_list->data;
		if (filter_component->operation == E_GW_FILTER_OP_AND || filter_component->operation == E_GW_FILTER_OP_OR
		    || filter_component->operation == E_GW_FILTER_OP_NOT ) {
			component_list = append_complex_component (component_list, msg);
		}
		else 
			append_child_component (filter_component, msg);
		

	}
	soup_soap_message_end_element (msg);

	return component_list;
}

void 
e_gw_filter_append_to_soap_message (EGwFilter *filter, SoupSoapMessage *msg)
{
	EGwFilterPrivate *priv;
	GSList *component_list;
	FilterComponent *filter_component;

	g_return_if_fail (E_IS_GW_FILTER (filter));
	g_return_if_fail (SOUP_IS_SOAP_MESSAGE (msg));
 
	priv = filter->priv;
	component_list = priv->component_list;
   
	soup_soap_message_start_element (msg, "filter", NULL, NULL);
	for (; component_list != NULL; component_list = g_slist_next (component_list)) {
		filter_component = (FilterComponent *) (component_list->data);
		if (filter_component->operation == E_GW_FILTER_OP_AND || filter_component->operation == E_GW_FILTER_OP_OR
		    || filter_component->operation == E_GW_FILTER_OP_NOT) {
			component_list = append_complex_component (component_list, msg);
		}
		else 
			append_child_component (filter_component, msg);
	soup_soap_message_end_element (msg); //end filter
    
	}
}

static void
e_gw_filter_finalize (GObject *object)
{
	EGwFilter *filter;
	EGwFilterPrivate *priv;
	GSList *filter_components;
	FilterComponent *component;

	filter = E_GW_FILTER (object);
	priv = filter->priv;
	filter_components = priv->component_list;
	for (; filter_components != NULL; filter_components = filter_components = g_slist_next (filter_components)) {
		component = filter_components->data;
		g_free (component->field_name);
		g_free (component->field_value);
		g_free (filter_components->data);
	}

	g_slist_free (priv->component_list);
	g_free (priv);
	filter->priv = NULL;
	if (parent_class->finalize)
		(* parent_class->finalize) (object);
}

static void 
e_gw_filter_dispose (GObject *object)
{
  
	if (parent_class->dispose)
		(* parent_class->dispose) (object);

}

static void
e_gw_filter_init (EGwFilter *filter, EGwFilterClass *klass)
{
	EGwFilterPrivate *priv;

	priv = g_new0(EGwFilterPrivate, 1);
	priv->filter_group_type = E_GW_FILTER_OP_AND; /*by default all condtions are to be met*/
	priv->component_list = NULL;
	filter->priv = priv;
}


static void
e_gw_filter_class_init (EGwFilterClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
  
	parent_class = g_type_class_peek_parent (klass);
	object_class->dispose = e_gw_filter_dispose;
	object_class->finalize = e_gw_filter_finalize;
}

GType
e_gw_filter_get_type (void)
{
	static GType type = 0;
  
	if (!type) {
		static GTypeInfo info = {
			sizeof (EGwFilterClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) e_gw_filter_class_init,
			NULL, NULL,
			sizeof (EGwFilter),
			0,
			(GInstanceInitFunc) e_gw_filter_init
		};
		type = g_type_register_static (G_TYPE_OBJECT, "EGwFilter", &info, 0);
	}

	return type;
}

EGwFilter *
e_gw_filter_new (void)
{
	return g_object_new (E_TYPE_GW_FILTER, NULL);
}