Fixed wrong german translation
[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
237         GtkWidget *window;
238         GtkWidget *table;
239         GtkWidget *ok_btn;
240         GtkWidget *cancel_btn;
241         GtkWidget *confirm_area;
242         GtkWidget *hbox;
243         
244         GtkWidget *checkbtn_request_return_receipt;
245         GtkWidget *checkbtn_default_to;
246         GtkWidget *entry_default_to;
247         GtkWidget *checkbtn_folder_chmod;
248         GtkWidget *entry_folder_chmod;
249         GtkWidget *hbox2;
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         dialog = g_new0(struct PrefsFolderItemDialog, 1);
262         dialog->item = item;
263
264         /* Window */
265         window = gtk_window_new (GTK_WINDOW_DIALOG);
266         gtk_window_set_title (GTK_WINDOW(window),
267                               _("Folder Property"));
268         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
269         gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
270         gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE);
271         gtk_signal_connect (GTK_OBJECT(window), "delete_event",
272                             GTK_SIGNAL_FUNC(prefs_folder_item_delete_cb), dialog);
273         gtk_signal_connect (GTK_OBJECT(window), "focus_in_event",
274                             GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
275         gtk_signal_connect (GTK_OBJECT(window), "focus_out_event",
276                             GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
277
278         /* Table */
279         table = gtk_table_new(4, 2, FALSE);
280         gtk_widget_show(table);
281         gtk_table_set_row_spacings(GTK_TABLE(table), VSPACING_NARROW);
282         gtk_container_add(GTK_CONTAINER (window), table);
283         rowcount = 0;
284
285         /* Request Return Receipt */
286         checkbtn_request_return_receipt = gtk_check_button_new_with_label(_("Request Return Receipt"));
287         gtk_widget_show(checkbtn_request_return_receipt);
288         gtk_table_attach(GTK_TABLE(table), checkbtn_request_return_receipt, 0, 2, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
289         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_request_return_receipt),
290                                      item->ret_rcpt ? TRUE : FALSE);
291
292         rowcount++;
293
294         /* Default To */
295         checkbtn_default_to = gtk_check_button_new_with_label(_("Default To: "));
296         gtk_widget_show(checkbtn_default_to);
297         gtk_table_attach(GTK_TABLE(table), checkbtn_default_to, 0, 1, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
298         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_default_to), item->prefs->enable_default_to);
299         gtk_signal_connect(GTK_OBJECT(checkbtn_default_to), "toggled",
300                             GTK_SIGNAL_FUNC(prefs_folder_item_default_to_cb), dialog);
301
302         entry_default_to = gtk_entry_new();
303         gtk_widget_show(entry_default_to);
304         gtk_table_attach_defaults(GTK_TABLE(table), entry_default_to, 1, 2, rowcount, rowcount + 1);
305         gtk_editable_set_editable(GTK_EDITABLE(entry_default_to), item->prefs->enable_default_to);
306         gtk_entry_set_text(GTK_ENTRY(entry_default_to), SAFE_STRING(item->prefs->default_to));
307         address_completion_register_entry(GTK_ENTRY(entry_default_to));
308
309         rowcount++;
310
311         /* Folder chmod */
312         checkbtn_folder_chmod = gtk_check_button_new_with_label(_("Folder chmod: "));
313         gtk_widget_show(checkbtn_folder_chmod);
314         gtk_table_attach(GTK_TABLE(table), checkbtn_folder_chmod, 0, 1, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
315
316         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_folder_chmod), item->prefs->enable_folder_chmod);
317         gtk_signal_connect(GTK_OBJECT(checkbtn_folder_chmod), "toggled",
318                             GTK_SIGNAL_FUNC(prefs_folder_item_folder_chmod_cb), dialog);
319
320         entry_folder_chmod = gtk_entry_new();
321         gtk_widget_show(entry_folder_chmod);
322         gtk_table_attach_defaults(GTK_TABLE(table), entry_folder_chmod, 1, 2, rowcount, rowcount + 1);
323         gtk_editable_set_editable(GTK_EDITABLE(entry_folder_chmod), item->prefs->enable_folder_chmod);
324         if (item->prefs->folder_chmod) {
325                 gchar *buf;
326
327                 buf = g_strdup_printf("%o", item->prefs->folder_chmod);
328                 gtk_entry_set_text(GTK_ENTRY(entry_folder_chmod), buf);
329                 g_free(buf);
330         }
331         
332         rowcount++;
333
334         /* Default account */
335         checkbtn_enable_default_account = gtk_check_button_new_with_label(_("Default account: "));
336         gtk_widget_show(checkbtn_enable_default_account);
337         gtk_table_attach(GTK_TABLE(table), checkbtn_enable_default_account, 0, 1, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
338         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_enable_default_account), item->prefs->enable_default_account);
339         gtk_signal_connect(GTK_OBJECT(checkbtn_enable_default_account), "toggled",
340                             GTK_SIGNAL_FUNC(prefs_folder_item_default_account_cb), dialog);
341
342         optmenu_default_account = gtk_option_menu_new ();
343         gtk_widget_show (optmenu_default_account);
344         gtk_table_attach_defaults(GTK_TABLE(table), optmenu_default_account, 1, 2, rowcount, rowcount + 1);
345         optmenu_default_account_menu = gtk_menu_new ();
346
347         account_list = account_get_list();
348         account_index = 0;
349         index = 0;
350         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
351                 ac_prefs = (PrefsAccount *)cur_ac->data;
352                 MENUITEM_ADD (optmenu_default_account_menu, optmenu_default_account_menuitem,
353                                         ac_prefs->account_name?ac_prefs->account_name : _("Untitled"),
354                                         ac_prefs->account_id);
355                 /* get the index for menu's set_history (sad method?) */
356                 if (ac_prefs->account_id == item->prefs->default_account)
357                         account_index = index;
358                 index++;                        
359         }
360
361         dialog->item->prefs->default_account=item->prefs->default_account;
362
363         optmenu=GTK_OPTION_MENU(optmenu_default_account);
364         gtk_option_menu_set_menu(optmenu, optmenu_default_account_menu);
365
366         gtk_option_menu_set_history(optmenu, account_index);
367
368         menu = gtk_option_menu_get_menu(optmenu);
369         menuitem = gtk_menu_get_active(GTK_MENU(menu));
370         gtk_menu_item_activate(GTK_MENU_ITEM(menuitem));
371
372         gtk_widget_set_sensitive(optmenu_default_account, item->prefs->enable_default_account);
373
374         rowcount++;
375
376         /* Ok and Cancle Buttons */
377         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
378                                 &cancel_btn, _("Cancel"), NULL, NULL);
379         gtk_widget_show(confirm_area);
380         gtk_table_attach_defaults(GTK_TABLE(table), confirm_area, 0, 2, rowcount, rowcount + 1);
381         gtk_widget_grab_default(ok_btn);
382         gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
383                             GTK_SIGNAL_FUNC(prefs_folder_item_ok_cb), dialog);
384         gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
385                             GTK_SIGNAL_FUNC(prefs_folder_item_cancel_cb), dialog);
386
387         dialog->window = window;
388         dialog->checkbtn_request_return_receipt = checkbtn_request_return_receipt;
389         dialog->checkbtn_default_to = checkbtn_default_to;
390         dialog->entry_default_to = entry_default_to;
391         dialog->checkbtn_folder_chmod = checkbtn_folder_chmod;
392         dialog->entry_folder_chmod = entry_folder_chmod;
393         dialog->checkbtn_enable_default_account = checkbtn_enable_default_account;
394         dialog->optmenu_default_account = optmenu_default_account;
395
396         gtk_widget_show(window);
397 }
398
399 void prefs_folder_item_destroy(struct PrefsFolderItemDialog *dialog) {
400         gtk_widget_destroy(dialog->window);
401         g_free(dialog);
402 }
403
404 void prefs_folder_item_cancel_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
405         prefs_folder_item_destroy(dialog);
406 }
407
408 void prefs_folder_item_delete_cb(GtkWidget *widget, GdkEventAny *event, struct PrefsFolderItemDialog *dialog) {
409         prefs_folder_item_destroy(dialog);
410 }
411
412 void prefs_folder_item_ok_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
413         gchar *buf;
414         PrefsFolderItem *prefs = dialog->item->prefs;
415         GtkWidget *menu;
416         GtkWidget *menuitem;
417
418         prefs->request_return_receipt = 
419             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_request_return_receipt));
420         /* MIGRATION */    
421         dialog->item->ret_rcpt = prefs->request_return_receipt;
422
423         prefs->enable_default_to = 
424             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_default_to));
425         g_free(prefs->default_to);
426         prefs->default_to = 
427             gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_default_to), 0, -1);
428         prefs->enable_folder_chmod = 
429             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_folder_chmod));
430         buf = gtk_editable_get_chars(GTK_EDITABLE(dialog->entry_folder_chmod), 0, -1);
431         prefs->folder_chmod = prefs_folder_item_chmod_mode(buf);
432         g_free(buf);
433
434         prefs->enable_default_account = 
435             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_enable_default_account));
436         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(dialog->optmenu_default_account));
437         menuitem = gtk_menu_get_active(GTK_MENU(menu));
438         prefs->default_account = GPOINTER_TO_INT(gtk_object_get_user_data(GTK_OBJECT(menuitem)));
439
440         prefs_folder_item_save_config(dialog->item);
441         prefs_folder_item_destroy(dialog);
442 }
443
444 void prefs_folder_item_default_to_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
445         gtk_editable_set_editable(GTK_EDITABLE(dialog->entry_default_to),
446             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_default_to)));
447 }
448
449 void prefs_folder_item_folder_chmod_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
450         gtk_editable_set_editable(GTK_EDITABLE(dialog->entry_folder_chmod),
451             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_folder_chmod)));
452 }
453
454 gint prefs_folder_item_chmod_mode(gchar *folder_chmod) {
455         gint newmode = 0;
456         gchar *tmp;
457
458         if (folder_chmod) {
459                 newmode = strtol(folder_chmod, &tmp, 8);
460                 if (!(*(folder_chmod) && !(*tmp)))
461                         newmode = 0;
462         }
463
464         return newmode;
465 }
466 void prefs_folder_item_default_account_cb(GtkWidget *widget, struct PrefsFolderItemDialog *dialog) {
467         gtk_widget_set_sensitive(dialog->optmenu_default_account,
468             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbtn_enable_default_account)));
469 }