0.9.7claws10
[claws.git] / src / account.c
index 63bf2feb667386f7fd0e5a67049e63d4697019a2..dd2e568b62db017b042aa6ebf9c6dbd833be30c3 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2002 Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include "folderview.h"
 #include "folder.h"
 #include "account.h"
-#include "prefs.h"
+#include "prefs_gtk.h"
 #include "prefs_account.h"
-#include "prefs_folder_item.h"
+#include "folder_item_prefs.h"
 #include "compose.h"
 #include "manage_window.h"
+#include "stock_pixmap.h"
 #include "inc.h"
 #include "gtkutils.h"
 #include "utils.h"
 #include "alertpanel.h"
-
-#include "pixmaps/mark.xpm"
-#include "pixmaps/checkbox_on.xpm"
-#include "pixmaps/checkbox_off.xpm"
+#include "procheader.h"
+#include "customheader.h"
+#include "remotefolder.h"
 
 typedef enum
 {
@@ -83,6 +83,7 @@ static void account_edit_create               (void);
 
 static void account_edit_prefs         (void);
 static void account_delete             (void);
+static void account_clone              (void);
 
 static void account_up                 (void);
 static void account_down               (void);
@@ -90,6 +91,10 @@ static void account_down             (void);
 static void account_set_default                (void);
 
 static void account_edit_close         (void);
+
+static gint account_clone_event                (GtkWidget      *widget,
+                                        GdkEventAny    *event,
+                                        gpointer        data);
 static gint account_delete_event       (GtkWidget      *widget,
                                         GdkEventAny    *event,
                                         gpointer        data);
@@ -98,6 +103,9 @@ static void account_selected         (GtkCList       *clist,
                                         gint            column,
                                         GdkEvent       *event,
                                         gpointer        data);
+static void account_row_moved          (GtkCList       *clist,
+                                        gint            source_row,
+                                        gint            dest_row);
 static void account_key_pressed                (GtkWidget      *widget,
                                         GdkEventKey    *event,
                                         gpointer        data);
@@ -116,10 +124,10 @@ void account_read_config_all(void)
        gchar buf[PREFSBUFSIZE];
        PrefsAccount *ac_prefs;
 
-       debug_print(_("Reading all config for each account...\n"));
+       debug_print("Reading all config for each account...\n");
 
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
-       if ((fp = fopen(rcpath, "r")) == NULL) {
+       if ((fp = fopen(rcpath, "rb")) == NULL) {
                if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
                g_free(rcpath);
                return;
@@ -131,7 +139,7 @@ void account_read_config_all(void)
                        strretchomp(buf);
                        memmove(buf, buf + 1, strlen(buf));
                        buf[strlen(buf) - 1] = '\0';
-                       debug_print(_("Found label: %s\n"), buf);
+                       debug_print("Found label: %s\n", buf);
                        ac_label_list = g_slist_append(ac_label_list,
                                                       g_strdup(buf));
                }
@@ -141,7 +149,7 @@ void account_read_config_all(void)
        /* read config data from file */
        cur_account = NULL;
        for (cur = ac_label_list; cur != NULL; cur = cur->next) {
-               ac_prefs = g_new0(PrefsAccount, 1);
+               ac_prefs = prefs_account_new();
                prefs_account_read_config(ac_prefs, (gchar *)cur->data);
                account_list = g_list_append(account_list, ac_prefs);
                if (ac_prefs->is_default)
@@ -169,12 +177,42 @@ void account_save_config_all(void)
        prefs_account_save_config_all(account_list);
 }
 
+/*
+ * account_find_all_from_address:
+ * @ac_list: initial list of accounts. NULL to create a new one.
+ * Accounts found in the @address will be appended to this list.
+ * @address: Email address string.
+ *
+ * Find all the mail (not news) accounts within the specified address.
+ *
+ * Return value: the original accounts list with the found accounts appended.
+ */
+GList *account_find_all_from_address(GList *ac_list, const gchar *address)
+{
+       GList *cur;
+       PrefsAccount *ac;
+
+       if (address == NULL)
+               return ac_list;
+
+       for (cur = account_list; cur != NULL; cur = cur->next) {
+               ac = (PrefsAccount *)cur->data;
+               if (ac->protocol != A_NNTP && ac->address &&
+                   strcasestr(address, ac->address) != NULL)
+                       ac_list = g_list_append(ac_list, ac);
+       }
+       return ac_list;
+}
+       
 PrefsAccount *account_find_from_smtp_server(const gchar *address,
                                            const gchar *smtp_server)
 {
        GList *cur;
        PrefsAccount *ac;
 
+       g_return_val_if_fail(address != NULL, NULL);
+       g_return_val_if_fail(smtp_server != NULL, NULL);
+
        for (cur = account_list; cur != NULL; cur = cur->next) {
                ac = (PrefsAccount *)cur->data;
                if (!strcmp2(address, ac->address) &&
@@ -198,9 +236,12 @@ PrefsAccount *account_find_from_address(const gchar *address)
        GList *cur;
        PrefsAccount *ac;
 
+       g_return_val_if_fail(address != NULL, NULL);
+
        for (cur = account_list; cur != NULL; cur = cur->next) {
                ac = (PrefsAccount *)cur->data;
-               if (ac->protocol != A_NNTP && strcasestr(address, ac->address))
+               if (ac->protocol != A_NNTP && ac->address &&
+                   g_strcasecmp(address, ac->address) == 0)
                        return ac;
        }
 
@@ -221,6 +262,29 @@ PrefsAccount *account_find_from_id(gint id)
        return NULL;
 }
 
+PrefsAccount *account_find_from_item(FolderItem *item)
+{
+       PrefsAccount *ac;
+
+       g_return_val_if_fail(item != NULL, NULL);
+
+       ac = item->account;
+       if (!ac) {
+               FolderItem *cur_item = item->parent;
+               while (cur_item != NULL) {
+                       if (cur_item->account && cur_item->apply_sub) {
+                               ac = cur_item->account;
+                               break;
+                       }                               
+                       cur_item = cur_item->parent;
+               }
+       }
+       if (!ac)
+               ac = item->folder->account;
+
+       return ac;
+}
+
 void account_set_menu(void)
 {
        main_window_set_account_menu(account_list);
@@ -251,7 +315,7 @@ void account_edit_open(void)
                return;
        }
 
-       debug_print(_("Opening account edit window...\n"));
+       debug_print("Opening account edit window...\n");
 
        if (!edit_account.window)
                account_edit_create();
@@ -284,22 +348,49 @@ void account_add(void)
                Folder *folder;
 
                if (ac_prefs->protocol == A_IMAP4) {
-                       folder = folder_new(F_IMAP, ac_prefs->account_name,
+                       folder = folder_new(folder_get_class_from_string("imap"), ac_prefs->account_name,
                                            ac_prefs->recv_server);
                } else {
-                       folder = folder_new(F_NEWS, ac_prefs->account_name,
+                       folder = folder_new(folder_get_class_from_string("news"), ac_prefs->account_name,
                                            ac_prefs->nntp_server);
                }
 
                folder->account = ac_prefs;
-               ac_prefs->folder = REMOTE_FOLDER(folder);
+               ac_prefs->folder = folder;
                folder_add(folder);
                if (ac_prefs->protocol == A_IMAP4)
-                       folder->create_tree(folder);
+                       folder->klass->create_tree(folder);
                folderview_set_all();
        }
 }
 
+void account_open(PrefsAccount *ac_prefs)
+{
+       gboolean prev_default;
+       gchar *ac_name;
+
+       g_return_if_fail(ac_prefs != NULL);
+
+       prev_default = ac_prefs->is_default;
+       Xstrdup_a(ac_name, ac_prefs->account_name ? ac_prefs->account_name : "",
+                 return);
+
+       prefs_account_open(ac_prefs);
+
+       if (!prev_default && ac_prefs->is_default)
+               account_set_as_default(ac_prefs);
+
+       if (ac_prefs->folder && strcmp2(ac_name, ac_prefs->account_name) != 0) {
+               folder_set_name(FOLDER(ac_prefs->folder),
+                               ac_prefs->account_name);
+               folderview_set_all();
+       }
+
+       account_save_config_all();
+       account_set_menu();
+       main_window_reflect_prefs_all();
+}
+
 void account_set_as_default(PrefsAccount *ac_prefs)
 {
        PrefsAccount *ap;
@@ -340,26 +431,91 @@ void account_set_missing_folder(void)
                        Folder *folder;
 
                        if (ap->protocol == A_IMAP4) {
-                               folder = folder_new(F_IMAP, ap->account_name,
+                               folder = folder_new(folder_get_class_from_string("imap"), ap->account_name,
                                                    ap->recv_server);
                        } else {
-                               folder = folder_new(F_NEWS, ap->account_name,
+                               folder = folder_new(folder_get_class_from_string("news"), ap->account_name,
                                                    ap->nntp_server);
                        }
 
                        folder->account = ap;
-                       ap->folder = REMOTE_FOLDER(folder);
+                       ap->folder = folder;
                        folder_add(folder);
                        if (ap->protocol == A_IMAP4)
-                               folder->create_tree(folder);
+                               folder->klass->create_tree(folder);
                }
        }
 }
 
+FolderItem *account_get_special_folder(PrefsAccount *ac_prefs,
+                                      SpecialFolderItemType type)
+{
+       FolderItem *item = NULL;
+
+       g_return_val_if_fail(ac_prefs != NULL, NULL);
+
+       switch (type) {
+       case F_INBOX:
+               if (ac_prefs->folder)
+                       item = FOLDER(ac_prefs->folder)->inbox;
+               if (!item)
+                       item = folder_get_default_inbox();
+               break;
+       case F_OUTBOX:
+               if (ac_prefs->set_sent_folder && ac_prefs->sent_folder) {
+                       item = folder_find_item_from_identifier
+                               (ac_prefs->sent_folder);
+               }
+               if (!item) {
+                       if (ac_prefs->folder)
+                               item = FOLDER(ac_prefs->folder)->outbox;
+                       if (!item)
+                               item = folder_get_default_outbox();
+               }
+               break;
+       case F_DRAFT:
+               if (ac_prefs->set_draft_folder && ac_prefs->draft_folder) {
+                       item = folder_find_item_from_identifier
+                               (ac_prefs->draft_folder);
+               }
+               if (!item) {
+                       if (ac_prefs->folder)
+                               item = FOLDER(ac_prefs->folder)->draft;
+                       if (!item)
+                               item = folder_get_default_draft();
+               }
+               break;
+       case F_QUEUE:
+               if (ac_prefs->folder)
+                       item = FOLDER(ac_prefs->folder)->queue;
+               if (!item)
+                       item = folder_get_default_queue();
+               break;
+       case F_TRASH:
+               if (ac_prefs->set_trash_folder && ac_prefs->trash_folder) {
+                       item = folder_find_item_from_identifier
+                               (ac_prefs->trash_folder);
+               }
+               if (!item) {
+                       if (ac_prefs->folder)
+                               item = FOLDER(ac_prefs->folder)->trash;
+                       if (!item)
+                               item = folder_get_default_trash();
+               }
+               break;
+       default:
+               break;
+       }
+
+       return item;
+}
+
 void account_destroy(PrefsAccount *ac_prefs)
 {
        g_return_if_fail(ac_prefs != NULL);
 
+       folder_unref_account_all(ac_prefs);
+
        prefs_account_free(ac_prefs);
        account_list = g_list_remove(account_list, ac_prefs);
 
@@ -390,6 +546,7 @@ static void account_edit_create(void)
        GtkWidget *add_btn;
        GtkWidget *edit_btn;
        GtkWidget *del_btn;
+       GtkWidget *clone_btn;
        GtkWidget *up_btn;
        GtkWidget *down_btn;
 
@@ -398,10 +555,10 @@ static void account_edit_create(void)
        GtkWidget *hbbox;
        GtkWidget *close_btn;
 
-       debug_print(_("Creating account edit window...\n"));
+       debug_print("Creating account edit window...\n");
 
        window = gtk_window_new (GTK_WINDOW_DIALOG);
-       gtk_widget_set_usize (window, 500, 320);
+       gtk_widget_set_usize (window, 500, 350);
        gtk_container_set_border_width (GTK_CONTAINER (window), 8);
        gtk_window_set_title (GTK_WINDOW (window), _("Edit accounts"));
        gtk_window_set_modal (GTK_WINDOW (window), TRUE);
@@ -409,10 +566,7 @@ static void account_edit_create(void)
                            GTK_SIGNAL_FUNC (account_delete_event), NULL);
        gtk_signal_connect (GTK_OBJECT (window), "key_press_event",
                            GTK_SIGNAL_FUNC (account_key_pressed), NULL);
-       gtk_signal_connect (GTK_OBJECT (window), "focus_in_event",
-                           GTK_SIGNAL_FUNC (manage_window_focus_in), NULL);
-       gtk_signal_connect (GTK_OBJECT (window), "focus_out_event",
-                           GTK_SIGNAL_FUNC (manage_window_focus_out), NULL);
+       MANAGE_WINDOW_SIGNALS_CONNECT (window);
        gtk_widget_realize(window);
 
        vbox = gtk_vbox_new (FALSE, 10);
@@ -468,6 +622,8 @@ static void account_edit_create(void)
 
        gtk_signal_connect (GTK_OBJECT (clist), "select_row",
                            GTK_SIGNAL_FUNC (account_selected), NULL);
+       gtk_signal_connect_after (GTK_OBJECT (clist), "row_move",
+                                 GTK_SIGNAL_FUNC (account_row_moved), NULL);
 
        vbox2 = gtk_vbox_new (FALSE, 0);
        gtk_widget_show (vbox2);
@@ -491,6 +647,12 @@ static void account_edit_create(void)
        gtk_signal_connect (GTK_OBJECT(del_btn), "clicked",
                            GTK_SIGNAL_FUNC (account_delete), NULL);
 
+       clone_btn = gtk_button_new_with_label (_(" Clone "));
+       gtk_widget_show (clone_btn);
+       gtk_box_pack_start (GTK_BOX (vbox2), clone_btn, FALSE, FALSE, 4);
+       gtk_signal_connect (GTK_OBJECT(clone_btn), "clicked",
+                           GTK_SIGNAL_FUNC (account_clone), NULL);
+       
        down_btn = gtk_button_new_with_label (_("Down"));
        gtk_widget_show (down_btn);
        gtk_box_pack_end (GTK_BOX (vbox2), down_btn, FALSE, FALSE, 4);
@@ -527,10 +689,11 @@ static void account_edit_create(void)
                            GTK_SIGNAL_FUNC (account_edit_close),
                            NULL);
 
-       PIXMAP_CREATE(clist, markxpm, markxpmmask, mark_xpm);
-       PIXMAP_CREATE(clist, checkboxonxpm, checkboxonxpmmask, checkbox_on_xpm);
-       PIXMAP_CREATE(clist, checkboxoffxpm, checkboxoffxpmmask,
-                     checkbox_off_xpm);
+       stock_pixmap_gdk(clist, STOCK_PIXMAP_MARK, &markxpm, &markxpmmask);
+       stock_pixmap_gdk(clist, STOCK_PIXMAP_CHECKBOX_ON,
+                        &checkboxonxpm, &checkboxonxpmmask);
+       stock_pixmap_gdk(clist, STOCK_PIXMAP_CHECKBOX_OFF,
+                        &checkboxoffxpm, &checkboxoffxpmmask);
 
        edit_account.window    = window;
        edit_account.clist     = clist;
@@ -542,28 +705,13 @@ static void account_edit_prefs(void)
        GtkCList *clist = GTK_CLIST(edit_account.clist);
        PrefsAccount *ac_prefs;
        gint row;
-       gboolean prev_default;
-       gchar *ac_name;
 
        if (!clist->selection) return;
 
        row = GPOINTER_TO_INT(clist->selection->data);
        ac_prefs = gtk_clist_get_row_data(clist, row);
-       prev_default = ac_prefs->is_default;
-       Xstrdup_a(ac_name, ac_prefs->account_name, return);
-
-       prefs_account_open(ac_prefs);
-
-       if (!prev_default && ac_prefs->is_default)
-               account_set_as_default(ac_prefs);
-
-       if ((ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) &&
-           ac_prefs->folder && strcmp(ac_name, ac_prefs->account_name) != 0) {
-               folder_set_name(FOLDER(ac_prefs->folder),
-                               ac_prefs->account_name);
-               folderview_update_all();
-       }
-
+       account_open(ac_prefs);
+       
        account_clist_set();
 }
 
@@ -584,8 +732,161 @@ static gboolean account_delete_references_func(GNode *node, gpointer data)
        
        item->prefs->enable_default_account = FALSE;
        item->prefs->default_account = 0;
-       prefs_folder_item_save_config(item);
+       folder_item_prefs_save_config(item);
+
+       return FALSE;
+}
+
+       
+#define ACP_FDUP(fld) ac_clon->fld = ((ac_prefs->fld) != NULL)?\
+                                    g_strdup(ac_prefs->fld): NULL
+#define ACP_FASSIGN(fld) ac_clon->fld = ac_prefs->fld
+static void account_clone(void)
+{
+       GtkCList *clist = GTK_CLIST(edit_account.clist);
+       gint row;
+       PrefsAccount *ac_prefs, *ac_clon;
+       GSList *hdrs = NULL;
+       CustomHeader *cch = NULL, *ch = NULL;
+
+       if (!clist->selection) return;
+
+       row = GPOINTER_TO_INT(clist->selection->data);
+       ac_prefs = gtk_clist_get_row_data(clist, row);
+       
+       ac_clon = prefs_account_new();
+       /* copy fields */
+       ac_clon->account_name = g_strdup_printf(_("Cloned %s"), 
+                                               ac_prefs->account_name);
+       /* personal */
+       ACP_FDUP(name);
+       ACP_FDUP(address);
+       ACP_FDUP(organization);
+
+       /* server */
+       ACP_FASSIGN(protocol);
+       ACP_FDUP(recv_server);
+       ACP_FDUP(smtp_server);
+       ACP_FDUP(nntp_server);
+       ACP_FASSIGN(use_nntp_auth);
+       ACP_FASSIGN(use_nntp_auth_onconnect);
+       ACP_FDUP(userid);
+       ACP_FDUP(passwd);
+
+       ACP_FDUP(local_mbox);
+       ACP_FASSIGN(use_mail_command);
+       ACP_FDUP(mail_command);
+       
+#if USE_OPENSSL
+       ACP_FASSIGN(ssl_pop);
+       ACP_FASSIGN(ssl_imap);
+       ACP_FASSIGN(ssl_nntp);
+       ACP_FASSIGN(ssl_smtp);
+       ACP_FASSIGN(use_nonblocking_ssl);
+#endif /* USE_OPENSSL */
+       
+       ac_clon->tmp_pass = NULL;
+
+       /* receive */
+       ACP_FASSIGN(rmmail);
+       ACP_FASSIGN(msg_leave_time);
+       ACP_FASSIGN(getall);
+       ACP_FASSIGN(recv_at_getall);
+       ACP_FASSIGN(sd_rmmail_on_download);
+       ACP_FASSIGN(sd_filter_on_recv);
+       ACP_FASSIGN(enable_size_limit);
+       ACP_FASSIGN(size_limit);
+       ACP_FASSIGN(filter_on_recv);
+       ACP_FDUP(inbox);
+       ACP_FASSIGN(max_articles);
+
+       ACP_FASSIGN(imap_auth_type);
+
+       /* send */
+       ACP_FASSIGN(add_date);
+       ACP_FASSIGN(gen_msgid);
+       ACP_FASSIGN(add_customhdr);
+       ACP_FASSIGN(use_smtp_auth);
+       ACP_FASSIGN(smtp_auth_type);
+       ACP_FDUP(smtp_userid);
+       ACP_FDUP(smtp_passwd);
+
+       ac_clon->tmp_smtp_pass = NULL;
+
+       ACP_FASSIGN(pop_before_smtp);
+       ACP_FASSIGN(pop_before_smtp_timeout);
+       ACP_FASSIGN(last_pop_login_time);
+
+       ac_clon->customhdr_list = NULL;
+       hdrs = ac_prefs->customhdr_list;
+       while (hdrs != NULL) {
+               ch = (CustomHeader *)hdrs->data;
+
+               cch = g_new0(CustomHeader, 1);
+               cch->account_id = ac_clon->account_id;  
+               cch->name = (ch->name != NULL) ? g_strdup(ch->name) : NULL;
+               cch->value = (ch->value != NULL) ? g_strdup(ch->value) : NULL;
+               
+               ac_clon->customhdr_list = g_slist_append(ac_clon->customhdr_list, cch);
+               
+               hdrs = g_slist_next(hdrs);
+       }
+
+       /* compose */
+        ACP_FASSIGN(sig_type);
+        ACP_FDUP(sig_path);
+        ACP_FASSIGN(auto_sig);
+        ACP_FDUP(sig_sep);
+        ACP_FASSIGN(set_autocc);
+        ACP_FDUP(auto_cc);
+        ACP_FASSIGN(set_autobcc);
+        ACP_FDUP(auto_bcc);
+        ACP_FASSIGN(set_autoreplyto);
+        ACP_FDUP(auto_replyto);
+
+#if USE_GPGME
+        /* privacy */
+        ACP_FASSIGN(default_encrypt);
+        ACP_FASSIGN(default_sign);
+        ACP_FASSIGN(default_gnupg_mode);
+        ACP_FASSIGN(sign_key);
+        ACP_FDUP(sign_key_id);
+#endif /* USE_GPGME */
+       
+        /* advanced */
+        ACP_FASSIGN(set_smtpport);
+        ACP_FASSIGN(smtpport);
+        ACP_FASSIGN(set_popport);
+        ACP_FASSIGN(popport);
+        ACP_FASSIGN(set_imapport);
+        ACP_FASSIGN(imapport);
+        ACP_FASSIGN(set_nntpport);
+        ACP_FASSIGN(nntpport);
+        ACP_FASSIGN(set_domain);
+        ACP_FDUP(domain);
+        ACP_FASSIGN(mark_crosspost_read);
+        ACP_FASSIGN(crosspost_col);
+
+        ACP_FASSIGN(set_tunnelcmd);
+        ACP_FDUP(tunnelcmd);
+
+        ACP_FDUP(imap_dir);
+
+        ACP_FASSIGN(set_sent_folder);
+        ACP_FDUP(sent_folder);
+        ACP_FASSIGN(set_draft_folder);
+        ACP_FDUP(draft_folder);
+        ACP_FASSIGN(set_trash_folder);
+        ACP_FDUP(trash_folder);
+       /* don't want two default accounts */
+       ac_clon->is_default = FALSE;
+       ACP_FASSIGN(folder);
+
+       account_list = g_list_append(account_list, ac_clon);
+       account_clist_set();
 }
+#undef ACP_FDUP
+#undef ACP_FASSIGN
 
 static void account_delete(void)
 {
@@ -606,12 +907,12 @@ static void account_delete(void)
        ac_prefs = gtk_clist_get_row_data(clist, row);
        if (ac_prefs->folder) {
                folder_destroy(FOLDER(ac_prefs->folder));
-               folderview_update_all();
+               folderview_set_all();
        }
        account_destroy(ac_prefs);
        account_clist_set();
 
-       debug_print(_("Removing deleted account references for all the folders...\n"));
+       debug_print("Removing deleted account references for all the folders...\n");
        list = folder_get_list();
        for (; list != NULL; list = list->next) {
                folder = FOLDER(list->data);
@@ -631,10 +932,8 @@ static void account_up(void)
        if (!clist->selection) return;
 
        row = GPOINTER_TO_INT(clist->selection->data);
-       if (row > 0) {
+       if (row > 0)
                gtk_clist_row_move(clist, row, row - 1);
-               account_list_set();
-       }
 }
 
 static void account_down(void)
@@ -645,10 +944,8 @@ static void account_down(void)
        if (!clist->selection) return;
 
        row = GPOINTER_TO_INT(clist->selection->data);
-       if (row < clist->rows - 1) {
+       if (row < clist->rows - 1)
                gtk_clist_row_move(clist, row, row + 1);
-               account_list_set();
-       }
 }
 
 static void account_set_default(void)
@@ -688,6 +985,13 @@ static void account_edit_close(void)
        inc_unlock();
 }
 
