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