Clone also proxy-related prefs in account_clone().
[claws.git] / src / account.c
index ad6ba4e6358d734f10cb36d1585b3443ea2f335a..b51e7b8943a53fc6292d4d772e2634bec6859d18 100644 (file)
@@ -223,8 +223,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 = prefs_account_new();
-               prefs_account_read_config(ac_prefs, (gchar *)cur->data);
+               ac_prefs = prefs_account_new_from_config((gchar *)cur->data);
                account_list = g_list_append(account_list, ac_prefs);
                if (ac_prefs->is_default)
                        cur_account = ac_prefs;
@@ -451,7 +450,7 @@ void account_add(void)
        }
 }
 
-void account_open(PrefsAccount *ac_prefs)
+void account_open(PrefsAccount *ac_prefs, gboolean called_from_acc_list)
 {
        gboolean prev_default;
        gchar *ac_name, *old_prefix, *new_prefix;
@@ -472,7 +471,8 @@ void account_open(PrefsAccount *ac_prefs)
 
        prefs_account_open(ac_prefs, &account_dirty);
 
-       account_edit_focus();
+       if (called_from_acc_list)
+               account_edit_focus();
 
        if (account_dirty) {
                if (!prev_default && ac_prefs->is_default)
@@ -724,7 +724,6 @@ static void account_edit_create(void)
        debug_print("Creating account edit window...\n");
 
        window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "account");
-       gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
        gtk_container_set_border_width (GTK_CONTAINER (window), 8);
        gtk_window_set_title (GTK_WINDOW (window), _("Edit accounts"));
        g_signal_connect (G_OBJECT (window), "delete_event",
@@ -864,7 +863,7 @@ static void account_edit_prefs(GtkWidget *widget, gpointer data)
        ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
        
        if (ac_prefs) {
-               account_open(ac_prefs);
+               account_open(ac_prefs, TRUE);
                account_list_view_set();
        }               
 }
@@ -1054,12 +1053,45 @@ static void account_clone(GtkWidget *widget, gpointer data)
        ac_clon->is_default = FALSE;
        ACP_FASSIGN(folder);
 
+       ACP_FASSIGN(config_version);
+
+       ACP_FASSIGN(use_proxy);
+       ACP_FASSIGN(use_default_proxy);
+       ACP_FASSIGN(use_proxy_for_send);
+       ACP_FASSIGN(proxy_info.proxy_type);
+       ACP_FDUP(proxy_info.proxy_host);
+       ACP_FASSIGN(proxy_info.proxy_port);
+       ACP_FASSIGN(proxy_info.use_proxy_auth);
+       ACP_FDUP(proxy_info.proxy_name);
+       ACP_FDUP(proxy_info.proxy_pass);
+
        account_list = g_list_append(account_list, ac_clon);
        account_list_view_set();
 }
 #undef ACP_FDUP
 #undef ACP_FASSIGN
 
+static void account_empty_cache(PrefsAccount *ac_prefs)
+{
+       gchar *cache_dir;
+
+       cache_dir = prefs_account_cache_dir(ac_prefs, FALSE);
+       if (cache_dir == NULL)
+               return; /* no cache dir, nothing to do */
+
+       if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0) {
+               g_warning("can't remove directory '%s'", cache_dir);
+       } else {
+               gchar *server_dir =  prefs_account_cache_dir(ac_prefs, TRUE);
+               if (g_rmdir(server_dir) == 0)
+                       debug_print("Removed empty cache server directory\n");
+               else
+                       debug_print("Cache server directory not empty: not removed\n");
+               g_free(server_dir);
+       }
+       g_free(cache_dir);
+}
+
 static void account_delete(GtkWidget *widget, gpointer data)
 {
        PrefsAccount *ac_prefs;
@@ -1067,7 +1099,7 @@ static void account_delete(GtkWidget *widget, gpointer data)
        GList *list;
        Folder *folder;
        GSList *cur;
+
        ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
        if (ac_prefs == NULL)
                return;
@@ -1077,8 +1109,8 @@ static void account_delete(GtkWidget *widget, gpointer data)
                   ac_prefs->account_name ? ac_prefs->account_name :
                   _("(Untitled)"));
        if (alertpanel_full(_("Delete account"), buf,
-                           GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
-                           NULL, ALERT_WARNING, G_ALERTDEFAULT) != G_ALERTALTERNATE)
+                           GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, ALERTFOCUS_FIRST, FALSE,
+                           NULL, ALERT_WARNING) != G_ALERTALTERNATE)
                return;
        account_list_dirty = TRUE;
 
@@ -1124,6 +1156,10 @@ static void account_delete(GtkWidget *widget, gpointer data)
                        cur = g_slist_next(cur);
                }
        }
