2005-04-12 [paul] 1.9.6cvs33
[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 <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkprogressbar.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <time.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/wait.h>
40 #include <signal.h>
41 #include <errno.h>
42
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 GdkPixbuf *currentpix;
75 static GdkPixbuf *errorpix;
76 static GdkPixbuf *okpix;
77
78 #define MSGBUFSIZE      8192
79
80 static void inc_finished                (MainWindow             *mainwin,
81                                          gboolean                new_messages);
82 static gint inc_account_mail_real       (MainWindow             *mainwin,
83                                          PrefsAccount           *account);
84
85 static IncProgressDialog *inc_progress_dialog_create
86                                         (gboolean                autocheck);
87 static void inc_progress_dialog_set_list(IncProgressDialog      *inc_dialog);
88 static void inc_progress_dialog_destroy (IncProgressDialog      *inc_dialog);
89
90 static IncSession *inc_session_new      (PrefsAccount           *account);
91 static void inc_session_destroy         (IncSession             *session);
92 static gint inc_start                   (IncProgressDialog      *inc_dialog);
93 static IncState inc_pop3_session_do     (IncSession             *session);
94
95 static void inc_progress_dialog_update  (IncProgressDialog      *inc_dialog,
96                                          IncSession             *inc_session);
97
98 static void inc_progress_dialog_set_label
99                                         (IncProgressDialog      *inc_dialog,
100                                          IncSession             *inc_session);
101 static void inc_progress_dialog_set_progress
102                                         (IncProgressDialog      *inc_dialog,
103                                          IncSession             *inc_session);
104
105 static void inc_progress_dialog_update_periodic
106                                         (IncProgressDialog      *inc_dialog,
107                                          IncSession             *inc_session);
108
109 static gint inc_recv_data_progressive   (Session        *session,
110                                          guint           cur_len,
111                                          guint           total_len,
112                                          gpointer        data);
113 static gint inc_recv_data_finished      (Session        *session,
114                                          guint           len,
115                                          gpointer        data);
116 static gint inc_recv_message            (Session        *session,
117                                          const gchar    *msg,
118                                          gpointer        data);
119 static gint inc_drop_message            (Pop3Session    *session,
120                                          const gchar    *file);
121
122 static void inc_put_error               (IncState        istate,
123                                          Pop3Session    *session);
124
125 static void inc_cancel_cb               (GtkWidget      *widget,
126                                          gpointer        data);
127 static gint inc_dialog_delete_cb        (GtkWidget      *widget,
128                                          GdkEventAny    *event,
129                                          gpointer        data);
130
131 static gint get_spool                   (FolderItem     *dest,
132                                          const gchar    *mbox);
133
134 static gint inc_spool_account(PrefsAccount *account);
135 static gint inc_all_spool(void);
136 static void inc_autocheck_timer_set_interval    (guint           interval);
137 static gint inc_autocheck_func                  (gpointer        data);
138
139 static void inc_notify_cmd              (gint new_msgs, 
140                                          gboolean notify);
141         
142 /**
143  * inc_finished:
144  * @mainwin: Main window.
145  * @new_messages: TRUE if some messages have been received.
146  * 
147  * Update the folder view and the summary view after receiving
148  * messages.  If @new_messages is FALSE, this function avoids unneeded
149  * updating.
150  **/
151 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
152 {
153         FolderItem *item;
154
155         if (prefs_common.scan_all_after_inc)
156                 folderview_check_new(NULL);
157
158         if (!new_messages && !prefs_common.scan_all_after_inc) return;
159
160         if (prefs_common.open_inbox_on_inc) {
161                 item = cur_account && cur_account->inbox
162                         ? folder_find_item_from_identifier(cur_account->inbox)
163                         : folder_get_default_inbox();
164                 folderview_unselect(mainwin->folderview);
165                 folderview_select(mainwin->folderview, item);
166         }
167 }
168
169 void inc_mail(MainWindow *mainwin, gboolean notify)
170 {
171         gint new_msgs = 0;
172         gint account_new_msgs = 0;
173
174         if (inc_lock_count) return;
175
176         if (prefs_common.work_offline)
177                 if (alertpanel(_("Offline warning"), 
178                                _("You're working offline. Override?"),
179                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
180                 return;
181
182         inc_lock();
183         inc_autocheck_timer_remove();
184         main_window_lock(mainwin);
185
186         if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
187                 /* external incorporating program */
188                 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
189                         main_window_unlock(mainwin);
190                         inc_autocheck_timer_set();
191                         return;
192                 }
193         } else {
194                 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
195                 if (account_new_msgs > 0)
196                         new_msgs += account_new_msgs;
197         }
198
199         inc_finished(mainwin, new_msgs > 0);
200         main_window_unlock(mainwin);
201         inc_notify_cmd(new_msgs, notify);
202         inc_autocheck_timer_set();
203         inc_unlock();
204 }
205
206 void inc_pop_before_smtp(PrefsAccount *acc)
207 {
208         IncProgressDialog *inc_dialog;
209         IncSession *session;
210         MainWindow *mainwin;
211
212         mainwin = mainwindow_get_mainwindow();
213
214         session = inc_session_new(acc);
215         if (!session) return;
216         POP3_SESSION(session->session)->pop_before_smtp = TRUE;
217                 
218         inc_dialog = inc_progress_dialog_create(FALSE);
219         inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
220                                                session);
221         /* FIXME: assumes to attach to first main window */
222         inc_dialog->mainwin = mainwin;
223         inc_progress_dialog_set_list(inc_dialog);
224
225         if (mainwin) {
226                 toolbar_main_set_sensitive(mainwin);
227                 main_window_set_menu_sensitive(mainwin);
228         }
229                         
230         inc_start(inc_dialog);
231 }
232
233 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
234 {
235         IncProgressDialog *inc_dialog;
236         IncSession *session;
237         
238         switch (account->protocol) {
239         case A_IMAP4:
240         case A_NNTP:
241                 /* Melvin: bug [14]
242                  * FIXME: it should return foldeview_check_new() value.
243                  * TODO: do it when bug [19] is fixed (IMAP folder sets 
244                  * an incorrect new message count)
245                  */
246                 folderview_check_new(FOLDER(account->folder));
247                 return 0;
248         case A_POP3:
249         case A_APOP:
250                 session = inc_session_new(account);
251                 if (!session) return 0;
252                 
253                 inc_dialog = inc_progress_dialog_create(FALSE);
254                 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
255                                                        session);
256                 inc_dialog->mainwin = mainwin;
257                 inc_progress_dialog_set_list(inc_dialog);
258
259                 if (mainwin) {
260                         toolbar_main_set_sensitive(mainwin);
261                         main_window_set_menu_sensitive(mainwin);
262                 }
263                         
264                 return inc_start(inc_dialog);
265
266         case A_LOCAL:
267                 return inc_spool_account(account);
268
269         default:
270                 break;
271         }
272         return 0;
273 }
274
275 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
276 {
277         gint new_msgs;
278
279         if (inc_lock_count) return 0;
280
281         if (prefs_common.work_offline)
282                 if (alertpanel(_("Offline warning"), 
283                                _("You're working offline. Override?"),
284                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
285                         return 0;
286
287         inc_autocheck_timer_remove();
288         main_window_lock(mainwin);
289
290         new_msgs = inc_account_mail_real(mainwin, account);
291
292         inc_finished(mainwin, new_msgs > 0);
293         main_window_unlock(mainwin);
294         inc_autocheck_timer_set();
295
296         return new_msgs;
297 }
298
299 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
300                           gboolean notify)
301 {
302         GList *list, *queue_list = NULL;
303         IncProgressDialog *inc_dialog;
304         gint new_msgs = 0;
305         gint account_new_msgs = 0;
306         
307         if (prefs_common.work_offline)
308                 if (alertpanel(_("Offline warning"), 
309                                _("You're working offline. Override?"),
310                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
311                 return;
312
313         if (inc_lock_count) return;
314
315         inc_autocheck_timer_remove();
316         main_window_lock(mainwin);
317
318         list = account_get_list();
319         if (!list) {
320                 inc_finished(mainwin, new_msgs > 0);
321                 main_window_unlock(mainwin);
322                 inc_notify_cmd(new_msgs, notify);
323                 inc_autocheck_timer_set();
324                 return;
325         }
326
327         /* check local folders */
328         account_new_msgs = inc_all_spool();
329         if (account_new_msgs > 0)
330                 new_msgs += account_new_msgs;
331
332         /* check IMAP4 / News folders */
333         for (list = account_get_list(); list != NULL; list = list->next) {
334                 PrefsAccount *account = list->data;
335                 if ((account->protocol == A_IMAP4 ||
336                      account->protocol == A_NNTP) && account->recv_at_getall) {
337                         new_msgs += folderview_check_new(FOLDER(account->folder));
338                 }
339         }
340
341         /* check POP3 accounts */
342         for (list = account_get_list(); list != NULL; list = list->next) {
343                 IncSession *session;
344                 PrefsAccount *account = list->data;
345
346                 if (account->recv_at_getall) {
347                         session = inc_session_new(account);
348                         if (session)
349                                 queue_list = g_list_append(queue_list, session);
350                 }
351         }
352
353         if (queue_list) {
354                 inc_dialog = inc_progress_dialog_create(autocheck);
355                 inc_dialog->queue_list = queue_list;
356                 inc_dialog->mainwin = mainwin;
357                 inc_progress_dialog_set_list(inc_dialog);
358
359                 toolbar_main_set_sensitive(mainwin);
360                 main_window_set_menu_sensitive(mainwin);
361                 new_msgs += inc_start(inc_dialog);
362         }
363
364         inc_finished(mainwin, new_msgs > 0);
365         main_window_unlock(mainwin);
366         inc_notify_cmd(new_msgs, notify);
367         inc_autocheck_timer_set();
368 }
369
370 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
371 {
372         IncProgressDialog *dialog;
373         ProgressDialog *progress;
374
375         dialog = g_new0(IncProgressDialog, 1);
376
377         progress = progress_dialog_create();
378         gtk_window_set_title(GTK_WINDOW(progress->window),
379                              _("Retrieving new messages"));
380         g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
381                          G_CALLBACK(inc_cancel_cb), dialog);
382         g_signal_connect(G_OBJECT(progress->window), "delete_event",
383                          G_CALLBACK(inc_dialog_delete_cb), dialog);
384         /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
385
386         progress_dialog_get_fraction(progress);
387
388         stock_pixbuf_gdk(progress->list_view, STOCK_PIXMAP_COMPLETE,
389                          &okpix);
390         stock_pixbuf_gdk(progress->list_view, STOCK_PIXMAP_CONTINUE,
391                          &currentpix);
392         stock_pixbuf_gdk(progress->list_view, STOCK_PIXMAP_ERROR,
393                          &errorpix);
394
395         if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
396             (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
397              !autocheck)) {
398                 dialog->show_dialog = TRUE;
399                 gtk_widget_show_now(progress->window);
400         }
401
402         dialog->dialog = progress;
403         gettimeofday(&dialog->progress_tv, NULL);
404         gettimeofday(&dialog->folder_tv, NULL);
405         dialog->queue_list = NULL;
406         dialog->cur_row = 0;
407
408         inc_dialog_list = g_list_append(inc_dialog_list, dialog);
409
410         return dialog;
411 }
412
413 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
414 {
415         GList *list;
416
417         for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
418                 IncSession *session = list->data;
419                 Pop3Session *pop3_session = POP3_SESSION(session->session);
420
421                 session->data = inc_dialog;
422
423                 progress_dialog_list_set(inc_dialog->dialog,
424                                          -1, NULL,
425                                          pop3_session->ac_prefs->account_name,
426                                          _("Standby"));
427         }
428 }
429
430 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
431 {
432         progress_dialog_get_fraction(inc_dialog->dialog);
433         progress_dialog_set_label(inc_dialog->dialog, "");
434         if (inc_dialog->mainwin)
435                 main_window_progress_off(inc_dialog->mainwin);
436 }
437
438 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
439 {
440         g_return_if_fail(inc_dialog != NULL);
441
442         inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
443
444         if (inc_dialog->mainwin)
445                 main_window_progress_off(inc_dialog->mainwin);
446         progress_dialog_destroy(inc_dialog->dialog);
447
448         g_free(inc_dialog);
449 }
450
451 static IncSession *inc_session_new(PrefsAccount *account)
452 {
453         IncSession *session;
454
455         g_return_val_if_fail(account != NULL, NULL);
456
457         if (account->protocol != A_POP3)
458                 return NULL;
459         if (!account->recv_server || !account->userid)
460                 return NULL;
461
462         session = g_new0(IncSession, 1);
463
464         session->session = pop3_session_new(account);
465         session->session->data = session;
466         POP3_SESSION(session->session)->drop_message = inc_drop_message;
467         session_set_recv_message_notify(session->session,
468                                         inc_recv_message, session);
469         session_set_recv_data_progressive_notify(session->session,
470                                                  inc_recv_data_progressive,
471                                                  session);
472         session_set_recv_data_notify(session->session,
473                                      inc_recv_data_finished, session);
474
475         return session;
476 }
477
478 static void inc_session_destroy(IncSession *session)
479 {
480         g_return_if_fail(session != NULL);
481
482         session_destroy(session->session);
483         g_free(session);
484 }
485
486 static gint inc_start(IncProgressDialog *inc_dialog)
487 {
488         IncSession *session;
489         GList *qlist;
490         Pop3Session *pop3_session;
491         IncState inc_state;
492         gint error_num = 0;
493         gint new_msgs = 0;
494         gchar *msg;
495         gchar *fin_msg;
496         FolderItem *processing, *inbox;
497         MsgInfo *msginfo;
498         GSList *msglist, *msglist_element;
499         gboolean cancelled = FALSE;
500
501         qlist = inc_dialog->queue_list;
502         while (qlist != NULL) {
503                 GList *next = qlist->next;
504
505                 session = qlist->data;
506                 pop3_session = POP3_SESSION(session->session); 
507                 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
508                 if (pop3_session->ac_prefs->passwd)
509                         pop3_session->pass =
510                                 g_strdup(pop3_session->ac_prefs->passwd);
511                 else if (pop3_session->ac_prefs->tmp_pass)
512                         pop3_session->pass =
513                                 g_strdup(pop3_session->ac_prefs->tmp_pass);
514                 else {
515                         gchar *pass;
516
517                         if (inc_dialog->show_dialog)
518                                 manage_window_focus_in
519                                         (inc_dialog->dialog->window,
520                                          NULL, NULL);
521
522                         pass = input_dialog_query_password
523                                 (pop3_session->ac_prefs->recv_server,
524                                  pop3_session->user);
525
526                         if (inc_dialog->show_dialog)
527                                 manage_window_focus_out
528                                         (inc_dialog->dialog->window,
529                                          NULL, NULL);
530
531                         if (pass) {
532                                 pop3_session->ac_prefs->tmp_pass =
533                                         g_strdup(pass);
534                                 pop3_session->pass = pass;
535                         }
536                 }
537
538                 qlist = next;
539         }
540
541 #define SET_PIXMAP_AND_TEXT(pix, str)                                      \
542 {                                                                          \
543         progress_dialog_list_set(inc_dialog->dialog,                       \
544                                  inc_dialog->cur_row,                      \
545                                  pix,                                      \
546                                  NULL,                                     \
547                                  str);                                     \
548 }
549
550         for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
551                 session = inc_dialog->queue_list->data;
552                 pop3_session = POP3_SESSION(session->session);
553
554                 if (pop3_session->pass == NULL) {
555                         SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
556                         inc_session_destroy(session);
557                         inc_dialog->queue_list =
558                                 g_list_remove(inc_dialog->queue_list, session);
559                         continue;
560                 }
561
562                 inc_progress_dialog_clear(inc_dialog);
563                 progress_dialog_list_select_row(inc_dialog->dialog,
564                                                 inc_dialog->cur_row);
565
566                 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
567
568                 /* begin POP3 session */
569                 inc_state = inc_pop3_session_do(session);
570
571                 switch (inc_state) {
572                 case INC_SUCCESS:
573                         if (pop3_session->cur_total_num > 0)
574                                 msg = g_strdup_printf
575                                         (_("Done (%d message(s) (%s) received)"),
576                                          pop3_session->cur_total_num,
577                                          to_human_readable(pop3_session->cur_total_recv_bytes));
578                         else
579                                 msg = g_strdup_printf(_("Done (no new messages)"));
580                         SET_PIXMAP_AND_TEXT(okpix, msg);
581                         g_free(msg);
582                         break;
583                 case INC_CONNECT_ERROR:
584                         SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
585                         break;
586                 case INC_AUTH_FAILED:
587                         SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
588                         break;
589                 case INC_LOCKED:
590                         SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
591                         break;
592                 case INC_ERROR:
593                 case INC_NO_SPACE:
594                 case INC_IO_ERROR:
595                 case INC_SOCKET_ERROR:
596                 case INC_EOF:
597                         SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
598                         break;
599                 case INC_TIMEOUT:
600                         SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
601                         break;
602                 case INC_CANCEL:
603                         SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
604                         if (!inc_dialog->show_dialog)
605                                 cancelled = TRUE;
606                         break;
607                 default:
608                         break;
609                 }
610                 
611                 if (pop3_session->error_val == PS_AUTHFAIL) {
612                         if(!prefs_common.no_recv_err_panel) {
613                                 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
614                                     ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
615                                         manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
616                         }
617                 }
618
619                 /* CLAWS: perform filtering actions on dropped message */
620                 /* CLAWS: get default inbox (perhaps per account) */
621                 if (pop3_session->ac_prefs->inbox) {
622                         /* CLAWS: get destination folder / mailbox */
623                         inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
624                         if (!inbox)
625                                 inbox = folder_get_default_inbox();
626                 } else
627                         inbox = folder_get_default_inbox();
628
629                 /* get list of messages in processing */
630                 processing = folder_get_default_processing();
631                 folder_item_scan(processing);
632                 msglist = folder_item_get_msg_list(processing);
633
634                 /* process messages */
635                 folder_item_update_freeze();
636                 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
637                         msginfo = (MsgInfo *) msglist_element->data;
638                         if (!pop3_session->ac_prefs->filter_on_recv || !procmsg_msginfo_filter(msginfo))
639                                 folder_item_move_msg(inbox, msginfo);
640                         procmsg_msginfo_free(msginfo);
641                 }
642                 folder_item_update_thaw();
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);
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_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
713                                      GTK_STOCK_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_fraction
919                 (inc_dialog->dialog,(gfloat)cur_total / (gfloat)total);
920
921         g_snprintf(buf, sizeof(buf), "%d / %d",
922                    pop3_session->cur_msg, pop3_session->count);
923         gtk_progress_bar_set_text
924                 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), buf);
925         gtk_progress_bar_set_fraction
926                 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
927                  (gfloat)cur_total / (gfloat)total);
928
929         if (pop3_session->cur_total_num > 0) {
930                 g_snprintf(buf, sizeof(buf),
931                            _("Retrieving (%d message(s) (%s) received)"),
932                            pop3_session->cur_total_num,
933                            to_human_readable
934                            (pop3_session->cur_total_recv_bytes));
935                 progress_dialog_list_set_status(inc_dialog->dialog,
936                                                 inc_dialog->cur_row,
937                                                 buf);
938         }
939 }
940
941 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
942                                                 IncSession *inc_session)
943 {
944         struct timeval tv_cur;
945         struct timeval tv_result;
946         gint msec;
947
948         gettimeofday(&tv_cur, NULL);
949
950         tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
951         tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
952         if (tv_result.tv_usec < 0) {
953                 tv_result.tv_sec--;
954                 tv_result.tv_usec += 1000000;
955         }
956
957         msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
958         if (msec > PROGRESS_UPDATE_INTERVAL) {
959                 inc_progress_dialog_update(inc_dialog, inc_session);
960                 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
961                 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
962         }
963 }
964
965 static gint inc_recv_data_progressive(Session *session, guint cur_len,
966                                       guint total_len, gpointer data)
967 {
968         IncSession *inc_session = (IncSession *)data;
969         Pop3Session *pop3_session = POP3_SESSION(session);
970         IncProgressDialog *inc_dialog;
971         gint cur_total;
972
973         g_return_val_if_fail(inc_session != NULL, -1);
974
975         if (pop3_session->state != POP3_RETR &&
976             pop3_session->state != POP3_RETR_RECV &&
977             pop3_session->state != POP3_DELETE &&
978             pop3_session->state != POP3_LOGOUT) return 0;
979
980         if (!pop3_session->new_msg_exist) return 0;
981
982         cur_total = pop3_session->cur_total_bytes + cur_len;
983         if (cur_total > pop3_session->total_bytes)
984                 cur_total = pop3_session->total_bytes;
985         inc_session->cur_total_bytes = cur_total;
986
987         inc_dialog = (IncProgressDialog *)inc_session->data;
988         inc_progress_dialog_update_periodic(inc_dialog, inc_session);
989
990         return 0;
991 }
992
993 static gint inc_recv_data_finished(Session *session, guint len, 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         inc_recv_data_progressive(session, 0, 0, inc_session);
1003
1004         if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1005                 inc_progress_dialog_update(inc_dialog, inc_session);
1006         }
1007
1008         return 0;
1009 }
1010
1011 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1012 {
1013         IncSession *inc_session = (IncSession *)data;
1014         IncProgressDialog *inc_dialog;
1015
1016         g_return_val_if_fail(inc_session != NULL, -1);
1017
1018         inc_dialog = (IncProgressDialog *)inc_session->data;
1019
1020         switch (POP3_SESSION(session)->state) {
1021         case POP3_GETAUTH_USER:
1022         case POP3_GETAUTH_PASS:
1023         case POP3_GETAUTH_APOP:
1024         case POP3_GETRANGE_STAT:
1025         case POP3_GETRANGE_LAST:
1026         case POP3_GETRANGE_UIDL:
1027         case POP3_GETSIZE_LIST:
1028                 inc_progress_dialog_update(inc_dialog, inc_session);
1029                 break;
1030         case POP3_RETR:
1031                 inc_recv_data_progressive(session, 0, 0, inc_session);
1032                 break;
1033         case POP3_LOGOUT:
1034                 inc_progress_dialog_update(inc_dialog, inc_session);
1035                 break;
1036         default:
1037                 break;
1038         }
1039
1040         return 0;
1041 }
1042
1043 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1044 {
1045         FolderItem *inbox;
1046         FolderItem *dropfolder;
1047         IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1048         gint msgnum;
1049
1050         g_return_val_if_fail(inc_session != NULL, -1);
1051
1052         if (session->ac_prefs->inbox) {
1053                 inbox = folder_find_item_from_identifier
1054                         (session->ac_prefs->inbox);
1055                 if (!inbox)
1056                         inbox = folder_get_default_inbox();
1057         } else
1058                 inbox = folder_get_default_inbox();
1059         if (!inbox) {
1060                 unlink(file);
1061                 return -1;
1062         }
1063
1064         /* CLAWS: claws uses a global .processing folder for the filtering. */
1065         dropfolder = folder_get_default_processing();
1066
1067         /* add msg file to drop folder */
1068         if ((msgnum = folder_item_add_msg(
1069                         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, Pop3Session *session)
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_printf(_("Connection to %s:%d failed."),
1089                                           SESSION(session)->server, 
1090                                           SESSION(session)->port);
1091                 break;
1092         case INC_ERROR:
1093                 log_msg = _("Error occurred while processing mail.");
1094                 if (prefs_common.no_recv_err_panel)
1095                         break;
1096                 if (session->error_msg)
1097                         err_msg = g_strdup_printf
1098                                 (_("Error occurred while processing mail:\n%s"),
1099                                  session->error_msg);
1100                 else
1101                         err_msg = g_strdup(log_msg);
1102                 break;
1103         case INC_NO_SPACE:
1104                 log_msg = _("No disk space left.");
1105                 err_msg = g_strdup(log_msg);
1106                 fatal_error = TRUE;
1107                 break;
1108         case INC_IO_ERROR:
1109                 log_msg = _("Can't write file.");
1110                 err_msg = g_strdup(log_msg);
1111                 fatal_error = TRUE;
1112                 break;
1113         case INC_SOCKET_ERROR:
1114                 log_msg = _("Socket error.");
1115                 if (prefs_common.no_recv_err_panel)
1116                         break;
1117                 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1118                                           SESSION(session)->server, 
1119                                           SESSION(session)->port);
1120                 break;
1121         case INC_EOF:
1122                 log_msg = _("Connection closed by the remote host.");
1123                 if (prefs_common.no_recv_err_panel)
1124                         break;
1125                 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."), 
1126                                           SESSION(session)->server, 
1127                                           SESSION(session)->port);
1128                 break;
1129         case INC_LOCKED:
1130                 log_msg = _("Mailbox is locked.");
1131                 if (prefs_common.no_recv_err_panel)
1132                         break;
1133                 if (session->error_msg)
1134                         err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1135                                                   session->error_msg);
1136                 else
1137                         err_msg = g_strdup(log_msg);
1138                 break;
1139         case INC_AUTH_FAILED:
1140                 log_msg = _("Authentication failed.");
1141                 if (prefs_common.no_recv_err_panel)
1142                         break;
1143                 if (session->error_msg)
1144                         err_msg = g_strdup_printf
1145                                 (_("Authentication failed:\n%s"), session->error_msg);
1146                 else
1147                         err_msg = g_strdup(log_msg);
1148                 break;
1149         case INC_TIMEOUT:
1150                 log_msg = _("Session timed out.");
1151                 if (prefs_common.no_recv_err_panel)
1152                         break;
1153                 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."), 
1154                                           SESSION(session)->server, 
1155                                           SESSION(session)->port);
1156                 break;
1157         default:
1158                 break;
1159         }
1160
1161         if (log_msg) {
1162                 if (fatal_error)
1163                         log_error("%s\n", log_msg);
1164                 else
1165                         log_warning("%s\n", log_msg);
1166         }
1167         if (err_msg) {
1168                 alertpanel_error_log(err_msg);
1169                 g_free(err_msg);
1170         }
1171 }
1172
1173 static void inc_cancel(IncProgressDialog *dialog)
1174 {
1175         IncSession *session;
1176
1177         g_return_if_fail(dialog != NULL);
1178
1179         if (dialog->queue_list == NULL) {
1180                 inc_progress_dialog_destroy(dialog);
1181                 return;
1182         }
1183
1184         session = dialog->queue_list->data;
1185
1186         session->inc_state = INC_CANCEL;
1187
1188         log_message(_("Incorporation cancelled\n"));
1189 }
1190
1191 gboolean inc_is_active(void)
1192 {
1193         return (inc_dialog_list != NULL);
1194 }
1195
1196 void inc_cancel_all(void)
1197 {
1198         GList *cur;
1199
1200         for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1201                 inc_cancel((IncProgressDialog *)cur->data);
1202 }
1203
1204 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1205 {
1206         inc_cancel((IncProgressDialog *)data);
1207 }
1208
1209 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1210                                  gpointer data)
1211 {
1212         IncProgressDialog *dialog = (IncProgressDialog *)data;
1213
1214         if (dialog->queue_list == NULL)
1215                 inc_progress_dialog_destroy(dialog);
1216
1217         return TRUE;
1218 }
1219
1220 static gint inc_spool_account(PrefsAccount *account)
1221 {
1222         FolderItem *inbox;
1223         gchar *mbox;
1224         gint result;
1225
1226         if (account->inbox) {
1227                 inbox = folder_find_item_from_path(account->inbox);
1228                 if (!inbox)
1229                         inbox = folder_get_default_inbox();
1230         } else
1231                 inbox = folder_get_default_inbox();
1232
1233         if (is_file_exist(account->local_mbox))
1234                 mbox = g_strdup(account->local_mbox);
1235         else if (is_dir_exist(account->local_mbox)) 
1236                 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1237                                    g_get_user_name(), NULL);
1238         else {
1239                 debug_print("%s: local mailbox not found.\n", 
1240                             account->local_mbox);
1241                 return -1;
1242         }
1243         
1244         result = get_spool(inbox, mbox);
1245         g_free(mbox);
1246         
1247         statusbar_pop_all();
1248         
1249         return result;
1250 }
1251
1252 static gint inc_all_spool(void)
1253 {
1254         GList *list = NULL;
1255         gint new_msgs = 0;
1256         gint account_new_msgs = 0;
1257
1258         list = account_get_list();
1259         if (!list) return 0;
1260
1261         for (; list != NULL; list = list->next) {
1262                 PrefsAccount *account = list->data;
1263
1264                 if ((account->protocol == A_LOCAL) &&
1265                     (account->recv_at_getall)) {
1266                         account_new_msgs = inc_spool_account(account);
1267                         if (account_new_msgs > 0)
1268                                 new_msgs += account_new_msgs;
1269                 }
1270         }
1271
1272         return new_msgs;
1273 }
1274
1275 static gint get_spool(FolderItem *dest, const gchar *mbox)
1276 {
1277         gint msgs, size;
1278         gint lockfd;
1279         gchar tmp_mbox[MAXPATHLEN + 1];
1280
1281         g_return_val_if_fail(dest != NULL, -1);
1282         g_return_val_if_fail(mbox != NULL, -1);
1283
1284         if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1285                 debug_print("%s: no messages in local mailbox.\n", mbox);
1286                 return 0;
1287         } else if (size < 0)
1288                 return -1;
1289
1290         if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1291                 return -1;
1292
1293         g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1294                    get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1295
1296         if (copy_mbox(mbox, tmp_mbox) < 0) {
1297                 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1298                 return -1;
1299         }
1300
1301         debug_print("Getting new messages from %s into %s...\n",
1302                     mbox, dest->path);
1303
1304         msgs = proc_mbox(dest, tmp_mbox, TRUE);
1305
1306         unlink(tmp_mbox);
1307         if (msgs >= 0) empty_mbox(mbox);
1308         unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1309
1310         return msgs;
1311 }
1312
1313 void inc_lock(void)
1314 {
1315         inc_lock_count++;
1316 }
1317
1318 void inc_unlock(void)
1319 {
1320         if (inc_lock_count > 0)
1321                 inc_lock_count--;
1322 }
1323
1324 static guint autocheck_timer = 0;
1325 static gpointer autocheck_data = NULL;
1326
1327 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1328 {
1329
1330         gchar *buf, *numpos, *ret_str;
1331         gssize by_read = 0, by_written = 0;
1332
1333         if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1334             *prefs_common.newmail_notify_cmd))
1335                      return;
1336         buf = g_strdup(prefs_common.newmail_notify_cmd);
1337         if ((numpos = strstr(buf, "%d")) != NULL) {
1338                 gchar *buf2;
1339
1340                 *numpos = '\0';
1341                 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1342                 g_free(buf);
1343                 buf = buf2;
1344         }
1345
1346         ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1347                                      NULL);
1348         if (ret_str && by_written) {
1349                 g_free(buf);
1350                 buf = ret_str;
1351         }
1352         debug_print("executing new mail notification command: %s\n", buf);
1353         execute_command_line(buf, TRUE);
1354
1355         g_free(buf);
1356 }
1357  
1358 void inc_autocheck_timer_init(MainWindow *mainwin)
1359 {
1360         autocheck_data = mainwin;
1361         inc_autocheck_timer_set();
1362 }
1363
1364 static void inc_autocheck_timer_set_interval(guint interval)
1365 {
1366         inc_autocheck_timer_remove();
1367         /* last test is to avoid re-enabling auto_check after modifying 
1368            the common preferences */
1369         if (prefs_common.autochk_newmail && autocheck_data
1370             && prefs_common.work_offline == FALSE) {
1371                 autocheck_timer = gtk_timeout_add
1372                         (interval, inc_autocheck_func, autocheck_data);
1373                 debug_print("added timer = %d\n", autocheck_timer);
1374         }
1375 }
1376
1377 void inc_autocheck_timer_set(void)
1378 {
1379         inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1380 }
1381
1382 void inc_autocheck_timer_remove(void)
1383 {
1384         if (autocheck_timer) {
1385                 debug_print("removed timer = %d\n", autocheck_timer);
1386                 gtk_timeout_remove(autocheck_timer);
1387                 autocheck_timer = 0;
1388         }
1389 }
1390
1391 static gint inc_autocheck_func(gpointer data)
1392 {
1393         MainWindow *mainwin = (MainWindow *)data;
1394
1395         if (inc_lock_count) {
1396                 debug_print("autocheck is locked.\n");
1397                 inc_autocheck_timer_set_interval(1000);
1398                 return FALSE;
1399         }
1400
1401         inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1402
1403         return FALSE;
1404 }