2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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.
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.
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.
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>
38 #include "mainwindow.h"
39 #include "folderview.h"
40 #include "summaryview.h"
41 #include "prefs_common.h"
42 #include "prefs_account.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"
59 #include "filtering.h"
63 static GList *inc_dialog_list = NULL;
65 static guint inc_lock_count = 0;
67 static GdkPixbuf *currentpix;
68 static GdkPixbuf *errorpix;
69 static GdkPixbuf *okpix;
71 #define MSGBUFSIZE 8192
73 static void inc_finished (MainWindow *mainwin,
74 gboolean new_messages);
75 static gint inc_account_mail_real (MainWindow *mainwin,
76 PrefsAccount *account);
78 static IncProgressDialog *inc_progress_dialog_create
80 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
81 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
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);
88 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
89 IncSession *inc_session);
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);
98 static void inc_progress_dialog_update_periodic
99 (IncProgressDialog *inc_dialog,
100 IncSession *inc_session);
102 static gint inc_recv_data_progressive (Session *session,
106 static gint inc_recv_data_finished (Session *session,
109 static gint inc_recv_message (Session *session,
112 static gint inc_drop_message (Pop3Session *session,
115 static void inc_put_error (IncState istate,
116 Pop3Session *session);
118 static void inc_cancel_cb (GtkWidget *widget,
120 static gint inc_dialog_delete_cb (GtkWidget *widget,
124 static gint get_spool (FolderItem *dest,
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);
132 static void inc_notify_cmd (gint new_msgs,
137 * @mainwin: Main window.
138 * @new_messages: TRUE if some messages have been received.
140 * Update the folder view and the summary view after receiving
141 * messages. If @new_messages is FALSE, this function avoids unneeded
144 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
148 if (prefs_common.scan_all_after_inc)
149 folderview_check_new(NULL);
151 if (!new_messages && !prefs_common.scan_all_after_inc) return;
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);
162 void inc_mail(MainWindow *mainwin, gboolean notify)
165 gint account_new_msgs = 0;
167 if (inc_lock_count) return;
169 if (prefs_common.work_offline && !inc_offline_should_override())
173 inc_autocheck_timer_remove();
174 main_window_lock(mainwin);
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();
184 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
185 if (account_new_msgs > 0)
186 new_msgs += account_new_msgs;
189 inc_finished(mainwin, new_msgs > 0);
190 main_window_unlock(mainwin);
191 inc_notify_cmd(new_msgs, notify);
192 inc_autocheck_timer_set();
196 void inc_pop_before_smtp(PrefsAccount *acc)
198 IncProgressDialog *inc_dialog;
202 mainwin = mainwindow_get_mainwindow();
204 session = inc_session_new(acc);
205 if (!session) return;
206 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
208 inc_dialog = inc_progress_dialog_create(FALSE);
209 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
211 /* FIXME: assumes to attach to first main window */
212 inc_dialog->mainwin = mainwin;
213 inc_progress_dialog_set_list(inc_dialog);
216 toolbar_main_set_sensitive(mainwin);
217 main_window_set_menu_sensitive(mainwin);
220 inc_start(inc_dialog);
223 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
225 IncProgressDialog *inc_dialog;
228 switch (account->protocol) {
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)
236 folderview_check_new(FOLDER(account->folder));
240 session = inc_session_new(account);
241 if (!session) return 0;
243 inc_dialog = inc_progress_dialog_create(FALSE);
244 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
246 inc_dialog->mainwin = mainwin;
247 inc_progress_dialog_set_list(inc_dialog);
250 toolbar_main_set_sensitive(mainwin);
251 main_window_set_menu_sensitive(mainwin);
254 return inc_start(inc_dialog);
257 return inc_spool_account(account);
265 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
269 if (inc_lock_count) return 0;
271 if (prefs_common.work_offline && !inc_offline_should_override())
274 inc_autocheck_timer_remove();
275 main_window_lock(mainwin);
277 new_msgs = inc_account_mail_real(mainwin, account);
279 inc_finished(mainwin, new_msgs > 0);
280 main_window_unlock(mainwin);
281 inc_autocheck_timer_set();
286 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
289 GList *list, *queue_list = NULL;
290 IncProgressDialog *inc_dialog;
292 gint account_new_msgs = 0;
294 if (prefs_common.work_offline && !inc_offline_should_override())
297 if (inc_lock_count) return;
299 inc_autocheck_timer_remove();
300 main_window_lock(mainwin);
302 list = account_get_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();
311 /* check local folders */
312 account_new_msgs = inc_all_spool();
313 if (account_new_msgs > 0)
314 new_msgs += account_new_msgs;
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));
325 /* check POP3 accounts */
326 for (list = account_get_list(); list != NULL; list = list->next) {
328 PrefsAccount *account = list->data;
330 if (account->recv_at_getall) {
331 session = inc_session_new(account);
333 queue_list = g_list_append(queue_list, session);
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);
343 toolbar_main_set_sensitive(mainwin);
344 main_window_set_menu_sensitive(mainwin);
345 new_msgs += inc_start(inc_dialog);
348 inc_finished(mainwin, new_msgs > 0);
349 main_window_unlock(mainwin);
350 inc_notify_cmd(new_msgs, notify);
351 inc_autocheck_timer_set();
354 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
356 IncProgressDialog *dialog;
357 ProgressDialog *progress;
359 dialog = g_new0(IncProgressDialog, 1);
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)); */
370 progress_dialog_get_fraction(progress);
372 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_COMPLETE,
374 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_CONTINUE,
376 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_ERROR,
379 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
380 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
382 dialog->show_dialog = TRUE;
383 gtk_widget_show_now(progress->window);
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;
392 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
397 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
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);
405 session->data = inc_dialog;
407 progress_dialog_list_set(inc_dialog->dialog,
409 pop3_session->ac_prefs->account_name,
414 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
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);
422 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
424 g_return_if_fail(inc_dialog != NULL);
426 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
428 if (inc_dialog->mainwin)
429 main_window_progress_off(inc_dialog->mainwin);
430 progress_dialog_destroy(inc_dialog->dialog);
435 static IncSession *inc_session_new(PrefsAccount *account)
439 g_return_val_if_fail(account != NULL, NULL);
441 if (account->protocol != A_POP3)
443 if (!account->recv_server || !account->userid)
446 session = g_new0(IncSession, 1);
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,
456 session_set_recv_data_notify(session->session,
457 inc_recv_data_finished, session);
462 static void inc_session_destroy(IncSession *session)
464 g_return_if_fail(session != NULL);
466 session_destroy(session->session);
470 static gint inc_start(IncProgressDialog *inc_dialog)
474 Pop3Session *pop3_session;
480 FolderItem *processing, *inbox;
482 GSList *msglist, *msglist_element;
483 gboolean cancelled = FALSE;
485 qlist = inc_dialog->queue_list;
486 while (qlist != NULL) {
487 GList *next = qlist->next;
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)
494 g_strdup(pop3_session->ac_prefs->passwd);
495 else if (pop3_session->ac_prefs->tmp_pass)
497 g_strdup(pop3_session->ac_prefs->tmp_pass);
501 if (inc_dialog->show_dialog)
502 manage_window_focus_in
503 (inc_dialog->dialog->window,
506 pass = input_dialog_query_password
507 (pop3_session->ac_prefs->recv_server,
510 if (inc_dialog->show_dialog)
511 manage_window_focus_out
512 (inc_dialog->dialog->window,
516 pop3_session->ac_prefs->tmp_pass =
518 pop3_session->pass = pass;
525 #define SET_PIXMAP_AND_TEXT(pix, str) \
527 progress_dialog_list_set(inc_dialog->dialog, \
528 inc_dialog->cur_row, \
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);
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);
547 inc_progress_dialog_clear(inc_dialog);
548 progress_dialog_scroll_to_row(inc_dialog->dialog,
549 inc_dialog->cur_row);
551 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
553 /* begin POP3 session */
554 inc_state = inc_pop3_session_do(session);
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));
566 msg = g_strdup_printf(_("Done (no new messages)"));
567 SET_PIXMAP_AND_TEXT(okpix, msg);
570 case INC_CONNECT_ERROR:
571 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
573 case INC_AUTH_FAILED:
574 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
577 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
582 case INC_SOCKET_ERROR:
584 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
587 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
590 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
591 if (!inc_dialog->show_dialog)
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);
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);
612 inbox = folder_get_default_inbox();
614 inbox = folder_get_default_inbox();
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);
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);
627 for(msglist_element = msglist; msglist_element != NULL;
628 msglist_element = msglist_element->next) {
630 msginfo = (MsgInfo *) msglist_element->data;
631 filename = folder_item_fetch_msg(processing, msginfo->msgnum);
634 if (pop3_session->ac_prefs->filter_on_recv)
635 statusbar_progress_all(cur++,total, prefs_common.statusbar_update_step);
637 if (!pop3_session->ac_prefs->filter_on_recv ||
638 !procmsg_msginfo_filter(msginfo))
639 folder_item_move_msg(inbox, msginfo);
640 procmsg_msginfo_free(msginfo);
642 folder_item_update_thaw();
644 statusbar_progress_all(0,0,0);
647 g_slist_free(msglist);
651 new_msgs += pop3_session->cur_total_num;
653 if (pop3_session->error_val == PS_AUTHFAIL &&
654 pop3_session->ac_prefs->tmp_pass) {
655 g_free(pop3_session->ac_prefs->tmp_pass);
656 pop3_session->ac_prefs->tmp_pass = NULL;
659 pop3_write_uidl_list(pop3_session);
661 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
663 if (inc_dialog->show_dialog)
664 manage_window_focus_in
665 (inc_dialog->dialog->window,
667 inc_put_error(inc_state, pop3_session);
668 if (inc_dialog->show_dialog)
669 manage_window_focus_out
670 (inc_dialog->dialog->window,
672 if (inc_state == INC_NO_SPACE ||
673 inc_state == INC_IO_ERROR)
677 inc_session_destroy(session);
678 inc_dialog->queue_list =
679 g_list_remove(inc_dialog->queue_list, session);
682 #undef SET_PIXMAP_AND_TEXT
685 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
686 "Finished (%d new messages)",
687 new_msgs), new_msgs);
689 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
691 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
694 if (error_num && !prefs_common.no_recv_err_panel) {
695 if (inc_dialog->show_dialog)
696 manage_window_focus_in(inc_dialog->dialog->window,
698 alertpanel_error_log(_("Some errors occurred while getting mail."));
699 if (inc_dialog->show_dialog)
700 manage_window_focus_out(inc_dialog->dialog->window,
705 while (inc_dialog->queue_list != NULL) {
706 session = inc_dialog->queue_list->data;
707 inc_session_destroy(session);
708 inc_dialog->queue_list =
709 g_list_remove(inc_dialog->queue_list, session);
712 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
713 inc_progress_dialog_destroy(inc_dialog);
715 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
717 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
726 static IncState inc_pop3_session_do(IncSession *session)
728 Pop3Session *pop3_session = POP3_SESSION(session->session);
729 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
734 debug_print("getting new messages of account %s...\n",
735 pop3_session->ac_prefs->account_name);
737 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
739 buf = g_strdup_printf(_("%s: Retrieving new messages"),
740 pop3_session->ac_prefs->recv_server);
741 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
744 server = pop3_session->ac_prefs->recv_server;
746 port = pop3_session->ac_prefs->set_popport ?
747 pop3_session->ac_prefs->popport :
748 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
749 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
750 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
751 SESSION(pop3_session)->nonblocking =
752 pop3_session->ac_prefs->use_nonblocking_ssl;
754 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE) {
755 if (alertpanel_full(_("Insecure connection"),
756 _("This connection is configured to be secured "
757 "using SSL, but SSL is not available in this "
758 "build of Sylpheed-Claws. \n\n"
759 "Do you want to continue connecting to this "
760 "server? The communication would not be "
762 _("Con_tinue connecting"),
763 GTK_STOCK_CANCEL, NULL,
764 FALSE, NULL, ALERT_WARNING,
765 G_ALERTALTERNATE) != G_ALERTDEFAULT)
766 return INC_CONNECT_ERROR;
768 port = pop3_session->ac_prefs->set_popport ?
769 pop3_session->ac_prefs->popport : 110;
772 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
773 log_message("%s\n", buf);
775 progress_dialog_set_label(inc_dialog->dialog, buf);
778 session_set_timeout(SESSION(pop3_session),
779 prefs_common.io_timeout_secs * 1000);
781 if (session_connect(SESSION(pop3_session), server, port) < 0) {
782 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
784 if(!prefs_common.no_recv_err_panel) {
785 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
786 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
787 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
789 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
791 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
793 session->inc_state = INC_CONNECT_ERROR;
795 return INC_CONNECT_ERROR;
798 while (session_is_connected(SESSION(pop3_session)) &&
799 session->inc_state != INC_CANCEL)
800 gtk_main_iteration();
802 if (session->inc_state == INC_SUCCESS) {
803 switch (pop3_session->error_val) {
805 switch (SESSION(pop3_session)->state) {
807 if (pop3_session->state == POP3_READY)
808 session->inc_state = INC_CONNECT_ERROR;
810 session->inc_state = INC_ERROR;
813 session->inc_state = INC_EOF;
815 case SESSION_TIMEOUT:
816 session->inc_state = INC_TIMEOUT;
819 session->inc_state = INC_SUCCESS;
824 session->inc_state = INC_AUTH_FAILED;
827 session->inc_state = INC_IO_ERROR;
830 session->inc_state = INC_SOCKET_ERROR;
833 session->inc_state = INC_LOCKED;
836 session->inc_state = INC_ERROR;
841 session_disconnect(SESSION(pop3_session));
844 return session->inc_state;
847 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
848 IncSession *inc_session)
850 inc_progress_dialog_set_label(inc_dialog, inc_session);
851 inc_progress_dialog_set_progress(inc_dialog, inc_session);
854 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
855 IncSession *inc_session)
857 ProgressDialog *dialog = inc_dialog->dialog;
858 Pop3Session *session;
860 g_return_if_fail(inc_session != NULL);
862 session = POP3_SESSION(inc_session->session);
864 switch (session->state) {
867 case POP3_GETAUTH_USER:
868 case POP3_GETAUTH_PASS:
869 case POP3_GETAUTH_APOP:
870 progress_dialog_set_label(dialog, _("Authenticating..."));
871 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
872 SESSION(session)->server,
873 session->ac_prefs->account_name);
875 case POP3_GETRANGE_STAT:
876 progress_dialog_set_label
877 (dialog, _("Getting the number of new messages (STAT)..."));
879 case POP3_GETRANGE_LAST:
880 progress_dialog_set_label
881 (dialog, _("Getting the number of new messages (LAST)..."));
883 case POP3_GETRANGE_UIDL:
884 progress_dialog_set_label
885 (dialog, _("Getting the number of new messages (UIDL)..."));
887 case POP3_GETSIZE_LIST:
888 progress_dialog_set_label
889 (dialog, _("Getting the size of messages (LIST)..."));
896 if (session->msg[session->cur_msg].recv_time <
897 session->current_time) {
898 gchar buf[MSGBUFSIZE];
899 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
901 progress_dialog_set_label(dialog, buf);
906 progress_dialog_set_label(dialog, _("Quitting"));
913 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
914 IncSession *inc_session)
916 gchar buf[MSGBUFSIZE];
917 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
918 gchar *total_size_str;
922 if (!pop3_session->new_msg_exist) return;
924 cur_total = inc_session->cur_total_bytes;
925 total = pop3_session->total_bytes;
926 if (pop3_session->state == POP3_RETR ||
927 pop3_session->state == POP3_RETR_RECV ||
928 pop3_session->state == POP3_DELETE) {
929 Xstrdup_a(total_size_str, to_human_readable(total), return);
930 g_snprintf(buf, sizeof(buf),
931 _("Retrieving message (%d / %d) (%s / %s)"),
932 pop3_session->cur_msg, pop3_session->count,
933 to_human_readable(cur_total), total_size_str);
934 progress_dialog_set_label(inc_dialog->dialog, buf);
937 progress_dialog_set_fraction
938 (inc_dialog->dialog,(gfloat)cur_total / (gfloat)total);
940 g_snprintf(buf, sizeof(buf), "%d / %d",
941 pop3_session->cur_msg, pop3_session->count);
942 gtk_progress_bar_set_text
943 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), buf);
944 gtk_progress_bar_set_fraction
945 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
946 (gfloat)cur_total / (gfloat)total);
948 if (pop3_session->cur_total_num > 0) {
949 g_snprintf(buf, sizeof(buf),
950 ngettext("Retrieving (%d message (%s) received)",
951 "Retrieving (%d messages (%s) received)",
952 pop3_session->cur_total_num),
953 pop3_session->cur_total_num,
955 (pop3_session->cur_total_recv_bytes));
956 progress_dialog_list_set_status(inc_dialog->dialog,
962 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
963 IncSession *inc_session)
969 g_get_current_time(&tv_cur);
971 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
972 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
973 if (tv_result.tv_usec < 0) {
975 tv_result.tv_usec += G_USEC_PER_SEC;
978 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
979 if (msec > PROGRESS_UPDATE_INTERVAL) {
980 inc_progress_dialog_update(inc_dialog, inc_session);
981 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
982 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
986 static gint inc_recv_data_progressive(Session *session, guint cur_len,
987 guint total_len, gpointer data)
989 IncSession *inc_session = (IncSession *)data;
990 Pop3Session *pop3_session = POP3_SESSION(session);
991 IncProgressDialog *inc_dialog;
994 g_return_val_if_fail(inc_session != NULL, -1);
996 if (pop3_session->state != POP3_RETR &&
997 pop3_session->state != POP3_RETR_RECV &&
998 pop3_session->state != POP3_DELETE &&
999 pop3_session->state != POP3_LOGOUT) return 0;
1001 if (!pop3_session->new_msg_exist) return 0;
1003 cur_total = pop3_session->cur_total_bytes + cur_len;
1004 if (cur_total > pop3_session->total_bytes)
1005 cur_total = pop3_session->total_bytes;
1006 inc_session->cur_total_bytes = cur_total;
1008 inc_dialog = (IncProgressDialog *)inc_session->data;
1009 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1014 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1016 IncSession *inc_session = (IncSession *)data;
1017 IncProgressDialog *inc_dialog;
1019 g_return_val_if_fail(inc_session != NULL, -1);
1021 inc_dialog = (IncProgressDialog *)inc_session->data;
1023 inc_recv_data_progressive(session, 0, 0, inc_session);
1025 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1026 inc_progress_dialog_update(inc_dialog, inc_session);
1032 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1034 IncSession *inc_session = (IncSession *)data;
1035 IncProgressDialog *inc_dialog;
1037 g_return_val_if_fail(inc_session != NULL, -1);
1039 inc_dialog = (IncProgressDialog *)inc_session->data;
1041 switch (POP3_SESSION(session)->state) {
1042 case POP3_GETAUTH_USER:
1043 case POP3_GETAUTH_PASS:
1044 case POP3_GETAUTH_APOP:
1045 case POP3_GETRANGE_STAT:
1046 case POP3_GETRANGE_LAST:
1047 case POP3_GETRANGE_UIDL:
1048 case POP3_GETSIZE_LIST:
1049 inc_progress_dialog_update(inc_dialog, inc_session);
1052 inc_recv_data_progressive(session, 0, 0, inc_session);
1055 inc_progress_dialog_update(inc_dialog, inc_session);
1064 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1067 FolderItem *dropfolder;
1068 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1071 g_return_val_if_fail(inc_session != NULL, -1);
1073 if (session->ac_prefs->inbox) {
1074 inbox = folder_find_item_from_identifier
1075 (session->ac_prefs->inbox);
1077 inbox = folder_get_default_inbox();
1079 inbox = folder_get_default_inbox();
1085 /* CLAWS: claws uses a global .processing folder for the filtering. */
1086 dropfolder = folder_get_default_processing();
1088 /* add msg file to drop folder */
1089 if ((msgnum = folder_item_add_msg(
1090 dropfolder, file, NULL, TRUE)) < 0) {
1098 static void inc_put_error(IncState istate, Pop3Session *session)
1100 gchar *log_msg = NULL;
1101 gchar *err_msg = NULL;
1102 gboolean fatal_error = FALSE;
1105 case INC_CONNECT_ERROR:
1106 log_msg = _("Connection failed.");
1107 if (prefs_common.no_recv_err_panel)
1109 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1110 SESSION(session)->server,
1111 SESSION(session)->port);
1114 log_msg = _("Error occurred while processing mail.");
1115 if (prefs_common.no_recv_err_panel)
1117 if (session->error_msg)
1118 err_msg = g_strdup_printf
1119 (_("Error occurred while processing mail:\n%s"),
1120 session->error_msg);
1122 err_msg = g_strdup(log_msg);
1125 log_msg = _("No disk space left.");
1126 err_msg = g_strdup(log_msg);
1130 log_msg = _("Can't write file.");
1131 err_msg = g_strdup(log_msg);
1134 case INC_SOCKET_ERROR:
1135 log_msg = _("Socket error.");
1136 if (prefs_common.no_recv_err_panel)
1138 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1139 SESSION(session)->server,
1140 SESSION(session)->port);
1143 log_msg = _("Connection closed by the remote host.");
1144 if (prefs_common.no_recv_err_panel)
1146 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1147 SESSION(session)->server,
1148 SESSION(session)->port);
1151 log_msg = _("Mailbox is locked.");
1152 if (prefs_common.no_recv_err_panel)
1154 if (session->error_msg)
1155 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1156 session->error_msg);
1158 err_msg = g_strdup(log_msg);
1160 case INC_AUTH_FAILED:
1161 log_msg = _("Authentication failed.");
1162 if (prefs_common.no_recv_err_panel)
1164 if (session->error_msg)
1165 err_msg = g_strdup_printf
1166 (_("Authentication failed:\n%s"), session->error_msg);
1168 err_msg = g_strdup(log_msg);
1171 log_msg = _("Session timed out.");
1172 if (prefs_common.no_recv_err_panel)
1174 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1175 SESSION(session)->server,
1176 SESSION(session)->port);
1184 log_error("%s\n", log_msg);
1186 log_warning("%s\n", log_msg);
1189 alertpanel_error_log(err_msg);
1194 static void inc_cancel(IncProgressDialog *dialog)
1196 IncSession *session;
1198 g_return_if_fail(dialog != NULL);
1200 if (dialog->queue_list == NULL) {
1201 inc_progress_dialog_destroy(dialog);
1205 session = dialog->queue_list->data;
1207 session->inc_state = INC_CANCEL;
1209 log_message(_("Incorporation cancelled\n"));
1212 gboolean inc_is_active(void)
1214 return (inc_dialog_list != NULL);
1217 void inc_cancel_all(void)
1221 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1222 inc_cancel((IncProgressDialog *)cur->data);
1225 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1227 inc_cancel((IncProgressDialog *)data);
1230 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1233 IncProgressDialog *dialog = (IncProgressDialog *)data;
1235 if (dialog->queue_list == NULL)
1236 inc_progress_dialog_destroy(dialog);
1241 static gint inc_spool_account(PrefsAccount *account)
1247 if (account->inbox) {
1248 inbox = folder_find_item_from_path(account->inbox);
1250 inbox = folder_get_default_inbox();
1252 inbox = folder_get_default_inbox();
1254 if (is_file_exist(account->local_mbox))
1255 mbox = g_strdup(account->local_mbox);
1256 else if (is_dir_exist(account->local_mbox))
1257 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1258 g_get_user_name(), NULL);
1260 debug_print("%s: local mailbox not found.\n",
1261 account->local_mbox);
1265 result = get_spool(inbox, mbox);
1268 statusbar_pop_all();
1273 static gint inc_all_spool(void)
1277 gint account_new_msgs = 0;
1279 list = account_get_list();
1280 if (!list) return 0;
1282 for (; list != NULL; list = list->next) {
1283 PrefsAccount *account = list->data;
1285 if ((account->protocol == A_LOCAL) &&
1286 (account->recv_at_getall)) {
1287 account_new_msgs = inc_spool_account(account);
1288 if (account_new_msgs > 0)
1289 new_msgs += account_new_msgs;
1296 static gint get_spool(FolderItem *dest, const gchar *mbox)
1300 gchar tmp_mbox[MAXPATHLEN + 1];
1302 g_return_val_if_fail(dest != NULL, -1);
1303 g_return_val_if_fail(mbox != NULL, -1);
1305 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1306 debug_print("%s: no messages in local mailbox.\n", mbox);
1308 } else if (size < 0)
1311 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1314 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1315 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1317 if (copy_mbox(mbox, tmp_mbox) < 0) {
1318 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1322 debug_print("Getting new messages from %s into %s...\n",
1325 msgs = proc_mbox(dest, tmp_mbox, TRUE);
1328 if (msgs >= 0) empty_mbox(mbox);
1329 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1339 void inc_unlock(void)
1341 if (inc_lock_count > 0)
1345 static guint autocheck_timer = 0;
1346 static gpointer autocheck_data = NULL;
1348 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1351 gchar *buf, *numpos, *ret_str;
1352 gssize by_read = 0, by_written = 0;
1354 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1355 *prefs_common.newmail_notify_cmd))
1357 buf = g_strdup(prefs_common.newmail_notify_cmd);
1358 if ((numpos = strstr(buf, "%d")) != NULL) {
1362 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1367 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1369 if (ret_str && by_written) {
1373 debug_print("executing new mail notification command: %s\n", buf);
1374 execute_command_line(buf, TRUE);
1379 void inc_autocheck_timer_init(MainWindow *mainwin)
1381 autocheck_data = mainwin;
1382 inc_autocheck_timer_set();
1385 static void inc_autocheck_timer_set_interval(guint interval)
1387 inc_autocheck_timer_remove();
1388 /* last test is to avoid re-enabling auto_check after modifying
1389 the common preferences */
1390 if (prefs_common.autochk_newmail && autocheck_data
1391 && prefs_common.work_offline == FALSE) {
1392 autocheck_timer = gtk_timeout_add
1393 (interval, inc_autocheck_func, autocheck_data);
1394 debug_print("added timer = %d\n", autocheck_timer);
1398 void inc_autocheck_timer_set(void)
1400 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1403 void inc_autocheck_timer_remove(void)
1405 if (autocheck_timer) {
1406 debug_print("removed timer = %d\n", autocheck_timer);
1407 gtk_timeout_remove(autocheck_timer);
1408 autocheck_timer = 0;
1412 static gint inc_autocheck_func(gpointer data)
1414 MainWindow *mainwin = (MainWindow *)data;
1416 if (inc_lock_count) {
1417 debug_print("autocheck is locked.\n");
1418 inc_autocheck_timer_set_interval(1000);
1422 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1427 gboolean inc_offline_should_override(void)
1429 static time_t overridden_yes = 0;
1430 static time_t overridden_no = 0;
1432 gint answer = G_ALERTDEFAULT;
1434 if (prefs_common.autochk_newmail)
1435 length = prefs_common.autochk_itv;
1437 if (prefs_common.work_offline) {
1438 gchar *tmp = g_strdup_printf(
1439 _("You're working offline. Override for %d minutes?"),
1441 if (time(NULL) - overridden_yes < length * 10)
1443 else if (time(NULL) - overridden_no < length * 10)
1446 answer = alertpanel(_("Offline warning"),
1448 GTK_STOCK_YES, GTK_STOCK_NO, _("On_ly once"));
1450 if (answer == G_ALERTDEFAULT) {
1451 overridden_yes = time(NULL);
1453 } else if (answer == G_ALERTALTERNATE) {
1454 overridden_no = time(NULL);
1457 overridden_yes = (time_t)0;
1458 overridden_no = (time_t)0;