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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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>
37 #include <sys/types.h>
45 #include "mainwindow.h"
46 #include "folderview.h"
47 #include "summaryview.h"
48 #include "prefs_common.h"
49 #include "prefs_account.h"
59 #include "statusbar.h"
60 #include "manage_window.h"
61 #include "stock_pixmap.h"
62 #include "progressdialog.h"
63 #include "inputdialog.h"
64 #include "alertpanel.h"
66 #include "filtering.h"
70 static GList *inc_dialog_list = NULL;
72 static guint inc_lock_count = 0;
74 static GdkPixbuf *currentpix;
75 static GdkPixbuf *errorpix;
76 static GdkPixbuf *okpix;
78 #define MSGBUFSIZE 8192
80 static void inc_finished (MainWindow *mainwin,
81 gboolean new_messages);
82 static gint inc_account_mail_real (MainWindow *mainwin,
83 PrefsAccount *account);
85 static IncProgressDialog *inc_progress_dialog_create
87 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
88 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
90 static IncSession *inc_session_new (PrefsAccount *account);
91 static void inc_session_destroy (IncSession *session);
92 static gint inc_start (IncProgressDialog *inc_dialog);
93 static IncState inc_pop3_session_do (IncSession *session);
95 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
96 IncSession *inc_session);
98 static void inc_progress_dialog_set_label
99 (IncProgressDialog *inc_dialog,
100 IncSession *inc_session);
101 static void inc_progress_dialog_set_progress
102 (IncProgressDialog *inc_dialog,
103 IncSession *inc_session);
105 static void inc_progress_dialog_update_periodic
106 (IncProgressDialog *inc_dialog,
107 IncSession *inc_session);
109 static gint inc_recv_data_progressive (Session *session,
113 static gint inc_recv_data_finished (Session *session,
116 static gint inc_recv_message (Session *session,
119 static gint inc_drop_message (Pop3Session *session,
122 static void inc_put_error (IncState istate,
123 Pop3Session *session);
125 static void inc_cancel_cb (GtkWidget *widget,
127 static gint inc_dialog_delete_cb (GtkWidget *widget,
131 static gint get_spool (FolderItem *dest,
134 static gint inc_spool_account(PrefsAccount *account);
135 static gint inc_all_spool(void);
136 static void inc_autocheck_timer_set_interval (guint interval);
137 static gint inc_autocheck_func (gpointer data);
139 static void inc_notify_cmd (gint new_msgs,
144 * @mainwin: Main window.
145 * @new_messages: TRUE if some messages have been received.
147 * Update the folder view and the summary view after receiving
148 * messages. If @new_messages is FALSE, this function avoids unneeded
151 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
155 if (prefs_common.scan_all_after_inc)
156 folderview_check_new(NULL);
158 if (!new_messages && !prefs_common.scan_all_after_inc) return;
160 if (prefs_common.open_inbox_on_inc) {
161 item = cur_account && cur_account->inbox
162 ? folder_find_item_from_identifier(cur_account->inbox)
163 : folder_get_default_inbox();
164 folderview_unselect(mainwin->folderview);
165 folderview_select(mainwin->folderview, item);
169 void inc_mail(MainWindow *mainwin, gboolean notify)
172 gint account_new_msgs = 0;
174 if (inc_lock_count) return;
176 if (prefs_common.work_offline)
177 if (alertpanel(_("Offline warning"),
178 _("You're working offline. Override?"),
179 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
183 inc_autocheck_timer_remove();
184 main_window_lock(mainwin);
186 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
187 /* external incorporating program */
188 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
189 main_window_unlock(mainwin);
190 inc_autocheck_timer_set();
194 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
195 if (account_new_msgs > 0)
196 new_msgs += account_new_msgs;
199 inc_finished(mainwin, new_msgs > 0);
200 main_window_unlock(mainwin);
201 inc_notify_cmd(new_msgs, notify);
202 inc_autocheck_timer_set();
206 void inc_pop_before_smtp(PrefsAccount *acc)
208 IncProgressDialog *inc_dialog;
212 mainwin = mainwindow_get_mainwindow();
214 session = inc_session_new(acc);
215 if (!session) return;
216 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
218 inc_dialog = inc_progress_dialog_create(FALSE);
219 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
221 /* FIXME: assumes to attach to first main window */
222 inc_dialog->mainwin = mainwin;
223 inc_progress_dialog_set_list(inc_dialog);
226 toolbar_main_set_sensitive(mainwin);
227 main_window_set_menu_sensitive(mainwin);
230 inc_start(inc_dialog);
233 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
235 IncProgressDialog *inc_dialog;
238 switch (account->protocol) {
242 * FIXME: it should return foldeview_check_new() value.
243 * TODO: do it when bug [19] is fixed (IMAP folder sets
244 * an incorrect new message count)
246 folderview_check_new(FOLDER(account->folder));
250 session = inc_session_new(account);
251 if (!session) return 0;
253 inc_dialog = inc_progress_dialog_create(FALSE);
254 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
256 inc_dialog->mainwin = mainwin;
257 inc_progress_dialog_set_list(inc_dialog);
260 toolbar_main_set_sensitive(mainwin);
261 main_window_set_menu_sensitive(mainwin);
264 return inc_start(inc_dialog);
267 return inc_spool_account(account);
275 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
279 if (inc_lock_count) return 0;
281 if (prefs_common.work_offline)
282 if (alertpanel(_("Offline warning"),
283 _("You're working offline. Override?"),
284 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
287 inc_autocheck_timer_remove();
288 main_window_lock(mainwin);
290 new_msgs = inc_account_mail_real(mainwin, account);
292 inc_finished(mainwin, new_msgs > 0);
293 main_window_unlock(mainwin);
294 inc_autocheck_timer_set();
299 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
302 GList *list, *queue_list = NULL;
303 IncProgressDialog *inc_dialog;
305 gint account_new_msgs = 0;
307 if (prefs_common.work_offline)
308 if (alertpanel(_("Offline warning"),
309 _("You're working offline. Override?"),
310 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
313 if (inc_lock_count) return;
315 inc_autocheck_timer_remove();
316 main_window_lock(mainwin);
318 list = account_get_list();
320 inc_finished(mainwin, new_msgs > 0);
321 main_window_unlock(mainwin);
322 inc_notify_cmd(new_msgs, notify);
323 inc_autocheck_timer_set();
327 /* check local folders */
328 account_new_msgs = inc_all_spool();
329 if (account_new_msgs > 0)
330 new_msgs += account_new_msgs;
332 /* check IMAP4 / News folders */
333 for (list = account_get_list(); list != NULL; list = list->next) {
334 PrefsAccount *account = list->data;
335 if ((account->protocol == A_IMAP4 ||
336 account->protocol == A_NNTP) && account->recv_at_getall) {
337 new_msgs += folderview_check_new(FOLDER(account->folder));
341 /* check POP3 accounts */
342 for (list = account_get_list(); list != NULL; list = list->next) {
344 PrefsAccount *account = list->data;
346 if (account->recv_at_getall) {
347 session = inc_session_new(account);
349 queue_list = g_list_append(queue_list, session);
354 inc_dialog = inc_progress_dialog_create(autocheck);
355 inc_dialog->queue_list = queue_list;
356 inc_dialog->mainwin = mainwin;
357 inc_progress_dialog_set_list(inc_dialog);
359 toolbar_main_set_sensitive(mainwin);
360 main_window_set_menu_sensitive(mainwin);
361 new_msgs += inc_start(inc_dialog);
364 inc_finished(mainwin, new_msgs > 0);
365 main_window_unlock(mainwin);
366 inc_notify_cmd(new_msgs, notify);
367 inc_autocheck_timer_set();
370 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
372 IncProgressDialog *dialog;
373 ProgressDialog *progress;
375 dialog = g_new0(IncProgressDialog, 1);
377 progress = progress_dialog_create();
378 gtk_window_set_title(GTK_WINDOW(progress->window),
379 _("Retrieving new messages"));
380 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
381 G_CALLBACK(inc_cancel_cb), dialog);
382 g_signal_connect(G_OBJECT(progress->window), "delete_event",
383 G_CALLBACK(inc_dialog_delete_cb), dialog);
384 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
386 progress_dialog_get_fraction(progress);
388 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_COMPLETE,
390 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_CONTINUE,
392 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_ERROR,
395 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
396 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
398 dialog->show_dialog = TRUE;
399 gtk_widget_show_now(progress->window);
402 dialog->dialog = progress;
403 gettimeofday(&dialog->progress_tv, NULL);
404 gettimeofday(&dialog->folder_tv, NULL);
405 dialog->queue_list = NULL;
408 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
413 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
417 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
418 IncSession *session = list->data;
419 Pop3Session *pop3_session = POP3_SESSION(session->session);
421 session->data = inc_dialog;
423 progress_dialog_list_set(inc_dialog->dialog,
425 pop3_session->ac_prefs->account_name,
430 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
432 progress_dialog_get_fraction(inc_dialog->dialog);
433 progress_dialog_set_label(inc_dialog->dialog, "");
434 if (inc_dialog->mainwin)
435 main_window_progress_off(inc_dialog->mainwin);
438 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
440 g_return_if_fail(inc_dialog != NULL);
442 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
444 if (inc_dialog->mainwin)
445 main_window_progress_off(inc_dialog->mainwin);
446 progress_dialog_destroy(inc_dialog->dialog);
451 static IncSession *inc_session_new(PrefsAccount *account)
455 g_return_val_if_fail(account != NULL, NULL);
457 if (account->protocol != A_POP3)
459 if (!account->recv_server || !account->userid)
462 session = g_new0(IncSession, 1);
464 session->session = pop3_session_new(account);
465 session->session->data = session;
466 POP3_SESSION(session->session)->drop_message = inc_drop_message;
467 session_set_recv_message_notify(session->session,
468 inc_recv_message, session);
469 session_set_recv_data_progressive_notify(session->session,
470 inc_recv_data_progressive,
472 session_set_recv_data_notify(session->session,
473 inc_recv_data_finished, session);
478 static void inc_session_destroy(IncSession *session)
480 g_return_if_fail(session != NULL);
482 session_destroy(session->session);
486 static gint inc_start(IncProgressDialog *inc_dialog)
490 Pop3Session *pop3_session;
496 FolderItem *processing, *inbox;
498 GSList *msglist, *msglist_element;
499 gboolean cancelled = FALSE;
501 qlist = inc_dialog->queue_list;
502 while (qlist != NULL) {
503 GList *next = qlist->next;
505 session = qlist->data;
506 pop3_session = POP3_SESSION(session->session);
507 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
508 if (pop3_session->ac_prefs->passwd)
510 g_strdup(pop3_session->ac_prefs->passwd);
511 else if (pop3_session->ac_prefs->tmp_pass)
513 g_strdup(pop3_session->ac_prefs->tmp_pass);
517 if (inc_dialog->show_dialog)
518 manage_window_focus_in
519 (inc_dialog->dialog->window,
522 pass = input_dialog_query_password
523 (pop3_session->ac_prefs->recv_server,
526 if (inc_dialog->show_dialog)
527 manage_window_focus_out
528 (inc_dialog->dialog->window,
532 pop3_session->ac_prefs->tmp_pass =
534 pop3_session->pass = pass;
541 #define SET_PIXMAP_AND_TEXT(pix, str) \
543 progress_dialog_list_set(inc_dialog->dialog, \
544 inc_dialog->cur_row, \
550 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
551 int cur = 0, total = 0;
552 session = inc_dialog->queue_list->data;
553 pop3_session = POP3_SESSION(session->session);
555 if (pop3_session->pass == NULL) {
556 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
557 inc_session_destroy(session);
558 inc_dialog->queue_list =
559 g_list_remove(inc_dialog->queue_list, session);
563 inc_progress_dialog_clear(inc_dialog);
564 progress_dialog_scroll_to_row(inc_dialog->dialog,
565 inc_dialog->cur_row);
567 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
569 /* begin POP3 session */
570 inc_state = inc_pop3_session_do(session);
574 if (pop3_session->cur_total_num > 0)
575 msg = g_strdup_printf(
576 ngettext("Done (%d message (%s) received)",
577 "Done (%d messages (%s) received)",
578 pop3_session->cur_total_num),
579 pop3_session->cur_total_num,
580 to_human_readable(pop3_session->cur_total_recv_bytes));
582 msg = g_strdup_printf(_("Done (no new messages)"));
583 SET_PIXMAP_AND_TEXT(okpix, msg);
586 case INC_CONNECT_ERROR:
587 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
589 case INC_AUTH_FAILED:
590 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
593 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
598 case INC_SOCKET_ERROR:
600 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
603 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
606 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
607 if (!inc_dialog->show_dialog)
614 if (pop3_session->error_val == PS_AUTHFAIL) {
615 if(!prefs_common.no_recv_err_panel) {
616 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
617 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
618 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
622 /* CLAWS: perform filtering actions on dropped message */
623 /* CLAWS: get default inbox (perhaps per account) */
624 if (pop3_session->ac_prefs->inbox) {
625 /* CLAWS: get destination folder / mailbox */
626 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
628 inbox = folder_get_default_inbox();
630 inbox = folder_get_default_inbox();
632 /* get list of messages in processing */
633 processing = folder_get_default_processing();
634 folder_item_scan(processing);
635 msglist = folder_item_get_msg_list(processing);
637 /* process messages */
638 folder_item_update_freeze();
639 if (pop3_session->ac_prefs->filter_on_recv)
640 statusbar_print_all(_("Filtering messages...\n"));
641 total = g_slist_length(msglist);
643 for(msglist_element = msglist; msglist_element != NULL;
644 msglist_element = msglist_element->next) {
646 msginfo = (MsgInfo *) msglist_element->data;
647 filename = folder_item_fetch_msg(processing, msginfo->msgnum);
650 if (pop3_session->ac_prefs->filter_on_recv)
651 statusbar_progress_all(cur++,total, 10);
653 if (!pop3_session->ac_prefs->filter_on_recv ||
654 !procmsg_msginfo_filter(msginfo))
655 folder_item_move_msg(inbox, msginfo);
656 procmsg_msginfo_free(msginfo);
658 folder_item_update_thaw();
660 statusbar_progress_all(0,0,0);
663 g_slist_free(msglist);
667 new_msgs += pop3_session->cur_total_num;
669 if (pop3_session->error_val == PS_AUTHFAIL &&
670 pop3_session->ac_prefs->tmp_pass) {
671 g_free(pop3_session->ac_prefs->tmp_pass);
672 pop3_session->ac_prefs->tmp_pass = NULL;
675 pop3_write_uidl_list(pop3_session);
677 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
679 if (inc_dialog->show_dialog)
680 manage_window_focus_in
681 (inc_dialog->dialog->window,
683 inc_put_error(inc_state, pop3_session);
684 if (inc_dialog->show_dialog)
685 manage_window_focus_out
686 (inc_dialog->dialog->window,
688 if (inc_state == INC_NO_SPACE ||
689 inc_state == INC_IO_ERROR)
693 inc_session_destroy(session);
694 inc_dialog->queue_list =
695 g_list_remove(inc_dialog->queue_list, session);
698 #undef SET_PIXMAP_AND_TEXT
701 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
702 "Finished (%d new messages)",
703 new_msgs), new_msgs);
705 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
707 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
710 if (error_num && !prefs_common.no_recv_err_panel) {
711 if (inc_dialog->show_dialog)
712 manage_window_focus_in(inc_dialog->dialog->window,
714 alertpanel_error_log(_("Some errors occurred while getting mail."));
715 if (inc_dialog->show_dialog)
716 manage_window_focus_out(inc_dialog->dialog->window,
721 while (inc_dialog->queue_list != NULL) {
722 session = inc_dialog->queue_list->data;
723 inc_session_destroy(session);
724 inc_dialog->queue_list =
725 g_list_remove(inc_dialog->queue_list, session);
728 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
729 inc_progress_dialog_destroy(inc_dialog);
731 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
733 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
742 static IncState inc_pop3_session_do(IncSession *session)
744 Pop3Session *pop3_session = POP3_SESSION(session->session);
745 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
750 debug_print("getting new messages of account %s...\n",
751 pop3_session->ac_prefs->account_name);
753 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
755 buf = g_strdup_printf(_("%s: Retrieving new messages"),
756 pop3_session->ac_prefs->recv_server);
757 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
760 server = pop3_session->ac_prefs->recv_server;
762 port = pop3_session->ac_prefs->set_popport ?
763 pop3_session->ac_prefs->popport :
764 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
765 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
766 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
767 SESSION(pop3_session)->nonblocking =
768 pop3_session->ac_prefs->use_nonblocking_ssl;
770 port = pop3_session->ac_prefs->set_popport ?
771 pop3_session->ac_prefs->popport : 110;
774 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
775 log_message("%s\n", buf);
777 progress_dialog_set_label(inc_dialog->dialog, buf);
780 session_set_timeout(SESSION(pop3_session),
781 prefs_common.io_timeout_secs * 1000);
783 if (session_connect(SESSION(pop3_session), server, port) < 0) {
784 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
786 if(!prefs_common.no_recv_err_panel) {
787 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
788 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
789 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
791 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
793 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
795 session->inc_state = INC_CONNECT_ERROR;
797 return INC_CONNECT_ERROR;
800 while (session_is_connected(SESSION(pop3_session)) &&
801 session->inc_state != INC_CANCEL)
802 gtk_main_iteration();
804 if (session->inc_state == INC_SUCCESS) {
805 switch (pop3_session->error_val) {
807 switch (SESSION(pop3_session)->state) {
809 if (pop3_session->state == POP3_READY)
810 session->inc_state = INC_CONNECT_ERROR;
812 session->inc_state = INC_ERROR;
815 session->inc_state = INC_EOF;
817 case SESSION_TIMEOUT:
818 session->inc_state = INC_TIMEOUT;
821 session->inc_state = INC_SUCCESS;
826 session->inc_state = INC_AUTH_FAILED;
829 session->inc_state = INC_IO_ERROR;
832 session->inc_state = INC_SOCKET_ERROR;
835 session->inc_state = INC_LOCKED;
838 session->inc_state = INC_ERROR;
843 session_disconnect(SESSION(pop3_session));
846 return session->inc_state;
849 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
850 IncSession *inc_session)
852 inc_progress_dialog_set_label(inc_dialog, inc_session);
853 inc_progress_dialog_set_progress(inc_dialog, inc_session);
856 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
857 IncSession *inc_session)
859 ProgressDialog *dialog = inc_dialog->dialog;
860 Pop3Session *session;
862 g_return_if_fail(inc_session != NULL);
864 session = POP3_SESSION(inc_session->session);
866 switch (session->state) {
869 case POP3_GETAUTH_USER:
870 case POP3_GETAUTH_PASS:
871 case POP3_GETAUTH_APOP:
872 progress_dialog_set_label(dialog, _("Authenticating..."));
873 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
874 SESSION(session)->server,
875 session->ac_prefs->account_name);
877 case POP3_GETRANGE_STAT:
878 progress_dialog_set_label
879 (dialog, _("Getting the number of new messages (STAT)..."));
881 case POP3_GETRANGE_LAST:
882 progress_dialog_set_label
883 (dialog, _("Getting the number of new messages (LAST)..."));
885 case POP3_GETRANGE_UIDL:
886 progress_dialog_set_label
887 (dialog, _("Getting the number of new messages (UIDL)..."));
889 case POP3_GETSIZE_LIST:
890 progress_dialog_set_label
891 (dialog, _("Getting the size of messages (LIST)..."));
898 if (session->msg[session->cur_msg].recv_time <
899 session->current_time) {
900 gchar buf[MSGBUFSIZE];
901 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
903 progress_dialog_set_label(dialog, buf);
908 progress_dialog_set_label(dialog, _("Quitting"));
915 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
916 IncSession *inc_session)
918 gchar buf[MSGBUFSIZE];
919 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
920 gchar *total_size_str;
924 if (!pop3_session->new_msg_exist) return;
926 cur_total = inc_session->cur_total_bytes;
927 total = pop3_session->total_bytes;
928 if (pop3_session->state == POP3_RETR ||
929 pop3_session->state == POP3_RETR_RECV ||
930 pop3_session->state == POP3_DELETE) {
931 Xstrdup_a(total_size_str, to_human_readable(total), return);
932 g_snprintf(buf, sizeof(buf),
933 _("Retrieving message (%d / %d) (%s / %s)"),
934 pop3_session->cur_msg, pop3_session->count,
935 to_human_readable(cur_total), total_size_str);
936 progress_dialog_set_label(inc_dialog->dialog, buf);
939 progress_dialog_set_fraction
940 (inc_dialog->dialog,(gfloat)cur_total / (gfloat)total);
942 g_snprintf(buf, sizeof(buf), "%d / %d",
943 pop3_session->cur_msg, pop3_session->count);
944 gtk_progress_bar_set_text
945 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), buf);
946 gtk_progress_bar_set_fraction
947 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
948 (gfloat)cur_total / (gfloat)total);
950 if (pop3_session->cur_total_num > 0) {
951 g_snprintf(buf, sizeof(buf),
952 ngettext("Retrieving (%d message (%s) received)",
953 "Retrieving (%d messages (%s) received)",
954 pop3_session->cur_total_num),
955 pop3_session->cur_total_num,
957 (pop3_session->cur_total_recv_bytes));
958 progress_dialog_list_set_status(inc_dialog->dialog,
964 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
965 IncSession *inc_session)
967 struct timeval tv_cur;
968 struct timeval tv_result;
971 gettimeofday(&tv_cur, NULL);
973 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
974 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
975 if (tv_result.tv_usec < 0) {
977 tv_result.tv_usec += 1000000;
980 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
981 if (msec > PROGRESS_UPDATE_INTERVAL) {
982 inc_progress_dialog_update(inc_dialog, inc_session);
983 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
984 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
988 static gint inc_recv_data_progressive(Session *session, guint cur_len,
989 guint total_len, gpointer data)
991 IncSession *inc_session = (IncSession *)data;
992 Pop3Session *pop3_session = POP3_SESSION(session);
993 IncProgressDialog *inc_dialog;
996 g_return_val_if_fail(inc_session != NULL, -1);
998 if (pop3_session->state != POP3_RETR &&
999 pop3_session->state != POP3_RETR_RECV &&
1000 pop3_session->state != POP3_DELETE &&
1001 pop3_session->state != POP3_LOGOUT) return 0;
1003 if (!pop3_session->new_msg_exist) return 0;
1005 cur_total = pop3_session->cur_total_bytes + cur_len;
1006 if (cur_total > pop3_session->total_bytes)
1007 cur_total = pop3_session->total_bytes;
1008 inc_session->cur_total_bytes = cur_total;
1010 inc_dialog = (IncProgressDialog *)inc_session->data;
1011 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1016 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1018 IncSession *inc_session = (IncSession *)data;
1019 IncProgressDialog *inc_dialog;
1021 g_return_val_if_fail(inc_session != NULL, -1);
1023 inc_dialog = (IncProgressDialog *)inc_session->data;
1025 inc_recv_data_progressive(session, 0, 0, inc_session);
1027 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1028 inc_progress_dialog_update(inc_dialog, inc_session);
1034 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1036 IncSession *inc_session = (IncSession *)data;
1037 IncProgressDialog *inc_dialog;
1039 g_return_val_if_fail(inc_session != NULL, -1);
1041 inc_dialog = (IncProgressDialog *)inc_session->data;
1043 switch (POP3_SESSION(session)->state) {
1044 case POP3_GETAUTH_USER:
1045 case POP3_GETAUTH_PASS:
1046 case POP3_GETAUTH_APOP:
1047 case POP3_GETRANGE_STAT:
1048 case POP3_GETRANGE_LAST:
1049 case POP3_GETRANGE_UIDL:
1050 case POP3_GETSIZE_LIST:
1051 inc_progress_dialog_update(inc_dialog, inc_session);
1054 inc_recv_data_progressive(session, 0, 0, inc_session);
1057 inc_progress_dialog_update(inc_dialog, inc_session);
1066 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1069 FolderItem *dropfolder;
1070 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1073 g_return_val_if_fail(inc_session != NULL, -1);
1075 if (session->ac_prefs->inbox) {
1076 inbox = folder_find_item_from_identifier
1077 (session->ac_prefs->inbox);
1079 inbox = folder_get_default_inbox();
1081 inbox = folder_get_default_inbox();
1087 /* CLAWS: claws uses a global .processing folder for the filtering. */
1088 dropfolder = folder_get_default_processing();
1090 /* add msg file to drop folder */
1091 if ((msgnum = folder_item_add_msg(
1092 dropfolder, file, NULL, TRUE)) < 0) {
1100 static void inc_put_error(IncState istate, Pop3Session *session)
1102 gchar *log_msg = NULL;
1103 gchar *err_msg = NULL;
1104 gboolean fatal_error = FALSE;
1107 case INC_CONNECT_ERROR:
1108 log_msg = _("Connection failed.");
1109 if (prefs_common.no_recv_err_panel)
1111 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1112 SESSION(session)->server,
1113 SESSION(session)->port);
1116 log_msg = _("Error occurred while processing mail.");
1117 if (prefs_common.no_recv_err_panel)
1119 if (session->error_msg)
1120 err_msg = g_strdup_printf
1121 (_("Error occurred while processing mail:\n%s"),
1122 session->error_msg);
1124 err_msg = g_strdup(log_msg);
1127 log_msg = _("No disk space left.");
1128 err_msg = g_strdup(log_msg);
1132 log_msg = _("Can't write file.");
1133 err_msg = g_strdup(log_msg);
1136 case INC_SOCKET_ERROR:
1137 log_msg = _("Socket error.");
1138 if (prefs_common.no_recv_err_panel)
1140 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1141 SESSION(session)->server,
1142 SESSION(session)->port);
1145 log_msg = _("Connection closed by the remote host.");
1146 if (prefs_common.no_recv_err_panel)
1148 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1149 SESSION(session)->server,
1150 SESSION(session)->port);
1153 log_msg = _("Mailbox is locked.");
1154 if (prefs_common.no_recv_err_panel)
1156 if (session->error_msg)
1157 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1158 session->error_msg);
1160 err_msg = g_strdup(log_msg);
1162 case INC_AUTH_FAILED:
1163 log_msg = _("Authentication failed.");
1164 if (prefs_common.no_recv_err_panel)
1166 if (session->error_msg)
1167 err_msg = g_strdup_printf
1168 (_("Authentication failed:\n%s"), session->error_msg);
1170 err_msg = g_strdup(log_msg);
1173 log_msg = _("Session timed out.");
1174 if (prefs_common.no_recv_err_panel)
1176 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1177 SESSION(session)->server,
1178 SESSION(session)->port);
1186 log_error("%s\n", log_msg);
1188 log_warning("%s\n", log_msg);
1191 alertpanel_error_log(err_msg);
1196 static void inc_cancel(IncProgressDialog *dialog)
1198 IncSession *session;
1200 g_return_if_fail(dialog != NULL);
1202 if (dialog->queue_list == NULL) {
1203 inc_progress_dialog_destroy(dialog);
1207 session = dialog->queue_list->data;
1209 session->inc_state = INC_CANCEL;
1211 log_message(_("Incorporation cancelled\n"));
1214 gboolean inc_is_active(void)
1216 return (inc_dialog_list != NULL);
1219 void inc_cancel_all(void)
1223 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1224 inc_cancel((IncProgressDialog *)cur->data);
1227 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1229 inc_cancel((IncProgressDialog *)data);
1232 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1235 IncProgressDialog *dialog = (IncProgressDialog *)data;
1237 if (dialog->queue_list == NULL)
1238 inc_progress_dialog_destroy(dialog);
1243 static gint inc_spool_account(PrefsAccount *account)
1249 if (account->inbox) {
1250 inbox = folder_find_item_from_path(account->inbox);
1252 inbox = folder_get_default_inbox();
1254 inbox = folder_get_default_inbox();
1256 if (is_file_exist(account->local_mbox))
1257 mbox = g_strdup(account->local_mbox);
1258 else if (is_dir_exist(account->local_mbox))
1259 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1260 g_get_user_name(), NULL);
1262 debug_print("%s: local mailbox not found.\n",
1263 account->local_mbox);
1267 result = get_spool(inbox, mbox);
1270 statusbar_pop_all();
1275 static gint inc_all_spool(void)
1279 gint account_new_msgs = 0;
1281 list = account_get_list();
1282 if (!list) return 0;
1284 for (; list != NULL; list = list->next) {
1285 PrefsAccount *account = list->data;
1287 if ((account->protocol == A_LOCAL) &&
1288 (account->recv_at_getall)) {
1289 account_new_msgs = inc_spool_account(account);
1290 if (account_new_msgs > 0)
1291 new_msgs += account_new_msgs;
1298 static gint get_spool(FolderItem *dest, const gchar *mbox)
1302 gchar tmp_mbox[MAXPATHLEN + 1];
1304 g_return_val_if_fail(dest != NULL, -1);
1305 g_return_val_if_fail(mbox != NULL, -1);
1307 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1308 debug_print("%s: no messages in local mailbox.\n", mbox);
1310 } else if (size < 0)
1313 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1316 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1317 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1319 if (copy_mbox(mbox, tmp_mbox) < 0) {
1320 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1324 debug_print("Getting new messages from %s into %s...\n",
1327 msgs = proc_mbox(dest, tmp_mbox, TRUE);
1330 if (msgs >= 0) empty_mbox(mbox);
1331 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1341 void inc_unlock(void)
1343 if (inc_lock_count > 0)
1347 static guint autocheck_timer = 0;
1348 static gpointer autocheck_data = NULL;
1350 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1353 gchar *buf, *numpos, *ret_str;
1354 gssize by_read = 0, by_written = 0;
1356 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1357 *prefs_common.newmail_notify_cmd))
1359 buf = g_strdup(prefs_common.newmail_notify_cmd);
1360 if ((numpos = strstr(buf, "%d")) != NULL) {
1364 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1369 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1371 if (ret_str && by_written) {
1375 debug_print("executing new mail notification command: %s\n", buf);
1376 execute_command_line(buf, TRUE);
1381 void inc_autocheck_timer_init(MainWindow *mainwin)
1383 autocheck_data = mainwin;
1384 inc_autocheck_timer_set();
1387 static void inc_autocheck_timer_set_interval(guint interval)
1389 inc_autocheck_timer_remove();
1390 /* last test is to avoid re-enabling auto_check after modifying
1391 the common preferences */
1392 if (prefs_common.autochk_newmail && autocheck_data
1393 && prefs_common.work_offline == FALSE) {
1394 autocheck_timer = gtk_timeout_add
1395 (interval, inc_autocheck_func, autocheck_data);
1396 debug_print("added timer = %d\n", autocheck_timer);
1400 void inc_autocheck_timer_set(void)
1402 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1405 void inc_autocheck_timer_remove(void)
1407 if (autocheck_timer) {
1408 debug_print("removed timer = %d\n", autocheck_timer);
1409 gtk_timeout_remove(autocheck_timer);
1410 autocheck_timer = 0;
1414 static gint inc_autocheck_func(gpointer data)
1416 MainWindow *mainwin = (MainWindow *)data;
1418 if (inc_lock_count) {
1419 debug_print("autocheck is locked.\n");
1420 inc_autocheck_timer_set_interval(1000);
1424 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);