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