+static gint account_clone_event(GtkWidget *widget, GdkEventAny *event,
+                                gpointer data)
+{
+       account_clone();
+       return TRUE;
+}
+
 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
                                 gpointer data)
 {
@@ -705,13 +1009,22 @@ static void account_selected(GtkCList *clist, gint row, gint column,
                PrefsAccount *ac;
 
                ac = gtk_clist_get_row_data(clist, row);
-               if (ac->protocol == A_POP3 || ac->protocol == A_APOP) {
+               if (ac->protocol == A_POP3 || ac->protocol == A_APOP ||
+                   ac->protocol == A_IMAP4 || ac->protocol == A_NNTP ||
+                   ac->protocol == A_LOCAL) {
                        ac->recv_at_getall ^= TRUE;
                        account_clist_set_row(ac, row);
                }
        }
 }
 
+static void account_row_moved(GtkCList *clist, gint source_row, gint dest_row)
+{
+       account_list_set();
+       if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL)
+               gtk_clist_moveto(clist, dest_row, -1, 0.5, 0.0);
+}
+
 static void account_key_pressed(GtkWidget *widget, GdkEventKey *event,
                                gpointer data)
 {
@@ -730,16 +1043,26 @@ static gint account_clist_set_row(PrefsAccount *ac_prefs, gint row)
        text[COL_DEFAULT] = "";
        text[COL_GETALL]  = "";
        text[COL_NAME]    = ac_prefs->account_name;
-#if USE_SSL
+#if USE_OPENSSL
        text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3 ?
-                            (ac_prefs->ssl_pop ? "POP3 (SSL)" : "POP3") :
+                            (ac_prefs->ssl_pop == SSL_TUNNEL ?
+                             "POP3 (SSL)" :
+                             ac_prefs->ssl_pop == SSL_STARTTLS ?
+                             "POP3 (TLS)" : "POP3") :
                             ac_prefs->protocol == A_APOP ?
-                            (ac_prefs->ssl_pop ?
-                             "POP3 (APOP, SSL)" : "POP3 (APOP)") :
+                            (ac_prefs->ssl_pop == SSL_TUNNEL ?
+                             "POP3 (APOP, SSL)" :
+                             ac_prefs->ssl_pop == SSL_STARTTLS ?
+                             "POP3 (APOP, TLS)" : "POP3 (APOP)") :
                             ac_prefs->protocol == A_IMAP4 ?
-                            (ac_prefs->ssl_imap ? "IMAP4 (SSL)" : "IMAP4") :
-                            ac_prefs->protocol == A_LOCAL ? "Local" :
-                            ac_prefs->protocol == A_NNTP ? "NNTP"  :  "";
+                            (ac_prefs->ssl_imap == SSL_TUNNEL ?
+                             "IMAP4 (SSL)" :
+                             ac_prefs->ssl_imap == SSL_STARTTLS ?
+                             "IMAP4 (TLS)" : "IMAP4") :
+                            ac_prefs->protocol == A_NNTP ?
+                            (ac_prefs->ssl_nntp == SSL_TUNNEL ?
+                             "NNTP (SSL)" : "NNTP") :
+                            "";
 #else
        text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3  ? "POP3" :
                             ac_prefs->protocol == A_APOP  ? "POP3 (APOP)" :
@@ -760,8 +1083,11 @@ static gint account_clist_set_row(PrefsAccount *ac_prefs, gint row)
                gtk_clist_set_text(clist, row, COL_SERVER, text[COL_SERVER]);
        }
 