+
+       debug_print("Removing cache directory of this account...\n");
+       account_empty_cache(ac_prefs);
+
        folder_write_list();
 }
 
@@ -1289,22 +1325,22 @@ static void account_list_view_add(PrefsAccount *ac_prefs)
 #ifdef USE_GNUTLS
        protocol = ac_prefs->protocol == A_POP3 ?
                  (ac_prefs->ssl_pop == SSL_TUNNEL ?
-                  "POP3 (SSL)" :
+                  "POP (SSL/TLS)" :
                   ac_prefs->ssl_pop == SSL_STARTTLS ?
-                  "POP3 (TLS)" : "POP3") :
+                  "POP (STARTTLS)" : "POP") :
                   ac_prefs->protocol == A_IMAP4 ?
                  (ac_prefs->ssl_imap == SSL_TUNNEL ?
-                  "IMAP4 (SSL)" :
+                  "IMAP (SSL/TLS)" :
                   ac_prefs->ssl_imap == SSL_STARTTLS ?
-                  "IMAP4 (TLS)" : "IMAP4") :
+                  "IMAP (STARTTLS)" : "IMAP") :
                   ac_prefs->protocol == A_NNTP ?
                  (ac_prefs->ssl_nntp == SSL_TUNNEL ?
-                  "NNTP (SSL)" : "NNTP") :
+                  "NNTP (SSL/TLS)" : "NNTP") :
                   ac_prefs->protocol == A_LOCAL ? "Local" :
                   ac_prefs->protocol == A_NONE ?  "SMTP" : "-";
 #else
-       protocol = ac_prefs->protocol == A_POP3  ? "POP3" :
-                  ac_prefs->protocol == A_IMAP4 ? "IMAP4" :
+       protocol = ac_prefs->protocol == A_POP3  ? "POP" :
+                  ac_prefs->protocol == A_IMAP4 ? "IMAP" :
                   ac_prefs->protocol == A_LOCAL ? "Local" :
                   ac_prefs->protocol == A_NNTP  ? "NNTP" :
                   ac_prefs->protocol == A_NONE ?  "SMTP" : "-";
@@ -1403,13 +1439,14 @@ PrefsAccount *account_get_reply_account(MsgInfo *msginfo, gboolean reply_autosel
        else if (folder_has_parent_of_type(msginfo->folder, F_QUEUE) ||
                 folder_has_parent_of_type(msginfo->folder, F_OUTBOX) ||
                 folder_has_parent_of_type(msginfo->folder, F_DRAFT)) {
-                       gchar from[BUFFSIZE];
+                       gchar *from = NULL;
                        if (!procheader_get_header_from_msginfo
-                               (msginfo, from, sizeof from, "From:")) {
+                               (msginfo, &from, "From:")) {
                                gchar *buf = from + strlen("From:");
                                extract_address(buf);
                                account = account_find_from_address(buf, FALSE);
-                       }
+                       g_free(from);
+                       }
        }
        /* select account by to: and cc: header if enabled */
        if (reply_autosel) {
@@ -1439,13 +1476,14 @@ PrefsAccount *account_get_reply_account(MsgInfo *msginfo, gboolean reply_autosel
                        }
                }
                if (!account) {
-                       gchar deliveredto[BUFFSIZE];
+                       gchar *deliveredto = NULL;
                        if (!procheader_get_header_from_msginfo
-                               (msginfo, deliveredto,sizeof deliveredto , "Delivered-To:")) { 
+                               (msginfo, &deliveredto, "Delivered-To:")) {
                                gchar *buf = deliveredto + strlen("Delivered-To:");
                                extract_address(buf);
                                account = account_find_from_address(buf, FALSE);
-                       }
+                       g_free(deliveredto);
+                       }
                }
        }
 
