sync with sylpheed 0.6.0cvs4
[claws.git] / src / inc.c
index cd1ccc555fffc71eea3e5ebfc88ca703dd37c889..b86a174c7b8a83f8ebcaf272738974451a43f129 100644 (file)
--- a/src/inc.c
+++ b/src/inc.c
@@ -27,6 +27,7 @@
 #include <gtk/gtkmain.h>
 #include <gtk/gtkwindow.h>
 #include <gtk/gtksignal.h>
+#include <gtk/gtkprogressbar.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
@@ -47,6 +48,7 @@
 #include "account.h"
 #include "procmsg.h"
 #include "socket.h"
+#include "ssl.h"
 #include "pop.h"
 #include "recv.h"
 #include "mbox.h"
 #include "pixmaps/complete.xpm"
 #include "pixmaps/error.xpm"
 
-GdkPixmap *currentxpm;
-GdkBitmap *currentxpmmask;
-GdkPixmap *errorxpm;
-GdkBitmap *errorxpmmask;
-GdkPixmap *okxpm;
-GdkBitmap *okxpmmask;
+static guint inc_lock_count = 0;
+
+static GdkPixmap *currentxpm;
+static GdkBitmap *currentxpmmask;
+static GdkPixmap *errorxpm;
+static GdkBitmap *errorxpmmask;
+static GdkPixmap *okxpm;
+static GdkBitmap *okxpmmask;
 
 #define MSGBUFSIZE     8192
 
-static void inc_finished               (MainWindow             *mainwin);
-static void inc_account_mail           (PrefsAccount           *account,
+static void inc_finished               (MainWindow             *mainwin,
+                                        gboolean                new_messages);
+static gint inc_account_mail           (PrefsAccount           *account,
                                         MainWindow             *mainwin);
 
 static IncProgressDialog *inc_progress_dialog_create   (void);
@@ -86,7 +91,7 @@ static IncSession *inc_session_new    (PrefsAccount           *account);
 static void inc_session_destroy                (IncSession             *session);
 static Pop3State *inc_pop3_state_new   (PrefsAccount           *account);
 static void inc_pop3_state_destroy     (Pop3State              *state);
-static void inc_start                  (IncProgressDialog      *inc_dialog);
+static gint inc_start                  (IncProgressDialog      *inc_dialog);
 static IncState inc_pop3_session_do    (IncSession             *session);
 static gint pop3_automaton_terminate   (SockInfo               *source,
                                         Automaton              *atm);
@@ -113,10 +118,19 @@ static gint get_spool                     (FolderItem     *dest,
                                         const gchar    *mbox);
 
 static void inc_all_spool(void);
-
-static gint inc_autocheck_func         (gpointer        data);
-
-static void inc_finished(MainWindow *mainwin)
+static void inc_autocheck_timer_set_interval   (guint           interval);
+static gint inc_autocheck_func                 (gpointer        data);
+
+/**
+ * inc_finished:
+ * @mainwin: Main window.
+ * @new_messages: TRUE if some messages have been received.
+ * 
+ * Update the folder view and the summary view after receiving
+ * messages.  If @new_messages is FALSE, this function avoids unneeded
+ * updating.
+ **/
+static void inc_finished(MainWindow *mainwin, gboolean new_messages)
 {
        FolderItem *item;
 
@@ -139,6 +153,7 @@ static void inc_finished(MainWindow *mainwin)
 
        /* XXX: filtering_happened is reset by summary_show() */
 
+       if (!new_messages && !prefs_common.scan_all_after_inc) return;
 
        if (prefs_common.open_inbox_on_inc) {
                item = cur_account && cur_account->inbox
@@ -155,8 +170,13 @@ static void inc_finished(MainWindow *mainwin)
 
 void inc_mail(MainWindow *mainwin)
 {
+       gint new_msgs = 0;
+
+       if (inc_lock_count) return;
+
        inc_autocheck_timer_remove();
        summary_write_cache(mainwin->summaryview);
+       main_window_lock(mainwin);
 
        if (prefs_common.use_extinc && prefs_common.extinc_path) {
                gint pid;
@@ -164,6 +184,7 @@ void inc_mail(MainWindow *mainwin)
                /* external incorporating program */
                if ((pid = fork()) < 0) {
                        perror("fork");
+                       main_window_unlock(mainwin);
                        inc_autocheck_timer_set();
                        return;
                }
@@ -181,27 +202,28 @@ void inc_mail(MainWindow *mainwin)
                /* wait until child process is terminated */
                waitpid(pid, NULL, 0);
 
-               if (prefs_common.inc_local) inc_spool();
-               inc_all_spool();
+               if (prefs_common.inc_local)
+                       new_msgs = inc_spool();
        } else {
-               if (prefs_common.inc_local) inc_spool();
-               inc_all_spool();
+               if (prefs_common.inc_local)
+                       new_msgs = inc_spool();
 
-               inc_account_mail(cur_account, mainwin);
+               new_msgs += inc_account_mail(cur_account, mainwin);
        }
 
-       inc_finished(mainwin);
+       inc_finished(mainwin, new_msgs > 0);
+       main_window_unlock(mainwin);
        inc_autocheck_timer_set();
 }
 
-static void inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
+static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
 {
        IncProgressDialog *inc_dialog;
        IncSession *session;
        gchar *text[3];
 
        session = inc_session_new(account);
-       if (!session) return;
+       if (!session) return 0;
 
        inc_dialog = inc_progress_dialog_create();
        inc_dialog->queue_list = g_list_append(inc_dialog->queue_list, session);
@@ -213,22 +235,28 @@ static void inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
        text[2] = _("Standby");
        gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
 
-       inc_start(inc_dialog);
+       return inc_start(inc_dialog);
 }
 
 void inc_all_account_mail(MainWindow *mainwin)
 {
        GList *list, *queue_list = NULL;
        IncProgressDialog *inc_dialog;
+       gint new_msgs = 0;
+
+       if (inc_lock_count) return;
 
        inc_autocheck_timer_remove();
        summary_write_cache(mainwin->summaryview);
+       main_window_lock(mainwin);
 
-       if (prefs_common.inc_local) inc_spool();
+       if (prefs_common.inc_local)
+               new_msgs = inc_spool();
 
        list = account_get_list();
        if (!list) {
-               inc_finished(mainwin);
+               inc_finished(mainwin, new_msgs > 0);
+               main_window_unlock(mainwin);
                inc_autocheck_timer_set();
                return;
        }
@@ -245,7 +273,8 @@ void inc_all_account_mail(MainWindow *mainwin)
        }
 
        if (!queue_list) {
-               inc_finished(mainwin);
+               inc_finished(mainwin, new_msgs > 0);
+               main_window_unlock(mainwin);
                inc_autocheck_timer_set();
                return;
        }
@@ -265,9 +294,10 @@ void inc_all_account_mail(MainWindow *mainwin)
                gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
        }
 
-       inc_start(inc_dialog);
+       new_msgs += inc_start(inc_dialog);
 
-       inc_finished(mainwin);
+       inc_finished(mainwin, new_msgs > 0);
+       main_window_unlock(mainwin);
        inc_autocheck_timer_set();
 }
 
@@ -285,25 +315,18 @@ static IncProgressDialog *inc_progress_dialog_create(void)
                           GTK_SIGNAL_FUNC(inc_cancel), dialog);
        gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
                           GTK_SIGNAL_FUNC(gtk_true), NULL);
