5354525b18ff12fe00553c67f9b4533d8450ec8b
[claws.git] / src / prefs_folder_item.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 /* alfons - all folder item specific settings should migrate into 
21  * folderlist.xml!!! the old folderitemrc file will only serve for a few 
22  * versions (for compatibility) */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "defs.h"
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include "folder.h"
33 #include "prefs_folder_item.h"
34 #include "folderview.h"
35 #include "folder.h"
36 #include "summaryview.h"
37 #include "menu.h"
38 #include "account.h"
39 #include "prefs_gtk.h"
40 #include "manage_window.h"
41 #include "utils.h"
42 #include "addr_compl.h"
43 #include "prefs_common.h"
44 #include "gtkutils.h"
45 #include "filtering.h"
46 #include "folder_item_prefs.h"
47 #include "gtk/colorsel.h"
48 #include "string_match.h"
49
50 #if USE_ASPELL
51 #include "gtkaspell.h"
52 #endif
53
54 #define ASSIGN_STRING(string, value) \
55         { \
56                 g_free(string); \
57                 string = (value); \
58         }
59
60 typedef struct _FolderItemGeneralPage FolderItemGeneralPage;
61 typedef struct _FolderItemComposePage FolderItemComposePage;
62
63 struct _FolderItemGeneralPage
64 {
65         PrefsPage page;
66
67         FolderItem *item;
68
69         GtkWidget *table;
70         GtkWidget *folder_type;
71         GtkWidget *checkbtn_simplify_subject;
72         GtkWidget *entry_simplify_subject;
73         GtkWidget *entry_regexp_test_string;
74         GtkWidget *entry_regexp_test_result;
75         GtkWidget *checkbtn_folder_chmod;
76         GtkWidget *entry_folder_chmod;
77         GtkWidget *folder_color_btn;
78         GtkWidget *checkbtn_enable_processing;
79         GtkWidget *checkbtn_newmailcheck;
80         GtkWidget *checkbtn_offlinesync;
81
82         /* apply to sub folders */
83         GtkWidget *simplify_subject_rec_checkbtn;
84         GtkWidget *folder_chmod_rec_checkbtn;
85         GtkWidget *folder_color_rec_checkbtn;
86         GtkWidget *enable_processing_rec_checkbtn;
87         GtkWidget *newmailcheck_rec_checkbtn;
88         GtkWidget *offlinesync_rec_checkbtn;
89
90         gint       folder_color;
91 };
92
93 struct _FolderItemComposePage
94 {
95         PrefsPage page;
96
97         FolderItem *item;
98
99         GtkWidget *window;
100         GtkWidget *table;
101         GtkWidget *checkbtn_request_return_receipt;
102         GtkWidget *checkbtn_save_copy_to_folder;
103         GtkWidget *checkbtn_default_to;
104         GtkWidget *entry_default_to;
105         GtkWidget *checkbtn_default_reply_to;
106         GtkWidget *entry_default_reply_to;
107         GtkWidget *checkbtn_enable_default_account;
108         GtkWidget *optmenu_default_account;
109 #if USE_ASPELL
110         GtkWidget *checkbtn_enable_default_dictionary;
111         GtkWidget *optmenu_default_dictionary;
112 #endif
113
114         /* apply to sub folders */
115         GtkWidget *request_return_receipt_rec_checkbtn;
116         GtkWidget *save_copy_to_folder_rec_checkbtn;
117         GtkWidget *default_to_rec_checkbtn;
118         GtkWidget *default_reply_to_rec_checkbtn;
119         GtkWidget *default_account_rec_checkbtn;
120 #if USE_ASPELL
121         GtkWidget *default_dictionary_rec_checkbtn;
122 #endif
123 };
124
125
126 static void general_save_folder_prefs(FolderItem *folder, FolderItemGeneralPage *page);
127 static void compose_save_folder_prefs(FolderItem *folder, FolderItemComposePage *page);
128
129 static gboolean general_save_recurse_func(GNode *node, gpointer data);
130 static gboolean compose_save_recurse_func(GNode *node, gpointer data);
131
132 gint prefs_folder_item_chmod_mode               (gchar *folder_chmod);
133
134 static void folder_color_set_dialog(GtkWidget *widget, gpointer data);
135 static void folder_regexp_test_cb(GtkWidget *widget, gpointer data);
136 static void folder_regexp_set_subject_example_cb(GtkWidget *widget, gpointer data);
137
138 #define SAFE_STRING(str) \
139         (str) ? (str) : ""
140
141 void prefs_folder_item_general_create_widget_func(PrefsPage * page_,
142                                                    GtkWindow * window,
143                                                    gpointer data)
144 {
145         FolderItemGeneralPage *page = (FolderItemGeneralPage *) page_;
146         FolderItem *item = (FolderItem *) data;
147         guint rowcount;
148
149
150         GtkWidget *table;
151         GtkWidget *hbox;
152         GtkWidget *label;
153         
154         GtkWidget *folder_type_menu;
155         GtkWidget *folder_type;
156         GtkWidget *dummy_chkbtn;
157         GtkWidget *menuitem;
158         SpecialFolderItemType type;
159         
160         GtkWidget *checkbtn_simplify_subject;
161         GtkWidget *entry_simplify_subject;
162         GtkWidget *hbox_regexp;
163         GtkWidget *label_regexp_test;
164         GtkWidget *entry_regexp_test_string;
165         GtkWidget *left_arrow;
166         GtkWidget *entry_regexp_test_result;
167
168         GtkWidget *checkbtn_folder_chmod;
169         GtkWidget *entry_folder_chmod;
170         GtkWidget *folder_color;
171         GtkWidget *folder_color_btn;
172         GtkWidget *checkbtn_enable_processing;
173         GtkWidget *checkbtn_newmailcheck;
174         GtkWidget *checkbtn_offlinesync;
175
176         GtkWidget *simplify_subject_rec_checkbtn;
177         GtkWidget *folder_chmod_rec_checkbtn;
178         GtkWidget *folder_color_rec_checkbtn;
179         GtkWidget *enable_processing_rec_checkbtn;
180         GtkWidget *newmailcheck_rec_checkbtn;
181         GtkWidget *offlinesync_rec_checkbtn;
182         GtkTooltips *tooltips;
183
184         page->item         = item;
185
186         /* Table */
187         table = gtk_table_new(6, 2, FALSE);
188         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
189         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
190         rowcount = 0;
191
192         /* Apply to subfolders */
193         label = gtk_label_new(_("Apply to\nsubfolders"));
194         gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0.5);
195         gtk_table_attach(GTK_TABLE(table), label, 3, 4,
196                          rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
197         rowcount++;
198
199         /* folder_type */
200         folder_type = gtk_option_menu_new ();
201         gtk_widget_show (folder_type);
202
203         type = F_NORMAL;
204         if (item->stype == F_INBOX)
205                 type = F_INBOX;
206         else if (folder_has_parent_of_type(item, F_OUTBOX))
207                 type = F_OUTBOX;
208         else if (folder_has_parent_of_type(item, F_DRAFT))
209                 type = F_DRAFT;
210         else if (folder_has_parent_of_type(item, F_QUEUE))
211                 type = F_QUEUE;
212         else if (folder_has_parent_of_type(item, F_TRASH))
213                 type = F_TRASH;
214
215         folder_type_menu = gtk_menu_new ();
216
217         MENUITEM_ADD (folder_type_menu, menuitem, _("Normal"),  F_NORMAL);
218         MENUITEM_ADD (folder_type_menu, menuitem, _("Inbox"),  F_INBOX);
219         MENUITEM_ADD (folder_type_menu, menuitem, _("Outbox"),  F_OUTBOX);
220         MENUITEM_ADD (folder_type_menu, menuitem, _("Drafts"),  F_DRAFT);
221         MENUITEM_ADD (folder_type_menu, menuitem, _("Queue"),  F_QUEUE);
222         MENUITEM_ADD (folder_type_menu, menuitem, _("Trash"),  F_TRASH);
223         gtk_option_menu_set_menu (GTK_OPTION_MENU (folder_type), folder_type_menu);
224
225         gtk_option_menu_set_history(GTK_OPTION_MENU(folder_type), type);
226
227         dummy_chkbtn = gtk_check_button_new();
228         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dummy_chkbtn), type != F_INBOX);
229         gtk_widget_set_sensitive(dummy_chkbtn, FALSE);
230
231         if (type == item->stype && type == F_NORMAL)
232                 gtk_widget_set_sensitive(folder_type, TRUE);
233         else
234                 gtk_widget_set_sensitive(folder_type, FALSE);
235
236         label = gtk_label_new(_("Folder type"));
237         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
238         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 
239                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_FILL, 0, 0);
240         gtk_table_attach(GTK_TABLE(table), folder_type, 1, 2, 
241                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_FILL, 0, 0);
242         gtk_table_attach(GTK_TABLE(table), dummy_chkbtn, 3, 4, 
243                          rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
244
245         rowcount++;
246
247         /* Simplify Subject */
248         checkbtn_simplify_subject = gtk_check_button_new_with_label(_("Simplify Subject RegExp"));
249         gtk_table_attach(GTK_TABLE(table), checkbtn_simplify_subject, 0, 1, 
250                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_FILL, 0, 0);
251         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_simplify_subject), 
252                                      item->prefs->enable_simplify_subject);
253
254         g_signal_connect(G_OBJECT(checkbtn_simplify_subject), "toggled",
255                         G_CALLBACK(folder_regexp_set_subject_example_cb), page);
256
257         entry_simplify_subject = gtk_entry_new();
258         gtk_table_attach(GTK_TABLE(table), entry_simplify_subject, 1, 3, 
259                          rowcount, rowcount + 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
260         SET_TOGGLE_SENSITIVITY(checkbtn_simplify_subject, entry_simplify_subject);
261         gtk_entry_set_text(GTK_ENTRY(entry_simplify_subject), 
262                            SAFE_STRING(item->prefs->simplify_subject_regexp));
263
264         g_signal_connect(G_OBJECT(entry_simplify_subject), "changed",
265                         G_CALLBACK(folder_regexp_test_cb), page);
266
267         simplify_subject_rec_checkbtn = gtk_check_button_new();
268         gtk_table_attach(GTK_TABLE(table), simplify_subject_rec_checkbtn, 3, 4, 
269                          rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
270
271         rowcount++;
272
273         /* Test RegExp */
274         label_regexp_test = gtk_label_new(_("Test RegExp"));
275         gtk_misc_set_alignment(GTK_MISC(label_regexp_test), 0, 0.5);
276         gtk_table_attach(GTK_TABLE(table), label_regexp_test, 0, 1, 
277                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_FILL, 0, 0);
278         SET_TOGGLE_SENSITIVITY(checkbtn_simplify_subject, label_regexp_test);
279
280         entry_regexp_test_string = gtk_entry_new();
281         gtk_table_attach(GTK_TABLE(table), entry_regexp_test_string, 1, 2, 
282                          rowcount, rowcount + 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
283         SET_TOGGLE_SENSITIVITY(checkbtn_simplify_subject, entry_regexp_test_string);
284
285         g_signal_connect(G_OBJECT(entry_regexp_test_string), "changed",
286                         G_CALLBACK(folder_regexp_test_cb), page);
287
288         hbox_regexp = gtk_hbox_new (FALSE, 4);
289         gtk_widget_show (hbox_regexp);
290         gtk_table_attach(GTK_TABLE(table), hbox_regexp, 2, 3, 
291                          rowcount, rowcount + 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
292
293         left_arrow = gtk_arrow_new(GTK_ARROW_RIGHT, GTK_SHADOW_OUT);
294         gtk_widget_show(left_arrow);
295         gtk_box_pack_start (GTK_BOX(hbox_regexp), left_arrow, FALSE, FALSE, 0);
296         SET_TOGGLE_SENSITIVITY(checkbtn_simplify_subject, left_arrow);
297
298         entry_regexp_test_result = gtk_entry_new();
299         gtk_box_pack_end (GTK_BOX(hbox_regexp), entry_regexp_test_result, TRUE, TRUE, 0);
300         SET_TOGGLE_SENSITIVITY(checkbtn_simplify_subject, entry_regexp_test_result);
301         gtk_entry_set_editable(GTK_ENTRY(entry_regexp_test_result), FALSE);
302
303         rowcount++;
304
305         /* Folder chmod */
306         checkbtn_folder_chmod = gtk_check_button_new_with_label(_("Folder chmod"));
307         gtk_table_attach(GTK_TABLE(table), checkbtn_folder_chmod, 0, 1, 
308                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_FILL, 0, 0);
309
310         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_folder_chmod), 
311                                      item->prefs->enable_folder_chmod);
312
313         entry_folder_chmod = gtk_entry_new();
314         gtk_table_attach(GTK_TABLE(table), entry_folder_chmod, 1, 2, 
315                          rowcount, rowcount + 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
316         SET_TOGGLE_SENSITIVITY(checkbtn_folder_chmod, entry_folder_chmod);
317         if (item->prefs->folder_chmod) {
318                 gchar *buf;
319
320                 buf = g_strdup_printf("%o", item->prefs->folder_chmod);
321                 gtk_entry_set_text(GTK_ENTRY(entry_folder_chmod), buf);
322                 g_free(buf);
323         }
324         
325         folder_chmod_rec_checkbtn = gtk_check_button_new();
326         gtk_table_attach(GTK_TABLE(table), folder_chmod_rec_checkbtn, 3, 4, 
327                          rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
328
329         rowcount++;
330         
331         /* Folder color */
332         folder_color = gtk_label_new(_("Folder color"));
333         gtk_misc_set_alignment(GTK_MISC(folder_color), 0, 0.5);
334         gtk_table_attach(GTK_TABLE(table), folder_color, 0, 1, 
335                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_FILL, 0, 0);
336
337         hbox = gtk_hbox_new(FALSE, 0);
338         gtk_table_attach(GTK_TABLE(table), hbox, 1, 2, 
339                          rowcount, rowcount + 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
340
341         folder_color_btn = gtk_button_new_with_label("");
342         gtk_widget_set_size_request(folder_color_btn, 36, 26);
343         gtk_box_pack_start (GTK_BOX(hbox), folder_color_btn, FALSE, FALSE, 0);
344         tooltips = gtk_tooltips_new();
345         gtk_tooltips_set_tip(tooltips, folder_color_btn,
346                              _("Pick color for folder"), NULL);
347
348         page->folder_color = item->prefs->color;
349
350         g_signal_connect(G_OBJECT(folder_color_btn), "clicked",
351                          G_CALLBACK(folder_color_set_dialog),
352                          page);
353
354         gtkut_set_widget_bgcolor_rgb(folder_color_btn, item->prefs->color);
355
356         folder_color_rec_checkbtn = gtk_check_button_new();
357         gtk_table_attach(GTK_TABLE(table), folder_color_rec_checkbtn, 3, 4, 
358                          rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
359
360         rowcount++;
361
362         /* Enable processing at startup */
363         checkbtn_enable_processing = gtk_check_button_new_with_label(_("Process at startup"));
364         gtk_table_attach(GTK_TABLE(table), checkbtn_enable_processing, 0, 2, 
365                          rowcount, rowcount + 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
366
367         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_enable_processing), 
368                                      item->prefs->enable_processing);
369
370         enable_processing_rec_checkbtn = gtk_check_button_new();
371         gtk_table_attach(GTK_TABLE(table), enable_processing_rec_checkbtn, 3, 4, 
372                          rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
373         
374         rowcount++;
375
376         /* Check folder for new mail */
377         checkbtn_newmailcheck = gtk_check_button_new_with_label(_("Scan for new mail"));
378         gtk_table_attach(GTK_TABLE(table), checkbtn_newmailcheck, 0, 2,
379                          rowcount, rowcount+1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
380         
381         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_newmailcheck),
382                                                                  item->prefs->newmailcheck);
383         newmailcheck_rec_checkbtn = gtk_check_button_new();
384         gtk_table_attach(GTK_TABLE(table), newmailcheck_rec_checkbtn, 3, 4, 
385                          rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
386
387         rowcount++;
388
389         /* Synchronise folder for offline use */
390         checkbtn_offlinesync = gtk_check_button_new_with_label(_("Synchronise for offline use"));
391         gtk_table_attach(GTK_TABLE(table), checkbtn_offlinesync, 0, 2,
392                          rowcount, rowcount+1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
393         
394         offlinesync_rec_checkbtn = gtk_check_button_new();
395         gtk_table_attach(GTK_TABLE(table), offlinesync_rec_checkbtn, 3, 4, 
396                          rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
397
398         if (item->folder && (item->folder->klass->type != F_IMAP && 
399             item->folder->klass->type != F_NEWS)) {
400                  item->prefs->offlinesync = TRUE;
401                 gtk_widget_set_sensitive(GTK_WIDGET(checkbtn_offlinesync),
402                                                                  FALSE);
403                 gtk_widget_set_sensitive(GTK_WIDGET(offlinesync_rec_checkbtn),
404                                                                  FALSE);
405         
406         }
407         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_offlinesync),
408                                                                  item->prefs->offlinesync);
409         rowcount++;
410
411         gtk_widget_show_all(table);
412
413         page->table = table;
414         page->folder_type = folder_type;
415         page->checkbtn_simplify_subject = checkbtn_simplify_subject;
416         page->entry_simplify_subject = entry_simplify_subject;
417         page->entry_regexp_test_string = entry_regexp_test_string;
418         page->entry_regexp_test_result = entry_regexp_test_result;
419         page->checkbtn_folder_chmod = checkbtn_folder_chmod;
420         page->entry_folder_chmod = entry_folder_chmod;
421         page->folder_color_btn = folder_color_btn;
422         page->checkbtn_enable_processing = checkbtn_enable_processing;
423         page->checkbtn_newmailcheck = checkbtn_newmailcheck;
424         page->checkbtn_offlinesync = checkbtn_offlinesync;
425
426         page->simplify_subject_rec_checkbtn  = simplify_subject_rec_checkbtn;
427         page->folder_chmod_rec_checkbtn      = folder_chmod_rec_checkbtn;
428         page->folder_color_rec_checkbtn      = folder_color_rec_checkbtn;
429         page->enable_processing_rec_checkbtn = enable_processing_rec_checkbtn;
430         page->newmailcheck_rec_checkbtn      = newmailcheck_rec_checkbtn;
431         page->offlinesync_rec_checkbtn       = offlinesync_rec_checkbtn;
432
433         page->page.widget = table;
434
435         folder_regexp_set_subject_example_cb(NULL, page);
436 }
437
438 void prefs_folder_item_general_destroy_widget_func(PrefsPage *page_) 
439 {
440         /* FolderItemGeneralPage *page = (FolderItemGeneralPage *) page_; */
441 }
442
443 /** \brief  Save the prefs in page to folder.
444  *
445  *  If the folder is not the one  specified in page->item, then only those properties 
446  *  that have the relevant 'apply to sub folders' button checked are saved
447  */
448 static void general_save_folder_prefs(FolderItem *folder, FolderItemGeneralPage *page)
449 {
450         FolderItemPrefs *prefs = folder->prefs;
451         gchar *buf;
452         gboolean all = FALSE;
453         SpecialFolderItemType type = F_NORMAL;
454         GtkWidget *menu;
455         GtkWidget *menuitem;
456
457         if (folder->path == NULL)
458                 return;
459
460         g_return_if_fail(prefs != NULL);
461
462         if (page->item == folder) 
463                 all = TRUE;
464
465         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(page->folder_type));
466         menuitem = gtk_menu_get_active(GTK_MENU(menu));
467         type = GPOINTER_TO_INT
468                 (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
469         if (all && folder->stype != type) {
470                 folder_item_change_type(folder, type);
471         }
472
473         if (all || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->simplify_subject_rec_checkbtn))) {
474                 prefs->enable_simplify_subject =
475                         gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_simplify_subject));
476                 ASSIGN_STRING(prefs->simplify_subject_regexp,
477                               gtk_editable_get_chars(GTK_EDITABLE(page->entry_simplify_subject), 0, -1));
478         }
479         
480         if (all || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->folder_chmod_rec_checkbtn))) {
481                 prefs->enable_folder_chmod = 
482                         gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_folder_chmod));
483                 buf = gtk_editable_get_chars(GTK_EDITABLE(page->entry_folder_chmod), 0, -1);
484                 prefs->folder_chmod = prefs_folder_item_chmod_mode(buf);
485                 g_free(buf);
486         }
487
488         if (all || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->folder_color_rec_checkbtn))) {
489                 int old_color = prefs->color;
490                 prefs->color = page->folder_color;
491         
492                 /* update folder view */
493                 if (prefs->color != old_color)
494                         folder_item_update(folder, F_ITEM_UPDATE_MSGCNT);
495         }
496
497         if (all || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->enable_processing_rec_checkbtn))) {
498                 prefs->enable_processing = 
499                         gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_enable_processing));
500         }
501
502         if (all ||  gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->newmailcheck_rec_checkbtn))) {
503                 prefs->newmailcheck = 
504                         gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_newmailcheck));
505         }
506
507         if (all ||  gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->offlinesync_rec_checkbtn))) {
508                 prefs->offlinesync = 
509                         gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_offlinesync));
510         }
511
512         folder_item_prefs_save_config(folder);
513 }       
514
515 static gboolean general_save_recurse_func(GNode *node, gpointer data)
516 {
517         FolderItem *item = (FolderItem *) node->data;
518         FolderItemGeneralPage *page = (FolderItemGeneralPage *) data;
519
520         g_return_val_if_fail(item != NULL, TRUE);
521         g_return_val_if_fail(page != NULL, TRUE);
522
523         general_save_folder_prefs(item, page);
524
525         /* optimise by not continuing if none of the 'apply to sub folders'
526            check boxes are selected - and optimise the checking by only doing
527            it once */
528         if ((node == page->item->node) &&
529             !(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->simplify_subject_rec_checkbtn)) ||
530               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->folder_chmod_rec_checkbtn)) ||
531               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->folder_color_rec_checkbtn)) ||
532               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->enable_processing_rec_checkbtn)) ||
533               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->newmailcheck_rec_checkbtn)) ||
534               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->offlinesync_rec_checkbtn))))
535                 return TRUE;
536         else 
537                 return FALSE;
538 }
539
540 void prefs_folder_item_general_save_func(PrefsPage *page_) 
541 {
542         FolderItemGeneralPage *page = (FolderItemGeneralPage *) page_;
543
544         g_node_traverse(page->item->node, G_PRE_ORDER, G_TRAVERSE_ALL,
545                         -1, general_save_recurse_func, page);
546
547 }
548
549 static RecvProtocol item_protocol(FolderItem *item)
550 {
551         if (!item)
552                 return A_NONE;
553         if (!item->folder)
554                 return A_NONE;
555         if (!item->folder->account)
556                 return A_NONE;
557         return item->folder->account->protocol;
558 }
559 void prefs_folder_item_compose_create_widget_func(PrefsPage * page_,
560                                                    GtkWindow * window,
561                                                    gpointer data)
562 {
563         FolderItemComposePage *page = (FolderItemComposePage *) page_;
564         FolderItem *item = (FolderItem *) data;
565         guint rowcount;
566
567         GtkWidget *table;
568         GtkWidget *label;
569         
570         GtkWidget *checkbtn_request_return_receipt = NULL;
571         GtkWidget *checkbtn_save_copy_to_folder = NULL;
572         GtkWidget *checkbtn_default_to = NULL;
573         GtkWidget *entry_default_to = NULL;
574         GtkWidget *checkbtn_default_reply_to = NULL;
575         GtkWidget *entry_default_reply_to = NULL;
576         GtkWidget *checkbtn_enable_default_account = NULL;
577         GtkWidget *optmenu_default_account = NULL;
578         GtkWidget *optmenu_default_account_menu = NULL;
579         GtkWidget *optmenu_default_account_menuitem = NULL;
580 #if USE_ASPELL
581         GtkWidget *checkbtn_enable_default_dictionary = NULL;
582         GtkWidget *optmenu_default_dictionary = NULL;
583 #endif
584         GtkWidget *request_return_receipt_rec_checkbtn = NULL;
585         GtkWidget *save_copy_to_folder_rec_checkbtn = NULL;
586         GtkWidget *default_to_rec_checkbtn = NULL;
587         GtkWidget *default_reply_to_rec_checkbtn = NULL;
588         GtkWidget *default_account_rec_checkbtn = NULL;
589 #if USE_ASPELL
590         GtkWidget *default_dictionary_rec_checkbtn = NULL;
591 #endif
592
593         GList *cur_ac;
594         GList *account_list;
595 #if USE_ASPELL
596         gchar *dictionary;
597 #endif
598         PrefsAccount *ac_prefs;
599         GtkOptionMenu *optmenu;
600         GtkWidget *menu;
601         GtkWidget *menuitem;
602         gint account_index, index;
603
604         page->item         = item;
605
606         /* Table */
607 #if USE_ASPELL
608 # define TABLEHEIGHT 6
609 #else
610 # define TABLEHEIGHT 5
611 #endif
612         table = gtk_table_new(TABLEHEIGHT, 2, FALSE);
613         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
614         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
615         rowcount = 0;
616
617         /* Apply to subfolders */
618         label = gtk_label_new(_("Apply to\nsubfolders"));
619         gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0.5);
620         gtk_table_attach(GTK_TABLE(table), label, 2, 3,
621                          rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
622         rowcount++;
623
624         if (item_protocol(item) != A_NNTP) {
625                 /* Request Return Receipt */
626                 checkbtn_request_return_receipt = gtk_check_button_new_with_label
627                         (_("Request Return Receipt"));
628                 gtk_table_attach(GTK_TABLE(table), checkbtn_request_return_receipt, 
629                                  0, 2, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, 
630                                  GTK_FILL, 0, 0);
631                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_request_return_receipt),
632                                              item->ret_rcpt ? TRUE : FALSE);
633
634                 request_return_receipt_rec_checkbtn = gtk_check_button_new();
635                 gtk_table_attach(GTK_TABLE(table), request_return_receipt_rec_checkbtn, 2, 3, 
636                                  rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
637
638                 rowcount++;
639
640                 /* Save Copy to Folder */
641                 checkbtn_save_copy_to_folder = gtk_check_button_new_with_label
642                         (_("Save copy of outgoing messages to this folder instead of Sent"));
643                 gtk_table_attach(GTK_TABLE(table), checkbtn_save_copy_to_folder, 0, 2, 
644                                  rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_FILL, 0, 0);
645                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_save_copy_to_folder),
646                                              item->prefs->save_copy_to_folder ? TRUE : FALSE);
647
648                 save_copy_to_folder_rec_checkbtn = gtk_check_button_new();
649                 gtk_table_attach(GTK_TABLE(table), save_copy_to_folder_rec_checkbtn, 2, 3, 
650                                  rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
651
652                 rowcount++;
653
654                 /* Default To */
655                 checkbtn_default_to = gtk_check_button_new_with_label(_("Default To: "));
656                 gtk_table_attach(GTK_TABLE(table), checkbtn_default_to, 0, 1, 
657                                  rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
658                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_default_to), 
659                                              item->prefs->enable_default_to);
660
661                 entry_default_to = gtk_entry_new();
662                 gtk_table_attach(GTK_TABLE(table), entry_default_to, 1, 2,
663                                  rowcount, rowcount + 1, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
664                 SET_TOGGLE_SENSITIVITY(checkbtn_default_to, entry_default_to);
665                 gtk_entry_set_text(GTK_ENTRY(entry_default_to), SAFE_STRING(item->prefs->default_to));
666                 address_completion_register_entry(GTK_ENTRY(entry_default_to),
667                                 TRUE);
668
669                 default_to_rec_checkbtn = gtk_check_button_new();
670                 gtk_table_attach(GTK_TABLE(table), default_to_rec_checkbtn, 2, 3, 
671                                  rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
672
673                 rowcount++;
674
675                 /* Default address to reply to */
676                 checkbtn_default_reply_to = gtk_check_button_new_with_label(_("Default To for replies: "));
677                 gtk_table_attach(GTK_TABLE(table), checkbtn_default_reply_to, 0, 1, 
678                                  rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
679                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_default_reply_to), 
680                                              item->prefs->enable_default_reply_to);
681
682                 entry_default_reply_to = gtk_entry_new();
683                 gtk_table_attach(GTK_TABLE(table), entry_default_reply_to, 1, 2,
684                                  rowcount, rowcount + 1, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
685                 SET_TOGGLE_SENSITIVITY(checkbtn_default_reply_to, entry_default_reply_to);
686                 gtk_entry_set_text(GTK_ENTRY(entry_default_reply_to), SAFE_STRING(item->prefs->default_reply_to));
687                 address_completion_register_entry(
688                                 GTK_ENTRY(entry_default_reply_to), TRUE);
689
690                 default_reply_to_rec_checkbtn = gtk_check_button_new();
691                 gtk_table_attach(GTK_TABLE(table), default_reply_to_rec_checkbtn, 2, 3, 
692                                  rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
693
694                 rowcount++;
695         }
696         /* Default account */
697         checkbtn_enable_default_account = gtk_check_button_new_with_label(_("Default account: "));
698         gtk_table_attach(GTK_TABLE(table), checkbtn_enable_default_account, 0, 1, 
699                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
700         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_enable_default_account), 
701                                      item->prefs->enable_default_account);
702
703         optmenu_default_account = gtk_option_menu_new ();
704         gtk_table_attach(GTK_TABLE(table), optmenu_default_account, 1, 2, 
705                          rowcount, rowcount + 1, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
706         optmenu_default_account_menu = gtk_menu_new ();
707
708         account_list = account_get_list();
709         account_index = 0;
710         index = 0;
711         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
712                 ac_prefs = (PrefsAccount *)cur_ac->data;
713                 if (item->folder->account &&
714                     ( (item_protocol(item) == A_NNTP && ac_prefs->protocol != A_NNTP)
715                     ||(item_protocol(item) != A_NNTP && ac_prefs->protocol == A_NNTP))) 
716                         continue;
717
718                 if (item->folder->klass->type != F_NEWS && ac_prefs->protocol == A_NNTP)
719                         continue;
720
721                 MENUITEM_ADD (optmenu_default_account_menu, optmenu_default_account_menuitem,
722                                         ac_prefs->account_name?ac_prefs->account_name : _("Untitled"),
723                                         ac_prefs->account_id);
724                 /* get the index for menu's set_history (sad method?) */
725                 if (ac_prefs->account_id == item->prefs->default_account)
726                         account_index = index;
727                 index++;                        
728         }
729
730         optmenu = GTK_OPTION_MENU(optmenu_default_account);
731         gtk_option_menu_set_menu(optmenu, optmenu_default_account_menu);
732
733         gtk_option_menu_set_history(optmenu, account_index);
734
735         menu = gtk_option_menu_get_menu(optmenu);
736         menuitem = gtk_menu_get_active(GTK_MENU(menu));
737         gtk_menu_item_activate(GTK_MENU_ITEM(menuitem));
738
739         SET_TOGGLE_SENSITIVITY(checkbtn_enable_default_account, optmenu_default_account);
740
741         default_account_rec_checkbtn = gtk_check_button_new();
742         gtk_table_attach(GTK_TABLE(table), default_account_rec_checkbtn, 2, 3, 
743                          rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
744         rowcount++;
745
746 #if USE_ASPELL
747         /* Default dictionary */
748         checkbtn_enable_default_dictionary = gtk_check_button_new_with_label(_("Default dictionary: "));
749         gtk_table_attach(GTK_TABLE(table), checkbtn_enable_default_dictionary, 0, 1,
750                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
751         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_enable_default_dictionary),
752                                      item->prefs->enable_default_dictionary);
753
754         optmenu_default_dictionary = gtk_option_menu_new();
755         gtk_table_attach(GTK_TABLE(table), optmenu_default_dictionary, 1, 2,
756                          rowcount, rowcount + 1, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
757
758         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu_default_dictionary), 
759                                  gtkaspell_dictionary_option_menu_new(
760                                          prefs_common.aspell_path));
761
762         dictionary = item->prefs->default_dictionary;
763
764         optmenu = GTK_OPTION_MENU(optmenu_default_dictionary);
765
766         menu = gtk_option_menu_get_menu(optmenu);
767         if (dictionary)
768                 gtkaspell_set_dictionary_menu_active_item(optmenu_default_dictionary, dictionary);
769         menuitem = gtk_menu_get_active(GTK_MENU(menu));
770         gtk_menu_item_activate(GTK_MENU_ITEM(menuitem));
771
772         SET_TOGGLE_SENSITIVITY(checkbtn_enable_default_dictionary, optmenu_default_dictionary);
773
774         default_dictionary_rec_checkbtn = gtk_check_button_new();
775         gtk_table_attach(GTK_TABLE(table), default_dictionary_rec_checkbtn, 2, 3, 
776                          rowcount, rowcount + 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
777         
778         rowcount++;
779 #endif
780
781         gtk_widget_show_all(table);
782
783         page->window = GTK_WIDGET(window);
784         page->table = table;
785         page->checkbtn_request_return_receipt = checkbtn_request_return_receipt;
786         page->checkbtn_save_copy_to_folder = checkbtn_save_copy_to_folder;
787         page->checkbtn_default_to = checkbtn_default_to;
788         page->entry_default_to = entry_default_to;
789         page->checkbtn_default_reply_to = checkbtn_default_reply_to;
790         page->entry_default_reply_to = entry_default_reply_to;
791         page->checkbtn_enable_default_account = checkbtn_enable_default_account;
792         page->optmenu_default_account = optmenu_default_account;
793 #ifdef USE_ASPELL
794         page->checkbtn_enable_default_dictionary = checkbtn_enable_default_dictionary;
795         page->optmenu_default_dictionary = optmenu_default_dictionary;
796 #endif
797
798         page->request_return_receipt_rec_checkbtn = request_return_receipt_rec_checkbtn;
799         page->save_copy_to_folder_rec_checkbtn    = save_copy_to_folder_rec_checkbtn;
800         page->default_to_rec_checkbtn             = default_to_rec_checkbtn;
801         page->default_reply_to_rec_checkbtn       = default_reply_to_rec_checkbtn;
802         page->default_account_rec_checkbtn        = default_account_rec_checkbtn;
803 #if USE_ASPELL
804         page->default_dictionary_rec_checkbtn = default_dictionary_rec_checkbtn;
805 #endif
806
807         address_completion_start(page->window);
808
809         page->page.widget = table;
810 }
811
812 void prefs_folder_item_compose_destroy_widget_func(PrefsPage *page_) 
813 {
814         FolderItemComposePage *page = (FolderItemComposePage *) page_;
815
816         if (page->entry_default_to)
817                 address_completion_unregister_entry(GTK_ENTRY(page->entry_default_to));
818         if (page->entry_default_reply_to)
819                 address_completion_unregister_entry(GTK_ENTRY(page->entry_default_reply_to));
820         address_completion_end(page->window);
821 }
822
823 /** \brief  Save the prefs in page to folder.
824  *
825  *  If the folder is not the one  specified in page->item, then only those properties 
826  *  that have the relevant 'apply to sub folders' button checked are saved
827  */
828 static void compose_save_folder_prefs(FolderItem *folder, FolderItemComposePage *page)
829 {
830         FolderItemPrefs *prefs = folder->prefs;
831         GtkWidget *menu;
832         GtkWidget *menuitem;
833         gboolean all = FALSE;
834
835         if (folder->path == NULL)
836                 return;
837
838         if (page->item == folder) 
839                 all = TRUE;
840
841         g_return_if_fail(prefs != NULL);
842
843         if (item_protocol(folder) != A_NNTP) {
844                 if (all || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->request_return_receipt_rec_checkbtn))) {
845                         prefs->request_return_receipt = 
846                                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_request_return_receipt));
847                         /* MIGRATION */    
848                         folder->ret_rcpt = prefs->request_return_receipt;
849                 }
850
851                 if (all || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->save_copy_to_folder_rec_checkbtn))) {
852                         prefs->save_copy_to_folder = 
853                                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_save_copy_to_folder));
854                 }
855
856                 if (all || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->default_to_rec_checkbtn))) {
857
858                         prefs->enable_default_to = 
859                                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_default_to));
860                         ASSIGN_STRING(prefs->default_to,
861                                       gtk_editable_get_chars(GTK_EDITABLE(page->entry_default_to), 0, -1));
862                 }
863
864                 if (all || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->default_reply_to_rec_checkbtn))) {
865                         prefs->enable_default_reply_to = 
866                                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_default_reply_to));
867                         ASSIGN_STRING(prefs->default_reply_to,
868                                       gtk_editable_get_chars(GTK_EDITABLE(page->entry_default_reply_to), 0, -1));
869                 }
870         } else {
871                 prefs->request_return_receipt = FALSE;
872                 prefs->save_copy_to_folder = FALSE;
873                 prefs->enable_default_to = FALSE;
874                 prefs->enable_default_reply_to = FALSE;
875         }
876         if (all || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->default_account_rec_checkbtn))) {
877                 prefs->enable_default_account = 
878                         gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_enable_default_account));
879                 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(page->optmenu_default_account));
880                 menuitem = gtk_menu_get_active(GTK_MENU(menu));
881                 prefs->default_account = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
882         }
883
884 #if USE_ASPELL
885         if (all || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->default_dictionary_rec_checkbtn))) {
886                 prefs->enable_default_dictionary =
887                         gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_enable_default_dictionary));
888                 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(page->optmenu_default_dictionary));
889                 ASSIGN_STRING(prefs->default_dictionary,
890                               gtkaspell_get_dictionary_menu_active_item(menu));
891         }
892 #endif
893
894         folder_item_prefs_save_config(folder);
895 }       
896
897 static gboolean compose_save_recurse_func(GNode *node, gpointer data)
898 {
899         FolderItem *item = (FolderItem *) node->data;
900         FolderItemComposePage *page = (FolderItemComposePage *) data;
901
902         g_return_val_if_fail(item != NULL, TRUE);
903         g_return_val_if_fail(page != NULL, TRUE);
904
905         compose_save_folder_prefs(item, page);
906
907         /* optimise by not continuing if none of the 'apply to sub folders'
908            check boxes are selected - and optimise the checking by only doing
909            it once */
910         if ((node == page->item->node) && item_protocol(item) != A_NNTP &&
911             !(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->request_return_receipt_rec_checkbtn)) ||
912               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->save_copy_to_folder_rec_checkbtn)) ||
913               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->default_to_rec_checkbtn)) ||
914               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->default_account_rec_checkbtn)) ||
915 #if USE_ASPELL
916               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->default_dictionary_rec_checkbtn)) ||
917 #endif
918               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->default_reply_to_rec_checkbtn))))
919                 return TRUE;
920
921         if ((node == page->item->node) &&
922             !(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->default_account_rec_checkbtn)) 
923 #if USE_ASPELL
924               || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->default_dictionary_rec_checkbtn))
925 #endif
926                     ))
927                 return TRUE;
928         else 
929                 return FALSE;
930 }
931
932 void prefs_folder_item_compose_save_func(PrefsPage *page_) 
933 {
934         FolderItemComposePage *page = (FolderItemComposePage *) page_;
935
936         g_node_traverse(page->item->node, G_PRE_ORDER, G_TRAVERSE_ALL,
937                         -1, compose_save_recurse_func, page);
938
939 }
940
941 gint prefs_folder_item_chmod_mode(gchar *folder_chmod) 
942 {
943         gint newmode = 0;
944         gchar *tmp;
945
946         if (folder_chmod) {
947                 newmode = strtol(folder_chmod, &tmp, 8);
948                 if (!(*(folder_chmod) && !(*tmp)))
949                         newmode = 0;
950         }
951
952         return newmode;
953 }
954
955 static void folder_color_set_dialog(GtkWidget *widget, gpointer data)
956 {
957         FolderItemGeneralPage *page = (FolderItemGeneralPage *) data;
958         gint rgbcolor;
959
960         rgbcolor = colorsel_select_color_rgb(_("Pick color for folder"), 
961                                              page->folder_color);
962         gtkut_set_widget_bgcolor_rgb(page->folder_color_btn, rgbcolor);
963         page->folder_color = rgbcolor;
964 }
965
966 static regex_t *summary_compile_simplify_regexp(gchar *simplify_subject_regexp)
967 {
968         int err;
969         gchar buf[BUFFSIZE];
970         regex_t *preg = NULL;
971
972         preg = g_new0(regex_t, 1);
973
974         err = string_match_precompile(simplify_subject_regexp, 
975                                       preg, REG_EXTENDED);
976         if (err) {
977                 regerror(err, preg, buf, BUFFSIZE);
978                 g_free(preg);
979                 preg = NULL;
980         }
981
982         return preg;
983 }
984
985 static void folder_regexp_test_cb(GtkWidget *widget, gpointer data)
986 {
987         static GdkColor red;
988         static gboolean colors_initialised = FALSE;
989         static gchar buf[BUFFSIZE];
990         FolderItemGeneralPage *page = (FolderItemGeneralPage *)data;
991         gchar *test_string, *regexp;
992         regex_t *preg;
993
994         regexp = g_strdup(gtk_entry_get_text(GTK_ENTRY(page->entry_simplify_subject)));
995         test_string = g_strdup(gtk_entry_get_text(GTK_ENTRY(page->entry_regexp_test_string)));
996
997         if (!regexp || !regexp[0]) {
998                 if (test_string) {
999                         gtk_entry_set_text(GTK_ENTRY(page->entry_regexp_test_result), test_string);
1000                         g_free(test_string);
1001                 }
1002                 return;
1003         }
1004
1005         if (!test_string || !test_string[0]) {
1006                 g_free(regexp);
1007                 return;
1008         }
1009
1010         if (!colors_initialised) {
1011                 gdk_color_parse("#ff7070", &red);
1012                 colors_initialised = gdk_colormap_alloc_color(
1013                         gdk_colormap_get_system(), &red, FALSE, TRUE);
1014         }
1015
1016         preg = summary_compile_simplify_regexp(regexp);
1017         if (colors_initialised) {
1018                 gtk_widget_modify_base(page->entry_simplify_subject,
1019                                 GTK_STATE_NORMAL, preg ? NULL : &red);
1020         }
1021
1022         if (preg != NULL) {
1023                 string_remove_match(buf, BUFFSIZE, test_string, preg);
1024
1025                 gtk_entry_set_text(GTK_ENTRY(page->entry_regexp_test_result), buf);
1026
1027                 regfree(preg);
1028                 g_free(preg);
1029         }
1030
1031         g_free(test_string);
1032         g_free(regexp);
1033 }
1034
1035 static gchar *folder_regexp_get_subject_example(void)
1036 {
1037         MsgInfo *msginfo_selected;
1038         SummaryView *summaryview = NULL;
1039
1040         if (!mainwindow_get_mainwindow())
1041                 return NULL;
1042         summaryview = mainwindow_get_mainwindow()->summaryview;
1043
1044         msginfo_selected = summary_get_selected_msg(summaryview);
1045         return msginfo_selected ? g_strdup(msginfo_selected->subject) : NULL;
1046 }
1047
1048 static void folder_regexp_set_subject_example_cb(GtkWidget *widget, gpointer data)
1049 {
1050         FolderItemGeneralPage *page = (FolderItemGeneralPage *)data;
1051
1052         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_simplify_subject))) {
1053                 gchar *subject = folder_regexp_get_subject_example();
1054                 if (subject) {
1055                         gtk_entry_set_text(GTK_ENTRY(page->entry_regexp_test_string), subject);
1056                         g_free(subject);
1057                 }
1058         }
1059 }
1060
1061 static void register_general_page()
1062 {
1063         static gchar *pfi_general_path[2];
1064         static FolderItemGeneralPage folder_item_general_page;
1065
1066         pfi_general_path[0] = _("General");
1067         pfi_general_path[1] = NULL;
1068
1069         folder_item_general_page.page.path = pfi_general_path;
1070         folder_item_general_page.page.create_widget = prefs_folder_item_general_create_widget_func;
1071         folder_item_general_page.page.destroy_widget = prefs_folder_item_general_destroy_widget_func;
1072         folder_item_general_page.page.save_page = prefs_folder_item_general_save_func;
1073         
1074         prefs_folder_item_register_page((PrefsPage *) &folder_item_general_page);
1075 }
1076
1077
1078 static void register_compose_page(void)
1079 {
1080         static gchar *pfi_compose_path[2];
1081         static FolderItemComposePage folder_item_compose_page;
1082
1083         pfi_compose_path[0] = _("Compose");
1084         pfi_compose_path[1] = NULL;
1085
1086         folder_item_compose_page.page.path = pfi_compose_path;
1087         folder_item_compose_page.page.create_widget = prefs_folder_item_compose_create_widget_func;
1088         folder_item_compose_page.page.destroy_widget = prefs_folder_item_compose_destroy_widget_func;
1089         folder_item_compose_page.page.save_page = prefs_folder_item_compose_save_func;
1090         
1091         prefs_folder_item_register_page((PrefsPage *) &folder_item_compose_page);
1092 }
1093
1094 static GSList *prefs_pages = NULL;
1095
1096 void prefs_folder_item_open(FolderItem *item)
1097 {
1098         gchar *id, *title;
1099
1100         if (prefs_pages == NULL) {
1101                 register_general_page();
1102                 register_compose_page();
1103         }
1104
1105         if (item->path)
1106                 id = folder_item_get_identifier (item);
1107         else 
1108                 id = g_strdup(item->name);
1109         title = g_strdup_printf (_("Properties for folder %s"), id);
1110         g_free (id);
1111         prefswindow_open(title, prefs_pages, item,
1112                         &prefs_common.folderitemwin_width, &prefs_common.folderitemwin_height);
1113         g_free (title);
1114 }
1115
1116 void prefs_folder_item_register_page(PrefsPage *page)
1117 {
1118         prefs_pages = g_slist_append(prefs_pages, page);
1119 }
1120
1121 void prefs_folder_item_unregister_page(PrefsPage *page)
1122 {
1123         prefs_pages = g_slist_remove(prefs_pages, page);
1124 }