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"
62 #include "passwordstore.h"
64 extern SessionStats session_stats;
66 static GList *inc_dialog_list = NULL;
68 guint inc_lock_count = 0;
70 static GdkPixbuf *currentpix;
71 static GdkPixbuf *errorpix;
72 static GdkPixbuf *okpix;
74 #define MESSAGEBUFSIZE 8192
76 static void inc_update_stats(gint new_msgs);
77 static void inc_finished (MainWindow *mainwin,
78 gboolean new_messages,
80 static gint inc_account_mail_real (MainWindow *mainwin,
81 PrefsAccount *account);
83 static IncProgressDialog *inc_progress_dialog_create
85 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
86 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
88 static IncSession *inc_session_new (PrefsAccount *account);
89 static void inc_session_destroy (IncSession *session);
90 static gint inc_start (IncProgressDialog *inc_dialog);
91 static IncState inc_pop3_session_do (IncSession *session);
93 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
94 IncSession *inc_session);
96 static void inc_progress_dialog_set_label
97 (IncProgressDialog *inc_dialog,
98 IncSession *inc_session);
99 static void inc_progress_dialog_set_progress
100 (IncProgressDialog *inc_dialog,
101 IncSession *inc_session);
103 static void inc_progress_dialog_update_periodic
104 (IncProgressDialog *inc_dialog,
105 IncSession *inc_session);
107 static gint inc_recv_data_progressive (Session *session,
111 static gint inc_recv_data_finished (Session *session,
114 static gint inc_recv_message (Session *session,
117 static gint inc_drop_message (Pop3Session *session,
120 static void inc_put_error (IncState istate,
121 Pop3Session *session);
123 static void inc_showlog_cb (GtkWidget *widget,
125 static void inc_cancel_cb (GtkWidget *widget,
127 static gint inc_dialog_delete_cb (GtkWidget *widget,
131 static gint get_spool (FolderItem *dest,
133 PrefsAccount *account);
135 static gint inc_spool_account(PrefsAccount *account);
136 static gint inc_all_spool(void);
137 static void inc_autocheck_timer_set_interval (guint interval);
138 static gint inc_autocheck_func (gpointer data);
140 static void inc_notify_cmd (gint new_msgs,
143 static void inc_update_stats(gint new_msgs)
145 /* update session statistics */
146 session_stats.received += new_msgs;
151 * @mainwin: Main window.
152 * @new_messages: TRUE if some messages have been received.
154 * Update the folder view and the summary view after receiving
155 * messages. If @new_messages is FALSE, this function avoids unneeded
158 static void inc_finished(MainWindow *mainwin, gboolean new_messages, gboolean autocheck)
160 if (prefs_common.scan_all_after_inc)
161 folderview_check_new(NULL);
163 if (!autocheck && new_messages && prefs_common.open_inbox_on_inc) {
164 FolderItem *item = NULL;
166 if (cur_account && cur_account->inbox)
167 item = folder_find_item_from_identifier(cur_account->inbox);
168 if (item == NULL && cur_account && cur_account->folder)
169 item = cur_account->folder->inbox;
171 item = folder_get_default_inbox();
173 folderview_unselect(mainwin->folderview);
174 folderview_select(mainwin->folderview, item);
178 void inc_mail(MainWindow *mainwin, gboolean notify)
181 gint account_new_msgs = 0;
183 if (inc_lock_count) return;
185 if (prefs_common.work_offline &&
186 !inc_offline_should_override(TRUE,
187 _("Claws Mail needs network access in order "
192 inc_autocheck_timer_remove();
193 main_window_lock(mainwin);
195 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
196 /* external incorporating program */
197 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
198 main_window_unlock(mainwin);
199 inc_autocheck_timer_set();
204 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
205 if (account_new_msgs > 0)
206 new_msgs += account_new_msgs;
209 inc_update_stats(new_msgs);
210 inc_finished(mainwin, new_msgs > 0, FALSE);
211 main_window_unlock(mainwin);
212 inc_notify_cmd(new_msgs, notify);
213 inc_autocheck_timer_set();
217 void inc_pop_before_smtp(PrefsAccount *acc)
219 IncProgressDialog *inc_dialog;
223 mainwin = mainwindow_get_mainwindow();
225 session = inc_session_new(acc);
226 if (!session) return;
227 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
229 inc_dialog = inc_progress_dialog_create(FALSE);
230 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
232 /* FIXME: assumes to attach to first main window */
233 inc_dialog->mainwin = mainwin;
234 inc_progress_dialog_set_list(inc_dialog);
237 toolbar_main_set_sensitive(mainwin);
238 main_window_set_menu_sensitive(mainwin);
241 inc_start(inc_dialog);
244 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
246 IncProgressDialog *inc_dialog;
249 switch (account->protocol) {
253 * FIXME: it should return foldeview_check_new() value.
254 * TODO: do it when bug [19] is fixed (IMAP folder sets
255 * an incorrect new message count)
257 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 (inc_dialog->show_dialog)
573 manage_window_focus_in
574 (inc_dialog->dialog->window,
577 if (password_get(pop3_session->user,
578 pop3_session->ac_prefs->recv_server,
579 "pop3", pop3_get_port(pop3_session),
580 &(pop3_session->pass))) {
582 } else if ((pop3_session->pass = passwd_store_get_account(
583 pop3_session->ac_prefs->account_id, PWS_ACCOUNT_RECV)) == NULL) {
586 pass = input_dialog_query_password_keep
587 (pop3_session->ac_prefs->recv_server,
589 &(pop3_session->ac_prefs->session_passwd));
592 pop3_session->pass = pass;
596 if (inc_dialog->show_dialog)
597 manage_window_focus_out
598 (inc_dialog->dialog->window,
604 #define SET_PIXMAP_AND_TEXT(pix, str) \
606 progress_dialog_list_set(inc_dialog->dialog, \
607 inc_dialog->cur_row, \
613 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
614 session = inc_dialog->queue_list->data;
615 pop3_session = POP3_SESSION(session->session);
616 GSList *filtered, *unfiltered;
618 if (pop3_session->pass == NULL) {
619 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
620 inc_session_destroy(session);
621 inc_dialog->queue_list =
622 g_list_remove(inc_dialog->queue_list, session);
626 inc_progress_dialog_clear(inc_dialog);
627 progress_dialog_scroll_to_row(inc_dialog->dialog,
628 inc_dialog->cur_row);
630 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
632 /* begin POP3 session */
633 inc_state = inc_pop3_session_do(session);
637 if (pop3_session->cur_total_num > 0)
638 msg = g_strdup_printf(
639 ngettext("Done (%d message (%s) received)",
640 "Done (%d messages (%s) received)",
641 pop3_session->cur_total_num),
642 pop3_session->cur_total_num,
643 to_human_readable((goffset)pop3_session->cur_total_recv_bytes));
645 msg = g_strdup_printf(_("Done (no new messages)"));
646 SET_PIXMAP_AND_TEXT(okpix, msg);
649 case INC_CONNECT_ERROR:
650 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
652 case INC_AUTH_FAILED:
653 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
654 if (pop3_session->ac_prefs->session_passwd) {
655 g_free(pop3_session->ac_prefs->session_passwd);
656 pop3_session->ac_prefs->session_passwd = NULL;
660 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
665 case INC_SOCKET_ERROR:
667 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
670 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
673 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
674 if (!inc_dialog->show_dialog)
681 if (pop3_session->error_val == PS_AUTHFAIL) {
682 if(!prefs_common.no_recv_err_panel) {
683 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
684 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
685 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
689 /* CLAWS: perform filtering actions on dropped message */
690 /* CLAWS: get default inbox (perhaps per account) */
691 if (pop3_session->ac_prefs->inbox) {
692 /* CLAWS: get destination folder / mailbox */
693 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
695 inbox = folder_get_default_inbox();
697 inbox = folder_get_default_inbox();
699 /* get list of messages in processing */
700 processing = folder_get_default_processing(pop3_session->ac_prefs->account_id);
701 folder_item_scan(processing);
702 msglist = folder_item_get_msg_list(processing);
704 /* process messages */
705 folder_item_update_freeze();
707 procmsg_msglist_filter(msglist, pop3_session->ac_prefs,
708 &filtered, &unfiltered,
709 pop3_session->ac_prefs->filter_on_recv);
711 filtering_move_and_copy_msgs(msglist);
712 if (unfiltered != NULL)
713 folder_item_move_msgs(inbox, unfiltered);
715 for(msglist_element = msglist; msglist_element != NULL;
716 msglist_element = msglist_element->next) {
717 procmsg_msginfo_free((MsgInfo**)&(msglist_element->data));
719 folder_item_update_thaw();
721 g_slist_free(msglist);
722 g_slist_free(filtered);
723 g_slist_free(unfiltered);
727 new_msgs += pop3_session->cur_total_num;
729 pop3_write_uidl_list(pop3_session);
731 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
733 if (inc_dialog->show_dialog)
734 manage_window_focus_in
735 (inc_dialog->dialog->window,
737 inc_put_error(inc_state, pop3_session);
738 if (inc_dialog->show_dialog)
739 manage_window_focus_out
740 (inc_dialog->dialog->window,
742 if (inc_state == INC_NO_SPACE ||
743 inc_state == INC_IO_ERROR)
746 folder_item_free_cache(processing, TRUE);
748 inc_session_destroy(session);
749 inc_dialog->queue_list =
750 g_list_remove(inc_dialog->queue_list, session);
753 #undef SET_PIXMAP_AND_TEXT
756 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
757 "Finished (%d new messages)",
758 new_msgs), new_msgs);
760 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
762 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
764 while (inc_dialog->queue_list != NULL) {
765 session = inc_dialog->queue_list->data;
766 inc_session_destroy(session);
767 inc_dialog->queue_list =
768 g_list_remove(inc_dialog->queue_list, session);
771 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
772 inc_progress_dialog_destroy(inc_dialog);
774 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
776 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
785 static IncState inc_pop3_session_do(IncSession *session)
787 Pop3Session *pop3_session = POP3_SESSION(session->session);
788 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
794 debug_print("getting new messages of account %s...\n",
795 pop3_session->ac_prefs->account_name);
797 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
799 buf = g_strdup_printf(_("%s: Retrieving new messages"),
800 pop3_session->ac_prefs->recv_server);
801 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
804 server = pop3_session->ac_prefs->recv_server;
805 account_name = pop3_session->ac_prefs->account_name;
806 port = pop3_get_port(pop3_session);
809 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
810 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
811 SESSION(pop3_session)->nonblocking =
812 pop3_session->ac_prefs->use_nonblocking_ssl;
814 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE) {
815 if (alertpanel_full(_("Insecure connection"),
816 _("This connection is configured to be secured "
817 "using SSL, but SSL is not available in this "
818 "build of Claws Mail. \n\n"
819 "Do you want to continue connecting to this "
820 "server? The communication would not be "
822 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
823 NULL, FALSE, NULL, ALERT_WARNING,
824 G_ALERTDEFAULT) != G_ALERTALTERNATE)
829 buf = g_strdup_printf(_("Account '%s': Connecting to POP3 server: %s:%d..."),
830 account_name, server, port);
831 statuswindow_print_all("%s", buf);
832 log_message(LOG_PROTOCOL, "%s\n", buf);
834 progress_dialog_set_label(inc_dialog->dialog, buf);
838 session_set_timeout(SESSION(pop3_session),
839 prefs_common.io_timeout_secs * 1000);
841 if (session_connect(SESSION(pop3_session), server, port) < 0) {
842 if(!prefs_common.no_recv_err_panel) {
843 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
844 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
845 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
847 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
849 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
851 log_error(LOG_PROTOCOL, _("Can't connect to POP3 server: %s:%d\n"),
854 session->inc_state = INC_CONNECT_ERROR;
855 statuswindow_pop_all();
856 return INC_CONNECT_ERROR;
859 while (session_is_running(SESSION(pop3_session)) &&
860 session->inc_state != INC_CANCEL)
861 gtk_main_iteration();
863 if (session->inc_state == INC_SUCCESS) {
864 switch (pop3_session->error_val) {
866 switch (SESSION(pop3_session)->state) {
868 if (pop3_session->state == POP3_READY)
869 session->inc_state = INC_CONNECT_ERROR;
871 session->inc_state = INC_ERROR;
874 session->inc_state = INC_EOF;
876 case SESSION_TIMEOUT:
877 session->inc_state = INC_TIMEOUT;
880 session->inc_state = INC_SUCCESS;
885 session->inc_state = INC_AUTH_FAILED;
888 session->inc_state = INC_IO_ERROR;
891 session->inc_state = INC_SOCKET_ERROR;
894 session->inc_state = INC_LOCKED;
897 session->inc_state = INC_ERROR;
902 session_disconnect(SESSION(pop3_session));
905 return session->inc_state;
908 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
909 IncSession *inc_session)
911 inc_progress_dialog_set_label(inc_dialog, inc_session);
912 inc_progress_dialog_set_progress(inc_dialog, inc_session);
915 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
916 IncSession *inc_session)
918 ProgressDialog *dialog = inc_dialog->dialog;
919 Pop3Session *session;
921 cm_return_if_fail(inc_session != NULL);
923 session = POP3_SESSION(inc_session->session);
925 switch (session->state) {
928 case POP3_GETAUTH_USER:
929 case POP3_GETAUTH_PASS:
930 case POP3_GETAUTH_APOP:
931 progress_dialog_set_label(dialog, _("Authenticating..."));
932 statuswindow_pop_all();
933 statusbar_print_all(_("Retrieving messages from %s (%s)..."),
934 SESSION(session)->server,
935 session->ac_prefs->account_name);
937 case POP3_GETRANGE_STAT:
938 progress_dialog_set_label
939 (dialog, _("Getting the number of new messages (STAT)..."));
941 case POP3_GETRANGE_LAST:
942 progress_dialog_set_label
943 (dialog, _("Getting the number of new messages (LAST)..."));
945 case POP3_GETRANGE_UIDL:
946 progress_dialog_set_label
947 (dialog, _("Getting the number of new messages (UIDL)..."));
949 case POP3_GETSIZE_LIST:
950 progress_dialog_set_label
951 (dialog, _("Getting the size of messages (LIST)..."));
958 progress_dialog_set_label(dialog, _("Quitting"));
965 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
966 IncSession *inc_session)
968 gchar buf[MESSAGEBUFSIZE];
969 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
970 gchar *total_size_str;
974 if (!pop3_session->new_msg_exist) return;
976 cur_total = inc_session->cur_total_bytes;
977 total = pop3_session->total_bytes;
978 if (pop3_session->state == POP3_RETR ||
979 pop3_session->state == POP3_RETR_RECV ||
980 pop3_session->state == POP3_DELETE) {
981 Xstrdup_a(total_size_str, to_human_readable((goffset)total), return);
982 g_snprintf(buf, sizeof(buf),
983 _("Retrieving message (%d / %d) (%s / %s)"),
984 pop3_session->cur_msg, pop3_session->count,
985 to_human_readable((goffset)cur_total), total_size_str);
986 progress_dialog_set_label(inc_dialog->dialog, buf);
989 progress_dialog_set_fraction
990 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
992 statusbar_progress_all(pop3_session->cur_msg, pop3_session->count, 1);
994 if (pop3_session->cur_total_num > 0) {
995 g_snprintf(buf, sizeof(buf),
996 ngettext("Retrieving (%d message (%s) received)",
997 "Retrieving (%d messages (%s) received)",
998 pop3_session->cur_total_num),
999 pop3_session->cur_total_num,
1001 ((goffset)pop3_session->cur_total_recv_bytes));
1002 progress_dialog_list_set_status(inc_dialog->dialog,
1003 inc_dialog->cur_row,
1008 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
1009 IncSession *inc_session)
1015 g_get_current_time(&tv_cur);
1017 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
1018 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
1019 if (tv_result.tv_usec < 0) {
1021 tv_result.tv_usec += G_USEC_PER_SEC;
1024 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
1025 if (msec > PROGRESS_UPDATE_INTERVAL) {
1026 inc_progress_dialog_update(inc_dialog, inc_session);
1027 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
1028 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
1032 static gint inc_recv_data_progressive(Session *session, guint cur_len,
1033 guint total_len, gpointer data)
1035 IncSession *inc_session = (IncSession *)data;
1036 Pop3Session *pop3_session = POP3_SESSION(session);
1037 IncProgressDialog *inc_dialog;
1040 cm_return_val_if_fail(inc_session != NULL, -1);
1042 if (pop3_session->state != POP3_RETR &&
1043 pop3_session->state != POP3_RETR_RECV &&
1044 pop3_session->state != POP3_DELETE &&
1045 pop3_session->state != POP3_LOGOUT) return 0;
1047 if (!pop3_session->new_msg_exist) return 0;
1049 cur_total = pop3_session->cur_total_bytes + cur_len;
1050 if (cur_total > pop3_session->total_bytes)
1051 cur_total = pop3_session->total_bytes;
1052 inc_session->cur_total_bytes = cur_total;
1054 inc_dialog = (IncProgressDialog *)inc_session->data;
1055 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1060 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1062 IncSession *inc_session = (IncSession *)data;
1063 IncProgressDialog *inc_dialog;
1065 cm_return_val_if_fail(inc_session != NULL, -1);
1067 inc_dialog = (IncProgressDialog *)inc_session->data;
1069 inc_recv_data_progressive(session, 0, 0, inc_session);
1071 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1072 inc_progress_dialog_update(inc_dialog, inc_session);
1078 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1080 IncSession *inc_session = (IncSession *)data;
1081 IncProgressDialog *inc_dialog;
1083 cm_return_val_if_fail(inc_session != NULL, -1);
1085 inc_dialog = (IncProgressDialog *)inc_session->data;
1087 switch (POP3_SESSION(session)->state) {
1088 case POP3_GETAUTH_USER:
1089 case POP3_GETAUTH_PASS:
1090 case POP3_GETAUTH_APOP:
1091 case POP3_GETRANGE_STAT:
1092 case POP3_GETRANGE_LAST:
1093 case POP3_GETRANGE_UIDL:
1094 case POP3_GETSIZE_LIST:
1095 inc_progress_dialog_update(inc_dialog, inc_session);
1098 inc_recv_data_progressive(session, 0, 0, inc_session);
1101 inc_progress_dialog_update(inc_dialog, inc_session);
1110 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1113 FolderItem *dropfolder;
1114 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1117 cm_return_val_if_fail(inc_session != NULL, -1);
1119 if (session->ac_prefs->inbox) {
1120 inbox = folder_find_item_from_identifier
1121 (session->ac_prefs->inbox);
1123 inbox = folder_get_default_inbox();
1125 inbox = folder_get_default_inbox();
1131 /* CLAWS: claws uses a global .processing folder for the filtering. */
1132 dropfolder = folder_get_default_processing(session->ac_prefs->account_id);
1134 /* add msg file to drop folder */
1135 if ((msgnum = folder_item_add_msg(
1136 dropfolder, file, NULL, TRUE)) < 0) {
1144 static void inc_put_error(IncState istate, Pop3Session *session)
1146 gchar *log_msg = NULL;
1147 gchar *err_msg = NULL;
1148 gboolean fatal_error = FALSE;
1151 case INC_CONNECT_ERROR:
1153 if (prefs_common.no_recv_err_panel)
1155 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1156 SESSION(session)->server,
1157 SESSION(session)->port);
1160 log_msg = _("Error occurred while processing mail.");
1162 if (prefs_common.no_recv_err_panel)
1164 if (session->error_msg)
1165 err_msg = g_strdup_printf
1166 (_("Error occurred while processing mail:\n%s"),
1167 session->error_msg);
1169 err_msg = g_strdup(log_msg);
1172 log_msg = _("No disk space left.");
1173 err_msg = g_strdup(log_msg);
1177 log_msg = _("Can't write file.");
1178 err_msg = g_strdup(log_msg);
1181 case INC_SOCKET_ERROR:
1182 log_msg = _("Socket error.");
1183 if (prefs_common.no_recv_err_panel)
1185 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1186 SESSION(session)->server,
1187 SESSION(session)->port);
1190 log_msg = _("Connection closed by the remote host.");
1191 if (prefs_common.no_recv_err_panel)
1193 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1194 SESSION(session)->server,
1195 SESSION(session)->port);
1198 log_msg = _("Mailbox is locked.");
1199 if (prefs_common.no_recv_err_panel)
1201 if (session->error_msg)
1202 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1203 session->error_msg);
1205 err_msg = g_strdup(log_msg);
1207 case INC_AUTH_FAILED:
1208 log_msg = _("Authentication failed.");
1210 if (prefs_common.no_recv_err_panel)
1212 if (session->error_msg)
1213 err_msg = g_strdup_printf
1214 (_("Authentication failed:\n%s"), session->error_msg);
1216 err_msg = g_strdup(log_msg);
1219 log_msg = _("Session timed out. You may be able to "
1220 "recover by increasing the timeout value in "
1221 "Preferences/Other/Miscellaneous.");
1222 if (prefs_common.no_recv_err_panel)
1224 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1225 SESSION(session)->server,
1226 SESSION(session)->port);
1234 log_error(LOG_PROTOCOL, "%s\n", log_msg);
1236 log_warning(LOG_PROTOCOL, "%s\n", log_msg);
1238 if (prefs_common.no_recv_err_panel && fatal_error)
1239 mainwindow_show_error();
1242 alertpanel_error_log("%s", err_msg);
1247 static void inc_cancel(IncProgressDialog *dialog)
1249 IncSession *session;
1251 cm_return_if_fail(dialog != NULL);
1253 if (dialog->queue_list == NULL) {
1254 inc_progress_dialog_destroy(dialog);
1258 session = dialog->queue_list->data;
1260 session->inc_state = INC_CANCEL;
1262 log_message(LOG_PROTOCOL, _("Incorporation cancelled\n"));
1265 gboolean inc_is_active(void)
1267 return (inc_dialog_list != NULL);
1270 void inc_cancel_all(void)
1274 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1275 inc_cancel((IncProgressDialog *)cur->data);
1278 static void inc_showlog_cb(GtkWidget *widget, gpointer data)
1280 MainWindow *mainwin = mainwindow_get_mainwindow();
1282 log_window_show(mainwin->logwin);
1285 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1287 inc_cancel((IncProgressDialog *)data);
1290 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1293 IncProgressDialog *dialog = (IncProgressDialog *)data;
1295 if (dialog->queue_list == NULL)
1296 inc_progress_dialog_destroy(dialog);
1301 static gint inc_spool_account(PrefsAccount *account)
1307 if (account->local_inbox) {
1308 inbox = folder_find_item_from_identifier(account->local_inbox);
1310 inbox = folder_get_default_inbox();
1312 inbox = folder_get_default_inbox();
1314 if (account->local_mbox) {
1315 if (is_file_exist(account->local_mbox))
1316 mbox = g_strdup(account->local_mbox);
1317 else if (is_dir_exist(account->local_mbox))
1318 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1319 g_get_user_name(), NULL);
1321 debug_print("%s: local mailbox not found.\n",
1322 account->local_mbox);
1326 debug_print("local mailbox not set in account info.\n");
1330 result = get_spool(inbox, mbox, account);
1333 statusbar_pop_all();
1338 static gint inc_all_spool(void)
1342 gint account_new_msgs = 0;
1344 list = account_get_list();
1345 if (!list) return 0;
1347 for (; list != NULL; list = list->next) {
1348 PrefsAccount *account = list->data;
1350 if ((account->protocol == A_LOCAL) &&
1351 (account->recv_at_getall)) {
1352 account_new_msgs = inc_spool_account(account);
1353 if (account_new_msgs > 0)
1354 new_msgs += account_new_msgs;
1361 static gint get_spool(FolderItem *dest, const gchar *mbox, PrefsAccount *account)
1365 gchar tmp_mbox[MAXPATHLEN + 1];
1367 cm_return_val_if_fail(dest != NULL, -1);
1368 cm_return_val_if_fail(mbox != NULL, -1);
1369 cm_return_val_if_fail(account != NULL, -1);
1371 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1372 debug_print("%s: no messages in local mailbox.\n", mbox);
1374 } else if (size < 0)
1377 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1380 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%p",
1381 get_tmp_dir(), G_DIR_SEPARATOR, mbox);
1383 if (copy_mbox(lockfd, tmp_mbox) < 0) {
1384 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1388 debug_print("Getting new messages from %s into %s...\n",
1391 msgs = proc_mbox(dest, tmp_mbox, account->filter_on_recv, account);
1393 claws_unlink(tmp_mbox);
1394 if (msgs >= 0) empty_mbox(mbox);
1395 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1400 void inc_lock_real(void)
1405 void inc_unlock_real(void)
1407 if (inc_lock_count > 0)
1411 static guint autocheck_timer = 0;
1412 static gpointer autocheck_data = NULL;
1414 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1416 gchar *buf, *numpos, *ret_str;
1417 gssize by_read = 0, by_written = 0;
1419 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1420 *prefs_common.newmail_notify_cmd))
1423 buf = g_strdup(prefs_common.newmail_notify_cmd);
1424 if ((numpos = strstr(buf, "%d")) != NULL) {
1428 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1433 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1435 if (ret_str && by_written) {
1439 debug_print("executing new mail notification command: %s\n", buf);
1440 execute_command_line(buf, TRUE);
1445 void inc_autocheck_timer_init(MainWindow *mainwin)
1447 autocheck_data = mainwin;
1448 inc_autocheck_timer_set();
1451 static void inc_autocheck_timer_set_interval(guint interval)
1453 inc_autocheck_timer_remove();
1454 /* last test is to avoid re-enabling auto_check after modifying
1455 the common preferences */
1456 if (prefs_common.autochk_newmail && autocheck_data
1457 && prefs_common.work_offline == FALSE) {
1458 if (interval % 1000 == 0)
1460 g_timeout_add_seconds(interval/1000, inc_autocheck_func, autocheck_data);
1462 autocheck_timer = g_timeout_add
1463 (interval, inc_autocheck_func, autocheck_data);
1464 debug_print("added timer = %d\n", autocheck_timer);
1468 void inc_autocheck_timer_set(void)
1470 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1473 void inc_autocheck_timer_remove(void)
1475 if (autocheck_timer) {
1476 debug_print("removed timer = %d\n", autocheck_timer);
1477 g_source_remove(autocheck_timer);
1478 autocheck_timer = 0;
1482 static gint inc_autocheck_func(gpointer data)
1484 MainWindow *mainwin = (MainWindow *)data;
1486 if (inc_lock_count) {
1487 debug_print("autocheck is locked.\n");
1488 inc_autocheck_timer_set_interval(1000);
1492 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1497 gboolean inc_offline_should_override(gboolean force_ask, const gchar *msg)
1499 static time_t overridden_yes = 0;
1500 static time_t overridden_no = 0;
1501 int length = 10; /* minutes */
1502 gint answer = G_ALERTDEFAULT;
1504 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1505 /* If no network connection is available, override is not possible */
1506 if(!networkmanager_is_online(NULL))
1510 if (prefs_common.autochk_newmail)
1511 length = prefs_common.autochk_itv; /* minutes */
1514 overridden_no = (time_t)0;
1517 if (prefs_common.work_offline) {
1520 if (time(NULL) - overridden_yes < length * 60) /* seconds */
1522 else if (time(NULL) - overridden_no < length * 60) /* seconds */
1526 tmp = g_strdup_printf(
1527 _("%s%sYou're working offline. Override for %d minutes?"),
1532 tmp = g_strdup_printf(
1533 _("%s%sYou're working offline. Override?"),
1537 answer = alertpanel(_("Offline warning"),
1539 GTK_STOCK_NO, "+" GTK_STOCK_YES,
1540 !force_ask? _("On_ly once"):NULL);
1542 if (answer == G_ALERTALTERNATE) {
1543 overridden_yes = time(NULL);
1545 } else if (answer == G_ALERTDEFAULT) {
1547 overridden_no = time(NULL);
1550 overridden_yes = (time_t)0;
1551 overridden_no = (time_t)0;