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