-       has_getallbox = (ac_prefs->protocol == A_POP3 ||
-                        ac_prefs->protocol == A_APOP);
+       has_getallbox = (ac_prefs->protocol == A_POP3  ||
+                        ac_prefs->protocol == A_APOP  ||
+                        ac_prefs->protocol == A_IMAP4 ||
+                        ac_prefs->protocol == A_NNTP ||
+                        ac_prefs->protocol == A_LOCAL);
        getall = has_getallbox && ac_prefs->recv_at_getall;
 
        if (ac_prefs->is_default)
@@ -784,19 +1110,15 @@ static void account_clist_set(void)
 {
        GtkCList *clist = GTK_CLIST(edit_account.clist);
        GList *cur;
-       gint prev_row;
+       gint row = -1, prev_row = -1;
 
        if (clist->selection)
                prev_row = GPOINTER_TO_INT(clist->selection->data);
-       else
-               prev_row = -1;
 
        gtk_clist_freeze(clist);
        gtk_clist_clear(clist);
 
        for (cur = account_list; cur != NULL; cur = cur->next) {
-               gint row;
-
                row = account_clist_set_row((PrefsAccount *)cur->data, -1);
                if ((PrefsAccount *)cur->data == cur_account) {
                        gtk_clist_select_row(clist, row, -1);
@@ -805,10 +1127,15 @@ static void account_clist_set(void)
        }
 
        if (prev_row >= 0) {
-               gtk_clist_select_row(clist, prev_row, -1);
-               gtkut_clist_set_focus_row(clist, prev_row);
+               row = prev_row;
+               gtk_clist_select_row(clist, row, -1);
+               gtkut_clist_set_focus_row(clist, row);
        }
 
+       if (row >= 0 &&
+           gtk_clist_row_is_visible(clist, row) != GTK_VISIBILITY_FULL)
+               gtk_clist_moveto(clist, row, -1, 0.5, 0);
+
        gtk_clist_thaw(clist);
 }
 
@@ -826,3 +1153,48 @@ static void account_list_set(void)
             row++)
                account_list = g_list_append(account_list, ac_prefs);
 }
