Sync with hiro's cvs 10 to 17.
[claws.git] / src / account.c
index cc65a457102ae051d6042eabe71ca35f07f56e2a..34ced31edb5ce16de16f21b10e160720c489d34b 100644 (file)
 #include "account.h"
 #include "prefs.h"
 #include "prefs_account.h"
+#include "prefs_folder_item.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"
+
 typedef enum
 {
        COL_DEFAULT     = 0,
-       COL_NAME        = 1,
-       COL_PROTOCOL    = 2,
-       COL_SERVER      = 3
+       COL_GETALL      = 1,
+       COL_NAME        = 2,
+       COL_PROTOCOL    = 3,
+       COL_SERVER      = 4
 } EditAccountColumnPos;
 
-# define N_EDIT_ACCOUNT_COLS   4
+# define N_EDIT_ACCOUNT_COLS   5
 
 #define PREFSBUFSIZE           1024
 
@@ -66,6 +73,13 @@ static struct EditAccount {
        GtkWidget *close_btn;
 } edit_account;
 
+static GdkPixmap *markxpm;
+static GdkBitmap *markxpmmask;
+static GdkPixmap *checkboxonxpm;
+static GdkPixmap *checkboxonxpmmask;
+static GdkPixmap *checkboxoffxpm;
+static GdkPixmap *checkboxoffxpmmask;
+
 static void account_edit_create                (void);
 
 static void account_edit_prefs         (void);
@@ -85,6 +99,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);
@@ -162,6 +179,9 @@ PrefsAccount *account_find_from_smtp_server(const gchar *address,
        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) &&
@@ -173,22 +193,24 @@ PrefsAccount *account_find_from_smtp_server(const gchar *address,
 }
 
 /*
- * account_find_mail_from_address:
+ * account_find_from_address:
  * @address: Email address string.
  *
  * Find a mail (not news) account with the specified email address.
  *
  * Return value: The found account, or NULL if not found.
  */
-PrefsAccount *account_find_mail_from_address(const gchar *address)
+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 &&
-                   !strcmp2(address, ac->address))
+               if (ac->protocol != A_NNTP && ac->address &&
+                   strcasestr(address, ac->address))
                        return ac;
        }
 
@@ -230,12 +252,12 @@ GList *account_get_list(void)
 
 void account_edit_open(void)
 {
-       inc_autocheck_timer_remove();
+       inc_lock();
 
        if (compose_get_compose_list()) {
                alertpanel_notice(_("Some composing windows are open.\n"
                                    "Please close all the composing windows before editing the accounts."));
-               inc_autocheck_timer_set();
+               inc_unlock();
                return;
        }
 
@@ -258,7 +280,6 @@ void account_add(void)
        PrefsAccount *ac_prefs;
 
        ac_prefs = prefs_account_open(NULL);
-       inc_autocheck_timer_remove();
 
        if (!ac_prefs) return;
 
@@ -368,6 +389,7 @@ static void account_edit_create(void)
 {
        GtkWidget *window;
        GtkWidget *vbox;
+       GtkWidget *label;
        GtkWidget *hbox;
        GtkWidget *scrolledwin;
        GtkWidget *clist;
@@ -401,11 +423,23 @@ static void account_edit_create(void)
                            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);
+       gtk_widget_realize(window);
 
-       vbox = gtk_vbox_new (FALSE, 12);
+       vbox = gtk_vbox_new (FALSE, 10);
        gtk_widget_show (vbox);
        gtk_container_add (GTK_CONTAINER (window), vbox);
 
+       hbox = gtk_hbox_new (FALSE, 0);
+       gtk_widget_show (hbox);
+       gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
+
+       label = gtk_label_new
+               (_("New messages will be checked in this order. Check the boxes\n"
+                  "on the `G' column to enable message retrieval by `Get all'."));
+       gtk_widget_show (label);
+       gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
+       gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
+
        hbox = gtk_hbox_new (FALSE, 8);
        gtk_widget_show (hbox);
        gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
@@ -418,7 +452,8 @@ static void account_edit_create(void)
                                        GTK_POLICY_AUTOMATIC,
                                        GTK_POLICY_AUTOMATIC);
 
