2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkprogressbar.h>
38 #include "mainwindow.h"
39 #include "folderview.h"
40 #include "summaryview.h"
41 #include "prefs_common.h"
42 #include "prefs_account.h"
52 #include "statusbar.h"
54 #include "manage_window.h"
55 #include "stock_pixmap.h"
56 #include "progressdialog.h"
57 #include "inputdialog.h"
58 #include "alertpanel.h"
60 #include "filtering.h"
64 static GList *inc_dialog_list = NULL;
66 static guint inc_lock_count = 0;
68 static GdkPixbuf *currentpix;
69 static GdkPixbuf *errorpix;
70 static GdkPixbuf *okpix;
72 #define MSGBUFSIZE 8192
74 static void inc_finished (MainWindow *mainwin,
75 gboolean new_messages);
76 static gint inc_account_mail_real (MainWindow *mainwin,
77 PrefsAccount *account);
79 static IncProgressDialog *inc_progress_dialog_create
81 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
82 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
84 static IncSession *inc_session_new (PrefsAccount *account);
85 static void inc_session_destroy (IncSession *session);
86 static gint inc_start (IncProgressDialog *inc_dialog);
87 static IncState inc_pop3_session_do (IncSession *session);
89 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
90 IncSession *inc_session);
92 static void inc_progress_dialog_set_label
93 (IncProgressDialog *inc_dialog,
94 IncSession *inc_session);
95 static void inc_progress_dialog_set_progress
96 (IncProgressDialog *inc_dialog,
97 IncSession *inc_session);
99 static void inc_progress_dialog_update_periodic
100 (IncProgressDialog *inc_dialog,
101 IncSession *inc_session);
103 static gint inc_recv_data_progressive (Session *session,
107 static gint inc_recv_data_finished (Session *session,
110 static gint inc_recv_message (Session *session,
113 static gint inc_drop_message (Pop3Session *session,
116 static void inc_put_error (IncState istate,
117 Pop3Session *session);
119 static void inc_cancel_cb (GtkWidget *widget,
121 static gint inc_dialog_delete_cb (GtkWidget *widget,
125 static gint get_spool (FolderItem *dest,
128 static gint inc_spool_account(PrefsAccount *account);
129 static gint inc_all_spool(void);
130 static void inc_autocheck_timer_set_interval (guint interval);
131 static gint inc_autocheck_func (gpointer data);
133 static void inc_notify_cmd (gint new_msgs,
138 * @mainwin: Main window.
139 * @new_messages: TRUE if some messages have been received.
141 * Update the folder view and the summary view after receiving
142 * messages. If @new_messages is FALSE, this function avoids unneeded
145 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
149 if (prefs_common.scan_all_after_inc)
150 folderview_check_new(NULL);
152 if (!new_messages && !prefs_common.scan_all_after_inc) return;
154 if (prefs_common.open_inbox_on_inc) {
155 item = cur_account && cur_account->inbox
156 ? folder_find_item_from_identifier(cur_account->inbox)
157 : folder_get_default_inbox();
158 folderview_unselect(mainwin->folderview);
159 folderview_select(mainwin->folderview, item);
163 void inc_mail(MainWindow *mainwin, gboolean notify)
166 gint account_new_msgs = 0;
168 if (inc_lock_count) return;
170 if (prefs_common.work_offline &&
171 !inc_offline_should_override(
172 _("Sylpheed-Claws needs network access in order "
177 inc_autocheck_timer_remove();
178 main_window_lock(mainwin);
180 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
181 /* external incorporating program */
182 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
183 main_window_unlock(mainwin);
184 inc_autocheck_timer_set();
188 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
189 if (account_new_msgs > 0)
190 new_msgs += account_new_msgs;
193 inc_finished(mainwin, new_msgs > 0);
194 main_window_unlock(mainwin);
195 inc_notify_cmd(new_msgs, notify);
196 inc_autocheck_timer_set();
200 void inc_pop_before_smtp(PrefsAccount *acc)
202 IncProgressDialog *inc_dialog;
206 mainwin = mainwindow_get_mainwindow();
208 session = inc_session_new(acc);
209 if (!session) return;
210 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
212 inc_dialog = inc_progress_dialog_create(FALSE);
213 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
215 /* FIXME: assumes to attach to first main window */
216 inc_dialog->mainwin = mainwin;
217 inc_progress_dialog_set_list(inc_dialog);
220 toolbar_main_set_sensitive(mainwin);
221 main_window_set_menu_sensitive(mainwin);
224 inc_start(inc_dialog);
227 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
229 IncProgressDialog *inc_dialog;
232 switch (account->protocol) {
236 * FIXME: it should return foldeview_check_new() value.
237 * TODO: do it when bug [19] is fixed (IMAP folder sets
238 * an incorrect new message count)
240 folderview_check_new(FOLDER(account->folder));
244 session = inc_session_new(account);
245 if (!session) return 0;
247 inc_dialog = inc_progress_dialog_create(FALSE);
248 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
250 inc_dialog->mainwin = mainwin;
251 inc_progress_dialog_set_list(inc_dialog);
254 toolbar_main_set_sensitive(mainwin);
255 main_window_set_menu_sensitive(mainwin);
258 return inc_start(inc_dialog);
261 return inc_spool_account(account);
269 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
273 if (inc_lock_count) return 0;
275 if (prefs_common.work_offline &&
276 !inc_offline_should_override(
277 _("Sylpheed-Claws needs network access in order "
281 inc_autocheck_timer_remove();
282 main_window_lock(mainwin);
284 new_msgs = inc_account_mail_real(mainwin, account);
286 inc_finished(mainwin, new_msgs > 0);
287 main_window_unlock(mainwin);
288 inc_autocheck_timer_set();
293 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
296 GList *list, *queue_list = NULL;
297 IncProgressDialog *inc_dialog;
299 gint account_new_msgs = 0;
301 if (prefs_common.work_offline &&
302 !inc_offline_should_override(
303 _("Sylpheed-Claws needs network access in order "
307 if (inc_lock_count) return;
309 inc_autocheck_timer_remove();
310 main_window_lock(mainwin);
312 list = account_get_list();
314 inc_finished(mainwin, new_msgs > 0);
315 main_window_unlock(mainwin);
316 inc_notify_cmd(new_msgs, notify);
317 inc_autocheck_timer_set();
321 /* check local folders */
322 account_new_msgs = inc_all_spool();
323 if (account_new_msgs > 0)
324 new_msgs += account_new_msgs;
326 /* check IMAP4 / News folders */
327 for (list = account_get_list(); list != NULL; list = list->next) {
328 PrefsAccount *account = list->data;
329 if ((account->protocol == A_IMAP4 ||
330 account->protocol == A_NNTP) && account->recv_at_getall) {
331 new_msgs += folderview_check_new(FOLDER(account->folder));
335 /* check POP3 accounts */
336 for (list = account_get_list(); list != NULL; list = list->next) {
338 PrefsAccount *account = list->data;
340 if (account->recv_at_getall) {
341 session = inc_session_new(account);
343 queue_list = g_list_append(queue_list, session);
348 inc_dialog = inc_progress_dialog_create(autocheck);
349 inc_dialog->queue_list = queue_list;
350 inc_dialog->mainwin = mainwin;
351 inc_progress_dialog_set_list(inc_dialog);
353 toolbar_main_set_sensitive(mainwin);
354 main_window_set_menu_sensitive(mainwin);
355 new_msgs += inc_start(inc_dialog);
358 inc_finished(mainwin, new_msgs > 0);
359 main_window_unlock(mainwin);
360 inc_notify_cmd(new_msgs, notify);
361 inc_autocheck_timer_set();
364 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
366 IncProgressDialog *dialog;
367 ProgressDialog *progress;
369 dialog = g_new0(IncProgressDialog, 1);
371 progress = progress_dialog_create();
372 gtk_window_set_title(GTK_WINDOW(progress->window),
373 _("Retrieving new messages"));
374 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
375 G_CALLBACK(inc_cancel_cb), dialog);
376 g_signal_connect(G_OBJECT(progress->window), "delete_event",
377 G_CALLBACK(inc_dialog_delete_cb), dialog);
378 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
380 progress_dialog_get_fraction(progress);
382 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_COMPLETE,
384 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_CONTINUE,
386 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_ERROR,
389 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
390 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
392 dialog->show_dialog = TRUE;
393 gtk_widget_show_now(progress->window);
396 dialog->dialog = progress;
397 g_get_current_time(&dialog->progress_tv);
398 g_get_current_time(&dialog->folder_tv);
399 dialog->queue_list = NULL;
402 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
407 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
411 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
412 IncSession *session = list->data;
413 Pop3Session *pop3_session = POP3_SESSION(session->session);
415 session->data = inc_dialog;
417 progress_dialog_list_set(inc_dialog->dialog,
419 pop3_session->ac_prefs->account_name,
424 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
426 progress_dialog_get_fraction(inc_dialog->dialog);
427 progress_dialog_set_label(inc_dialog->dialog, "");
428 if (inc_dialog->mainwin)
429 main_window_progress_off(inc_dialog->mainwin);
432 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
434 g_return_if_fail(inc_dialog != NULL);
436 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
438 if (inc_dialog->mainwin)
439 main_window_progress_off(inc_dialog->mainwin);
440 progress_dialog_destroy(inc_dialog->dialog);
445 static IncSession *inc_session_new(PrefsAccount *account)
449 g_return_val_if_fail(account != NULL, NULL);
451 if (account->protocol != A_POP3)
453 if (!account->recv_server || !account->userid)
456 session = g_new0(IncSession, 1);
458 session->session = pop3_session_new(account);
459 session->session->data = session;
460 POP3_SESSION(session->session)->drop_message = inc_drop_message;
461 session_set_recv_message_notify(session->session,
462 inc_recv_message, session);
463 session_set_recv_data_progressive_notify(session->session,
464 inc_recv_data_progressive,
466 session_set_recv_data_notify(session->session,
467 inc_recv_data_finished, session);
472 static void inc_session_destroy(IncSession *session)
474 g_return_if_fail(session != NULL);
476 session_destroy(session->session);
480 static gint inc_start(IncProgressDialog *inc_dialog)
484 Pop3Session *pop3_session;
490 FolderItem *processing, *inbox;
492 GSList *msglist, *msglist_element;
493 gboolean cancelled = FALSE;
495 qlist = inc_dialog->queue_list;
496 while (qlist != NULL) {
497 GList *next = qlist->next;
499 session = qlist->data;
500 pop3_session = POP3_SESSION(session->session);
501 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
502 if (pop3_session->ac_prefs->passwd)
504 g_strdup(pop3_session->ac_prefs->passwd);
505 else if (pop3_session->ac_prefs->tmp_pass)
507 g_strdup(pop3_session->ac_prefs->tmp_pass);
511 if (inc_dialog->show_dialog)
512 manage_window_focus_in
513 (inc_dialog->dialog->window,
516 pass = input_dialog_query_password
517 (pop3_session->ac_prefs->recv_server,
520 if (inc_dialog->show_dialog)
521 manage_window_focus_out
522 (inc_dialog->dialog->window,
526 pop3_session->ac_prefs->tmp_pass =
528 pop3_session->pass = pass;
535 #define SET_PIXMAP_AND_TEXT(pix, str) \
537 progress_dialog_list_set(inc_dialog->dialog, \
538 inc_dialog->cur_row, \
544 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
545 int cur = 0, total = 0;
546 session = inc_dialog->queue_list->data;
547 pop3_session = POP3_SESSION(session->session);
549 if (pop3_session->pass == NULL) {
550 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
551 inc_session_destroy(session);
552 inc_dialog->queue_list =
553 g_list_remove(inc_dialog->queue_list, session);
557 inc_progress_dialog_clear(inc_dialog);
558 progress_dialog_scroll_to_row(inc_dialog->dialog,
559 inc_dialog->cur_row);
561 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
563 /* begin POP3 session */
564 inc_state = inc_pop3_session_do(session);
568 if (pop3_session->cur_total_num > 0)
569 msg = g_strdup_printf(
570 ngettext("Done (%d message (%s) received)",
571 "Done (%d messages (%s) received)",
572 pop3_session->cur_total_num),
573 pop3_session->cur_total_num,
574 to_human_readable(pop3_session->cur_total_recv_bytes));
576 msg = g_strdup_printf(_("Done (no new messages)"));
577 SET_PIXMAP_AND_TEXT(okpix, msg);
580 case INC_CONNECT_ERROR:
581 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
583 case INC_AUTH_FAILED:
584 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
587 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
592 case INC_SOCKET_ERROR:
594 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
597 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
600 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
601 if (!inc_dialog->show_dialog)
608 if (pop3_session->error_val == PS_AUTHFAIL) {
609 if(!prefs_common.no_recv_err_panel) {
610 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
611 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
612 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
616 /* CLAWS: perform filtering actions on dropped message */
617 /* CLAWS: get default inbox (perhaps per account) */
618 if (pop3_session->ac_prefs->inbox) {
619 /* CLAWS: get destination folder / mailbox */
620 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
622 inbox = folder_get_default_inbox();
624 inbox = folder_get_default_inbox();
626 /* get list of messages in processing */
627 processing = folder_get_default_processing();
628 folder_item_scan(processing);
629 msglist = folder_item_get_msg_list(processing);
631 /* process messages */
632 folder_item_update_freeze();
633 if (pop3_session->ac_prefs->filter_on_recv)
634 statusbar_print_all(_("Filtering messages...\n"));
635 total = g_slist_length(msglist);
637 for(msglist_element = msglist; msglist_element != NULL;
638 msglist_element = msglist_element->next) {
640 msginfo = (MsgInfo *) msglist_element->data;
641 filename = folder_item_fetch_msg(processing, msginfo->msgnum);
644 if (pop3_session->ac_prefs->filter_on_recv)
645 statusbar_progress_all(cur++,total, prefs_common.statusbar_update_step);
647 if (!pop3_session->ac_prefs->filter_on_recv ||
648 !procmsg_msginfo_filter(msginfo))
649 folder_item_move_msg(inbox, msginfo);
651 filtering_move_and_copy_msgs(msglist);
652 for(msglist_element = msglist; msglist_element != NULL;
653 msglist_element = msglist_element->next) {
654 MsgInfo *msginfo = (MsgInfo *)msglist_element->data;
655 procmsg_msginfo_free(msginfo);
657 folder_item_update_thaw();
659 statusbar_progress_all(0,0,0);
662 g_slist_free(msglist);
666 new_msgs += pop3_session->cur_total_num;
668 if (pop3_session->error_val == PS_AUTHFAIL &&
669 pop3_session->ac_prefs->tmp_pass) {
670 g_free(pop3_session->ac_prefs->tmp_pass);
671 pop3_session->ac_prefs->tmp_pass = NULL;
674 pop3_write_uidl_list(pop3_session);
676 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
678 if (inc_dialog->show_dialog)
679 manage_window_focus_in
680 (inc_dialog->dialog->window,
682 inc_put_error(inc_state, pop3_session);
683 if (inc_dialog->show_dialog)
684 manage_window_focus_out
685 (inc_dialog->dialog->window,
687 if (inc_state == INC_NO_SPACE ||
688 inc_state == INC_IO_ERROR)
691 folder_item_free_cache(processing);
693 inc_session_destroy(session);
694 inc_dialog->queue_list =
695 g_list_remove(inc_dialog->queue_list, session);
698 #undef SET_PIXMAP_AND_TEXT
701 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
702 "Finished (%d new messages)",
703 new_msgs), new_msgs);
705 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
707 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
709 while (inc_dialog->queue_list != NULL) {
710 session = inc_dialog->queue_list->data;
711 inc_session_destroy(session);
712 inc_dialog->queue_list =
713 g_list_remove(inc_dialog->queue_list, session);
716 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
717 inc_progress_dialog_destroy(inc_dialog);
719 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
721 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
730 static IncState inc_pop3_session_do(IncSession *session)
732 Pop3Session *pop3_session = POP3_SESSION(session->session);
733 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
738 debug_print("getting new messages of account %s...\n",
739 pop3_session->ac_prefs->account_name);
741 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
743 buf = g_strdup_printf(_("%s: Retrieving new messages"),
744 pop3_session->ac_prefs->recv_server);
745 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
748 server = pop3_session->ac_prefs->recv_server;
750 port = pop3_session->ac_prefs->set_popport ?
751 pop3_session->ac_prefs->popport :
752 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
753 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
754 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
755 SESSION(pop3_session)->nonblocking =
756 pop3_session->ac_prefs->use_nonblocking_ssl;
758 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE) {
759 if (alertpanel_full(_("Insecure connection"),
760 _("This connection is configured to be secured "
761 "using SSL, but SSL is not available in this "
762 "build of Sylpheed-Claws. \n\n"
763 "Do you want to continue connecting to this "
764 "server? The communication would not be "
766 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
767 NULL, FALSE, NULL, ALERT_WARNING,
768 G_ALERTDEFAULT) != G_ALERTALTERNATE)
771 port = pop3_session->ac_prefs->set_popport ?
772 pop3_session->ac_prefs->popport : 110;
775 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
776 log_message("%s\n", buf);
778 progress_dialog_set_label(inc_dialog->dialog, buf);
781 session_set_timeout(SESSION(pop3_session),
782 prefs_common.io_timeout_secs * 1000);
784 if (session_connect(SESSION(pop3_session), server, port) < 0) {
785 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
787 if(!prefs_common.no_recv_err_panel) {
788 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
789 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
790 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
792 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
794 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
796 session->inc_state = INC_CONNECT_ERROR;
798 return INC_CONNECT_ERROR;
801 while (session_is_connected(SESSION(pop3_session)) &&
802 session->inc_state != INC_CANCEL)
803 gtk_main_iteration();
805 if (session->inc_state == INC_SUCCESS) {
806 switch (pop3_session->error_val) {
808 switch (SESSION(pop3_session)->state) {
810 if (pop3_session->state == POP3_READY)
811 session->inc_state = INC_CONNECT_ERROR;
813 session->inc_state = INC_ERROR;
816 session->inc_state = INC_EOF;
818 case SESSION_TIMEOUT:
819 session->inc_state = INC_TIMEOUT;
822 session->inc_state = INC_SUCCESS;
827 session->inc_state = INC_AUTH_FAILED;
830 session->inc_state = INC_IO_ERROR;
833 session->inc_state = INC_SOCKET_ERROR;
836 session->inc_state = INC_LOCKED;
839 session->inc_state = INC_ERROR;
844 session_disconnect(SESSION(pop3_session));
847 return session->inc_state;
850 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
851 IncSession *inc_session)
853 inc_progress_dialog_set_label(inc_dialog, inc_session);
854 inc_progress_dialog_set_progress(inc_dialog, inc_session);
857 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
858 IncSession *inc_session)
860 ProgressDialog *dialog = inc_dialog->dialog;
861 Pop3Session *session;
863 g_return_if_fail(inc_session != NULL);
865 session = POP3_SESSION(inc_session->session);
867 switch (session->state) {
870 case POP3_GETAUTH_USER:
871 case POP3_GETAUTH_PASS:
872 case POP3_GETAUTH_APOP:
873 progress_dialog_set_label(dialog, _("Authenticating..."));
874 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
875 SESSION(session)->server,
876 session->ac_prefs->account_name);
878 case POP3_GETRANGE_STAT:
879 progress_dialog_set_label
880 (dialog, _("Getting the number of new messages (STAT)..."));
882 case POP3_GETRANGE_LAST:
883 progress_dialog_set_label
884 (dialog, _("Getting the number of new messages (LAST)..."));
886 case POP3_GETRANGE_UIDL:
887 progress_dialog_set_label
888 (dialog, _("Getting the number of new messages (UIDL)..."));
890 case POP3_GETSIZE_LIST:
891 progress_dialog_set_label
892 (dialog, _("Getting the size of messages (LIST)..."));
899 progress_dialog_set_label(dialog, _("Quitting"));
906 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
907 IncSession *inc_session)
909 gchar buf[MSGBUFSIZE];
910 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
911 gchar *total_size_str;
915 if (!pop3_session->new_msg_exist) return;
917 cur_total = inc_session->cur_total_bytes;
918 total = pop3_session->total_bytes;
919 if (pop3_session->state == POP3_RETR ||
920 pop3_session->state == POP3_RETR_RECV ||
921 pop3_session->state == POP3_DELETE) {
922 Xstrdup_a(total_size_str, to_human_readable(total), return);
923 g_snprintf(buf, sizeof(buf),
924 _("Retrieving message (%d / %d) (%s / %s)"),
925 pop3_session->cur_msg, pop3_session->count,
926 to_human_readable(cur_total), total_size_str);
927 progress_dialog_set_label(inc_dialog->dialog, buf);
930 progress_dialog_set_fraction
931 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
933 g_snprintf(buf, sizeof(buf), "%d / %d",
934 pop3_session->cur_msg, pop3_session->count);
935 gtk_progress_bar_set_text
936 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), buf);
937 gtk_progress_bar_set_fraction
938 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
939 (total == 0) ? 0 : (gfloat)cur_total / (gfloat)total);
941 if (pop3_session->cur_total_num > 0) {
942 g_snprintf(buf, sizeof(buf),
943 ngettext("Retrieving (%d message (%s) received)",
944 "Retrieving (%d messages (%s) received)",
945 pop3_session->cur_total_num),
946 pop3_session->cur_total_num,
948 (pop3_session->cur_total_recv_bytes));
949 progress_dialog_list_set_status(inc_dialog->dialog,
955 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
956 IncSession *inc_session)
962 g_get_current_time(&tv_cur);
964 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
965 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
966 if (tv_result.tv_usec < 0) {
968 tv_result.tv_usec += G_USEC_PER_SEC;
971 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
972 if (msec > PROGRESS_UPDATE_INTERVAL) {
973 inc_progress_dialog_update(inc_dialog, inc_session);
974 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
975 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
979 static gint inc_recv_data_progressive(Session *session, guint cur_len,
980 guint total_len, gpointer data)
982 IncSession *inc_session = (IncSession *)data;
983 Pop3Session *pop3_session = POP3_SESSION(session);
984 IncProgressDialog *inc_dialog;
987 g_return_val_if_fail(inc_session != NULL, -1);
989 if (pop3_session->state != POP3_RETR &&
990 pop3_session->state != POP3_RETR_RECV &&
991 pop3_session->state != POP3_DELETE &&
992 pop3_session->state != POP3_LOGOUT) return 0;
994 if (!pop3_session->new_msg_exist) return 0;
996 cur_total = pop3_session->cur_total_bytes + cur_len;
997 if (cur_total > pop3_session->total_bytes)
998 cur_total = pop3_session->total_bytes;
999 inc_session->cur_total_bytes = cur_total;
1001 inc_dialog = (IncProgressDialog *)inc_session->data;
1002 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1007 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1009 IncSession *inc_session = (IncSession *)data;
1010 IncProgressDialog *inc_dialog;
1012 g_return_val_if_fail(inc_session != NULL, -1);
1014 inc_dialog = (IncProgressDialog *)inc_session->data;
1016 inc_recv_data_progressive(session, 0, 0, inc_session);
1018 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1019 inc_progress_dialog_update(inc_dialog, inc_session);
1025 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1027 IncSession *inc_session = (IncSession *)data;
1028 IncProgressDialog *inc_dialog;
1030 g_return_val_if_fail(inc_session != NULL, -1);
1032 inc_dialog = (IncProgressDialog *)inc_session->data;
1034 switch (POP3_SESSION(session)->state) {
1035 case POP3_GETAUTH_USER:
1036 case POP3_GETAUTH_PASS:
1037 case POP3_GETAUTH_APOP:
1038 case POP3_GETRANGE_STAT:
1039 case POP3_GETRANGE_LAST:
1040 case POP3_GETRANGE_UIDL:
1041 case POP3_GETSIZE_LIST:
1042 inc_progress_dialog_update(inc_dialog, inc_session);
1045 inc_recv_data_progressive(session, 0, 0, inc_session);
1048 inc_progress_dialog_update(inc_dialog, inc_session);
1057 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1060 FolderItem *dropfolder;
1061 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1064 g_return_val_if_fail(inc_session != NULL, -1);
1066 if (session->ac_prefs->inbox) {
1067 inbox = folder_find_item_from_identifier
1068 (session->ac_prefs->inbox);
1070 inbox = folder_get_default_inbox();
1072 inbox = folder_get_default_inbox();
1078 /* CLAWS: claws uses a global .processing folder for the filtering. */
1079 dropfolder = folder_get_default_processing();
1081 /* add msg file to drop folder */
1082 if ((msgnum = folder_item_add_msg(
1083 dropfolder, file, NULL, TRUE)) < 0) {
1091 static void inc_put_error(IncState istate, Pop3Session *session)
1093 gchar *log_msg = NULL;
1094 gchar *err_msg = NULL;
1095 gboolean fatal_error = FALSE;
1098 case INC_CONNECT_ERROR:
1099 log_msg = _("Connection failed.");
1100 if (prefs_common.no_recv_err_panel)
1102 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1103 SESSION(session)->server,
1104 SESSION(session)->port);
1107 log_msg = _("Error occurred while processing mail.");
1108 if (prefs_common.no_recv_err_panel)
1110 if (session->error_msg)
1111 err_msg = g_strdup_printf
1112 (_("Error occurred while processing mail:\n%s"),
1113 session->error_msg);
1115 err_msg = g_strdup(log_msg);
1118 log_msg = _("No disk space left.");
1119 err_msg = g_strdup(log_msg);
1123 log_msg = _("Can't write file.");
1124 err_msg = g_strdup(log_msg);
1127 case INC_SOCKET_ERROR:
1128 log_msg = _("Socket error.");
1129 if (prefs_common.no_recv_err_panel)
1131 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1132 SESSION(session)->server,
1133 SESSION(session)->port);
1136 log_msg = _("Connection closed by the remote host.");
1137 if (prefs_common.no_recv_err_panel)
1139 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1140 SESSION(session)->server,
1141 SESSION(session)->port);
1144 log_msg = _("Mailbox is locked.");
1145 if (prefs_common.no_recv_err_panel)
1147 if (session->error_msg)
1148 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1149 session->error_msg);
1151 err_msg = g_strdup(log_msg);
1153 case INC_AUTH_FAILED:
1154 log_msg = _("Authentication failed.");
1155 if (prefs_common.no_recv_err_panel)
1157 if (session->error_msg)
1158 err_msg = g_strdup_printf
1159 (_("Authentication failed:\n%s"), session->error_msg);
1161 err_msg = g_strdup(log_msg);
1164 log_msg = _("Session timed out.");
1165 if (prefs_common.no_recv_err_panel)
1167 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1168 SESSION(session)->server,
1169 SESSION(session)->port);
1177 log_error("%s\n", log_msg);
1179 log_warning("%s\n", log_msg);
1182 alertpanel_error_log(err_msg);
1187 static void inc_cancel(IncProgressDialog *dialog)
1189 IncSession *session;
1191 g_return_if_fail(dialog != NULL);
1193 if (dialog->queue_list == NULL) {
1194 inc_progress_dialog_destroy(dialog);
1198 session = dialog->queue_list->data;
1200 session->inc_state = INC_CANCEL;
1202 log_message(_("Incorporation cancelled\n"));
1205 gboolean inc_is_active(void)
1207 return (inc_dialog_list != NULL);
1210 void inc_cancel_all(void)
1214 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1215 inc_cancel((IncProgressDialog *)cur->data);
1218 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1220 inc_cancel((IncProgressDialog *)data);
1223 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1226 IncProgressDialog *dialog = (IncProgressDialog *)data;
1228 if (dialog->queue_list == NULL)
1229 inc_progress_dialog_destroy(dialog);
1234 static gint inc_spool_account(PrefsAccount *account)
1240 if (account->local_inbox) {
1241 inbox = folder_find_item_from_identifier(account->local_inbox);
1243 inbox = folder_get_default_inbox();
1245 inbox = folder_get_default_inbox();
1247 if (is_file_exist(account->local_mbox))
1248 mbox = g_strdup(account->local_mbox);
1249 else if (is_dir_exist(account->local_mbox))
1250 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1251 g_get_user_name(), NULL);
1253 debug_print("%s: local mailbox not found.\n",
1254 account->local_mbox);
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("%s: no messages in local mailbox.\n", mbox);
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)
1344 gchar *buf, *numpos, *ret_str;
1345 gssize by_read = 0, by_written = 0;
1347 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1348 *prefs_common.newmail_notify_cmd))
1350 buf = g_strdup(prefs_common.newmail_notify_cmd);
1351 if ((numpos = strstr(buf, "%d")) != NULL) {
1355 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1360 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1362 if (ret_str && by_written) {
1366 debug_print("executing new mail notification command: %s\n", buf);
1367 execute_command_line(buf, TRUE);
1372 void inc_autocheck_timer_init(MainWindow *mainwin)
1374 autocheck_data = mainwin;
1375 inc_autocheck_timer_set();
1378 static void inc_autocheck_timer_set_interval(guint interval)
1380 inc_autocheck_timer_remove();
1381 /* last test is to avoid re-enabling auto_check after modifying
1382 the common preferences */
1383 if (prefs_common.autochk_newmail && autocheck_data
1384 && prefs_common.work_offline == FALSE) {
1385 autocheck_timer = gtk_timeout_add
1386 (interval, inc_autocheck_func, autocheck_data);
1387 debug_print("added timer = %d\n", autocheck_timer);
1391 void inc_autocheck_timer_set(void)
1393 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1396 void inc_autocheck_timer_remove(void)
1398 if (autocheck_timer) {
1399 debug_print("removed timer = %d\n", autocheck_timer);
1400 gtk_timeout_remove(autocheck_timer);
1401 autocheck_timer = 0;
1405 static gint inc_autocheck_func(gpointer data)
1407 MainWindow *mainwin = (MainWindow *)data;
1409 if (inc_lock_count) {
1410 debug_print("autocheck is locked.\n");
1411 inc_autocheck_timer_set_interval(1000);
1415 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1420 gboolean inc_offline_should_override(const gchar *msg)
1422 static time_t overridden_yes = 0;
1423 static time_t overridden_no = 0;
1424 int length = 10; /* minutes */
1425 gint answer = G_ALERTDEFAULT;
1427 if (prefs_common.autochk_newmail)
1428 length = prefs_common.autochk_itv; /* minutes */
1430 if (prefs_common.work_offline) {
1433 if (time(NULL) - overridden_yes < length * 60) /* seconds */
1435 else if (time(NULL) - overridden_no < 3) /* seconds */
1438 tmp = g_strdup_printf(
1439 _("%s%sYou're working offline. Override for %d minutes?"),
1444 answer = alertpanel(_("Offline warning"),
1446 GTK_STOCK_NO, "+" GTK_STOCK_YES, _("On_ly once"));
1448 if (answer == G_ALERTALTERNATE) {
1449 overridden_yes = time(NULL);
1451 } else if (answer == G_ALERTDEFAULT) {
1452 overridden_no = time(NULL);
1455 overridden_yes = (time_t)0;
1456 overridden_no = (time_t)0;