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