allow 'local_mbox' value to point to either a directory or a file
[claws.git] / src / inc.c
index a3ec3c366fd93c85a34c6c20e014a0a4ad9e7e55..3850358756ed7154cc29d6cd12abce4484a7784c 100644 (file)
--- a/src/inc.c
+++ b/src/inc.c
@@ -84,7 +84,8 @@ static void inc_finished              (MainWindow             *mainwin,
 static gint inc_account_mail           (PrefsAccount           *account,
                                         MainWindow             *mainwin);
 
-static IncProgressDialog *inc_progress_dialog_create   (void);
+static IncProgressDialog *inc_progress_dialog_create
+                                       (gboolean                autocheck);
 static void inc_progress_dialog_set_list(IncProgressDialog     *inc_dialog);
 static void inc_progress_dialog_destroy        (IncProgressDialog      *inc_dialog);
 
@@ -117,7 +118,6 @@ static gint inc_dialog_delete_cb    (GtkWidget      *widget,
                                         GdkEventAny    *event,
                                         gpointer        data);
 
-static gint inc_spool                  (void);
 static gint get_spool                  (FolderItem     *dest,
                                         const gchar    *mbox);
 
@@ -128,10 +128,6 @@ static gint inc_autocheck_func                     (gpointer        data);
 
 static void inc_notify_cmd             (gint new_msgs, 
                                         gboolean notify);
-
-#define FOLDER_SUMMARY_MISMATCH(f, s) \
-       (f) && (s) ? ((s)->newmsgs != (f)->new_msgs) || ((f)->unread_msgs != (s)->unread) || ((f)->total_msgs != (s)->messages) \
-       : FALSE
        
 /**
  * inc_finished:
@@ -155,10 +151,8 @@ static void inc_finished(MainWindow *mainwin, gboolean new_messages)
                item = cur_account && cur_account->inbox
                        ? folder_find_item_from_identifier(cur_account->inbox)
                        : folder_get_default_inbox();
-               if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {      
-                       folderview_unselect(mainwin->folderview);
-                       folderview_select(mainwin->folderview, item);
-               }       
+               folderview_unselect(mainwin->folderview);
+               folderview_select(mainwin->folderview, item);
        }
 }
 
@@ -186,19 +180,7 @@ void inc_mail(MainWindow *mainwin, gboolean notify)
                        inc_autocheck_timer_set();
                        return;
                }
-
-               if (prefs_common.inc_local) {
-                       account_new_msgs = inc_spool();
-                       if (account_new_msgs > 0)
-                               new_msgs += account_new_msgs;
-               }
        } else {
-               if (prefs_common.inc_local) {
-                       account_new_msgs = inc_spool();
-                       if (account_new_msgs > 0)
-                               new_msgs += account_new_msgs;
-               }
-
                account_new_msgs = inc_account_mail(cur_account, mainwin);
                if (account_new_msgs > 0)
                        new_msgs += account_new_msgs;
@@ -213,7 +195,8 @@ void inc_mail(MainWindow *mainwin, gboolean notify)
 
 void inc_pop_before_smtp(PrefsAccount *acc)
 {
-       inc_account_mail(acc, NULL);
+       /* FIXME: assumes to attach to first main window */
+       inc_account_mail(acc, mainwindow_get_mainwindow());
 }
 
 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
@@ -240,7 +223,7 @@ static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
                session = inc_session_new(account);
                if (!session) return 0;
                
-               inc_dialog = inc_progress_dialog_create();
+               inc_dialog = inc_progress_dialog_create(FALSE);
                inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
                                                       session);
                inc_dialog->mainwin = mainwin;
@@ -262,7 +245,8 @@ static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
        return 0;
 }
 
-void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
+void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
+                         gboolean notify)
 {
        GList *list, *queue_list = NULL;
        IncProgressDialog *inc_dialog;
@@ -280,12 +264,6 @@ void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
        inc_autocheck_timer_remove();
        main_window_lock(mainwin);
 
-       if (prefs_common.inc_local) {
-               account_new_msgs = inc_spool();
-               if (account_new_msgs > 0)
-                       new_msgs += account_new_msgs;   
-       }
-
        list = account_get_list();
        if (!list) {
                inc_finished(mainwin, new_msgs > 0);
@@ -329,7 +307,7 @@ void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
                return;
        }
 
-       inc_dialog = inc_progress_dialog_create();
+       inc_dialog = inc_progress_dialog_create(autocheck);
        inc_dialog->queue_list = queue_list;
        inc_dialog->mainwin = mainwin;
        inc_progress_dialog_set_list(inc_dialog);
@@ -344,7 +322,7 @@ void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
        inc_autocheck_timer_set();
 }
 
