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 (account->receive_in_progress) return 0;
293 if (prefs_common.work_offline &&
294 !inc_offline_should_override(TRUE,
295 _("Claws Mail needs network access in order "
299 inc_autocheck_timer_remove();
300 main_window_lock(mainwin);
302 new_msgs = inc_account_mail_real(mainwin, account);
304 inc_update_stats(new_msgs);
305 inc_finished(mainwin, new_msgs > 0, FALSE);
306 main_window_unlock(mainwin);
307 inc_autocheck_timer_set();
312 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
315 GList *list, *queue_list = NULL;
316 IncProgressDialog *inc_dialog;
318 gint account_new_msgs = 0;
320 if (prefs_common.work_offline &&
321 !inc_offline_should_override( (autocheck == FALSE),
322 _("Claws Mail needs network access in order "
326 if (inc_lock_count) return;
328 inc_autocheck_timer_remove();
329 main_window_lock(mainwin);
331 list = account_get_list();
333 inc_update_stats(new_msgs);
334 inc_finished(mainwin, new_msgs > 0, autocheck);
335 main_window_unlock(mainwin);
336 inc_notify_cmd(new_msgs, notify);
337 inc_autocheck_timer_set();
341 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
342 /* external incorporating program */
343 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
344 log_error(LOG_PROTOCOL, _("%s failed\n"), prefs_common.extinc_cmd);
346 main_window_unlock(mainwin);
347 inc_autocheck_timer_set();
352 /* check local folders */
353 account_new_msgs = inc_all_spool();
354 if (account_new_msgs > 0)
355 new_msgs += account_new_msgs;
357 /* check IMAP4 / News folders */
358 for (list = account_get_list(); list != NULL; list = list->next) {
359 PrefsAccount *account = list->data;
360 if ((account->protocol == A_IMAP4 ||
361 account->protocol == A_NNTP) && account->recv_at_getall) {
362 new_msgs += folderview_check_new(FOLDER(account->folder));
366 /* check POP3 accounts */
367 for (list = account_get_list(); list != NULL; list = list->next) {
369 PrefsAccount *account = list->data;
371 if (account->recv_at_getall) {
372 if (!(account->receive_in_progress)) {
373 session = inc_session_new(account);
375 queue_list = g_list_append(queue_list, session);
381 inc_dialog = inc_progress_dialog_create(autocheck);
382 inc_dialog->queue_list = queue_list;
383 inc_dialog->mainwin = mainwin;
384 inc_progress_dialog_set_list(inc_dialog);
386 toolbar_main_set_sensitive(mainwin);
387 main_window_set_menu_sensitive(mainwin);
388 new_msgs += inc_start(inc_dialog);
391 inc_update_stats(new_msgs);
392 inc_finished(mainwin, new_msgs > 0, autocheck);
393 main_window_unlock(mainwin);
394 inc_notify_cmd(new_msgs, notify);
395 inc_autocheck_timer_set();
398 static void inc_progress_dialog_size_allocate_cb(GtkWidget *widget,
399 GtkAllocation *allocation)
401 cm_return_if_fail(allocation != NULL);
403 prefs_common.receivewin_width = allocation->width;
404 prefs_common.receivewin_height = allocation->height;
407 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
409 IncProgressDialog *dialog;
410 ProgressDialog *progress;
411 static GdkGeometry geometry;
413 dialog = g_new0(IncProgressDialog, 1);
415 progress = progress_dialog_create();
416 gtk_window_set_title(GTK_WINDOW(progress->window),
417 _("Retrieving new messages"));
418 g_signal_connect(G_OBJECT(progress->showlog_btn), "clicked",
419 G_CALLBACK(inc_showlog_cb), dialog);
420 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
421 G_CALLBACK(inc_cancel_cb), dialog);
422 g_signal_connect(G_OBJECT(progress->window), "delete_event",
423 G_CALLBACK(inc_dialog_delete_cb), dialog);
424 g_signal_connect(G_OBJECT(progress->window), "size_allocate",
425 G_CALLBACK(inc_progress_dialog_size_allocate_cb), NULL);
426 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
428 progress_dialog_get_fraction(progress);
430 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_COMPLETE,
432 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_CONTINUE,
434 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_ERROR,
437 if (!geometry.min_height) {
438 geometry.min_width = 460;
439 geometry.min_height = 250;
442 gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
444 gtk_widget_set_size_request(progress->window, prefs_common.receivewin_width,
445 prefs_common.receivewin_height);
447 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
448 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
450 dialog->show_dialog = TRUE;
451 gtk_widget_show_now(progress->window);
454 dialog->dialog = progress;
455 g_get_current_time(&dialog->progress_tv);
456 g_get_current_time(&dialog->folder_tv);
457 dialog->queue_list = NULL;
460 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
465 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
469 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
470 IncSession *session = list->data;
471 Pop3Session *pop3_session = POP3_SESSION(session->session);
473 session->data = inc_dialog;
475 progress_dialog_list_set(inc_dialog->dialog,
477 pop3_session->ac_prefs->account_name,
482 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
484 progress_dialog_get_fraction(inc_dialog->dialog);
485 progress_dialog_set_label(inc_dialog->dialog, "");
486 if (inc_dialog->mainwin)
487 main_window_progress_off(inc_dialog->mainwin);
490 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
492 cm_return_if_fail(inc_dialog != NULL);
494 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
496 if (inc_dialog->mainwin)
497 main_window_progress_off(inc_dialog->mainwin);
498 progress_dialog_destroy(inc_dialog->dialog);
503 static IncSession *inc_session_new(PrefsAccount *account)
507 cm_return_val_if_fail(account != NULL, NULL);
509 if (account->protocol != A_POP3)
511 if (!account->recv_server || !account->userid)
514 session = g_new0(IncSession, 1);
516 session->session = pop3_session_new(account);
517 session->session->data = session;
518 POP3_SESSION(session->session)->drop_message = inc_drop_message;
519 session_set_recv_message_notify(session->session,
520 inc_recv_message, session);
521 session_set_recv_data_progressive_notify(session->session,
522 inc_recv_data_progressive,
524 session_set_recv_data_notify(session->session,
525 inc_recv_data_finished, session);
530 static void inc_session_destroy(IncSession *session)
532 cm_return_if_fail(session != NULL);
534 session_destroy(session->session);
538 static gint pop3_get_port(Pop3Session *pop3_session)
541 return pop3_session->ac_prefs->set_popport ?
542 pop3_session->ac_prefs->popport :
543 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
545 return pop3_session->ac_prefs->set_popport ?
546 pop3_session->ac_prefs->popport : 110;
550 static gint inc_start(IncProgressDialog *inc_dialog)
554 Pop3Session *pop3_session;
560 FolderItem *processing, *inbox;
561 GSList *msglist, *msglist_element;
562 gboolean cancelled = FALSE;
564 qlist = inc_dialog->queue_list;
565 while (qlist != NULL) {
566 GList *next = qlist->next;
568 session = qlist->data;
569 pop3_session = POP3_SESSION(session->session);
570 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
572 if (password_get(pop3_session->user,
573 pop3_session->ac_prefs->recv_server,
574 "pop3", pop3_get_port(pop3_session),
575 &(pop3_session->pass))) {
577 } else if (pop3_session->ac_prefs->passwd)
579 g_strdup(pop3_session->ac_prefs->passwd);
583 if (inc_dialog->show_dialog)
584 manage_window_focus_in
585 (inc_dialog->dialog->window,
588 pass = input_dialog_query_password_keep
589 (pop3_session->ac_prefs->recv_server,
591 &(pop3_session->ac_prefs->session_passwd));
593 if (inc_dialog->show_dialog)
594 manage_window_focus_out
595 (inc_dialog->dialog->window,
599 pop3_session->pass = pass;
606 #define SET_PIXMAP_AND_TEXT(pix, str) \
608 progress_dialog_list_set(inc_dialog->dialog, \
609 inc_dialog->cur_row, \
615 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
616 session = inc_dialog->queue_list->data;
617 pop3_session = POP3_SESSION(session->session);
618 GSList *filtered, *unfiltered;
620 if (pop3_session->pass == NULL) {
621 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
622 inc_session_destroy(session);
623 inc_dialog->queue_list =
624 g_list_remove(inc_dialog->queue_list, session);
628 inc_progress_dialog_clear(inc_dialog);
629 progress_dialog_scroll_to_row(inc_dialog->dialog,
630 inc_dialog->cur_row);
632 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
634 /* begin POP3 session */
635 inc_state = inc_pop3_session_do(session);
639 if (pop3_session->cur_total_num > 0)
640 msg = g_strdup_printf(
641 ngettext("Done (%d message (%s) received)",
642 "Done (%d messages (%s) received)",
643 pop3_session->cur_total_num),
644 pop3_session->cur_total_num,
645 to_human_readable((goffset)pop3_session->cur_total_recv_bytes));
647 msg = g_strdup_printf(_("Done (no new messages)"));
648 SET_PIXMAP_AND_TEXT(okpix, msg);
651 case INC_CONNECT_ERROR:
652 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
654 case INC_AUTH_FAILED:
655 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
656 if (pop3_session->ac_prefs->session_passwd) {
657 g_free(pop3_session->ac_prefs->session_passwd);
658 pop3_session->ac_prefs->session_passwd = NULL;
662 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
667 case INC_SOCKET_ERROR:
669 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
672 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
675 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
676 if (!inc_dialog->show_dialog)
683 if (pop3_session->error_val == PS_AUTHFAIL) {
684 if(!prefs_common.no_recv_err_panel) {
685 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
686 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
687 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
691 /* CLAWS: perform filtering actions on dropped message */
692 /* CLAWS: get default inbox (perhaps per account) */
693 if (pop3_session->ac_prefs->inbox) {
694 /* CLAWS: get destination folder / mailbox */
695 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
697 inbox = folder_get_default_inbox();
699 inbox = folder_get_default_inbox();
701 /* get list of messages in processing */
702 processing = folder_get_default_processing(pop3_session->ac_prefs->account_id);
703 folder_item_scan(processing);
704 msglist = folder_item_get_msg_list(processing);
706 /* process messages */
707 folder_item_update_freeze();
709 procmsg_msglist_filter(msglist, pop3_session->ac_prefs,
710 &filtered, &unfiltered,
711 pop3_session->ac_prefs->filter_on_recv);
713 filtering_move_and_copy_msgs(msglist);
714 if (unfiltered != NULL)
715 folder_item_move_msgs(inbox, unfiltered);
717 for(msglist_element = msglist; msglist_element != NULL;
718 msglist_element = msglist_element->next) {
719 MsgInfo *msginfo = (MsgInfo *)msglist_element->data;
720 procmsg_msginfo_free(msginfo);
722 folder_item_update_thaw();
724 g_slist_free(msglist);
725 g_slist_free(filtered);
726 g_slist_free(unfiltered);
730 new_msgs += pop3_session->cur_total_num;
732 pop3_write_uidl_list(pop3_session);
734 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
736 if (inc_dialog->show_dialog)
737 manage_window_focus_in
738 (inc_dialog->dialog->window,
740 inc_put_error(inc_state, pop3_session);
741 if (inc_dialog->show_dialog)
742 manage_window_focus_out
743 (inc_dialog->dialog->window,
745 if (inc_state == INC_NO_SPACE ||
746 inc_state == INC_IO_ERROR)
749 folder_item_free_cache(processing, TRUE);
751 inc_session_destroy(session);
752 inc_dialog->queue_list =
753 g_list_remove(inc_dialog->queue_list, session);
756 #undef SET_PIXMAP_AND_TEXT
759 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
760 "Finished (%d new messages)",
761 new_msgs), new_msgs);
763 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
765 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
767 while (inc_dialog->queue_list != NULL) {
768 session = inc_dialog->queue_list->data;
769 inc_session_destroy(session);
770 inc_dialog->queue_list =
771 g_list_remove(inc_dialog->queue_list, session);
774 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
775 inc_progress_dialog_destroy(inc_dialog);
777 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
779 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
788 static IncState inc_pop3_session_do(IncSession *session)
790 Pop3Session *pop3_session = POP3_SESSION(session->session);
791 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
797 debug_print("getting new messages of account %s...\n",
798 pop3_session->ac_prefs->account_name);
800 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
802 buf = g_strdup_printf(_("%s: Retrieving new messages"),
803 pop3_session->ac_prefs->recv_server);
804 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
807 server = pop3_session->ac_prefs->recv_server;
808 account_name = pop3_session->ac_prefs->account_name;
809 port = pop3_get_port(pop3_session);
812 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
813 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
814 SESSION(pop3_session)->nonblocking =
815 pop3_session->ac_prefs->use_nonblocking_ssl;
817 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE) {
818 if (alertpanel_full(_("Insecure connection"),
819 _("This connection is configured to be secured "
820 "using SSL, but SSL is not available in this "
821 "build of Claws Mail. \n\n"
822 "Do you want to continue connecting to this "
823 "server? The communication would not be "
825 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
826 NULL, FALSE, NULL, ALERT_WARNING,
827 G_ALERTDEFAULT) != G_ALERTALTERNATE)
832 buf = g_strdup_printf(_("Account '%s': Connecting to POP3 server: %s:%d..."),
833 account_name, server, port);
834 statuswindow_print_all("%s", buf);
835 log_message(LOG_PROTOCOL, "%s\n", buf);
837 progress_dialog_set_label(inc_dialog->dialog, buf);
841 session_set_timeout(SESSION(pop3_session),
842 prefs_common.io_timeout_secs * 1000);
844 if (session_connect(SESSION(pop3_session), server, port) < 0) {
845 if(!prefs_common.no_recv_err_panel) {
846 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
847 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
848 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
850 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
852 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
854 log_error(LOG_PROTOCOL, _("Can't connect to POP3 server: %s:%d\n"),
857 session->inc_state = INC_CONNECT_ERROR;
858 statuswindow_pop_all();
859 return INC_CONNECT_ERROR;
862 while (session_is_running(SESSION(pop3_session)) &&
863 session->inc_state != INC_CANCEL)
864 gtk_main_iteration();
866 if (session->inc_state == INC_SUCCESS) {
867 switch (pop3_session->error_val) {
869 switch (SESSION(pop3_session)->state) {
871 if (pop3_session->state == POP3_READY)
872 session->inc_state = INC_CONNECT_ERROR;
874 session->inc_state = INC_ERROR;
877 session->inc_state = INC_EOF;
879 case SESSION_TIMEOUT:
880 session->inc_state = INC_TIMEOUT;
883 session->inc_state = INC_SUCCESS;
888 session->inc_state = INC_AUTH_FAILED;
891 session->inc_state = INC_IO_ERROR;
894 session->inc_state = INC_SOCKET_ERROR;
897 session->inc_state = INC_LOCKED;
900 session->inc_state = INC_ERROR;
905 session_disconnect(SESSION(pop3_session));
908 return session->inc_state;
911 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
912 IncSession *inc_session)
914 inc_progress_dialog_set_label(inc_dialog, inc_session);
915 inc_progress_dialog_set_progress(inc_dialog, inc_session);
918 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
919 IncSession *inc_session)
921 ProgressDialog *dialog = inc_dialog->dialog;
922 Pop3Session *session;
924 cm_return_if_fail(inc_session != NULL);
926 session = POP3_SESSION(inc_session->session);
928 switch (session->state) {
931 case POP3_GETAUTH_USER:
932 case POP3_GETAUTH_PASS:
933 case POP3_GETAUTH_APOP:
934 progress_dialog_set_label(dialog, _("Authenticating..."));
935 statuswindow_pop_all();
936 statusbar_print_all(_("Retrieving messages from %s (%s)..."),
937 SESSION(session)->server,
938 session->ac_prefs->account_name);
940 case POP3_GETRANGE_STAT:
941 progress_dialog_set_label
942 (dialog, _("Getting the number of new messages (STAT)..."));
944 case POP3_GETRANGE_LAST:
945 progress_dialog_set_label
946 (dialog, _("Getting the number of new messages (LAST)..."));
948 case POP3_GETRANGE_UIDL:
949 progress_dialog_set_label
950 (dialog, _("Getting the number of new messages (UIDL)..."));
952 case POP3_GETSIZE_LIST:
953 progress_dialog_set_label
954 (dialog, _("Getting the size of messages (LIST)..."));
961 progress_dialog_set_label(dialog, _("Quitting"));
968 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
969 IncSession *inc_session)
971 gchar buf[MESSAGEBUFSIZE];
972 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
973 gchar *total_size_str;
977 if (!pop3_session->new_msg_exist) return;
979 cur_total = inc_session->cur_total_bytes;
980 total = pop3_session->total_bytes;
981 if (pop3_session->state == POP3_RETR ||
982 pop3_session->state == POP3_RETR_RECV ||
983 pop3_session->state == POP3_DELETE) {
984 Xstrdup_a(total_size_str, to_human_readable((goffset)total), return);
985 g_snprintf(buf, sizeof(buf),
986 _("Retrieving message (%d / %d) (%s / %s)"),
987 pop3_session->cur_msg, pop3_session->count,
988 to_human_readable((goffset)cur_total), total_size_str);
989 progress_dialog_set_label(inc_dialog->dialog, buf);
992 progress_dialog_set_fraction
993 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
995 statusbar_progress_all(pop3_session->cur_msg, pop3_session->count, 1);
997 if (pop3_session->cur_total_num > 0) {
998 g_snprintf(buf, sizeof(buf),
999 ngettext("Retrieving (%d message (%s) received)",
1000 "Retrieving (%d messages (%s) received)",
1001 pop3_session->cur_total_num),
1002 pop3_session->cur_total_num,
1004 ((goffset)pop3_session->cur_total_recv_bytes));
1005 progress_dialog_list_set_status(inc_dialog->dialog,
1006 inc_dialog->cur_row,
1011 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
1012 IncSession *inc_session)
1018 g_get_current_time(&tv_cur);
1020 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
1021 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
1022 if (tv_result.tv_usec < 0) {
1024 tv_result.tv_usec += G_USEC_PER_SEC;
1027 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
1028 if (msec > PROGRESS_UPDATE_INTERVAL) {
1029 inc_progress_dialog_update(inc_dialog, inc_session);
1030 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
1031 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
1035 static gint inc_recv_data_progressive(Session *session, guint cur_len,
1036 guint total_len, gpointer data)
1038 IncSession *inc_session = (IncSession *)data;
1039 Pop3Session *pop3_session = POP3_SESSION(session);
1040 IncProgressDialog *inc_dialog;
1043 cm_return_val_if_fail(inc_session != NULL, -1);
1045 if (pop3_session->state != POP3_RETR &&
1046 pop3_session->state != POP3_RETR_RECV &&
1047 pop3_session->state != POP3_DELETE &&
1048 pop3_session->state != POP3_LOGOUT) return 0;
1050 if (!pop3_session->new_msg_exist) return 0;
1052 cur_total = pop3_session->cur_total_bytes + cur_len;
1053 if (cur_total > pop3_session->total_bytes)
1054 cur_total = pop3_session->total_bytes;
1055 inc_session->cur_total_bytes = cur_total;
1057 inc_dialog = (IncProgressDialog *)inc_session->data;
1058 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1063 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1065 IncSession *inc_session = (IncSession *)data;
1066 IncProgressDialog *inc_dialog;
1068 cm_return_val_if_fail(inc_session != NULL, -1);
1070 inc_dialog = (IncProgressDialog *)inc_session->data;
1072 inc_recv_data_progressive(session, 0, 0, inc_session);
1074 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1075 inc_progress_dialog_update(inc_dialog, inc_session);
1081 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1083 IncSession *inc_session = (IncSession *)data;
1084 IncProgressDialog *inc_dialog;
1086 cm_return_val_if_fail(inc_session != NULL, -1);
1088 inc_dialog = (IncProgressDialog *)inc_session->data;
1090 switch (POP3_SESSION(session)->state) {
1091 case POP3_GETAUTH_USER:
1092 case POP3_GETAUTH_PASS:
1093 case POP3_GETAUTH_APOP:
1094 case POP3_GETRANGE_STAT:
1095 case POP3_GETRANGE_LAST:
1096 case POP3_GETRANGE_UIDL:
1097 case POP3_GETSIZE_LIST:
1098 inc_progress_dialog_update(inc_dialog, inc_session);
1101 inc_recv_data_progressive(session, 0, 0, inc_session);
1104 inc_progress_dialog_update(inc_dialog, inc_session);
1113 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1116 FolderItem *dropfolder;
1117 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1120 cm_return_val_if_fail(inc_session != NULL, -1);
1122 if (session->ac_prefs->inbox) {
1123 inbox = folder_find_item_from_identifier
1124 (session->ac_prefs->inbox);
1126 inbox = folder_get_default_inbox();
1128 inbox = folder_get_default_inbox();
1134 /* CLAWS: claws uses a global .processing folder for the filtering. */
1135 dropfolder = folder_get_default_processing(session->ac_prefs->account_id);
1137 /* add msg file to drop folder */
1138 if ((msgnum = folder_item_add_msg(
1139 dropfolder, file, NULL, TRUE)) < 0) {
1147 static void inc_put_error(IncState istate, Pop3Session *session)
1149 gchar *log_msg = NULL;
1150 gchar *err_msg = NULL;
1151 gboolean fatal_error = FALSE;
1154 case INC_CONNECT_ERROR:
1156 if (prefs_common.no_recv_err_panel)
1158 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1159 SESSION(session)->server,
1160 SESSION(session)->port);
1163 log_msg = _("Error occurred while processing mail.");
1165 if (prefs_common.no_recv_err_panel)
1167 if (session->error_msg)
1168 err_msg = g_strdup_printf
1169 (_("Error occurred while processing mail:\n%s"),
1170 session->error_msg);
1172 err_msg = g_strdup(log_msg);
1175 log_msg = _("No disk space left.");
1176 err_msg = g_strdup(log_msg);
1180 log_msg = _("Can't write file.");
1181 err_msg = g_strdup(log_msg);
1184 case INC_SOCKET_ERROR:
1185 log_msg = _("Socket error.");
1186 if (prefs_common.no_recv_err_panel)
1188 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1189 SESSION(session)->server,
1190 SESSION(session)->port);
1193 log_msg = _("Connection closed by the remote host.");
1194 if (prefs_common.no_recv_err_panel)
1196 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1197 SESSION(session)->server,
1198 SESSION(session)->port);
1201 log_msg = _("Mailbox is locked.");
1202 if (prefs_common.no_recv_err_panel)
1204 if (session->error_msg)
1205 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1206 session->error_msg);
1208 err_msg = g_strdup(log_msg);
1210 case INC_AUTH_FAILED:
1211 log_msg = _("Authentication failed.");
1213 if (prefs_common.no_recv_err_panel)
1215 if (session->error_msg)
1216 err_msg = g_strdup_printf
1217 (_("Authentication failed:\n%s"), session->error_msg);
1219 err_msg = g_strdup(log_msg);
1222 log_msg = _("Session timed out. You may be able to "
1223 "recover by increasing the timeout value in "
1224 "Preferences/Other/Miscellaneous.");
1225 if (prefs_common.no_recv_err_panel)
1227 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1228 SESSION(session)->server,
1229 SESSION(session)->port);
1237 log_error(LOG_PROTOCOL, "%s\n", log_msg);
1239 log_warning(LOG_PROTOCOL, "%s\n", log_msg);
1241 if (prefs_common.no_recv_err_panel && fatal_error)
1242 mainwindow_show_error();
1245 alertpanel_error_log("%s", err_msg);
1250 static void inc_cancel(IncProgressDialog *dialog)
1252 IncSession *session;
1254 cm_return_if_fail(dialog != NULL);
1256 if (dialog->queue_list == NULL) {
1257 inc_progress_dialog_destroy(dialog);
1261 session = dialog->queue_list->data;
1263 session->inc_state = INC_CANCEL;
1265 log_message(LOG_PROTOCOL, _("Incorporation cancelled\n"));
1268 gboolean inc_is_active(void)
1270 return (inc_dialog_list != NULL);
1273 void inc_cancel_all(void)
1277 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1278 inc_cancel((IncProgressDialog *)cur->data);
1281 static void inc_showlog_cb(GtkWidget *widget, gpointer data)
1283 MainWindow *mainwin = mainwindow_get_mainwindow();
1285 log_window_show(mainwin->logwin);
1288 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1290 inc_cancel((IncProgressDialog *)data);
1293 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1296 IncProgressDialog *dialog = (IncProgressDialog *)data;
1298 if (dialog->queue_list == NULL)
1299 inc_progress_dialog_destroy(dialog);
1304 static gint inc_spool_account(PrefsAccount *account)
1310 if (account->local_inbox) {
1311 inbox = folder_find_item_from_identifier(account->local_inbox);
1313 inbox = folder_get_default_inbox();
1315 inbox = folder_get_default_inbox();
1317 if (account->local_mbox) {
1318 if (is_file_exist(account->local_mbox))
1319 mbox = g_strdup(account->local_mbox);
1320 else if (is_dir_exist(account->local_mbox))
1321 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1322 g_get_user_name(), NULL);
1324 debug_print("%s: local mailbox not found.\n",
1325 account->local_mbox);
1329 debug_print("local mailbox not set in account info.\n");
1333 result = get_spool(inbox, mbox, account);
1336 statusbar_pop_all();
1341 static gint inc_all_spool(void)
1345 gint account_new_msgs = 0;
1347 list = account_get_list();
1348 if (!list) return 0;
1350 for (; list != NULL; list = list->next) {
1351 PrefsAccount *account = list->data;
1353 if ((account->protocol == A_LOCAL) &&
1354 (account->recv_at_getall)) {
1355 account_new_msgs = inc_spool_account(account);
1356 if (account_new_msgs > 0)
1357 new_msgs += account_new_msgs;
1364 static gint get_spool(FolderItem *dest, const gchar *mbox, PrefsAccount *account)
1368 gchar tmp_mbox[MAXPATHLEN + 1];
1370 cm_return_val_if_fail(dest != NULL, -1);
1371 cm_return_val_if_fail(mbox != NULL, -1);
1372 cm_return_val_if_fail(account != NULL, -1);
1374 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1375 debug_print("%s: no messages in local mailbox.\n", mbox);
1377 } else if (size < 0)
1380 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1383 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%p",
1384 get_tmp_dir(), G_DIR_SEPARATOR, mbox);
1386 if (copy_mbox(lockfd, tmp_mbox) < 0) {
1387 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1391 debug_print("Getting new messages from %s into %s...\n",
1394 msgs = proc_mbox(dest, tmp_mbox, account->filter_on_recv, account);
1396 claws_unlink(tmp_mbox);
1397 if (msgs >= 0) empty_mbox(mbox);
1398 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1403 void inc_lock_real(void)
1408 void inc_unlock_real(void)
1410 if (inc_lock_count > 0)
1414 static guint autocheck_timer = 0;
1415 static gpointer autocheck_data = NULL;
1417 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1419 gchar *buf, *numpos, *ret_str;
1420 gssize by_read = 0, by_written = 0;
1422 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1423 *prefs_common.newmail_notify_cmd))
1426 buf = g_strdup(prefs_common.newmail_notify_cmd);
1427 if ((numpos = strstr(buf, "%d")) != NULL) {
1431 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1436 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1438 if (ret_str && by_written) {
1442 debug_print("executing new mail notification command: %s\n", buf);
1443 execute_command_line(buf, TRUE);
1448 void inc_autocheck_timer_init(MainWindow *mainwin)
1450 autocheck_data = mainwin;
1451 inc_autocheck_timer_set();
1454 static void inc_autocheck_timer_set_interval(guint interval)
1456 inc_autocheck_timer_remove();
1457 /* last test is to avoid re-enabling auto_check after modifying
1458 the common preferences */
1459 if (prefs_common.autochk_newmail && autocheck_data
1460 && prefs_common.work_offline == FALSE) {
1461 #if GLIB_CHECK_VERSION(2,14,0)
1462 if (interval % 1000 == 0)
1464 g_timeout_add_seconds(interval/1000, inc_autocheck_func, autocheck_data);
1467 autocheck_timer = g_timeout_add
1468 (interval, inc_autocheck_func, autocheck_data);
1469 debug_print("added timer = %d\n", autocheck_timer);
1473 void inc_autocheck_timer_set(void)
1475 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1478 void inc_autocheck_timer_remove(void)
1480 if (autocheck_timer) {
1481 debug_print("removed timer = %d\n", autocheck_timer);
1482 g_source_remove(autocheck_timer);
1483 autocheck_timer = 0;
1487 static gint inc_autocheck_func(gpointer data)
1489 MainWindow *mainwin = (MainWindow *)data;
1491 if (inc_lock_count) {
1492 debug_print("autocheck is locked.\n");
1493 inc_autocheck_timer_set_interval(1000);
1497 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1502 gboolean inc_offline_should_override(gboolean force_ask, const gchar *msg)
1504 static time_t overridden_yes = 0;
1505 static time_t overridden_no = 0;
1506 int length = 10; /* minutes */
1507 gint answer = G_ALERTDEFAULT;
1509 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1510 /* If no network connection is available, override is not possible */
1511 if(!networkmanager_is_online(NULL))
1515 if (prefs_common.autochk_newmail)
1516 length = prefs_common.autochk_itv; /* minutes */
1519 overridden_no = (time_t)0;
1522 if (prefs_common.work_offline) {
1525 if (time(NULL) - overridden_yes < length * 60) /* seconds */
1527 else if (time(NULL) - overridden_no < length * 60) /* seconds */
1531 tmp = g_strdup_printf(
1532 _("%s%sYou're working offline. Override for %d minutes?"),
1537 tmp = g_strdup_printf(
1538 _("%s%sYou're working offline. Override?"),
1542 answer = alertpanel(_("Offline warning"),
1544 GTK_STOCK_NO, "+" GTK_STOCK_YES,
1545 !force_ask? _("On_ly once"):NULL);
1547 if (answer == G_ALERTALTERNATE) {
1548 overridden_yes = time(NULL);
1550 } else if (answer == G_ALERTDEFAULT) {
1552 overridden_no = time(NULL);
1555 overridden_yes = (time_t)0;
1556 overridden_no = (time_t)0;