sync with main 0.9.11cvs2
[claws.git] / src / inc.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gtk/gtkmain.h>
28 #include <gtk/gtkwindow.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtkprogressbar.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <time.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
39 #include <signal.h>
40 #include <errno.h>
41
42 #include "intl.h"
43 #include "main.h"
44 #include "inc.h"
45 #include "mainwindow.h"
46 #include "folderview.h"
47 #include "summaryview.h"
48 #include "prefs_common.h"
49 #include "prefs_account.h"
50 #include "account.h"
51 #include "procmsg.h"
52 #include "socket.h"
53 #include "ssl.h"
54 #include "pop.h"
55 #include "recv.h"
56 #include "mbox.h"
57 #include "utils.h"
58 #include "gtkutils.h"
59 #include "statusbar.h"
60 #include "manage_window.h"
61 #include "stock_pixmap.h"
62 #include "progressdialog.h"
63 #include "inputdialog.h"
64 #include "alertpanel.h"
65 #include "folder.h"
66 #include "filtering.h"
67 #include "log.h"
68 #include "hooks.h"
69
70 static GList *inc_dialog_list = NULL;
71
72 static guint inc_lock_count = 0;
73
74 static GdkPixmap *currentxpm;
75 static GdkBitmap *currentxpmmask;
76 static GdkPixmap *errorxpm;
77 static GdkBitmap *errorxpmmask;
78 static GdkPixmap *okxpm;
79 static GdkBitmap *okxpmmask;
80
81 #define MSGBUFSIZE      8192
82
83 static void inc_finished                (MainWindow             *mainwin,
84                                          gboolean                new_messages);
85 static gint inc_account_mail_real       (MainWindow             *mainwin,
86                                          PrefsAccount           *account);
87
88 static IncProgressDialog *inc_progress_dialog_create
89                                         (gboolean                autocheck);
90 static void inc_progress_dialog_set_list(IncProgressDialog      *inc_dialog);
91 static void inc_progress_dialog_destroy (IncProgressDialog      *inc_dialog);
92
93 static IncSession *inc_session_new      (PrefsAccount           *account);
94 static void inc_session_destroy         (IncSession             *session);
95 static gint inc_start                   (IncProgressDialog      *inc_dialog);
96 static IncState inc_pop3_session_do     (IncSession             *session);
97
98 static void inc_progress_dialog_update  (IncProgressDialog      *inc_dialog,
99                                          IncSession             *inc_session);
100
101 static void inc_progress_dialog_set_label
102                                         (IncProgressDialog      *inc_dialog,
103                                          IncSession             *inc_session);
104 static void inc_progress_dialog_set_progress
105                                         (IncProgressDialog      *inc_dialog,
106                                          IncSession             *inc_session);
107
108 static void inc_progress_dialog_update_periodic
109                                         (IncProgressDialog      *inc_dialog,
110                                          IncSession             *inc_session);
111
112 static gint inc_recv_data_progressive   (Session        *session,
113                                          guint           cur_len,
114                                          guint           total_len,
115                                          gpointer        data);
116 static gint inc_recv_data_finished      (Session        *session,
117                                          guint           len,
118                                          gpointer        data);
119 static gint inc_recv_message            (Session        *session,
120                                          const gchar    *msg,
121                                          gpointer        data);
122 static gint inc_drop_message            (Pop3Session    *session,
123                                          const gchar    *file);
124
125 static void inc_put_error               (IncState        istate,
126                                          const gchar    *msg);
127
128 static void inc_cancel_cb               (GtkWidget      *widget,
129                                          gpointer        data);
130 static gint inc_dialog_delete_cb        (GtkWidget      *widget,
131                                          GdkEventAny    *event,
132                                          gpointer        data);
133
134 static gint get_spool                   (FolderItem     *dest,
135                                          const gchar    *mbox);
136
137 static gint inc_spool_account(PrefsAccount *account);
138 static gint inc_all_spool(void);
139 static void inc_autocheck_timer_set_interval    (guint           interval);
140 static gint inc_autocheck_func                  (gpointer        data);
141
142 static void inc_notify_cmd              (gint new_msgs, 
143                                          gboolean notify);
144         
145 /**
146  * inc_finished:
147  * @mainwin: Main window.
148  * @new_messages: TRUE if some messages have been received.
149  * 
150  * Update the folder view and the summary view after receiving
151  * messages.  If @new_messages is FALSE, this function avoids unneeded
152  * updating.
153  **/
154 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
155 {
156         FolderItem *item;
157
158         if (prefs_common.scan_all_after_inc)
159                 folderview_check_new(NULL);
160
161         if (!new_messages && !prefs_common.scan_all_after_inc) return;
162
163         if (prefs_common.open_inbox_on_inc) {
164                 item = cur_account && cur_account->inbox
165                         ? folder_find_item_from_identifier(cur_account->inbox)
166                         : folder_get_default_inbox();
167                 folderview_unselect(mainwin->folderview);
168                 folderview_select(mainwin->folderview, item);
169         }
170 }
171
172 void inc_mail(MainWindow *mainwin, gboolean notify)
173 {
174         gint new_msgs = 0;
175         gint account_new_msgs = 0;
176
177         if (inc_lock_count) return;
178
179         if (prefs_common.work_offline)
180                 if (alertpanel(_("Offline warning"), 
181                                _("You're working offline. Override?"),
182                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
183                 return;
184
185         inc_lock();
186         inc_autocheck_timer_remove();
187         main_window_lock(mainwin);
188
189         if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
190                 /* external incorporating program */
191                 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
192                         main_window_unlock(mainwin);
193                         inc_autocheck_timer_set();
194                         return;
195                 }
196         } else {
197                 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
198                 if (account_new_msgs > 0)
199                         new_msgs += account_new_msgs;
200         }
201
202         inc_finished(mainwin, new_msgs > 0);
203         main_window_unlock(mainwin);
204         inc_notify_cmd(new_msgs, notify);
205         inc_autocheck_timer_set();
206         inc_unlock();
207 }
208
209 void inc_pop_before_smtp(PrefsAccount *acc)
210 {
211         /* FIXME: assumes to attach to first main window */
212         inc_account_mail(mainwindow_get_mainwindow(), acc);
213 }
214
215 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
216 {
217         IncProgressDialog *inc_dialog;
218         IncSession *session;
219         FolderItem *item = NULL;
220         
221         if(mainwin && mainwin->summaryview)
222                 item = mainwin->summaryview->folder_item;
223
224         switch (account->protocol) {
225         case A_IMAP4:
226         case A_NNTP:
227                 /* Melvin: bug [14]
228                  * FIXME: it should return foldeview_check_new() value.
229                  * TODO: do it when bug [19] is fixed (IMAP folder sets 
230                  * an incorrect new message count)
231                  */
232                 folderview_check_new(FOLDER(account->folder));
233                 return 0;
234         case A_POP3:
235         case A_APOP:
236                 session = inc_session_new(account);
237                 if (!session) return 0;
238                 
239                 inc_dialog = inc_progress_dialog_create(FALSE);
240                 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
241                                                        session);
242                 inc_dialog->mainwin = mainwin;
243                 inc_progress_dialog_set_list(inc_dialog);
244
245                 if (mainwin) {
246                         toolbar_main_set_sensitive(mainwin);
247                         main_window_set_menu_sensitive(mainwin);
248                 }
249                         
250                 return inc_start(inc_dialog);
251
252         case A_LOCAL:
253                 return inc_spool_account(account);
254
255         default:
256                 break;
257         }
258         return 0;
259 }
260
261 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
262 {
263         gint new_msgs;
264
265         if (inc_lock_count) return 0;
266
267         if (prefs_common.work_offline)
268                 if (alertpanel(_("Offline warning"), 
269                                _("You're working offline. Override?"),
270                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
271                         return 0;
272
273         inc_autocheck_timer_remove();
274         main_window_lock(mainwin);
275
276         new_msgs = inc_account_mail_real(mainwin, account);
277
278         inc_finished(mainwin, new_msgs > 0);
279         main_window_unlock(mainwin);
280         inc_autocheck_timer_set();
281
282         return new_msgs;
283 }
284
285 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
286                           gboolean notify)
287 {
288         GList *list, *queue_list = NULL;
289         IncProgressDialog *inc_dialog;
290         gint new_msgs = 0;
291         gint account_new_msgs = 0;
292         
293         if (prefs_common.work_offline)
294                 if (alertpanel(_("Offline warning"), 
295                                _("You're working offline. Override?"),
296                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
297                 return;
298
299         if (inc_lock_count) return;
300
301         inc_autocheck_timer_remove();
302         main_window_lock(mainwin);
303
304         list = account_get_list();
305         if (!list) {
306                 inc_finished(mainwin, new_msgs > 0);
307                 main_window_unlock(mainwin);
308                 inc_notify_cmd(new_msgs, notify);
309                 inc_autocheck_timer_set();
310                 return;
311         }
312
313         /* check local folders */
314         account_new_msgs = inc_all_spool();
315         if (account_new_msgs > 0)
316                 new_msgs += account_new_msgs;
317
318         /* check IMAP4 / News folders */
319         for (list = account_get_list(); list != NULL; list = list->next) {
320                 PrefsAccount *account = list->data;
321                 if ((account->protocol == A_IMAP4 ||
322                      account->protocol == A_NNTP) && account->recv_at_getall) {
323                         new_msgs += folderview_check_new(FOLDER(account->folder));
324                 }
325         }
326
327         /* check POP3 accounts */
328         for (list = account_get_list(); list != NULL; list = list->next) {
329                 IncSession *session;
330                 PrefsAccount *account = list->data;
331
332                 if (account->recv_at_getall) {
333                         session = inc_session_new(account);
334                         if (session)
335                                 queue_list = g_list_append(queue_list, session);
336                 }
337         }
338
339         if (queue_list) {
340                 inc_dialog = inc_progress_dialog_create(autocheck);
341                 inc_dialog->queue_list = queue_list;
342                 inc_dialog->mainwin = mainwin;
343                 inc_progress_dialog_set_list(inc_dialog);
344
345                 toolbar_main_set_sensitive(mainwin);
346                 main_window_set_menu_sensitive(mainwin);
347                 new_msgs += inc_start(inc_dialog);
348         }
349
350         inc_finished(mainwin, new_msgs > 0);
351         main_window_unlock(mainwin);
352         inc_notify_cmd(new_msgs, notify);
353         inc_autocheck_timer_set();
354 }
355
356 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
357 {
358         IncProgressDialog *dialog;
359         ProgressDialog *progress;
360
361         dialog = g_new0(IncProgressDialog, 1);
362
363         progress = progress_dialog_create();
364         gtk_window_set_title(GTK_WINDOW(progress->window),
365                              _("Retrieving new messages"));
366         gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
367                            GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
368         gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
369                            GTK_SIGNAL_FUNC(inc_dialog_delete_cb), dialog);
370         /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
371
372         progress_dialog_set_value(progress, 0.0);
373
374         stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
375                          &okxpm, &okxpmmask);
376         stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
377                          &currentxpm, &currentxpmmask);
378         stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
379                          &errorxpm, &errorxpmmask);
380
381         if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
382             (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
383              !autocheck)) {
384                 dialog->show_dialog = TRUE;
385                 gtk_widget_show_now(progress->window);
386         }
387
388         dialog->dialog = progress;
389         gettimeofday(&dialog->progress_tv, NULL);
390         gettimeofday(&dialog->folder_tv, NULL);
391         dialog->queue_list = NULL;
392         dialog->cur_row = 0;
393
394         inc_dialog_list = g_list_append(inc_dialog_list, dialog);
395
396         return dialog;
397 }
398
399 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
400 {
401         GList *list;
402
403         for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
404                 IncSession *session = list->data;
405                 Pop3Session *pop3_session = POP3_SESSION(session->session);
406                 gchar *text[3];
407
408                 session->data = inc_dialog;
409
410                 text[0] = NULL;
411                 text[1] = pop3_session->ac_prefs->account_name;
412                 text[2] = _("Standby");
413                 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
414         }
415 }
416
417 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
418 {
419         progress_dialog_set_value(inc_dialog->dialog, 0.0);
420         progress_dialog_set_label(inc_dialog->dialog, "");
421         if (inc_dialog->mainwin)
422                 main_window_progress_off(inc_dialog->mainwin);
423 }
424
425 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
426 {
427         g_return_if_fail(inc_dialog != NULL);
428
429         inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
430
431         if (inc_dialog->mainwin)
432                 main_window_progress_off(inc_dialog->mainwin);
433         progress_dialog_destroy(inc_dialog->dialog);
434
435         g_free(inc_dialog);
436 }
437
438 static IncSession *inc_session_new(PrefsAccount *account)
439 {
440         IncSession *session;
441
442         g_return_val_if_fail(account != NULL, NULL);
443
444         if (account->protocol != A_POP3 && account->protocol != A_APOP)
445                 return NULL;
446         if (!account->recv_server || !account->userid)
447                 return NULL;
448
449         session = g_new0(IncSession, 1);
450
451         session->session = pop3_session_new(account);
452         session->session->data = session;
453         POP3_SESSION(session->session)->drop_message = inc_drop_message;
454         session_set_recv_message_notify(session->session,
455                                         inc_recv_message, session);
456         session_set_recv_data_progressive_notify(session->session,
457                                                  inc_recv_data_progressive,
458                                                  session);
459         session_set_recv_data_notify(session->session,
460                                      inc_recv_data_finished, session);
461
462         return session;
463 }
464
465 static void inc_session_destroy(IncSession *session)
466 {
467         g_return_if_fail(session != NULL);
468
469         session_destroy(session->session);
470         g_free(session);
471 }
472
473 static gint inc_start(IncProgressDialog *inc_dialog)
474 {
475         IncSession *session;
476         GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
477         GList *qlist;
478         Pop3Session *pop3_session;
479         IncState inc_state;
480         gint error_num = 0;
481         gint new_msgs = 0;
482         gchar *msg;
483         gchar *fin_msg;
484         FolderItem *processing, *inbox;
485         MsgInfo *msginfo;
486         GSList *msglist, *msglist_element;
487
488         qlist = inc_dialog->queue_list;
489         while (qlist != NULL) {
490                 GList *next = qlist->next;
491
492                 session = qlist->data;
493                 pop3_session = POP3_SESSION(session->session); 
494                 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
495                 if (pop3_session->ac_prefs->passwd)
496                         pop3_session->pass =
497                                 g_strdup(pop3_session->ac_prefs->passwd);
498                 else if (pop3_session->ac_prefs->tmp_pass)
499                         pop3_session->pass =
500                                 g_strdup(pop3_session->ac_prefs->tmp_pass);
501                 else {
502                         gchar *pass;
503
504                         if (inc_dialog->show_dialog)
505                                 manage_window_focus_in
506                                         (inc_dialog->dialog->window,
507                                          NULL, NULL);
508
509                         pass = input_dialog_query_password
510                                 (pop3_session->ac_prefs->recv_server,
511                                  pop3_session->user);
512
513                         if (inc_dialog->show_dialog)
514                                 manage_window_focus_out
515                                         (inc_dialog->dialog->window,
516                                          NULL, NULL);
517
518                         if (pass) {
519                                 pop3_session->ac_prefs->tmp_pass =
520                                         g_strdup(pass);
521                                 pop3_session->pass = pass;
522                         }
523                 }
524
525                 qlist = next;
526         }
527
528 #define SET_PIXMAP_AND_TEXT(xpm, xpmmask, str)                             \
529 {                                                                          \
530         gtk_clist_set_pixmap(clist, inc_dialog->cur_row, 0, xpm, xpmmask); \
531         gtk_clist_set_text(clist, inc_dialog->cur_row, 2, str);            \
532 }
533
534         for (; inc_dialog->queue_list != NULL; inc_dialog->cur_row++) {
535                 session = inc_dialog->queue_list->data;
536                 pop3_session = POP3_SESSION(session->session);
537
538                 if (pop3_session->pass == NULL) {
539                         SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
540                         inc_session_destroy(session);
541                         inc_dialog->queue_list =
542                                 g_list_remove(inc_dialog->queue_list, session);
543                         continue;
544                 }
545
546                 inc_progress_dialog_clear(inc_dialog);
547                 gtk_clist_moveto(clist, inc_dialog->cur_row, -1, 1.0, 0.0);
548
549                 SET_PIXMAP_AND_TEXT(currentxpm, currentxpmmask,
550                                     _("Retrieving"));
551
552                 /* begin POP3 session */
553                 inc_state = inc_pop3_session_do(session);
554
555                 switch (inc_state) {
556                 case INC_SUCCESS:
557                         if (pop3_session->cur_total_num > 0)
558                                 msg = g_strdup_printf
559                                         (_("Done (%d message(s) (%s) received)"),
560                                          pop3_session->cur_total_num,
561                                          to_human_readable(pop3_session->cur_total_recv_bytes));
562                         else
563                                 msg = g_strdup_printf(_("Done (no new messages)"));
564                         SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, msg);
565                         g_free(msg);
566                         break;
567                 case INC_CONNECT_ERROR:
568                         SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
569                                             _("Connection failed"));
570                         break;
571                 case INC_AUTH_FAILED:
572                         SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
573                                             _("Auth failed"));
574                         break;
575                 case INC_LOCKED:
576                         SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
577                                             _("Locked"));
578                         break;
579                 case INC_ERROR:
580                 case INC_NO_SPACE:
581                 case INC_IO_ERROR:
582                 case INC_SOCKET_ERROR:
583                 case INC_EOF:
584                         SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask, _("Error"));
585                         break;
586                 case INC_TIMEOUT:
587                         SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask, _("Timeout"));
588                         break;
589                 case INC_CANCEL:
590                         SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
591                         break;
592                 default:
593                         break;
594                 }
595                 
596                 if (pop3_session->error_val == PS_AUTHFAIL) {
597                         if(!prefs_common.no_recv_err_panel) {
598                                 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
599                                     ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
600                                         manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
601                         }
602                 }
603
604                 /* CLAWS: perform filtering actions on dropped message */
605                 /* CLAWS: get default inbox (perhaps per account) */
606                 if (pop3_session->ac_prefs->inbox) {
607                         /* CLAWS: get destination folder / mailbox */
608                         inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
609                         if (!inbox)
610                                 inbox = folder_get_default_inbox();
611                 } else
612                         inbox = folder_get_default_inbox();
613
614                 /* get list of messages in processing */
615                 processing = folder_get_default_processing();
616                 folder_item_scan(processing);
617                 msglist = folder_item_get_msg_list(processing);
618
619                 /* process messages */
620                 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
621                         msginfo = (MsgInfo *) msglist_element->data;
622                         if (!pop3_session->ac_prefs->filter_on_recv || !procmsg_msginfo_filter(msginfo))
623                                 folder_item_move_msg(inbox, msginfo);
624                         procmsg_msginfo_free(msginfo);
625                 }
626                 g_slist_free(msglist);
627
628                 new_msgs += pop3_session->cur_total_num;
629
630                 if (pop3_session->error_val == PS_AUTHFAIL &&
631                     pop3_session->ac_prefs->tmp_pass) {
632                         g_free(pop3_session->ac_prefs->tmp_pass);
633                         pop3_session->ac_prefs->tmp_pass = NULL;
634                 }
635
636                 pop3_write_uidl_list(pop3_session);
637
638                 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
639                         error_num++;
640                         if (inc_dialog->show_dialog)
641                                 manage_window_focus_in
642                                         (inc_dialog->dialog->window,
643                                          NULL, NULL);
644                         inc_put_error(inc_state, pop3_session->error_msg);
645                         if (inc_dialog->show_dialog)
646                                 manage_window_focus_out
647                                         (inc_dialog->dialog->window,
648                                          NULL, NULL);
649                         if (inc_state == INC_NO_SPACE ||
650                             inc_state == INC_IO_ERROR)
651                                 break;
652                 }
653
654                 inc_session_destroy(session);
655                 inc_dialog->queue_list =
656                         g_list_remove(inc_dialog->queue_list, session);
657         }
658
659 #undef SET_PIXMAP_AND_TEXT
660
661         if (new_msgs > 0)
662                 fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
663                                           new_msgs);
664         else
665                 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
666
667         progress_dialog_set_label(inc_dialog->dialog, fin_msg);
668
669 #if 0
670         if (error_num && !prefs_common.no_recv_err_panel) {
671                 if (inc_dialog->show_dialog)
672                         manage_window_focus_in(inc_dialog->dialog->window,
673                                                NULL, NULL);
674                 alertpanel_error_log(_("Some errors occurred while getting mail."));
675                 if (inc_dialog->show_dialog)
676                         manage_window_focus_out(inc_dialog->dialog->window,
677                                                 NULL, NULL);
678         }
679 #endif
680
681         while (inc_dialog->queue_list != NULL) {
682                 session = inc_dialog->queue_list->data;
683                 inc_session_destroy(session);
684                 inc_dialog->queue_list =
685                         g_list_remove(inc_dialog->queue_list, session);
686         }
687
688         if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
689                 inc_progress_dialog_destroy(inc_dialog);
690         else {
691                 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
692                                      fin_msg);
693                 gtk_label_set_text(GTK_LABEL(GTK_BIN(inc_dialog->dialog->cancel_btn)->child),
694                                    _("Close"));
695         }
696
697         g_free(fin_msg);
698
699         return new_msgs;
700 }
701
702 static IncState inc_pop3_session_do(IncSession *session)
703 {
704         Pop3Session *pop3_session = POP3_SESSION(session->session);
705         IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
706         gchar *server;
707         gushort port;
708         gchar *buf;
709
710         debug_print("getting new messages of account %s...\n",
711                     pop3_session->ac_prefs->account_name);
712                     
713         pop3_session->ac_prefs->last_pop_login_time = time(NULL);
714
715         buf = g_strdup_printf(_("%s: Retrieving new messages"),
716                               pop3_session->ac_prefs->recv_server);
717         gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
718         g_free(buf);
719
720         server = pop3_session->ac_prefs->recv_server;
721 #if USE_OPENSSL
722         port = pop3_session->ac_prefs->set_popport ?
723                 pop3_session->ac_prefs->popport :
724                 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
725         SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
726         if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
727                 SESSION(pop3_session)->nonblocking =
728                         pop3_session->ac_prefs->use_nonblocking_ssl;
729 #else
730         port = pop3_session->ac_prefs->set_popport ?
731                 pop3_session->ac_prefs->popport : 110;
732 #endif
733
734         buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
735         log_message("%s\n", buf);
736
737         progress_dialog_set_label(inc_dialog->dialog, buf);
738         g_free(buf);
739
740         session_set_timeout(SESSION(pop3_session),
741                             prefs_common.io_timeout_secs * 1000);
742
743         if (session_connect(SESSION(pop3_session), server, port) < 0) {
744                 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
745                             server, port);
746                 if(!prefs_common.no_recv_err_panel) {
747                         if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
748                             ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
749                                 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
750                         }
751                         alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
752                                          server, port);
753                         manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
754                 }
755                 session->inc_state = INC_CONNECT_ERROR;
756                 statusbar_pop_all();
757                 return INC_CONNECT_ERROR;
758         }
759
760         while (session_is_connected(SESSION(pop3_session)) &&
761                session->inc_state != INC_CANCEL)
762                 gtk_main_iteration();
763
764         if (session->inc_state == INC_SUCCESS) {
765                 switch (pop3_session->error_val) {
766                 case PS_SUCCESS:
767                         switch (SESSION(pop3_session)->state) {
768                         case SESSION_ERROR:
769                                 if (pop3_session->state == POP3_READY)
770                                         session->inc_state = INC_CONNECT_ERROR;
771                                 else
772                                         session->inc_state = INC_ERROR;
773                                 break;
774                         case SESSION_EOF:
775                                 session->inc_state = INC_EOF;
776                                 break;
777                         case SESSION_TIMEOUT:
778                                 session->inc_state = INC_TIMEOUT;
779                                 break;
780                         default:
781                                 session->inc_state = INC_SUCCESS;
782                                 break;
783                         }
784                         break;
785                 case PS_AUTHFAIL:
786                         session->inc_state = INC_AUTH_FAILED;
787                         break;
788                 case PS_IOERR:
789                         session->inc_state = INC_IO_ERROR;
790                         break;
791                 case PS_SOCKET:
792                         session->inc_state = INC_SOCKET_ERROR;
793                         break;
794                 case PS_LOCKBUSY:
795                         session->inc_state = INC_LOCKED;
796                         break;
797                 default:
798                         session->inc_state = INC_ERROR;
799                         break;
800                 }
801         }
802
803         session_disconnect(SESSION(pop3_session));
804         statusbar_pop_all();
805
806         return session->inc_state;
807 }
808
809 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
810                                        IncSession *inc_session)
811 {
812         inc_progress_dialog_set_label(inc_dialog, inc_session);
813         inc_progress_dialog_set_progress(inc_dialog, inc_session);
814 }
815
816 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
817                                           IncSession *inc_session)
818 {
819         ProgressDialog *dialog = inc_dialog->dialog;
820         Pop3Session *session;
821
822         g_return_if_fail(inc_session != NULL);
823
824         session = POP3_SESSION(inc_session->session);
825
826         switch (session->state) {
827         case POP3_GREETING:
828                 break;
829         case POP3_GETAUTH_USER:
830         case POP3_GETAUTH_PASS:
831         case POP3_GETAUTH_APOP:
832                 progress_dialog_set_label(dialog, _("Authenticating..."));
833                 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
834                                     SESSION(session)->server,
835                                     session->ac_prefs->account_name);
836                 break;
837         case POP3_GETRANGE_STAT:
838                 progress_dialog_set_label
839                         (dialog, _("Getting the number of new messages (STAT)..."));
840                 break;
841         case POP3_GETRANGE_LAST:
842                 progress_dialog_set_label
843                         (dialog, _("Getting the number of new messages (LAST)..."));
844                 break;
845         case POP3_GETRANGE_UIDL:
846                 progress_dialog_set_label
847                         (dialog, _("Getting the number of new messages (UIDL)..."));
848                 break;
849         case POP3_GETSIZE_LIST:
850                 progress_dialog_set_label
851                         (dialog, _("Getting the size of messages (LIST)..."));
852                 break;
853         case POP3_RETR:
854         case POP3_RETR_RECV:
855                 break;
856         case POP3_DELETE:
857 #if 0
858                 if (session->msg[session->cur_msg].recv_time <
859                         session->current_time) {
860                         gchar buf[MSGBUFSIZE];
861                         g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
862                                    session->cur_msg);
863                         progress_dialog_set_label(dialog, buf);
864                 }
865 #endif
866                 break;
867         case POP3_LOGOUT:
868                 progress_dialog_set_label(dialog, _("Quitting"));
869                 break;
870         default:
871                 break;
872         }
873 }
874
875 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
876                                              IncSession *inc_session)
877 {
878         gchar buf[MSGBUFSIZE];
879         Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
880         gchar *total_size_str;
881         gint cur_total;
882         gint total;
883
884         if (!pop3_session->new_msg_exist) return;
885
886         cur_total = inc_session->cur_total_bytes;
887         total = pop3_session->total_bytes;
888         if (pop3_session->state == POP3_RETR ||
889             pop3_session->state == POP3_RETR_RECV ||
890             pop3_session->state == POP3_DELETE) {
891                 Xstrdup_a(total_size_str, to_human_readable(total), return);
892                 g_snprintf(buf, sizeof(buf),
893                            _("Retrieving message (%d / %d) (%s / %s)"),
894                            pop3_session->cur_msg, pop3_session->count,
895                            to_human_readable(cur_total), total_size_str);
896                 progress_dialog_set_label(inc_dialog->dialog, buf);
897         }
898
899         progress_dialog_set_percentage
900                 (inc_dialog->dialog,(gfloat)cur_total / (gfloat)total);
901
902         gtk_progress_set_show_text
903                 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), TRUE);
904         g_snprintf(buf, sizeof(buf), "%d / %d",
905                    pop3_session->cur_msg, pop3_session->count);
906         gtk_progress_set_format_string
907                 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), buf);
908         gtk_progress_bar_update
909                 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
910                  (gfloat)cur_total / (gfloat)total);
911
912         if (pop3_session->cur_total_num > 0) {
913                 g_snprintf(buf, sizeof(buf),
914                            _("Retrieving (%d message(s) (%s) received)"),
915                            pop3_session->cur_total_num,
916                            to_human_readable
917                            (pop3_session->cur_total_recv_bytes));
918                 gtk_clist_set_text(GTK_CLIST(inc_dialog->dialog->clist),
919                                    inc_dialog->cur_row, 2, buf);
920         }
921 }
922
923 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
924                                                 IncSession *inc_session)
925 {
926         struct timeval tv_cur;
927         struct timeval tv_result;
928         gint msec;
929
930         gettimeofday(&tv_cur, NULL);
931
932         tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
933         tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
934         if (tv_result.tv_usec < 0) {
935                 tv_result.tv_sec--;
936                 tv_result.tv_usec += 1000000;
937         }
938
939         msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
940         if (msec > PROGRESS_UPDATE_INTERVAL) {
941                 inc_progress_dialog_update(inc_dialog, inc_session);
942                 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
943                 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
944         }
945 }
946
947 static gint inc_recv_data_progressive(Session *session, guint cur_len,
948                                       guint total_len, gpointer data)
949 {
950         IncSession *inc_session = (IncSession *)data;
951         Pop3Session *pop3_session = POP3_SESSION(session);
952         IncProgressDialog *inc_dialog;
953         gint cur_total;
954
955         g_return_val_if_fail(inc_session != NULL, -1);
956
957         if (pop3_session->state != POP3_RETR &&
958             pop3_session->state != POP3_RETR_RECV &&
959             pop3_session->state != POP3_DELETE &&
960             pop3_session->state != POP3_LOGOUT) return 0;
961
962         if (!pop3_session->new_msg_exist) return 0;
963
964         cur_total = pop3_session->cur_total_bytes + cur_len;
965         if (cur_total > pop3_session->total_bytes)
966                 cur_total = pop3_session->total_bytes;
967         inc_session->cur_total_bytes = cur_total;
968
969         inc_dialog = (IncProgressDialog *)inc_session->data;
970         inc_progress_dialog_update_periodic(inc_dialog, inc_session);
971
972         return 0;
973 }
974
975 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
976 {
977         IncSession *inc_session = (IncSession *)data;
978         IncProgressDialog *inc_dialog;
979
980         g_return_val_if_fail(inc_session != NULL, -1);
981
982         inc_dialog = (IncProgressDialog *)inc_session->data;
983
984         inc_recv_data_progressive(session, 0, 0, inc_session);
985
986         if (POP3_SESSION(session)->state == POP3_LOGOUT) {
987                 inc_progress_dialog_update(inc_dialog, inc_session);
988         }
989
990         return 0;
991 }
992
993 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
994 {
995         IncSession *inc_session = (IncSession *)data;
996         IncProgressDialog *inc_dialog;
997
998         g_return_val_if_fail(inc_session != NULL, -1);
999
1000         inc_dialog = (IncProgressDialog *)inc_session->data;
1001
1002         switch (POP3_SESSION(session)->state) {
1003         case POP3_GETAUTH_USER:
1004         case POP3_GETAUTH_PASS:
1005         case POP3_GETAUTH_APOP:
1006         case POP3_GETRANGE_STAT:
1007         case POP3_GETRANGE_LAST:
1008         case POP3_GETRANGE_UIDL:
1009         case POP3_GETSIZE_LIST:
1010                 inc_progress_dialog_update(inc_dialog, inc_session);
1011                 break;
1012         case POP3_RETR:
1013                 inc_recv_data_progressive(session, 0, 0, inc_session);
1014                 break;
1015         case POP3_LOGOUT:
1016                 inc_progress_dialog_update(inc_dialog, inc_session);
1017                 break;
1018         default:
1019                 break;
1020         }
1021
1022         return 0;
1023 }
1024
1025 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1026 {
1027         FolderItem *inbox;
1028         FolderItem *dropfolder;
1029         IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1030         gint msgnum;
1031
1032         g_return_val_if_fail(inc_session != NULL, -1);
1033
1034         if (session->ac_prefs->inbox) {
1035                 inbox = folder_find_item_from_identifier
1036                         (session->ac_prefs->inbox);
1037                 if (!inbox)
1038                         inbox = folder_get_default_inbox();
1039         } else
1040                 inbox = folder_get_default_inbox();
1041         if (!inbox) {
1042                 unlink(file);
1043                 return -1;
1044         }
1045
1046         /* CLAWS: claws uses a global .processing folder for the filtering. */
1047         dropfolder = folder_get_default_processing();
1048
1049         /* add msg file to drop folder */
1050         if ((msgnum = folder_item_add_msg(dropfolder, file, NULL, TRUE)) < 0) {
1051                 unlink(file);
1052                 return -1;
1053         }
1054
1055         return 0;
1056 }
1057
1058 static void inc_put_error(IncState istate, const gchar *msg)
1059 {
1060         gchar *log_msg = NULL;
1061         gchar *err_msg = NULL;
1062         gboolean fatal_error = FALSE;
1063
1064         switch (istate) {
1065         case INC_CONNECT_ERROR:
1066                 log_msg = _("Connection failed.");
1067                 if (prefs_common.no_recv_err_panel)
1068                         break;
1069                 err_msg = g_strdup(log_msg);
1070                 break;
1071         case INC_ERROR:
1072                 log_msg = _("Error occurred while processing mail.");
1073                 if (prefs_common.no_recv_err_panel)
1074                         break;
1075                 if (msg)
1076                         err_msg = g_strdup_printf
1077                                 (_("Error occurred while processing mail:\n%s"),
1078                                  msg);
1079                 else
1080                         err_msg = g_strdup(log_msg);
1081                 break;
1082         case INC_NO_SPACE:
1083                 log_msg = _("No disk space left.");
1084                 err_msg = g_strdup(log_msg);
1085                 fatal_error = TRUE;
1086                 break;
1087         case INC_IO_ERROR:
1088                 log_msg = _("Can't write file.");
1089                 err_msg = g_strdup(log_msg);
1090                 fatal_error = TRUE;
1091                 break;
1092         case INC_SOCKET_ERROR:
1093                 log_msg = _("Socket error.");
1094                 if (prefs_common.no_recv_err_panel)
1095                         break;
1096                 err_msg = g_strdup(log_msg);
1097                 break;
1098         case INC_EOF:
1099                 log_msg = _("Connection closed by the remote host.");
1100                 if (prefs_common.no_recv_err_panel)
1101                         break;
1102                 err_msg = g_strdup(log_msg);
1103                 break;
1104         case INC_LOCKED:
1105                 log_msg = _("Mailbox is locked.");
1106                 if (prefs_common.no_recv_err_panel)
1107                         break;
1108                 if (msg)
1109                         err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1110                                                   msg);
1111                 else
1112                         err_msg = g_strdup(log_msg);
1113                 break;
1114         case INC_AUTH_FAILED:
1115                 log_msg = _("Authentication failed.");
1116                 if (prefs_common.no_recv_err_panel)
1117                         break;
1118                 if (msg)
1119                         err_msg = g_strdup_printf
1120                                 (_("Authentication failed:\n%s"), msg);
1121                 else
1122                         err_msg = g_strdup(log_msg);
1123                 break;
1124         case INC_TIMEOUT:
1125                 log_msg = _("Session timed out.");
1126                 if (prefs_common.no_recv_err_panel)
1127                         break;
1128                 err_msg = g_strdup(log_msg);
1129                 break;
1130         default:
1131                 break;
1132         }
1133
1134         if (log_msg) {
1135                 if (fatal_error)
1136                         log_error("%s\n", log_msg);
1137                 else
1138                         log_warning("%s\n", log_msg);
1139         }
1140         if (err_msg) {
1141                 alertpanel_error_log(err_msg);
1142                 g_free(err_msg);
1143         }
1144 }
1145
1146 static void inc_cancel(IncProgressDialog *dialog)
1147 {
1148         IncSession *session;
1149
1150         g_return_if_fail(dialog != NULL);
1151
1152         if (dialog->queue_list == NULL) {
1153                 inc_progress_dialog_destroy(dialog);
1154                 return;
1155         }
1156
1157         session = dialog->queue_list->data;
1158
1159         session->inc_state = INC_CANCEL;
1160
1161         log_message(_("Incorporation cancelled\n"));
1162 }
1163
1164 gboolean inc_is_active(void)
1165 {
1166         return (inc_dialog_list != NULL);
1167 }
1168
1169 void inc_cancel_all(void)
1170 {
1171         GList *cur;
1172
1173         for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1174                 inc_cancel((IncProgressDialog *)cur->data);
1175 }
1176
1177 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1178 {
1179         inc_cancel((IncProgressDialog *)data);
1180 }
1181
1182 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1183                                  gpointer data)
1184 {
1185         IncProgressDialog *dialog = (IncProgressDialog *)data;
1186
1187         if (dialog->queue_list == NULL)
1188                 inc_progress_dialog_destroy(dialog);
1189
1190         return TRUE;
1191 }
1192
1193 static gint inc_spool_account(PrefsAccount *account)
1194 {
1195         FolderItem *inbox;
1196         gchar *mbox, *logname;
1197         gint result;
1198
1199         logname = g_get_user_name();
1200
1201         if (account->inbox) {
1202                 inbox = folder_find_item_from_path(account->inbox);
1203                 if (!inbox)
1204                         inbox = folder_get_default_inbox();
1205         } else
1206                 inbox = folder_get_default_inbox();
1207
1208         if (is_file_exist(account->local_mbox))
1209                 mbox = g_strdup(account->local_mbox);
1210         else 
1211                 mbox = g_strconcat(account->local_mbox,
1212                                    G_DIR_SEPARATOR_S, logname, NULL);
1213         
1214         result = get_spool(inbox, mbox);
1215         g_free(mbox);
1216         
1217         statusbar_pop_all();
1218         
1219         return result;
1220 }
1221
1222 static gint inc_all_spool(void)
1223 {
1224         GList *list = NULL;
1225         gint new_msgs = 0;
1226         gint account_new_msgs = 0;
1227
1228         list = account_get_list();
1229         if (!list) return 0;
1230
1231         for (; list != NULL; list = list->next) {
1232                 PrefsAccount *account = list->data;
1233
1234                 if ((account->protocol == A_LOCAL) &&
1235                     (account->recv_at_getall)) {
1236                         account_new_msgs = inc_spool_account(account);
1237                         if (account_new_msgs > 0)
1238                                 new_msgs += account_new_msgs;
1239                 }
1240         }
1241
1242         return new_msgs;
1243 }
1244
1245 static gint get_spool(FolderItem *dest, const gchar *mbox)
1246 {
1247         gint msgs, size;
1248         gint lockfd;
1249         gchar tmp_mbox[MAXPATHLEN + 1];
1250
1251         g_return_val_if_fail(dest != NULL, -1);
1252         g_return_val_if_fail(mbox != NULL, -1);
1253
1254         if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1255                 debug_print("no messages in local mailbox.\n");
1256                 return 0;
1257         } else if (size < 0)
1258                 return -1;
1259
1260         if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1261                 return -1;
1262
1263         g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1264                    get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1265
1266         if (copy_mbox(mbox, tmp_mbox) < 0) {
1267                 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1268                 return -1;
1269         }
1270
1271         debug_print("Getting new messages from %s into %s...\n",
1272                     mbox, dest->path);
1273
1274         msgs = proc_mbox(dest, tmp_mbox, TRUE);
1275
1276         unlink(tmp_mbox);
1277         if (msgs >= 0) empty_mbox(mbox);
1278         unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1279
1280         return msgs;
1281 }
1282
1283 void inc_lock(void)
1284 {
1285         inc_lock_count++;
1286 }
1287
1288 void inc_unlock(void)
1289 {
1290         if (inc_lock_count > 0)
1291                 inc_lock_count--;
1292 }
1293
1294 static guint autocheck_timer = 0;
1295 static gpointer autocheck_data = NULL;
1296
1297 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1298 {
1299
1300         gchar *buf;
1301
1302         if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1303             *prefs_common.newmail_notify_cmd))
1304                      return;
1305         if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1306                 buf[1] == 'd' && !strchr(&buf[1], '%'))
1307                 buf = g_strdup_printf(prefs_common.newmail_notify_cmd, 
1308                                       new_msgs);
1309         else
1310                 buf = g_strdup(prefs_common.newmail_notify_cmd);
1311
1312         execute_command_line(buf, TRUE);
1313
1314         g_free(buf);
1315 }
1316  
1317 void inc_autocheck_timer_init(MainWindow *mainwin)
1318 {
1319         autocheck_data = mainwin;
1320         inc_autocheck_timer_set();
1321 }
1322
1323 static void inc_autocheck_timer_set_interval(guint interval)
1324 {
1325         inc_autocheck_timer_remove();
1326         /* last test is to avoid re-enabling auto_check after modifying 
1327            the common preferences */
1328         if (prefs_common.autochk_newmail && autocheck_data
1329             && prefs_common.work_offline == FALSE) {
1330                 autocheck_timer = gtk_timeout_add
1331                         (interval, inc_autocheck_func, autocheck_data);
1332                 debug_print("added timer = %d\n", autocheck_timer);
1333         }
1334 }
1335
1336 void inc_autocheck_timer_set(void)
1337 {
1338         inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1339 }
1340
1341 void inc_autocheck_timer_remove(void)
1342 {
1343         if (autocheck_timer) {
1344                 debug_print("removed timer = %d\n", autocheck_timer);
1345                 gtk_timeout_remove(autocheck_timer);
1346                 autocheck_timer = 0;
1347         }
1348 }
1349
1350 static gint inc_autocheck_func(gpointer data)
1351 {
1352         MainWindow *mainwin = (MainWindow *)data;
1353
1354         if (inc_lock_count) {
1355                 debug_print("autocheck is locked.\n");
1356                 inc_autocheck_timer_set_interval(1000);
1357                 return FALSE;
1358         }
1359
1360         inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1361
1362         return FALSE;
1363 }