-static IncProgressDialog *inc_progress_dialog_create(void)
+static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
 {
        IncProgressDialog *dialog;
        ProgressDialog *progress;
@@ -370,8 +348,8 @@ static IncProgressDialog *inc_progress_dialog_create(void)
                         &errorxpm, &errorxpmmask);
 
        if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
-           (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
-            manage_window_get_focus_window())) {
+           (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
+            !autocheck)) {
                dialog->show_dialog = TRUE;
                gtk_widget_show_now(progress->window);
        }
@@ -407,12 +385,11 @@ 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, "");
-       if (inc_dialog->mainwin) {
-               gtk_progress_bar_update
-                       (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
+       if (inc_dialog->mainwin)
                gtk_progress_set_show_text
                        (GTK_PROGRESS(inc_dialog->mainwin->progressbar), FALSE);
-       }
+       gtk_progress_bar_update
+               (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
 }
 
 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
@@ -421,12 +398,12 @@ static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
 
        inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
 
-       if (inc_dialog->mainwin) {
+       if (inc_dialog->mainwin)
                gtk_progress_set_show_text
                        (GTK_PROGRESS(inc_dialog->mainwin->progressbar), FALSE);
-               gtk_progress_bar_update
-                       (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
-       }
+       gtk_progress_bar_update
+               (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
+
        progress_dialog_destroy(inc_dialog->dialog);
 
        g_free(inc_dialog);
@@ -575,6 +552,7 @@ static gint inc_start(IncProgressDialog *inc_dialog)
                case INC_NO_SPACE:
                case INC_IO_ERROR:
                case INC_SOCKET_ERROR:
+               case INC_EOF:
                        SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask, _("Error"));
                        break;
                case INC_CANCEL:
@@ -587,7 +565,7 @@ static gint inc_start(IncProgressDialog *inc_dialog)
                if (pop3_session->error_val == PS_AUTHFAIL) {
                        if(!prefs_common.no_recv_err_panel) {
                                if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
-                                   ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
+                                   ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
                                        manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
                                }
                                alertpanel_error
@@ -744,7 +722,7 @@ static IncState inc_pop3_session_do(IncSession *session)
                            server, port);
                if(!prefs_common.no_recv_err_panel) {
                        if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
-                           ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
+                           ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
                                manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
                        }
                        alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
@@ -756,8 +734,7 @@ static IncState inc_pop3_session_do(IncSession *session)
                return INC_CONNECT_ERROR;
        }
 
-       while (SESSION(pop3_session)->state != SESSION_DISCONNECTED &&
-              SESSION(pop3_session)->state != SESSION_ERROR &&
+       while (session_is_connected(SESSION(pop3_session)) &&
               session->inc_state != INC_CANCEL)
                gtk_main_iteration();
 
@@ -771,7 +748,9 @@ static IncState inc_pop3_session_do(IncSession *session)
                                        session->inc_state = INC_CONNECT_ERROR;
                                else
                                        session->inc_state = INC_ERROR;
-                       } else
+                       } else if (SESSION(pop3_session)->state == SESSION_EOF)
+                               session->inc_state = INC_EOF;
+                       else
                                session->inc_state = INC_SUCCESS;
                        break;
                case PS_AUTHFAIL:
@@ -965,7 +944,7 @@ gint inc_drop_message(const gchar *file, Pop3Session *session)
        dropfolder = folder_get_default_processing();
 
        /* add msg file to drop folder */
-       if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
+       if ((msgnum = folder_item_add_msg(dropfolder, file, NULL, TRUE)) < 0) {
                unlink(file);
                return -1;
        }
