3fdcab1ecf84bcad682f6912de39046e1c067afc
[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         /* create style for folder color */
166         if (tmp_prefs.color > 0) {
167                 GdkColor gdk_color;
168
169                 gtkut_convert_int_to_gdk_color(tmp_prefs.color, &gdk_color);
170                 item->color_style = gtk_style_copy(gtk_widget_get_default_style());
171                 item->color_style->fg[GTK_STATE_NORMAL] = gdk_color;
172         }
173 }
174
175 void prefs_folder_item_save_config(FolderItem * item)
176 {       
177         gchar * id;
178
179         tmp_prefs = * item->prefs;
180
181         id = folder_item_get_identifier(item);
182
183         prefs_save_config(param, id, FOLDERITEM_RC);
184         g_free(id);
185
186         /* MIGRATION: make sure migrated items are not saved
187          */
188 }
189
190 void prefs_folder_item_set_config(FolderItem * item,
191                                   int sort_type, gint sort_mode)
192 {
193         g_assert(item);
194         g_warning("prefs_folder_item_set_config() should never be called\n");
195         item->sort_key  = sort_type;
196         item->sort_type = sort_mode;
197 }
198
199 PrefsFolderItem * prefs_folder_item_new(void)
200 {
201         PrefsFolderItem * prefs;
202
203         prefs = g_new0(PrefsFolderItem, 1);
204
205         tmp_prefs.sort_by_number = FALSE;
206         tmp_prefs.sort_by_size = FALSE;
207         tmp_prefs.sort_by_date = FALSE;
208         tmp_prefs.sort_by_from = FALSE;
209         tmp_prefs.sort_by_subject = FALSE;
210         tmp_prefs.sort_by_score = FALSE;
211         tmp_prefs.sort_descending = FALSE;
212         tmp_prefs.kill_score = -9999;
213         tmp_prefs.important_score = 9999;
214
215         tmp_prefs.request_return_receipt = FALSE;
216         tmp_prefs.enable_default_to = FALSE;
217         tmp_prefs.default_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->scoring != NULL)
240                 prefs_scoring_free(prefs->scoring);
241         g_free(prefs);
242 }
243
244 gint prefs_folder_item_get_sort_mode(FolderItem * item)
245 {
246         g_assert(item != NULL);
247         g_warning("prefs_folder_item_get_sort_mode() should never be called\n");
248         return item->sort_key;
249 }
250
251 gint prefs_folder_item_get_sort_type(FolderItem * item)
252 {
253         g_assert(item != NULL);
254         g_warning("prefs_folder_item_get_sort_type() should never be called\n");
255         return item->sort_type;
256 }
257
258 #define SAFE_STRING(str) \
259         (str) ? (str) : ""
260
261 void prefs_folder_item_create(void *folderview, FolderItem *item) 
262 {
263         struct PrefsFolderItemDialog *dialog;
264         guint rowcount;
265         gchar *folder_identifier, *infotext;
266
267         GtkWidget *window;
268         GtkWidget *table;
269         GtkWidget *infolabel;
270         GtkWidget *ok_btn;
271         GtkWidget *cancel_btn;
272         GtkWidget *confirm_area;
273         
274         GtkWidget *checkbtn_request_return_receipt;
275         GtkWidget *checkbtn_save_copy_to_folder;
276         GtkWidget *checkbtn_default_to;
277         GtkWidget *entry_default_to;
278         GtkWidget *checkbtn_simplify_subject;
279         GtkWidget *entry_simplify_subject;
280         GtkWidget *checkbtn_folder_chmod;
281         GtkWidget *entry_folder_chmod;
282         GtkWidget *checkbtn_enable_default_account;
283         GtkWidget *optmenu_default_account;
284         GtkWidget *optmenu_default_account_menu;
285         GtkWidget *optmenu_default_account_menuitem;
286         GtkWidget *folder_color;
287         GtkWidget *folder_color_btn;
288         GList *cur_ac;
289         GList *account_list;
290         PrefsAccount *ac_prefs;
291         GtkOptionMenu *optmenu;
292         GtkWidget *menu;
293         GtkWidget *menuitem;
294         gint account_index, index;
295
296         dialog = g_new0(struct PrefsFolderItemDialog, 1);
297         dialog->folderview = folderview;
298         dialog->item       = item;
299
300         /* Window */
301         window = gtk_window_new (GTK_WINDOW_DIALOG);
302         gtk_window_set_title (GTK_WINDOW(window),
303                               _("Folder Property"));
304         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
305         gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
306         gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE);
307         gtk_signal_connect (GTK_OBJECT(window), "delete_event",
308                             GTK_SIGNAL_FUNC(prefs_folder_item_delete_cb), dialog);
309         MANAGE_WINDOW_SIGNALS_CONNECT (window);
310
311         /* Table */
312         table = gtk_table_new(8, 2, FALSE);
313         gtk_widget_show(table);
314         gtk_table_set_row_spacings(GTK_TABLE(table), VSPACING_NARROW);
315         gtk_container_add(GTK_CONTAINER (window), table);
316         rowcount = 0;
317
318         /* Label */
319         folder_identifier = folder_item_get_identifier(item);
320         infotext = g_strconcat(_("Folder Property for "), folder_identifier, NULL);
321         infolabel = gtk_label_new(infotext);
322         gtk_table_attach(GTK_TABLE(table), infolabel, 0, 2, rowcount, 
323                          rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
324         gtk_widget_show(infolabel);
325         rowcount++;
326
327         /* Request Return Receipt */
328         checkbtn_request_return_receipt = gtk_check_button_new_with_label
329                 (_("Request Return Receipt"));
330         gtk_widget_show(checkbtn_request_return_receipt);
331         gtk_table_attach(GTK_TABLE(table), checkbtn_request_return_receipt, 
332                          0, 2, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, 
333                          GTK_SHRINK, 0, 0);
334         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_request_return_receipt),
335                                      item->ret_rcpt ? TRUE : FALSE);
336
337         rowcount++;
338
339         /* Save Copy to Folder */
340         checkbtn_save_copy_to_folder = gtk_check_button_new_with_label
341                 (_("Save copy of outgoing messages to this folder instead of Sent"));
342         gtk_widget_show(checkbtn_save_copy_to_folder);
343         gtk_table_attach(GTK_TABLE(table), checkbtn_save_copy_to_folder, 0, 2, 
344                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
345         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_save_copy_to_folder),
346                                      item->prefs->save_copy_to_folder ? TRUE : FALSE);
347
348         rowcount++;
349
350         /* Default To */
351         checkbtn_default_to = gtk_check_button_new_with_label(_("Default To: "));
352         gtk_widget_show(checkbtn_default_to);
353         gtk_table_attach(GTK_TABLE(table), checkbtn_default_to, 0, 1, 
354                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
355         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_default_to), 
356                                      item->prefs->enable_default_to);
357
358         entry_default_to = gtk_entry_new();
359         gtk_widget_show(entry_default_to);
360         gtk_table_attach_defaults(GTK_TABLE(table), entry_default_to, 1, 2, rowcount, rowcount + 1);
361         SET_TOGGLE_SENSITIVITY(checkbtn_default_to, entry_default_to);
362         gtk_entry_set_text(GTK_ENTRY(entry_default_to), SAFE_STRING(item->prefs->default_to));
363         address_completion_register_entry(GTK_ENTRY(entry_default_to));
364
365         rowcount++;
366
367         /* Simplify Subject */
368         checkbtn_simplify_subject = gtk_check_button_new_with_label(_("Simplify Subject RegExp: "));
369         gtk_widget_show(checkbtn_simplify_subject);
370         gtk_table_attach(GTK_TABLE(table), checkbtn_simplify_subject, 0, 1, 
371                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
372         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_simplify_subject), 
373                                      item->prefs->enable_simplify_subject);
374
375         entry_simplify_subject = gtk_entry_new();
376         gtk_widget_show(entry_simplify_subject);
377         gtk_table_attach_defaults(GTK_TABLE(table), entry_simplify_subject, 1, 2, 
378                                   rowcount, rowcount + 1);
379         SET_TOGGLE_SENSITIVITY(checkbtn_simplify_subject, entry_simplify_subject);
380         gtk_entry_set_text(GTK_ENTRY(entry_simplify_subject), 
381                            SAFE_STRING(item->prefs->simplify_subject_regexp));
382
383         rowcount++;
384
385         /* Folder chmod */
386         checkbtn_folder_chmod = gtk_check_button_new_with_label(_("Folder chmod: "));
387         gtk_widget_show(checkbtn_folder_chmod);
388         gtk_table_attach(GTK_TABLE(table), checkbtn_folder_chmod, 0, 1, 
389                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
390
391         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_folder_chmod), 
392                                      item->prefs->enable_folder_chmod);
393
394         entry_folder_chmod = gtk_entry_new();
395         gtk_widget_show(entry_folder_chmod);
396         gtk_table_attach_defaults(GTK_TABLE(table), entry_folder_chmod, 1, 2, 
397                                   rowcount, rowcount + 1);
398         SET_TOGGLE_SENSITIVITY(checkbtn_folder_chmod, entry_folder_chmod);
399         if (item->prefs->folder_chmod) {
400                 gchar *buf;
401
402                 buf = g_strdup_printf("%o", item->prefs->folder_chmod);
403                 gtk_entry_set_text(GTK_ENTRY(entry_folder_chmod), buf);
404                 g_free(buf);
405         }
406         
407         rowcount++;
408
409         /* Default account */
410         checkbtn_enable_default_account = gtk_check_button_new_with_label(_("Default account: "));
411         gtk_widget_show(checkbtn_enable_default_account);
412         gtk_table_attach(GTK_TABLE(table), checkbtn_enable_default_account, 0, 1, 
413                          rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
414         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_enable_default_account), 
415                                      item->prefs->enable_default_account);
416
417         optmenu_default_account = gtk_option_menu_new ();
418         gtk_widget_show (optmenu_default_account);
419         gtk_table_attach_defaults(GTK_TABLE(table), optmenu_default_account, 1, 2, 
420                                   rowcount, rowcount + 1);
421         optmenu_default_account_menu = gtk_menu_new ();
422
423         account_list = account_get_list();
424         account_index = 0;
425         index = 0;
426         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
427                 ac_prefs = (PrefsAccount *)cur_ac->data;
428                 MENUITEM_ADD (optmenu_default_account_menu, optmenu_default_account_menuitem,
429                                         ac_prefs->account_name?ac_prefs->account_name : _("Untitled"),
430                                         ac_prefs->account_id);
431                 /* get the index for menu's set_history (sad method?) */
432                 if (ac_prefs->account_id == item->prefs->default_account)
433                         account_index = index;
434                 index++;                        
435         }
436
437         dialog->item->prefs->default_account = item->prefs->default_account;
438
439         optmenu=GTK_OPTION_MENU(optmenu_default_account);
440         gtk_option_menu_set_menu(optmenu, optmenu_default_account_menu);
441
442         gtk_option_menu_set_history(optmenu, account_index);
443
444         menu = gtk_option_menu_get_menu(optmenu);
445         menuitem = gtk_menu_get_active(GTK_MENU(menu));
446         gtk_menu_item_activate(GTK_MENU_ITEM(menuitem));
447
448         SET_TOGGLE_SENSITIVITY(checkbtn_enable_default_account, optmenu_default_account);
449
450         rowcount++;
451
452         /* Folder color */
453         folder_color = gtk_label_new(_("Folder color"));
454         gtk_misc_set_alignment(GTK_MISC(folder_color), 0, 0.5);
455         gtk_widget_show(folder_color);
456         gtk_table_attach_defaults(GTK_TABLE(table), folder_color, 0, 1, 
457                          rowcount, rowcount + 1);
458
459         folder_color_btn = gtk_button_new_with_label("");
460         gtk_widget_set_usize(folder_color_btn, 36, 26);
461         gtk_container_set_border_width(GTK_CONTAINER(folder_color_btn), 2);
462         gtk_widget_show(folder_color_btn);
463         gtk_table_attach(GTK_TABLE(table), folder_color_btn,
464                          1, 2, rowcount, rowcount + 1,
465                          GTK_SHRINK, 0, 0, 0);
466
467         dialog->item->prefs->color = item->prefs->color;
468
469         gtk_signal_connect(GTK_OBJECT(folder_color_btn), "clicked",
470                            GTK_SIGNAL_FUNC(folder_color_set_dialog),
471                            dialog);
472
473         set_button_color(item->prefs->color, folder_color_btn);
474
475         rowcount++;
476
477         /* Ok and Cancle Buttons */
478         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
479                                 &cancel_btn, _("Cancel"), NULL, NULL);
480         gtk_widget_show(confirm_area);
481         gtk_table_attach_defaults(GTK_TABLE(table), confirm_area, 0, 2, 
482                                   rowcount, rowcount + 1);
483         gtk_widget_grab_default(ok_btn);
484         gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
485                             GTK_SIGNAL_FUNC(prefs_folder_item_ok_cb), dialog);
486         gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
487                             GTK_SIGNAL_FUNC(prefs_folder_item_cancel_cb), dialog);
488
489         dialog->window = window;
490         dialog->checkbtn_request_return_receipt = checkbtn_request_return_receipt;
491         dialog->checkbtn_save_copy_to_folder = checkbtn_save_copy_to_folder;
492         dialog->checkbtn_default_to = checkbtn_default_to;
493         dialog->entry_default_to = entry_default_to;
494         dialog->checkbtn_simplify_subject = checkbtn_simplify_subject;
495         dialog->entry_simplify_subject = entry_simplify_subject;
496         dialog->checkbtn_folder_chmod = checkbtn_folder_chmod;
497         dialog->entry_folder_chmod = entry_folder_chmod;
498         dialog->checkbtn_enable_default_account = checkbtn_enable_default_account;
499         dialog->optmenu_default_account = optmenu_default_account;
500         dialog->folder_color = folder_color;
501         dialog->folder_color_btn = folder_color_btn;
502
503         g_free(infotext);
504
505         address_completion_start(window);
506
507         gtk_widget_show(window);
508 }
509
510 void prefs_folder_item_destroy(struct PrefsFolderItemDialog *dialog) 
511 {
512         address_completion_unregister_entry(GTK_ENTRY(dialog->entry_default_to));
513         address_completion_end(dialog->window);
514         gtk_widget_destroy(dialog->window);
515         g_free(dialog);
516 }
517
518 void prefs_folder_item_cancel_cb(GtkWidget *widget, 
519                                  struct PrefsFolderItemDialog *dialog) 
520 {
521         prefs_folder_item_destroy(dialog);
522 }
523
524 void prefs_folder_item_delete_cb(GtkWidget *widget, GdkEventAny *event, 
525                                  struct PrefsFolderItemDialog *dialog) 
526 {
527         prefs_folder_item_destroy(dialog);
528 }
529
530 void prefs_folder_item_ok_cb(GtkWidget *widget, 
531                              struct PrefsFolderItemDialog *dialog) 
532 {
533         gchar *buf;
534         PrefsFolderItem *prefs = dialog->item->prefs;
535         GtkWidget *menu;
536         GtkWidget *menuitem;
537         gboolean   old_simplify_val;
538         gchar     *old_simplify_str;
539
540         g_return_if_fail(prefs != NULL);
541
542         old_simplify_val = prefs->enable_simplify_subject;
543         old_simplify_str = prefs->simplify_subject_regexp;
544
545         prefs->request_return_receipt = 
546             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_request_return_receipt));
547         /* MIGRATION */    
548         dialog->item->ret_rcpt = prefs->request_return_receipt;
549
550         prefs->save_copy_to_folder = 
551             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_save_copy_to_folder));
552
553         prefs->enable_default_to = 
554             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_default_to));
555         g_free(prefs->default_to);
556         prefs->default_to = 
557             gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_default_to), 0, -1);
558
559         prefs->enable_simplify_subject =
560             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_simplify_subject));
561         prefs->simplify_subject_regexp = 
562             gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_simplify_subject), 0, -1);
563         
564         if (dialog->item == dialog->folderview->summaryview->folder_item &&
565             (prefs->enable_simplify_subject != old_simplify_val ||  
566             0 != strcmp2(prefs->simplify_subject_regexp, old_simplify_str))) 
567                 summary_show(dialog->folderview->summaryview, dialog->item);
568                 
569         if (old_simplify_str) g_free(old_simplify_str);
570
571         prefs->enable_folder_chmod = 
572             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_folder_chmod));
573         buf = gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_folder_chmod), 0, -1);
574         prefs->folder_chmod = prefs_folder_item_chmod_mode(buf);
575         g_free(buf);
576
577         prefs->enable_default_account = 
578             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_enable_default_account));
579         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(dialog->optmenu_default_account));
580         menuitem = gtk_menu_get_active(GTK_MENU(menu));
581         prefs->default_account = GPOINTER_TO_INT(gtk_object_get_user_data(GTK_OBJECT(menuitem)));
582
583         prefs->color = dialog->item->prefs->color;
584         if (prefs->color > 0) {
585                 GdkColor gdk_color;
586
587                 dialog->item->color_style = gtk_style_copy(gtk_widget_get_default_style());
588                 dialog->item->color_style->fg[GTK_STATE_NORMAL] = gdk_color;
589
590                 /* update folder view */
591                 folderview_update_item(dialog->item, FALSE);
592         }
593
594         prefs_folder_item_save_config(dialog->item);
595         prefs_folder_item_destroy(dialog);
596 }
597
598 gint prefs_folder_item_chmod_mode(gchar *folder_chmod) 
599 {
600         gint newmode = 0;
601         gchar *tmp;
602
603         if (folder_chmod) {
604                 newmode = strtol(folder_chmod, &tmp, 8);
605                 if (!(*(folder_chmod) && !(*tmp)))
606                         newmode = 0;
607         }
608
609         return newmode;
610 }
611
612 static void set_button_color(guint rgbvalue, GtkWidget *button)
613 {
614         GtkStyle *newstyle;
615         GdkColor gdk_color;
616
617         gtkut_convert_int_to_gdk_color(rgbvalue, &gdk_color);
618         newstyle = gtk_style_copy(gtk_widget_get_default_style());
619         newstyle->bg[GTK_STATE_NORMAL]   = gdk_color;
620         newstyle->bg[GTK_STATE_PRELIGHT] = gdk_color;
621         newstyle->bg[GTK_STATE_ACTIVE]   = gdk_color;
622         gtk_widget_set_style(GTK_WIDGET(button), newstyle);
623 }
624
625 static void folder_color_set_dialog(GtkWidget *widget, gpointer data)
626 {
627         struct PrefsFolderItemDialog *folder_dialog = data;
628         GtkColorSelectionDialog *dialog;
629         gdouble color[4] = {0.0, 0.0, 0.0, 0.0};
630         guint rgbcolor;
631
632         color_dialog = gtk_color_selection_dialog_new(_("Pick color for folder"));
633         gtk_window_set_position(GTK_WINDOW(color_dialog), GTK_WIN_POS_CENTER);
634         gtk_window_set_modal(GTK_WINDOW(color_dialog), TRUE);
635         gtk_window_set_policy(GTK_WINDOW(color_dialog), FALSE, FALSE, FALSE);
636         manage_window_set_transient(GTK_WINDOW(color_dialog));
637
638         gtk_signal_connect(GTK_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->ok_button),
639                            "clicked", GTK_SIGNAL_FUNC(folder_color_set_dialog_ok), data);
640         gtk_signal_connect(GTK_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->cancel_button),
641                            "clicked", GTK_SIGNAL_FUNC(folder_color_set_dialog_cancel), data);
642         gtk_signal_connect(GTK_OBJECT(color_dialog), "key_press_event",
643                            GTK_SIGNAL_FUNC(folder_color_set_dialog_key_pressed),
644                            data);
645
646         rgbcolor = folder_dialog->item->prefs->color;
647         color[0] = (gdouble) ((rgbcolor & 0xff0000) >> 16) / 255.0;
648         color[1] = (gdouble) ((rgbcolor & 0x00ff00) >>  8) / 255.0;
649         color[2] = (gdouble)  (rgbcolor & 0x0000ff)        / 255.0;
650
651         dialog = GTK_COLOR_SELECTION_DIALOG(color_dialog);
652         gtk_color_selection_set_color(GTK_COLOR_SELECTION(dialog->colorsel), color);
653
654         gtk_widget_show(color_dialog);
655 }
656
657 static void folder_color_set_dialog_ok(GtkWidget *widget, gpointer data)
658 {
659         struct PrefsFolderItemDialog *folder_dialog = data;
660         GtkColorSelection *colorsel = (GtkColorSelection *)
661                                 ((GtkColorSelectionDialog *) color_dialog)->colorsel;
662         gdouble color[4];
663         guint red, green, blue, rgbvalue;
664
665         gtk_color_selection_get_color(colorsel, color);
666
667         red      = (guint) (color[0] * 255.0);
668         green    = (guint) (color[1] * 255.0);
669         blue     = (guint) (color[2] * 255.0);
670         rgbvalue = (guint) ((red * 0x10000) | (green * 0x100) | blue);
671
672         folder_dialog->item->prefs->color = rgbvalue;
673         set_button_color(rgbvalue, folder_dialog->folder_color_btn);
674
675         gtk_widget_destroy(color_dialog);
676 }
677
678 static void folder_color_set_dialog_cancel(GtkWidget *widget, gpointer data)
679 {
680         gtk_widget_destroy(color_dialog);
681 }
682
683 static void folder_color_set_dialog_key_pressed(GtkWidget *widget,
684                                                 GdkEventKey *event,
685                                                 gpointer data)
686 {
687         gtk_widget_destroy(color_dialog);
688 }
689