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