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