sync with 0.9.2cvs6
[claws.git] / src / inc.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gtk/gtkmain.h>
28 #include <gtk/gtkwindow.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtkprogressbar.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <time.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/wait.h>
38 #include <signal.h>
39 #include <errno.h>
40
41 #include "intl.h"
42 #include "main.h"
43 #include "inc.h"
44 #include "mainwindow.h"
45 #include "folderview.h"
46 #include "summaryview.h"
47 #include "prefs_common.h"
48 #include "prefs_account.h"
49 #include "account.h"
50 #include "procmsg.h"
51 #include "socket.h"
52 #include "ssl.h"
53 #include "pop.h"
54 #include "recv.h"
55 #include "mbox.h"
56 #include "utils.h"
57 #include "gtkutils.h"
58 #include "statusbar.h"
59 #include "manage_window.h"
60 #include "stock_pixmap.h"
61 #include "progressdialog.h"
62 #include "inputdialog.h"
63 #include "alertpanel.h"
64 #include "folder.h"
65 #include "filtering.h"
66 #include "log.h"
67 #include "hooks.h"
68
69 static GList *inc_dialog_list = NULL;
70
71 static guint inc_lock_count = 0;
72
73 static GdkPixmap *currentxpm;
74 static GdkBitmap *currentxpmmask;
75 static GdkPixmap *errorxpm;
76 static GdkBitmap *errorxpmmask;
77 static GdkPixmap *okxpm;
78 static GdkBitmap *okxpmmask;
79
80 #define MSGBUFSIZE      8192
81
82 static void inc_finished                (MainWindow             *mainwin,
83                                          gboolean                new_messages);
84 static gint inc_account_mail            (PrefsAccount           *account,
85                                          MainWindow             *mainwin);
86
87 static IncProgressDialog *inc_progress_dialog_create    (void);
88 static void inc_progress_dialog_set_list(IncProgressDialog      *inc_dialog);
89 static void inc_progress_dialog_destroy (IncProgressDialog      *inc_dialog);
90
91 static IncSession *inc_session_new      (PrefsAccount           *account);
92 static void inc_session_destroy         (IncSession             *session);
93 static gint inc_start                   (IncProgressDialog      *inc_dialog);
94 static IncState inc_pop3_session_do     (IncSession             *session);
95
96 static void inc_progress_dialog_set_label
97                                         (IncProgressDialog      *inc_dialog,
98                                          IncSession             *inc_session);
99
100 static gint inc_recv_data_progressive   (Session        *session,
101                                          guint           cur_len,
102                                          guint           total_len,
103                                          gpointer        data);
104 static gint inc_recv_data_finished      (Session        *session,
105                                          guint           len,
106                                          gpointer        data);
107 static gint inc_recv_message            (Session        *session,
108                                          const gchar    *msg,
109                                          gpointer        data);
110
111 static void inc_put_error               (IncState        istate,
112                                          const gchar    *msg);
113
114 static void inc_cancel_cb               (GtkWidget      *widget,
115                                          gpointer        data);
116 static gint inc_dialog_delete_cb        (GtkWidget      *widget,
117                                          GdkEventAny    *event,
118                                          gpointer        data);
119
120 static gint inc_spool                   (void);
121 static gint get_spool                   (FolderItem     *dest,
122                                          const gchar    *mbox);
123
124 static gint inc_spool_account(PrefsAccount *account);
125 static gint inc_all_spool(void);
126 static void inc_autocheck_timer_set_interval    (guint           interval);
127 static gint inc_autocheck_func                  (gpointer        data);
128
129 static void inc_notify_cmd              (gint new_msgs, 
130                                          gboolean notify);
131
132 #define FOLDER_SUMMARY_MISMATCH(f, s) \
133         (f) && (s) ? ((s)->newmsgs != (f)->new_msgs) || ((f)->unread_msgs != (s)->unread) || ((f)->total_msgs != (s)->messages) \
134         : FALSE
135         
136 /**
137  * inc_finished:
138  * @mainwin: Main window.
139  * @new_messages: TRUE if some messages have been received.
140  * 
141  * Update the folder view and the summary view after receiving
142  * messages.  If @new_messages is FALSE, this function avoids unneeded
143  * updating.
144  **/
145 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
146 {
147         FolderItem *item;
148
149         if (prefs_common.scan_all_after_inc)
150                 folderview_check_new(NULL);
151
152         if (!new_messages && !prefs_common.scan_all_after_inc) return;
153
154         if (prefs_common.open_inbox_on_inc) {
155                 item = cur_account && cur_account->inbox
156                         ? folder_find_item_from_identifier(cur_account->inbox)
157                         : folder_get_default_inbox();
158                 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {      
159                         folderview_unselect(mainwin->folderview);
160                         folderview_select(mainwin->folderview, item);
161                 }       
162         }
163 }
164
165 void inc_mail(MainWindow *mainwin, gboolean notify)
166 {
167         gint new_msgs = 0;
168         gint account_new_msgs = 0;
169
170         if (inc_lock_count) return;
171
172         if (prefs_common.work_offline)
173                 if (alertpanel(_("Offline warning"), 
174                                _("You're working offline. Override?"),
175                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
176                 return;
177
178         inc_lock();
179         inc_autocheck_timer_remove();
180         main_window_lock(mainwin);
181
182         if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
183                 /* external incorporating program */
184                 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
185                         main_window_unlock(mainwin);
186                         inc_autocheck_timer_set();
187                         return;
188                 }
189
190                 if (prefs_common.inc_local) {
191                         account_new_msgs = inc_spool();
192                         if (account_new_msgs > 0)
193                                 new_msgs += account_new_msgs;
194                 }
195         } else {
196                 if (prefs_common.inc_local) {
197                         account_new_msgs = inc_spool();
198                         if (account_new_msgs > 0)
199                                 new_msgs += account_new_msgs;
200                 }
201
202                 account_new_msgs = inc_account_mail(cur_account, mainwin);
203                 if (account_new_msgs > 0)
204                         new_msgs += account_new_msgs;
205         }
206
207         inc_finished(mainwin, new_msgs > 0);
208         main_window_unlock(mainwin);
209         inc_notify_cmd(new_msgs, notify);
210         inc_autocheck_timer_set();
211         inc_unlock();
212 }
213
214 void inc_pop_before_smtp(PrefsAccount *acc)
215 {
216         inc_account_mail(acc, NULL);
217 }
218
219 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
220 {
221         IncProgressDialog *inc_dialog;
222         IncSession *session;
223         FolderItem *item = NULL;
224         
225         if(mainwin && mainwin->summaryview)
226                 item = mainwin->summaryview->folder_item;
227
228         switch (account->protocol) {
229         case A_IMAP4:
230         case A_NNTP:
231                 /* Melvin: bug [14]
232                  * FIXME: it should return foldeview_check_new() value.
233                  * TODO: do it when bug [19] is fixed (IMAP folder sets 
234                  * an incorrect new message count)
235                  */
236                 folderview_check_new(FOLDER(account->folder));
237                 return 0;
238         case A_POP3:
239         case A_APOP:
240                 session = inc_session_new(account);
241                 if (!session) return 0;
242                 
243                 inc_dialog = inc_progress_dialog_create();
244                 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
245                                                        session);
246                 inc_dialog->mainwin = mainwin;
247                 inc_progress_dialog_set_list(inc_dialog);
248
249                 if (mainwin) {
250                         toolbar_main_set_sensitive(mainwin);
251                         main_window_set_menu_sensitive(mainwin);
252                 }
253                         
254                 return inc_start(inc_dialog);
255
256         case A_LOCAL:
257                 return inc_spool_account(account);
258
259         default:
260                 break;
261         }
262         return 0;
263 }
264
265 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
266 {
267         GList *list, *queue_list = NULL;
268         IncProgressDialog *inc_dialog;
269         gint new_msgs = 0;
270         gint account_new_msgs = 0;
271         
272         if (prefs_common.work_offline)
273                 if (alertpanel(_("Offline warning"), 
274                                _("You're working offline. Override?"),
275                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
276                 return;
277
278         if (inc_lock_count) return;
279
280         inc_autocheck_timer_remove();
281         main_window_lock(mainwin);
282
283         if (prefs_common.inc_local) {
284                 account_new_msgs = inc_spool();
285                 if (account_new_msgs > 0)
286                         new_msgs += account_new_msgs;   
287         }
288
289         list = account_get_list();
290         if (!list) {
291                 inc_finished(mainwin, new_msgs > 0);
292                 main_window_unlock(mainwin);
293                 inc_notify_cmd(new_msgs, notify);
294                 inc_autocheck_timer_set();
295                 return;
296         }
297
298         /* check local folders */
299         account_new_msgs = inc_all_spool();
300         if (account_new_msgs > 0)
301                 new_msgs += account_new_msgs;
302
303         /* check IMAP4 folders */
304         for (; list != NULL; list = list->next) {
305                 PrefsAccount *account = list->data;
306                 if ((account->protocol == A_IMAP4 ||
307                      account->protocol == A_NNTP) && account->recv_at_getall) {
308                         new_msgs += folderview_check_new(FOLDER(account->folder));
309                 }
310         }
311
312         /* check POP3 accounts */
313         for (list = account_get_list(); list != NULL; list = list->next) {
314                 IncSession *session;
315                 PrefsAccount *account = list->data;
316
317                 if (account->recv_at_getall) {
318                         session = inc_session_new(account);
319                         if (session)
320                                 queue_list = g_list_append(queue_list, session);
321                 }
322         }
323
324         if (!queue_list) {
325                 inc_finished(mainwin, new_msgs > 0);
326                 main_window_unlock(mainwin);
327                 inc_notify_cmd(new_msgs, notify);
328                 inc_autocheck_timer_set();
329                 return;
330         }
331
332         inc_dialog = inc_progress_dialog_create();
333         inc_dialog->queue_list = queue_list;
334         inc_dialog->mainwin = mainwin;
335         inc_progress_dialog_set_list(inc_dialog);
336
337         toolbar_main_set_sensitive(mainwin);
338         main_window_set_menu_sensitive(mainwin);
339
340         new_msgs += inc_start(inc_dialog);
341         inc_finished(mainwin, new_msgs > 0);
342         main_window_unlock(mainwin);
343         inc_notify_cmd(new_msgs, notify);
344         inc_autocheck_timer_set();
345 }
346
347 static IncProgressDialog *inc_progress_dialog_create(void)
348 {
349         IncProgressDialog *dialog;
350         ProgressDialog *progress;
351
352         dialog = g_new0(IncProgressDialog, 1);
353
354         progress = progress_dialog_create();
355         gtk_window_set_title(GTK_WINDOW(progress->window),
356                              _("Retrieving new messages"));
357         gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
358                            GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
359         gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
360                            GTK_SIGNAL_FUNC(inc_dialog_delete_cb), dialog);
361         /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
362
363         progress_dialog_set_value(progress, 0.0);
364
365         stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
366                          &okxpm, &okxpmmask);
367         stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
368                          &currentxpm, &currentxpmmask);
369         stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
370                          &errorxpm, &errorxpmmask);
371
372         if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
373             (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
374              manage_window_get_focus_window())) {
375                 dialog->show_dialog = TRUE;
376                 gtk_widget_show_now(progress->window);
377         }
378
379         dialog->dialog = progress;
380         dialog->queue_list = NULL;
381
382         inc_dialog_list = g_list_append(inc_dialog_list, dialog);
383
384         return dialog;
385 }
386
387 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
388 {
389         GList *list;
390
391         for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
392                 IncSession *session = list->data;
393                 Pop3Session *pop3_session = POP3_SESSION(session->session);
394                 gchar *text[3];
395
396                 session->data = inc_dialog;
397
398                 text[0] = NULL;
399                 text[1] = pop3_session->ac_prefs->account_name;
400                 text[2] = _("Standby");
401                 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
402         }
403 }
404
405 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
406 {
407         progress_dialog_set_value(inc_dialog->dialog, 0.0);
408         progress_dialog_set_label(inc_dialog->dialog, "");
409         gtk_progress_set_show_text
410                 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), FALSE);
411         if (inc_dialog->mainwin)
412                 gtk_progress_bar_update
413                         (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
414 }
415
416 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
417 {
418         g_return_if_fail(inc_dialog != NULL);
419
420         inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
421
422         gtk_progress_set_show_text
423                 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), FALSE);
424         if (inc_dialog->mainwin)
425                 gtk_progress_bar_update
426                         (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
427         progress_dialog_destroy(inc_dialog->dialog);
428
429         g_free(inc_dialog);
430 }
431
432 static IncSession *inc_session_new(PrefsAccount *account)
433 {
434         IncSession *session;
435
436         g_return_val_if_fail(account != NULL, NULL);
437
438         if (account->protocol != A_POP3 && account->protocol != A_APOP)
439                 return NULL;
440         if (!account->recv_server || !account->userid)
441                 return NULL;
442
443         session = g_new0(IncSession, 1);
444         session->session = pop3_session_new(account);
445         session->session->data = session;
446
447         return session;
448 }
449
450 static void inc_session_destroy(IncSession *session)
451 {
452         g_return_if_fail(session != NULL);
453
454         session_destroy(session->session);
455         g_free(session);
456 }
457
458 static gint inc_start(IncProgressDialog *inc_dialog)
459 {
460         IncSession *session;
461         GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
462         GList *qlist;
463         Pop3Session *pop3_session;
464         IncState inc_state;
465         gint num = 0;
466         gint error_num = 0;
467         gint new_msgs = 0;
468         gchar *msg;
469         gchar *fin_msg;
470         FolderItem *processing, *inbox;
471         MsgInfo *msginfo;
472         GSList *msglist, *msglist_element;
473
474         qlist = inc_dialog->queue_list;
475         while (qlist != NULL) {
476                 GList *next = qlist->next;
477
478                 session = qlist->data;
479                 pop3_session = POP3_SESSION(session->session); 
480                 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
481                 if (pop3_session->ac_prefs->passwd)
482                         pop3_session->pass =
483                                 g_strdup(pop3_session->ac_prefs->passwd);
484                 else if (pop3_session->ac_prefs->tmp_pass)
485                         pop3_session->pass =
486                                 g_strdup(pop3_session->ac_prefs->tmp_pass);
487                 else {
488                         gchar *pass;
489
490                         if (inc_dialog->show_dialog)
491                                 manage_window_focus_in
492                                         (inc_dialog->dialog->window,
493                                          NULL, NULL);
494
495                         pass = input_dialog_query_password
496                                 (pop3_session->ac_prefs->recv_server,
497                                  pop3_session->user);
498
499                         if (inc_dialog->show_dialog)
500                                 manage_window_focus_out
501                                         (inc_dialog->dialog->window,
502                                          NULL, NULL);
503
504                         if (pass) {
505                                 pop3_session->ac_prefs->tmp_pass =
506                                         g_strdup(pass);
507                                 pop3_session->pass = pass;
508                         }
509                 }
510
511                 qlist = next;
512         }
513
514         for (; inc_dialog->queue_list != NULL; num++) {
515                 session = inc_dialog->queue_list->data;
516                 pop3_session = POP3_SESSION(session->session);
517
518                 if (pop3_session->pass == NULL) {
519                         gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
520                         gtk_clist_set_text(clist, num, 2, _("Cancelled"));
521                         inc_session_destroy(session);
522                         inc_dialog->queue_list =
523                                 g_list_remove(inc_dialog->queue_list, session);
524                         continue;
525                 }
526
527                 inc_progress_dialog_clear(inc_dialog);
528                 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
529
530                 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
531                 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
532
533                 session_set_recv_message_notify(session->session,
534                                                 inc_recv_message, session);
535                 session_set_recv_data_progressive_notify
536                         (session->session, inc_recv_data_progressive, session);
537                 session_set_recv_data_notify(session->session,
538                                              inc_recv_data_finished, session);
539
540                 /* begin POP3 session */
541                 inc_state = inc_pop3_session_do(session);
542
543                 switch (inc_state) {
544                 case INC_SUCCESS:
545                         if (pop3_session->cur_total_num > 0)
546                                 msg = g_strdup_printf
547                                         (_("Done (%d message(s) (%s) received)"),
548                                          pop3_session->cur_total_num,
549                                          to_human_readable(pop3_session->cur_total_recv_bytes));
550                         else
551                                 msg = g_strdup_printf(_("Done (no new messages)"));
552                         gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
553                         gtk_clist_set_text(clist, num, 2, msg);
554                         g_free(msg);
555                         break;
556                 case INC_CONNECT_ERROR:
557                         gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
558                         gtk_clist_set_text(clist, num, 2, _("Connection failed"));
559                         break;
560                 case INC_AUTH_FAILED:
561                         gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
562                         gtk_clist_set_text(clist, num, 2, _("Auth failed"));
563                         break;
564                 case INC_LOCKED:
565                         gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
566                         gtk_clist_set_text(clist, num, 2, _("Locked"));
567                         break;
568                 case INC_ERROR:
569                 case INC_NO_SPACE:
570                 case INC_IO_ERROR:
571                 case INC_SOCKET_ERROR:
572                         gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
573                         gtk_clist_set_text(clist, num, 2, _("Error"));
574                         break;
575                 case INC_CANCEL:
576                         gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
577                         gtk_clist_set_text(clist, num, 2, _("Cancelled"));
578                         break;
579                 default:
580                         break;
581                 }
582                 
583                 if (pop3_session->error_val == PS_AUTHFAIL) {
584                         if(!prefs_common.no_recv_err_panel) {
585                                 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
586                                     ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
587                                         manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
588                                 }
589                                 alertpanel_error
590                                         (_("Authorization for %s on %s failed"),
591                                          pop3_session->user,
592                                          pop3_session->ac_prefs->recv_server);
593                         }
594                 }
595
596                 /* CLAWS: perform filtering actions on dropped message */
597                 /* CLAWS: get default inbox (perhaps per account) */
598                 if (pop3_session->ac_prefs->inbox) {
599                         /* CLAWS: get destination folder / mailbox */
600                         inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
601                         if (!inbox)
602                                 inbox = folder_get_default_inbox();
603                 } else
604                         inbox = folder_get_default_inbox();
605
606                 /* get list of messages in processing */
607                 processing = folder_get_default_processing();
608                 folder_item_scan(processing);
609                 msglist = folder_item_get_msg_list(processing);
610
611                 folder_item_update_freeze();
612
613                 /* process messages */
614                 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
615                         msginfo = (MsgInfo *) msglist_element->data;
616                         if (!pop3_session->ac_prefs->filter_on_recv || !procmsg_msginfo_filter(msginfo))
617                                 folder_item_move_msg(inbox, msginfo);
618                         procmsg_msginfo_free(msginfo);
619                 }
620                 g_slist_free(msglist);
621
622                 folder_item_update_thaw();
623
624                 statusbar_pop_all();
625
626                 new_msgs += pop3_session->cur_total_num;
627
628                 if (pop3_session->error_val == PS_AUTHFAIL &&
629                     pop3_session->ac_prefs->tmp_pass) {
630                         g_free(pop3_session->ac_prefs->tmp_pass);
631                         pop3_session->ac_prefs->tmp_pass = NULL;
632                 }
633
634                 pop3_write_uidl_list(pop3_session);
635
636                 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
637                         error_num++;
638                         if (inc_dialog->show_dialog)
639                                 manage_window_focus_in
640                                         (inc_dialog->dialog->window,
641                                          NULL, NULL);
642                         inc_put_error(inc_state, pop3_session->error_msg);
643                         if (inc_dialog->show_dialog)
644                                 manage_window_focus_out
645                                         (inc_dialog->dialog->window,
646                                          NULL, NULL);
647                         if (inc_state == INC_NO_SPACE ||
648                             inc_state == INC_IO_ERROR)
649                                 break;
650                 }
651
652                 inc_session_destroy(session);
653                 inc_dialog->queue_list =
654                         g_list_remove(inc_dialog->queue_list, session);
655         }
656
657         if (new_msgs > 0)
658                 fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
659                                           new_msgs);
660         else
661                 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
662
663         progress_dialog_set_label(inc_dialog->dialog, fin_msg);
664
665 #if 0
666         if (error_num && !prefs_common.no_recv_err_panel) {
667                 if (inc_dialog->show_dialog)
668                         manage_window_focus_in(inc_dialog->dialog->window,
669                                                NULL, NULL);
670                 alertpanel_error_log(_("Some errors occurred while getting mail."));
671                 if (inc_dialog->show_dialog)
672                         manage_window_focus_out(inc_dialog->dialog->window,
673                                                 NULL, NULL);
674         }
675 #endif
676
677         while (inc_dialog->queue_list != NULL) {
678                 session = inc_dialog->queue_list->data;
679                 inc_session_destroy(session);
680                 inc_dialog->queue_list =
681                         g_list_remove(inc_dialog->queue_list, session);
682         }
683
684         if (prefs_common.close_recv_dialog)
685                 inc_progress_dialog_destroy(inc_dialog);
686         else {
687                 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
688                                      fin_msg);
689                 gtk_label_set_text(GTK_LABEL(GTK_BIN(inc_dialog->dialog->cancel_btn)->child),
690                                    _("Close"));
691         }
692
693         g_free(fin_msg);
694
695         return new_msgs;
696 }
697
698 static IncState inc_pop3_session_do(IncSession *session)
699 {
700         Pop3Session *pop3_session = POP3_SESSION(session->session);
701         IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
702         gchar *server;
703         gushort port;
704         gchar *buf;
705
706         debug_print("getting new messages of account %s...\n",
707                     pop3_session->ac_prefs->account_name);
708                     
709         pop3_session->ac_prefs->last_pop_login_time = time(NULL);
710
711         buf = g_strdup_printf(_("%s: Retrieving new messages"),
712                               pop3_session->ac_prefs->recv_server);
713         gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
714         g_free(buf);
715
716         server = pop3_session->ac_prefs->recv_server;
717 #if USE_OPENSSL
718         port = pop3_session->ac_prefs->set_popport ?
719                 pop3_session->ac_prefs->popport :
720                 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
721         SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
722 #else
723         port = pop3_session->ac_prefs->set_popport ?
724                 pop3_session->ac_prefs->popport : 110;
725 #endif
726
727         buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
728         log_message("%s\n", buf);
729
730         progress_dialog_set_label(inc_dialog->dialog, buf);
731         g_free(buf);
732         GTK_EVENTS_FLUSH();
733
734
735         if (session_connect(SESSION(pop3_session), server, port) < 0) {
736                 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
737                             server, port);
738                 if(!prefs_common.no_recv_err_panel) {
739                         if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
740                             ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
741                                 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
742                         }
743                         alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
744                                          server, port);
745                         manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
746                 }
747                 session->inc_state = INC_CONNECT_ERROR;
748                 statusbar_pop_all();
749                 return INC_CONNECT_ERROR;
750         }
751
752         while (SESSION(pop3_session)->state != SESSION_DISCONNECTED &&
753                SESSION(pop3_session)->state != SESSION_ERROR &&
754                session->inc_state != INC_CANCEL)
755                 gtk_main_iteration();
756
757         statusbar_pop_all();
758
759         if (session->inc_state == INC_SUCCESS) {
760                 switch (pop3_session->error_val) {
761                 case PS_SUCCESS:
762                         if (SESSION(pop3_session)->state == SESSION_ERROR) {
763                                 if (pop3_session->state == POP3_READY)
764                                         session->inc_state = INC_CONNECT_ERROR;
765                                 else
766                                         session->inc_state = INC_ERROR;
767                         } else
768                                 session->inc_state = INC_SUCCESS;
769                         break;
770                 case PS_AUTHFAIL:
771                         session->inc_state = INC_AUTH_FAILED;
772                         break;
773                 case PS_IOERR:
774                         session->inc_state = INC_IO_ERROR;
775                         break;
776                 case PS_SOCKET:
777                         session->inc_state = INC_SOCKET_ERROR;
778                         break;
779                 case PS_LOCKBUSY:
780                         session->inc_state = INC_LOCKED;
781                         break;
782                 default:
783                         session->inc_state = INC_ERROR;
784                         break;
785                 }
786         }
787
788         return session->inc_state;
789 }
790
791 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
792                                           IncSession *inc_session)
793 {
794         gchar buf[MSGBUFSIZE];
795         ProgressDialog *dialog = inc_dialog->dialog;
796         Pop3Session *session;
797
798         g_return_if_fail(inc_session != NULL);
799
800         session = POP3_SESSION(inc_session->session);
801
802         switch (session->state) {
803         case POP3_GREETING:
804                 break;
805         case POP3_GETAUTH_USER:
806         case POP3_GETAUTH_PASS:
807         case POP3_GETAUTH_APOP:
808                 progress_dialog_set_label(dialog, _("Authenticating..."));
809                 statusbar_print_all(_("Retrieving messages from %s..."),
810                                     SESSION(session)->server);
811                 break;
812         case POP3_GETRANGE_STAT:
813                 progress_dialog_set_label
814                         (dialog, _("Getting the number of new messages (STAT)..."));
815                 break;
816         case POP3_GETRANGE_LAST:
817                 progress_dialog_set_label
818                         (dialog, _("Getting the number of new messages (LAST)..."));
819                 break;
820         case POP3_GETRANGE_UIDL:
821                 progress_dialog_set_label
822                         (dialog, _("Getting the number of new messages (UIDL)..."));
823                 break;
824         case POP3_GETSIZE_LIST:
825                 progress_dialog_set_label
826                         (dialog, _("Getting the size of messages (LIST)..."));
827                 break;
828         case POP3_RETR:
829                 gtk_progress_set_show_text
830                         (GTK_PROGRESS(inc_dialog->mainwin->progressbar), TRUE);
831                 g_snprintf(buf, sizeof(buf), "%d / %d",
832                            session->cur_msg, session->count);
833                 gtk_progress_set_format_string
834                         (GTK_PROGRESS(inc_dialog->mainwin->progressbar), buf);
835                 inc_recv_data_progressive
836                         (SESSION(session), 0,
837                          session->msg[session->cur_msg].size,
838                          inc_session);
839                 break;
840         case POP3_DELETE:
841                 if (session->msg[session->cur_msg].recv_time <
842                         session->current_time) {
843                         g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
844                                    session->cur_msg);
845                         progress_dialog_set_label(dialog, buf);
846                 }
847                 break;
848         case POP3_LOGOUT:
849                 progress_dialog_set_label(dialog, _("Quitting"));
850                 break;
851         default:
852                 break;
853         }
854 }
855
856 static gint inc_recv_data_progressive(Session *session, guint cur_len,
857                                       guint total_len, gpointer data)
858 {
859         gchar buf[MSGBUFSIZE];
860         IncSession *inc_session = (IncSession *)data;
861         Pop3Session *pop3_session = POP3_SESSION(session);
862         IncProgressDialog *inc_dialog;
863         ProgressDialog *dialog;
864         gint cur_total;
865         gchar *total_size;
866
867         g_return_val_if_fail(inc_session != NULL, -1);
868
869         if (pop3_session->state != POP3_RETR &&
870             pop3_session->state != POP3_RETR_RECV &&
871             pop3_session->state != POP3_DELETE &&
872             pop3_session->state != POP3_LOGOUT) return 0;
873
874         if (!pop3_session->new_msg_exist) return 0;
875
876         inc_dialog = (IncProgressDialog *)inc_session->data;
877         dialog = inc_dialog->dialog;
878
879         cur_total = pop3_session->cur_total_bytes + cur_len;
880         if (cur_total > pop3_session->total_bytes)
881                 cur_total = pop3_session->total_bytes;
882
883         Xstrdup_a(total_size, to_human_readable(pop3_session->total_bytes),
884                   return FALSE);
885         g_snprintf(buf, sizeof(buf),
886                    _("Retrieving message (%d / %d) (%s / %s)"),
887                    pop3_session->cur_msg, pop3_session->count,
888                    to_human_readable(cur_total), total_size);
889         progress_dialog_set_label(dialog, buf);
890         progress_dialog_set_percentage
891                 (dialog, (gfloat)cur_total / (gfloat)pop3_session->total_bytes);
892         if (inc_dialog->mainwin)
893                 gtk_progress_bar_update
894                         (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
895                          (gfloat)cur_total / (gfloat)pop3_session->total_bytes);
896         GTK_EVENTS_FLUSH();
897
898         return 0;
899 }
900
901 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
902 {
903         IncSession *inc_session = (IncSession *)data;
904         IncProgressDialog *inc_dialog;
905
906         g_return_val_if_fail(inc_session != NULL, -1);
907
908         inc_dialog = (IncProgressDialog *)inc_session->data;
909         inc_recv_data_progressive(session, 0, len, inc_session);
910         inc_progress_dialog_set_label(inc_dialog, inc_session);
911
912         return 0;
913 }
914
915 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
916 {
917         IncSession *inc_session = (IncSession *)data;
918         IncProgressDialog *inc_dialog;
919
920         g_return_val_if_fail(inc_session != NULL, -1);
921
922         inc_dialog = (IncProgressDialog *)inc_session->data;
923         inc_progress_dialog_set_label(inc_dialog, inc_session);
924
925         return 0;
926 }
927
928 gint inc_drop_message(const gchar *file, Pop3Session *session)
929 {
930         FolderItem *inbox;
931         FolderItem *dropfolder;
932         gint msgnum;
933
934         if (session->ac_prefs->inbox) {
935                 inbox = folder_find_item_from_identifier
936                         (session->ac_prefs->inbox);
937                 if (!inbox)
938                         inbox = folder_get_default_inbox();
939         } else
940                 inbox = folder_get_default_inbox();
941         if (!inbox) {
942                 unlink(file);
943                 return -1;
944         }
945
946         /* CLAWS: claws uses a global .processing folder for the filtering. */
947         dropfolder = folder_get_default_processing();
948
949         /* add msg file to drop folder */
950         if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
951                 unlink(file);
952                 return -1;
953         }
954
955         return 0;
956 }
957
958 static void inc_put_error(IncState istate, const gchar *msg)
959 {
960         switch (istate) {
961         case INC_CONNECT_ERROR:
962                 if (prefs_common.no_recv_err_panel)
963                         break;
964                 alertpanel_error(_("Connection failed."));
965                 break;
966         case INC_ERROR:
967                 if (prefs_common.no_recv_err_panel)
968                         break;
969                 if (msg)
970                         alertpanel_error(_("Error occurred while processing mail:\n%s"), msg);
971                 else
972                         alertpanel_error(_("Error occurred while processing mail."));
973                 break;
974         case INC_NO_SPACE:
975                 alertpanel_error(_("No disk space left."));
976                 break;
977         case INC_IO_ERROR:
978                 alertpanel_error(_("Can't write file."));
979                 break;
980         case INC_SOCKET_ERROR:
981                 if (prefs_common.no_recv_err_panel)
982                         break;
983                 alertpanel_error(_("Socket error."));
984                 break;
985         case INC_LOCKED:
986                 if (prefs_common.no_recv_err_panel)
987                         break;
988                 if (msg)
989                         alertpanel_error(_("Mailbox is locked:\n%s"), msg);
990                 else
991                         alertpanel_error(_("Mailbox is locked."));
992                 break;
993         case INC_AUTH_FAILED:
994                 if (prefs_common.no_recv_err_panel)
995                         break;
996                 if (msg)
997                         alertpanel_error(_("Authentication failed:\n%s"),
998                                          msg);
999                 else
1000                         alertpanel_error(_("Authentication failed."));
1001                 break;
1002         default:
1003                 break;
1004         }
1005 }
1006
1007 static void inc_cancel(IncProgressDialog *dialog)
1008 {
1009         IncSession *session;
1010
1011         g_return_if_fail(dialog != NULL);
1012
1013         if (dialog->queue_list == NULL) {
1014                 inc_progress_dialog_destroy(dialog);
1015                 return;
1016         }
1017
1018         session = dialog->queue_list->data;
1019
1020         session->inc_state = INC_CANCEL;
1021
1022         log_message(_("Incorporation cancelled\n"));
1023 }
1024
1025 gboolean inc_is_active(void)
1026 {
1027         return (inc_dialog_list != NULL);
1028 }
1029
1030 void inc_cancel_all(void)
1031 {
1032         GList *cur;
1033
1034         for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1035                 inc_cancel((IncProgressDialog *)cur->data);
1036 }
1037
1038 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1039 {
1040         inc_cancel((IncProgressDialog *)data);
1041 }
1042
1043 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1044                                  gpointer data)
1045 {
1046         IncProgressDialog *dialog = (IncProgressDialog *)data;
1047
1048         if (dialog->queue_list == NULL)
1049                 inc_progress_dialog_destroy(dialog);
1050
1051         return TRUE;
1052 }
1053
1054 static gint inc_spool(void)
1055 {
1056         gchar *mbox, *logname;
1057         gint msgs;
1058
1059         logname = g_get_user_name();
1060         mbox = g_strconcat(prefs_common.spool_path
1061                            ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1062                            G_DIR_SEPARATOR_S, logname, NULL);
1063         msgs = get_spool(folder_get_default_inbox(), mbox);
1064         g_free(mbox);
1065
1066         return msgs;
1067 }
1068
1069 static gint inc_spool_account(PrefsAccount *account)
1070 {
1071         FolderItem *inbox;
1072
1073         if (account->inbox) {
1074                 inbox = folder_find_item_from_path(account->inbox);
1075                 if (!inbox)
1076                         inbox = folder_get_default_inbox();
1077         } else
1078                 inbox = folder_get_default_inbox();
1079
1080         return get_spool(inbox, account->local_mbox);
1081 }
1082
1083 static gint inc_all_spool(void)
1084 {
1085         GList *list = NULL;
1086         gint new_msgs = 0;
1087         gint account_new_msgs = 0;
1088
1089         list = account_get_list();
1090         if (!list) return 0;
1091
1092         for (; list != NULL; list = list->next) {
1093                 PrefsAccount *account = list->data;
1094
1095                 if ((account->protocol == A_LOCAL) &&
1096                     (account->recv_at_getall)) {
1097                         account_new_msgs = inc_spool_account(account);
1098                         if (account_new_msgs > 0)
1099                                 new_msgs += account_new_msgs;
1100                 }
1101         }
1102
1103         return new_msgs;
1104 }
1105
1106 static gint get_spool(FolderItem *dest, const gchar *mbox)
1107 {
1108         gint msgs, size;
1109         gint lockfd;
1110         gchar tmp_mbox[MAXPATHLEN + 1];
1111
1112         g_return_val_if_fail(dest != NULL, -1);
1113         g_return_val_if_fail(mbox != NULL, -1);
1114
1115         if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1116                 debug_print("no messages in local mailbox.\n");
1117                 return 0;
1118         } else if (size < 0)
1119                 return -1;
1120
1121         if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1122                 return -1;
1123
1124         g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1125                    get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1126
1127         if (copy_mbox(mbox, tmp_mbox) < 0) {
1128                 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1129                 return -1;
1130         }
1131
1132         debug_print("Getting new messages from %s into %s...\n",
1133                     mbox, dest->path);
1134
1135         msgs = proc_mbox(dest, tmp_mbox);
1136
1137         unlink(tmp_mbox);
1138         if (msgs >= 0) empty_mbox(mbox);
1139         unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1140
1141         return msgs;
1142 }
1143
1144 void inc_lock(void)
1145 {
1146         inc_lock_count++;
1147 }
1148
1149 void inc_unlock(void)
1150 {
1151         if (inc_lock_count > 0)
1152                 inc_lock_count--;
1153 }
1154
1155 static guint autocheck_timer = 0;
1156 static gpointer autocheck_data = NULL;
1157
1158 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1159 {
1160
1161         gchar *buf;
1162
1163         if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1164             *prefs_common.newmail_notify_cmd))
1165                      return;
1166         if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1167                 buf[1] == 'd' && !strchr(&buf[1], '%'))
1168                 buf = g_strdup_printf(prefs_common.newmail_notify_cmd, 
1169                                       new_msgs);
1170         else
1171                 buf = g_strdup(prefs_common.newmail_notify_cmd);
1172
1173         execute_command_line(buf, TRUE);
1174
1175         g_free(buf);
1176 }
1177  
1178 void inc_autocheck_timer_init(MainWindow *mainwin)
1179 {
1180         autocheck_data = mainwin;
1181         inc_autocheck_timer_set();
1182 }
1183
1184 static void inc_autocheck_timer_set_interval(guint interval)
1185 {
1186         inc_autocheck_timer_remove();
1187         /* last test is to avoid re-enabling auto_check after modifying 
1188            the common preferences */
1189         if (prefs_common.autochk_newmail && autocheck_data
1190             && prefs_common.work_offline == FALSE) {
1191                 autocheck_timer = gtk_timeout_add
1192                         (interval, inc_autocheck_func, autocheck_data);
1193                 debug_print("added timer = %d\n", autocheck_timer);
1194         }
1195 }
1196
1197 void inc_autocheck_timer_set(void)
1198 {
1199         inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1200 }
1201
1202 void inc_autocheck_timer_remove(void)
1203 {
1204         if (autocheck_timer) {
1205                 debug_print("removed timer = %d\n", autocheck_timer);
1206                 gtk_timeout_remove(autocheck_timer);
1207                 autocheck_timer = 0;
1208         }
1209 }
1210
1211 static gint inc_autocheck_func(gpointer data)
1212 {
1213         MainWindow *mainwin = (MainWindow *)data;
1214
1215         if (inc_lock_count) {
1216                 debug_print("autocheck is locked.\n");
1217                 inc_autocheck_timer_set_interval(1000);
1218                 return FALSE;
1219         }
1220
1221         inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);
1222
1223         return FALSE;
1224 }