1 /*
2  *
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
10  * for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public License
13  * along with this program; if not, see <http://www.gnu.org/licenses/>.
14  *
15  *
16  * Authors:
17  *		Not Zed <notzed@lostzed.mmc.com.au>
18  *      Jeffrey Stedfast <fejj@ximian.com>
19  *
20  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
21  *
22  */
23 
24 #if !defined (__E_UTIL_H_INSIDE__) && !defined (LIBEUTIL_COMPILATION)
25 #error "Only <e-util/e-util.h> should be included directly."
26 #endif
27 
28 #ifndef E_RULE_CONTEXT_H
29 #define E_RULE_CONTEXT_H
30 
31 #include <libxml/parser.h>
32 
33 #include <e-util/e-filter-part.h>
34 #include <e-util/e-filter-rule.h>
35 
36 /* Standard GObject macros */
37 #define E_TYPE_RULE_CONTEXT \
38 	(e_rule_context_get_type ())
39 #define E_RULE_CONTEXT(obj) \
40 	(G_TYPE_CHECK_INSTANCE_CAST \
41 	((obj), E_TYPE_RULE_CONTEXT, ERuleContext))
42 #define E_RULE_CONTEXT_CLASS(cls) \
43 	(G_TYPE_CHECK_CLASS_CAST \
44 	((cls), E_TYPE_RULE_CONTEXT, ERuleContextClass))
45 #define E_IS_RULE_CONTEXT(obj) \
46 	(G_TYPE_CHECK_INSTANCE_TYPE \
47 	((obj), E_TYPE_RULE_CONTEXT))
48 #define E_IS_RULE_CONTEXT_CLASS(cls) \
49 	(G_TYPE_CHECK_CLASS_TYPE \
50 	((cls), E_TYPE_RULE_CONTEXT))
51 #define E_RULE_CONTEXT_GET_CLASS(obj) \
52 	(G_TYPE_INSTANCE_GET_CLASS \
53 	((obj), E_TYPE_RULE_CONTEXT, ERuleContextClass))
54 
55 G_BEGIN_DECLS
56 
57 typedef struct _ERuleContext ERuleContext;
58 typedef struct _ERuleContextClass ERuleContextClass;
59 typedef struct _ERuleContextPrivate ERuleContextPrivate;
60 
61 /* backend capabilities, this is a hack since we don't support nested rules */
62 enum {
63 	E_RULE_CONTEXT_GROUPING = 1 << 0,
64 	E_RULE_CONTEXT_THREADING = 1 << 1
65 };
66 
67 typedef void	(*ERuleContextRegisterFunc)	(ERuleContext *context,
68 						 EFilterRule *rule,
69 						 gpointer user_data);
70 typedef void	(*ERuleContextPartFunc)		(ERuleContext *context,
71 						 EFilterPart *part);
72 typedef void	(*ERuleContextRuleFunc)		(ERuleContext *context,
73 						 EFilterRule *part);
74 typedef EFilterPart *
75 		(*ERuleContextNextPartFunc)	(ERuleContext *context,
76 						 EFilterPart *part);
77 typedef EFilterRule *
78 		(*ERuleContextNextRuleFunc)	(ERuleContext *context,
79 						 EFilterRule *rule,
80 						 const gchar *source);
81 
82 struct _ERuleContext {
83 	GObject parent;
84 	ERuleContextPrivate *priv;
85 
86 	gchar *error;		/* string version of error */
87 
88 	guint32 flags;		/* capability flags */
89 
90 	GList *parts;
91 	GList *rules;
92 
93 	GHashTable *part_set_map; /* map set types to part types */
94 	GList *part_set_list;
95 	GHashTable *rule_set_map; /* map set types to rule types */
96 	GList *rule_set_list;
97 };
98 
99 struct _ERuleContextClass {
100 	GObjectClass parent_class;
101 
102 	/* methods */
103 	gint		(*load)			(ERuleContext *context,
104 						 const gchar *system,
105 						 const gchar *user);
106 	gint		(*save)			(ERuleContext *context,
107 						 const gchar *user);
108 	gint		(*revert)		(ERuleContext *context,
109 						 const gchar *user);
110 
111 	GList *		(*delete_uri)		(ERuleContext *context,
112 						 const gchar *uri,
113 						 GCompareFunc compare_func);
114 	GList *		(*rename_uri)		(ERuleContext *context,
115 						 const gchar *old_uri,
116 						 const gchar *new_uri,
117 						 GCompareFunc compare_func);
118 
119 	EFilterElement *(*new_element)		(ERuleContext *context,
120 						 const gchar *name);
121 
122 	/* signals */
123 	void		(*rule_added)		(ERuleContext *context,
124 						 EFilterRule *rule);
125 	void		(*rule_removed)		(ERuleContext *context,
126 						 EFilterRule *rule);
127 	void		(*changed)		(ERuleContext *context);
128 };
129 
130 struct _part_set_map {
131 	gchar *name;
132 	GType type;
133 	ERuleContextPartFunc append;
134 	ERuleContextNextPartFunc next;
135 };
136 
137 struct _rule_set_map {
138 	gchar *name;
139 	GType type;
140 	ERuleContextRuleFunc append;
141 	ERuleContextNextRuleFunc next;
142 };
143 
144 GType		e_rule_context_get_type		(void) G_GNUC_CONST;
145 ERuleContext *	e_rule_context_new		(void);
146 
147 gint		e_rule_context_load		(ERuleContext *context,
148 						 const gchar *system,
149 						 const gchar *user);
150 gint		e_rule_context_save		(ERuleContext *context,
151 						 const gchar *user);
152 gint		e_rule_context_revert		(ERuleContext *context,
153 						 const gchar *user);
154 
155 void		e_rule_context_add_part		(ERuleContext *context,
156 						 EFilterPart *part);
157 EFilterPart *	e_rule_context_find_part	(ERuleContext *context,
158 						 const gchar *name);
159 EFilterPart *	e_rule_context_create_part	(ERuleContext *context,
160 						 const gchar *name);
161 EFilterPart *	e_rule_context_next_part	(ERuleContext *context,
162 						 EFilterPart *last);
163 
164 EFilterRule *	e_rule_context_next_rule	(ERuleContext *context,
165 						 EFilterRule *last,
166 						 const gchar *source);
167 EFilterRule *	e_rule_context_find_rule	(ERuleContext *context,
168 						 const gchar *name,
169 						 const gchar *source);
170 EFilterRule *	e_rule_context_find_rank_rule	(ERuleContext *context,
171 						 gint rank,
172 						 const gchar *source);
173 void		e_rule_context_add_rule		(ERuleContext *context,
174 						 EFilterRule *rule);
175 void		e_rule_context_add_rule_gui	(ERuleContext *context,
176 						 EFilterRule *rule,
177 						 const gchar *title,
178 						 const gchar *path);
179 void		e_rule_context_remove_rule	(ERuleContext *context,
180 						 EFilterRule *rule);
181 
182 void		e_rule_context_rank_rule	(ERuleContext *context,
183 						 EFilterRule *rule,
184 						 const gchar *source,
185 						 gint rank);
186 gint		e_rule_context_get_rank_rule	(ERuleContext *context,
187 						 EFilterRule *rule,
188 						 const gchar *source);
189 
190 void		e_rule_context_add_part_set	(ERuleContext *context,
191 						 const gchar *setname,
192 						 GType part_type,
193 						 ERuleContextPartFunc append,
194 						 ERuleContextNextPartFunc next);
195 void		e_rule_context_add_rule_set	(ERuleContext *context,
196 						 const gchar *setname,
197 						 GType rule_type,
198 						 ERuleContextRuleFunc append,
199 						 ERuleContextNextRuleFunc next);
200 
201 EFilterElement *e_rule_context_new_element	(ERuleContext *context,
202 						 const gchar *name);
203 
204 GList *		e_rule_context_delete_uri	(ERuleContext *context,
205 						 const gchar *uri,
206 						 GCompareFunc compare);
207 GList *		e_rule_context_rename_uri	(ERuleContext *context,
208 						 const gchar *old_uri,
209 						 const gchar *new_uri,
210 						 GCompareFunc compare);
211 
212 void		e_rule_context_free_uri_list	(ERuleContext *context,
213 						 GList *uris);
214 
215 G_END_DECLS
216 
217 #endif /* E_RULE_CONTEXT_H */
218