6190fb8c6a4966578a55d562515fe858a4ef2402
[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 "menu.h"
30 #include "account.h"
31 #include "prefs.h"
32 #include "manage_window.h"
33
34 PrefsFolderItem tmp_prefs;
35
36 struct PrefsFolderItemDialog
37 {
38         FolderItem *item;
39         GtkWidget *window;
40         GtkWidget *checkbtn_request_return_receipt;
41         GtkWidget *checkbtn_default_to;
42         GtkWidget *entry_default_to;
43         GtkWidget *checkbtn_folder_chmod;
44         GtkWidget *entry_folder_chmod;
45         GtkWidget *checkbtn_enable_default_account;
46         GtkWidget *optmenu_default_account;
47 };
48
49 static PrefParam param[] = {
50         {"sort_by_number", "FALSE", &tmp_prefs.sort_by_number, P_BOOL,
51          NULL, NULL, NULL},
52         {"sort_by_size", "FALSE", &tmp_prefs.sort_by_size, P_BOOL,
53          NULL, NULL, NULL},
54         {"sort_by_date", "FALSE", &tmp_prefs.sort_by_date, P_BOOL,
55          NULL, NULL, NULL},
56         {"sort_by_from", "FALSE", &tmp_prefs.sort_by_from, P_BOOL,
57          NULL, NULL, NULL},
58         {"sort_by_subject", "FALSE", &tmp_prefs.sort_by_subject, P_BOOL,
59          NULL, NULL, NULL},
60         {"sort_by_score", "FALSE", &tmp_prefs.sort_by_score, P_BOOL,
61          NULL, NULL, NULL},
62         {"sort_descending", "FALSE", &tmp_prefs.sort_descending, P_BOOL,
63          NULL, NULL, NULL},
64         /*{"enable_thread", "TRUE", &tmp_prefs.enable_thread, P_BOOL,
65          NULL, NULL, NULL},*/
66         {"kill_score", "-9999", &tmp_prefs.kill_score, P_INT,
67          NULL, NULL, NULL},
68         {"important_score", "1", &tmp_prefs.important_score, P_INT,
69          NULL, NULL, NULL},
70         /* MIGRATION */  
71         {"request_return_receipt", "", &tmp_prefs.request_return_receipt, P_BOOL,
72          NULL, NULL, NULL},
73         {"enable_default_to", "", &tmp_prefs.enable_default_to, P_BOOL,
74          NULL, NULL, NULL},
75         {"default_to", "", &tmp_prefs.default_to, P_STRING,
76          NULL, NULL, NULL},
77         {"enable_folder_chmod", "", &tmp_prefs.enable_folder_chmod, P_BOOL,
78          NULL, NULL, NULL},
79         {"folder_chmod", "", &tmp_prefs.folder_chmod, P_INT,
80          NULL, NULL, NULL},
81         {"enable_default_account", "", &tmp_prefs.enable_default_account, P_BOOL,
82          NULL, NULL, NULL},
83         {"default_account", NULL, &tmp_prefs.default_account, P_INT,
84          NULL, NULL, NULL},
85         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
86 };
87
88 void prefs_folder_item_delete_cb(GtkWidget *widget, GdkEventAny *event, struct PrefsFolderItemDialog *dialog);
89 void prefs_folder_item_cancel_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog);
90 void prefs_folder_item_ok_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog);
91 void prefs_folder_item_default_to_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog);
92 void prefs_folder_item_folder_chmod_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog);
93 void prefs_folder_item_default_account_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog);
94 gint prefs_folder_item_chmod_mode(gchar *folder_chmod);
95
96 void prefs_folder_item_read_config(FolderItem * item)
97 {
98         gchar * id;
99
100         id = folder_item_get_identifier(item);
101
102         prefs_read_config(param, id, FOLDERITEM_RC);
103         g_free(id);
104
105         *item->prefs = tmp_prefs;
106
107         /* MIGRATION */
108         item->ret_rcpt = tmp_prefs.request_return_receipt ? TRUE : FALSE; 
109 }
110
111 void prefs_folder_item_save_config(FolderItem * item)
112 {       
113         gchar * id;
114
115         tmp_prefs = * item->prefs;
116
117         id = folder_item_get_identifier(item);
118
119         prefs_save_config(param, id, FOLDERITEM_RC);
120         g_free(id);
121
122         /* MIGRATION: make sure migrated items are not saved
123          */
124 }
125
126 void prefs_folder_item_set_config(FolderItem * item,
127                                   int sort_type, gint sort_mode)
128 {
129         tmp_prefs = * item->prefs;
130
131         tmp_prefs.sort_by_number = FALSE;
132         tmp_prefs.sort_by_size = FALSE;
133         tmp_prefs.sort_by_date = FALSE;
134         tmp_prefs.sort_by_from = FALSE;
135         tmp_prefs.sort_by_subject = FALSE;
136         tmp_prefs.sort_by_score = FALSE;
137
138         switch (sort_mode) {
139         case SORT_BY_NUMBER:
140                 tmp_prefs.sort_by_number = TRUE;
141                 break;
142         case SORT_BY_SIZE:
143                 tmp_prefs.sort_by_size = TRUE;
144                 break;
145         case SORT_BY_DATE:
146                 tmp_prefs.sort_by_date = TRUE;
147                 break;
148         case SORT_BY_FROM:
149                 tmp_prefs.sort_by_from = TRUE;
150                 break;
151         case SORT_BY_SUBJECT:
152                 tmp_prefs.sort_by_subject = TRUE;
153                 break;
154         case SORT_BY_SCORE:
155                 tmp_prefs.sort_by_score = TRUE;
156                 break;
157         }
158         tmp_prefs.sort_descending = (sort_type == GTK_SORT_DESCENDING);
159
160         * item->prefs = tmp_prefs;
161 }
162
163 PrefsFolderItem * prefs_folder_item_new(void)
164 {
165         PrefsFolderItem * prefs;
166
167         prefs = g_new0(PrefsFolderItem, 1);
168
169         tmp_prefs.sort_by_number = FALSE;
170         tmp_prefs.sort_by_size = FALSE;
171         tmp_prefs.sort_by_date = FALSE;
172         tmp_prefs.sort_by_from = FALSE;
173         tmp_prefs.sort_by_subject = FALSE;
174         tmp_prefs.sort_by_score = FALSE;
175         tmp_prefs.sort_descending = FALSE;
176         tmp_prefs.kill_score = -9999;
177         tmp_prefs.important_score = 9999;
178
179         tmp_prefs.request_return_receipt = FALSE;
180         tmp_prefs.enable_default_to = FALSE;
181         tmp_prefs.default_to = NULL;
182         tmp_prefs.enable_folder_chmod = FALSE;
183         tmp_prefs.folder_chmod = 0;
184         tmp_prefs.enable_default_account = FALSE;
185         tmp_prefs.default_account = 0;
186
187         * prefs = tmp_prefs;
188         
189         return prefs;
190 }
191
192 void prefs_folder_item_free(PrefsFolderItem * prefs)
193 {
194         if (prefs->default_to) 
195                 g_free(prefs->default_to);
196         if (prefs->scoring != NULL)
197                 prefs_scoring_free(prefs->scoring);
198         g_free(prefs);
199 }
200
201 gint prefs_folder_item_get_sort_mode(FolderItem * item)
202 {
203         tmp_prefs = * item->prefs;
204
205         if (tmp_prefs.sort_by_number)
206                 return SORT_BY_NUMBER;
207         if (tmp_prefs.sort_by_size)
208                 return SORT_BY_SIZE;
209         if (tmp_prefs.sort_by_date)
210                 return SORT_BY_DATE;
211         if (tmp_prefs.sort_by_from)
212                 return SORT_BY_FROM;
213         if (tmp_prefs.sort_by_subject)
214                 return SORT_BY_SUBJECT;
215         if (tmp_prefs.sort_by_score)
216                 return SORT_BY_SCORE;
217         return SORT_BY_NONE;
218 }
219
220 gint prefs_folder_item_get_sort_type(FolderItem * item)
221 {
222         tmp_prefs = * item->prefs;
223
224         if (tmp_prefs.sort_descending)
225                 return GTK_SORT_DESCENDING;
226         else
227                 return GTK_SORT_ASCENDING;
228 }
229
230 #define SAFE_STRING(str) \
231         (str) ? (str) : ""
232
233 void prefs_folder_item_create(FolderItem *item) {
234         struct PrefsFolderItemDialog *dialog;
235         guint rowcount;
236         gchar *folder_identifier, *infotext;
237
238         GtkWidget *window;
239         GtkWidget *table;
240         GtkWidget *infolabel;
241         GtkWidget *ok_btn;
242         GtkWidget *cancel_btn;
243         GtkWidget *confirm_area;
244         
245         GtkWidget *checkbtn_request_return_receipt;
246         GtkWidget *checkbtn_default_to;
247         GtkWidget *entry_default_to;
248         GtkWidget *checkbtn_folder_chmod;
249         GtkWidget *entry_folder_chmod;
250         GtkWidget *checkbtn_enable_default_account;
251         GtkWidget *optmenu_default_account;
252         GtkWidget *optmenu_default_account_menu;
253         GtkWidget *optmenu_default_account_menuitem;
254         GList *cur_ac;
255         GList *account_list;
256         PrefsAccount *ac_prefs;
257         GtkOptionMenu *optmenu;
258         GtkWidget *menu;
259         GtkWidget *menuitem;
260         gint account_index, index;
261
262         dialog = g_new0(struct PrefsFolderItemDialog, 1);
263         dialog->item = item;
264
265         /* Window */
266         window = gtk_window_new (GTK_WINDOW_DIALOG);
267         gtk_window_set_title (GTK_WINDOW(window),
268                               _("Folder Property"));
269         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
270         gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
271         gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE);
272         gtk_signal_connect (GTK_OBJECT(window), "delete_event",
273                             GTK_SIGNAL_FUNC(prefs_folder_item_delete_cb), dialog);
274         gtk_signal_connect (GTK_OBJECT(window), "focus_in_event",
275                             GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
276         gtk_signal_connect (GTK_OBJECT(window), "focus_out_event",
277                             GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
278
279         /* Table */
280         table = gtk_table_new(4, 2, FALSE);
281         gtk_widget_show(table);
282         gtk_table_set_row_spacings(GTK_TABLE(table), VSPACING_NARROW);
283         gtk_container_add(GTK_CONTAINER (window), table);
284         rowcount = 0;
285
286         /* Label */
287         folder_identifier = folder_item_get_identifier(item);
288         infotext = g_strconcat(_("Folder Property for "), folder_identifier, NULL);
289         infolabel = gtk_label_new(infotext);
290         gtk_table_attach(GTK_TABLE(table), infolabel, 0, 2, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
291         gtk_widget_show(infolabel);
292         rowcount++;
293
294         /* Request Return Receipt */
295         checkbtn_request_return_receipt = gtk_check_button_new_with_label(_("Request Return Receipt"));
296         gtk_widget_show(checkbtn_request_return_receipt);
297         gtk_table_attach(GTK_TABLE(table), checkbtn_request_return_receipt, 0, 2, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
298         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_request_return_receipt),
299                                      item->ret_rcpt ? TRUE : FALSE);
300
301         rowcount++;
302
303         /* Default To */
304         checkbtn_default_to = gtk_check_button_new_with_label(_("Default To: "));
305         gtk_widget_show(checkbtn_default_to);
306         gtk_table_attach(GTK_TABLE(table), checkbtn_default_to, 0, 1, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
307         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_default_to), item->prefs->enable_default_to);
308         gtk_signal_connect(GTK_OBJECT(checkbtn_default_to), "toggled",
309                             GTK_SIGNAL_FUNC(prefs_folder_item_default_to_cb), dialog);
310
311         entry_default_to = gtk_entry_new();
312         gtk_widget_show(entry_default_to);
313         gtk_table_attach_defaults(GTK_TABLE(table), entry_default_to, 1, 2, rowcount, rowcount + 1);
314         gtk_editable_set_editable(GTK_EDITABLE(entry_default_to), item->prefs->enable_default_to);
315         gtk_entry_set_text(GTK_ENTRY(entry_default_to), SAFE_STRING(item->prefs->default_to));
316         address_completion_register_entry(GTK_ENTRY(entry_default_to));
317
318         rowcount++;
319
320         /* Folder chmod */
321         checkbtn_folder_chmod = gtk_check_button_new_with_label(_("Folder chmod: "));
322         gtk_widget_show(checkbtn_folder_chmod);
323         gtk_table_attach(GTK_TABLE(table), checkbtn_folder_chmod, 0, 1, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
324
325         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_folder_chmod), item->prefs->enable_folder_chmod);
326         gtk_signal_connect(GTK_OBJECT(checkbtn_folder_chmod), "toggled",
327                             GTK_SIGNAL_FUNC(prefs_folder_item_folder_chmod_cb), dialog);
328
329         entry_folder_chmod = gtk_entry_new();
330         gtk_widget_show(entry_folder_chmod);
331         gtk_table_attach_defaults(GTK_TABLE(table), entry_folder_chmod, 1, 2, rowcount, rowcount + 1);
332         gtk_editable_set_editable(GTK_EDITABLE(entry_folder_chmod), item->prefs->enable_folder_chmod);
333         if (item->prefs->folder_chmod) {
334                 gchar *buf;
335
336                 buf = g_strdup_printf("%o", item->prefs->folder_chmod);
337                 gtk_entry_set_text(GTK_ENTRY(entry_folder_chmod), buf);
338                 g_free(buf);
339         }
340         
341         rowcount++;
342
343         /* Default account */
344         checkbtn_enable_default_account = gtk_check_button_new_with_label(_("Default account: "));
345         gtk_widget_show(checkbtn_enable_default_account);
346         gtk_table_attach(GTK_TABLE(table), checkbtn_enable_default_account, 0, 1, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
347         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_enable_default_account), item->prefs->enable_default_account);
348         gtk_signal_connect(GTK_OBJECT(checkbtn_enable_default_account), "toggled",
349                             GTK_SIGNAL_FUNC(prefs_folder_item_default_account_cb), dialog);
350
351         optmenu_default_account = gtk_option_menu_new ();
352         gtk_widget_show (optmenu_default_account);
353         gtk_table_attach_defaults(GTK_TABLE(table), optmenu_default_account, 1, 2, rowcount, rowcount + 1);
354         optmenu_default_account_menu = gtk_menu_new ();
355
356         account_list = account_get_list();
357         account_index = 0;
358         index = 0;
359         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
360                 ac_prefs = (PrefsAccount *)cur_ac->data;
361                 MENUITEM_ADD (optmenu_default_account_menu, optmenu_default_account_menuitem,
362                                         ac_prefs->account_name?ac_prefs->account_name : _("Untitled"),
363                                         ac_prefs->account_id);
364                 /* get the index for menu's set_history (sad method?) */
365                 if (ac_prefs->account_id == item->prefs->default_account)
366                         account_index = index;
367                 index++;                        
368         }
369
370         dialog->item->prefs->default_account=item->prefs->default_account;
371
372         optmenu=GTK_OPTION_MENU(optmenu_default_account);
373         gtk_option_menu_set_menu(optmenu, optmenu_default_account_menu);
374
375         gtk_option_menu_set_history(optmenu, account_index);
376
377         menu = gtk_option_menu_get_menu(optmenu);
378         menuitem = gtk_menu_get_active(GTK_MENU(menu));
379         gtk_menu_item_activate(GTK_MENU_ITEM(menuitem));
380
381         gtk_widget_set_sensitive(optmenu_default_account, item->prefs->enable_default_account);
382
383         rowcount++;
384
385         /* Ok and Cancle Buttons */
386         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
387                                 &cancel_btn, _("Cancel"), NULL, NULL);
388         gtk_widget_show(confirm_area);
389         gtk_table_attach_defaults(GTK_TABLE(table), confirm_area, 0, 2, rowcount, rowcount + 1);
390         gtk_widget_grab_default(ok_btn);
391         gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
392                             GTK_SIGNAL_FUNC(prefs_folder_item_ok_cb), dialog);
393         gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
394                             GTK_SIGNAL_FUNC(prefs_folder_item_cancel_cb), dialog);
395
396         dialog->window = window;
397         dialog->checkbtn_request_return_receipt = checkbtn_request_return_receipt;
398         dialog->checkbtn_default_to = checkbtn_default_to;
399         dialog->entry_default_to = entry_default_to;
400         dialog->checkbtn_folder_chmod = checkbtn_folder_chmod;
401         dialog->entry_folder_chmod = entry_folder_chmod;
402         dialog->checkbtn_enable_default_account = checkbtn_enable_default_account;
403         dialog->optmenu_default_account = optmenu_default_account;
404
405         g_free(infotext);
406
407         gtk_widget_show(window);
408 }
409
410 void prefs_folder_item_destroy(struct PrefsFolderItemDialog *dialog) {
411         gtk_widget_destroy(dialog->window);
412         g_free(dialog);
413 }
414
415 void prefs_folder_item_cancel_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
416         prefs_folder_item_destroy(dialog);
417 }
418
419 void prefs_folder_item_delete_cb(GtkWidget *widget, GdkEventAny *event, struct PrefsFolderItemDialog *dialog) {
420         prefs_folder_item_destroy(dialog);
421 }
422
423 void prefs_folder_item_ok_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
424         gchar *buf;
425         PrefsFolderItem *prefs = dialog->item->prefs;
426         GtkWidget *menu;
427         GtkWidget *menuitem;
428
429         prefs->request_return_receipt = 
430             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_request_return_receipt));
431         /* MIGRATION */    
432         dialog->item->ret_rcpt = prefs->request_return_receipt;
433
434         prefs->enable_default_to = 
435             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_default_to));
436         g_free(prefs->default_to);
437         prefs->default_to = 
438             gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_default_to), 0, -1);
439         prefs->enable_folder_chmod = 
440             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_folder_chmod));
441         buf = gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_folder_chmod), 0, -1);
442         prefs->folder_chmod = prefs_folder_item_chmod_mode(buf);
443         g_free(buf);
444
445         prefs->enable_default_account = 
446             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_enable_default_account));
447         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(dialog->optmenu_default_account));
448         menuitem = gtk_menu_get_active(GTK_MENU(menu));
449         prefs->default_account = GPOINTER_TO_INT(gtk_object_get_user_data(GTK_OBJECT(menuitem)));
450
451         prefs_folder_item_save_config(dialog->item);
452         prefs_folder_item_destroy(dialog);
453 }
454
455 void prefs_folder_item_default_to_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
456         gtk_editable_set_editable(GTK_EDITABLE(dialog->entry_default_to),
457             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_default_to)));
458 }
459
460 void prefs_folder_item_folder_chmod_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
461         gtk_editable_set_editable(GTK_EDITABLE(dialog->entry_folder_chmod),
462             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_folder_chmod)));
463 }
464
465 gint prefs_folder_item_chmod_mode(gchar *folder_chmod) {
466         gint newmode = 0;
467         gchar *tmp;
468
469         if (folder_chmod) {
470                 newmode = strtol(folder_chmod, &tmp, 8);
471                 if (!(*(folder_chmod) && !(*tmp)))
472                         newmode = 0;
473         }
474
475         return newmode;
476 }
477 void prefs_folder_item_default_account_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
478         gtk_widget_set_sensitive(dialog->optmenu_default_account,
479             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_enable_default_account)));
480 }