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