-       if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
-           ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
-               manage_window_set_transient(GTK_WINDOW(progress->window));
-       }
 
        progress_dialog_set_value(progress, 0.0);
 
-       if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
-           ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
-               gtk_widget_show(progress->window);
-       }
-
        PIXMAP_CREATE(progress->clist, okxpm, okxpmmask, complete_xpm);
        PIXMAP_CREATE(progress->clist,
                      currentxpm, currentxpmmask, continue_xpm);
        PIXMAP_CREATE(progress->clist, errorxpm, errorxpmmask, error_xpm);
 
-       if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
-           ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
+       if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
+           (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
+            manage_window_get_focus_window())) {
+               dialog->show_dialog = TRUE;
                gtk_widget_show_now(progress->window);
        }
 
@@ -317,12 +340,16 @@ static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
 {
        progress_dialog_set_value(inc_dialog->dialog, 0.0);
        progress_dialog_set_label(inc_dialog->dialog, "");
+       gtk_progress_bar_update
+               (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
 }
 
 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
 {
        g_return_if_fail(inc_dialog != NULL);
 
+       gtk_progress_bar_update
+               (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
        progress_dialog_destroy(inc_dialog->dialog);
 
        g_free(inc_dialog);
@@ -392,7 +419,7 @@ static void inc_pop3_state_destroy(Pop3State *state)
        g_free(state);
 }
 
-static void inc_start(IncProgressDialog *inc_dialog)
+static gint inc_start(IncProgressDialog *inc_dialog)
 {
        IncSession *session;
        GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
@@ -400,6 +427,10 @@ static void inc_start(IncProgressDialog *inc_dialog)
        IncState inc_state;
        gint num = 0;
        gint error_num = 0;
+       gint new_msgs = 0;
+
+       /* gtk_label_set_text(GTK_LABEL(inc_dialog->mainwin->statuslabel),
+                          _("Receiving"));  */
 
        while (inc_dialog->queue_list != NULL) {
                session = inc_dialog->queue_list->data;
@@ -407,7 +438,7 @@ static void inc_start(IncProgressDialog *inc_dialog)
 
                inc_progress_dialog_clear(inc_dialog);
 
-               gtk_clist_moveto(clist, num, 0, 1.0, 0.0);
+               gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
 
                pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
                if (pop3_state->ac_prefs->passwd)
@@ -428,11 +459,10 @@ static void inc_start(IncProgressDialog *inc_dialog)
                        pass = input_dialog_with_invisible(_("Input password"),
                                                           message, NULL);
                        g_free(message);
-                       if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
-                           ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
-                               manage_window_focus_in(inc_dialog->mainwin->window,
-                                                      NULL, NULL);
-                       }
+                       if (inc_dialog->show_dialog)
+                               manage_window_focus_in
+                                       (inc_dialog->mainwin->window,
+                                        NULL, NULL);
                        if (pass) {
                                pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
                                pop3_state->pass = pass;
@@ -470,8 +500,8 @@ static void inc_start(IncProgressDialog *inc_dialog)
 
                if (pop3_state->error_val == PS_AUTHFAIL) {
                        if(!prefs_common.noerrorpanel) {
-                               if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
-                                   ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
+                               if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
+                                   ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
                                        manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
                                }
                                alertpanel_error
@@ -482,11 +512,9 @@ static void inc_start(IncProgressDialog *inc_dialog)
                }
 
                statusbar_pop_all();
-               if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
-                   ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
-                       manage_window_focus_in(inc_dialog->mainwin->window, NULL, NULL);
-               }
-               
+
+               new_msgs += pop3_state->cur_total_num;
+
                if (!prefs_common.scan_all_after_inc) {
                        folder_item_scan_foreach(pop3_state->folder_table);
                        folderview_update_item_foreach
@@ -516,10 +544,16 @@ static void inc_start(IncProgressDialog *inc_dialog)
                num++;
        }
 
-       if (error_num) {
-               manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
+       /* gtk_label_set_text(GTK_LABEL(inc_dialog->mainwin->statuslabel), "");  */
+
+       if (error_num && !prefs_common.noerrorpanel) {
+               if (inc_dialog->show_dialog)
+                       manage_window_focus_in(inc_dialog->dialog->window,
+                                              NULL, NULL);
                alertpanel_error(_("Some errors occured while getting mail."));
-               manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
+               if (inc_dialog->show_dialog)
+                       manage_window_focus_out(inc_dialog->dialog->window,
+                                               NULL, NULL);
        }
 
        while (inc_dialog->queue_list != NULL) {
@@ -530,8 +564,9 @@ static void inc_start(IncProgressDialog *inc_dialog)
        }
 
        inc_progress_dialog_destroy(inc_dialog);
-}
 
+       return new_msgs;
+}
 
 static IncState inc_pop3_session_do(IncSession *session)
 {
@@ -585,7 +620,8 @@ static IncState inc_pop3_session_do(IncSession *session)
        server = pop3_state->ac_prefs->recv_server;
 #if USE_SSL
        port = pop3_state->ac_prefs->set_popport ?
-               pop3_state->ac_prefs->popport : (pop3_state->ac_prefs->ssl_pop ? 995 : 110);
+               pop3_state->ac_prefs->popport :
+               pop3_state->ac_prefs->ssl_pop ? 995 : 110;
 #else
        port = pop3_state->ac_prefs->set_popport ?
                pop3_state->ac_prefs->popport : 110;
@@ -596,6 +632,7 @@ static IncState inc_pop3_session_do(IncSession *session)
        progress_dialog_set_label(inc_dialog->dialog, buf);
        g_free(buf);
        GTK_EVENTS_FLUSH();
+       statusbar_pop_all();
 
 #if USE_THREADS
        if ((sockinfo = sock_connect_with_thread(server, port)) == NULL) {
@@ -605,8 +642,8 @@ static IncState inc_pop3_session_do(IncSession *session)
                log_warning(_("Can't connect to POP3 server: %s:%d\n"),
                            server, port);
                if(!prefs_common.noerrorpanel) {
-                       if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
-                           ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
+                       if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
+                           ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
                                manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
                        }
                        alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
@@ -615,29 +652,23 @@ static IncState inc_pop3_session_do(IncSession *session)
                }
                pop3_automaton_terminate(NULL, atm);
                automaton_destroy(atm);
+               return INC_CONNECT_ERROR;
+       }
 
+#if USE_SSL
+       if (pop3_state->ac_prefs->ssl_pop && !ssl_init_socket(sockinfo)) {
+               pop3_automaton_terminate(NULL, atm);
+               automaton_destroy(atm);
                return INC_CONNECT_ERROR;
        }
+#endif
 
        /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
         * to the sock structure - implement a reference counter?? */
        pop3_state->sockinfo = sockinfo;
        atm->help_sock = sockinfo;
 
-#ifdef USE_SSL
-       if(pop3_state->ac_prefs->ssl_pop) {
-               if(!ssl_init_socket(sockinfo)) {
-                       pop3_automaton_terminate(NULL, atm);
-                       automaton_destroy(atm);
-                       
-                       return INC_CONNECT_ERROR;
-               }
-       } else {
-               sockinfo->ssl = NULL;
-       }
-#endif
-
-
+       log_verbosity_set(TRUE);
        recv_set_ui_func(inc_pop3_recv_func, session);
 
 #if USE_THREADS
@@ -651,16 +682,11 @@ static IncState inc_pop3_session_do(IncSession *session)
 
        gtk_main();
 
+       log_verbosity_set(FALSE);
        recv_set_ui_func(NULL, NULL);
 
 #if USE_THREADS
-/*
-       pthread_join(sockinfo->connect_thr, NULL);
-*/     
-#endif
-
-#if USE_SSL
-       ssl_done_socket(sockinfo);
+       //pthread_join(sockinfo->connect_thr, NULL);
 #endif
        automaton_destroy(atm);
 
@@ -763,8 +789,8 @@ static gint connection_check_cb(Automaton *atm)
                log_warning(_("Can't connect to POP3 server: %s:%d\n"),
                            sockinfo->hostname, sockinfo->port);
                if(!prefs_common.noerrorpanel) {
-                       if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
-                           ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
+                       if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
+                           ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
                                manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
                        }
                        alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
@@ -809,6 +835,9 @@ static void inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
 
        progress_dialog_set_percentage
                (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
+       gtk_progress_bar_update
+               (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
+                (gfloat)cur_total / (gfloat)state->total_bytes);
        GTK_EVENTS_FLUSH();
 }
 
@@ -828,7 +857,7 @@ void inc_progress_update(Pop3State *state, Pop3Phase phase)
        case POP3_GETAUTH_PASS_RECV:
        case POP3_GETAUTH_APOP_SEND:
        case POP3_GETAUTH_APOP_RECV:
-               progress_dialog_set_label(dialog, _("Authorizing..."));
+               progress_dialog_set_label(dialog, _("Authenticating..."));
                break;
        case POP3_GETRANGE_STAT_SEND:
        case POP3_GETRANGE_STAT_RECV:
@@ -863,6 +892,10 @@ void inc_progress_update(Pop3State *state, Pop3Phase phase)
                        (dialog,
                         (gfloat)(state->cur_total_bytes) /
                         (gfloat)(state->total_bytes));
+               gtk_progress_bar_update
+                       (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
+                        (gfloat)(state->cur_total_bytes) /
+                        (gfloat)(state->total_bytes));
                break;
        case POP3_DELETE_SEND:
        case POP3_DELETE_RECV:
@@ -1067,6 +1100,17 @@ static gint get_spool(FolderItem *dest, const gchar *mbox)
        return msgs;
 }
 
+void inc_lock(void)
+{
+       inc_lock_count++;
+}
+
+void inc_unlock(void)
+{
+       if (inc_lock_count > 0)
+               inc_lock_count--;
+}
+
 static guint autocheck_timer = 0;
 static gpointer autocheck_data = NULL;
 
@@ -1076,21 +1120,26 @@ void inc_autocheck_timer_init(MainWindow *mainwin)
        inc_autocheck_timer_set();
 }
 
-void inc_autocheck_timer_set(void)
+static void inc_autocheck_timer_set_interval(guint interval)
 {
        inc_autocheck_timer_remove();
 
        if (prefs_common.autochk_newmail && autocheck_data) {
                autocheck_timer = gtk_timeout_add
-                       (prefs_common.autochk_itv * 60000,
-                        inc_autocheck_func,
-                        autocheck_data);
+                       (interval, inc_autocheck_func, autocheck_data);
+               debug_print("added timer = %d\n", autocheck_timer);
        }
 }
 
+void inc_autocheck_timer_set(void)
+{
+       inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
+}
+
 void inc_autocheck_timer_remove(void)
 {
        if (autocheck_timer) {
+               debug_print("removed timer = %d\n", autocheck_timer);
                gtk_timeout_remove(autocheck_timer);
                autocheck_timer = 0;
        }
@@ -1100,6 +1149,12 @@ static gint inc_autocheck_func(gpointer data)
 {
        MainWindow *mainwin = (MainWindow *)data;
 
+       if (inc_lock_count) {
+               debug_print("autocheck is locked.\n");
+               inc_autocheck_timer_set_interval(1000);
+               return FALSE;
+       }
+
        inc_all_account_mail(mainwin);
 
        return FALSE;