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