sync with sylpheed 0.5.3 release
[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         GtkWidget *checkbtn_folder_chmod;
38         GtkWidget *entry_folder_chmod;
39 };
40
41 static PrefParam param[] = {
42         {"sort_by_number", "FALSE", &tmp_prefs.sort_by_number, P_BOOL,
43          NULL, NULL, NULL},
44         {"sort_by_size", "FALSE", &tmp_prefs.sort_by_size, P_BOOL,
45          NULL, NULL, NULL},
46         {"sort_by_date", "FALSE", &tmp_prefs.sort_by_date, P_BOOL,
47          NULL, NULL, NULL},
48         {"sort_by_from", "FALSE", &tmp_prefs.sort_by_from, P_BOOL,
49          NULL, NULL, NULL},
50         {"sort_by_subject", "FALSE", &tmp_prefs.sort_by_subject, P_BOOL,
51          NULL, NULL, NULL},
52         {"sort_by_score", "FALSE", &tmp_prefs.sort_by_score, P_BOOL,
53          NULL, NULL, NULL},
54         {"sort_descending", "FALSE", &tmp_prefs.sort_descending, P_BOOL,
55          NULL, NULL, NULL},
56         /*{"enable_thread", "TRUE", &tmp_prefs.enable_thread, P_BOOL,
57          NULL, NULL, NULL},*/
58         {"kill_score", "-9999", &tmp_prefs.kill_score, P_INT,
59          NULL, NULL, NULL},
60         {"important_score", "1", &tmp_prefs.important_score, P_INT,
61          NULL, NULL, NULL},
62         {"request_return_receipt", "", &tmp_prefs.request_return_receipt, P_BOOL,
63          NULL, NULL, NULL},
64         {"enable_default_to", "", &tmp_prefs.enable_default_to, P_BOOL,
65          NULL, NULL, NULL},
66         {"default_to", "", &tmp_prefs.default_to, P_STRING,
67          NULL, NULL, NULL},
68         {"enable_folder_chmod", "", &tmp_prefs.enable_folder_chmod, P_BOOL,
69          NULL, NULL, NULL},
70         {"folder_chmod", "", &tmp_prefs.folder_chmod, P_INT,
71          NULL, NULL, NULL},
72         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
73 };
74
75 void prefs_folder_item_delete_cb(GtkWidget *widget, GdkEventAny *event, struct PrefsFolderItemDialog *dialog);
76 void prefs_folder_item_cancel_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog);
77 void prefs_folder_item_ok_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog);
78 void prefs_folder_item_default_to_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog);
79 void prefs_folder_item_folder_chmod_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog);
80 gint prefs_folder_item_chmod_mode(gchar *folder_chmod);
81
82 void prefs_folder_item_read_config(FolderItem * item)
83 {
84         gchar * id;
85
86         id = folder_item_get_identifier(item);
87
88         prefs_read_config(param, id, FOLDERITEM_RC);
89         g_free(id);
90
91         * item->prefs = tmp_prefs;
92 }
93
94 void prefs_folder_item_save_config(FolderItem * item)
95 {       
96         gchar * id;
97
98         tmp_prefs = * item->prefs;
99
100         id = folder_item_get_identifier(item);
101
102         prefs_save_config(param, id, FOLDERITEM_RC);
103         g_free(id);
104 }
105
106 void prefs_folder_item_set_config(FolderItem * item,
107                                   int sort_type, gint sort_mode)
108 {
109         tmp_prefs = * item->prefs;
110
111         tmp_prefs.sort_by_number = FALSE;
112         tmp_prefs.sort_by_size = FALSE;
113         tmp_prefs.sort_by_date = FALSE;
114         tmp_prefs.sort_by_from = FALSE;
115         tmp_prefs.sort_by_subject = FALSE;
116         tmp_prefs.sort_by_score = FALSE;
117
118         switch (sort_mode) {
119         case SORT_BY_NUMBER:
120                 tmp_prefs.sort_by_number = TRUE;
121                 break;
122         case SORT_BY_SIZE:
123                 tmp_prefs.sort_by_size = TRUE;
124                 break;
125         case SORT_BY_DATE:
126                 tmp_prefs.sort_by_date = TRUE;
127                 break;
128         case SORT_BY_FROM:
129                 tmp_prefs.sort_by_from = TRUE;
130                 break;
131         case SORT_BY_SUBJECT:
132                 tmp_prefs.sort_by_subject = TRUE;
133                 break;
134         case SORT_BY_SCORE:
135                 tmp_prefs.sort_by_score = TRUE;
136                 break;
137         }
138         tmp_prefs.sort_descending = (sort_type == GTK_SORT_DESCENDING);
139
140         * item->prefs = tmp_prefs;
141 }
142
143 PrefsFolderItem * prefs_folder_item_new(void)
144 {
145         PrefsFolderItem * prefs;
146
147         prefs = g_new0(PrefsFolderItem, 1);
148
149         tmp_prefs.sort_by_number = FALSE;
150         tmp_prefs.sort_by_size = FALSE;
151         tmp_prefs.sort_by_date = FALSE;
152         tmp_prefs.sort_by_from = FALSE;
153         tmp_prefs.sort_by_subject = FALSE;
154         tmp_prefs.sort_by_score = FALSE;
155         tmp_prefs.sort_descending = FALSE;
156         tmp_prefs.kill_score = -9999;
157         tmp_prefs.important_score = 9999;
158
159         tmp_prefs.request_return_receipt = FALSE;
160         tmp_prefs.enable_default_to = FALSE;
161         tmp_prefs.default_to = NULL;
162         tmp_prefs.enable_folder_chmod = FALSE;
163         tmp_prefs.folder_chmod = 0;
164
165         * prefs = tmp_prefs;
166         
167         return prefs;
168 }
169
170 void prefs_folder_item_free(PrefsFolderItem * prefs)
171 {
172         g_free(prefs->default_to);
173         if (prefs->scoring != NULL)
174                 prefs_scoring_free(prefs->scoring);
175         g_free(prefs);
176 }
177
178 gint prefs_folder_item_get_sort_mode(FolderItem * item)
179 {
180         tmp_prefs = * item->prefs;
181
182         if (tmp_prefs.sort_by_number)
183                 return SORT_BY_NUMBER;
184         if (tmp_prefs.sort_by_size)
185                 return SORT_BY_SIZE;
186         if (tmp_prefs.sort_by_date)
187                 return SORT_BY_DATE;
188         if (tmp_prefs.sort_by_from)
189                 return SORT_BY_FROM;
190         if (tmp_prefs.sort_by_subject)
191                 return SORT_BY_SUBJECT;
192         if (tmp_prefs.sort_by_score)
193                 return SORT_BY_SCORE;
194         return SORT_BY_NONE;
195 }
196
197 gint prefs_folder_item_get_sort_type(FolderItem * item)
198 {
199         tmp_prefs = * item->prefs;
200
201         if (tmp_prefs.sort_descending)
202                 return GTK_SORT_DESCENDING;
203         else
204                 return GTK_SORT_ASCENDING;
205 }
206
207 void prefs_folder_item_create(FolderItem *item) {
208         struct PrefsFolderItemDialog *dialog;
209         GtkWidget *window;
210         GtkWidget *vbox;
211         GtkWidget *ok_btn;
212         GtkWidget *cancel_btn;
213         GtkWidget *confirm_area;
214         GtkWidget *hbox;
215         
216         GtkWidget *checkbtn_request_return_receipt;
217         GtkWidget *checkbtn_default_to;
218         GtkWidget *entry_default_to;
219         GtkWidget *checkbtn_folder_chmod;
220         GtkWidget *entry_folder_chmod;
221
222         dialog = g_new0(struct PrefsFolderItemDialog, 1);
223         dialog->item = item;
224
225         /* Window */
226         window = gtk_window_new (GTK_WINDOW_DIALOG);
227         gtk_window_set_title (GTK_WINDOW(window),
228                               _("Folder Property"));
229         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
230         gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
231         gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE);
232         gtk_signal_connect (GTK_OBJECT(window), "delete_event",
233                             GTK_SIGNAL_FUNC(prefs_folder_item_delete_cb), dialog);
234         gtk_signal_connect (GTK_OBJECT(window), "focus_in_event",
235                             GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
236         gtk_signal_connect (GTK_OBJECT(window), "focus_out_event",
237                             GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
238
239         vbox = gtk_vbox_new (FALSE, 6);
240         gtk_widget_show(vbox);
241         gtk_container_add(GTK_CONTAINER (window), vbox);
242
243         /* Ok and Cancle Buttons */
244         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
245                                 &cancel_btn, _("Cancel"), NULL, NULL);
246         gtk_widget_show(confirm_area);
247         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
248         gtk_widget_grab_default(ok_btn);
249         gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
250                             GTK_SIGNAL_FUNC(prefs_folder_item_ok_cb), dialog);
251         gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
252                             GTK_SIGNAL_FUNC(prefs_folder_item_cancel_cb), dialog);
253
254         /* Request Return Receipt */
255         PACK_CHECK_BUTTON(vbox, checkbtn_request_return_receipt,
256                            _("Request Return Receipt"));
257         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_request_return_receipt),
258             item->prefs->request_return_receipt);
259
260         /* Default To */
261         hbox = gtk_hbox_new(FALSE, 8);
262         gtk_widget_show(hbox);
263         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
264
265         PACK_CHECK_BUTTON(hbox, checkbtn_default_to,
266                            _("Default To: "));
267         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_default_to), item->prefs->enable_default_to);
268         gtk_signal_connect(GTK_OBJECT(checkbtn_default_to), "toggled",
269                             GTK_SIGNAL_FUNC(prefs_folder_item_default_to_cb), dialog);
270
271         entry_default_to = gtk_entry_new();
272         gtk_widget_show(entry_default_to);
273         gtk_box_pack_start(GTK_BOX(hbox), entry_default_to, FALSE, FALSE, 0);
274         gtk_editable_set_editable(GTK_EDITABLE(entry_default_to), item->prefs->enable_default_to);
275         gtk_entry_set_text(GTK_ENTRY(entry_default_to), item->prefs->default_to);
276
277         /* Folder chmod */
278         hbox = gtk_hbox_new(FALSE, 8);
279         gtk_widget_show(hbox);
280         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
281
282         PACK_CHECK_BUTTON(hbox, checkbtn_folder_chmod,
283                            _("Folder chmod: "));
284         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_folder_chmod), item->prefs->enable_folder_chmod);
285         gtk_signal_connect(GTK_OBJECT(checkbtn_folder_chmod), "toggled",
286                             GTK_SIGNAL_FUNC(prefs_folder_item_folder_chmod_cb), dialog);
287
288         entry_folder_chmod = gtk_entry_new();
289         gtk_widget_show(entry_folder_chmod);
290         gtk_box_pack_start(GTK_BOX(hbox), entry_folder_chmod, FALSE, FALSE, 0);
291         gtk_editable_set_editable(GTK_EDITABLE(entry_folder_chmod), item->prefs->enable_folder_chmod);
292         if (item->prefs->folder_chmod) {
293                 gint tmp;
294                 gint mult;
295                 gint count = 0;
296                 char buf[64]; /* plenty enough for an integer */
297
298                 tmp = item->prefs->folder_chmod;
299                 while (tmp && (count < sizeof(buf) - 1)) {
300                         mult = tmp / 8;
301                         buf[count++] = '0' + (tmp - mult * 8);
302                         tmp /= 8;
303                 }
304                 buf[count] = '\0';
305                 gtk_entry_set_text(GTK_ENTRY(entry_folder_chmod), buf);
306         }
307
308         dialog->window = window;
309         dialog->checkbtn_request_return_receipt = checkbtn_request_return_receipt;
310         dialog->checkbtn_default_to = checkbtn_default_to;
311         dialog->entry_default_to = entry_default_to;
312         dialog->checkbtn_folder_chmod = checkbtn_folder_chmod;
313         dialog->entry_folder_chmod = entry_folder_chmod;
314
315         gtk_widget_show(window);
316 }
317
318 void prefs_folder_item_destroy(struct PrefsFolderItemDialog *dialog) {
319         gtk_widget_destroy(dialog->window);
320         g_free(dialog);
321 }
322
323 void prefs_folder_item_cancel_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
324         prefs_folder_item_destroy(dialog);
325 }
326
327 void prefs_folder_item_delete_cb(GtkWidget *widget, GdkEventAny *event, struct PrefsFolderItemDialog *dialog) {
328         prefs_folder_item_destroy(dialog);
329 }
330
331 void prefs_folder_item_ok_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
332         PrefsFolderItem *prefs = dialog->item->prefs;
333
334         prefs->request_return_receipt = 
335             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_request_return_receipt));
336         prefs->enable_default_to = 
337             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_default_to));
338         g_free(prefs->default_to);
339         prefs->default_to = 
340             gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_default_to), 0, -1);
341         prefs->enable_folder_chmod = 
342             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_folder_chmod));
343         prefs->folder_chmod = prefs_folder_item_chmod_mode(
344                 gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_folder_chmod),
345                                        0, -1));
346
347         prefs_folder_item_save_config(dialog->item);
348         prefs_folder_item_destroy(dialog);
349 }
350
351 void prefs_folder_item_default_to_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
352         gtk_editable_set_editable(GTK_EDITABLE(dialog->entry_default_to),
353             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_default_to)));
354 }
355
356 void prefs_folder_item_folder_chmod_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
357         gtk_editable_set_editable(GTK_EDITABLE(dialog->entry_folder_chmod),
358             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_folder_chmod)));
359 }
360
361 gint prefs_folder_item_chmod_mode(gchar *folder_chmod) {
362         gint newmode = 0;
363         gchar *tmp;
364
365         if (folder_chmod) {
366                 newmode = strtol(folder_chmod, &tmp, 8);
367                 if (!(*(folder_chmod) && !(*tmp)))
368                         newmode = 0;
369         }
370
371         return newmode;
372 }