2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2013 Hiroyuki Yamamoto and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
36 #include "mainwindow.h"
37 #include "folderview.h"
38 #include "summaryview.h"
39 #include "prefs_common.h"
40 #include "prefs_account.h"
50 #include "statusbar.h"
52 #include "manage_window.h"
53 #include "stock_pixmap.h"
54 #include "progressdialog.h"
55 #include "inputdialog.h"
56 #include "alertpanel.h"
58 #include "filtering.h"
61 #include "logwindow.h"
63 extern SessionStats session_stats;
65 static GList *inc_dialog_list = NULL;
67 guint inc_lock_count = 0;
69 static GdkPixbuf *currentpix;
70 static GdkPixbuf *errorpix;
71 static GdkPixbuf *okpix;
73 #define MESSAGEBUFSIZE 8192
75 static void inc_update_stats(gint new_msgs);
76 static void inc_finished (MainWindow *mainwin,
77 gboolean new_messages,
79 static gint inc_account_mail_real (MainWindow *mainwin,
80 PrefsAccount *account);
82 static IncProgressDialog *inc_progress_dialog_create
84 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
85 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
87 static IncSession *inc_session_new (PrefsAccount *account);
88 static void inc_session_destroy (IncSession *session);
89 static gint inc_start (IncProgressDialog *inc_dialog);
90 static IncState inc_pop3_session_do (IncSession *session);
92 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
93 IncSession *inc_session);
95 static void inc_progress_dialog_set_label
96 (IncProgressDialog *inc_dialog,
97 IncSession *inc_session);
98 static void inc_progress_dialog_set_progress
99 (IncProgressDialog *inc_dialog,
100 IncSession *inc_session);
102 static void inc_progress_dialog_update_periodic
103 (IncProgressDialog *inc_dialog,
104 IncSession *inc_session);
106 static gint inc_recv_data_progressive (Session *session,
110 static gint inc_recv_data_finished (Session *session,
113 static gint inc_recv_message (Session *session,
116 static gint inc_drop_message (Pop3Session *session,
119 static void inc_put_error (IncState istate,
120 Pop3Session *session);
122 static void inc_showlog_cb (GtkWidget *widget,
124 static void inc_cancel_cb (GtkWidget *widget,
126 static gint inc_dialog_delete_cb (GtkWidget *widget,
130 static gint get_spool (FolderItem *dest,
132 PrefsAccount *account);
134 static gint inc_spool_account(PrefsAccount *account);
135 static gint inc_all_spool(void);
136 static void inc_autocheck_timer_set_interval (guint interval);
137 static gint inc_autocheck_func (gpointer data);
139 static void inc_notify_cmd (gint new_msgs,
142 static void inc_update_stats(gint new_msgs)
144 /* update session statistics */
145 session_stats.received += new_msgs;
150 * @mainwin: Main window.
151 * @new_messages: TRUE if some messages have been received.
153 * Update the folder view and the summary view after receiving
154 * messages. If @new_messages is FALSE, this function avoids unneeded
157 static void inc_finished(MainWindow *mainwin, gboolean new_messages, gboolean autocheck)
159 if (prefs_common.scan_all_after_inc)
160 folderview_check_new(NULL);
162 if (!autocheck && new_messages && prefs_common.open_inbox_on_inc) {
163 FolderItem *item = NULL;
165 if (cur_account && cur_account->inbox)
166 item = folder_find_item_from_identifier(cur_account->inbox);
167 if (item == NULL && cur_account && cur_account->folder)
168 item = cur_account->folder->inbox;
170 item = folder_get_default_inbox();
172 folderview_unselect(mainwin->folderview);
173 folderview_select(mainwin->folderview, item);
177 void inc_mail(MainWindow *mainwin, gboolean notify)
180 gint account_new_msgs = 0;
182 if (inc_lock_count) return;
184 if (prefs_common.work_offline &&
185 !inc_offline_should_override(TRUE,
186 _("Claws Mail needs network access in order "
191 inc_autocheck_timer_remove();
192 main_window_lock(mainwin);
194 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
195 /* external incorporating program */
196 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
197 main_window_unlock(mainwin);
198 inc_autocheck_timer_set();
203 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
204 if (account_new_msgs > 0)
205 new_msgs += account_new_msgs;
208 inc_update_stats(new_msgs);
209 inc_finished(mainwin, new_msgs > 0, FALSE);
210 main_window_unlock(mainwin);
211 inc_notify_cmd(new_msgs, notify);
212 inc_autocheck_timer_set();
216 void inc_pop_before_smtp(PrefsAccount *acc)
218 IncProgressDialog *inc_dialog;
222 mainwin = mainwindow_get_mainwindow();
224 session = inc_session_new(acc);
225 if (!session) return;
226 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
228 inc_dialog = inc_progress_dialog_create(FALSE);
229 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
231 /* FIXME: assumes to attach to first main window */
232 inc_dialog->mainwin = mainwin;
233 inc_progress_dialog_set_list(inc_dialog);
236 toolbar_main_set_sensitive(mainwin);
237 main_window_set_menu_sensitive(mainwin);
240 inc_start(inc_dialog);
243 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
245 IncProgressDialog *inc_dialog;
248 switch (account->protocol) {
252 * FIXME: it should return foldeview_check_new() value.
253 * TODO: do it when bug [19] is fixed (IMAP folder sets
254 * an incorrect new message count)
256 folderview_check_new(FOLDER(account->folder));
260 session = inc_session_new(account);
261 if (!session) return 0;
263 inc_dialog = inc_progress_dialog_create(FALSE);
264 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
266 inc_dialog->mainwin = mainwin;
267 inc_progress_dialog_set_list(inc_dialog);
270 toolbar_main_set_sensitive(mainwin);
271 main_window_set_menu_sensitive(mainwin);
274 return inc_start(inc_dialog);
277 return inc_spool_account(account);
285 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
289 if (inc_lock_count) return 0;
291 if (prefs_common.work_offline &&
292 !inc_offline_should_override(TRUE,
293 _("Claws Mail needs network access in order "
297 inc_autocheck_timer_remove();
298 main_window_lock(mainwin);
300 new_msgs = inc_account_mail_real(mainwin, account);
302 inc_update_stats(new_msgs);
303 inc_finished(mainwin, new_msgs > 0, FALSE);
304 main_window_unlock(mainwin);
305 inc_autocheck_timer_set();
310 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
313 GList *list, *queue_list = NULL;
314 IncProgressDialog *inc_dialog;
316 gint account_new_msgs = 0;
318 if (prefs_common.work_offline &&
319 !inc_offline_should_override( (autocheck == FALSE),
320 _("Claws Mail needs network access in order "
324 if (inc_lock_count) return;
326 inc_autocheck_timer_remove();
327 main_window_lock(mainwin);
329 list = account_get_list();
331 inc_update_stats(new_msgs);
332 inc_finished(mainwin, new_msgs > 0, autocheck);
333 main_window_unlock(mainwin);
334 inc_notify_cmd(new_msgs, notify);
335 inc_autocheck_timer_set();
339 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
340 /* external incorporating program */
341 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
342 log_error(LOG_PROTOCOL, _("%s failed\n"), prefs_common.extinc_cmd);
344 main_window_unlock(mainwin);
345 inc_autocheck_timer_set();
350 /* check local folders */
351 account_new_msgs = inc_all_spool();
352 if (account_new_msgs > 0)
353 new_msgs += account_new_msgs;
355 /* check IMAP4 / News folders */
356 for (list = account_get_list(); list != NULL; list = list->next) {
357 PrefsAccount *account = list->data;
358 if ((account->protocol == A_IMAP4 ||
359 account->protocol == A_NNTP) && account->recv_at_getall) {
360 new_msgs += folderview_check_new(FOLDER(account->folder));
364 /* check POP3 accounts */
365 for (list = account_get_list(); list != NULL; list = list->next) {
367 PrefsAccount *account = list->data;
369 if (account->recv_at_getall) {
370 session = inc_session_new(account);
372 queue_list = g_list_append(queue_list, session);
377 inc_dialog = inc_progress_dialog_create(autocheck);
378 inc_dialog->queue_list = queue_list;
379 inc_dialog->mainwin = mainwin;
380 inc_progress_dialog_set_list(inc_dialog);
382 toolbar_main_set_sensitive(mainwin);
383 main_window_set_menu_sensitive(mainwin);
384 new_msgs += inc_start(inc_dialog);
387 inc_update_stats(new_msgs);
388 inc_finished(mainwin, new_msgs > 0, autocheck);
389 main_window_unlock(mainwin);
390 inc_notify_cmd(new_msgs, notify);
391 inc_autocheck_timer_set();
394 static void inc_progress_dialog_size_allocate_cb(GtkWidget *widget,
395 GtkAllocation *allocation)
397 cm_return_if_fail(allocation != NULL);
399 prefs_common.receivewin_width = allocation->width;
400 prefs_common.receivewin_height = allocation->height;
403 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
405 IncProgressDialog *dialog;
406 ProgressDialog *progress;
407 static GdkGeometry geometry;
409 dialog = g_new0(IncProgressDialog, 1);
411 progress = progress_dialog_create();
412 gtk_window_set_title(GTK_WINDOW(progress->window),
413 _("Retrieving new messages"));
414 g_signal_connect(G_OBJECT(progress->showlog_btn), "clicked",
415 G_CALLBACK(inc_showlog_cb), dialog);
416 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
417 G_CALLBACK(inc_cancel_cb), dialog);
418 g_signal_connect(G_OBJECT(progress->window), "delete_event",
419 G_CALLBACK(inc_dialog_delete_cb), dialog);
420 g_signal_connect(G_OBJECT(progress->window), "size_allocate",
421 G_CALLBACK(inc_progress_dialog_size_allocate_cb), NULL);
422 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
424 progress_dialog_get_fraction(progress);
426 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_COMPLETE,
428 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_CONTINUE,
430 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_ERROR,
433 if (!geometry.min_height) {
434 geometry.min_width = 460;
435 geometry.min_height = 250;
438 gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
440 gtk_widget_set_size_request(progress->window, prefs_common.receivewin_width,
441 prefs_common.receivewin_height);
443 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
444 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
446 dialog->show_dialog = TRUE;
447 gtk_widget_show_now(progress->window);
450 dialog->dialog = progress;
451 g_get_current_time(&dialog->progress_tv);
452 g_get_current_time(&dialog->folder_tv);
453 dialog->queue_list = NULL;
456 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
461 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
465 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
466 IncSession *session = list->data;
467 Pop3Session *pop3_session = POP3_SESSION(session->session);
469 session->data = inc_dialog;
471 progress_dialog_list_set(inc_dialog->dialog,
473 pop3_session->ac_prefs->account_name,
478 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
480 progress_dialog_get_fraction(inc_dialog->dialog);
481 progress_dialog_set_label(inc_dialog->dialog, "");
482 if (inc_dialog->mainwin)
483 main_window_progress_off(inc_dialog->mainwin);
486 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
488 cm_return_if_fail(inc_dialog != NULL);
490 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
492 if (inc_dialog->mainwin)
493 main_window_progress_off(inc_dialog->mainwin);
494 progress_dialog_destroy(inc_dialog->dialog);
499 static IncSession *inc_session_new(PrefsAccount *account)
503 cm_return_val_if_fail(account != NULL, NULL);
505 if (account->protocol != A_POP3)
507 if (!account->recv_server || !account->userid)
510 session = g_new0(IncSession, 1);
512 session->session = pop3_session_new(account);
513 session->session->data = session;
514 POP3_SESSION(session->session)->drop_message = inc_drop_message;
515 session_set_recv_message_notify(session->session,
516 inc_recv_message, session);
517 session_set_recv_data_progressive_notify(session->session,
518 inc_recv_data_progressive,
520 session_set_recv_data_notify(session->session,
521 inc_recv_data_finished, session);
526 static void inc_session_destroy(IncSession *session)
528 cm_return_if_fail(session != NULL);
530 session_destroy(session->session);
534 static gint inc_start(IncProgressDialog *inc_dialog)
538 Pop3Session *pop3_session;
544 FolderItem *processing, *inbox;
545 GSList *msglist, *msglist_element;
546 gboolean cancelled = FALSE;
548 qlist = inc_dialog->queue_list;
549 while (qlist != NULL) {
550 GList *next = qlist->next;
552 session = qlist->data;
553 pop3_session = POP3_SESSION(session->session);
554 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
555 if (password_get(pop3_session->user,
556 pop3_session->ac_prefs->recv_server,
557 "pop3", SESSION(session)->port,
558 &(pop3_session->pass))) {
560 } else if (pop3_session->ac_prefs->passwd)
562 g_strdup(pop3_session->ac_prefs->passwd);
566 if (inc_dialog->show_dialog)
567 manage_window_focus_in
568 (inc_dialog->dialog->window,
571 pass = input_dialog_query_password_keep
572 (pop3_session->ac_prefs->recv_server,
574 &(pop3_session->ac_prefs->session_passwd));
576 if (inc_dialog->show_dialog)
577 manage_window_focus_out
578 (inc_dialog->dialog->window,
582 pop3_session->pass = pass;
589 #define SET_PIXMAP_AND_TEXT(pix, str) \
591 progress_dialog_list_set(inc_dialog->dialog, \
592 inc_dialog->cur_row, \
598 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
599 session = inc_dialog->queue_list->data;
600 pop3_session = POP3_SESSION(session->session);
601 GSList *filtered, *unfiltered;
603 if (pop3_session->pass == NULL) {
604 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
605 inc_session_destroy(session);
606 inc_dialog->queue_list =
607 g_list_remove(inc_dialog->queue_list, session);
611 inc_progress_dialog_clear(inc_dialog);
612 progress_dialog_scroll_to_row(inc_dialog->dialog,
613 inc_dialog->cur_row);
615 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
617 /* begin POP3 session */
618 inc_state = inc_pop3_session_do(session);
622 if (pop3_session->cur_total_num > 0)
623 msg = g_strdup_printf(
624 ngettext("Done (%d message (%s) received)",
625 "Done (%d messages (%s) received)",
626 pop3_session->cur_total_num),
627 pop3_session->cur_total_num,
628 to_human_readable((goffset)pop3_session->cur_total_recv_bytes));
630 msg = g_strdup_printf(_("Done (no new messages)"));
631 SET_PIXMAP_AND_TEXT(okpix, msg);
634 case INC_CONNECT_ERROR:
635 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
637 case INC_AUTH_FAILED:
638 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
639 if (pop3_session->ac_prefs->session_passwd) {
640 g_free(pop3_session->ac_prefs->session_passwd);
641 pop3_session->ac_prefs->session_passwd = NULL;
645 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
650 case INC_SOCKET_ERROR:
652 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
655 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
658 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
659 if (!inc_dialog->show_dialog)
666 if (pop3_session->error_val == PS_AUTHFAIL) {
667 if(!prefs_common.no_recv_err_panel) {
668 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
669 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
670 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
674 /* CLAWS: perform filtering actions on dropped message */
675 /* CLAWS: get default inbox (perhaps per account) */
676 if (pop3_session->ac_prefs->inbox) {
677 /* CLAWS: get destination folder / mailbox */
678 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
680 inbox = folder_get_default_inbox();
682 inbox = folder_get_default_inbox();
684 /* get list of messages in processing */
685 processing = folder_get_default_processing();
686 folder_item_scan(processing);
687 msglist = folder_item_get_msg_list(processing);
689 /* process messages */
690 folder_item_update_freeze();
692 procmsg_msglist_filter(msglist, pop3_session->ac_prefs,
693 &filtered, &unfiltered,
694 pop3_session->ac_prefs->filter_on_recv);
696 filtering_move_and_copy_msgs(msglist);
697 if (unfiltered != NULL)
698 folder_item_move_msgs(inbox, unfiltered);
700 for(msglist_element = msglist; msglist_element != NULL;
701 msglist_element = msglist_element->next) {
702 MsgInfo *msginfo = (MsgInfo *)msglist_element->data;
703 procmsg_msginfo_free(msginfo);
705 folder_item_update_thaw();
707 g_slist_free(msglist);
708 g_slist_free(filtered);
709 g_slist_free(unfiltered);
713 new_msgs += pop3_session->cur_total_num;
715 pop3_write_uidl_list(pop3_session);
717 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
719 if (inc_dialog->show_dialog)
720 manage_window_focus_in
721 (inc_dialog->dialog->window,
723 inc_put_error(inc_state, pop3_session);
724 if (inc_dialog->show_dialog)
725 manage_window_focus_out
726 (inc_dialog->dialog->window,
728 if (inc_state == INC_NO_SPACE ||
729 inc_state == INC_IO_ERROR)
732 folder_item_free_cache(processing, TRUE);
734 inc_session_destroy(session);
735 inc_dialog->queue_list =
736 g_list_remove(inc_dialog->queue_list, session);
739 #undef SET_PIXMAP_AND_TEXT
742 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
743 "Finished (%d new messages)",
744 new_msgs), new_msgs);
746 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
748 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
750 while (inc_dialog->queue_list != NULL) {
751 session = inc_dialog->queue_list->data;
752 inc_session_destroy(session);
753 inc_dialog->queue_list =
754 g_list_remove(inc_dialog->queue_list, session);
757 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
758 inc_progress_dialog_destroy(inc_dialog);
760 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
762 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
771 static IncState inc_pop3_session_do(IncSession *session)
773 Pop3Session *pop3_session = POP3_SESSION(session->session);
774 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
780 debug_print("getting new messages of account %s...\n",
781 pop3_session->ac_prefs->account_name);
783 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
785 buf = g_strdup_printf(_("%s: Retrieving new messages"),
786 pop3_session->ac_prefs->recv_server);
787 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
790 server = pop3_session->ac_prefs->recv_server;
791 account_name = pop3_session->ac_prefs->account_name;
793 port = pop3_session->ac_prefs->set_popport ?
794 pop3_session->ac_prefs->popport :
795 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
796 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
797 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
798 SESSION(pop3_session)->nonblocking =
799 pop3_session->ac_prefs->use_nonblocking_ssl;
801 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE) {
802 if (alertpanel_full(_("Insecure connection"),
803 _("This connection is configured to be secured "
804 "using SSL, but SSL is not available in this "
805 "build of Claws Mail. \n\n"
806 "Do you want to continue connecting to this "
807 "server? The communication would not be "
809 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
810 NULL, FALSE, NULL, ALERT_WARNING,
811 G_ALERTDEFAULT) != G_ALERTALTERNATE)
814 port = pop3_session->ac_prefs->set_popport ?
815 pop3_session->ac_prefs->popport : 110;
818 buf = g_strdup_printf(_("Account '%s': Connecting to POP3 server: %s:%d..."),
819 account_name, server, port);
820 statuswindow_print_all("%s", buf);
821 log_message(LOG_PROTOCOL, "%s\n", buf);
823 progress_dialog_set_label(inc_dialog->dialog, buf);
827 session_set_timeout(SESSION(pop3_session),
828 prefs_common.io_timeout_secs * 1000);
830 if (session_connect(SESSION(pop3_session), server, port) < 0) {
831 if(!prefs_common.no_recv_err_panel) {
832 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
833 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
834 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
836 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
838 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
840 log_error(LOG_PROTOCOL, _("Can't connect to POP3 server: %s:%d\n"),
843 session->inc_state = INC_CONNECT_ERROR;
844 statuswindow_pop_all();
845 return INC_CONNECT_ERROR;
848 while (session_is_running(SESSION(pop3_session)) &&
849 session->inc_state != INC_CANCEL)
850 gtk_main_iteration();
852 if (session->inc_state == INC_SUCCESS) {
853 switch (pop3_session->error_val) {
855 switch (SESSION(pop3_session)->state) {
857 if (pop3_session->state == POP3_READY)
858 session->inc_state = INC_CONNECT_ERROR;
860 session->inc_state = INC_ERROR;
863 session->inc_state = INC_EOF;
865 case SESSION_TIMEOUT:
866 session->inc_state = INC_TIMEOUT;
869 session->inc_state = INC_SUCCESS;
874 session->inc_state = INC_AUTH_FAILED;
877 session->inc_state = INC_IO_ERROR;
880 session->inc_state = INC_SOCKET_ERROR;
883 session->inc_state = INC_LOCKED;
886 session->inc_state = INC_ERROR;
891 session_disconnect(SESSION(pop3_session));
894 return session->inc_state;
897 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
898 IncSession *inc_session)
900 inc_progress_dialog_set_label(inc_dialog, inc_session);
901 inc_progress_dialog_set_progress(inc_dialog, inc_session);
904 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
905 IncSession *inc_session)
907 ProgressDialog *dialog = inc_dialog->dialog;
908 Pop3Session *session;
910 cm_return_if_fail(inc_session != NULL);
912 session = POP3_SESSION(inc_session->session);
914 switch (session->state) {
917 case POP3_GETAUTH_USER:
918 case POP3_GETAUTH_PASS:
919 case POP3_GETAUTH_APOP:
920 progress_dialog_set_label(dialog, _("Authenticating..."));
921 statuswindow_pop_all();
922 statusbar_print_all(_("Retrieving messages from %s (%s)..."),
923 SESSION(session)->server,
924 session->ac_prefs->account_name);
926 case POP3_GETRANGE_STAT:
927 progress_dialog_set_label
928 (dialog, _("Getting the number of new messages (STAT)..."));
930 case POP3_GETRANGE_LAST:
931 progress_dialog_set_label
932 (dialog, _("Getting the number of new messages (LAST)..."));
934 case POP3_GETRANGE_UIDL:
935 progress_dialog_set_label
936 (dialog, _("Getting the number of new messages (UIDL)..."));
938 case POP3_GETSIZE_LIST:
939 progress_dialog_set_label
940 (dialog, _("Getting the size of messages (LIST)..."));
947 progress_dialog_set_label(dialog, _("Quitting"));
954 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
955 IncSession *inc_session)
957 gchar buf[MESSAGEBUFSIZE];
958 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
959 gchar *total_size_str;
963 if (!pop3_session->new_msg_exist) return;
965 cur_total = inc_session->cur_total_bytes;
966 total = pop3_session->total_bytes;
967 if (pop3_session->state == POP3_RETR ||
968 pop3_session->state == POP3_RETR_RECV ||
969 pop3_session->state == POP3_DELETE) {
970 Xstrdup_a(total_size_str, to_human_readable((goffset)total), return);
971 g_snprintf(buf, sizeof(buf),
972 _("Retrieving message (%d / %d) (%s / %s)"),
973 pop3_session->cur_msg, pop3_session->count,
974 to_human_readable((goffset)cur_total), total_size_str);
975 progress_dialog_set_label(inc_dialog->dialog, buf);
978 progress_dialog_set_fraction
979 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
981 statusbar_progress_all(pop3_session->cur_msg, pop3_session->count, 1);
983 if (pop3_session->cur_total_num > 0) {
984 g_snprintf(buf, sizeof(buf),
985 ngettext("Retrieving (%d message (%s) received)",
986 "Retrieving (%d messages (%s) received)",
987 pop3_session->cur_total_num),
988 pop3_session->cur_total_num,
990 ((goffset)pop3_session->cur_total_recv_bytes));
991 progress_dialog_list_set_status(inc_dialog->dialog,
997 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
998 IncSession *inc_session)
1004 g_get_current_time(&tv_cur);
1006 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
1007 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
1008 if (tv_result.tv_usec < 0) {
1010 tv_result.tv_usec += G_USEC_PER_SEC;
1013 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
1014 if (msec > PROGRESS_UPDATE_INTERVAL) {
1015 inc_progress_dialog_update(inc_dialog, inc_session);
1016 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
1017 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
1021 static gint inc_recv_data_progressive(Session *session, guint cur_len,
1022 guint total_len, gpointer data)
1024 IncSession *inc_session = (IncSession *)data;
1025 Pop3Session *pop3_session = POP3_SESSION(session);
1026 IncProgressDialog *inc_dialog;
1029 cm_return_val_if_fail(inc_session != NULL, -1);
1031 if (pop3_session->state != POP3_RETR &&
1032 pop3_session->state != POP3_RETR_RECV &&
1033 pop3_session->state != POP3_DELETE &&
1034 pop3_session->state != POP3_LOGOUT) return 0;
1036 if (!pop3_session->new_msg_exist) return 0;
1038 cur_total = pop3_session->cur_total_bytes + cur_len;
1039 if (cur_total > pop3_session->total_bytes)
1040 cur_total = pop3_session->total_bytes;
1041 inc_session->cur_total_bytes = cur_total;
1043 inc_dialog = (IncProgressDialog *)inc_session->data;
1044 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1049 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1051 IncSession *inc_session = (IncSession *)data;
1052 IncProgressDialog *inc_dialog;
1054 cm_return_val_if_fail(inc_session != NULL, -1);
1056 inc_dialog = (IncProgressDialog *)inc_session->data;
1058 inc_recv_data_progressive(session, 0, 0, inc_session);
1060 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1061 inc_progress_dialog_update(inc_dialog, inc_session);
1067 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1069 IncSession *inc_session = (IncSession *)data;
1070 IncProgressDialog *inc_dialog;
1072 cm_return_val_if_fail(inc_session != NULL, -1);
1074 inc_dialog = (IncProgressDialog *)inc_session->data;
1076 switch (POP3_SESSION(session)->state) {
1077 case POP3_GETAUTH_USER:
1078 case POP3_GETAUTH_PASS:
1079 case POP3_GETAUTH_APOP:
1080 case POP3_GETRANGE_STAT:
1081 case POP3_GETRANGE_LAST:
1082 case POP3_GETRANGE_UIDL:
1083 case POP3_GETSIZE_LIST:
1084 inc_progress_dialog_update(inc_dialog, inc_session);
1087 inc_recv_data_progressive(session, 0, 0, inc_session);
1090 inc_progress_dialog_update(inc_dialog, inc_session);
1099 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1102 FolderItem *dropfolder;
1103 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1106 cm_return_val_if_fail(inc_session != NULL, -1);
1108 if (session->ac_prefs->inbox) {
1109 inbox = folder_find_item_from_identifier
1110 (session->ac_prefs->inbox);
1112 inbox = folder_get_default_inbox();
1114 inbox = folder_get_default_inbox();
1120 /* CLAWS: claws uses a global .processing folder for the filtering. */
1121 dropfolder = folder_get_default_processing();
1123 /* add msg file to drop folder */
1124 if ((msgnum = folder_item_add_msg(
1125 dropfolder, file, NULL, TRUE)) < 0) {
1133 static void inc_put_error(IncState istate, Pop3Session *session)
1135 gchar *log_msg = NULL;
1136 gchar *err_msg = NULL;
1137 gboolean fatal_error = FALSE;
1140 case INC_CONNECT_ERROR:
1142 if (prefs_common.no_recv_err_panel)
1144 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1145 SESSION(session)->server,
1146 SESSION(session)->port);
1149 log_msg = _("Error occurred while processing mail.");
1151 if (prefs_common.no_recv_err_panel)
1153 if (session->error_msg)
1154 err_msg = g_strdup_printf
1155 (_("Error occurred while processing mail:\n%s"),
1156 session->error_msg);
1158 err_msg = g_strdup(log_msg);
1161 log_msg = _("No disk space left.");
1162 err_msg = g_strdup(log_msg);
1166 log_msg = _("Can't write file.");
1167 err_msg = g_strdup(log_msg);
1170 case INC_SOCKET_ERROR:
1171 log_msg = _("Socket error.");
1172 if (prefs_common.no_recv_err_panel)
1174 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1175 SESSION(session)->server,
1176 SESSION(session)->port);
1179 log_msg = _("Connection closed by the remote host.");
1180 if (prefs_common.no_recv_err_panel)
1182 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1183 SESSION(session)->server,
1184 SESSION(session)->port);
1187 log_msg = _("Mailbox is locked.");
1188 if (prefs_common.no_recv_err_panel)
1190 if (session->error_msg)
1191 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1192 session->error_msg);
1194 err_msg = g_strdup(log_msg);
1196 case INC_AUTH_FAILED:
1197 log_msg = _("Authentication failed.");
1199 if (prefs_common.no_recv_err_panel)
1201 if (session->error_msg)
1202 err_msg = g_strdup_printf
1203 (_("Authentication failed:\n%s"), session->error_msg);
1205 err_msg = g_strdup(log_msg);
1208 log_msg = _("Session timed out. You may be able to "
1209 "recover by increasing the timeout value in "
1210 "Preferences/Other/Miscellaneous.");
1211 if (prefs_common.no_recv_err_panel)
1213 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1214 SESSION(session)->server,
1215 SESSION(session)->port);
1223 log_error(LOG_PROTOCOL, "%s\n", log_msg);
1225 log_warning(LOG_PROTOCOL, "%s\n", log_msg);
1227 if (prefs_common.no_recv_err_panel && fatal_error)
1228 mainwindow_show_error();
1231 alertpanel_error_log("%s", err_msg);
1236 static void inc_cancel(IncProgressDialog *dialog)
1238 IncSession *session;
1240 cm_return_if_fail(dialog != NULL);
1242 if (dialog->queue_list == NULL) {
1243 inc_progress_dialog_destroy(dialog);
1247 session = dialog->queue_list->data;
1249 session->inc_state = INC_CANCEL;
1251 log_message(LOG_PROTOCOL, _("Incorporation cancelled\n"));
1254 gboolean inc_is_active(void)
1256 return (inc_dialog_list != NULL);
1259 void inc_cancel_all(void)
1263 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1264 inc_cancel((IncProgressDialog *)cur->data);
1267 static void inc_showlog_cb(GtkWidget *widget, gpointer data)
1269 MainWindow *mainwin = mainwindow_get_mainwindow();
1271 log_window_show(mainwin->logwin);
1274 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1276 inc_cancel((IncProgressDialog *)data);
1279 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1282 IncProgressDialog *dialog = (IncProgressDialog *)data;
1284 if (dialog->queue_list == NULL)
1285 inc_progress_dialog_destroy(dialog);
1290 static gint inc_spool_account(PrefsAccount *account)
1296 if (account->local_inbox) {
1297 inbox = folder_find_item_from_identifier(account->local_inbox);
1299 inbox = folder_get_default_inbox();
1301 inbox = folder_get_default_inbox();
1303 if (account->local_mbox) {
1304 if (is_file_exist(account->local_mbox))
1305 mbox = g_strdup(account->local_mbox);
1306 else if (is_dir_exist(account->local_mbox))
1307 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1308 g_get_user_name(), NULL);
1310 debug_print("%s: local mailbox not found.\n",
1311 account->local_mbox);
1315 debug_print("local mailbox not set in account info.\n");
1319 result = get_spool(inbox, mbox, account);
1322 statusbar_pop_all();
1327 static gint inc_all_spool(void)
1331 gint account_new_msgs = 0;
1333 list = account_get_list();
1334 if (!list) return 0;
1336 for (; list != NULL; list = list->next) {
1337 PrefsAccount *account = list->data;
1339 if ((account->protocol == A_LOCAL) &&
1340 (account->recv_at_getall)) {
1341 account_new_msgs = inc_spool_account(account);
1342 if (account_new_msgs > 0)
1343 new_msgs += account_new_msgs;
1350 static gint get_spool(FolderItem *dest, const gchar *mbox, PrefsAccount *account)
1354 gchar tmp_mbox[MAXPATHLEN + 1];
1356 cm_return_val_if_fail(dest != NULL, -1);
1357 cm_return_val_if_fail(mbox != NULL, -1);
1358 cm_return_val_if_fail(account != NULL, -1);
1360 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1361 debug_print("%s: no messages in local mailbox.\n", mbox);
1363 } else if (size < 0)
1366 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1369 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%p",
1370 get_tmp_dir(), G_DIR_SEPARATOR, mbox);
1372 if (copy_mbox(lockfd, tmp_mbox) < 0) {
1373 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1377 debug_print("Getting new messages from %s into %s...\n",
1380 msgs = proc_mbox(dest, tmp_mbox, account->filter_on_recv, account);
1382 claws_unlink(tmp_mbox);
1383 if (msgs >= 0) empty_mbox(mbox);
1384 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1389 void inc_lock_real(void)
1394 void inc_unlock_real(void)
1396 if (inc_lock_count > 0)
1400 static guint autocheck_timer = 0;
1401 static gpointer autocheck_data = NULL;
1403 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1405 gchar *buf, *numpos, *ret_str;
1406 gssize by_read = 0, by_written = 0;
1408 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1409 *prefs_common.newmail_notify_cmd))
1412 buf = g_strdup(prefs_common.newmail_notify_cmd);
1413 if ((numpos = strstr(buf, "%d")) != NULL) {
1417 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1422 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1424 if (ret_str && by_written) {
1428 debug_print("executing new mail notification command: %s\n", buf);
1429 execute_command_line(buf, TRUE);
1434 void inc_autocheck_timer_init(MainWindow *mainwin)
1436 autocheck_data = mainwin;
1437 inc_autocheck_timer_set();
1440 static void inc_autocheck_timer_set_interval(guint interval)
1442 inc_autocheck_timer_remove();
1443 /* last test is to avoid re-enabling auto_check after modifying
1444 the common preferences */
1445 if (prefs_common.autochk_newmail && autocheck_data
1446 && prefs_common.work_offline == FALSE) {
1447 #if GLIB_CHECK_VERSION(2,14,0)
1448 if (interval % 1000 == 0)
1450 g_timeout_add_seconds(interval/1000, inc_autocheck_func, autocheck_data);
1453 autocheck_timer = g_timeout_add
1454 (interval, inc_autocheck_func, autocheck_data);
1455 debug_print("added timer = %d\n", autocheck_timer);
1459 void inc_autocheck_timer_set(void)
1461 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1464 void inc_autocheck_timer_remove(void)
1466 if (autocheck_timer) {
1467 debug_print("removed timer = %d\n", autocheck_timer);
1468 g_source_remove(autocheck_timer);
1469 autocheck_timer = 0;
1473 static gint inc_autocheck_func(gpointer data)
1475 MainWindow *mainwin = (MainWindow *)data;
1477 if (inc_lock_count) {
1478 debug_print("autocheck is locked.\n");
1479 inc_autocheck_timer_set_interval(1000);
1483 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1488 gboolean inc_offline_should_override(gboolean force_ask, const gchar *msg)
1490 static time_t overridden_yes = 0;
1491 static time_t overridden_no = 0;
1492 int length = 10; /* minutes */
1493 gint answer = G_ALERTDEFAULT;
1495 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1496 /* If no network connection is available, override is not possible */
1497 if(!networkmanager_is_online(NULL))
1501 if (prefs_common.autochk_newmail)
1502 length = prefs_common.autochk_itv; /* minutes */
1505 overridden_no = (time_t)0;
1508 if (prefs_common.work_offline) {
1511 if (time(NULL) - overridden_yes < length * 60) /* seconds */
1513 else if (time(NULL) - overridden_no < length * 60) /* seconds */
1517 tmp = g_strdup_printf(
1518 _("%s%sYou're working offline. Override for %d minutes?"),
1523 tmp = g_strdup_printf(
1524 _("%s%sYou're working offline. Override?"),
1528 answer = alertpanel(_("Offline warning"),
1530 GTK_STOCK_NO, "+" GTK_STOCK_YES,
1531 !force_ask? _("On_ly once"):NULL);
1533 if (answer == G_ALERTALTERNATE) {
1534 overridden_yes = time(NULL);
1536 } else if (answer == G_ALERTDEFAULT) {
1538 overridden_no = time(NULL);
1541 overridden_yes = (time_t)0;
1542 overridden_no = (time_t)0;