2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkprogressbar.h>
38 #include "mainwindow.h"
39 #include "folderview.h"
40 #include "summaryview.h"
41 #include "prefs_common.h"
42 #include "prefs_account.h"
52 #include "statusbar.h"
53 #include "manage_window.h"
54 #include "stock_pixmap.h"
55 #include "progressdialog.h"
56 #include "inputdialog.h"
57 #include "alertpanel.h"
59 #include "filtering.h"
63 static GList *inc_dialog_list = NULL;
65 static guint inc_lock_count = 0;
67 static GdkPixbuf *currentpix;
68 static GdkPixbuf *errorpix;
69 static GdkPixbuf *okpix;
71 #define MSGBUFSIZE 8192
73 static void inc_finished (MainWindow *mainwin,
74 gboolean new_messages);
75 static gint inc_account_mail_real (MainWindow *mainwin,
76 PrefsAccount *account);
78 static IncProgressDialog *inc_progress_dialog_create
80 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
81 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
83 static IncSession *inc_session_new (PrefsAccount *account);
84 static void inc_session_destroy (IncSession *session);
85 static gint inc_start (IncProgressDialog *inc_dialog);
86 static IncState inc_pop3_session_do (IncSession *session);
88 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
89 IncSession *inc_session);
91 static void inc_progress_dialog_set_label
92 (IncProgressDialog *inc_dialog,
93 IncSession *inc_session);
94 static void inc_progress_dialog_set_progress
95 (IncProgressDialog *inc_dialog,
96 IncSession *inc_session);
98 static void inc_progress_dialog_update_periodic
99 (IncProgressDialog *inc_dialog,
100 IncSession *inc_session);
102 static gint inc_recv_data_progressive (Session *session,
106 static gint inc_recv_data_finished (Session *session,
109 static gint inc_recv_message (Session *session,
112 static gint inc_drop_message (Pop3Session *session,
115 static void inc_put_error (IncState istate,
116 Pop3Session *session);
118 static void inc_cancel_cb (GtkWidget *widget,
120 static gint inc_dialog_delete_cb (GtkWidget *widget,
124 static gint get_spool (FolderItem *dest,
127 static gint inc_spool_account(PrefsAccount *account);
128 static gint inc_all_spool(void);
129 static void inc_autocheck_timer_set_interval (guint interval);
130 static gint inc_autocheck_func (gpointer data);
132 static void inc_notify_cmd (gint new_msgs,
137 * @mainwin: Main window.
138 * @new_messages: TRUE if some messages have been received.
140 * Update the folder view and the summary view after receiving
141 * messages. If @new_messages is FALSE, this function avoids unneeded
144 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
148 if (prefs_common.scan_all_after_inc)
149 folderview_check_new(NULL);
151 if (!new_messages && !prefs_common.scan_all_after_inc) return;
153 if (prefs_common.open_inbox_on_inc) {
154 item = cur_account && cur_account->inbox
155 ? folder_find_item_from_identifier(cur_account->inbox)
156 : folder_get_default_inbox();
157 folderview_unselect(mainwin->folderview);
158 folderview_select(mainwin->folderview, item);
162 void inc_mail(MainWindow *mainwin, gboolean notify)
165 gint account_new_msgs = 0;
167 if (inc_lock_count) return;
169 if (prefs_common.work_offline &&
170 !inc_offline_should_override(
171 _("Sylpheed-Claws needs network access in order "
176 inc_autocheck_timer_remove();
177 main_window_lock(mainwin);
179 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
180 /* external incorporating program */
181 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
182 main_window_unlock(mainwin);
183 inc_autocheck_timer_set();
187 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
188 if (account_new_msgs > 0)
189 new_msgs += account_new_msgs;
192 inc_finished(mainwin, new_msgs > 0);
193 main_window_unlock(mainwin);
194 inc_notify_cmd(new_msgs, notify);
195 inc_autocheck_timer_set();
199 void inc_pop_before_smtp(PrefsAccount *acc)
201 IncProgressDialog *inc_dialog;
205 mainwin = mainwindow_get_mainwindow();
207 session = inc_session_new(acc);
208 if (!session) return;
209 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
211 inc_dialog = inc_progress_dialog_create(FALSE);
212 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
214 /* FIXME: assumes to attach to first main window */
215 inc_dialog->mainwin = mainwin;
216 inc_progress_dialog_set_list(inc_dialog);
219 toolbar_main_set_sensitive(mainwin);
220 main_window_set_menu_sensitive(mainwin);
223 inc_start(inc_dialog);
226 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
228 IncProgressDialog *inc_dialog;
231 switch (account->protocol) {
235 * FIXME: it should return foldeview_check_new() value.
236 * TODO: do it when bug [19] is fixed (IMAP folder sets
237 * an incorrect new message count)
239 folderview_check_new(FOLDER(account->folder));
243 session = inc_session_new(account);
244 if (!session) return 0;
246 inc_dialog = inc_progress_dialog_create(FALSE);
247 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
249 inc_dialog->mainwin = mainwin;
250 inc_progress_dialog_set_list(inc_dialog);
253 toolbar_main_set_sensitive(mainwin);
254 main_window_set_menu_sensitive(mainwin);
257 return inc_start(inc_dialog);
260 return inc_spool_account(account);
268 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
272 if (inc_lock_count) return 0;
274 if (prefs_common.work_offline &&
275 !inc_offline_should_override(
276 _("Sylpheed-Claws needs network access in order "
280 inc_autocheck_timer_remove();
281 main_window_lock(mainwin);
283 new_msgs = inc_account_mail_real(mainwin, account);
285 inc_finished(mainwin, new_msgs > 0);
286 main_window_unlock(mainwin);
287 inc_autocheck_timer_set();
292 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
295 GList *list, *queue_list = NULL;
296 IncProgressDialog *inc_dialog;
298 gint account_new_msgs = 0;
300 if (prefs_common.work_offline &&
301 !inc_offline_should_override(
302 _("Sylpheed-Claws needs network access in order "
306 if (inc_lock_count) return;
308 inc_autocheck_timer_remove();
309 main_window_lock(mainwin);
311 list = account_get_list();
313 inc_finished(mainwin, new_msgs > 0);
314 main_window_unlock(mainwin);
315 inc_notify_cmd(new_msgs, notify);
316 inc_autocheck_timer_set();
320 /* check local folders */
321 account_new_msgs = inc_all_spool();
322 if (account_new_msgs > 0)
323 new_msgs += account_new_msgs;
325 /* check IMAP4 / News folders */
326 for (list = account_get_list(); list != NULL; list = list->next) {
327 PrefsAccount *account = list->data;
328 if ((account->protocol == A_IMAP4 ||
329 account->protocol == A_NNTP) && account->recv_at_getall) {
330 new_msgs += folderview_check_new(FOLDER(account->folder));
334 /* check POP3 accounts */
335 for (list = account_get_list(); list != NULL; list = list->next) {
337 PrefsAccount *account = list->data;
339 if (account->recv_at_getall) {
340 session = inc_session_new(account);
342 queue_list = g_list_append(queue_list, session);
347 inc_dialog = inc_progress_dialog_create(autocheck);
348 inc_dialog->queue_list = queue_list;
349 inc_dialog->mainwin = mainwin;
350 inc_progress_dialog_set_list(inc_dialog);
352 toolbar_main_set_sensitive(mainwin);
353 main_window_set_menu_sensitive(mainwin);
354 new_msgs += inc_start(inc_dialog);
357 inc_finished(mainwin, new_msgs > 0);
358 main_window_unlock(mainwin);
359 inc_notify_cmd(new_msgs, notify);
360 inc_autocheck_timer_set();
363 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
365 IncProgressDialog *dialog;
366 ProgressDialog *progress;
368 dialog = g_new0(IncProgressDialog, 1);
370 progress = progress_dialog_create();
371 gtk_window_set_title(GTK_WINDOW(progress->window),
372 _("Retrieving new messages"));
373 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
374 G_CALLBACK(inc_cancel_cb), dialog);
375 g_signal_connect(G_OBJECT(progress->window), "delete_event",
376 G_CALLBACK(inc_dialog_delete_cb), dialog);
377 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
379 progress_dialog_get_fraction(progress);
381 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_COMPLETE,
383 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_CONTINUE,
385 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_ERROR,
388 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
389 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
391 dialog->show_dialog = TRUE;
392 gtk_widget_show_now(progress->window);
395 dialog->dialog = progress;
396 g_get_current_time(&dialog->progress_tv);
397 g_get_current_time(&dialog->folder_tv);
398 dialog->queue_list = NULL;
401 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
406 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
410 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
411 IncSession *session = list->data;
412 Pop3Session *pop3_session = POP3_SESSION(session->session);
414 session->data = inc_dialog;
416 progress_dialog_list_set(inc_dialog->dialog,
418 pop3_session->ac_prefs->account_name,
423 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
425 progress_dialog_get_fraction(inc_dialog->dialog);
426 progress_dialog_set_label(inc_dialog->dialog, "");
427 if (inc_dialog->mainwin)
428 main_window_progress_off(inc_dialog->mainwin);
431 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
433 g_return_if_fail(inc_dialog != NULL);
435 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
437 if (inc_dialog->mainwin)
438 main_window_progress_off(inc_dialog->mainwin);
439 progress_dialog_destroy(inc_dialog->dialog);
444 static IncSession *inc_session_new(PrefsAccount *account)
448 g_return_val_if_fail(account != NULL, NULL);
450 if (account->protocol != A_POP3)
452 if (!account->recv_server || !account->userid)
455 session = g_new0(IncSession, 1);
457 session->session = pop3_session_new(account);
458 session->session->data = session;
459 POP3_SESSION(session->session)->drop_message = inc_drop_message;
460 session_set_recv_message_notify(session->session,
461 inc_recv_message, session);
462 session_set_recv_data_progressive_notify(session->session,
463 inc_recv_data_progressive,
465 session_set_recv_data_notify(session->session,
466 inc_recv_data_finished, session);
471 static void inc_session_destroy(IncSession *session)
473 g_return_if_fail(session != NULL);
475 session_destroy(session->session);
479 static gint inc_start(IncProgressDialog *inc_dialog)
483 Pop3Session *pop3_session;
489 FolderItem *processing, *inbox;
491 GSList *msglist, *msglist_element;
492 gboolean cancelled = FALSE;
494 qlist = inc_dialog->queue_list;
495 while (qlist != NULL) {
496 GList *next = qlist->next;
498 session = qlist->data;
499 pop3_session = POP3_SESSION(session->session);
500 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
501 if (pop3_session->ac_prefs->passwd)
503 g_strdup(pop3_session->ac_prefs->passwd);
504 else if (pop3_session->ac_prefs->tmp_pass)
506 g_strdup(pop3_session->ac_prefs->tmp_pass);
510 if (inc_dialog->show_dialog)
511 manage_window_focus_in
512 (inc_dialog->dialog->window,
515 pass = input_dialog_query_password
516 (pop3_session->ac_prefs->recv_server,
519 if (inc_dialog->show_dialog)
520 manage_window_focus_out
521 (inc_dialog->dialog->window,
525 pop3_session->ac_prefs->tmp_pass =
527 pop3_session->pass = pass;
534 #define SET_PIXMAP_AND_TEXT(pix, str) \
536 progress_dialog_list_set(inc_dialog->dialog, \
537 inc_dialog->cur_row, \
543 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
544 int cur = 0, total = 0;
545 session = inc_dialog->queue_list->data;
546 pop3_session = POP3_SESSION(session->session);
548 if (pop3_session->pass == NULL) {
549 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
550 inc_session_destroy(session);
551 inc_dialog->queue_list =
552 g_list_remove(inc_dialog->queue_list, session);
556 inc_progress_dialog_clear(inc_dialog);
557 progress_dialog_scroll_to_row(inc_dialog->dialog,
558 inc_dialog->cur_row);
560 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
562 /* begin POP3 session */
563 inc_state = inc_pop3_session_do(session);
567 if (pop3_session->cur_total_num > 0)
568 msg = g_strdup_printf(
569 ngettext("Done (%d message (%s) received)",
570 "Done (%d messages (%s) received)",
571 pop3_session->cur_total_num),
572 pop3_session->cur_total_num,
573 to_human_readable(pop3_session->cur_total_recv_bytes));
575 msg = g_strdup_printf(_("Done (no new messages)"));
576 SET_PIXMAP_AND_TEXT(okpix, msg);
579 case INC_CONNECT_ERROR:
580 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
582 case INC_AUTH_FAILED:
583 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
586 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
591 case INC_SOCKET_ERROR:
593 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
596 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
599 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
600 if (!inc_dialog->show_dialog)
607 if (pop3_session->error_val == PS_AUTHFAIL) {
608 if(!prefs_common.no_recv_err_panel) {
609 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
610 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
611 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
615 /* CLAWS: perform filtering actions on dropped message */
616 /* CLAWS: get default inbox (perhaps per account) */
617 if (pop3_session->ac_prefs->inbox) {
618 /* CLAWS: get destination folder / mailbox */
619 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
621 inbox = folder_get_default_inbox();
623 inbox = folder_get_default_inbox();
625 /* get list of messages in processing */
626 processing = folder_get_default_processing();
627 folder_item_scan(processing);
628 msglist = folder_item_get_msg_list(processing);
630 /* process messages */
631 folder_item_update_freeze();
632 if (pop3_session->ac_prefs->filter_on_recv)
633 statusbar_print_all(_("Filtering messages...\n"));
634 total = g_slist_length(msglist);
636 for(msglist_element = msglist; msglist_element != NULL;
637 msglist_element = msglist_element->next) {
639 msginfo = (MsgInfo *) msglist_element->data;
640 filename = folder_item_fetch_msg(processing, msginfo->msgnum);
643 if (pop3_session->ac_prefs->filter_on_recv)
644 statusbar_progress_all(cur++,total, prefs_common.statusbar_update_step);
646 if (!pop3_session->ac_prefs->filter_on_recv ||
647 !procmsg_msginfo_filter(msginfo))
648 folder_item_move_msg(inbox, msginfo);
650 filtering_move_and_copy_msgs(msglist);
651 for(msglist_element = msglist; msglist_element != NULL;
652 msglist_element = msglist_element->next) {
653 MsgInfo *msginfo = (MsgInfo *)msglist_element->data;
654 procmsg_msginfo_free(msginfo);
656 folder_item_update_thaw();
658 statusbar_progress_all(0,0,0);
661 g_slist_free(msglist);
665 new_msgs += pop3_session->cur_total_num;
667 if (pop3_session->error_val == PS_AUTHFAIL &&
668 pop3_session->ac_prefs->tmp_pass) {
669 g_free(pop3_session->ac_prefs->tmp_pass);
670 pop3_session->ac_prefs->tmp_pass = NULL;
673 pop3_write_uidl_list(pop3_session);
675 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
677 if (inc_dialog->show_dialog)
678 manage_window_focus_in
679 (inc_dialog->dialog->window,
681 inc_put_error(inc_state, pop3_session);
682 if (inc_dialog->show_dialog)
683 manage_window_focus_out
684 (inc_dialog->dialog->window,
686 if (inc_state == INC_NO_SPACE ||
687 inc_state == INC_IO_ERROR)
691 inc_session_destroy(session);
692 inc_dialog->queue_list =
693 g_list_remove(inc_dialog->queue_list, session);
696 #undef SET_PIXMAP_AND_TEXT
699 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
700 "Finished (%d new messages)",
701 new_msgs), new_msgs);
703 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
705 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
707 while (inc_dialog->queue_list != NULL) {
708 session = inc_dialog->queue_list->data;
709 inc_session_destroy(session);
710 inc_dialog->queue_list =
711 g_list_remove(inc_dialog->queue_list, session);
714 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
715 inc_progress_dialog_destroy(inc_dialog);
717 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
719 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
728 static IncState inc_pop3_session_do(IncSession *session)
730 Pop3Session *pop3_session = POP3_SESSION(session->session);
731 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
736 debug_print("getting new messages of account %s...\n",
737 pop3_session->ac_prefs->account_name);
739 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
741 buf = g_strdup_printf(_("%s: Retrieving new messages"),
742 pop3_session->ac_prefs->recv_server);
743 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
746 server = pop3_session->ac_prefs->recv_server;
748 port = pop3_session->ac_prefs->set_popport ?
749 pop3_session->ac_prefs->popport :
750 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
751 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
752 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
753 SESSION(pop3_session)->nonblocking =
754 pop3_session->ac_prefs->use_nonblocking_ssl;
756 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE) {
757 if (alertpanel_full(_("Insecure connection"),
758 _("This connection is configured to be secured "
759 "using SSL, but SSL is not available in this "
760 "build of Sylpheed-Claws. \n\n"
761 "Do you want to continue connecting to this "
762 "server? The communication would not be "
764 _("Con_tinue connecting"),
765 GTK_STOCK_CANCEL, NULL,
766 FALSE, NULL, ALERT_WARNING,
767 G_ALERTALTERNATE) != G_ALERTDEFAULT)
768 return INC_CONNECT_ERROR;
770 port = pop3_session->ac_prefs->set_popport ?
771 pop3_session->ac_prefs->popport : 110;
774 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
775 log_message("%s\n", buf);
777 progress_dialog_set_label(inc_dialog->dialog, buf);
780 session_set_timeout(SESSION(pop3_session),
781 prefs_common.io_timeout_secs * 1000);
783 if (session_connect(SESSION(pop3_session), server, port) < 0) {
784 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
786 if(!prefs_common.no_recv_err_panel) {
787 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
788 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
789 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
791 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
793 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
795 session->inc_state = INC_CONNECT_ERROR;
797 return INC_CONNECT_ERROR;
800 while (session_is_connected(SESSION(pop3_session)) &&
801 session->inc_state != INC_CANCEL)
802 gtk_main_iteration();
804 if (session->inc_state == INC_SUCCESS) {
805 switch (pop3_session->error_val) {
807 switch (SESSION(pop3_session)->state) {
809 if (pop3_session->state == POP3_READY)
810 session->inc_state = INC_CONNECT_ERROR;
812 session->inc_state = INC_ERROR;
815 session->inc_state = INC_EOF;
817 case SESSION_TIMEOUT:
818 session->inc_state = INC_TIMEOUT;
821 session->inc_state = INC_SUCCESS;
826 session->inc_state = INC_AUTH_FAILED;
829 session->inc_state = INC_IO_ERROR;
832 session->inc_state = INC_SOCKET_ERROR;
835 session->inc_state = INC_LOCKED;
838 session->inc_state = INC_ERROR;
843 session_disconnect(SESSION(pop3_session));
846 return session->inc_state;
849 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
850 IncSession *inc_session)
852 inc_progress_dialog_set_label(inc_dialog, inc_session);
853 inc_progress_dialog_set_progress(inc_dialog, inc_session);
856 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
857 IncSession *inc_session)
859 ProgressDialog *dialog = inc_dialog->dialog;
860 Pop3Session *session;
862 g_return_if_fail(inc_session != NULL);
864 session = POP3_SESSION(inc_session->session);
866 switch (session->state) {
869 case POP3_GETAUTH_USER:
870 case POP3_GETAUTH_PASS:
871 case POP3_GETAUTH_APOP:
872 progress_dialog_set_label(dialog, _("Authenticating..."));
873 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
874 SESSION(session)->server,
875 session->ac_prefs->account_name);
877 case POP3_GETRANGE_STAT:
878 progress_dialog_set_label
879 (dialog, _("Getting the number of new messages (STAT)..."));
881 case POP3_GETRANGE_LAST:
882 progress_dialog_set_label
883 (dialog, _("Getting the number of new messages (LAST)..."));
885 case POP3_GETRANGE_UIDL:
886 progress_dialog_set_label
887 (dialog, _("Getting the number of new messages (UIDL)..."));
889 case POP3_GETSIZE_LIST:
890 progress_dialog_set_label
891 (dialog, _("Getting the size of messages (LIST)..."));
898 progress_dialog_set_label(dialog, _("Quitting"));
905 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
906 IncSession *inc_session)
908 gchar buf[MSGBUFSIZE];
909 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
910 gchar *total_size_str;
914 if (!pop3_session->new_msg_exist) return;
916 cur_total = inc_session->cur_total_bytes;
917 total = pop3_session->total_bytes;
918 if (pop3_session->state == POP3_RETR ||
919 pop3_session->state == POP3_RETR_RECV ||
920 pop3_session->state == POP3_DELETE) {
921 Xstrdup_a(total_size_str, to_human_readable(total), return);
922 g_snprintf(buf, sizeof(buf),
923 _("Retrieving message (%d / %d) (%s / %s)"),
924 pop3_session->cur_msg, pop3_session->count,
925 to_human_readable(cur_total), total_size_str);
926 progress_dialog_set_label(inc_dialog->dialog, buf);
929 progress_dialog_set_fraction
930 (inc_dialog->dialog,(gfloat)cur_total / (gfloat)total);
932 g_snprintf(buf, sizeof(buf), "%d / %d",
933 pop3_session->cur_msg, pop3_session->count);
934 gtk_progress_bar_set_text
935 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), buf);
936 gtk_progress_bar_set_fraction
937 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
938 (gfloat)cur_total / (gfloat)total);
940 if (pop3_session->cur_total_num > 0) {
941 g_snprintf(buf, sizeof(buf),
942 ngettext("Retrieving (%d message (%s) received)",
943 "Retrieving (%d messages (%s) received)",
944 pop3_session->cur_total_num),
945 pop3_session->cur_total_num,
947 (pop3_session->cur_total_recv_bytes));
948 progress_dialog_list_set_status(inc_dialog->dialog,
954 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
955 IncSession *inc_session)
961 g_get_current_time(&tv_cur);
963 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
964 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
965 if (tv_result.tv_usec < 0) {
967 tv_result.tv_usec += G_USEC_PER_SEC;
970 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
971 if (msec > PROGRESS_UPDATE_INTERVAL) {
972 inc_progress_dialog_update(inc_dialog, inc_session);
973 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
974 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
978 static gint inc_recv_data_progressive(Session *session, guint cur_len,
979 guint total_len, gpointer data)
981 IncSession *inc_session = (IncSession *)data;
982 Pop3Session *pop3_session = POP3_SESSION(session);
983 IncProgressDialog *inc_dialog;
986 g_return_val_if_fail(inc_session != NULL, -1);
988 if (pop3_session->state != POP3_RETR &&
989 pop3_session->state != POP3_RETR_RECV &&
990 pop3_session->state != POP3_DELETE &&
991 pop3_session->state != POP3_LOGOUT) return 0;
993 if (!pop3_session->new_msg_exist) return 0;
995 cur_total = pop3_session->cur_total_bytes + cur_len;
996 if (cur_total > pop3_session->total_bytes)
997 cur_total = pop3_session->total_bytes;
998 inc_session->cur_total_bytes = cur_total;
1000 inc_dialog = (IncProgressDialog *)inc_session->data;
1001 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1006 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1008 IncSession *inc_session = (IncSession *)data;
1009 IncProgressDialog *inc_dialog;
1011 g_return_val_if_fail(inc_session != NULL, -1);
1013 inc_dialog = (IncProgressDialog *)inc_session->data;
1015 inc_recv_data_progressive(session, 0, 0, inc_session);
1017 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1018 inc_progress_dialog_update(inc_dialog, inc_session);
1024 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1026 IncSession *inc_session = (IncSession *)data;
1027 IncProgressDialog *inc_dialog;
1029 g_return_val_if_fail(inc_session != NULL, -1);
1031 inc_dialog = (IncProgressDialog *)inc_session->data;
1033 switch (POP3_SESSION(session)->state) {
1034 case POP3_GETAUTH_USER:
1035 case POP3_GETAUTH_PASS:
1036 case POP3_GETAUTH_APOP:
1037 case POP3_GETRANGE_STAT:
1038 case POP3_GETRANGE_LAST:
1039 case POP3_GETRANGE_UIDL:
1040 case POP3_GETSIZE_LIST:
1041 inc_progress_dialog_update(inc_dialog, inc_session);
1044 inc_recv_data_progressive(session, 0, 0, inc_session);
1047 inc_progress_dialog_update(inc_dialog, inc_session);
1056 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1059 FolderItem *dropfolder;
1060 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1063 g_return_val_if_fail(inc_session != NULL, -1);
1065 if (session->ac_prefs->inbox) {
1066 inbox = folder_find_item_from_identifier
1067 (session->ac_prefs->inbox);
1069 inbox = folder_get_default_inbox();
1071 inbox = folder_get_default_inbox();
1077 /* CLAWS: claws uses a global .processing folder for the filtering. */
1078 dropfolder = folder_get_default_processing();
1080 /* add msg file to drop folder */
1081 if ((msgnum = folder_item_add_msg(
1082 dropfolder, file, NULL, TRUE)) < 0) {
1090 static void inc_put_error(IncState istate, Pop3Session *session)
1092 gchar *log_msg = NULL;
1093 gchar *err_msg = NULL;
1094 gboolean fatal_error = FALSE;
1097 case INC_CONNECT_ERROR:
1098 log_msg = _("Connection failed.");
1099 if (prefs_common.no_recv_err_panel)
1101 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1102 SESSION(session)->server,
1103 SESSION(session)->port);
1106 log_msg = _("Error occurred while processing mail.");
1107 if (prefs_common.no_recv_err_panel)
1109 if (session->error_msg)
1110 err_msg = g_strdup_printf
1111 (_("Error occurred while processing mail:\n%s"),
1112 session->error_msg);
1114 err_msg = g_strdup(log_msg);
1117 log_msg = _("No disk space left.");
1118 err_msg = g_strdup(log_msg);
1122 log_msg = _("Can't write file.");
1123 err_msg = g_strdup(log_msg);
1126 case INC_SOCKET_ERROR:
1127 log_msg = _("Socket error.");
1128 if (prefs_common.no_recv_err_panel)
1130 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1131 SESSION(session)->server,
1132 SESSION(session)->port);
1135 log_msg = _("Connection closed by the remote host.");
1136 if (prefs_common.no_recv_err_panel)
1138 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1139 SESSION(session)->server,
1140 SESSION(session)->port);
1143 log_msg = _("Mailbox is locked.");
1144 if (prefs_common.no_recv_err_panel)
1146 if (session->error_msg)
1147 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1148 session->error_msg);
1150 err_msg = g_strdup(log_msg);
1152 case INC_AUTH_FAILED:
1153 log_msg = _("Authentication failed.");
1154 if (prefs_common.no_recv_err_panel)
1156 if (session->error_msg)
1157 err_msg = g_strdup_printf
1158 (_("Authentication failed:\n%s"), session->error_msg);
1160 err_msg = g_strdup(log_msg);
1163 log_msg = _("Session timed out.");
1164 if (prefs_common.no_recv_err_panel)
1166 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1167 SESSION(session)->server,
1168 SESSION(session)->port);
1176 log_error("%s\n", log_msg);
1178 log_warning("%s\n", log_msg);
1181 alertpanel_error_log(err_msg);
1186 static void inc_cancel(IncProgressDialog *dialog)
1188 IncSession *session;
1190 g_return_if_fail(dialog != NULL);
1192 if (dialog->queue_list == NULL) {
1193 inc_progress_dialog_destroy(dialog);
1197 session = dialog->queue_list->data;
1199 session->inc_state = INC_CANCEL;
1201 log_message(_("Incorporation cancelled\n"));
1204 gboolean inc_is_active(void)
1206 return (inc_dialog_list != NULL);
1209 void inc_cancel_all(void)
1213 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1214 inc_cancel((IncProgressDialog *)cur->data);
1217 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1219 inc_cancel((IncProgressDialog *)data);
1222 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1225 IncProgressDialog *dialog = (IncProgressDialog *)data;
1227 if (dialog->queue_list == NULL)
1228 inc_progress_dialog_destroy(dialog);
1233 static gint inc_spool_account(PrefsAccount *account)
1239 if (account->local_inbox) {
1240 inbox = folder_find_item_from_identifier(account->local_inbox);
1242 inbox = folder_get_default_inbox();
1244 inbox = folder_get_default_inbox();
1246 if (is_file_exist(account->local_mbox))
1247 mbox = g_strdup(account->local_mbox);
1248 else if (is_dir_exist(account->local_mbox))
1249 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1250 g_get_user_name(), NULL);
1252 debug_print("%s: local mailbox not found.\n",
1253 account->local_mbox);
1257 result = get_spool(inbox, mbox);
1260 statusbar_pop_all();
1265 static gint inc_all_spool(void)
1269 gint account_new_msgs = 0;
1271 list = account_get_list();
1272 if (!list) return 0;
1274 for (; list != NULL; list = list->next) {
1275 PrefsAccount *account = list->data;
1277 if ((account->protocol == A_LOCAL) &&
1278 (account->recv_at_getall)) {
1279 account_new_msgs = inc_spool_account(account);
1280 if (account_new_msgs > 0)
1281 new_msgs += account_new_msgs;
1288 static gint get_spool(FolderItem *dest, const gchar *mbox)
1292 gchar tmp_mbox[MAXPATHLEN + 1];
1294 g_return_val_if_fail(dest != NULL, -1);
1295 g_return_val_if_fail(mbox != NULL, -1);
1297 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1298 debug_print("%s: no messages in local mailbox.\n", mbox);
1300 } else if (size < 0)
1303 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1306 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1307 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1309 if (copy_mbox(mbox, tmp_mbox) < 0) {
1310 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1314 debug_print("Getting new messages from %s into %s...\n",
1317 msgs = proc_mbox(dest, tmp_mbox, TRUE);
1320 if (msgs >= 0) empty_mbox(mbox);
1321 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1331 void inc_unlock(void)
1333 if (inc_lock_count > 0)
1337 static guint autocheck_timer = 0;
1338 static gpointer autocheck_data = NULL;
1340 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1343 gchar *buf, *numpos, *ret_str;
1344 gssize by_read = 0, by_written = 0;
1346 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1347 *prefs_common.newmail_notify_cmd))
1349 buf = g_strdup(prefs_common.newmail_notify_cmd);
1350 if ((numpos = strstr(buf, "%d")) != NULL) {
1354 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1359 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1361 if (ret_str && by_written) {
1365 debug_print("executing new mail notification command: %s\n", buf);
1366 execute_command_line(buf, TRUE);
1371 void inc_autocheck_timer_init(MainWindow *mainwin)
1373 autocheck_data = mainwin;
1374 inc_autocheck_timer_set();
1377 static void inc_autocheck_timer_set_interval(guint interval)
1379 inc_autocheck_timer_remove();
1380 /* last test is to avoid re-enabling auto_check after modifying
1381 the common preferences */
1382 if (prefs_common.autochk_newmail && autocheck_data
1383 && prefs_common.work_offline == FALSE) {
1384 autocheck_timer = gtk_timeout_add
1385 (interval, inc_autocheck_func, autocheck_data);
1386 debug_print("added timer = %d\n", autocheck_timer);
1390 void inc_autocheck_timer_set(void)
1392 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1395 void inc_autocheck_timer_remove(void)
1397 if (autocheck_timer) {
1398 debug_print("removed timer = %d\n", autocheck_timer);
1399 gtk_timeout_remove(autocheck_timer);
1400 autocheck_timer = 0;
1404 static gint inc_autocheck_func(gpointer data)
1406 MainWindow *mainwin = (MainWindow *)data;
1408 if (inc_lock_count) {
1409 debug_print("autocheck is locked.\n");
1410 inc_autocheck_timer_set_interval(1000);
1414 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1419 gboolean inc_offline_should_override(const gchar *msg)
1421 static time_t overridden_yes = 0;
1422 static time_t overridden_no = 0;
1423 int length = 10; /* minutes */
1424 gint answer = G_ALERTDEFAULT;
1426 if (prefs_common.autochk_newmail)
1427 length = prefs_common.autochk_itv; /* minutes */
1429 if (prefs_common.work_offline) {
1432 if (time(NULL) - overridden_yes < length * 60) /* seconds */
1434 else if (time(NULL) - overridden_no < 3) /* seconds */
1437 tmp = g_strdup_printf(
1438 _("%s%sYou're working offline. Override for %d minutes?"),
1443 answer = alertpanel(_("Offline warning"),
1445 GTK_STOCK_NO, "+" GTK_STOCK_YES, _("On_ly once"));
1447 if (answer == G_ALERTALTERNATE) {
1448 overridden_yes = time(NULL);
1450 } else if (answer == G_ALERTDEFAULT) {
1451 overridden_no = time(NULL);
1454 overridden_yes = (time_t)0;
1455 overridden_no = (time_t)0;