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