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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
|
/*
* 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:
* Not Zed <notzed@lostzed.mmc.com.au>
* 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 <gtk/gtk.h>
#include <glade/glade.h>
#include <glib/gi18n.h>
#include "camel/camel-url.h"
#include "em-vfolder-context.h"
#include "em-vfolder-rule.h"
#include "mail/em-utils.h"
#include "mail/em-folder-tree.h"
#include "mail/em-folder-selector.h"
#include "mail/mail-component.h"
#include "e-util/e-error.h"
#include "e-util/e-util-private.h"
#define d(x)
static gint validate(FilterRule *);
static gint vfolder_eq(FilterRule *fr, FilterRule *cm);
static xmlNodePtr xml_encode(FilterRule *);
static gint xml_decode(FilterRule *, xmlNodePtr, RuleContext *f);
static void rule_copy(FilterRule *dest, FilterRule *src);
/*static void build_code(FilterRule *, GString *out);*/
static GtkWidget *get_widget(FilterRule *fr, RuleContext *f);
static void em_vfolder_rule_class_init(EMVFolderRuleClass *klass);
static void em_vfolder_rule_init(EMVFolderRule *vr);
static void em_vfolder_rule_finalise(GObject *obj);
/* DO NOT internationalise these strings */
static const gchar *with_names[] = {
"specific",
"local_remote_active",
"remote_active",
"local"
};
static FilterRuleClass *parent_class = NULL;
GType
em_vfolder_rule_get_type(void)
{
static GType type = 0;
if (!type) {
static const GTypeInfo info = {
sizeof(EMVFolderRuleClass),
NULL, /* base_class_init */
NULL, /* base_class_finalize */
(GClassInitFunc)em_vfolder_rule_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof(EMVFolderRule),
0, /* n_preallocs */
(GInstanceInitFunc)em_vfolder_rule_init,
};
type = g_type_register_static(FILTER_TYPE_RULE, "EMVFolderRule", &info, 0);
}
return type;
}
static void
em_vfolder_rule_class_init(EMVFolderRuleClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
FilterRuleClass *fr_class =(FilterRuleClass *)klass;
parent_class = g_type_class_ref(FILTER_TYPE_RULE);
object_class->finalize = em_vfolder_rule_finalise;
/* override methods */
fr_class->validate = validate;
fr_class->eq = vfolder_eq;
fr_class->xml_encode = xml_encode;
fr_class->xml_decode = xml_decode;
fr_class->copy = rule_copy;
/*fr_class->build_code = build_code;*/
fr_class->get_widget = get_widget;
}
static void
em_vfolder_rule_init(EMVFolderRule *vr)
{
vr->with = EM_VFOLDER_RULE_WITH_SPECIFIC;
vr->rule.source = g_strdup("incoming");
}
static void
em_vfolder_rule_finalise(GObject *obj)
{
EMVFolderRule *vr =(EMVFolderRule *)obj;
g_list_foreach(vr->sources, (GFunc)g_free, NULL);
g_list_free(vr->sources);
G_OBJECT_CLASS(parent_class)->finalize(obj);
}
/**
* em_vfolder_rule_new:
*
* Create a new EMVFolderRule object.
*
* Return value: A new #EMVFolderRule object.
**/
EMVFolderRule *
em_vfolder_rule_new(void)
{
return (EMVFolderRule *)g_object_new(em_vfolder_rule_get_type(), NULL, NULL);
}
void
em_vfolder_rule_add_source(EMVFolderRule *vr, const gchar *uri)
{
g_return_if_fail (EM_IS_VFOLDER_RULE(vr));
g_return_if_fail (uri);
vr->sources = g_list_append(vr->sources, g_strdup(uri));
filter_rule_emit_changed((FilterRule *)vr);
}
const gchar *
em_vfolder_rule_find_source(EMVFolderRule *vr, const gchar *uri)
{
GList *l;
g_return_val_if_fail (EM_IS_VFOLDER_RULE(vr), NULL);
/* only does a simple string or address comparison, should
probably do a decoded url comparison */
l = vr->sources;
while (l) {
if (l->data == uri || !strcmp(l->data, uri))
return l->data;
l = l->next;
}
return NULL;
}
void
em_vfolder_rule_remove_source(EMVFolderRule *vr, const gchar *uri)
{
gchar *found;
g_return_if_fail (EM_IS_VFOLDER_RULE(vr));
found =(gchar *)em_vfolder_rule_find_source(vr, uri);
if (found) {
vr->sources = g_list_remove(vr->sources, found);
g_free(found);
filter_rule_emit_changed((FilterRule *)vr);
}
}
const gchar *
em_vfolder_rule_next_source(EMVFolderRule *vr, const gchar *last)
{
GList *node;
if (last == NULL) {
node = vr->sources;
} else {
node = g_list_find(vr->sources, (gchar *)last);
if (node == NULL)
node = vr->sources;
else
node = g_list_next(node);
}
if (node)
return (const gchar *)node->data;
return NULL;
}
static gint
validate(FilterRule *fr)
{
g_return_val_if_fail(fr != NULL, 0);
if (!fr->name || !*fr->name) {
/* FIXME: set a parent window? */
e_error_run(NULL, "mail:no-name-vfolder", NULL);
return 0;
}
/* We have to have at least one source set in the "specific" case.
Do not translate this string! */
if (((EMVFolderRule *)fr)->with == EM_VFOLDER_RULE_WITH_SPECIFIC && ((EMVFolderRule *)fr)->sources == NULL) {
/* FIXME: set a parent window? */
e_error_run(NULL, "mail:vfolder-no-source", NULL);
return 0;
}
return FILTER_RULE_CLASS(parent_class)->validate(fr);
}
static gint
list_eq(GList *al, GList *bl)
{
gint truth = TRUE;
while (truth && al && bl) {
gchar *a = al->data, *b = bl->data;
truth = strcmp(a, b)== 0;
al = al->next;
bl = bl->next;
}
return truth && al == NULL && bl == NULL;
}
static gint
vfolder_eq(FilterRule *fr, FilterRule *cm)
{
return FILTER_RULE_CLASS(parent_class)->eq(fr, cm)
&& list_eq(((EMVFolderRule *)fr)->sources, ((EMVFolderRule *)cm)->sources);
}
static xmlNodePtr
xml_encode(FilterRule *fr)
{
EMVFolderRule *vr =(EMVFolderRule *)fr;
xmlNodePtr node, set, work;
GList *l;
node = FILTER_RULE_CLASS(parent_class)->xml_encode(fr);
g_return_val_if_fail (node != NULL, NULL);
g_return_val_if_fail (vr->with < sizeof(with_names)/sizeof(with_names[0]), NULL);
set = xmlNewNode(NULL, (const guchar *)"sources");
xmlAddChild(node, set);
xmlSetProp(set, (const guchar *)"with", (guchar *)with_names[vr->with]);
l = vr->sources;
while (l) {
work = xmlNewNode(NULL, (const guchar *)"folder");
xmlSetProp(work, (const guchar *)"uri", (guchar *)l->data);
xmlAddChild(set, work);
l = l->next;
}
return node;
}
static void
set_with(EMVFolderRule *vr, const gchar *name)
{
gint i;
for(i=0;i<sizeof(with_names)/sizeof(with_names[0]);i++) {
if (!strcmp(name, with_names[i])) {
vr->with = i;
return;
}
}
vr->with = 0;
}
static gint
xml_decode(FilterRule *fr, xmlNodePtr node, struct _RuleContext *f)
{
xmlNodePtr set, work;
gint result;
EMVFolderRule *vr =(EMVFolderRule *)fr;
gchar *tmp;
result = FILTER_RULE_CLASS(parent_class)->xml_decode(fr, node, f);
if (result != 0)
return result;
/* handle old format file, vfolder source is in filterrule */
if (strcmp(fr->source, "incoming")!= 0) {
set_with(vr, fr->source);
g_free(fr->source);
fr->source = g_strdup("incoming");
}
set = node->children;
while (set) {
if (!strcmp((gchar *)set->name, "sources")) {
tmp = (gchar *)xmlGetProp(set, (const guchar *)"with");
if (tmp) {
set_with(vr, tmp);
xmlFree(tmp);
}
work = set->children;
while (work) {
if (!strcmp((gchar *)work->name, "folder")) {
tmp = (gchar *)xmlGetProp(work, (const guchar *)"uri");
if (tmp) {
vr->sources = g_list_append(vr->sources, g_strdup(tmp));
xmlFree(tmp);
}
}
work = work->next;
}
}
set = set->next;
}
return 0;
}
static void
rule_copy(FilterRule *dest, FilterRule *src)
{
EMVFolderRule *vdest, *vsrc;
GList *node;
vdest =(EMVFolderRule *)dest;
vsrc =(EMVFolderRule *)src;
if (vdest->sources) {
g_list_foreach(vdest->sources, (GFunc)g_free, NULL);
g_list_free(vdest->sources);
vdest->sources = NULL;
}
node = vsrc->sources;
while (node) {
gchar *uri = node->data;
vdest->sources = g_list_append(vdest->sources, g_strdup(uri));
node = node->next;
}
vdest->with = vsrc->with;
FILTER_RULE_CLASS(parent_class)->copy(dest, src);
}
enum {
BUTTON_ADD,
BUTTON_REMOVE,
BUTTON_LAST
};
struct _source_data {
RuleContext *rc;
EMVFolderRule *vr;
const gchar *current;
GtkListStore *model;
GtkTreeView *list;
GtkWidget *source_selector;
GtkButton *buttons[BUTTON_LAST];
};
static void source_add(GtkWidget *widget, struct _source_data *data);
static void source_remove(GtkWidget *widget, struct _source_data *data);
static struct {
const gchar *name;
GCallback func;
} edit_buttons[] = {
{ "source_add", G_CALLBACK(source_add) },
{ "source_remove", G_CALLBACK(source_remove)},
};
static void
set_sensitive(struct _source_data *data)
{
gtk_widget_set_sensitive((GtkWidget *)data->buttons[BUTTON_ADD], TRUE);
gtk_widget_set_sensitive((GtkWidget *)data->buttons[BUTTON_REMOVE], data->current != NULL);
}
static void
select_source(GtkWidget *list, struct _source_data *data)
{
GtkTreeViewColumn *column;
GtkTreePath *path;
GtkTreeIter iter;
gtk_tree_view_get_cursor(data->list, &path, &column);
gtk_tree_model_get_iter(GTK_TREE_MODEL(data->model), &iter, path);
gtk_tree_path_free(path);
gtk_tree_model_get(GTK_TREE_MODEL(data->model), &iter, 0, &data->current, -1);
set_sensitive(data);
}
static void
select_source_with_changed(GtkWidget *widget, struct _source_data *data)
{
em_vfolder_rule_with_t with = 0;
GSList *group = NULL;
gint i = 0;
if ( !gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)) )
return;
group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget));
for (i=0; i< g_slist_length(group); i++) {
if ( g_slist_nth_data (group, with = i) == widget )
break;
}
if (with > EM_VFOLDER_RULE_WITH_LOCAL )
with = 0;
gtk_widget_set_sensitive (data->source_selector, !with );
data->vr->with = with;
}
/* attempt to make a 'nice' folder name out of the raw uri */
static gchar *format_source(const gchar *euri)
{
CamelURL *url;
GString *out;
gchar *res, *uri;
/* This should really probably base it on the account name? */
uri = em_uri_to_camel(euri);
url = camel_url_new(uri, NULL);
/* bad uri */
if (url == NULL)
return uri;
g_free(uri);
out = g_string_new(url->protocol);
g_string_append_c(out, ':');
if (url->user && url->host) {
g_string_append_printf(out, "%s@%s", url->user, url->host);
if (url->port)
g_string_append_printf(out, ":%d", url->port);
}
if (url->fragment)
g_string_append(out, url->fragment);
else if (url->path)
g_string_append(out, url->path);
res = out->str;
g_string_free(out, FALSE);
return res;
}
static void
vfr_folder_response(GtkWidget *dialog, gint button, struct _source_data *data)
{
const gchar *uri = em_folder_selector_get_selected_uri((EMFolderSelector *)dialog);
if (button == GTK_RESPONSE_OK && uri != NULL) {
gchar *urinice, *euri;
GtkTreeSelection *selection;
GtkTreeIter iter;
euri = em_uri_from_camel(uri);
data->vr->sources = g_list_append(data->vr->sources, euri);
gtk_list_store_append(data->model, &iter);
urinice = format_source(euri);
gtk_list_store_set(data->model, &iter, 0, urinice, 1, euri, -1);
g_free(urinice);
selection = gtk_tree_view_get_selection(data->list);
gtk_tree_selection_select_iter(selection, &iter);
data->current = euri;
set_sensitive(data);
}
gtk_widget_destroy(dialog);
}
static void
source_add(GtkWidget *widget, struct _source_data *data)
{
EMFolderTree *emft;
GtkWidget *dialog;
emft =(EMFolderTree *)em_folder_tree_new_with_model(mail_component_peek_tree_model(mail_component_peek()));
em_folder_tree_set_excluded(emft, EMFT_EXCLUDE_NOSELECT);
dialog = em_folder_selector_new(emft, EM_FOLDER_SELECTOR_CAN_CREATE, _("Select Folder"), NULL, _("_Add"));
gtk_window_set_transient_for((GtkWindow *)dialog, (GtkWindow *)gtk_widget_get_toplevel(widget));
gtk_window_set_modal((GtkWindow *)dialog, TRUE);
g_signal_connect(dialog, "response", G_CALLBACK(vfr_folder_response), data);
gtk_widget_show(dialog);
}
static void
source_remove(GtkWidget *widget, struct _source_data *data)
{
GtkTreeSelection *selection;
const gchar *source;
GtkTreePath *path;
GtkTreeIter iter;
gint index = 0;
gint n;
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(data->list));
source = NULL;
while ((source = em_vfolder_rule_next_source(data->vr, source))) {
path = gtk_tree_path_new();
gtk_tree_path_append_index(path, index);
if (gtk_tree_selection_path_is_selected(selection, path)) {
gtk_tree_model_get_iter(GTK_TREE_MODEL(data->model), &iter, path);
em_vfolder_rule_remove_source(data->vr, source);
gtk_list_store_remove(data->model, &iter);
gtk_tree_path_free(path);
/* now select the next rule */
n = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(data->model), NULL);
index = index >= n ? n - 1 : index;
if (index >= 0) {
path = gtk_tree_path_new();
gtk_tree_path_append_index(path, index);
gtk_tree_model_get_iter(GTK_TREE_MODEL(data->model), &iter, path);
gtk_tree_path_free(path);
gtk_tree_selection_select_iter(selection, &iter);
gtk_tree_model_get(GTK_TREE_MODEL(data->model), &iter, 0, &data->current, -1);
} else {
data->current = NULL;
}
break;
}
index++;
gtk_tree_path_free(path);
}
set_sensitive(data);
}
GtkWidget *em_vfolder_editor_sourcelist_new(gchar *widget_name, gchar *string1, gchar *string2,
gint int1, gint int2);
GtkWidget *
em_vfolder_editor_sourcelist_new(gchar *widget_name, gchar *string1, gchar *string2, gint int1, gint int2)
{
GtkWidget *table, *scrolled;
GtkTreeSelection *selection;
GtkCellRenderer *renderer;
GtkListStore *model;
scrolled = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
table = gtk_tree_view_new_with_model((GtkTreeModel *)model);
gtk_tree_view_set_headers_visible((GtkTreeView *)table, FALSE);
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes((GtkTreeView *)table, -1,
_("Search Folder source"), renderer,
"text", 0, NULL);
selection = gtk_tree_view_get_selection((GtkTreeView *)table);
gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
gtk_container_add(GTK_CONTAINER(scrolled), table);
g_object_set_data((GObject *)scrolled, "table", table);
g_object_set_data((GObject *)scrolled, "model", model);
gtk_widget_show(scrolled);
gtk_widget_show(table);
g_object_unref (model);
return scrolled;
}
static GtkWidget *
get_widget(FilterRule *fr, RuleContext *rc)
{
EMVFolderRule *vr =(EMVFolderRule *)fr;
GtkWidget *widget, *frame, *list;
struct _source_data *data;
GtkRadioButton *rb;
const gchar *source;
GtkTreeIter iter;
GladeXML *gui;
gint i;
gchar *gladefile;
widget = FILTER_RULE_CLASS(parent_class)->get_widget(fr, rc);
data = g_malloc0(sizeof(*data));
data->rc = rc;
data->vr = vr;
gladefile = g_build_filename (EVOLUTION_GLADEDIR,
"mail-dialogs.glade",
NULL);
gui = glade_xml_new(gladefile, "vfolder_source_frame", NULL);
g_free (gladefile);
frame = glade_xml_get_widget(gui, "vfolder_source_frame");
g_object_set_data_full((GObject *)frame, "data", data, g_free);
for(i = 0; i < BUTTON_LAST; i++) {
data->buttons[i] =(GtkButton *)glade_xml_get_widget(gui, edit_buttons[i].name);
g_signal_connect(data->buttons[i], "clicked", edit_buttons[i].func, data);
}
list = glade_xml_get_widget(gui, "source_list");
data->list =(GtkTreeView *)g_object_get_data((GObject *)list, "table");
data->model =(GtkListStore *)g_object_get_data((GObject *)list, "model");
source = NULL;
while ((source = em_vfolder_rule_next_source(vr, source))) {
gchar *nice = format_source(source);
gtk_list_store_append(data->model, &iter);
gtk_list_store_set(data->model, &iter, 0, nice, 1, source, -1);
g_free(nice);
}
g_signal_connect(data->list, "cursor-changed", G_CALLBACK(select_source), data);
rb = (GtkRadioButton *)glade_xml_get_widget (gui, "local_rb");
g_signal_connect (GTK_WIDGET(rb), "toggled", G_CALLBACK(select_source_with_changed), data);
rb = (GtkRadioButton *)glade_xml_get_widget (gui, "remote_rb");
g_signal_connect (GTK_WIDGET(rb), "toggled", G_CALLBACK(select_source_with_changed), data);
rb = (GtkRadioButton *)glade_xml_get_widget (gui, "local_and_remote_rb");
g_signal_connect (GTK_WIDGET(rb), "toggled", G_CALLBACK(select_source_with_changed), data);
rb = (GtkRadioButton *)glade_xml_get_widget (gui, "specific_rb");
g_signal_connect (GTK_WIDGET(rb), "toggled", G_CALLBACK(select_source_with_changed), data);
data->source_selector = (GtkWidget *)glade_xml_get_widget (gui, "source_selector");
rb = g_slist_nth_data(gtk_radio_button_get_group (rb), vr->with);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rb), TRUE);
g_signal_emit_by_name (rb, "toggled");
set_sensitive(data);
g_object_unref(gui);
gtk_box_pack_start(GTK_BOX(widget), frame, TRUE, TRUE, 3);
return widget;
}
|