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