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