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 <gtk/gtkmain.h>
28 #include <gtk/gtkwindow.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtkprogressbar.h>
36 #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 GdkPixmap *currentxpm;
75 static GdkBitmap *currentxpmmask;
76 static GdkPixmap *errorxpm;
77 static GdkBitmap *errorxpmmask;
78 static GdkPixmap *okxpm;
79 static GdkBitmap *okxpmmask;
81 #define MSGBUFSIZE 8192
83 static void inc_finished (MainWindow *mainwin,
84 gboolean new_messages);
85 static gint inc_account_mail_real (MainWindow *mainwin,
86 PrefsAccount *account);
88 static IncProgressDialog *inc_progress_dialog_create
90 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
91 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
93 static IncSession *inc_session_new (PrefsAccount *account);
94 static void inc_session_destroy (IncSession *session);
95 static gint inc_start (IncProgressDialog *inc_dialog);
96 static IncState inc_pop3_session_do (IncSession *session);
98 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
99 IncSession *inc_session);
101 static void inc_progress_dialog_set_label
102 (IncProgressDialog *inc_dialog,
103 IncSession *inc_session);
104 static void inc_progress_dialog_set_progress
105 (IncProgressDialog *inc_dialog,
106 IncSession *inc_session);
108 static void inc_progress_dialog_update_periodic
109 (IncProgressDialog *inc_dialog,
110 IncSession *inc_session);
112 static gint inc_recv_data_progressive (Session *session,
116 static gint inc_recv_data_finished (Session *session,
119 static gint inc_recv_message (Session *session,
122 static gint inc_drop_message (Pop3Session *session,
125 static void inc_put_error (IncState istate,
128 static void inc_cancel_cb (GtkWidget *widget,
130 static gint inc_dialog_delete_cb (GtkWidget *widget,
134 static gint get_spool (FolderItem *dest,
137 static gint inc_spool_account(PrefsAccount *account);
138 static gint inc_all_spool(void);
139 static void inc_autocheck_timer_set_interval (guint interval);
140 static gint inc_autocheck_func (gpointer data);
142 static void inc_notify_cmd (gint new_msgs,
147 * @mainwin: Main window.
148 * @new_messages: TRUE if some messages have been received.
150 * Update the folder view and the summary view after receiving
151 * messages. If @new_messages is FALSE, this function avoids unneeded
154 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
158 if (prefs_common.scan_all_after_inc)
159 folderview_check_new(NULL);
161 if (!new_messages && !prefs_common.scan_all_after_inc) return;
163 if (prefs_common.open_inbox_on_inc) {
164 item = cur_account && cur_account->inbox
165 ? folder_find_item_from_identifier(cur_account->inbox)
166 : folder_get_default_inbox();
167 folderview_unselect(mainwin->folderview);
168 folderview_select(mainwin->folderview, item);
172 void inc_mail(MainWindow *mainwin, gboolean notify)
175 gint account_new_msgs = 0;
177 if (inc_lock_count) return;
179 if (prefs_common.work_offline)
180 if (alertpanel(_("Offline warning"),
181 _("You're working offline. Override?"),
182 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
186 inc_autocheck_timer_remove();
187 main_window_lock(mainwin);
189 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
190 /* external incorporating program */
191 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
192 main_window_unlock(mainwin);
193 inc_autocheck_timer_set();
197 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
198 if (account_new_msgs > 0)
199 new_msgs += account_new_msgs;
202 inc_finished(mainwin, new_msgs > 0);
203 main_window_unlock(mainwin);
204 inc_notify_cmd(new_msgs, notify);
205 inc_autocheck_timer_set();
209 void inc_pop_before_smtp(PrefsAccount *acc)
211 IncProgressDialog *inc_dialog;
215 mainwin = mainwindow_get_mainwindow();
217 session = inc_session_new(acc);
218 if (!session) return;
219 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
221 inc_dialog = inc_progress_dialog_create(FALSE);
222 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
224 /* FIXME: assumes to attach to first main window */
225 inc_dialog->mainwin = mainwin;
226 inc_progress_dialog_set_list(inc_dialog);
229 toolbar_main_set_sensitive(mainwin);
230 main_window_set_menu_sensitive(mainwin);
233 inc_start(inc_dialog);
236 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
238 IncProgressDialog *inc_dialog;
241 switch (account->protocol) {
245 * FIXME: it should return foldeview_check_new() value.
246 * TODO: do it when bug [19] is fixed (IMAP folder sets
247 * an incorrect new message count)
249 folderview_check_new(FOLDER(account->folder));
253 session = inc_session_new(account);
254 if (!session) return 0;
256 inc_dialog = inc_progress_dialog_create(FALSE);
257 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
259 inc_dialog->mainwin = mainwin;
260 inc_progress_dialog_set_list(inc_dialog);
263 toolbar_main_set_sensitive(mainwin);
264 main_window_set_menu_sensitive(mainwin);
267 return inc_start(inc_dialog);
270 return inc_spool_account(account);
278 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
282 if (inc_lock_count) return 0;
284 if (prefs_common.work_offline)
285 if (alertpanel(_("Offline warning"),
286 _("You're working offline. Override?"),
287 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
290 inc_autocheck_timer_remove();
291 main_window_lock(mainwin);
293 new_msgs = inc_account_mail_real(mainwin, account);
295 inc_finished(mainwin, new_msgs > 0);
296 main_window_unlock(mainwin);
297 inc_autocheck_timer_set();
302 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
305 GList *list, *queue_list = NULL;
306 IncProgressDialog *inc_dialog;
308 gint account_new_msgs = 0;
310 if (prefs_common.work_offline)
311 if (alertpanel(_("Offline warning"),
312 _("You're working offline. Override?"),
313 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
316 if (inc_lock_count) return;
318 inc_autocheck_timer_remove();
319 main_window_lock(mainwin);
321 list = account_get_list();
323 inc_finished(mainwin, new_msgs > 0);
324 main_window_unlock(mainwin);
325 inc_notify_cmd(new_msgs, notify);
326 inc_autocheck_timer_set();
330 /* check local folders */
331 account_new_msgs = inc_all_spool();
332 if (account_new_msgs > 0)
333 new_msgs += account_new_msgs;
335 /* check IMAP4 / News folders */
336 for (list = account_get_list(); list != NULL; list = list->next) {
337 PrefsAccount *account = list->data;
338 if ((account->protocol == A_IMAP4 ||
339 account->protocol == A_NNTP) && account->recv_at_getall) {
340 new_msgs += folderview_check_new(FOLDER(account->folder));
344 /* check POP3 accounts */
345 for (list = account_get_list(); list != NULL; list = list->next) {
347 PrefsAccount *account = list->data;
349 if (account->recv_at_getall) {
350 session = inc_session_new(account);
352 queue_list = g_list_append(queue_list, session);
357 inc_dialog = inc_progress_dialog_create(autocheck);
358 inc_dialog->queue_list = queue_list;
359 inc_dialog->mainwin = mainwin;
360 inc_progress_dialog_set_list(inc_dialog);
362 toolbar_main_set_sensitive(mainwin);
363 main_window_set_menu_sensitive(mainwin);
364 new_msgs += inc_start(inc_dialog);
367 inc_finished(mainwin, new_msgs > 0);
368 main_window_unlock(mainwin);
369 inc_notify_cmd(new_msgs, notify);
370 inc_autocheck_timer_set();
373 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
375 IncProgressDialog *dialog;
376 ProgressDialog *progress;
378 dialog = g_new0(IncProgressDialog, 1);
380 progress = progress_dialog_create();
381 gtk_window_set_title(GTK_WINDOW(progress->window),
382 _("Retrieving new messages"));
383 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
384 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
385 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
386 GTK_SIGNAL_FUNC(inc_dialog_delete_cb), dialog);
387 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
389 progress_dialog_set_value(progress, 0.0);
391 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
393 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
394 ¤txpm, ¤txpmmask);
395 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
396 &errorxpm, &errorxpmmask);
398 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
399 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
401 dialog->show_dialog = TRUE;
402 gtk_widget_show_now(progress->window);
405 dialog->dialog = progress;
406 gettimeofday(&dialog->progress_tv, NULL);
407 gettimeofday(&dialog->folder_tv, NULL);
408 dialog->queue_list = NULL;
411 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
416 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
420 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
421 IncSession *session = list->data;
422 Pop3Session *pop3_session = POP3_SESSION(session->session);
425 session->data = inc_dialog;
428 text[1] = pop3_session->ac_prefs->account_name;
429 text[2] = _("Standby");
430 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
434 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
436 progress_dialog_set_value(inc_dialog->dialog, 0.0);
437 progress_dialog_set_label(inc_dialog->dialog, "");
438 if (inc_dialog->mainwin)
439 main_window_progress_off(inc_dialog->mainwin);
442 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
444 g_return_if_fail(inc_dialog != NULL);
446 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
448 if (inc_dialog->mainwin)
449 main_window_progress_off(inc_dialog->mainwin);
450 progress_dialog_destroy(inc_dialog->dialog);
455 static IncSession *inc_session_new(PrefsAccount *account)
459 g_return_val_if_fail(account != NULL, NULL);
461 if (account->protocol != A_POP3 && account->protocol != A_APOP)
463 if (!account->recv_server || !account->userid)
466 session = g_new0(IncSession, 1);
468 session->session = pop3_session_new(account);
469 session->session->data = session;
470 POP3_SESSION(session->session)->drop_message = inc_drop_message;
471 session_set_recv_message_notify(session->session,
472 inc_recv_message, session);
473 session_set_recv_data_progressive_notify(session->session,
474 inc_recv_data_progressive,
476 session_set_recv_data_notify(session->session,
477 inc_recv_data_finished, session);
482 static void inc_session_destroy(IncSession *session)
484 g_return_if_fail(session != NULL);
486 session_destroy(session->session);
490 static gint inc_start(IncProgressDialog *inc_dialog)
493 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
495 Pop3Session *pop3_session;
501 FolderItem *processing, *inbox;
503 GSList *msglist, *msglist_element;
505 qlist = inc_dialog->queue_list;
506 while (qlist != NULL) {
507 GList *next = qlist->next;
509 session = qlist->data;
510 pop3_session = POP3_SESSION(session->session);
511 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
512 if (pop3_session->ac_prefs->passwd)
514 g_strdup(pop3_session->ac_prefs->passwd);
515 else if (pop3_session->ac_prefs->tmp_pass)
517 g_strdup(pop3_session->ac_prefs->tmp_pass);
521 if (inc_dialog->show_dialog)
522 manage_window_focus_in
523 (inc_dialog->dialog->window,
526 pass = input_dialog_query_password
527 (pop3_session->ac_prefs->recv_server,
530 if (inc_dialog->show_dialog)
531 manage_window_focus_out
532 (inc_dialog->dialog->window,
536 pop3_session->ac_prefs->tmp_pass =
538 pop3_session->pass = pass;
545 #define SET_PIXMAP_AND_TEXT(xpm, xpmmask, str) \
547 gtk_clist_set_pixmap(clist, inc_dialog->cur_row, 0, xpm, xpmmask); \
548 gtk_clist_set_text(clist, inc_dialog->cur_row, 2, str); \
551 for (; inc_dialog->queue_list != NULL; inc_dialog->cur_row++) {
552 session = inc_dialog->queue_list->data;
553 pop3_session = POP3_SESSION(session->session);
555 if (pop3_session->pass == NULL) {
556 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
557 inc_session_destroy(session);
558 inc_dialog->queue_list =
559 g_list_remove(inc_dialog->queue_list, session);
563 inc_progress_dialog_clear(inc_dialog);
564 gtk_clist_moveto(clist, inc_dialog->cur_row, -1, 1.0, 0.0);
566 SET_PIXMAP_AND_TEXT(currentxpm, currentxpmmask,
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 (_("Done (%d message(s) (%s) received)"),
577 pop3_session->cur_total_num,
578 to_human_readable(pop3_session->cur_total_recv_bytes));
580 msg = g_strdup_printf(_("Done (no new messages)"));
581 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, msg);
584 case INC_CONNECT_ERROR:
585 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
586 _("Connection failed"));
588 case INC_AUTH_FAILED:
589 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
593 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
599 case INC_SOCKET_ERROR:
601 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask, _("Error"));
604 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask, _("Timeout"));
607 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
613 if (pop3_session->error_val == PS_AUTHFAIL) {
614 if(!prefs_common.no_recv_err_panel) {
615 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
616 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
617 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
621 /* CLAWS: perform filtering actions on dropped message */
622 /* CLAWS: get default inbox (perhaps per account) */
623 if (pop3_session->ac_prefs->inbox) {
624 /* CLAWS: get destination folder / mailbox */
625 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
627 inbox = folder_get_default_inbox();
629 inbox = folder_get_default_inbox();
631 /* get list of messages in processing */
632 processing = folder_get_default_processing();
633 folder_item_scan(processing);
634 msglist = folder_item_get_msg_list(processing);
636 /* process messages */
637 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
638 msginfo = (MsgInfo *) msglist_element->data;
639 if (!pop3_session->ac_prefs->filter_on_recv || !procmsg_msginfo_filter(msginfo))
640 folder_item_move_msg(inbox, msginfo);
641 procmsg_msginfo_free(msginfo);
643 g_slist_free(msglist);
645 new_msgs += pop3_session->cur_total_num;
647 if (pop3_session->error_val == PS_AUTHFAIL &&
648 pop3_session->ac_prefs->tmp_pass) {
649 g_free(pop3_session->ac_prefs->tmp_pass);
650 pop3_session->ac_prefs->tmp_pass = NULL;
653 pop3_write_uidl_list(pop3_session);
655 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
657 if (inc_dialog->show_dialog)
658 manage_window_focus_in
659 (inc_dialog->dialog->window,
661 inc_put_error(inc_state, pop3_session->error_msg);
662 if (inc_dialog->show_dialog)
663 manage_window_focus_out
664 (inc_dialog->dialog->window,
666 if (inc_state == INC_NO_SPACE ||
667 inc_state == INC_IO_ERROR)
671 inc_session_destroy(session);
672 inc_dialog->queue_list =
673 g_list_remove(inc_dialog->queue_list, session);
676 #undef SET_PIXMAP_AND_TEXT
679 fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
682 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
684 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
687 if (error_num && !prefs_common.no_recv_err_panel) {
688 if (inc_dialog->show_dialog)
689 manage_window_focus_in(inc_dialog->dialog->window,
691 alertpanel_error_log(_("Some errors occurred while getting mail."));
692 if (inc_dialog->show_dialog)
693 manage_window_focus_out(inc_dialog->dialog->window,
698 while (inc_dialog->queue_list != NULL) {
699 session = inc_dialog->queue_list->data;
700 inc_session_destroy(session);
701 inc_dialog->queue_list =
702 g_list_remove(inc_dialog->queue_list, session);
705 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
706 inc_progress_dialog_destroy(inc_dialog);
708 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
710 gtk_label_set_text(GTK_LABEL(GTK_BIN(inc_dialog->dialog->cancel_btn)->child),
719 static IncState inc_pop3_session_do(IncSession *session)
721 Pop3Session *pop3_session = POP3_SESSION(session->session);
722 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
727 debug_print("getting new messages of account %s...\n",
728 pop3_session->ac_prefs->account_name);
730 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
732 buf = g_strdup_printf(_("%s: Retrieving new messages"),
733 pop3_session->ac_prefs->recv_server);
734 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
737 server = pop3_session->ac_prefs->recv_server;
739 port = pop3_session->ac_prefs->set_popport ?
740 pop3_session->ac_prefs->popport :
741 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
742 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
743 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
744 SESSION(pop3_session)->nonblocking =
745 pop3_session->ac_prefs->use_nonblocking_ssl;
747 port = pop3_session->ac_prefs->set_popport ?
748 pop3_session->ac_prefs->popport : 110;
751 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
752 log_message("%s\n", buf);
754 progress_dialog_set_label(inc_dialog->dialog, buf);
757 session_set_timeout(SESSION(pop3_session),
758 prefs_common.io_timeout_secs * 1000);
760 if (session_connect(SESSION(pop3_session), server, port) < 0) {
761 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
763 if(!prefs_common.no_recv_err_panel) {
764 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
765 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
766 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
768 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
770 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
772 session->inc_state = INC_CONNECT_ERROR;
774 return INC_CONNECT_ERROR;
777 while (session_is_connected(SESSION(pop3_session)) &&
778 session->inc_state != INC_CANCEL)
779 gtk_main_iteration();
781 if (session->inc_state == INC_SUCCESS) {
782 switch (pop3_session->error_val) {
784 switch (SESSION(pop3_session)->state) {
786 if (pop3_session->state == POP3_READY)
787 session->inc_state = INC_CONNECT_ERROR;
789 session->inc_state = INC_ERROR;
792 session->inc_state = INC_EOF;
794 case SESSION_TIMEOUT:
795 session->inc_state = INC_TIMEOUT;
798 session->inc_state = INC_SUCCESS;
803 session->inc_state = INC_AUTH_FAILED;
806 session->inc_state = INC_IO_ERROR;
809 session->inc_state = INC_SOCKET_ERROR;
812 session->inc_state = INC_LOCKED;
815 session->inc_state = INC_ERROR;
820 session_disconnect(SESSION(pop3_session));
823 return session->inc_state;
826 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
827 IncSession *inc_session)
829 inc_progress_dialog_set_label(inc_dialog, inc_session);
830 inc_progress_dialog_set_progress(inc_dialog, inc_session);
833 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
834 IncSession *inc_session)
836 ProgressDialog *dialog = inc_dialog->dialog;
837 Pop3Session *session;
839 g_return_if_fail(inc_session != NULL);
841 session = POP3_SESSION(inc_session->session);
843 switch (session->state) {
846 case POP3_GETAUTH_USER:
847 case POP3_GETAUTH_PASS:
848 case POP3_GETAUTH_APOP:
849 progress_dialog_set_label(dialog, _("Authenticating..."));
850 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
851 SESSION(session)->server,
852 session->ac_prefs->account_name);
854 case POP3_GETRANGE_STAT:
855 progress_dialog_set_label
856 (dialog, _("Getting the number of new messages (STAT)..."));
858 case POP3_GETRANGE_LAST:
859 progress_dialog_set_label
860 (dialog, _("Getting the number of new messages (LAST)..."));
862 case POP3_GETRANGE_UIDL:
863 progress_dialog_set_label
864 (dialog, _("Getting the number of new messages (UIDL)..."));
866 case POP3_GETSIZE_LIST:
867 progress_dialog_set_label
868 (dialog, _("Getting the size of messages (LIST)..."));
875 if (session->msg[session->cur_msg].recv_time <
876 session->current_time) {
877 gchar buf[MSGBUFSIZE];
878 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
880 progress_dialog_set_label(dialog, buf);
885 progress_dialog_set_label(dialog, _("Quitting"));
892 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
893 IncSession *inc_session)
895 gchar buf[MSGBUFSIZE];
896 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
897 gchar *total_size_str;
901 if (!pop3_session->new_msg_exist) return;
903 cur_total = inc_session->cur_total_bytes;
904 total = pop3_session->total_bytes;
905 if (pop3_session->state == POP3_RETR ||
906 pop3_session->state == POP3_RETR_RECV ||
907 pop3_session->state == POP3_DELETE) {
908 Xstrdup_a(total_size_str, to_human_readable(total), return);
909 g_snprintf(buf, sizeof(buf),
910 _("Retrieving message (%d / %d) (%s / %s)"),
911 pop3_session->cur_msg, pop3_session->count,
912 to_human_readable(cur_total), total_size_str);
913 progress_dialog_set_label(inc_dialog->dialog, buf);
916 progress_dialog_set_percentage
917 (inc_dialog->dialog,(gfloat)cur_total / (gfloat)total);
919 gtk_progress_set_show_text
920 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), TRUE);
921 g_snprintf(buf, sizeof(buf), "%d / %d",
922 pop3_session->cur_msg, pop3_session->count);
923 gtk_progress_set_format_string
924 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), buf);
925 gtk_progress_bar_update
926 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
927 (gfloat)cur_total / (gfloat)total);
929 if (pop3_session->cur_total_num > 0) {
930 g_snprintf(buf, sizeof(buf),
931 _("Retrieving (%d message(s) (%s) received)"),
932 pop3_session->cur_total_num,
934 (pop3_session->cur_total_recv_bytes));
935 gtk_clist_set_text(GTK_CLIST(inc_dialog->dialog->clist),
936 inc_dialog->cur_row, 2, buf);
940 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
941 IncSession *inc_session)
943 struct timeval tv_cur;
944 struct timeval tv_result;
947 gettimeofday(&tv_cur, NULL);
949 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
950 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
951 if (tv_result.tv_usec < 0) {
953 tv_result.tv_usec += 1000000;
956 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
957 if (msec > PROGRESS_UPDATE_INTERVAL) {
958 inc_progress_dialog_update(inc_dialog, inc_session);
959 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
960 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
964 static gint inc_recv_data_progressive(Session *session, guint cur_len,
965 guint total_len, gpointer data)
967 IncSession *inc_session = (IncSession *)data;
968 Pop3Session *pop3_session = POP3_SESSION(session);
969 IncProgressDialog *inc_dialog;
972 g_return_val_if_fail(inc_session != NULL, -1);
974 if (pop3_session->state != POP3_RETR &&
975 pop3_session->state != POP3_RETR_RECV &&
976 pop3_session->state != POP3_DELETE &&
977 pop3_session->state != POP3_LOGOUT) return 0;
979 if (!pop3_session->new_msg_exist) return 0;
981 cur_total = pop3_session->cur_total_bytes + cur_len;
982 if (cur_total > pop3_session->total_bytes)
983 cur_total = pop3_session->total_bytes;
984 inc_session->cur_total_bytes = cur_total;
986 inc_dialog = (IncProgressDialog *)inc_session->data;
987 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
992 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
994 IncSession *inc_session = (IncSession *)data;
995 IncProgressDialog *inc_dialog;
997 g_return_val_if_fail(inc_session != NULL, -1);
999 inc_dialog = (IncProgressDialog *)inc_session->data;
1001 inc_recv_data_progressive(session, 0, 0, inc_session);
1003 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1004 inc_progress_dialog_update(inc_dialog, inc_session);
1010 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1012 IncSession *inc_session = (IncSession *)data;
1013 IncProgressDialog *inc_dialog;
1015 g_return_val_if_fail(inc_session != NULL, -1);
1017 inc_dialog = (IncProgressDialog *)inc_session->data;
1019 switch (POP3_SESSION(session)->state) {
1020 case POP3_GETAUTH_USER:
1021 case POP3_GETAUTH_PASS:
1022 case POP3_GETAUTH_APOP:
1023 case POP3_GETRANGE_STAT:
1024 case POP3_GETRANGE_LAST:
1025 case POP3_GETRANGE_UIDL:
1026 case POP3_GETSIZE_LIST:
1027 inc_progress_dialog_update(inc_dialog, inc_session);
1030 inc_recv_data_progressive(session, 0, 0, inc_session);
1033 inc_progress_dialog_update(inc_dialog, inc_session);
1042 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1045 FolderItem *dropfolder;
1046 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1049 g_return_val_if_fail(inc_session != NULL, -1);
1051 if (session->ac_prefs->inbox) {
1052 inbox = folder_find_item_from_identifier
1053 (session->ac_prefs->inbox);
1055 inbox = folder_get_default_inbox();
1057 inbox = folder_get_default_inbox();
1063 /* CLAWS: claws uses a global .processing folder for the filtering. */
1064 dropfolder = folder_get_default_processing();
1066 /* add msg file to drop folder */
1067 if ((msgnum = folder_item_add_msg(dropfolder, file, NULL, TRUE)) < 0) {
1075 static void inc_put_error(IncState istate, const gchar *msg)
1077 gchar *log_msg = NULL;
1078 gchar *err_msg = NULL;
1079 gboolean fatal_error = FALSE;
1082 case INC_CONNECT_ERROR:
1083 log_msg = _("Connection failed.");
1084 if (prefs_common.no_recv_err_panel)
1086 err_msg = g_strdup(log_msg);
1089 log_msg = _("Error occurred while processing mail.");
1090 if (prefs_common.no_recv_err_panel)
1093 err_msg = g_strdup_printf
1094 (_("Error occurred while processing mail:\n%s"),
1097 err_msg = g_strdup(log_msg);
1100 log_msg = _("No disk space left.");
1101 err_msg = g_strdup(log_msg);
1105 log_msg = _("Can't write file.");
1106 err_msg = g_strdup(log_msg);
1109 case INC_SOCKET_ERROR:
1110 log_msg = _("Socket error.");
1111 if (prefs_common.no_recv_err_panel)
1113 err_msg = g_strdup(log_msg);
1116 log_msg = _("Connection closed by the remote host.");
1117 if (prefs_common.no_recv_err_panel)
1119 err_msg = g_strdup(log_msg);
1122 log_msg = _("Mailbox is locked.");
1123 if (prefs_common.no_recv_err_panel)
1126 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1129 err_msg = g_strdup(log_msg);
1131 case INC_AUTH_FAILED:
1132 log_msg = _("Authentication failed.");
1133 if (prefs_common.no_recv_err_panel)
1136 err_msg = g_strdup_printf
1137 (_("Authentication failed:\n%s"), msg);
1139 err_msg = g_strdup(log_msg);
1142 log_msg = _("Session timed out.");
1143 if (prefs_common.no_recv_err_panel)
1145 err_msg = g_strdup(log_msg);
1153 log_error("%s\n", log_msg);
1155 log_warning("%s\n", log_msg);
1158 alertpanel_error_log(err_msg);
1163 static void inc_cancel(IncProgressDialog *dialog)
1165 IncSession *session;
1167 g_return_if_fail(dialog != NULL);
1169 if (dialog->queue_list == NULL) {
1170 inc_progress_dialog_destroy(dialog);
1174 session = dialog->queue_list->data;
1176 session->inc_state = INC_CANCEL;
1178 log_message(_("Incorporation cancelled\n"));
1181 gboolean inc_is_active(void)
1183 return (inc_dialog_list != NULL);
1186 void inc_cancel_all(void)
1190 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1191 inc_cancel((IncProgressDialog *)cur->data);
1194 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1196 inc_cancel((IncProgressDialog *)data);
1199 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1202 IncProgressDialog *dialog = (IncProgressDialog *)data;
1204 if (dialog->queue_list == NULL)
1205 inc_progress_dialog_destroy(dialog);
1210 static gint inc_spool_account(PrefsAccount *account)
1213 gchar *mbox, *logname;
1216 logname = g_get_user_name();
1218 if (account->inbox) {
1219 inbox = folder_find_item_from_path(account->inbox);
1221 inbox = folder_get_default_inbox();
1223 inbox = folder_get_default_inbox();
1225 if (is_file_exist(account->local_mbox))
1226 mbox = g_strdup(account->local_mbox);
1228 mbox = g_strconcat(account->local_mbox,
1229 G_DIR_SEPARATOR_S, logname, NULL);
1231 result = get_spool(inbox, mbox);
1234 statusbar_pop_all();
1239 static gint inc_all_spool(void)
1243 gint account_new_msgs = 0;
1245 list = account_get_list();
1246 if (!list) return 0;
1248 for (; list != NULL; list = list->next) {
1249 PrefsAccount *account = list->data;
1251 if ((account->protocol == A_LOCAL) &&
1252 (account->recv_at_getall)) {
1253 account_new_msgs = inc_spool_account(account);
1254 if (account_new_msgs > 0)
1255 new_msgs += account_new_msgs;
1262 static gint get_spool(FolderItem *dest, const gchar *mbox)
1266 gchar tmp_mbox[MAXPATHLEN + 1];
1268 g_return_val_if_fail(dest != NULL, -1);
1269 g_return_val_if_fail(mbox != NULL, -1);
1271 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1272 debug_print("no messages in local mailbox.\n");
1274 } else if (size < 0)
1277 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1280 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1281 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1283 if (copy_mbox(mbox, tmp_mbox) < 0) {
1284 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1288 debug_print("Getting new messages from %s into %s...\n",
1291 msgs = proc_mbox(dest, tmp_mbox, TRUE);
1294 if (msgs >= 0) empty_mbox(mbox);
1295 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1305 void inc_unlock(void)
1307 if (inc_lock_count > 0)
1311 static guint autocheck_timer = 0;
1312 static gpointer autocheck_data = NULL;
1314 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1317 gchar *buf, *numpos;
1319 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1320 *prefs_common.newmail_notify_cmd))
1322 buf = g_strdup(prefs_common.newmail_notify_cmd);
1323 if ((numpos = strstr(buf, "%d")) != NULL) {
1327 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1332 debug_print("executing new mail notification command: %s\n", buf);
1333 execute_command_line(buf, TRUE);
1338 void inc_autocheck_timer_init(MainWindow *mainwin)
1340 autocheck_data = mainwin;
1341 inc_autocheck_timer_set();
1344 static void inc_autocheck_timer_set_interval(guint interval)
1346 inc_autocheck_timer_remove();
1347 /* last test is to avoid re-enabling auto_check after modifying
1348 the common preferences */
1349 if (prefs_common.autochk_newmail && autocheck_data
1350 && prefs_common.work_offline == FALSE) {
1351 autocheck_timer = gtk_timeout_add
1352 (interval, inc_autocheck_func, autocheck_data);
1353 debug_print("added timer = %d\n", autocheck_timer);
1357 void inc_autocheck_timer_set(void)
1359 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1362 void inc_autocheck_timer_remove(void)
1364 if (autocheck_timer) {
1365 debug_print("removed timer = %d\n", autocheck_timer);
1366 gtk_timeout_remove(autocheck_timer);
1367 autocheck_timer = 0;
1371 static gint inc_autocheck_func(gpointer data)
1373 MainWindow *mainwin = (MainWindow *)data;
1375 if (inc_lock_count) {
1376 debug_print("autocheck is locked.\n");
1377 inc_autocheck_timer_set_interval(1000);
1381 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);