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