2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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);
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);
647 folder_item_update_thaw();
649 statusbar_progress_all(0,0,0);
652 g_slist_free(msglist);
656 new_msgs += pop3_session->cur_total_num;
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;
664 pop3_write_uidl_list(pop3_session);
666 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
668 if (inc_dialog->show_dialog)
669 manage_window_focus_in
670 (inc_dialog->dialog->window,
672 inc_put_error(inc_state, pop3_session);
673 if (inc_dialog->show_dialog)
674 manage_window_focus_out
675 (inc_dialog->dialog->window,
677 if (inc_state == INC_NO_SPACE ||
678 inc_state == INC_IO_ERROR)
682 inc_session_destroy(session);
683 inc_dialog->queue_list =
684 g_list_remove(inc_dialog->queue_list, session);
687 #undef SET_PIXMAP_AND_TEXT
690 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
691 "Finished (%d new messages)",
692 new_msgs), new_msgs);
694 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
696 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
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,
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,
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);
717 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
718 inc_progress_dialog_destroy(inc_dialog);
720 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
722 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
731 static IncState inc_pop3_session_do(IncSession *session)
733 Pop3Session *pop3_session = POP3_SESSION(session->session);
734 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
739 debug_print("getting new messages of account %s...\n",
740 pop3_session->ac_prefs->account_name);
742 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
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);
749 server = pop3_session->ac_prefs->recv_server;
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;
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 "
767 _("Con_tinue connecting"),
768 GTK_STOCK_CANCEL, NULL,
769 FALSE, NULL, ALERT_WARNING,
770 G_ALERTALTERNATE) != G_ALERTDEFAULT)
771 return INC_CONNECT_ERROR;
773 port = pop3_session->ac_prefs->set_popport ?
774 pop3_session->ac_prefs->popport : 110;
777 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
778 log_message("%s\n", buf);
780 progress_dialog_set_label(inc_dialog->dialog, buf);
783 session_set_timeout(SESSION(pop3_session),
784 prefs_common.io_timeout_secs * 1000);
786 if (session_connect(SESSION(pop3_session), server, port) < 0) {
787 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
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);
794 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
796 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
798 session->inc_state = INC_CONNECT_ERROR;
800 return INC_CONNECT_ERROR;
803 while (session_is_connected(SESSION(pop3_session)) &&
804 session->inc_state != INC_CANCEL)
805 gtk_main_iteration();
807 if (session->inc_state == INC_SUCCESS) {
808 switch (pop3_session->error_val) {
810 switch (SESSION(pop3_session)->state) {
812 if (pop3_session->state == POP3_READY)
813 session->inc_state = INC_CONNECT_ERROR;
815 session->inc_state = INC_ERROR;
818 session->inc_state = INC_EOF;
820 case SESSION_TIMEOUT:
821 session->inc_state = INC_TIMEOUT;
824 session->inc_state = INC_SUCCESS;
829 session->inc_state = INC_AUTH_FAILED;
832 session->inc_state = INC_IO_ERROR;
835 session->inc_state = INC_SOCKET_ERROR;
838 session->inc_state = INC_LOCKED;
841 session->inc_state = INC_ERROR;
846 session_disconnect(SESSION(pop3_session));
849 return session->inc_state;
852 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
853 IncSession *inc_session)
855 inc_progress_dialog_set_label(inc_dialog, inc_session);
856 inc_progress_dialog_set_progress(inc_dialog, inc_session);
859 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
860 IncSession *inc_session)
862 ProgressDialog *dialog = inc_dialog->dialog;
863 Pop3Session *session;
865 g_return_if_fail(inc_session != NULL);
867 session = POP3_SESSION(inc_session->session);
869 switch (session->state) {
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);
880 case POP3_GETRANGE_STAT:
881 progress_dialog_set_label
882 (dialog, _("Getting the number of new messages (STAT)..."));
884 case POP3_GETRANGE_LAST:
885 progress_dialog_set_label
886 (dialog, _("Getting the number of new messages (LAST)..."));
888 case POP3_GETRANGE_UIDL:
889 progress_dialog_set_label
890 (dialog, _("Getting the number of new messages (UIDL)..."));
892 case POP3_GETSIZE_LIST:
893 progress_dialog_set_label
894 (dialog, _("Getting the size of messages (LIST)..."));
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"),
906 progress_dialog_set_label(dialog, buf);
911 progress_dialog_set_label(dialog, _("Quitting"));
918 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
919 IncSession *inc_session)
921 gchar buf[MSGBUFSIZE];
922 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
923 gchar *total_size_str;
927 if (!pop3_session->new_msg_exist) return;
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);
942 progress_dialog_set_fraction
943 (inc_dialog->dialog,(gfloat)cur_total / (gfloat)total);
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);
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,
960 (pop3_session->cur_total_recv_bytes));
961 progress_dialog_list_set_status(inc_dialog->dialog,
967 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
968 IncSession *inc_session)
974 g_get_current_time(&tv_cur);
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) {
980 tv_result.tv_usec += G_USEC_PER_SEC;
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;
991 static gint inc_recv_data_progressive(Session *session, guint cur_len,
992 guint total_len, gpointer data)
994 IncSession *inc_session = (IncSession *)data;
995 Pop3Session *pop3_session = POP3_SESSION(session);
996 IncProgressDialog *inc_dialog;
999 g_return_val_if_fail(inc_session != NULL, -1);
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;
1006 if (!pop3_session->new_msg_exist) return 0;
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;
1013 inc_dialog = (IncProgressDialog *)inc_session->data;
1014 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1019 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1021 IncSession *inc_session = (IncSession *)data;
1022 IncProgressDialog *inc_dialog;
1024 g_return_val_if_fail(inc_session != NULL, -1);
1026 inc_dialog = (IncProgressDialog *)inc_session->data;
1028 inc_recv_data_progressive(session, 0, 0, inc_session);
1030 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1031 inc_progress_dialog_update(inc_dialog, inc_session);
1037 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1039 IncSession *inc_session = (IncSession *)data;
1040 IncProgressDialog *inc_dialog;
1042 g_return_val_if_fail(inc_session != NULL, -1);
1044 inc_dialog = (IncProgressDialog *)inc_session->data;
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);
1057 inc_recv_data_progressive(session, 0, 0, inc_session);
1060 inc_progress_dialog_update(inc_dialog, inc_session);
1069 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1072 FolderItem *dropfolder;
1073 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1076 g_return_val_if_fail(inc_session != NULL, -1);
1078 if (session->ac_prefs->inbox) {
1079 inbox = folder_find_item_from_identifier
1080 (session->ac_prefs->inbox);
1082 inbox = folder_get_default_inbox();
1084 inbox = folder_get_default_inbox();
1090 /* CLAWS: claws uses a global .processing folder for the filtering. */
1091 dropfolder = folder_get_default_processing();
1093 /* add msg file to drop folder */
1094 if ((msgnum = folder_item_add_msg(
1095 dropfolder, file, NULL, TRUE)) < 0) {
1103 static void inc_put_error(IncState istate, Pop3Session *session)
1105 gchar *log_msg = NULL;
1106 gchar *err_msg = NULL;
1107 gboolean fatal_error = FALSE;
1110 case INC_CONNECT_ERROR:
1111 log_msg = _("Connection failed.");
1112 if (prefs_common.no_recv_err_panel)
1114 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1115 SESSION(session)->server,
1116 SESSION(session)->port);
1119 log_msg = _("Error occurred while processing mail.");
1120 if (prefs_common.no_recv_err_panel)
1122 if (session->error_msg)
1123 err_msg = g_strdup_printf
1124 (_("Error occurred while processing mail:\n%s"),
1125 session->error_msg);
1127 err_msg = g_strdup(log_msg);
1130 log_msg = _("No disk space left.");
1131 err_msg = g_strdup(log_msg);
1135 log_msg = _("Can't write file.");
1136 err_msg = g_strdup(log_msg);
1139 case INC_SOCKET_ERROR:
1140 log_msg = _("Socket error.");
1141 if (prefs_common.no_recv_err_panel)
1143 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1144 SESSION(session)->server,
1145 SESSION(session)->port);
1148 log_msg = _("Connection closed by the remote host.");
1149 if (prefs_common.no_recv_err_panel)
1151 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1152 SESSION(session)->server,
1153 SESSION(session)->port);
1156 log_msg = _("Mailbox is locked.");
1157 if (prefs_common.no_recv_err_panel)
1159 if (session->error_msg)
1160 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1161 session->error_msg);
1163 err_msg = g_strdup(log_msg);
1165 case INC_AUTH_FAILED:
1166 log_msg = _("Authentication failed.");
1167 if (prefs_common.no_recv_err_panel)
1169 if (session->error_msg)
1170 err_msg = g_strdup_printf
1171 (_("Authentication failed:\n%s"), session->error_msg);
1173 err_msg = g_strdup(log_msg);
1176 log_msg = _("Session timed out.");
1177 if (prefs_common.no_recv_err_panel)
1179 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1180 SESSION(session)->server,
1181 SESSION(session)->port);
1189 log_error("%s\n", log_msg);
1191 log_warning("%s\n", log_msg);
1194 alertpanel_error_log(err_msg);
1199 static void inc_cancel(IncProgressDialog *dialog)
1201 IncSession *session;
1203 g_return_if_fail(dialog != NULL);
1205 if (dialog->queue_list == NULL) {
1206 inc_progress_dialog_destroy(dialog);
1210 session = dialog->queue_list->data;
1212 session->inc_state = INC_CANCEL;
1214 log_message(_("Incorporation cancelled\n"));
1217 gboolean inc_is_active(void)
1219 return (inc_dialog_list != NULL);
1222 void inc_cancel_all(void)
1226 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1227 inc_cancel((IncProgressDialog *)cur->data);
1230 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1232 inc_cancel((IncProgressDialog *)data);
1235 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1238 IncProgressDialog *dialog = (IncProgressDialog *)data;
1240 if (dialog->queue_list == NULL)
1241 inc_progress_dialog_destroy(dialog);
1246 static gint inc_spool_account(PrefsAccount *account)
1252 if (account->local_inbox) {
1253 inbox = folder_find_item_from_identifier(account->local_inbox);
1255 inbox = folder_get_default_inbox();
1257 inbox = folder_get_default_inbox();
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);
1265 debug_print("%s: local mailbox not found.\n",
1266 account->local_mbox);
1270 result = get_spool(inbox, mbox);
1273 statusbar_pop_all();
1278 static gint inc_all_spool(void)
1282 gint account_new_msgs = 0;
1284 list = account_get_list();
1285 if (!list) return 0;
1287 for (; list != NULL; list = list->next) {
1288 PrefsAccount *account = list->data;
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;
1301 static gint get_spool(FolderItem *dest, const gchar *mbox)
1305 gchar tmp_mbox[MAXPATHLEN + 1];
1307 g_return_val_if_fail(dest != NULL, -1);
1308 g_return_val_if_fail(mbox != NULL, -1);
1310 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1311 debug_print("%s: no messages in local mailbox.\n", mbox);
1313 } else if (size < 0)
1316 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1319 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1320 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1322 if (copy_mbox(mbox, tmp_mbox) < 0) {
1323 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1327 debug_print("Getting new messages from %s into %s...\n",
1330 msgs = proc_mbox(dest, tmp_mbox, TRUE);
1333 if (msgs >= 0) empty_mbox(mbox);
1334 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1344 void inc_unlock(void)
1346 if (inc_lock_count > 0)
1350 static guint autocheck_timer = 0;
1351 static gpointer autocheck_data = NULL;
1353 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1356 gchar *buf, *numpos, *ret_str;
1357 gssize by_read = 0, by_written = 0;
1359 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1360 *prefs_common.newmail_notify_cmd))
1362 buf = g_strdup(prefs_common.newmail_notify_cmd);
1363 if ((numpos = strstr(buf, "%d")) != NULL) {
1367 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1372 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1374 if (ret_str && by_written) {
1378 debug_print("executing new mail notification command: %s\n", buf);
1379 execute_command_line(buf, TRUE);
1384 void inc_autocheck_timer_init(MainWindow *mainwin)
1386 autocheck_data = mainwin;
1387 inc_autocheck_timer_set();
1390 static void inc_autocheck_timer_set_interval(guint interval)
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);
1403 void inc_autocheck_timer_set(void)
1405 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1408 void inc_autocheck_timer_remove(void)
1410 if (autocheck_timer) {
1411 debug_print("removed timer = %d\n", autocheck_timer);
1412 gtk_timeout_remove(autocheck_timer);
1413 autocheck_timer = 0;
1417 static gint inc_autocheck_func(gpointer data)
1419 MainWindow *mainwin = (MainWindow *)data;
1421 if (inc_lock_count) {
1422 debug_print("autocheck is locked.\n");
1423 inc_autocheck_timer_set_interval(1000);
1427 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1432 gboolean inc_offline_should_override(void)
1434 static time_t overridden_yes = 0;
1435 static time_t overridden_no = 0;
1436 int length = 10; /* minutes */
1437 gint answer = G_ALERTDEFAULT;
1439 if (prefs_common.autochk_newmail)
1440 length = prefs_common.autochk_itv; /* minutes */
1442 if (prefs_common.work_offline) {
1445 if (time(NULL) - overridden_yes < length * 60) /* seconds */
1447 else if (time(NULL) - overridden_no < length * 60) /* seconds */
1450 tmp = g_strdup_printf(
1451 _("You're working offline. Override for %d minutes?"),
1454 answer = alertpanel(_("Offline warning"),
1456 GTK_STOCK_NO, "+" GTK_STOCK_YES, _("On_ly once"));
1458 if (answer == G_ALERTALTERNATE) {
1459 overridden_yes = time(NULL);
1461 } else if (answer == G_ALERTDEFAULT) {
1462 overridden_no = time(NULL);
1465 overridden_yes = (time_t)0;
1466 overridden_no = (time_t)0;