-       titles[COL_DEFAULT]  = "";
+       titles[COL_DEFAULT]  = "D";
+       titles[COL_GETALL]   = "G";
        titles[COL_NAME]     = _("Name");
        titles[COL_PROTOCOL] = _("Protocol");
        titles[COL_SERVER]   = _("Server");
@@ -426,10 +461,15 @@ static void account_edit_create(void)
        clist = gtk_clist_new_with_titles (N_EDIT_ACCOUNT_COLS, titles);
        gtk_widget_show (clist);
        gtk_container_add (GTK_CONTAINER (scrolledwin), clist);
-       gtk_clist_set_column_width (GTK_CLIST(clist), COL_DEFAULT , 16);
+       gtk_clist_set_column_width (GTK_CLIST(clist), COL_DEFAULT , 10);
+       gtk_clist_set_column_width (GTK_CLIST(clist), COL_GETALL  , 11);
        gtk_clist_set_column_width (GTK_CLIST(clist), COL_NAME    , 100);
-       gtk_clist_set_column_width (GTK_CLIST(clist), COL_PROTOCOL, 70);
+       gtk_clist_set_column_width (GTK_CLIST(clist), COL_PROTOCOL, 100);
        gtk_clist_set_column_width (GTK_CLIST(clist), COL_SERVER  , 100);
+       gtk_clist_set_column_justification (GTK_CLIST(clist), COL_DEFAULT,
+                                           GTK_JUSTIFY_CENTER);
+       gtk_clist_set_column_justification (GTK_CLIST(clist), COL_GETALL,
+                                           GTK_JUSTIFY_CENTER);
        gtk_clist_set_selection_mode (GTK_CLIST(clist), GTK_SELECTION_BROWSE);
 
        for (i = 0; i < N_EDIT_ACCOUNT_COLS; i++)
@@ -438,6 +478,8 @@ static void account_edit_create(void)
 
        gtk_signal_connect (GTK_OBJECT (clist), "select_row",
                            GTK_SIGNAL_FUNC (account_selected), NULL);
+       gtk_signal_connect (GTK_OBJECT (clist), "row_move",
+                           GTK_SIGNAL_FUNC (account_row_moved), NULL);
 
        vbox2 = gtk_vbox_new (FALSE, 0);
        gtk_widget_show (vbox2);
@@ -497,6 +539,12 @@ static void account_edit_create(void)
                            GTK_SIGNAL_FUNC (account_edit_close),
                            NULL);
 
+       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;
        edit_account.close_btn = close_btn;
@@ -518,7 +566,6 @@ static void account_edit_prefs(void)
        Xstrdup_a(ac_name, ac_prefs->account_name, return);
 
        prefs_account_open(ac_prefs);
-       inc_autocheck_timer_remove();
 
        if (!prev_default && ac_prefs->is_default)
                account_set_as_default(ac_prefs);
@@ -533,12 +580,34 @@ static void account_edit_prefs(void)
        account_clist_set();
 }
 
+static gboolean account_delete_references_func(GNode *node, gpointer data)
+{
+       FolderItem *item;
+       gint account;
+
+       g_return_val_if_fail(node->data != NULL, FALSE);
+
+       item = FOLDER_ITEM(node->data);
+       account = GPOINTER_TO_INT(data);
+
+       if(!item->prefs) /* && item->prefs->stype == F_NORMAL */
+               return FALSE;
+       if(item->prefs->default_account != account)
+               return FALSE;
+       
+       item->prefs->enable_default_account = FALSE;
+       item->prefs->default_account = 0;
+       prefs_folder_item_save_config(item);
+}
+
 static void account_delete(void)
 {
        GtkCList *clist = GTK_CLIST(edit_account.clist);
        PrefsAccount *ac_prefs;
        gint row;
-
+       GList *list;
+       Folder *folder;
+       
        if (!clist->selection) return;
 
        if (alertpanel(_("Delete account"),
@@ -554,6 +623,17 @@ static void account_delete(void)
        }
        account_destroy(ac_prefs);
        account_clist_set();
+
+       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);
+               if (folder->node)  /* && folder->type == F_? */
+                       g_node_traverse(folder->node, G_PRE_ORDER,
+                               G_TRAVERSE_ALL, -1,
+                               account_delete_references_func,
+                               GINT_TO_POINTER(ac_prefs->account_id));
+       }
 }
 
 static void account_up(void)