@@ -1603,19 +1641,13 @@ static void account_create_list_view_columns(GtkWidget *list_view)
  */
 static gint account_list_view_get_selected_account_id(GtkWidget *list_view)
 {
-       GtkTreeSelection *selector;
-       GtkTreeModel *model;
-       GtkTreeIter iter;
        PrefsAccount *res = NULL;
 
-       selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
-       
-       if (!gtk_tree_selection_get_selected(selector, &model, &iter))
-               return -1;
-
-       gtk_tree_model_get(model, &iter, ACCOUNT_DATA, &res, -1);
+       res = (PrefsAccount *)gtkut_tree_view_get_selected_pointer(
+                       GTK_TREE_VIEW(list_view), ACCOUNT_DATA,
+                       NULL, NULL, NULL);
 
-       return res->account_id;                    
+       return (res != NULL ? res->account_id : -1);
 }
 
 /*!
@@ -1641,19 +1673,12 @@ static GtkTreePath *account_list_view_get_selected_account_path(GtkWidget *list_
  */
 static PrefsAccount *account_list_view_get_selected_account(GtkWidget *list_view)
 {
-       GtkTreeSelection *selector;
-       GtkTreeModel *model;
-       GtkTreeIter iter;
-       PrefsAccount *res = NULL;
+       PrefsAccount *res =
+               (PrefsAccount *)gtkut_tree_view_get_selected_pointer(
+                       GTK_TREE_VIEW(list_view), ACCOUNT_DATA,
+                       NULL, NULL, NULL);
 
-       selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
-       
-       if (!gtk_tree_selection_get_selected(selector, &model, &iter))
-               return NULL;
-
-       gtk_tree_model_get(model, &iter, ACCOUNT_DATA, &res, -1);
-
-       return res;                        
+       return res;
 }
 
 /*!
@@ -1833,23 +1858,30 @@ gchar *account_get_signature_str(PrefsAccount *account)
                return NULL;
 
        if (account->sig_type == SIG_FILE) {
-               if (!is_file_or_fifo_exist(account->sig_path)) {
-                       g_warning("can't open signature file: '%s'",
-                                 account->sig_path);
+               gchar *sig_full_path;
+               if (!g_path_is_absolute(account->sig_path)) {
+                       sig_full_path = g_build_filename(get_home_dir(), account->sig_path, NULL);
+               } else {
+                       sig_full_path = g_strdup(account->sig_path);
+               }
+
+               if (!is_file_or_fifo_exist(sig_full_path)) {
+                       g_warning("can't open signature file: '%s'", sig_full_path);
+                       g_free(sig_full_path);
                        return NULL;
                }
-       }
 
-       if (account->sig_type == SIG_COMMAND)
-               sig_body = get_command_output(account->sig_path);
-       else {
-               gchar *tmp;
+               debug_print("Reading signature from file '%s'\n", sig_full_path);
+               gchar *tmp = file_read_to_str(sig_full_path);
+               g_free(sig_full_path);
 
-               tmp = file_read_to_str(account->sig_path);
                if (!tmp)
                        return NULL;
+
                sig_body = normalize_newlines(tmp);
                g_free(tmp);
+       } else {
+               sig_body = get_command_output(account->sig_path);
        }
 
        if (account->sig_sep) {
@@ -1903,3 +1935,75 @@ gboolean password_get(const gchar *user,
        }
        return FALSE;
 }
+
+static GSList *account_signatures_list = NULL;
+
+/* create a list of unique signatures from accounts list */
+void account_signatures_matchlist_create(void)
+{
+       GList *cur_ac = NULL;
+       PrefsAccount *ac_prefs = NULL;
+
+       if (account_signatures_list)
+               return;
+
+       account_signatures_list = g_slist_prepend(account_signatures_list, g_strdup("-- "));
+       for (cur_ac = account_get_list();
+                cur_ac != NULL;
+                cur_ac = g_list_next(cur_ac)) {
+               ac_prefs = (PrefsAccount *)cur_ac->data;
+
+               if (ac_prefs->sig_sep && *ac_prefs->sig_sep != '\0') {
+                       if (!g_slist_find_custom(account_signatures_list, ac_prefs->sig_sep,
+                                       (GCompareFunc)strcmp2)) {
+                               account_signatures_list = g_slist_prepend(account_signatures_list,
+                                               g_strdup(ac_prefs->sig_sep));
+                       }
+               }
+       }
+}
+
+/* delete the list of signatures created by account_signatures_matchlist_create() */
+void account_signatures_matchlist_delete(void)
+{
+       if (account_signatures_list) {
+               slist_free_strings_full(account_signatures_list);
+               account_signatures_list = NULL;
+       }
+}
+
+/* match a string against all signatures in list, using the specified format */
+gboolean account_signatures_matchlist_str_found(const gchar *str, const gchar *format)
+{
+       gchar *tmp = NULL;
+       gboolean found = FALSE;
+       GSList *item;
+
+       for (item = account_signatures_list;
+                item != NULL && !found;
+                item = g_slist_next(item)) {
+               tmp = g_strdup_printf(format, (gchar *)item->data);
+               found = (strcmp(tmp, str) == 0);
+               g_free(tmp);
+       }
+       return found;
+}
+
+/* match M first char of a string against all signatures in list, using the specified format */
+gboolean account_signatures_matchlist_nchar_found(const gchar *str, const gchar *format)
+{
+       gchar *tmp = NULL;
+       gboolean found = FALSE;
+       GSList *item;
+       gint len;
+
+       for (item = account_signatures_list;
+                item != NULL && !found;
+                item = g_slist_next(item)) {
+               tmp = g_strdup_printf(format, (gchar *)item->data);
+               len = strlen(tmp);
+               found = (strncmp(tmp, str, len) == 0);
+               g_free(tmp);
+       }
+       return found;
+}