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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see <http://www.gnu.org/licenses/>
*
*
* Authors:
* Jon Trowbridge <trow@ximian.com>
* Jeffrey Stedfast <fejj@ximian.com>
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include "em-filter-source-element.h"
#include <gtk/gtk.h>
#include <camel/camel.h>
#include <libedataserver/e-sexp.h>
#include <libedataserver/e-account-list.h>
#include "filter/e-filter-part.h"
/* backward-compatibility cruft */
#include "e-util/gtk-compat.h"
static void em_filter_source_element_class_init (EMFilterSourceElementClass *klass);
static void em_filter_source_element_init (EMFilterSourceElement *fs);
static void em_filter_source_element_finalize (GObject *obj);
static gint source_eq (EFilterElement *fe, EFilterElement *cm);
static void xml_create (EFilterElement *fe, xmlNodePtr node);
static xmlNodePtr xml_encode (EFilterElement *fe);
static gint xml_decode (EFilterElement *fe, xmlNodePtr node);
static EFilterElement *filter_clone (EFilterElement *fe);
static GtkWidget *get_widget (EFilterElement *fe);
static void build_code (EFilterElement *fe, GString *out, EFilterPart *ff);
static void format_sexp (EFilterElement *, GString *);
static void em_filter_source_element_add_source (EMFilterSourceElement *fs, const gchar *account_name, const gchar *name,
const gchar *addr, const gchar *url);
static void em_filter_source_element_get_sources (EMFilterSourceElement *fs);
typedef struct _SourceInfo {
gchar *account_name;
gchar *name;
gchar *address;
gchar *url;
} SourceInfo;
struct _EMFilterSourceElementPrivate {
GList *sources;
gchar *current_url;
};
static EFilterElementClass *parent_class = NULL;
GType
em_filter_source_element_get_type (void)
{
static GType type = 0;
if (!type) {
static const GTypeInfo info = {
sizeof (EMFilterSourceElementClass),
NULL, /* base_class_init */
NULL, /* base_class_finalize */
(GClassInitFunc)em_filter_source_element_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (EMFilterSourceElement),
0, /* n_preallocs */
(GInstanceInitFunc)em_filter_source_element_init,
};
type = g_type_register_static(E_TYPE_FILTER_ELEMENT, "EMFilterSourceElement", &info, 0);
}
return type;
}
static void
em_filter_source_element_class_init (EMFilterSourceElementClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
EFilterElementClass *fe_class = E_FILTER_ELEMENT_CLASS (klass);
parent_class = g_type_class_ref (E_TYPE_FILTER_ELEMENT);
object_class->finalize = em_filter_source_element_finalize;
/* override methods */
fe_class->eq = source_eq;
fe_class->xml_create = xml_create;
fe_class->xml_encode = xml_encode;
fe_class->xml_decode = xml_decode;
fe_class->clone = filter_clone;
fe_class->get_widget = get_widget;
fe_class->build_code = build_code;
fe_class->format_sexp = format_sexp;
}
static void
em_filter_source_element_init (EMFilterSourceElement *fs)
{
fs->priv = g_new (struct _EMFilterSourceElementPrivate, 1);
fs->priv->sources = NULL;
fs->priv->current_url = NULL;
}
static void
em_filter_source_element_finalize (GObject *obj)
{
EMFilterSourceElement *fs = (EMFilterSourceElement *)obj;
GList *i = fs->priv->sources;
while (i) {
SourceInfo *info = i->data;
g_free (info->account_name);
g_free (info->name);
g_free (info->address);
g_free (info->url);
g_free (info);
i = g_list_next (i);
}
g_list_free (fs->priv->sources);
g_free (fs->priv->current_url);
g_free (fs->priv);
G_OBJECT_CLASS (parent_class)->finalize (obj);
}
EFilterElement *
em_filter_source_element_new (void)
{
return g_object_new (em_filter_source_element_get_type (), NULL, NULL);
}
static gint
source_eq (EFilterElement *fe, EFilterElement *cm)
{
EMFilterSourceElement *fs = (EMFilterSourceElement *)fe, *cs = (EMFilterSourceElement *)cm;
return E_FILTER_ELEMENT_CLASS (parent_class)->eq (fe, cm)
&&((fs->priv->current_url && cs->priv->current_url
&& strcmp (fs->priv->current_url, cs->priv->current_url)== 0)
||(fs->priv->current_url == NULL && cs->priv->current_url == NULL));
}
static void
xml_create (EFilterElement *fe, xmlNodePtr node)
{
/* Call parent implementation */
E_FILTER_ELEMENT_CLASS (parent_class)->xml_create (fe, node);
}
static xmlNodePtr
xml_encode (EFilterElement *fe)
{
xmlNodePtr value;
EMFilterSourceElement *fs = (EMFilterSourceElement *)fe;
value = xmlNewNode(NULL, (const guchar *)"value");
xmlSetProp(value, (const guchar *)"name", (guchar *)fe->name);
xmlSetProp(value, (const guchar *)"type", (const guchar *)"uri");
if (fs->priv->current_url)
xmlNewTextChild(value, NULL, (const guchar *)"uri", (guchar *)fs->priv->current_url);
return value;
}
static gint
xml_decode (EFilterElement *fe, xmlNodePtr node)
{
EMFilterSourceElement *fs = (EMFilterSourceElement *)fe;
CamelURL *url;
gchar *uri;
node = node->children;
while (node != NULL) {
if (!strcmp((gchar *)node->name, "uri")) {
uri = (gchar *)xmlNodeGetContent (node);
url = camel_url_new (uri, NULL);
xmlFree (uri);
g_free (fs->priv->current_url);
fs->priv->current_url = camel_url_to_string (url, CAMEL_URL_HIDE_ALL);
camel_url_free (url);
break;
}
node = node->next;
}
return 0;
}
static EFilterElement *
filter_clone (EFilterElement *fe)
{
EMFilterSourceElement *fs = (EMFilterSourceElement *)fe;
EMFilterSourceElement *cpy;
GList *i;
cpy = (EMFilterSourceElement *) em_filter_source_element_new ();
((EFilterElement *)cpy)->name = (gchar *)xmlStrdup ((guchar *)fe->name);
cpy->priv->current_url = g_strdup (fs->priv->current_url);
for (i = fs->priv->sources; i != NULL; i = g_list_next (i)) {
SourceInfo *info = (SourceInfo *)i->data;
em_filter_source_element_add_source (cpy, info->account_name, info->name, info->address, info->url);
}
return (EFilterElement *)cpy;
}
static void
source_changed (GtkComboBox *combobox, EMFilterSourceElement *fs)
{
SourceInfo *info;
gint idx;
idx = gtk_combo_box_get_active (combobox);
g_return_if_fail (idx >= 0 && idx < g_list_length (fs->priv->sources));
info = (SourceInfo *) g_list_nth_data (fs->priv->sources, idx);
g_return_if_fail (info != NULL);
g_free (fs->priv->current_url);
fs->priv->current_url = g_strdup (info->url);
}
static GtkWidget *
get_widget (EFilterElement *fe)
{
EMFilterSourceElement *fs = (EMFilterSourceElement *)fe;
GtkWidget *combobox;
GList *i;
SourceInfo *first = NULL;
gint index, current_index;
if (fs->priv->sources == NULL)
em_filter_source_element_get_sources (fs);
combobox = gtk_combo_box_text_new ();
index = 0;
current_index = -1;
for (i = fs->priv->sources; i != NULL; i = g_list_next (i)) {
SourceInfo *info = (SourceInfo *)i->data;
gchar *label;
if (info->url != NULL) {
if (first == NULL)
first = info;
if (info->account_name && strcmp (info->account_name, info->address))
label = g_strdup_printf("%s <%s>(%s)", info->name,
info->address, info->account_name);
else
label = g_strdup_printf("%s <%s>", info->name, info->address);
gtk_combo_box_text_append_text (
GTK_COMBO_BOX_TEXT (combobox), label);
g_free (label);
if (fs->priv->current_url && !strcmp (info->url, fs->priv->current_url))
current_index = index;
index++;
}
}
if (current_index >= 0) {
gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), current_index);
} else {
gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), 0);
g_free (fs->priv->current_url);
if (first)
fs->priv->current_url = g_strdup (first->url);
else
fs->priv->current_url = NULL;
}
g_signal_connect (combobox, "changed", G_CALLBACK (source_changed), fs);
return combobox;
}
static void
build_code (EFilterElement *fe, GString *out, EFilterPart *ff)
{
/* We are doing nothing on purpose. */
}
static void
format_sexp (EFilterElement *fe, GString *out)
{
EMFilterSourceElement *fs = (EMFilterSourceElement *)fe;
e_sexp_encode_string (out, fs->priv->current_url);
}
static void
em_filter_source_element_add_source (EMFilterSourceElement *fs, const gchar *account_name, const gchar *name,
const gchar *addr, const gchar *url)
{
SourceInfo *info;
g_return_if_fail (EM_IS_FILTER_SOURCE_ELEMENT (fs));
info = g_new0 (SourceInfo, 1);
info->account_name = g_strdup (account_name);
info->name = g_strdup (name);
info->address = g_strdup (addr);
info->url = g_strdup (url);
fs->priv->sources = g_list_append (fs->priv->sources, info);
}
static void
em_filter_source_element_get_sources (EMFilterSourceElement *fs)
{
EAccountList *accounts;
const EAccount *account;
GConfClient *gconf;
EIterator *it;
gchar *uri;
CamelURL *url;
/* should this get the global object from mail? */
gconf = gconf_client_get_default ();
accounts = e_account_list_new (gconf);
g_object_unref (gconf);
for (it = e_list_get_iterator ((EList *)accounts);
e_iterator_is_valid (it);
e_iterator_next (it)) {
account = (const EAccount *)e_iterator_get (it);
if (account->source == NULL || account->source->url == NULL || account->source->url[0] == 0)
continue;
url = camel_url_new (account->source->url, NULL);
if (url) {
/* hide secret stuff */
uri = camel_url_to_string (url, CAMEL_URL_HIDE_ALL);
camel_url_free (url);
em_filter_source_element_add_source (fs, account->name, account->id->name, account->id->address, uri);
g_free (uri);
}
}
g_object_unref (it);
g_object_unref (accounts);
}
|