@@ -975,51 +954,84 @@ gint inc_drop_message(const gchar *file, Pop3Session *session)
 
 static void inc_put_error(IncState istate, const gchar *msg)
 {
+       gchar *log_msg = NULL;
+       gchar *err_msg = NULL;
+       gboolean fatal_error = FALSE;
+
        switch (istate) {
        case INC_CONNECT_ERROR:
+               log_msg = _("Connection failed.");
                if (prefs_common.no_recv_err_panel)
                        break;
-               alertpanel_error(_("Connection failed."));
+               err_msg = g_strdup(log_msg);
                break;
        case INC_ERROR:
+               log_msg = _("Error occurred while processing mail.");
                if (prefs_common.no_recv_err_panel)
                        break;
                if (msg)
-                       alertpanel_error(_("Error occurred while processing mail:\n%s"), msg);
+                       err_msg = g_strdup_printf
+                               (_("Error occurred while processing mail:\n%s"),
+                                msg);
                else
-                       alertpanel_error(_("Error occurred while processing mail."));
+                       err_msg = g_strdup(log_msg);
                break;
        case INC_NO_SPACE:
-               alertpanel_error(_("No disk space left."));
+               log_msg = _("No disk space left.");
+               err_msg = g_strdup(log_msg);
+               fatal_error = TRUE;
                break;
        case INC_IO_ERROR:
-               alertpanel_error(_("Can't write file."));
+               log_msg = _("Can't write file.");
+               err_msg = g_strdup(log_msg);
+               fatal_error = TRUE;
                break;
        case INC_SOCKET_ERROR:
+               log_msg = _("Socket error.");
+               if (prefs_common.no_recv_err_panel)
+                       break;
+               err_msg = g_strdup(log_msg);
+               break;
+       case INC_EOF:
+               log_msg = _("Connection closed by the remote host.");
                if (prefs_common.no_recv_err_panel)
                        break;
-               alertpanel_error(_("Socket error."));
+               err_msg = g_strdup(log_msg);
                break;
        case INC_LOCKED:
+               log_msg = _("Mailbox is locked.");
                if (prefs_common.no_recv_err_panel)
                        break;
                if (msg)
-                       alertpanel_error(_("Mailbox is locked:\n%s"), msg);
+                       err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
+                                                 msg);
                else
-                       alertpanel_error(_("Mailbox is locked."));
+                       err_msg = g_strdup(log_msg);
                break;
        case INC_AUTH_FAILED:
+               log_msg = _("Authentication failed.");
                if (prefs_common.no_recv_err_panel)
                        break;
                if (msg)
-                       alertpanel_error(_("Authentication failed:\n%s"),
-                                        msg);
+                       err_msg = g_strdup_printf
+                               (_("Authentication failed:\n%s"), msg);
                else
-                       alertpanel_error(_("Authentication failed."));
+                       err_msg = g_strdup(log_msg);
                break;
        default:
                break;
        }
+
+       if (log_msg) {
+               if (fatal_error)
+                       log_error("%s\n", log_msg);
+               else
+                       log_warning("%s\n", log_msg);
+       }
+       if (err_msg) {
+               alertpanel_error_log(err_msg);
+               g_free(err_msg);
+       }
 }
 
 static void inc_cancel(IncProgressDialog *dialog)
@@ -1069,24 +1081,12 @@ static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
        return TRUE;
 }
 
-static gint inc_spool(void)
+static gint inc_spool_account(PrefsAccount *account)
 {
+       FolderItem *inbox;
        gchar *mbox, *logname;
-       gint msgs;
 
        logname = g_get_user_name();
-       mbox = g_strconcat(prefs_common.spool_path
-                          ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
-                          G_DIR_SEPARATOR_S, logname, NULL);
-       msgs = get_spool(folder_get_default_inbox(), mbox);
-       g_free(mbox);
-
-       return msgs;
-}
-
-static gint inc_spool_account(PrefsAccount *account)
-{
-       FolderItem *inbox;
 
        if (account->inbox) {
                inbox = folder_find_item_from_path(account->inbox);
@@ -1095,7 +1095,13 @@ static gint inc_spool_account(PrefsAccount *account)
        } else
                inbox = folder_get_default_inbox();
 
-       return get_spool(inbox, account->local_mbox);
+       if (is_file_exist(account->local_mbox))
+               mbox = g_strconcat(account->local_mbox, NULL);
+       else 
+               mbox = g_strconcat(account->local_mbox,
+                                  G_DIR_SEPARATOR_S, logname, NULL);
+
+       return get_spool(inbox, mbox);
 }
 
 static gint inc_all_spool(void)
@@ -1236,7 +1242,7 @@ static gint inc_autocheck_func(gpointer data)
                return FALSE;
        }
 
-       inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);
+       inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
 
        return FALSE;
 }