preferences for folder items
[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 #include "intl.h"
21 #include "defs.h"
22 #include "folder.h"
23 #include "prefs_folder_item.h"
24 #include "summaryview.h"
25 #include "prefs.h"
26 #include "manage_window.h"
27
28 PrefsFolderItem tmp_prefs;
29
30 struct PrefsFolderItemDialog
31 {
32         FolderItem *item;
33         GtkWidget *window;
34         GtkWidget *checkbtn_request_return_receipt;
35         GtkWidget *checkbtn_default_to;
36         GtkWidget *entry_default_to;
37 };
38
39 static PrefParam param[] = {
40         {"sort_by_number", "FALSE", &tmp_prefs.sort_by_number, P_BOOL,
41          NULL, NULL, NULL},
42         {"sort_by_size", "FALSE", &tmp_prefs.sort_by_size, P_BOOL,
43          NULL, NULL, NULL},
44         {"sort_by_date", "FALSE", &tmp_prefs.sort_by_date, P_BOOL,
45          NULL, NULL, NULL},
46         {"sort_by_from", "FALSE", &tmp_prefs.sort_by_from, P_BOOL,
47          NULL, NULL, NULL},
48         {"sort_by_subject", "FALSE", &tmp_prefs.sort_by_subject, P_BOOL,
49          NULL, NULL, NULL},
50         {"sort_by_score", "FALSE", &tmp_prefs.sort_by_score, P_BOOL,
51          NULL, NULL, NULL},
52         {"sort_descending", "FALSE", &tmp_prefs.sort_descending, P_BOOL,
53          NULL, NULL, NULL},
54         {"enable_thread", "TRUE", &tmp_prefs.enable_thread, P_BOOL,
55          NULL, NULL, NULL},
56         {"kill_score", "-9999", &tmp_prefs.kill_score, P_INT,
57          NULL, NULL, NULL},
58         {"important_score", "1", &tmp_prefs.important_score, P_INT,
59          NULL, NULL, NULL},
60         {"request_return_receipt", "", &tmp_prefs.request_return_receipt, P_BOOL,
61          NULL, NULL, NULL},
62         {"enable_default_to", "", &tmp_prefs.enable_default_to, P_BOOL,
63          NULL, NULL, NULL},
64         {"default_to", "", &tmp_prefs.default_to, P_STRING,
65          NULL, NULL, NULL},
66         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
67 };
68
69 void prefs_folder_item_delete_cb(GtkWidget *widget, GdkEventAny *event, struct PrefsFolderItemDialog *dialog);
70 void prefs_folder_item_cancel_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog);
71 void prefs_folder_item_ok_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog);
72 void prefs_folder_item_default_to_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog);
73
74 void prefs_folder_item_read_config(FolderItem * item)
75 {
76         gchar * id;
77
78         id = folder_item_get_identifier(item);
79
80         prefs_read_config(param, id, FOLDERITEM_RC);
81         g_free(id);
82
83         * item->prefs = tmp_prefs;
84 }
85
86 void prefs_folder_item_save_config(FolderItem * item)
87 {       
88         gchar * id;
89
90         tmp_prefs = * item->prefs;
91
92         id = folder_item_get_identifier(item);
93
94         prefs_save_config(param, id, FOLDERITEM_RC);
95         g_free(id);
96 }
97
98 void prefs_folder_item_set_config(FolderItem * item,
99                                   int sort_type, gint sort_mode)
100 {
101         tmp_prefs = * item->prefs;
102
103         tmp_prefs.sort_by_number = FALSE;
104         tmp_prefs.sort_by_size = FALSE;
105         tmp_prefs.sort_by_date = FALSE;
106         tmp_prefs.sort_by_from = FALSE;
107         tmp_prefs.sort_by_subject = FALSE;
108         tmp_prefs.sort_by_score = FALSE;
109
110         switch (sort_mode) {
111         case SORT_BY_NUMBER:
112                 tmp_prefs.sort_by_number = TRUE;
113                 break;
114         case SORT_BY_SIZE:
115                 tmp_prefs.sort_by_size = TRUE;
116                 break;
117         case SORT_BY_DATE:
118                 tmp_prefs.sort_by_date = TRUE;
119                 break;
120         case SORT_BY_FROM:
121                 tmp_prefs.sort_by_from = TRUE;
122                 break;
123         case SORT_BY_SUBJECT:
124                 tmp_prefs.sort_by_subject = TRUE;
125                 break;
126         case SORT_BY_SCORE:
127                 tmp_prefs.sort_by_score = TRUE;
128                 break;
129         }
130         tmp_prefs.sort_descending = (sort_type == GTK_SORT_DESCENDING);
131
132         * item->prefs = tmp_prefs;
133 }
134
135 PrefsFolderItem * prefs_folder_item_new(void)
136 {
137         PrefsFolderItem * prefs;
138
139         prefs = g_new0(PrefsFolderItem, 1);
140
141         tmp_prefs.sort_by_number = FALSE;
142         tmp_prefs.sort_by_size = FALSE;
143         tmp_prefs.sort_by_date = FALSE;
144         tmp_prefs.sort_by_from = FALSE;
145         tmp_prefs.sort_by_subject = FALSE;
146         tmp_prefs.sort_by_score = FALSE;
147         tmp_prefs.sort_descending = FALSE;
148         tmp_prefs.kill_score = -9999;
149         tmp_prefs.important_score = 9999;
150
151         tmp_prefs.request_return_receipt = FALSE;
152         tmp_prefs.enable_default_to = FALSE;
153         tmp_prefs.default_to = NULL;
154
155         * prefs = tmp_prefs;
156         
157         return prefs;
158 }
159
160 void prefs_folder_item_free(PrefsFolderItem * prefs)
161 {
162         g_free(prefs->default_to);
163         if (prefs->scoring != NULL)
164                 prefs_scoring_free(prefs->scoring);
165         g_free(prefs);
166 }
167
168 gint prefs_folder_item_get_sort_mode(FolderItem * item)
169 {
170         tmp_prefs = * item->prefs;
171
172         if (tmp_prefs.sort_by_number)
173                 return SORT_BY_NUMBER;
174         if (tmp_prefs.sort_by_size)
175                 return SORT_BY_SIZE;
176         if (tmp_prefs.sort_by_date)
177                 return SORT_BY_DATE;
178         if (tmp_prefs.sort_by_from)
179                 return SORT_BY_FROM;
180         if (tmp_prefs.sort_by_subject)
181                 return SORT_BY_SUBJECT;
182         if (tmp_prefs.sort_by_score)
183                 return SORT_BY_SCORE;
184         return SORT_BY_NONE;
185 }
186
187 gint prefs_folder_item_get_sort_type(FolderItem * item)
188 {
189         tmp_prefs = * item->prefs;
190
191         if (tmp_prefs.sort_descending)
192                 return GTK_SORT_DESCENDING;
193         else
194                 return GTK_SORT_ASCENDING;
195 }
196
197 void prefs_folder_item_create(FolderItem *item) {
198         struct PrefsFolderItemDialog *dialog;
199         GtkWidget *window;
200         GtkWidget *vbox;
201         GtkWidget *ok_btn;
202         GtkWidget *cancel_btn;
203         GtkWidget *confirm_area;
204         GtkWidget *hbox;
205         
206         GtkWidget *checkbtn_request_return_receipt;
207         GtkWidget *checkbtn_default_to;
208         GtkWidget *entry_default_to;
209
210         dialog = g_new0(struct PrefsFolderItemDialog, 1);
211         dialog->item = item;
212
213         /* Window */
214         window = gtk_window_new (GTK_WINDOW_DIALOG);
215         gtk_window_set_title (GTK_WINDOW(window),
216                               _("Folder Property"));
217         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
218         gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
219         gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE);
220         gtk_signal_connect (GTK_OBJECT(window), "delete_event",
221                             GTK_SIGNAL_FUNC(prefs_folder_item_delete_cb), dialog);
222         gtk_signal_connect (GTK_OBJECT(window), "focus_in_event",
223                             GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
224         gtk_signal_connect (GTK_OBJECT(window), "focus_out_event",
225                             GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
226
227         vbox = gtk_vbox_new (FALSE, 6);
228         gtk_widget_show(vbox);
229         gtk_container_add(GTK_CONTAINER (window), vbox);
230
231         /* Ok and Cancle Buttons */
232         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
233                                 &cancel_btn, _("Cancel"), NULL, NULL);
234         gtk_widget_show(confirm_area);
235         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
236         gtk_widget_grab_default(ok_btn);
237         gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
238                             GTK_SIGNAL_FUNC(prefs_folder_item_ok_cb), dialog);
239         gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
240                             GTK_SIGNAL_FUNC(prefs_folder_item_cancel_cb), dialog);
241
242         /* Request Return Receipt */
243         PACK_CHECK_BUTTON(vbox, checkbtn_request_return_receipt,
244                            _("Request Return Receipt"));
245         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_request_return_receipt),
246             item->prefs->request_return_receipt);
247
248         /* Default To */
249         hbox = gtk_hbox_new(FALSE, 8);
250         gtk_widget_show(hbox);
251         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
252
253         PACK_CHECK_BUTTON(hbox, checkbtn_default_to,
254                            _("Default To: "));
255         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_default_to), item->prefs->enable_default_to);
256         gtk_signal_connect(GTK_OBJECT(checkbtn_default_to), "toggled",
257                             GTK_SIGNAL_FUNC(prefs_folder_item_default_to_cb), dialog);
258
259         entry_default_to = gtk_entry_new();
260         gtk_widget_show(entry_default_to);
261         gtk_box_pack_start(GTK_BOX(hbox), entry_default_to, FALSE, FALSE, 0);
262         gtk_editable_set_editable(GTK_EDITABLE(entry_default_to), item->prefs->enable_default_to);
263         gtk_entry_set_text(GTK_ENTRY(entry_default_to), item->prefs->default_to);
264
265         dialog->window = window;
266         dialog->checkbtn_request_return_receipt = checkbtn_request_return_receipt;
267         dialog->checkbtn_default_to = checkbtn_default_to;
268         dialog->entry_default_to = entry_default_to;
269
270         gtk_widget_show(window);
271 }
272
273 void prefs_folder_item_destroy(struct PrefsFolderItemDialog *dialog) {
274         gtk_widget_destroy(dialog->window);
275         g_free(dialog);
276 }
277
278 void prefs_folder_item_cancel_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
279         prefs_folder_item_destroy(dialog);
280 }
281
282 void prefs_folder_item_delete_cb(GtkWidget *widget, GdkEventAny *event, struct PrefsFolderItemDialog *dialog) {
283         prefs_folder_item_destroy(dialog);
284 }
285
286 void prefs_folder_item_ok_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
287         PrefsFolderItem *prefs = dialog->item->prefs;
288
289         prefs->request_return_receipt = 
290             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_request_return_receipt));
291         prefs->enable_default_to = 
292             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_default_to));
293         g_free(prefs->default_to);
294         prefs->default_to = 
295             gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_default_to), 0, -1);
296
297         prefs_folder_item_save_config(dialog->item);
298         prefs_folder_item_destroy(dialog);
299 }
300
301 void prefs_folder_item_default_to_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
302         gtk_editable_set_editable(GTK_EDITABLE(dialog->entry_default_to),
303             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_default_to)));
304 }