* src/prefs_folder_item.c
[claws.git] / src / prefs_folder_item.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "intl.h"
29 #include "defs.h"
30 #include "folder.h"
31 #include "prefs_folder_item.h"
32 #include "folderview.h"
33 #include "summaryview.h"
34 #include "menu.h"
35 #include "account.h"
36 #include "prefs_gtk.h"
37 #include "manage_window.h"
38 #include "utils.h"
39 #include "addr_compl.h"
40 #include "prefs_scoring.h"
41 #include "gtkutils.h"
42 #include "filtering.h"
43
44 PrefsFolderItem tmp_prefs;
45
46 struct PrefsFolderItemDialog
47 {
48         FolderView *folderview;
49         FolderItem *item;
50         GtkWidget *window;
51         GtkWidget *checkbtn_request_return_receipt;
52         GtkWidget *checkbtn_save_copy_to_folder;
53         GtkWidget *checkbtn_default_to;
54         GtkWidget *entry_default_to;
55         GtkWidget *checkbtn_default_reply_to;
56         GtkWidget *entry_default_reply_to;
57         GtkWidget *checkbtn_simplify_subject;
58         GtkWidget *entry_simplify_subject;
59         GtkWidget *checkbtn_folder_chmod;
60         GtkWidget *entry_folder_chmod;
61         GtkWidget *checkbtn_enable_default_account;
62         GtkWidget *optmenu_default_account;
63         GtkWidget *folder_color;
64         GtkWidget *folder_color_btn;
65 };
66
67 static GtkWidget *color_dialog;
68
69 static PrefParam param[] = {
70         {"sort_by_number", "FALSE", &tmp_prefs.sort_by_number, P_BOOL,
71          NULL, NULL, NULL},
72         {"sort_by_size", "FALSE", &tmp_prefs.sort_by_size, P_BOOL,
73          NULL, NULL, NULL},
74         {"sort_by_date", "FALSE", &tmp_prefs.sort_by_date, P_BOOL,
75          NULL, NULL, NULL},
76         {"sort_by_from", "FALSE", &tmp_prefs.sort_by_from, P_BOOL,
77          NULL, NULL, NULL},
78         {"sort_by_subject", "FALSE", &tmp_prefs.sort_by_subject, P_BOOL,
79          NULL, NULL, NULL},
80         {"sort_by_score", "FALSE", &tmp_prefs.sort_by_score, P_BOOL,
81          NULL, NULL, NULL},
82         {"sort_descending", "FALSE", &tmp_prefs.sort_descending, P_BOOL,
83          NULL, NULL, NULL},
84         /*{"enable_thread", "TRUE", &tmp_prefs.enable_thread, P_BOOL,
85          NULL, NULL, NULL},*/
86         {"hide_score", "-9999", &tmp_prefs.kill_score, P_INT,
87          NULL, NULL, NULL},
88         {"important_score", "1", &tmp_prefs.important_score, P_INT,
89          NULL, NULL, NULL},
90         /* MIGRATION */  
91         {"request_return_receipt", "", &tmp_prefs.request_return_receipt, P_BOOL,
92          NULL, NULL, NULL},
93         {"enable_default_to", "", &tmp_prefs.enable_default_to, P_BOOL,
94          NULL, NULL, NULL},
95         {"default_to", "", &tmp_prefs.default_to, P_STRING,
96          NULL, NULL, NULL},
97         {"enable_default_reply_to", "", &tmp_prefs.enable_default_reply_to, P_BOOL,
98          NULL, NULL, NULL},
99         {"default_reply_to", "", &tmp_prefs.default_reply_to, P_STRING,
100          NULL, NULL, NULL},
101         {"enable_simplify_subject", "", &tmp_prefs.enable_simplify_subject, P_BOOL,
102          NULL, NULL, NULL},
103         {"simplify_subject_regexp", "", &tmp_prefs.simplify_subject_regexp, P_STRING,
104          NULL, NULL, NULL},
105         {"enable_folder_chmod", "", &tmp_prefs.enable_folder_chmod, P_BOOL,
106          NULL, NULL, NULL},
107         {"folder_chmod", "", &tmp_prefs.folder_chmod, P_INT,
108          NULL, NULL, NULL},
109         {"enable_default_account", "", &tmp_prefs.enable_default_account, P_BOOL,
110          NULL, NULL, NULL},
111         {"default_account", NULL, &tmp_prefs.default_account, P_INT,
112          NULL, NULL, NULL},
113         {"save_copy_to_folder", NULL, &tmp_prefs.save_copy_to_folder, P_BOOL,
114          NULL, NULL, NULL},
115         {"folder_color", "", &tmp_prefs.color, P_INT,
116          NULL, NULL, NULL},
117         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
118 };
119
120 static PrefsFolderItem *prefs_folder_item_clear (PrefsFolderItem *prefs);
121 void prefs_folder_item_delete_cb                (GtkWidget *widget, GdkEventAny *event, 
122                                                  struct PrefsFolderItemDialog *dialog);
123 void prefs_folder_item_cancel_cb                (GtkWidget *widget, 
124                                                  struct PrefsFolderItemDialog *dialog);
125 void prefs_folder_item_ok_cb                    (GtkWidget *widget, 
126                                                  struct PrefsFolderItemDialog *dialog);
127 gint prefs_folder_item_chmod_mode               (gchar *folder_chmod);
128
129 static void set_button_color(guint rgbvalue, GtkWidget *button);
130 static void folder_color_set_dialog(GtkWidget *widget, gpointer data);
131 static void folder_color_set_dialog_ok(GtkWidget *widget, gpointer data);
132 static void folder_color_set_dialog_cancel(GtkWidget *widget, gpointer data);
133 static void folder_color_set_dialog_key_pressed(GtkWidget *widget,
134                                                 GdkEventKey *event,
135                                                 gpointer data);
136
137
138 void prefs_folder_item_read_config(FolderItem * item)
139 {
140         gchar * id;
141
142         id = folder_item_get_identifier(item);
143         prefs_folder_item_clear(&tmp_prefs);
144         prefs_read_config(param, id, FOLDERITEM_RC);
145         g_free(id);
146
147         *item->prefs = tmp_prefs;
148
149         /*
150          * MIGRATION: next lines are migration code. the idea is that
151          *            if used regularly, claws folder config ends up
152          *            in the same file as sylpheed-main
153          */
154
155         item->ret_rcpt = tmp_prefs.request_return_receipt ? TRUE : FALSE;
156
157         /* MIGRATION: 0.7.8main+ has persistent sort order. claws had the sort
158          *            order in different members, which is ofcourse a little
159          *            bit phoney. */
160         if (item->sort_key == SORT_BY_NONE) {
161                 item->sort_key  = (tmp_prefs.sort_by_number  ? SORT_BY_NUMBER  :
162                                    tmp_prefs.sort_by_size    ? SORT_BY_SIZE    :
163                                    tmp_prefs.sort_by_date    ? SORT_BY_DATE    :
164                                    tmp_prefs.sort_by_from    ? SORT_BY_FROM    :
165                                    tmp_prefs.sort_by_subject ? SORT_BY_SUBJECT :
166                                    tmp_prefs.sort_by_score   ? SORT_BY_SCORE   :
167                                                                  SORT_BY_NONE);
168                 item->sort_type = tmp_prefs.sort_descending ? SORT_DESCENDING : SORT_ASCENDING;
169         }                                                               
170 }
171
172 void prefs_folder_item_save_config(FolderItem * item)
173 {       
174         gchar * id;
175
176         tmp_prefs = * item->prefs;
177
178         id = folder_item_get_identifier(item);
179
180         prefs_save_config(param, id, FOLDERITEM_RC);
181         g_free(id);
182
183         /* MIGRATION: make sure migrated items are not saved
184          */
185 }
186
187 void prefs_folder_item_set_config(FolderItem * item,
188                                   int sort_type, gint sort_mode)
189 {
190         g_assert(item);
191         g_warning("prefs_folder_item_set_config() should never be called\n");
192         item->sort_key  = sort_type;
193         item->sort_type = sort_mode;
194 }
195
196 static PrefsFolderItem *prefs_folder_item_clear(PrefsFolderItem *prefs)
197 {
198         prefs->sort_by_number = FALSE;
199         prefs->sort_by_size = FALSE;
200         prefs->sort_by_date = FALSE;
201         prefs->sort_by_from = FALSE;
202         prefs->sort_by_subject = FALSE;
203         prefs->sort_by_score = FALSE;
204         prefs->sort_descending = FALSE;
205         prefs->kill_score = -9999;
206         prefs->important_score = 9999;
207
208         prefs->request_return_receipt = FALSE;
209         prefs->enable_default_to = FALSE;
210         prefs->default_to = NULL;
211         prefs->enable_default_reply_to = FALSE;
212         prefs->default_reply_to = NULL;
213         prefs->enable_simplify_subject = FALSE;
214         prefs->simplify_subject_regexp = NULL;
215         prefs->enable_folder_chmod = FALSE;
216         prefs->folder_chmod = 0;
217         prefs->enable_default_account = FALSE;
218         prefs->default_account = 0;
219         prefs->save_copy_to_folder = FALSE;
220         prefs->color = 0;
221
222         prefs->scoring = NULL;
223         prefs->processing = NULL;
224         return prefs;
225 }
226
227 PrefsFolderItem * prefs_folder_item_new(void)
228 {
229         PrefsFolderItem * prefs;
230
231         prefs = g_new0(PrefsFolderItem, 1);
232
233         return prefs_folder_item_clear(prefs);
234 }
235
236 void prefs_folder_item_free(PrefsFolderItem * prefs)
237 {
238         if (prefs->default_to) 
239                 g_free(prefs->default_to);
240         if (prefs->default_reply_to) 
241                 g_free(prefs->default_reply_to);
242         if (prefs->scoring != NULL)
243                 prefs_scoring_free(prefs->scoring);
244         g_free(prefs);
245 }
246
247 gint prefs_folder_item_get_sort_mode(FolderItem * item)
248 {
249         g_assert(item != NULL);
250         g_warning("prefs_folder_item_get_sort_mode() should never be called\n");
251         return item->sort_key;
252 }
253
254 gint prefs_folder_item_get_sort_type(FolderItem * item)
255 {
256         g_assert(item != NULL);
257         g_warning("prefs_folder_item_get_sort_type() should never be called\n");
258         return item->sort_type;
259 }
260
261 #define SAFE_STRING(str) \
262         (str) ? (str) : ""
263
264 void prefs_folder_item_create(void *folderview, FolderItem *item) 
265 {
266         struct PrefsFolderItemDialog *dialog;
267         guint rowcount;
268         gchar *folder_identifier, *infotext;
269
270         GtkWidget *window;
271         GtkWidget *table;
272         GtkWidget *infolabel;
273         GtkWidget *ok_btn;
274         GtkWidget *cancel_btn;
275         GtkWidget *confirm_area;
276         
277         GtkWidget *checkbtn_request_return_receipt;
278         GtkWidget *checkbtn_save_copy_to_folder;
279         GtkWidget *checkbtn_default_to;
280         GtkWidget *entry_default_to;
281         GtkWidget *checkbtn_default_reply_to;
282         GtkWidget *entry_default_reply_to;
283         GtkWidget *checkbtn_simplify_subject;
284         GtkWidget *entry_simplify_subject;
285         GtkWidget *checkbtn_folder_chmod;
286         GtkWidget *entry_folder_chmod;
287         GtkWidget *checkbtn_enable_default_account;
288         GtkWidget *optmenu_default_account;
289         GtkWidget *optmenu_default_account_menu;
290         GtkWidget *optmenu_default_account_menuitem;
291         GtkWidget *folder_color;
292         GtkWidget *folder_color_btn;
293         GList *cur_ac;
294         GList *account_list;
295         PrefsAccount *ac_prefs;
296         GtkOptionMenu *optmenu;
297         GtkWidget *menu;
298         GtkWidget *menuitem;
299         gint account_index, index;
300
301         dialog = g_new0(struct PrefsFolderItemDialog, 1);
302         dialog->folderview = folderview;
303         dialog->item       = item;
304
305         /* Window */
306         window = gtk_window_new (GTK_WINDOW_DIALOG);
307         gtk_window_set_title (GTK_WINDOW(window),
308                               _("Folder Properties"));
309         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
310         gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
311         gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE);
312         gtk_signal_connect (GTK_OBJECT(window), "delete_event",
313                             GTK_SIGNAL_FUNC(prefs_folder_item_delete_cb), dialog);
314         MANAGE_WINDOW_SIGNALS_CONNECT (window);
315
316         /* Table */
317         table = gtk_table_new(8, 2, FALSE);
318         gtk_widget_show(table);
319         gtk_table_set_row_spacings(GTK_TABLE(table), VSPACING_NARROW);
320         gtk_container_add(GTK_CONTAINER (window), table);
321         rowcount = 0;
322
323         /* Label */
324         folder_identifier = folder_item_get_identifier(item);
325         infotext = g_strconcat(_("Folder Properties for "), folder_identifier, NULL);
326         infolabel = gtk_label_new(infotext);
327         gtk_table_attach(GTK_TABLE(table), infolabel, 0, 2, rowcount, 
328                          rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
329         gtk_widget_show(infolabel);
330         rowcount++;
331
332         /* Request Return Receipt */
333         checkbtn_request_return_receipt = gtk_check_button_new_with_label
334                 (_("Request Return Receipt"));
335         gtk_widget_show(checkbtn_request_return_receipt);
336         gtk_table_attach(GTK_TABLE(table), checkbtn_request_return_receipt, 
337                          0, 2, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, 
338                          GTK_SHRINK, 0, 0);
339         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_request_return_receipt),
340                                      item->ret_rcpt ? TRUE : FALSE);
341
342         rowcount++;
343
344         /* Save Copy to Folder */
345         checkbtn_save_copy_to_folder = gtk_check_button_new_with_label
346                 (_("Save copy of outgoing messages to this folder instead of Sent"));
347         gtk_widget_show(checkbtn_save_copy_to_folder);
348         gtk_table_attach(GTK_TABLE(table), checkbtn_save_copy_to_folder, 0, 2, 
349                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
350         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_save_copy_to_folder),
351                                      item->prefs->save_copy_to_folder ? TRUE : FALSE);
352
353         rowcount++;
354
355         /* Default To */
356         checkbtn_default_to = gtk_check_button_new_with_label(_("Default To: "));
357         gtk_widget_show(checkbtn_default_to);
358         gtk_table_attach(GTK_TABLE(table), checkbtn_default_to, 0, 1, 
359                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
360         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_default_to), 
361                                      item->prefs->enable_default_to);
362
363         entry_default_to = gtk_entry_new();
364         gtk_widget_show(entry_default_to);
365         gtk_table_attach_defaults(GTK_TABLE(table), entry_default_to, 1, 2, rowcount, rowcount + 1);
366         SET_TOGGLE_SENSITIVITY(checkbtn_default_to, entry_default_to);
367         gtk_entry_set_text(GTK_ENTRY(entry_default_to), SAFE_STRING(item->prefs->default_to));
368         address_completion_register_entry(GTK_ENTRY(entry_default_to));
369
370         rowcount++;
371
372         /* Default address to reply to */
373         checkbtn_default_reply_to = gtk_check_button_new_with_label(_("Send replies to: "));
374         gtk_widget_show(checkbtn_default_reply_to);
375         gtk_table_attach(GTK_TABLE(table), checkbtn_default_reply_to, 0, 1, 
376                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
377         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_default_reply_to), 
378                                      item->prefs->enable_default_reply_to);
379
380         entry_default_reply_to = gtk_entry_new();
381         gtk_widget_show(entry_default_reply_to);
382         gtk_table_attach_defaults(GTK_TABLE(table), entry_default_reply_to, 1, 2, rowcount, rowcount + 1);
383         SET_TOGGLE_SENSITIVITY(checkbtn_default_reply_to, entry_default_reply_to);
384         gtk_entry_set_text(GTK_ENTRY(entry_default_reply_to), SAFE_STRING(item->prefs->default_reply_to));
385         address_completion_register_entry(GTK_ENTRY(entry_default_reply_to));
386
387         rowcount++;
388
389         /* Simplify Subject */
390         checkbtn_simplify_subject = gtk_check_button_new_with_label(_("Simplify Subject RegExp: "));
391         gtk_widget_show(checkbtn_simplify_subject);
392         gtk_table_attach(GTK_TABLE(table), checkbtn_simplify_subject, 0, 1, 
393                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
394         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_simplify_subject), 
395                                      item->prefs->enable_simplify_subject);
396
397         entry_simplify_subject = gtk_entry_new();
398         gtk_widget_show(entry_simplify_subject);
399         gtk_table_attach_defaults(GTK_TABLE(table), entry_simplify_subject, 1, 2, 
400                                   rowcount, rowcount + 1);
401         SET_TOGGLE_SENSITIVITY(checkbtn_simplify_subject, entry_simplify_subject);
402         gtk_entry_set_text(GTK_ENTRY(entry_simplify_subject), 
403                            SAFE_STRING(item->prefs->simplify_subject_regexp));
404
405         rowcount++;
406
407         /* Folder chmod */
408         checkbtn_folder_chmod = gtk_check_button_new_with_label(_("Folder chmod: "));
409         gtk_widget_show(checkbtn_folder_chmod);
410         gtk_table_attach(GTK_TABLE(table), checkbtn_folder_chmod, 0, 1, 
411                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
412
413         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_folder_chmod), 
414                                      item->prefs->enable_folder_chmod);
415
416         entry_folder_chmod = gtk_entry_new();
417         gtk_widget_show(entry_folder_chmod);
418         gtk_table_attach_defaults(GTK_TABLE(table), entry_folder_chmod, 1, 2, 
419                                   rowcount, rowcount + 1);
420         SET_TOGGLE_SENSITIVITY(checkbtn_folder_chmod, entry_folder_chmod);
421         if (item->prefs->folder_chmod) {
422                 gchar *buf;
423
424                 buf = g_strdup_printf("%o", item->prefs->folder_chmod);
425                 gtk_entry_set_text(GTK_ENTRY(entry_folder_chmod), buf);
426                 g_free(buf);
427         }
428         
429         rowcount++;
430
431         /* Default account */
432         checkbtn_enable_default_account = gtk_check_button_new_with_label(_("Default account: "));
433         gtk_widget_show(checkbtn_enable_default_account);
434         gtk_table_attach(GTK_TABLE(table), checkbtn_enable_default_account, 0, 1, 
435                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
436         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_enable_default_account), 
437                                      item->prefs->enable_default_account);
438
439         optmenu_default_account = gtk_option_menu_new ();
440         gtk_widget_show (optmenu_default_account);
441         gtk_table_attach_defaults(GTK_TABLE(table), optmenu_default_account, 1, 2, 
442                                   rowcount, rowcount + 1);
443         optmenu_default_account_menu = gtk_menu_new ();
444
445         account_list = account_get_list();
446         account_index = 0;
447         index = 0;
448         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
449                 ac_prefs = (PrefsAccount *)cur_ac->data;
450                 MENUITEM_ADD (optmenu_default_account_menu, optmenu_default_account_menuitem,
451                                         ac_prefs->account_name?ac_prefs->account_name : _("Untitled"),
452                                         ac_prefs->account_id);
453                 /* get the index for menu's set_history (sad method?) */
454                 if (ac_prefs->account_id == item->prefs->default_account)
455                         account_index = index;
456                 index++;                        
457         }
458
459         dialog->item->prefs->default_account = item->prefs->default_account;
460
461         optmenu=GTK_OPTION_MENU(optmenu_default_account);
462         gtk_option_menu_set_menu(optmenu, optmenu_default_account_menu);
463
464         gtk_option_menu_set_history(optmenu, account_index);
465
466         menu = gtk_option_menu_get_menu(optmenu);
467         menuitem = gtk_menu_get_active(GTK_MENU(menu));
468         gtk_menu_item_activate(GTK_MENU_ITEM(menuitem));
469
470         SET_TOGGLE_SENSITIVITY(checkbtn_enable_default_account, optmenu_default_account);
471
472         rowcount++;
473
474         /* Folder color */
475         folder_color = gtk_label_new(_("Folder color: "));
476         gtk_misc_set_alignment(GTK_MISC(folder_color), 0, 0.5);
477         gtk_widget_show(folder_color);
478         gtk_table_attach_defaults(GTK_TABLE(table), folder_color, 0, 1, 
479                          rowcount, rowcount + 1);
480
481         folder_color_btn = gtk_button_new_with_label("");
482         gtk_widget_set_usize(folder_color_btn, 36, 26);
483         gtk_container_set_border_width(GTK_CONTAINER(folder_color_btn), 2);
484         gtk_widget_show(folder_color_btn);
485         gtk_table_attach(GTK_TABLE(table), folder_color_btn,
486                          1, 2, rowcount, rowcount + 1,
487                          GTK_SHRINK, 0, 0, 0);
488
489         dialog->item->prefs->color = item->prefs->color;
490
491         gtk_signal_connect(GTK_OBJECT(folder_color_btn), "clicked",
492                            GTK_SIGNAL_FUNC(folder_color_set_dialog),
493                            dialog);
494
495         set_button_color(item->prefs->color, folder_color_btn);
496
497         rowcount++;
498
499         /* Ok and Cancle Buttons */
500         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
501                                 &cancel_btn, _("Cancel"), NULL, NULL);
502         gtk_widget_show(confirm_area);
503         gtk_table_attach_defaults(GTK_TABLE(table), confirm_area, 0, 2, 
504                                   rowcount, rowcount + 1);
505         gtk_widget_grab_default(ok_btn);
506         gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
507                             GTK_SIGNAL_FUNC(prefs_folder_item_ok_cb), dialog);
508         gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
509                             GTK_SIGNAL_FUNC(prefs_folder_item_cancel_cb), dialog);
510
511         dialog->window = window;
512         dialog->checkbtn_request_return_receipt = checkbtn_request_return_receipt;
513         dialog->checkbtn_save_copy_to_folder = checkbtn_save_copy_to_folder;
514         dialog->checkbtn_default_to = checkbtn_default_to;
515         dialog->entry_default_to = entry_default_to;
516         dialog->checkbtn_default_reply_to = checkbtn_default_reply_to;
517         dialog->entry_default_reply_to = entry_default_reply_to;
518         dialog->checkbtn_simplify_subject = checkbtn_simplify_subject;
519         dialog->entry_simplify_subject = entry_simplify_subject;
520         dialog->checkbtn_folder_chmod = checkbtn_folder_chmod;
521         dialog->entry_folder_chmod = entry_folder_chmod;
522         dialog->checkbtn_enable_default_account = checkbtn_enable_default_account;
523         dialog->optmenu_default_account = optmenu_default_account;
524         dialog->folder_color = folder_color;
525         dialog->folder_color_btn = folder_color_btn;
526
527         g_free(infotext);
528
529         address_completion_start(window);
530
531         gtk_widget_show(window);
532 }
533
534 void prefs_folder_item_destroy(struct PrefsFolderItemDialog *dialog) 
535 {
536         address_completion_unregister_entry(GTK_ENTRY(dialog->entry_default_to));
537         address_completion_unregister_entry(GTK_ENTRY(dialog->entry_default_reply_to));
538         address_completion_end(dialog->window);
539         gtk_widget_destroy(dialog->window);
540         g_free(dialog);
541 }
542
543 void prefs_folder_item_cancel_cb(GtkWidget *widget, 
544                                  struct PrefsFolderItemDialog *dialog) 
545 {
546         prefs_folder_item_destroy(dialog);
547 }
548
549 void prefs_folder_item_delete_cb(GtkWidget *widget, GdkEventAny *event, 
550                                  struct PrefsFolderItemDialog *dialog) 
551 {
552         prefs_folder_item_destroy(dialog);
553 }
554
555 void prefs_folder_item_ok_cb(GtkWidget *widget, 
556                              struct PrefsFolderItemDialog *dialog) 
557 {
558         gchar *buf;
559         PrefsFolderItem *prefs = dialog->item->prefs;
560         GtkWidget *menu;
561         GtkWidget *menuitem;
562         gboolean   old_simplify_val;
563         gchar     *old_simplify_str;
564
565         g_return_if_fail(prefs != NULL);
566
567         old_simplify_val = prefs->enable_simplify_subject;
568         old_simplify_str = prefs->simplify_subject_regexp;
569
570         prefs->request_return_receipt = 
571             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_request_return_receipt));
572         /* MIGRATION */    
573         dialog->item->ret_rcpt = prefs->request_return_receipt;
574
575         prefs->save_copy_to_folder = 
576             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_save_copy_to_folder));
577
578         prefs->enable_default_to = 
579             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_default_to));
580         g_free(prefs->default_to);
581         prefs->default_to = 
582             gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_default_to), 0, -1);
583
584         prefs->enable_default_reply_to = 
585             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_default_reply_to));
586         g_free(prefs->default_reply_to);
587         prefs->default_reply_to = 
588             gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_default_reply_to), 0, -1);
589
590         prefs->enable_simplify_subject =
591             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_simplify_subject));
592         prefs->simplify_subject_regexp = 
593             gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_simplify_subject), 0, -1);
594         
595         if (dialog->item == dialog->folderview->summaryview->folder_item &&
596             (prefs->enable_simplify_subject != old_simplify_val ||  
597             0 != strcmp2(prefs->simplify_subject_regexp, old_simplify_str))) {
598                 summary_clear_all(dialog->folderview->summaryview);
599                 dialog->folderview->opened = NULL;
600                 dialog->folderview->selected = NULL;
601                 folderview_select(dialog->folderview, dialog->item);
602         }
603
604         if (old_simplify_str) g_free(old_simplify_str);
605
606         prefs->enable_folder_chmod = 
607             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_folder_chmod));
608         buf = gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_folder_chmod), 0, -1);
609         prefs->folder_chmod = prefs_folder_item_chmod_mode(buf);
610         g_free(buf);
611
612         prefs->enable_default_account = 
613             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_enable_default_account));
614         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(dialog->optmenu_default_account));
615         menuitem = gtk_menu_get_active(GTK_MENU(menu));
616         prefs->default_account = GPOINTER_TO_INT(gtk_object_get_user_data(GTK_OBJECT(menuitem)));
617
618         prefs->color = dialog->item->prefs->color;
619         /* update folder view */
620         if (prefs->color > 0)
621                 folder_item_update(dialog->item, F_ITEM_UPDATE_MSGCNT);
622
623         prefs_folder_item_save_config(dialog->item);
624         prefs_folder_item_destroy(dialog);
625 }
626
627 gint prefs_folder_item_chmod_mode(gchar *folder_chmod) 
628 {
629         gint newmode = 0;
630         gchar *tmp;
631
632         if (folder_chmod) {
633                 newmode = strtol(folder_chmod, &tmp, 8);
634                 if (!(*(folder_chmod) && !(*tmp)))
635                         newmode = 0;
636         }
637
638         return newmode;
639 }
640
641 static void set_button_color(guint rgbvalue, GtkWidget *button)
642 {
643         GtkStyle *newstyle;
644         GdkColor gdk_color;
645
646         gtkut_convert_int_to_gdk_color(rgbvalue, &gdk_color);
647         newstyle = gtk_style_copy(gtk_widget_get_default_style());
648         newstyle->bg[GTK_STATE_NORMAL]   = gdk_color;
649         newstyle->bg[GTK_STATE_PRELIGHT] = gdk_color;
650         newstyle->bg[GTK_STATE_ACTIVE]   = gdk_color;
651         gtk_widget_set_style(GTK_WIDGET(button), newstyle);
652 }
653
654 static void folder_color_set_dialog(GtkWidget *widget, gpointer data)
655 {
656         struct PrefsFolderItemDialog *folder_dialog = data;
657         GtkColorSelectionDialog *dialog;
658         gdouble color[4] = {0.0, 0.0, 0.0, 0.0};
659         guint rgbcolor;
660
661         color_dialog = gtk_color_selection_dialog_new(_("Pick color for folder"));
662         gtk_window_set_position(GTK_WINDOW(color_dialog), GTK_WIN_POS_CENTER);
663         gtk_window_set_modal(GTK_WINDOW(color_dialog), TRUE);
664         gtk_window_set_policy(GTK_WINDOW(color_dialog), FALSE, FALSE, FALSE);
665         manage_window_set_transient(GTK_WINDOW(color_dialog));
666
667         gtk_signal_connect(GTK_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->ok_button),
668                            "clicked", GTK_SIGNAL_FUNC(folder_color_set_dialog_ok), data);
669         gtk_signal_connect(GTK_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->cancel_button),
670                            "clicked", GTK_SIGNAL_FUNC(folder_color_set_dialog_cancel), data);
671         gtk_signal_connect(GTK_OBJECT(color_dialog), "key_press_event",
672                            GTK_SIGNAL_FUNC(folder_color_set_dialog_key_pressed),
673                            data);
674
675         rgbcolor = folder_dialog->item->prefs->color;
676         color[0] = (gdouble) ((rgbcolor & 0xff0000) >> 16) / 255.0;
677         color[1] = (gdouble) ((rgbcolor & 0x00ff00) >>  8) / 255.0;
678         color[2] = (gdouble)  (rgbcolor & 0x0000ff)        / 255.0;
679
680         dialog = GTK_COLOR_SELECTION_DIALOG(color_dialog);
681         gtk_color_selection_set_color(GTK_COLOR_SELECTION(dialog->colorsel), color);
682
683         gtk_widget_show(color_dialog);
684 }
685
686 static void folder_color_set_dialog_ok(GtkWidget *widget, gpointer data)
687 {
688         struct PrefsFolderItemDialog *folder_dialog = data;
689         GtkColorSelection *colorsel = (GtkColorSelection *)
690                                 ((GtkColorSelectionDialog *) color_dialog)->colorsel;
691         gdouble color[4];
692         guint red, green, blue, rgbvalue;
693
694         gtk_color_selection_get_color(colorsel, color);
695
696         red      = (guint) (color[0] * 255.0);
697         green    = (guint) (color[1] * 255.0);
698         blue     = (guint) (color[2] * 255.0);
699         rgbvalue = (guint) ((red * 0x10000) | (green * 0x100) | blue);
700
701         folder_dialog->item->prefs->color = rgbvalue;
702         set_button_color(rgbvalue, folder_dialog->folder_color_btn);
703
704         gtk_widget_destroy(color_dialog);
705 }
706
707 static void folder_color_set_dialog_cancel(GtkWidget *widget, gpointer data)
708 {
709         gtk_widget_destroy(color_dialog);
710 }
711
712 static void folder_color_set_dialog_key_pressed(GtkWidget *widget,
713                                                 GdkEventKey *event,
714                                                 gpointer data)
715 {
716         gtk_widget_destroy(color_dialog);
717 }
718
719 void prefs_folder_item_copy_prefs(FolderItem * src, FolderItem * dest)
720 {
721         GSList *tmp_prop_list = NULL, *tmp_scor_list = NULL, *tmp;
722         prefs_folder_item_read_config(src);
723
724         tmp_prefs.directory                     = g_strdup(src->prefs->directory);
725         tmp_prefs.sort_by_number                = src->prefs->sort_by_number;
726         tmp_prefs.sort_by_size                  = src->prefs->sort_by_size;
727         tmp_prefs.sort_by_date                  = src->prefs->sort_by_date;
728         tmp_prefs.sort_by_from                  = src->prefs->sort_by_from;
729         tmp_prefs.sort_by_subject               = src->prefs->sort_by_subject;
730         tmp_prefs.sort_by_score                 = src->prefs->sort_by_score;
731         tmp_prefs.sort_descending               = src->prefs->sort_descending;
732         tmp_prefs.enable_thread                 = src->prefs->enable_thread;
733         tmp_prefs.kill_score                    = src->prefs->kill_score;
734         tmp_prefs.important_score               = src->prefs->important_score;
735
736         prefs_matcher_read_config();
737         for (tmp = src->prefs->scoring; tmp != NULL && tmp->data != NULL;) {
738                 ScoringProp *prop = (ScoringProp *)tmp->data;
739                 
740                 tmp_scor_list = g_slist_append(tmp_scor_list,
741                                            scoringprop_copy(prop));
742                 tmp = tmp->next;
743         }
744         tmp_prefs.scoring                       = tmp_scor_list;
745
746         for (tmp = src->prefs->processing; tmp != NULL && tmp->data != NULL;) {
747                 FilteringProp *prop = (FilteringProp *)tmp->data;
748                 
749                 tmp_prop_list = g_slist_append(tmp_prop_list,
750                                            filteringprop_copy(prop));
751                 tmp = tmp->next;
752         }
753         tmp_prefs.processing                    = tmp_prop_list;
754         
755         tmp_prefs.request_return_receipt        = src->prefs->request_return_receipt;
756         tmp_prefs.enable_default_to             = src->prefs->enable_default_to;
757         tmp_prefs.default_to                    = g_strdup(src->prefs->default_to);
758         tmp_prefs.enable_default_reply_to       = src->prefs->enable_default_reply_to;
759         tmp_prefs.default_reply_to              = src->prefs->default_reply_to;
760         tmp_prefs.enable_simplify_subject       = src->prefs->enable_simplify_subject;
761         tmp_prefs.simplify_subject_regexp       = g_strdup(src->prefs->simplify_subject_regexp);
762         tmp_prefs.enable_folder_chmod           = src->prefs->enable_folder_chmod;
763         tmp_prefs.folder_chmod                  = src->prefs->folder_chmod;
764         tmp_prefs.enable_default_account        = src->prefs->enable_default_account;
765         tmp_prefs.default_account               = src->prefs->default_account;
766         tmp_prefs.save_copy_to_folder           = src->prefs->save_copy_to_folder;
767         tmp_prefs.color                         = src->prefs->color;
768
769         *dest->prefs = tmp_prefs;
770         prefs_folder_item_save_config(dest);
771 }