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