0.9.0claws83
[claws.git] / src / account.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gtk/gtk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <stdio.h>
30 #include <errno.h>
31
32 #include "intl.h"
33 #include "main.h"
34 #include "mainwindow.h"
35 #include "folderview.h"
36 #include "folder.h"
37 #include "account.h"
38 #include "prefs_gtk.h"
39 #include "prefs_account.h"
40 #include "prefs_folder_item.h"
41 #include "compose.h"
42 #include "manage_window.h"
43 #include "stock_pixmap.h"
44 #include "inc.h"
45 #include "gtkutils.h"
46 #include "utils.h"
47 #include "alertpanel.h"
48 #include "procheader.h"
49
50 typedef enum
51 {
52         COL_DEFAULT     = 0,
53         COL_GETALL      = 1,
54         COL_NAME        = 2,
55         COL_PROTOCOL    = 3,
56         COL_SERVER      = 4
57 } EditAccountColumnPos;
58
59 # define N_EDIT_ACCOUNT_COLS    5
60
61 #define PREFSBUFSIZE            1024
62
63 PrefsAccount *cur_account;
64
65 static GList *account_list = NULL;
66
67 static struct EditAccount {
68         GtkWidget *window;
69         GtkWidget *clist;
70         GtkWidget *close_btn;
71 } edit_account;
72
73 static GdkPixmap *markxpm;
74 static GdkBitmap *markxpmmask;
75 static GdkPixmap *checkboxonxpm;
76 static GdkPixmap *checkboxonxpmmask;
77 static GdkPixmap *checkboxoffxpm;
78 static GdkPixmap *checkboxoffxpmmask;
79
80 static void account_edit_create         (void);
81
82 static void account_edit_prefs          (void);
83 static void account_delete              (void);
84
85 static void account_up                  (void);
86 static void account_down                (void);
87
88 static void account_set_default         (void);
89
90 static void account_edit_close          (void);
91 static gint account_delete_event        (GtkWidget      *widget,
92                                          GdkEventAny    *event,
93                                          gpointer        data);
94 static void account_selected            (GtkCList       *clist,
95                                          gint            row,
96                                          gint            column,
97                                          GdkEvent       *event,
98                                          gpointer        data);
99 static void account_row_moved           (GtkCList       *clist,
100                                          gint            source_row,
101                                          gint            dest_row);
102 static void account_key_pressed         (GtkWidget      *widget,
103                                          GdkEventKey    *event,
104                                          gpointer        data);
105
106 static gint account_clist_set_row       (PrefsAccount   *ac_prefs,
107                                          gint            row);
108 static void account_clist_set           (void);
109
110 static void account_list_set            (void);
111
112 void account_read_config_all(void)
113 {
114         GSList *ac_label_list = NULL, *cur;
115         gchar *rcpath;
116         FILE *fp;
117         gchar buf[PREFSBUFSIZE];
118         PrefsAccount *ac_prefs;
119
120         debug_print("Reading all config for each account...\n");
121
122         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
123         if ((fp = fopen(rcpath, "rb")) == NULL) {
124                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
125                 g_free(rcpath);
126                 return;
127         }
128         g_free(rcpath);
129
130         while (fgets(buf, sizeof(buf), fp) != NULL) {
131                 if (!strncmp(buf, "[Account: ", 10)) {
132                         strretchomp(buf);
133                         memmove(buf, buf + 1, strlen(buf));
134                         buf[strlen(buf) - 1] = '\0';
135                         debug_print("Found label: %s\n", buf);
136                         ac_label_list = g_slist_append(ac_label_list,
137                                                        g_strdup(buf));
138                 }
139         }
140         fclose(fp);
141
142         /* read config data from file */
143         cur_account = NULL;
144         for (cur = ac_label_list; cur != NULL; cur = cur->next) {
145                 ac_prefs = prefs_account_new();
146                 prefs_account_read_config(ac_prefs, (gchar *)cur->data);
147                 account_list = g_list_append(account_list, ac_prefs);
148                 if (ac_prefs->is_default)
149                         cur_account = ac_prefs;
150         }
151         /* if default is not set, assume first account as default */
152         if (!cur_account && account_list) {
153                 ac_prefs = (PrefsAccount *)account_list->data;
154                 account_set_as_default(ac_prefs);
155                 cur_account = ac_prefs;
156         }
157
158         account_set_menu();
159         main_window_reflect_prefs_all();
160
161         while (ac_label_list) {
162                 g_free(ac_label_list->data);
163                 ac_label_list = g_slist_remove(ac_label_list,
164                                                ac_label_list->data);
165         }
166 }
167
168 void account_save_config_all(void)
169 {
170         prefs_account_save_config_all(account_list);
171 }
172
173 /*
174  * account_find_all_from_address:
175  * @ac_list: initial list of accounts. NULL to create a new one.
176  * Accounts found in the @address will be appended to this list.
177  * @address: Email address string.
178  *
179  * Find all the mail (not news) accounts within the specified address.
180  *
181  * Return value: the original accounts list with the found accounts appended.
182  */
183 GList *account_find_all_from_address(GList *ac_list, const gchar *address)
184 {
185         GList *cur;
186         PrefsAccount *ac;
187
188         if (address == NULL)
189                 return ac_list;
190
191         for (cur = account_list; cur != NULL; cur = cur->next) {
192                 ac = (PrefsAccount *)cur->data;
193                 if (ac->protocol != A_NNTP && ac->address &&
194                     strcasestr(address, ac->address) != NULL)
195                         ac_list = g_list_append(ac_list, ac);
196         }
197         return ac_list;
198 }
199         
200 PrefsAccount *account_find_from_smtp_server(const gchar *address,
201                                             const gchar *smtp_server)
202 {
203         GList *cur;
204         PrefsAccount *ac;
205
206         g_return_val_if_fail(address != NULL, NULL);
207         g_return_val_if_fail(smtp_server != NULL, NULL);
208
209         for (cur = account_list; cur != NULL; cur = cur->next) {
210                 ac = (PrefsAccount *)cur->data;
211                 if (!strcmp2(address, ac->address) &&
212                     !strcmp2(smtp_server, ac->smtp_server))
213                         return ac;
214         }
215
216         return NULL;
217 }
218
219 /*
220  * account_find_from_address:
221  * @address: Email address string.
222  *
223  * Find a mail (not news) account with the specified email address.
224  *
225  * Return value: The found account, or NULL if not found.
226  */
227 PrefsAccount *account_find_from_address(const gchar *address)
228 {
229         GList *cur;
230         PrefsAccount *ac;
231
232         g_return_val_if_fail(address != NULL, NULL);
233
234         for (cur = account_list; cur != NULL; cur = cur->next) {
235                 ac = (PrefsAccount *)cur->data;
236                 if (ac->protocol != A_NNTP && ac->address &&
237                     g_strcasecmp(address, ac->address) == 0)
238                         return ac;
239         }
240
241         return NULL;
242 }
243
244 PrefsAccount *account_find_from_id(gint id)
245 {
246         GList *cur;
247         PrefsAccount *ac;
248
249         for (cur = account_list; cur != NULL; cur = cur->next) {
250                 ac = (PrefsAccount *)cur->data;
251                 if (id == ac->account_id)
252                         return ac;
253         }
254
255         return NULL;
256 }
257
258 PrefsAccount *account_find_from_item(FolderItem *item)
259 {
260         PrefsAccount *ac;
261
262         g_return_val_if_fail(item != NULL, NULL);
263
264         ac = item->account;
265         if (!ac) {
266                 FolderItem *cur_item = item->parent;
267                 while (cur_item != NULL) {
268                         if (cur_item->account && cur_item->apply_sub) {
269                                 ac = cur_item->account;
270                                 break;
271                         }                               
272                         cur_item = cur_item->parent;
273                 }
274         }
275         if (!ac)
276                 ac = item->folder->account;
277
278         return ac;
279 }
280
281 void account_set_menu(void)
282 {
283         main_window_set_account_menu(account_list);
284 }
285
286 void account_foreach(AccountFunc func, gpointer user_data)
287 {
288         GList *cur;
289
290         for (cur = account_list; cur != NULL; cur = cur->next)
291                 if (func((PrefsAccount *)cur->data, user_data) != 0)
292                         return;
293 }
294
295 GList *account_get_list(void)
296 {
297         return account_list;
298 }
299
300 void account_edit_open(void)
301 {
302         inc_lock();
303
304         if (compose_get_compose_list()) {
305                 alertpanel_notice(_("Some composing windows are open.\n"
306                                     "Please close all the composing windows before editing the accounts."));
307                 inc_unlock();
308                 return;
309         }
310
311         debug_print("Opening account edit window...\n");
312
313         if (!edit_account.window)
314                 account_edit_create();
315
316         account_clist_set();
317
318         manage_window_set_transient(GTK_WINDOW(edit_account.window));
319         gtk_widget_grab_focus(edit_account.close_btn);
320         gtk_widget_show(edit_account.window);
321
322         manage_window_focus_in(edit_account.window, NULL, NULL);
323 }
324
325 void account_add(void)
326 {
327         PrefsAccount *ac_prefs;
328
329         ac_prefs = prefs_account_open(NULL);
330
331         if (!ac_prefs) return;
332
333         account_list = g_list_append(account_list, ac_prefs);
334
335         if (ac_prefs->is_default)
336                 account_set_as_default(ac_prefs);
337
338         account_clist_set();
339
340         if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
341                 Folder *folder;
342
343                 if (ac_prefs->protocol == A_IMAP4) {
344                         folder = folder_new(folder_get_class_from_string("imap"), ac_prefs->account_name,
345                                             ac_prefs->recv_server);
346                 } else {
347                         folder = folder_new(folder_get_class_from_string("news"), ac_prefs->account_name,
348                                             ac_prefs->nntp_server);
349                 }
350
351                 folder->account = ac_prefs;
352                 ac_prefs->folder = REMOTE_FOLDER(folder);
353                 folder_add(folder);
354                 if (ac_prefs->protocol == A_IMAP4)
355                         folder->klass->create_tree(folder);
356                 folderview_set_all();
357         }
358 }
359
360 void account_open(PrefsAccount *ac_prefs)
361 {
362         gboolean prev_default;
363         gchar *ac_name;
364
365         g_return_if_fail(ac_prefs != NULL);
366
367         prev_default = ac_prefs->is_default;
368         Xstrdup_a(ac_name, ac_prefs->account_name ? ac_prefs->account_name : "",
369                   return);
370
371         prefs_account_open(ac_prefs);
372
373         if (!prev_default && ac_prefs->is_default)
374                 account_set_as_default(ac_prefs);
375
376         if (ac_prefs->folder && strcmp2(ac_name, ac_prefs->account_name) != 0) {
377                 folder_set_name(FOLDER(ac_prefs->folder),
378                                 ac_prefs->account_name);
379                 folderview_set_all();
380         }
381
382         account_save_config_all();
383         account_set_menu();
384         main_window_reflect_prefs_all();
385 }
386
387 void account_set_as_default(PrefsAccount *ac_prefs)
388 {
389         PrefsAccount *ap;
390         GList *cur;
391
392         for (cur = account_list; cur != NULL; cur = cur->next) {
393                 ap = (PrefsAccount *)cur->data;
394                 if (ap->is_default)
395                         ap->is_default = FALSE;
396         }
397
398         ac_prefs->is_default = TRUE;
399 }
400
401 PrefsAccount *account_get_default(void)
402 {
403         PrefsAccount *ap;
404         GList *cur;
405
406         for (cur = account_list; cur != NULL; cur = cur->next) {
407                 ap = (PrefsAccount *)cur->data;
408                 if (ap->is_default)
409                         return ap;
410         }
411
412         return NULL;
413 }
414
415 void account_set_missing_folder(void)
416 {
417         PrefsAccount *ap;
418         GList *cur;
419
420         for (cur = account_list; cur != NULL; cur = cur->next) {
421                 ap = (PrefsAccount *)cur->data;
422                 if ((ap->protocol == A_IMAP4 || ap->protocol == A_NNTP) &&
423                     !ap->folder) {
424                         Folder *folder;
425
426                         if (ap->protocol == A_IMAP4) {
427                                 folder = folder_new(folder_get_class_from_string("imap"), ap->account_name,
428                                                     ap->recv_server);
429                         } else {
430                                 folder = folder_new(folder_get_class_from_string("news"), ap->account_name,
431                                                     ap->nntp_server);
432                         }
433
434                         folder->account = ap;
435                         ap->folder = REMOTE_FOLDER(folder);
436                         folder_add(folder);
437                         if (ap->protocol == A_IMAP4)
438                                 folder->klass->create_tree(folder);
439                 }
440         }
441 }
442
443 FolderItem *account_get_special_folder(PrefsAccount *ac_prefs,
444                                        SpecialFolderItemType type)
445 {
446         FolderItem *item = NULL;
447
448         g_return_val_if_fail(ac_prefs != NULL, NULL);
449
450         switch (type) {
451         case F_INBOX:
452                 if (ac_prefs->folder)
453                         item = FOLDER(ac_prefs->folder)->inbox;
454                 if (!item)
455                         item = folder_get_default_inbox();
456                 break;
457         case F_OUTBOX:
458                 if (ac_prefs->set_sent_folder && ac_prefs->sent_folder) {
459                         item = folder_find_item_from_identifier
460                                 (ac_prefs->sent_folder);
461                 }
462                 if (!item) {
463                         if (ac_prefs->folder)
464                                 item = FOLDER(ac_prefs->folder)->outbox;
465                         if (!item)
466                                 item = folder_get_default_outbox();
467                 }
468                 break;
469         case F_DRAFT:
470                 if (ac_prefs->set_draft_folder && ac_prefs->draft_folder) {
471                         item = folder_find_item_from_identifier
472                                 (ac_prefs->draft_folder);
473                 }
474                 if (!item) {
475                         if (ac_prefs->folder)
476                                 item = FOLDER(ac_prefs->folder)->draft;
477                         if (!item)
478                                 item = folder_get_default_draft();
479                 }
480                 break;
481         case F_QUEUE:
482                 if (ac_prefs->folder)
483                         item = FOLDER(ac_prefs->folder)->queue;
484                 if (!item)
485                         item = folder_get_default_queue();
486                 break;
487         case F_TRASH:
488                 if (ac_prefs->set_trash_folder && ac_prefs->trash_folder) {
489                         item = folder_find_item_from_identifier
490                                 (ac_prefs->trash_folder);
491                 }
492                 if (!item) {
493                         if (ac_prefs->folder)
494                                 item = FOLDER(ac_prefs->folder)->trash;
495                         if (!item)
496                                 item = folder_get_default_trash();
497                 }
498                 break;
499         default:
500                 break;
501         }
502
503         return item;
504 }
505
506 void account_destroy(PrefsAccount *ac_prefs)
507 {
508         g_return_if_fail(ac_prefs != NULL);
509
510         folder_unref_account_all(ac_prefs);
511
512         prefs_account_free(ac_prefs);
513         account_list = g_list_remove(account_list, ac_prefs);
514
515         if (cur_account == ac_prefs) cur_account = NULL;
516         if (!cur_account && account_list) {
517                 cur_account = account_get_default();
518                 if (!cur_account) {
519                         ac_prefs = (PrefsAccount *)account_list->data;
520                         account_set_as_default(ac_prefs);
521                         cur_account = ac_prefs;
522                 }
523         }
524 }
525
526
527 static void account_edit_create(void)
528 {
529         GtkWidget *window;
530         GtkWidget *vbox;
531         GtkWidget *label;
532         GtkWidget *hbox;
533         GtkWidget *scrolledwin;
534         GtkWidget *clist;
535         gchar *titles[N_EDIT_ACCOUNT_COLS];
536         gint i;
537
538         GtkWidget *vbox2;
539         GtkWidget *add_btn;
540         GtkWidget *edit_btn;
541         GtkWidget *del_btn;
542         GtkWidget *up_btn;
543         GtkWidget *down_btn;
544
545         GtkWidget *default_btn;
546
547         GtkWidget *hbbox;
548         GtkWidget *close_btn;
549
550         debug_print("Creating account edit window...\n");
551
552         window = gtk_window_new (GTK_WINDOW_DIALOG);
553         gtk_widget_set_usize (window, 500, 320);
554         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
555         gtk_window_set_title (GTK_WINDOW (window), _("Edit accounts"));
556         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
557         gtk_signal_connect (GTK_OBJECT (window), "delete_event",
558                             GTK_SIGNAL_FUNC (account_delete_event), NULL);
559         gtk_signal_connect (GTK_OBJECT (window), "key_press_event",
560                             GTK_SIGNAL_FUNC (account_key_pressed), NULL);
561         MANAGE_WINDOW_SIGNALS_CONNECT (window);
562         gtk_widget_realize(window);
563
564         vbox = gtk_vbox_new (FALSE, 10);
565         gtk_widget_show (vbox);
566         gtk_container_add (GTK_CONTAINER (window), vbox);
567
568         hbox = gtk_hbox_new (FALSE, 0);
569         gtk_widget_show (hbox);
570         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
571
572         label = gtk_label_new
573                 (_("New messages will be checked in this order. Check the boxes\n"
574                    "on the `G' column to enable message retrieval by `Get all'."));
575         gtk_widget_show (label);
576         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
577         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
578
579         hbox = gtk_hbox_new (FALSE, 8);
580         gtk_widget_show (hbox);
581         gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
582         gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
583
584         scrolledwin = gtk_scrolled_window_new (NULL, NULL);
585         gtk_widget_show (scrolledwin);
586         gtk_box_pack_start (GTK_BOX (hbox), scrolledwin, TRUE, TRUE, 0);
587         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
588                                         GTK_POLICY_AUTOMATIC,
589                                         GTK_POLICY_AUTOMATIC);
590
591         titles[COL_DEFAULT]  = "D";
592         titles[COL_GETALL]   = "G";
593         titles[COL_NAME]     = _("Name");
594         titles[COL_PROTOCOL] = _("Protocol");
595         titles[COL_SERVER]   = _("Server");
596
597         clist = gtk_clist_new_with_titles (N_EDIT_ACCOUNT_COLS, titles);
598         gtk_widget_show (clist);
599         gtk_container_add (GTK_CONTAINER (scrolledwin), clist);
600         gtk_clist_set_column_width (GTK_CLIST(clist), COL_DEFAULT , 10);
601         gtk_clist_set_column_width (GTK_CLIST(clist), COL_GETALL  , 11);
602         gtk_clist_set_column_width (GTK_CLIST(clist), COL_NAME    , 100);
603         gtk_clist_set_column_width (GTK_CLIST(clist), COL_PROTOCOL, 100);
604         gtk_clist_set_column_width (GTK_CLIST(clist), COL_SERVER  , 100);
605         gtk_clist_set_column_justification (GTK_CLIST(clist), COL_DEFAULT,
606                                             GTK_JUSTIFY_CENTER);
607         gtk_clist_set_column_justification (GTK_CLIST(clist), COL_GETALL,
608                                             GTK_JUSTIFY_CENTER);
609         gtk_clist_set_selection_mode (GTK_CLIST(clist), GTK_SELECTION_BROWSE);
610
611         for (i = 0; i < N_EDIT_ACCOUNT_COLS; i++)
612                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button,
613                                        GTK_CAN_FOCUS);
614
615         gtk_signal_connect (GTK_OBJECT (clist), "select_row",
616                             GTK_SIGNAL_FUNC (account_selected), NULL);
617         gtk_signal_connect_after (GTK_OBJECT (clist), "row_move",
618                                   GTK_SIGNAL_FUNC (account_row_moved), NULL);
619
620         vbox2 = gtk_vbox_new (FALSE, 0);
621         gtk_widget_show (vbox2);
622         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
623
624         add_btn = gtk_button_new_with_label (_("Add"));
625         gtk_widget_show (add_btn);
626         gtk_box_pack_start (GTK_BOX (vbox2), add_btn, FALSE, FALSE, 4);
627         gtk_signal_connect (GTK_OBJECT(add_btn), "clicked",
628                             GTK_SIGNAL_FUNC (account_add), NULL);
629
630         edit_btn = gtk_button_new_with_label (_("Edit"));
631         gtk_widget_show (edit_btn);
632         gtk_box_pack_start (GTK_BOX (vbox2), edit_btn, FALSE, FALSE, 4);
633         gtk_signal_connect (GTK_OBJECT(edit_btn), "clicked",
634                             GTK_SIGNAL_FUNC (account_edit_prefs), NULL);
635
636         del_btn = gtk_button_new_with_label (_(" Delete "));
637         gtk_widget_show (del_btn);
638         gtk_box_pack_start (GTK_BOX (vbox2), del_btn, FALSE, FALSE, 4);
639         gtk_signal_connect (GTK_OBJECT(del_btn), "clicked",
640                             GTK_SIGNAL_FUNC (account_delete), NULL);
641
642         down_btn = gtk_button_new_with_label (_("Down"));
643         gtk_widget_show (down_btn);
644         gtk_box_pack_end (GTK_BOX (vbox2), down_btn, FALSE, FALSE, 4);
645         gtk_signal_connect (GTK_OBJECT(down_btn), "clicked",
646                             GTK_SIGNAL_FUNC (account_down), NULL);
647
648         up_btn = gtk_button_new_with_label (_("Up"));
649         gtk_widget_show (up_btn);
650         gtk_box_pack_end (GTK_BOX (vbox2), up_btn, FALSE, FALSE, 4);
651         gtk_signal_connect (GTK_OBJECT(up_btn), "clicked",
652                             GTK_SIGNAL_FUNC (account_up), NULL);
653
654         hbox = gtk_hbox_new (FALSE, 8);
655         gtk_widget_show (hbox);
656         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
657
658         vbox2 = gtk_vbox_new(FALSE, 0);
659         gtk_widget_show (vbox2);
660         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
661
662         default_btn = gtk_button_new_with_label (_(" Set as default account "));
663         gtk_widget_show (default_btn);
664         gtk_box_pack_start (GTK_BOX (vbox2), default_btn, TRUE, FALSE, 0);
665         gtk_signal_connect (GTK_OBJECT(default_btn), "clicked",
666                             GTK_SIGNAL_FUNC (account_set_default), NULL);
667
668         gtkut_button_set_create(&hbbox, &close_btn, _("Close"),
669                                 NULL, NULL, NULL, NULL);
670         gtk_widget_show(hbbox);
671         gtk_box_pack_end (GTK_BOX (hbox), hbbox, FALSE, FALSE, 0);
672         gtk_widget_grab_default (close_btn);
673
674         gtk_signal_connect (GTK_OBJECT (close_btn), "clicked",
675                             GTK_SIGNAL_FUNC (account_edit_close),
676                             NULL);
677
678         stock_pixmap_gdk(clist, STOCK_PIXMAP_MARK, &markxpm, &markxpmmask);
679         stock_pixmap_gdk(clist, STOCK_PIXMAP_CHECKBOX_ON,
680                          &checkboxonxpm, &checkboxonxpmmask);
681         stock_pixmap_gdk(clist, STOCK_PIXMAP_CHECKBOX_OFF,
682                          &checkboxoffxpm, &checkboxoffxpmmask);
683
684         edit_account.window    = window;
685         edit_account.clist     = clist;
686         edit_account.close_btn = close_btn;
687 }
688
689 static void account_edit_prefs(void)
690 {
691         GtkCList *clist = GTK_CLIST(edit_account.clist);
692         PrefsAccount *ac_prefs;
693         gint row;
694
695         if (!clist->selection) return;
696
697         row = GPOINTER_TO_INT(clist->selection->data);
698         ac_prefs = gtk_clist_get_row_data(clist, row);
699         account_open(ac_prefs);
700         
701         account_clist_set();
702 }
703
704 static gboolean account_delete_references_func(GNode *node, gpointer data)
705 {
706         FolderItem *item;
707         gint account;
708
709         g_return_val_if_fail(node->data != NULL, FALSE);
710
711         item = FOLDER_ITEM(node->data);
712         account = GPOINTER_TO_INT(data);
713
714         if(!item->prefs) /* && item->prefs->stype == F_NORMAL */
715                 return FALSE;
716         if(item->prefs->default_account != account)
717                 return FALSE;
718         
719         item->prefs->enable_default_account = FALSE;
720         item->prefs->default_account = 0;
721         prefs_folder_item_save_config(item);
722
723         return FALSE;
724 }
725
726 static void account_delete(void)
727 {
728         GtkCList *clist = GTK_CLIST(edit_account.clist);
729         PrefsAccount *ac_prefs;
730         gint row;
731         GList *list;
732         Folder *folder;
733         
734         if (!clist->selection) return;
735
736         if (alertpanel(_("Delete account"),
737                        _("Do you really want to delete this account?"),
738                        _("Yes"), _("+No"), NULL) != G_ALERTDEFAULT)
739                 return;
740
741         row = GPOINTER_TO_INT(clist->selection->data);
742         ac_prefs = gtk_clist_get_row_data(clist, row);
743         if (ac_prefs->folder) {
744                 folder_destroy(FOLDER(ac_prefs->folder));
745                 folderview_set_all();
746         }
747         account_destroy(ac_prefs);
748         account_clist_set();
749
750         debug_print("Removing deleted account references for all the folders...\n");
751         list = folder_get_list();
752         for (; list != NULL; list = list->next) {
753                 folder = FOLDER(list->data);
754                 if (folder->node)  /* && folder->type == F_? */
755                         g_node_traverse(folder->node, G_PRE_ORDER,
756                                 G_TRAVERSE_ALL, -1,
757                                 account_delete_references_func,
758                                 GINT_TO_POINTER(ac_prefs->account_id));
759         }
760 }
761
762 static void account_up(void)
763 {
764         GtkCList *clist = GTK_CLIST(edit_account.clist);
765         gint row;
766
767         if (!clist->selection) return;
768
769         row = GPOINTER_TO_INT(clist->selection->data);
770         if (row > 0)
771                 gtk_clist_row_move(clist, row, row - 1);
772 }
773
774 static void account_down(void)
775 {
776         GtkCList *clist = GTK_CLIST(edit_account.clist);
777         gint row;
778
779         if (!clist->selection) return;
780
781         row = GPOINTER_TO_INT(clist->selection->data);
782         if (row < clist->rows - 1)
783                 gtk_clist_row_move(clist, row, row + 1);
784 }
785
786 static void account_set_default(void)
787 {
788         GtkCList *clist = GTK_CLIST(edit_account.clist);
789         gint row;
790         PrefsAccount *ac_prefs;
791
792         if (!clist->selection) return;
793
794         row = GPOINTER_TO_INT(clist->selection->data);
795         ac_prefs = gtk_clist_get_row_data(clist, row);
796         account_set_as_default(ac_prefs);
797         account_clist_set();
798
799         cur_account = ac_prefs;
800         account_set_menu();
801         main_window_reflect_prefs_all();
802 }
803
804 static void account_edit_close(void)
805 {
806         account_list_set();
807         account_save_config_all();
808
809         if (!cur_account && account_list) {
810                 PrefsAccount *ac_prefs = (PrefsAccount *)account_list->data;
811                 account_set_as_default(ac_prefs);
812                 cur_account = ac_prefs;
813         }
814
815         account_set_menu();
816         main_window_reflect_prefs_all();
817
818         gtk_widget_hide(edit_account.window);
819
820         inc_unlock();
821 }
822
823 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
824                                  gpointer data)
825 {
826         account_edit_close();
827         return TRUE;
828 }
829
830 static void account_selected(GtkCList *clist, gint row, gint column,
831                              GdkEvent *event, gpointer data)
832 {
833         if (event && event->type == GDK_2BUTTON_PRESS)
834                 account_edit_prefs();
835
836         if (column == COL_GETALL) {
837                 PrefsAccount *ac;
838
839                 ac = gtk_clist_get_row_data(clist, row);
840                 if (ac->protocol == A_POP3 || ac->protocol == A_APOP ||
841                     ac->protocol == A_IMAP4 || ac->protocol == A_NNTP ||
842                     ac->protocol == A_LOCAL) {
843                         ac->recv_at_getall ^= TRUE;
844                         account_clist_set_row(ac, row);
845                 }
846         }
847 }
848
849 static void account_row_moved(GtkCList *clist, gint source_row, gint dest_row)
850 {
851         account_list_set();
852         if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL)
853                 gtk_clist_moveto(clist, dest_row, -1, 0.5, 0.0);
854 }
855
856 static void account_key_pressed(GtkWidget *widget, GdkEventKey *event,
857                                 gpointer data)
858 {
859         if (event && event->keyval == GDK_Escape)
860                 account_edit_close();
861 }
862
863 /* set one CList row or add new row */
864 static gint account_clist_set_row(PrefsAccount *ac_prefs, gint row)
865 {
866         GtkCList *clist = GTK_CLIST(edit_account.clist);
867         gchar *text[N_EDIT_ACCOUNT_COLS];
868         gboolean has_getallbox;
869         gboolean getall;
870
871         text[COL_DEFAULT] = "";
872         text[COL_GETALL]  = "";
873         text[COL_NAME]    = ac_prefs->account_name;
874 #if USE_OPENSSL
875         text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3 ?
876                              (ac_prefs->ssl_pop == SSL_TUNNEL ?
877                               "POP3 (SSL)" :
878                               ac_prefs->ssl_pop == SSL_STARTTLS ?
879                               "POP3 (TLS)" : "POP3") :
880                              ac_prefs->protocol == A_APOP ?
881                              (ac_prefs->ssl_pop == SSL_TUNNEL ?
882                               "POP3 (APOP, SSL)" :
883                               ac_prefs->ssl_pop == SSL_STARTTLS ?
884                               "POP3 (APOP, TLS)" : "POP3 (APOP)") :
885                              ac_prefs->protocol == A_IMAP4 ?
886                              (ac_prefs->ssl_imap == SSL_TUNNEL ?
887                               "IMAP4 (SSL)" :
888                               ac_prefs->ssl_imap == SSL_STARTTLS ?
889                               "IMAP4 (TLS)" : "IMAP4") :
890                              ac_prefs->protocol == A_NNTP ?
891                              (ac_prefs->ssl_nntp == SSL_TUNNEL ?
892                               "NNTP (SSL)" : "NNTP") :
893                              "";
894 #else
895         text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3  ? "POP3" :
896                              ac_prefs->protocol == A_APOP  ? "POP3 (APOP)" :
897                              ac_prefs->protocol == A_IMAP4 ? "IMAP4" :
898                              ac_prefs->protocol == A_LOCAL ? "Local" :
899                              ac_prefs->protocol == A_NNTP  ? "NNTP" : "";
900 #endif
901         text[COL_SERVER] = ac_prefs->protocol == A_NNTP
902                 ? ac_prefs->nntp_server : ac_prefs->recv_server;
903
904         if (row < 0)
905                 row = gtk_clist_append(clist, text);
906         else {
907                 gtk_clist_set_text(clist, row, COL_DEFAULT, text[COL_DEFAULT]);
908                 gtk_clist_set_text(clist, row, COL_GETALL, text[COL_GETALL]);
909                 gtk_clist_set_text(clist, row, COL_NAME, text[COL_NAME]);
910                 gtk_clist_set_text(clist, row, COL_PROTOCOL, text[COL_PROTOCOL]);
911                 gtk_clist_set_text(clist, row, COL_SERVER, text[COL_SERVER]);
912         }
913
914         has_getallbox = (ac_prefs->protocol == A_POP3  ||
915                          ac_prefs->protocol == A_APOP  ||
916                          ac_prefs->protocol == A_IMAP4 ||
917                          ac_prefs->protocol == A_NNTP ||
918                          ac_prefs->protocol == A_LOCAL);
919         getall = has_getallbox && ac_prefs->recv_at_getall;
920
921         if (ac_prefs->is_default)
922                 gtk_clist_set_pixmap(clist, row, COL_DEFAULT,
923                                      markxpm, markxpmmask);
924         if (getall)
925                 gtk_clist_set_pixmap(clist, row, COL_GETALL,
926                                      checkboxonxpm, checkboxonxpmmask);
927         else if (has_getallbox)
928                 gtk_clist_set_pixmap(clist, row, COL_GETALL,
929                                      checkboxoffxpm, checkboxoffxpmmask);
930
931         gtk_clist_set_row_data(clist, row, ac_prefs);
932
933         return row;
934 }
935
936 /* set CList from account list */
937 static void account_clist_set(void)
938 {
939         GtkCList *clist = GTK_CLIST(edit_account.clist);
940         GList *cur;
941         gint row = -1, prev_row = -1;
942
943         if (clist->selection)
944                 prev_row = GPOINTER_TO_INT(clist->selection->data);
945
946         gtk_clist_freeze(clist);
947         gtk_clist_clear(clist);
948
949         for (cur = account_list; cur != NULL; cur = cur->next) {
950                 row = account_clist_set_row((PrefsAccount *)cur->data, -1);
951                 if ((PrefsAccount *)cur->data == cur_account) {
952                         gtk_clist_select_row(clist, row, -1);
953                         gtkut_clist_set_focus_row(clist, row);
954                 }
955         }
956
957         if (prev_row >= 0) {
958                 row = prev_row;
959                 gtk_clist_select_row(clist, row, -1);
960                 gtkut_clist_set_focus_row(clist, row);
961         }
962
963         if (row >= 0 &&
964             gtk_clist_row_is_visible(clist, row) != GTK_VISIBILITY_FULL)
965                 gtk_clist_moveto(clist, row, -1, 0.5, 0);
966
967         gtk_clist_thaw(clist);
968 }
969
970 /* set account list from CList */
971 static void account_list_set(void)
972 {
973         GtkCList *clist = GTK_CLIST(edit_account.clist);
974         gint row;
975         PrefsAccount *ac_prefs;
976
977         while (account_list)
978                 account_list = g_list_remove(account_list, account_list->data);
979
980         for (row = 0; (ac_prefs = gtk_clist_get_row_data(clist, row)) != NULL;
981              row++)
982                 account_list = g_list_append(account_list, ac_prefs);
983 }
984
985 /*!
986  *\brief        finds the PrefsAccounts which should be used to answer a mail
987  *
988  *\param        msginfo The message to be answered
989  *\param        reply_autosel Indicates whether reply account autoselection is on
990  *
991  *\return       PrefsAccount * the correct account, NULL if not found
992  */
993 PrefsAccount *account_get_reply_account(MsgInfo *msginfo, gboolean reply_autosel)
994 {
995         PrefsAccount *account = NULL;
996         /* select the account set in folderitem's property (if enabled) */
997         if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
998                 account = account_find_from_id(msginfo->folder->prefs->default_account);
999         
1000         /* select the account for the whole folder (IMAP / NNTP) */
1001         if (!account)
1002                 /* FIXME: this is not right, because folder may be nested. we should
1003                  * ascend the tree until we find a parent with proper account 
1004                  * information */
1005                 account = msginfo->folder->folder->account;
1006
1007         /* select account by to: and cc: header if enabled */
1008         if (reply_autosel) {
1009                 if (!account && msginfo->to) {
1010                         gchar *to;
1011                         Xstrdup_a(to, msginfo->to, return NULL);
1012                         extract_address(to);
1013                         account = account_find_from_address(to);
1014                 }
1015                 if (!account) {
1016                         gchar cc[BUFFSIZE];
1017                         if (!get_header_from_msginfo(msginfo, cc, sizeof(cc), "CC:")) { /* Found a CC header */
1018                                 extract_address(cc);
1019                                 account = account_find_from_address(cc);
1020                         }        
1021                 }
1022         }
1023
1024         /* select current account */
1025         if (!account) account = cur_account;
1026         
1027         return account;
1028 }