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