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