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