+
+/*!
+ *\brief       finds the PrefsAccounts which should be used to answer a mail
+ *
+ *\param       msginfo The message to be answered
+ *\param       reply_autosel Indicates whether reply account autoselection is on
+ *
+ *\return      PrefsAccount * the correct account, NULL if not found
+ */
+PrefsAccount *account_get_reply_account(MsgInfo *msginfo, gboolean reply_autosel)
+{
+       PrefsAccount *account = NULL;
+       /* select the account set in folderitem's property (if enabled) */
+       if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
+               account = account_find_from_id(msginfo->folder->prefs->default_account);
+       
+       /* select the account for the whole folder (IMAP / NNTP) */
+       if (!account)
+               /* FIXME: this is not right, because folder may be nested. we should
+                * ascend the tree until we find a parent with proper account 
+                * information */
+               account = msginfo->folder->folder->account;
+
+       /* select account by to: and cc: header if enabled */
+       if (reply_autosel) {
+               if (!account && msginfo->to) {
+                       gchar *to;
+                       Xstrdup_a(to, msginfo->to, return NULL);
+                       extract_address(to);
+                       account = account_find_from_address(to);
+               }
+               if (!account) {
+                       gchar cc[BUFFSIZE];
+                       if (!procheader_get_header_from_msginfo(msginfo, cc, sizeof(cc), "CC:")) { /* Found a CC header */
+                               extract_address(cc);
+                               account = account_find_from_address(cc);
+                       }        
+               }
+       }
+
+       /* select current account */
+       if (!account) account = cur_account;
+       
+       return account;
+}