more plumbing in Hoa's code (can't wait to have all these settings moved into folderl...
[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         if (prefs->default_to) 
184                 g_free(prefs->default_to);
185         if (prefs->scoring != NULL)
186                 prefs_scoring_free(prefs->scoring);
187         g_free(prefs);
188 }
189
190 gint prefs_folder_item_get_sort_mode(FolderItem * item)
191 {
192         tmp_prefs = * item->prefs;
193
194         if (tmp_prefs.sort_by_number)
195                 return SORT_BY_NUMBER;
196         if (tmp_prefs.sort_by_size)
197                 return SORT_BY_SIZE;
198         if (tmp_prefs.sort_by_date)
199                 return SORT_BY_DATE;
200         if (tmp_prefs.sort_by_from)
201                 return SORT_BY_FROM;
202         if (tmp_prefs.sort_by_subject)
203                 return SORT_BY_SUBJECT;
204         if (tmp_prefs.sort_by_score)
205                 return SORT_BY_SCORE;
206         return SORT_BY_NONE;
207 }
208
209 gint prefs_folder_item_get_sort_type(FolderItem * item)
210 {
211         tmp_prefs = * item->prefs;
212
213         if (tmp_prefs.sort_descending)
214                 return GTK_SORT_DESCENDING;
215         else
216                 return GTK_SORT_ASCENDING;
217 }
218
219 #define SAFE_STRING(str) \
220         (str) ? (str) : ""
221
222 void prefs_folder_item_create(FolderItem *item) {
223         struct PrefsFolderItemDialog *dialog;
224         GtkWidget *window;
225         GtkWidget *vbox;
226         GtkWidget *ok_btn;
227         GtkWidget *cancel_btn;
228         GtkWidget *confirm_area;
229         GtkWidget *hbox;
230         
231         GtkWidget *checkbtn_request_return_receipt;
232         GtkWidget *checkbtn_default_to;
233         GtkWidget *entry_default_to;
234         GtkWidget *checkbtn_folder_chmod;
235         GtkWidget *entry_folder_chmod;
236
237         dialog = g_new0(struct PrefsFolderItemDialog, 1);
238         dialog->item = item;
239
240         /* Window */
241         window = gtk_window_new (GTK_WINDOW_DIALOG);
242         gtk_window_set_title (GTK_WINDOW(window),
243                               _("Folder Property"));
244         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
245         gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
246         gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE);
247         gtk_signal_connect (GTK_OBJECT(window), "delete_event",
248                             GTK_SIGNAL_FUNC(prefs_folder_item_delete_cb), dialog);
249         gtk_signal_connect (GTK_OBJECT(window), "focus_in_event",
250                             GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
251         gtk_signal_connect (GTK_OBJECT(window), "focus_out_event",
252                             GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
253
254         vbox = gtk_vbox_new (FALSE, 6);
255         gtk_widget_show(vbox);
256         gtk_container_add(GTK_CONTAINER (window), vbox);
257
258         /* Ok and Cancle Buttons */
259         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
260                                 &cancel_btn, _("Cancel"), NULL, NULL);
261         gtk_widget_show(confirm_area);
262         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
263         gtk_widget_grab_default(ok_btn);
264         gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
265                             GTK_SIGNAL_FUNC(prefs_folder_item_ok_cb), dialog);
266         gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
267                             GTK_SIGNAL_FUNC(prefs_folder_item_cancel_cb), dialog);
268
269         /* Request Return Receipt */
270         PACK_CHECK_BUTTON(vbox, checkbtn_request_return_receipt,
271                            _("Request Return Receipt"));
272         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_request_return_receipt),
273                                      item->ret_rcpt ? TRUE : FALSE);
274
275         /* Default To */
276         hbox = gtk_hbox_new(FALSE, 8);
277         gtk_widget_show(hbox);
278         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
279
280         PACK_CHECK_BUTTON(hbox, checkbtn_default_to,
281                            _("Default To: "));
282         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_default_to), item->prefs->enable_default_to);
283         gtk_signal_connect(GTK_OBJECT(checkbtn_default_to), "toggled",
284                             GTK_SIGNAL_FUNC(prefs_folder_item_default_to_cb), dialog);
285
286         entry_default_to = gtk_entry_new();
287         gtk_widget_show(entry_default_to);
288         gtk_box_pack_start(GTK_BOX(hbox), entry_default_to, FALSE, FALSE, 0);
289         gtk_editable_set_editable(GTK_EDITABLE(entry_default_to), item->prefs->enable_default_to);
290         gtk_entry_set_text(GTK_ENTRY(entry_default_to), SAFE_STRING(item->prefs->default_to));
291
292         /* Folder chmod */
293         hbox = gtk_hbox_new(FALSE, 8);
294         gtk_widget_show(hbox);
295         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
296
297         PACK_CHECK_BUTTON(hbox, checkbtn_folder_chmod,
298                            _("Folder chmod: "));
299         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_folder_chmod), item->prefs->enable_folder_chmod);
300         gtk_signal_connect(GTK_OBJECT(checkbtn_folder_chmod), "toggled",
301                             GTK_SIGNAL_FUNC(prefs_folder_item_folder_chmod_cb), dialog);
302
303         entry_folder_chmod = gtk_entry_new();
304         gtk_widget_show(entry_folder_chmod);
305         gtk_box_pack_start(GTK_BOX(hbox), entry_folder_chmod, FALSE, FALSE, 0);
306         gtk_editable_set_editable(GTK_EDITABLE(entry_folder_chmod), item->prefs->enable_folder_chmod);
307         if (item->prefs->folder_chmod) {
308                 gint tmp;
309                 gint mult;
310                 gint count = 0;
311                 char buf[64]; /* plenty enough for an integer */
312
313                 tmp = item->prefs->folder_chmod;
314                 while (tmp && (count < sizeof(buf) - 1)) {
315                         mult = tmp / 8;
316                         buf[count++] = '0' + (tmp - mult * 8);
317                         tmp /= 8;
318                 }
319                 buf[count] = '\0';
320                 gtk_entry_set_text(GTK_ENTRY(entry_folder_chmod), buf);
321         }
322
323         dialog->window = window;
324         dialog->checkbtn_request_return_receipt = checkbtn_request_return_receipt;
325         dialog->checkbtn_default_to = checkbtn_default_to;
326         dialog->entry_default_to = entry_default_to;
327         dialog->checkbtn_folder_chmod = checkbtn_folder_chmod;
328         dialog->entry_folder_chmod = entry_folder_chmod;
329
330         gtk_widget_show(window);
331 }
332
333 void prefs_folder_item_destroy(struct PrefsFolderItemDialog *dialog) {
334         gtk_widget_destroy(dialog->window);
335         g_free(dialog);
336 }
337
338 void prefs_folder_item_cancel_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
339         prefs_folder_item_destroy(dialog);
340 }
341
342 void prefs_folder_item_delete_cb(GtkWidget *widget, GdkEventAny *event, struct PrefsFolderItemDialog *dialog) {
343         prefs_folder_item_destroy(dialog);
344 }
345
346 void prefs_folder_item_ok_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
347         PrefsFolderItem *prefs = dialog->item->prefs;
348
349         prefs->request_return_receipt = 
350             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_request_return_receipt));
351         /* MIGRATION */    
352         dialog->item->ret_rcpt = prefs->request_return_receipt;
353             
354         prefs->enable_default_to = 
355             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_default_to));
356         g_free(prefs->default_to);
357         prefs->default_to = 
358             gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_default_to), 0, -1);
359         prefs->enable_folder_chmod = 
360             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_folder_chmod));
361         prefs->folder_chmod = prefs_folder_item_chmod_mode(
362                 gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_folder_chmod),
363                                        0, -1));
364
365         prefs_folder_item_save_config(dialog->item);
366         prefs_folder_item_destroy(dialog);
367 }
368
369 void prefs_folder_item_default_to_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
370         gtk_editable_set_editable(GTK_EDITABLE(dialog->entry_default_to),
371             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_default_to)));
372 }
373
374 void prefs_folder_item_folder_chmod_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
375         gtk_editable_set_editable(GTK_EDITABLE(dialog->entry_folder_chmod),
376             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_folder_chmod)));
377 }
378
379 gint prefs_folder_item_chmod_mode(gchar *folder_chmod) {
380         gint newmode = 0;
381         gchar *tmp;
382
383         if (folder_chmod) {
384                 newmode = strtol(folder_chmod, &tmp, 8);
385                 if (!(*(folder_chmod) && !(*tmp)))
386                         newmode = 0;
387         }
388
389         return newmode;
390 }