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