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