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,
126 Pop3Session *session);
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 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
384 G_CALLBACK(inc_cancel_cb), dialog);
385 g_signal_connect(G_OBJECT(progress->window), "delete_event",
386 G_CALLBACK(inc_dialog_delete_cb), dialog);
387 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
389 progress_dialog_get_fraction(progress);
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_get_fraction(inc_dialog->dialog);
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)
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 folder_item_update_freeze();
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 folder_item_update_thaw();
645 g_slist_free(msglist);
649 new_msgs += pop3_session->cur_total_num;
651 if (pop3_session->error_val == PS_AUTHFAIL &&
652 pop3_session->ac_prefs->tmp_pass) {
653 g_free(pop3_session->ac_prefs->tmp_pass);
654 pop3_session->ac_prefs->tmp_pass = NULL;
657 pop3_write_uidl_list(pop3_session);
659 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
661 if (inc_dialog->show_dialog)
662 manage_window_focus_in
663 (inc_dialog->dialog->window,
665 inc_put_error(inc_state, pop3_session);
666 if (inc_dialog->show_dialog)
667 manage_window_focus_out
668 (inc_dialog->dialog->window,
670 if (inc_state == INC_NO_SPACE ||
671 inc_state == INC_IO_ERROR)
675 inc_session_destroy(session);
676 inc_dialog->queue_list =
677 g_list_remove(inc_dialog->queue_list, session);
680 #undef SET_PIXMAP_AND_TEXT
683 fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
686 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
688 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
691 if (error_num && !prefs_common.no_recv_err_panel) {
692 if (inc_dialog->show_dialog)
693 manage_window_focus_in(inc_dialog->dialog->window,
695 alertpanel_error_log(_("Some errors occurred while getting mail."));
696 if (inc_dialog->show_dialog)
697 manage_window_focus_out(inc_dialog->dialog->window,
702 while (inc_dialog->queue_list != NULL) {
703 session = inc_dialog->queue_list->data;
704 inc_session_destroy(session);
705 inc_dialog->queue_list =
706 g_list_remove(inc_dialog->queue_list, session);
709 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
710 inc_progress_dialog_destroy(inc_dialog);
712 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
714 gtk_label_set_text(GTK_LABEL(GTK_BIN(inc_dialog->dialog->cancel_btn)->child),
723 static IncState inc_pop3_session_do(IncSession *session)
725 Pop3Session *pop3_session = POP3_SESSION(session->session);
726 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
731 debug_print("getting new messages of account %s...\n",
732 pop3_session->ac_prefs->account_name);
734 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
736 buf = g_strdup_printf(_("%s: Retrieving new messages"),
737 pop3_session->ac_prefs->recv_server);
738 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
741 server = pop3_session->ac_prefs->recv_server;
743 port = pop3_session->ac_prefs->set_popport ?
744 pop3_session->ac_prefs->popport :
745 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
746 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
747 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
748 SESSION(pop3_session)->nonblocking =
749 pop3_session->ac_prefs->use_nonblocking_ssl;
751 port = pop3_session->ac_prefs->set_popport ?
752 pop3_session->ac_prefs->popport : 110;
755 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
756 log_message("%s\n", buf);
758 progress_dialog_set_label(inc_dialog->dialog, buf);
761 session_set_timeout(SESSION(pop3_session),
762 prefs_common.io_timeout_secs * 1000);
764 if (session_connect(SESSION(pop3_session), server, port) < 0) {
765 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
767 if(!prefs_common.no_recv_err_panel) {
768 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
769 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
770 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
772 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
774 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
776 session->inc_state = INC_CONNECT_ERROR;
778 return INC_CONNECT_ERROR;
781 while (session_is_connected(SESSION(pop3_session)) &&
782 session->inc_state != INC_CANCEL)
783 gtk_main_iteration();
785 if (session->inc_state == INC_SUCCESS) {
786 switch (pop3_session->error_val) {
788 switch (SESSION(pop3_session)->state) {
790 if (pop3_session->state == POP3_READY)
791 session->inc_state = INC_CONNECT_ERROR;
793 session->inc_state = INC_ERROR;
796 session->inc_state = INC_EOF;
798 case SESSION_TIMEOUT:
799 session->inc_state = INC_TIMEOUT;
802 session->inc_state = INC_SUCCESS;
807 session->inc_state = INC_AUTH_FAILED;
810 session->inc_state = INC_IO_ERROR;
813 session->inc_state = INC_SOCKET_ERROR;
816 session->inc_state = INC_LOCKED;
819 session->inc_state = INC_ERROR;
824 session_disconnect(SESSION(pop3_session));
827 return session->inc_state;
830 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
831 IncSession *inc_session)
833 inc_progress_dialog_set_label(inc_dialog, inc_session);
834 inc_progress_dialog_set_progress(inc_dialog, inc_session);
837 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
838 IncSession *inc_session)
840 ProgressDialog *dialog = inc_dialog->dialog;
841 Pop3Session *session;
843 g_return_if_fail(inc_session != NULL);
845 session = POP3_SESSION(inc_session->session);
847 switch (session->state) {
850 case POP3_GETAUTH_USER:
851 case POP3_GETAUTH_PASS:
852 case POP3_GETAUTH_APOP:
853 progress_dialog_set_label(dialog, _("Authenticating..."));
854 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
855 SESSION(session)->server,
856 session->ac_prefs->account_name);
858 case POP3_GETRANGE_STAT:
859 progress_dialog_set_label
860 (dialog, _("Getting the number of new messages (STAT)..."));
862 case POP3_GETRANGE_LAST:
863 progress_dialog_set_label
864 (dialog, _("Getting the number of new messages (LAST)..."));
866 case POP3_GETRANGE_UIDL:
867 progress_dialog_set_label
868 (dialog, _("Getting the number of new messages (UIDL)..."));
870 case POP3_GETSIZE_LIST:
871 progress_dialog_set_label
872 (dialog, _("Getting the size of messages (LIST)..."));
879 if (session->msg[session->cur_msg].recv_time <
880 session->current_time) {
881 gchar buf[MSGBUFSIZE];
882 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
884 progress_dialog_set_label(dialog, buf);
889 progress_dialog_set_label(dialog, _("Quitting"));
896 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
897 IncSession *inc_session)
899 gchar buf[MSGBUFSIZE];
900 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
901 gchar *total_size_str;
905 if (!pop3_session->new_msg_exist) return;
907 cur_total = inc_session->cur_total_bytes;
908 total = pop3_session->total_bytes;
909 if (pop3_session->state == POP3_RETR ||
910 pop3_session->state == POP3_RETR_RECV ||
911 pop3_session->state == POP3_DELETE) {
912 Xstrdup_a(total_size_str, to_human_readable(total), return);
913 g_snprintf(buf, sizeof(buf),
914 _("Retrieving message (%d / %d) (%s / %s)"),
915 pop3_session->cur_msg, pop3_session->count,
916 to_human_readable(cur_total), total_size_str);
917 progress_dialog_set_label(inc_dialog->dialog, buf);
920 progress_dialog_set_fraction
921 (inc_dialog->dialog,(gfloat)cur_total / (gfloat)total);
923 g_snprintf(buf, sizeof(buf), "%d / %d",
924 pop3_session->cur_msg, pop3_session->count);
925 gtk_progress_bar_set_text
926 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), buf);
927 gtk_progress_bar_set_fraction
928 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
929 (gfloat)cur_total / (gfloat)total);
931 if (pop3_session->cur_total_num > 0) {
932 g_snprintf(buf, sizeof(buf),
933 _("Retrieving (%d message(s) (%s) received)"),
934 pop3_session->cur_total_num,
936 (pop3_session->cur_total_recv_bytes));
937 gtk_clist_set_text(GTK_CLIST(inc_dialog->dialog->clist),
938 inc_dialog->cur_row, 2, buf);
942 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
943 IncSession *inc_session)
945 struct timeval tv_cur;
946 struct timeval tv_result;
949 gettimeofday(&tv_cur, NULL);
951 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
952 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
953 if (tv_result.tv_usec < 0) {
955 tv_result.tv_usec += 1000000;
958 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
959 if (msec > PROGRESS_UPDATE_INTERVAL) {
960 inc_progress_dialog_update(inc_dialog, inc_session);
961 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
962 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
966 static gint inc_recv_data_progressive(Session *session, guint cur_len,
967 guint total_len, gpointer data)
969 IncSession *inc_session = (IncSession *)data;
970 Pop3Session *pop3_session = POP3_SESSION(session);
971 IncProgressDialog *inc_dialog;
974 g_return_val_if_fail(inc_session != NULL, -1);
976 if (pop3_session->state != POP3_RETR &&
977 pop3_session->state != POP3_RETR_RECV &&
978 pop3_session->state != POP3_DELETE &&
979 pop3_session->state != POP3_LOGOUT) return 0;
981 if (!pop3_session->new_msg_exist) return 0;
983 cur_total = pop3_session->cur_total_bytes + cur_len;
984 if (cur_total > pop3_session->total_bytes)
985 cur_total = pop3_session->total_bytes;
986 inc_session->cur_total_bytes = cur_total;
988 inc_dialog = (IncProgressDialog *)inc_session->data;
989 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
994 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
996 IncSession *inc_session = (IncSession *)data;
997 IncProgressDialog *inc_dialog;
999 g_return_val_if_fail(inc_session != NULL, -1);
1001 inc_dialog = (IncProgressDialog *)inc_session->data;
1003 inc_recv_data_progressive(session, 0, 0, inc_session);
1005 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1006 inc_progress_dialog_update(inc_dialog, inc_session);
1012 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1014 IncSession *inc_session = (IncSession *)data;
1015 IncProgressDialog *inc_dialog;
1017 g_return_val_if_fail(inc_session != NULL, -1);
1019 inc_dialog = (IncProgressDialog *)inc_session->data;
1021 switch (POP3_SESSION(session)->state) {
1022 case POP3_GETAUTH_USER:
1023 case POP3_GETAUTH_PASS:
1024 case POP3_GETAUTH_APOP:
1025 case POP3_GETRANGE_STAT:
1026 case POP3_GETRANGE_LAST:
1027 case POP3_GETRANGE_UIDL:
1028 case POP3_GETSIZE_LIST:
1029 inc_progress_dialog_update(inc_dialog, inc_session);
1032 inc_recv_data_progressive(session, 0, 0, inc_session);
1035 inc_progress_dialog_update(inc_dialog, inc_session);
1044 static gint inc_drop_message(Pop3Session *session, const gchar *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, TRUE)) < 0) {
1078 static void inc_put_error(IncState istate, Pop3Session *session)
1080 gchar *log_msg = NULL;
1081 gchar *err_msg = NULL;
1082 gboolean fatal_error = FALSE;
1085 case INC_CONNECT_ERROR:
1086 log_msg = _("Connection failed.");
1087 if (prefs_common.no_recv_err_panel)
1089 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1090 SESSION(session)->server,
1091 SESSION(session)->port);
1094 log_msg = _("Error occurred while processing mail.");
1095 if (prefs_common.no_recv_err_panel)
1097 if (session->error_msg)
1098 err_msg = g_strdup_printf
1099 (_("Error occurred while processing mail:\n%s"),
1100 session->error_msg);
1102 err_msg = g_strdup(log_msg);
1105 log_msg = _("No disk space left.");
1106 err_msg = g_strdup(log_msg);
1110 log_msg = _("Can't write file.");
1111 err_msg = g_strdup(log_msg);
1114 case INC_SOCKET_ERROR:
1115 log_msg = _("Socket error.");
1116 if (prefs_common.no_recv_err_panel)
1118 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1119 SESSION(session)->server,
1120 SESSION(session)->port);
1123 log_msg = _("Connection closed by the remote host.");
1124 if (prefs_common.no_recv_err_panel)
1126 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1127 SESSION(session)->server,
1128 SESSION(session)->port);
1131 log_msg = _("Mailbox is locked.");
1132 if (prefs_common.no_recv_err_panel)
1134 if (session->error_msg)
1135 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1136 session->error_msg);
1138 err_msg = g_strdup(log_msg);
1140 case INC_AUTH_FAILED:
1141 log_msg = _("Authentication failed.");
1142 if (prefs_common.no_recv_err_panel)
1144 if (session->error_msg)
1145 err_msg = g_strdup_printf
1146 (_("Authentication failed:\n%s"), session->error_msg);
1148 err_msg = g_strdup(log_msg);
1151 log_msg = _("Session timed out.");
1152 if (prefs_common.no_recv_err_panel)
1154 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1155 SESSION(session)->server,
1156 SESSION(session)->port);
1164 log_error("%s\n", log_msg);
1166 log_warning("%s\n", log_msg);
1169 alertpanel_error_log(err_msg);
1174 static void inc_cancel(IncProgressDialog *dialog)
1176 IncSession *session;
1178 g_return_if_fail(dialog != NULL);
1180 if (dialog->queue_list == NULL) {
1181 inc_progress_dialog_destroy(dialog);
1185 session = dialog->queue_list->data;
1187 session->inc_state = INC_CANCEL;
1189 log_message(_("Incorporation cancelled\n"));
1192 gboolean inc_is_active(void)
1194 return (inc_dialog_list != NULL);
1197 void inc_cancel_all(void)
1201 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1202 inc_cancel((IncProgressDialog *)cur->data);
1205 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1207 inc_cancel((IncProgressDialog *)data);
1210 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1213 IncProgressDialog *dialog = (IncProgressDialog *)data;
1215 if (dialog->queue_list == NULL)
1216 inc_progress_dialog_destroy(dialog);
1221 static gint inc_spool_account(PrefsAccount *account)
1227 if (account->inbox) {
1228 inbox = folder_find_item_from_path(account->inbox);
1230 inbox = folder_get_default_inbox();
1232 inbox = folder_get_default_inbox();
1234 if (is_file_exist(account->local_mbox))
1235 mbox = g_strdup(account->local_mbox);
1236 else if (is_dir_exist(account->local_mbox))
1237 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1238 g_get_user_name(), NULL);
1240 debug_print("%s: local mailbox not found.\n",
1241 account->local_mbox);
1245 result = get_spool(inbox, mbox);
1248 statusbar_pop_all();
1253 static gint inc_all_spool(void)
1257 gint account_new_msgs = 0;
1259 list = account_get_list();
1260 if (!list) return 0;
1262 for (; list != NULL; list = list->next) {
1263 PrefsAccount *account = list->data;
1265 if ((account->protocol == A_LOCAL) &&
1266 (account->recv_at_getall)) {
1267 account_new_msgs = inc_spool_account(account);
1268 if (account_new_msgs > 0)
1269 new_msgs += account_new_msgs;
1276 static gint get_spool(FolderItem *dest, const gchar *mbox)
1280 gchar tmp_mbox[MAXPATHLEN + 1];
1282 g_return_val_if_fail(dest != NULL, -1);
1283 g_return_val_if_fail(mbox != NULL, -1);
1285 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1286 debug_print("%s: no messages in local mailbox.\n", mbox);
1288 } else if (size < 0)
1291 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1294 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1295 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1297 if (copy_mbox(mbox, tmp_mbox) < 0) {
1298 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1302 debug_print("Getting new messages from %s into %s...\n",
1305 msgs = proc_mbox(dest, tmp_mbox, TRUE);
1308 if (msgs >= 0) empty_mbox(mbox);
1309 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1319 void inc_unlock(void)
1321 if (inc_lock_count > 0)
1325 static guint autocheck_timer = 0;
1326 static gpointer autocheck_data = NULL;
1328 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1331 gchar *buf, *numpos;
1333 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1334 *prefs_common.newmail_notify_cmd))
1336 buf = g_strdup(prefs_common.newmail_notify_cmd);
1337 if ((numpos = strstr(buf, "%d")) != NULL) {
1341 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1346 debug_print("executing new mail notification command: %s\n", buf);
1347 execute_command_line(buf, TRUE);
1352 void inc_autocheck_timer_init(MainWindow *mainwin)
1354 autocheck_data = mainwin;
1355 inc_autocheck_timer_set();
1358 static void inc_autocheck_timer_set_interval(guint interval)
1360 inc_autocheck_timer_remove();
1361 /* last test is to avoid re-enabling auto_check after modifying
1362 the common preferences */
1363 if (prefs_common.autochk_newmail && autocheck_data
1364 && prefs_common.work_offline == FALSE) {
1365 autocheck_timer = gtk_timeout_add
1366 (interval, inc_autocheck_func, autocheck_data);
1367 debug_print("added timer = %d\n", autocheck_timer);
1371 void inc_autocheck_timer_set(void)
1373 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1376 void inc_autocheck_timer_remove(void)
1378 if (autocheck_timer) {
1379 debug_print("removed timer = %d\n", autocheck_timer);
1380 gtk_timeout_remove(autocheck_timer);
1381 autocheck_timer = 0;
1385 static gint inc_autocheck_func(gpointer data)
1387 MainWindow *mainwin = (MainWindow *)data;
1389 if (inc_lock_count) {
1390 debug_print("autocheck is locked.\n");
1391 inc_autocheck_timer_set_interval(1000);
1395 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);