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