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