@@ -564,10 +644,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)
@@ -578,10 +656,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)
@@ -618,7 +694,7 @@ static void account_edit_close(void)
 
        gtk_widget_hide(edit_account.window);
 
-       inc_autocheck_timer_set();
+       inc_unlock();
 }
 
 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
@@ -633,6 +709,25 @@ static void account_selected(GtkCList *clist, gint row, gint column,
 {
        if (event && event->type == GDK_2BUTTON_PRESS)
                account_edit_prefs();
+
+       if (column == COL_GETALL) {
+               PrefsAccount *ac;
+
+               ac = gtk_clist_get_row_data(clist, row);
+               if (ac->protocol == A_POP3 || ac->protocol == A_APOP) {
+                       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,
+                                source_row < dest_row ? 1.0 : 0.0, 0.0);
+       }
 }
 
 static void account_key_pressed(GtkWidget *widget, GdkEventKey *event,
@@ -647,14 +742,29 @@ static gint account_clist_set_row(PrefsAccount *ac_prefs, gint row)
 {
        GtkCList *clist = GTK_CLIST(edit_account.clist);
        gchar *text[N_EDIT_ACCOUNT_COLS];
-
-       text[COL_DEFAULT] = ac_prefs->is_default ? "*" : "";
-       text[COL_NAME] = ac_prefs->account_name;
-       text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3  ? "POP3"  :
-                            ac_prefs->protocol == A_APOP  ? "APOP"  :
+       gboolean has_getallbox;
+       gboolean getall;
+
+       text[COL_DEFAULT] = "";
+       text[COL_GETALL]  = "";
+       text[COL_NAME]    = ac_prefs->account_name;
+#if USE_SSL
+       text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3 ?
+                            (ac_prefs->ssl_pop ? "POP3 (SSL)" : "POP3") :
+                            ac_prefs->protocol == A_APOP ?
+                            (ac_prefs->ssl_pop ?
+                             "POP3 (APOP, SSL)" : "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"  :  "";
+#else
+       text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3  ? "POP3" :
+                            ac_prefs->protocol == A_APOP  ? "POP3 (APOP)" :
                             ac_prefs->protocol == A_IMAP4 ? "IMAP4" :
                             ac_prefs->protocol == A_LOCAL ? "Local" :
-                            ac_prefs->protocol == A_NNTP  ? "NNTP"  :  "";
+                            ac_prefs->protocol == A_NNTP  ? "NNTP" : "";
+#endif
        text[COL_SERVER] = ac_prefs->protocol == A_NNTP
                ? ac_prefs->nntp_server : ac_prefs->recv_server;
 
@@ -662,11 +772,26 @@ static gint account_clist_set_row(PrefsAccount *ac_prefs, gint row)
                row = gtk_clist_append(clist, text);
        else {
                gtk_clist_set_text(clist, row, COL_DEFAULT, text[COL_DEFAULT]);
+               gtk_clist_set_text(clist, row, COL_GETALL, text[COL_GETALL]);
                gtk_clist_set_text(clist, row, COL_NAME, text[COL_NAME]);
                gtk_clist_set_text(clist, row, COL_PROTOCOL, text[COL_PROTOCOL]);
                gtk_clist_set_text(clist, row, COL_SERVER, text[COL_SERVER]);
        }
 
+       has_getallbox = (ac_prefs->protocol == A_POP3 ||
+                        ac_prefs->protocol == A_APOP);
+       getall = has_getallbox && ac_prefs->recv_at_getall;
+
+       if (ac_prefs->is_default)
+               gtk_clist_set_pixmap(clist, row, COL_DEFAULT,
+                                    markxpm, markxpmmask);
+       if (getall)
+               gtk_clist_set_pixmap(clist, row, COL_GETALL,
+                                    checkboxonxpm, checkboxonxpmmask);
+       else if (has_getallbox)
+               gtk_clist_set_pixmap(clist, row, COL_GETALL,
+                                    checkboxoffxpm, checkboxoffxpmmask);
+
        gtk_clist_set_row_data(clist, row, ac_prefs);
 
        return row;
@@ -677,19 +802,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);
@@ -698,10 +819,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);
 }