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,
124 gboolean update_file);
126 static void inc_put_error (IncState istate,
129 static void inc_cancel_cb (GtkWidget *widget,
131 static gint inc_dialog_delete_cb (GtkWidget *widget,
135 static gint get_spool (FolderItem *dest,
138 static gint inc_spool_account(PrefsAccount *account);
139 static gint inc_all_spool(void);
140 static void inc_autocheck_timer_set_interval (guint interval);
141 static gint inc_autocheck_func (gpointer data);
143 static void inc_notify_cmd (gint new_msgs,
148 * @mainwin: Main window.
149 * @new_messages: TRUE if some messages have been received.
151 * Update the folder view and the summary view after receiving
152 * messages. If @new_messages is FALSE, this function avoids unneeded
155 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
159 if (prefs_common.scan_all_after_inc)
160 folderview_check_new(NULL);
162 if (!new_messages && !prefs_common.scan_all_after_inc) return;
164 if (prefs_common.open_inbox_on_inc) {
165 item = cur_account && cur_account->inbox
166 ? folder_find_item_from_identifier(cur_account->inbox)
167 : folder_get_default_inbox();
168 folderview_unselect(mainwin->folderview);
169 folderview_select(mainwin->folderview, item);
173 void inc_mail(MainWindow *mainwin, gboolean notify)
176 gint account_new_msgs = 0;
178 if (inc_lock_count) return;
180 if (prefs_common.work_offline)
181 if (alertpanel(_("Offline warning"),
182 _("You're working offline. Override?"),
183 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
187 inc_autocheck_timer_remove();
188 main_window_lock(mainwin);
190 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
191 /* external incorporating program */
192 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
193 main_window_unlock(mainwin);
194 inc_autocheck_timer_set();
198 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
199 if (account_new_msgs > 0)
200 new_msgs += account_new_msgs;
203 inc_finished(mainwin, new_msgs > 0);
204 main_window_unlock(mainwin);
205 inc_notify_cmd(new_msgs, notify);
206 inc_autocheck_timer_set();
210 void inc_pop_before_smtp(PrefsAccount *acc)
212 IncProgressDialog *inc_dialog;
216 mainwin = mainwindow_get_mainwindow();
218 session = inc_session_new(acc);
219 if (!session) return;
220 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
222 inc_dialog = inc_progress_dialog_create(FALSE);
223 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
225 /* FIXME: assumes to attach to first main window */
226 inc_dialog->mainwin = mainwin;
227 inc_progress_dialog_set_list(inc_dialog);
230 toolbar_main_set_sensitive(mainwin);
231 main_window_set_menu_sensitive(mainwin);
234 inc_start(inc_dialog);
237 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
239 IncProgressDialog *inc_dialog;
242 switch (account->protocol) {
246 * FIXME: it should return foldeview_check_new() value.
247 * TODO: do it when bug [19] is fixed (IMAP folder sets
248 * an incorrect new message count)
250 folderview_check_new(FOLDER(account->folder));
254 session = inc_session_new(account);
255 if (!session) return 0;
257 inc_dialog = inc_progress_dialog_create(FALSE);
258 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
260 inc_dialog->mainwin = mainwin;
261 inc_progress_dialog_set_list(inc_dialog);
264 toolbar_main_set_sensitive(mainwin);
265 main_window_set_menu_sensitive(mainwin);
268 return inc_start(inc_dialog);
271 return inc_spool_account(account);
279 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
283 if (inc_lock_count) return 0;
285 if (prefs_common.work_offline)
286 if (alertpanel(_("Offline warning"),
287 _("You're working offline. Override?"),
288 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
291 inc_autocheck_timer_remove();
292 main_window_lock(mainwin);
294 new_msgs = inc_account_mail_real(mainwin, account);
296 inc_finished(mainwin, new_msgs > 0);
297 main_window_unlock(mainwin);
298 inc_autocheck_timer_set();
303 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
306 GList *list, *queue_list = NULL;
307 IncProgressDialog *inc_dialog;
309 gint account_new_msgs = 0;
311 if (prefs_common.work_offline)
312 if (alertpanel(_("Offline warning"),
313 _("You're working offline. Override?"),
314 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
317 if (inc_lock_count) return;
319 inc_autocheck_timer_remove();
320 main_window_lock(mainwin);
322 list = account_get_list();
324 inc_finished(mainwin, new_msgs > 0);
325 main_window_unlock(mainwin);
326 inc_notify_cmd(new_msgs, notify);
327 inc_autocheck_timer_set();
331 /* check local folders */
332 account_new_msgs = inc_all_spool();
333 if (account_new_msgs > 0)
334 new_msgs += account_new_msgs;
336 /* check IMAP4 / News folders */
337 for (list = account_get_list(); list != NULL; list = list->next) {
338 PrefsAccount *account = list->data;
339 if ((account->protocol == A_IMAP4 ||
340 account->protocol == A_NNTP) && account->recv_at_getall) {
341 new_msgs += folderview_check_new(FOLDER(account->folder));
345 /* check POP3 accounts */
346 for (list = account_get_list(); list != NULL; list = list->next) {
348 PrefsAccount *account = list->data;
350 if (account->recv_at_getall) {
351 session = inc_session_new(account);
353 queue_list = g_list_append(queue_list, session);
358 inc_dialog = inc_progress_dialog_create(autocheck);
359 inc_dialog->queue_list = queue_list;
360 inc_dialog->mainwin = mainwin;
361 inc_progress_dialog_set_list(inc_dialog);
363 toolbar_main_set_sensitive(mainwin);
364 main_window_set_menu_sensitive(mainwin);
365 new_msgs += inc_start(inc_dialog);
368 inc_finished(mainwin, new_msgs > 0);
369 main_window_unlock(mainwin);
370 inc_notify_cmd(new_msgs, notify);
371 inc_autocheck_timer_set();
374 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
376 IncProgressDialog *dialog;
377 ProgressDialog *progress;
379 dialog = g_new0(IncProgressDialog, 1);
381 progress = progress_dialog_create();
382 gtk_window_set_title(GTK_WINDOW(progress->window),
383 _("Retrieving new messages"));
384 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
385 G_CALLBACK(inc_cancel_cb), dialog);
386 g_signal_connect(G_OBJECT(progress->window), "delete_event",
387 G_CALLBACK(inc_dialog_delete_cb), dialog);
388 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
390 progress_dialog_set_value(progress, 0.0);
392 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
394 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
395 ¤txpm, ¤txpmmask);
396 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
397 &errorxpm, &errorxpmmask);
399 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
400 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
402 dialog->show_dialog = TRUE;
403 gtk_widget_show_now(progress->window);
406 dialog->dialog = progress;
407 gettimeofday(&dialog->progress_tv, NULL);
408 gettimeofday(&dialog->folder_tv, NULL);
409 dialog->queue_list = NULL;
412 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
417 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
421 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
422 IncSession *session = list->data;
423 Pop3Session *pop3_session = POP3_SESSION(session->session);
426 session->data = inc_dialog;
429 text[1] = pop3_session->ac_prefs->account_name;
430 text[2] = _("Standby");
431 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
435 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
437 progress_dialog_set_value(inc_dialog->dialog, 0.0);
438 progress_dialog_set_label(inc_dialog->dialog, "");
439 if (inc_dialog->mainwin)
440 main_window_progress_off(inc_dialog->mainwin);
443 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
445 g_return_if_fail(inc_dialog != NULL);
447 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
449 if (inc_dialog->mainwin)
450 main_window_progress_off(inc_dialog->mainwin);
451 progress_dialog_destroy(inc_dialog->dialog);
456 static IncSession *inc_session_new(PrefsAccount *account)
460 g_return_val_if_fail(account != NULL, NULL);
462 if (account->protocol != A_POP3 && account->protocol != A_APOP)
464 if (!account->recv_server || !account->userid)
467 session = g_new0(IncSession, 1);
469 session->session = pop3_session_new(account);
470 session->session->data = session;
471 POP3_SESSION(session->session)->drop_message = inc_drop_message;
472 session_set_recv_message_notify(session->session,
473 inc_recv_message, session);
474 session_set_recv_data_progressive_notify(session->session,
475 inc_recv_data_progressive,
477 session_set_recv_data_notify(session->session,
478 inc_recv_data_finished, session);
483 static void inc_session_destroy(IncSession *session)
485 g_return_if_fail(session != NULL);
487 session_destroy(session->session);
491 static gint inc_start(IncProgressDialog *inc_dialog)
494 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
496 Pop3Session *pop3_session;
502 FolderItem *processing, *inbox;
504 GSList *msglist, *msglist_element;
506 qlist = inc_dialog->queue_list;
507 while (qlist != NULL) {
508 GList *next = qlist->next;
510 session = qlist->data;
511 pop3_session = POP3_SESSION(session->session);
512 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
513 if (pop3_session->ac_prefs->passwd)
515 g_strdup(pop3_session->ac_prefs->passwd);
516 else if (pop3_session->ac_prefs->tmp_pass)
518 g_strdup(pop3_session->ac_prefs->tmp_pass);
522 if (inc_dialog->show_dialog)
523 manage_window_focus_in
524 (inc_dialog->dialog->window,
527 pass = input_dialog_query_password
528 (pop3_session->ac_prefs->recv_server,
531 if (inc_dialog->show_dialog)
532 manage_window_focus_out
533 (inc_dialog->dialog->window,
537 pop3_session->ac_prefs->tmp_pass =
539 pop3_session->pass = pass;
546 #define SET_PIXMAP_AND_TEXT(xpm, xpmmask, str) \
548 gtk_clist_set_pixmap(clist, inc_dialog->cur_row, 0, xpm, xpmmask); \
549 gtk_clist_set_text(clist, inc_dialog->cur_row, 2, str); \
552 for (; inc_dialog->queue_list != NULL; inc_dialog->cur_row++) {
553 session = inc_dialog->queue_list->data;
554 pop3_session = POP3_SESSION(session->session);
556 if (pop3_session->pass == NULL) {
557 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
558 inc_session_destroy(session);
559 inc_dialog->queue_list =
560 g_list_remove(inc_dialog->queue_list, session);
564 inc_progress_dialog_clear(inc_dialog);
565 gtk_clist_moveto(clist, inc_dialog->cur_row, -1, 1.0, 0.0);
567 SET_PIXMAP_AND_TEXT(currentxpm, currentxpmmask,
570 /* begin POP3 session */
571 inc_state = inc_pop3_session_do(session);
575 if (pop3_session->cur_total_num > 0)
576 msg = g_strdup_printf
577 (_("Done (%d message(s) (%s) received)"),
578 pop3_session->cur_total_num,
579 to_human_readable(pop3_session->cur_total_recv_bytes));
581 msg = g_strdup_printf(_("Done (no new messages)"));
582 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, msg);
585 case INC_CONNECT_ERROR:
586 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
587 _("Connection failed"));
589 case INC_AUTH_FAILED:
590 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
594 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
600 case INC_SOCKET_ERROR:
602 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask, _("Error"));
605 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask, _("Timeout"));
608 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
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 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
639 msginfo = (MsgInfo *) msglist_element->data;
640 if (!pop3_session->ac_prefs->filter_on_recv || !procmsg_msginfo_filter(msginfo))
641 folder_item_move_msg(inbox, msginfo);
642 procmsg_msginfo_free(msginfo);
644 g_slist_free(msglist);
646 new_msgs += pop3_session->cur_total_num;
648 if (pop3_session->error_val == PS_AUTHFAIL &&
649 pop3_session->ac_prefs->tmp_pass) {
650 g_free(pop3_session->ac_prefs->tmp_pass);
651 pop3_session->ac_prefs->tmp_pass = NULL;
654 pop3_write_uidl_list(pop3_session);
656 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
658 if (inc_dialog->show_dialog)
659 manage_window_focus_in
660 (inc_dialog->dialog->window,
662 inc_put_error(inc_state, pop3_session->error_msg);
663 if (inc_dialog->show_dialog)
664 manage_window_focus_out
665 (inc_dialog->dialog->window,
667 if (inc_state == INC_NO_SPACE ||
668 inc_state == INC_IO_ERROR)
672 inc_session_destroy(session);
673 inc_dialog->queue_list =
674 g_list_remove(inc_dialog->queue_list, session);
677 #undef SET_PIXMAP_AND_TEXT
680 fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
683 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
685 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
688 if (error_num && !prefs_common.no_recv_err_panel) {
689 if (inc_dialog->show_dialog)
690 manage_window_focus_in(inc_dialog->dialog->window,
692 alertpanel_error_log(_("Some errors occurred while getting mail."));
693 if (inc_dialog->show_dialog)
694 manage_window_focus_out(inc_dialog->dialog->window,
699 while (inc_dialog->queue_list != NULL) {
700 session = inc_dialog->queue_list->data;
701 inc_session_destroy(session);
702 inc_dialog->queue_list =
703 g_list_remove(inc_dialog->queue_list, session);
706 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
707 inc_progress_dialog_destroy(inc_dialog);
709 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
711 gtk_label_set_text(GTK_LABEL(GTK_BIN(inc_dialog->dialog->cancel_btn)->child),
720 static IncState inc_pop3_session_do(IncSession *session)
722 Pop3Session *pop3_session = POP3_SESSION(session->session);
723 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
728 debug_print("getting new messages of account %s...\n",
729 pop3_session->ac_prefs->account_name);
731 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
733 buf = g_strdup_printf(_("%s: Retrieving new messages"),
734 pop3_session->ac_prefs->recv_server);
735 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
738 server = pop3_session->ac_prefs->recv_server;
740 port = pop3_session->ac_prefs->set_popport ?
741 pop3_session->ac_prefs->popport :
742 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
743 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
744 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
745 SESSION(pop3_session)->nonblocking =
746 pop3_session->ac_prefs->use_nonblocking_ssl;
748 port = pop3_session->ac_prefs->set_popport ?
749 pop3_session->ac_prefs->popport : 110;
752 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
753 log_message("%s\n", buf);
755 progress_dialog_set_label(inc_dialog->dialog, buf);
758 session_set_timeout(SESSION(pop3_session),
759 prefs_common.io_timeout_secs * 1000);
761 if (session_connect(SESSION(pop3_session), server, port) < 0) {
762 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
764 if(!prefs_common.no_recv_err_panel) {
765 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
766 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
767 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
769 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
771 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
773 session->inc_state = INC_CONNECT_ERROR;
775 return INC_CONNECT_ERROR;
778 while (session_is_connected(SESSION(pop3_session)) &&
779 session->inc_state != INC_CANCEL)
780 gtk_main_iteration();
782 if (session->inc_state == INC_SUCCESS) {
783 switch (pop3_session->error_val) {
785 switch (SESSION(pop3_session)->state) {
787 if (pop3_session->state == POP3_READY)
788 session->inc_state = INC_CONNECT_ERROR;
790 session->inc_state = INC_ERROR;
793 session->inc_state = INC_EOF;
795 case SESSION_TIMEOUT:
796 session->inc_state = INC_TIMEOUT;
799 session->inc_state = INC_SUCCESS;
804 session->inc_state = INC_AUTH_FAILED;
807 session->inc_state = INC_IO_ERROR;
810 session->inc_state = INC_SOCKET_ERROR;
813 session->inc_state = INC_LOCKED;
816 session->inc_state = INC_ERROR;
821 session_disconnect(SESSION(pop3_session));
824 return session->inc_state;
827 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
828 IncSession *inc_session)
830 inc_progress_dialog_set_label(inc_dialog, inc_session);
831 inc_progress_dialog_set_progress(inc_dialog, inc_session);
834 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
835 IncSession *inc_session)
837 ProgressDialog *dialog = inc_dialog->dialog;
838 Pop3Session *session;
840 g_return_if_fail(inc_session != NULL);
842 session = POP3_SESSION(inc_session->session);
844 switch (session->state) {
847 case POP3_GETAUTH_USER:
848 case POP3_GETAUTH_PASS:
849 case POP3_GETAUTH_APOP:
850 progress_dialog_set_label(dialog, _("Authenticating..."));
851 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
852 SESSION(session)->server,
853 session->ac_prefs->account_name);
855 case POP3_GETRANGE_STAT:
856 progress_dialog_set_label
857 (dialog, _("Getting the number of new messages (STAT)..."));
859 case POP3_GETRANGE_LAST:
860 progress_dialog_set_label
861 (dialog, _("Getting the number of new messages (LAST)..."));
863 case POP3_GETRANGE_UIDL:
864 progress_dialog_set_label
865 (dialog, _("Getting the number of new messages (UIDL)..."));
867 case POP3_GETSIZE_LIST:
868 progress_dialog_set_label
869 (dialog, _("Getting the size of messages (LIST)..."));
876 if (session->msg[session->cur_msg].recv_time <
877 session->current_time) {
878 gchar buf[MSGBUFSIZE];
879 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
881 progress_dialog_set_label(dialog, buf);
886 progress_dialog_set_label(dialog, _("Quitting"));
893 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
894 IncSession *inc_session)
896 gchar buf[MSGBUFSIZE];
897 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
898 gchar *total_size_str;
902 if (!pop3_session->new_msg_exist) return;
904 cur_total = inc_session->cur_total_bytes;
905 total = pop3_session->total_bytes;
906 if (pop3_session->state == POP3_RETR ||
907 pop3_session->state == POP3_RETR_RECV ||
908 pop3_session->state == POP3_DELETE) {
909 Xstrdup_a(total_size_str, to_human_readable(total), return);
910 g_snprintf(buf, sizeof(buf),
911 _("Retrieving message (%d / %d) (%s / %s)"),
912 pop3_session->cur_msg, pop3_session->count,
913 to_human_readable(cur_total), total_size_str);
914 progress_dialog_set_label(inc_dialog->dialog, buf);
917 progress_dialog_set_percentage
918 (inc_dialog->dialog,(gfloat)cur_total / (gfloat)total);
920 gtk_progress_set_show_text
921 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), TRUE);
922 g_snprintf(buf, sizeof(buf), "%d / %d",
923 pop3_session->cur_msg, pop3_session->count);
924 gtk_progress_set_format_string
925 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), buf);
926 gtk_progress_bar_update
927 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
928 (gfloat)cur_total / (gfloat)total);
930 if (pop3_session->cur_total_num > 0) {
931 g_snprintf(buf, sizeof(buf),
932 _("Retrieving (%d message(s) (%s) received)"),
933 pop3_session->cur_total_num,
935 (pop3_session->cur_total_recv_bytes));
936 gtk_clist_set_text(GTK_CLIST(inc_dialog->dialog->clist),
937 inc_dialog->cur_row, 2, buf);
941 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
942 IncSession *inc_session)
944 struct timeval tv_cur;
945 struct timeval tv_result;
948 gettimeofday(&tv_cur, NULL);
950 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
951 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
952 if (tv_result.tv_usec < 0) {
954 tv_result.tv_usec += 1000000;
957 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
958 if (msec > PROGRESS_UPDATE_INTERVAL) {
959 inc_progress_dialog_update(inc_dialog, inc_session);
960 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
961 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
965 static gint inc_recv_data_progressive(Session *session, guint cur_len,
966 guint total_len, gpointer data)
968 IncSession *inc_session = (IncSession *)data;
969 Pop3Session *pop3_session = POP3_SESSION(session);
970 IncProgressDialog *inc_dialog;
973 g_return_val_if_fail(inc_session != NULL, -1);
975 if (pop3_session->state != POP3_RETR &&
976 pop3_session->state != POP3_RETR_RECV &&
977 pop3_session->state != POP3_DELETE &&
978 pop3_session->state != POP3_LOGOUT) return 0;
980 if (!pop3_session->new_msg_exist) return 0;
982 cur_total = pop3_session->cur_total_bytes + cur_len;
983 if (cur_total > pop3_session->total_bytes)
984 cur_total = pop3_session->total_bytes;
985 inc_session->cur_total_bytes = cur_total;
987 inc_dialog = (IncProgressDialog *)inc_session->data;
988 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
993 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
995 IncSession *inc_session = (IncSession *)data;
996 IncProgressDialog *inc_dialog;
998 g_return_val_if_fail(inc_session != NULL, -1);
1000 inc_dialog = (IncProgressDialog *)inc_session->data;
1002 inc_recv_data_progressive(session, 0, 0, inc_session);
1004 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1005 inc_progress_dialog_update(inc_dialog, inc_session);
1011 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1013 IncSession *inc_session = (IncSession *)data;
1014 IncProgressDialog *inc_dialog;
1016 g_return_val_if_fail(inc_session != NULL, -1);
1018 inc_dialog = (IncProgressDialog *)inc_session->data;
1020 switch (POP3_SESSION(session)->state) {
1021 case POP3_GETAUTH_USER:
1022 case POP3_GETAUTH_PASS:
1023 case POP3_GETAUTH_APOP:
1024 case POP3_GETRANGE_STAT:
1025 case POP3_GETRANGE_LAST:
1026 case POP3_GETRANGE_UIDL:
1027 case POP3_GETSIZE_LIST:
1028 inc_progress_dialog_update(inc_dialog, inc_session);
1031 inc_recv_data_progressive(session, 0, 0, inc_session);
1034 inc_progress_dialog_update(inc_dialog, inc_session);
1043 static gint inc_drop_message(Pop3Session *session, const gchar *file,
1044 gboolean update_file)
1047 FolderItem *dropfolder;
1048 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1051 g_return_val_if_fail(inc_session != NULL, -1);
1053 if (session->ac_prefs->inbox) {
1054 inbox = folder_find_item_from_identifier
1055 (session->ac_prefs->inbox);
1057 inbox = folder_get_default_inbox();
1059 inbox = folder_get_default_inbox();
1065 /* CLAWS: claws uses a global .processing folder for the filtering. */
1066 dropfolder = folder_get_default_processing();
1068 /* add msg file to drop folder */
1069 if ((msgnum = folder_item_add_msg(
1070 dropfolder, file, NULL, !update_file)) < 0) {
1076 gchar *path = strdup(file);
1077 gchar *snum = strrchr(file, G_DIR_SEPARATOR);
1079 FolderItem *item = NULL;
1085 return 0; /* not a real problem */
1090 if (strrchr(path, G_DIR_SEPARATOR))
1091 *(strrchr(path, G_DIR_SEPARATOR))='\0';
1093 item = folder_find_item_from_phys_path(path);
1095 folder_item_remove_msg(item, num);
1102 static void inc_put_error(IncState istate, const gchar *msg)
1104 gchar *log_msg = NULL;
1105 gchar *err_msg = NULL;
1106 gboolean fatal_error = FALSE;
1109 case INC_CONNECT_ERROR:
1110 log_msg = _("Connection failed.");
1111 if (prefs_common.no_recv_err_panel)
1113 err_msg = g_strdup(log_msg);
1116 log_msg = _("Error occurred while processing mail.");
1117 if (prefs_common.no_recv_err_panel)
1120 err_msg = g_strdup_printf
1121 (_("Error occurred while processing mail:\n%s"),
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(log_msg);
1143 log_msg = _("Connection closed by the remote host.");
1144 if (prefs_common.no_recv_err_panel)
1146 err_msg = g_strdup(log_msg);
1149 log_msg = _("Mailbox is locked.");
1150 if (prefs_common.no_recv_err_panel)
1153 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1156 err_msg = g_strdup(log_msg);
1158 case INC_AUTH_FAILED:
1159 log_msg = _("Authentication failed.");
1160 if (prefs_common.no_recv_err_panel)
1163 err_msg = g_strdup_printf
1164 (_("Authentication failed:\n%s"), msg);
1166 err_msg = g_strdup(log_msg);
1169 log_msg = _("Session timed out.");
1170 if (prefs_common.no_recv_err_panel)
1172 err_msg = g_strdup(log_msg);
1180 log_error("%s\n", log_msg);
1182 log_warning("%s\n", log_msg);
1185 alertpanel_error_log(err_msg);
1190 static void inc_cancel(IncProgressDialog *dialog)
1192 IncSession *session;
1194 g_return_if_fail(dialog != NULL);
1196 if (dialog->queue_list == NULL) {
1197 inc_progress_dialog_destroy(dialog);
1201 session = dialog->queue_list->data;
1203 session->inc_state = INC_CANCEL;
1205 log_message(_("Incorporation cancelled\n"));
1208 gboolean inc_is_active(void)
1210 return (inc_dialog_list != NULL);
1213 void inc_cancel_all(void)
1217 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1218 inc_cancel((IncProgressDialog *)cur->data);
1221 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1223 inc_cancel((IncProgressDialog *)data);
1226 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1229 IncProgressDialog *dialog = (IncProgressDialog *)data;
1231 if (dialog->queue_list == NULL)
1232 inc_progress_dialog_destroy(dialog);
1237 static gint inc_spool_account(PrefsAccount *account)
1240 gchar *mbox, *logname;
1243 logname = g_get_user_name();
1245 if (account->inbox) {
1246 inbox = folder_find_item_from_path(account->inbox);
1248 inbox = folder_get_default_inbox();
1250 inbox = folder_get_default_inbox();
1252 if (is_file_exist(account->local_mbox))
1253 mbox = g_strdup(account->local_mbox);
1255 mbox = g_strconcat(account->local_mbox,
1256 G_DIR_SEPARATOR_S, logname, NULL);
1258 result = get_spool(inbox, mbox);
1261 statusbar_pop_all();
1266 static gint inc_all_spool(void)
1270 gint account_new_msgs = 0;
1272 list = account_get_list();
1273 if (!list) return 0;
1275 for (; list != NULL; list = list->next) {
1276 PrefsAccount *account = list->data;
1278 if ((account->protocol == A_LOCAL) &&
1279 (account->recv_at_getall)) {
1280 account_new_msgs = inc_spool_account(account);
1281 if (account_new_msgs > 0)
1282 new_msgs += account_new_msgs;
1289 static gint get_spool(FolderItem *dest, const gchar *mbox)
1293 gchar tmp_mbox[MAXPATHLEN + 1];
1295 g_return_val_if_fail(dest != NULL, -1);
1296 g_return_val_if_fail(mbox != NULL, -1);
1298 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1299 debug_print("no messages in local mailbox.\n");
1301 } else if (size < 0)
1304 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1307 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1308 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1310 if (copy_mbox(mbox, tmp_mbox) < 0) {
1311 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1315 debug_print("Getting new messages from %s into %s...\n",
1318 msgs = proc_mbox(dest, tmp_mbox, TRUE);
1321 if (msgs >= 0) empty_mbox(mbox);
1322 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1332 void inc_unlock(void)
1334 if (inc_lock_count > 0)
1338 static guint autocheck_timer = 0;
1339 static gpointer autocheck_data = NULL;
1341 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1346 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1347 *prefs_common.newmail_notify_cmd))
1349 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1350 buf[1] == 'd' && !strchr(&buf[1], '%'))
1351 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1354 buf = g_strdup(prefs_common.newmail_notify_cmd);
1356 execute_command_line(buf, TRUE);
1361 void inc_autocheck_timer_init(MainWindow *mainwin)
1363 autocheck_data = mainwin;
1364 inc_autocheck_timer_set();
1367 static void inc_autocheck_timer_set_interval(guint interval)
1369 inc_autocheck_timer_remove();
1370 /* last test is to avoid re-enabling auto_check after modifying
1371 the common preferences */
1372 if (prefs_common.autochk_newmail && autocheck_data
1373 && prefs_common.work_offline == FALSE) {
1374 autocheck_timer = gtk_timeout_add
1375 (interval, inc_autocheck_func, autocheck_data);
1376 debug_print("added timer = %d\n", autocheck_timer);
1380 void inc_autocheck_timer_set(void)
1382 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1385 void inc_autocheck_timer_remove(void)
1387 if (autocheck_timer) {
1388 debug_print("removed timer = %d\n", autocheck_timer);
1389 gtk_timeout_remove(autocheck_timer);
1390 autocheck_timer = 0;
1394 static gint inc_autocheck_func(gpointer data)
1396 MainWindow *mainwin = (MainWindow *)data;
1398 if (inc_lock_count) {
1399 debug_print("autocheck is locked.\n");
1400 inc_autocheck_timer_set_interval(1000);
1404 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);