2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2005 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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>
37 #include <sys/types.h>
45 #include "mainwindow.h"
46 #include "folderview.h"
47 #include "summaryview.h"
48 #include "prefs_common.h"
49 #include "prefs_account.h"
59 #include "statusbar.h"
60 #include "manage_window.h"
61 #include "stock_pixmap.h"
62 #include "progressdialog.h"
63 #include "inputdialog.h"
64 #include "alertpanel.h"
66 #include "filtering.h"
70 static GList *inc_dialog_list = NULL;
72 static guint inc_lock_count = 0;
74 static GdkPixbuf *current_pixbuf;
75 static GdkPixbuf *error_pixbuf;
76 static GdkPixbuf *ok_pixbuf;
78 #define MSGBUFSIZE 8192
80 static void inc_finished (MainWindow *mainwin,
81 gboolean new_messages);
82 static gint inc_account_mail_real (MainWindow *mainwin,
83 PrefsAccount *account);
85 static IncProgressDialog *inc_progress_dialog_create
87 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
88 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
90 static IncSession *inc_session_new (PrefsAccount *account);
91 static void inc_session_destroy (IncSession *session);
92 static gint inc_start (IncProgressDialog *inc_dialog);
93 static IncState inc_pop3_session_do (IncSession *session);
95 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
96 IncSession *inc_session);
98 static void inc_progress_dialog_set_label
99 (IncProgressDialog *inc_dialog,
100 IncSession *inc_session);
101 static void inc_progress_dialog_set_progress
102 (IncProgressDialog *inc_dialog,
103 IncSession *inc_session);
105 static void inc_progress_dialog_update_periodic
106 (IncProgressDialog *inc_dialog,
107 IncSession *inc_session);
109 static gint inc_recv_data_progressive (Session *session,
113 static gint inc_recv_data_finished (Session *session,
116 static gint inc_recv_message (Session *session,
119 static gint inc_drop_message (Pop3Session *session,
122 static void inc_put_error (IncState istate,
123 Pop3Session *session);
125 static void inc_cancel_cb (GtkWidget *widget,
127 static gint inc_dialog_delete_cb (GtkWidget *widget,
131 static gint get_spool (FolderItem *dest,
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,
144 * @mainwin: Main window.
145 * @new_messages: TRUE if some messages have been received.
147 * Update the folder view and the summary view after receiving
148 * messages. If @new_messages is FALSE, this function avoids unneeded
151 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
155 if (prefs_common.scan_all_after_inc)
156 folderview_check_new(NULL);
158 if (!new_messages && !prefs_common.scan_all_after_inc) return;
160 if (prefs_common.open_inbox_on_inc) {
161 item = cur_account && cur_account->inbox
162 ? folder_find_item_from_identifier(cur_account->inbox)
163 : folder_get_default_inbox();
164 folderview_unselect(mainwin->folderview);
165 folderview_select(mainwin->folderview, item);
169 void inc_mail(MainWindow *mainwin, gboolean notify)
172 gint account_new_msgs = 0;
174 if (inc_lock_count) return;
176 if (prefs_common.work_offline)
177 if (alertpanel(_("Offline warning"),
178 _("You're working offline. Override?"),
179 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
183 inc_autocheck_timer_remove();
184 main_window_lock(mainwin);
186 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
187 /* external incorporating program */
188 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
189 main_window_unlock(mainwin);
190 inc_autocheck_timer_set();
194 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
195 if (account_new_msgs > 0)
196 new_msgs += account_new_msgs;
199 inc_finished(mainwin, new_msgs > 0);
200 main_window_unlock(mainwin);
201 inc_notify_cmd(new_msgs, notify);
202 inc_autocheck_timer_set();
206 void inc_pop_before_smtp(PrefsAccount *acc)
208 IncProgressDialog *inc_dialog;
212 mainwin = mainwindow_get_mainwindow();
214 session = inc_session_new(acc);
215 if (!session) return;
216 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
218 inc_dialog = inc_progress_dialog_create(FALSE);
219 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
221 /* FIXME: assumes to attach to first main window */
222 inc_dialog->mainwin = mainwin;
223 inc_progress_dialog_set_list(inc_dialog);
226 toolbar_main_set_sensitive(mainwin);
227 main_window_set_menu_sensitive(mainwin);
230 inc_start(inc_dialog);
233 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
235 IncProgressDialog *inc_dialog;
238 switch (account->protocol) {
242 * FIXME: it should return foldeview_check_new() value.
243 * TODO: do it when bug [19] is fixed (IMAP folder sets
244 * an incorrect new message count)
246 folderview_check_new(FOLDER(account->folder));
250 session = inc_session_new(account);
251 if (!session) return 0;
253 inc_dialog = inc_progress_dialog_create(FALSE);
254 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
256 inc_dialog->mainwin = mainwin;
257 inc_progress_dialog_set_list(inc_dialog);
260 toolbar_main_set_sensitive(mainwin);
261 main_window_set_menu_sensitive(mainwin);
264 return inc_start(inc_dialog);
267 return inc_spool_account(account);
275 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
279 if (inc_lock_count) return 0;
281 if (prefs_common.work_offline)
282 if (alertpanel(_("Offline warning"),
283 _("You're working offline. Override?"),
284 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
287 inc_autocheck_timer_remove();
288 main_window_lock(mainwin);
290 new_msgs = inc_account_mail_real(mainwin, account);
292 inc_finished(mainwin, new_msgs > 0);
293 main_window_unlock(mainwin);
294 inc_autocheck_timer_set();
299 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
302 GList *list, *queue_list = NULL;
303 IncProgressDialog *inc_dialog;
305 gint account_new_msgs = 0;
307 if (prefs_common.work_offline)
308 if (alertpanel(_("Offline warning"),
309 _("You're working offline. Override?"),
310 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
313 if (inc_lock_count) return;
315 inc_autocheck_timer_remove();
316 main_window_lock(mainwin);
318 list = account_get_list();
320 inc_finished(mainwin, new_msgs > 0);
321 main_window_unlock(mainwin);
322 inc_notify_cmd(new_msgs, notify);
323 inc_autocheck_timer_set();
327 /* check local folders */
328 account_new_msgs = inc_all_spool();
329 if (account_new_msgs > 0)
330 new_msgs += account_new_msgs;
332 /* check IMAP4 / News folders */
333 for (list = account_get_list(); list != NULL; list = list->next) {
334 PrefsAccount *account = list->data;
335 if ((account->protocol == A_IMAP4 ||
336 account->protocol == A_NNTP) && account->recv_at_getall) {
337 new_msgs += folderview_check_new(FOLDER(account->folder));
341 /* check POP3 accounts */
342 for (list = account_get_list(); list != NULL; list = list->next) {
344 PrefsAccount *account = list->data;
346 if (account->recv_at_getall) {
347 session = inc_session_new(account);
349 queue_list = g_list_append(queue_list, session);
354 inc_dialog = inc_progress_dialog_create(autocheck);
355 inc_dialog->queue_list = queue_list;
356 inc_dialog->mainwin = mainwin;
357 inc_progress_dialog_set_list(inc_dialog);
359 toolbar_main_set_sensitive(mainwin);
360 main_window_set_menu_sensitive(mainwin);
361 new_msgs += inc_start(inc_dialog);
364 inc_finished(mainwin, new_msgs > 0);
365 main_window_unlock(mainwin);
366 inc_notify_cmd(new_msgs, notify);
367 inc_autocheck_timer_set();
370 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
372 IncProgressDialog *dialog;
373 ProgressDialog *progress;
375 dialog = g_new0(IncProgressDialog, 1);
377 progress = progress_dialog_create();
378 gtk_window_set_title(GTK_WINDOW(progress->window),
379 _("Retrieving new messages"));
380 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
381 G_CALLBACK(inc_cancel_cb), dialog);
382 g_signal_connect(G_OBJECT(progress->window), "delete_event",
383 G_CALLBACK(inc_dialog_delete_cb), dialog);
384 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
386 progress_dialog_set_value(progress, 0.0);
388 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_COMPLETE, &ok_pixbuf);
389 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_CONTINUE,
391 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_ERROR, &error_pixbuf);
393 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
394 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
396 dialog->show_dialog = TRUE;
397 gtk_widget_show_now(progress->window);
400 dialog->dialog = progress;
401 gettimeofday(&dialog->progress_tv, NULL);
402 gettimeofday(&dialog->folder_tv, NULL);
403 dialog->queue_list = NULL;
406 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
411 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
415 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
416 IncSession *session = list->data;
417 Pop3Session *pop3_session = POP3_SESSION(session->session);
419 session->data = inc_dialog;
420 progress_dialog_append(inc_dialog->dialog, NULL,
421 pop3_session->ac_prefs->account_name,
426 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
428 progress_dialog_set_value(inc_dialog->dialog, 0.0);
429 progress_dialog_set_label(inc_dialog->dialog, "");
430 if (inc_dialog->mainwin)
431 main_window_progress_off(inc_dialog->mainwin);
434 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
436 g_return_if_fail(inc_dialog != NULL);
438 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
440 if (inc_dialog->mainwin)
441 main_window_progress_off(inc_dialog->mainwin);
442 progress_dialog_destroy(inc_dialog->dialog);
447 static IncSession *inc_session_new(PrefsAccount *account)
451 g_return_val_if_fail(account != NULL, NULL);
453 if (account->protocol != A_POP3)
455 if (!account->recv_server || !account->userid)
458 session = g_new0(IncSession, 1);
460 session->session = pop3_session_new(account);
461 session->session->data = session;
462 POP3_SESSION(session->session)->drop_message = inc_drop_message;
463 session_set_recv_message_notify(session->session,
464 inc_recv_message, session);
465 session_set_recv_data_progressive_notify(session->session,
466 inc_recv_data_progressive,
468 session_set_recv_data_notify(session->session,
469 inc_recv_data_finished, session);
474 static void inc_session_destroy(IncSession *session)
476 g_return_if_fail(session != NULL);
478 session_destroy(session->session);
482 static gint inc_start(IncProgressDialog *inc_dialog)
486 Pop3Session *pop3_session;
492 FolderItem *processing, *inbox;
494 GSList *msglist, *msglist_element;
495 gboolean cancelled = FALSE;
497 qlist = inc_dialog->queue_list;
498 while (qlist != NULL) {
499 GList *next = qlist->next;
501 session = qlist->data;
502 pop3_session = POP3_SESSION(session->session);
503 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
504 if (pop3_session->ac_prefs->passwd)
506 g_strdup(pop3_session->ac_prefs->passwd);
507 else if (pop3_session->ac_prefs->tmp_pass)
509 g_strdup(pop3_session->ac_prefs->tmp_pass);
513 if (inc_dialog->show_dialog)
514 manage_window_focus_in
515 (inc_dialog->dialog->window,
518 pass = input_dialog_query_password
519 (pop3_session->ac_prefs->recv_server,
522 if (inc_dialog->show_dialog)
523 manage_window_focus_out
524 (inc_dialog->dialog->window,
528 pop3_session->ac_prefs->tmp_pass =
530 pop3_session->pass = pass;
537 #define SET_PIXMAP_AND_TEXT(pixbuf, str) \
539 progress_dialog_set_row_pixbuf(inc_dialog->dialog, \
540 inc_dialog->cur_row, pixbuf); \
541 progress_dialog_set_row_status(inc_dialog->dialog, \
542 inc_dialog->cur_row, str); \
545 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
546 session = inc_dialog->queue_list->data;
547 pop3_session = POP3_SESSION(session->session);
549 if (pop3_session->pass == NULL) {
550 SET_PIXMAP_AND_TEXT(ok_pixbuf, _("Cancelled"));
551 inc_session_destroy(session);
552 inc_dialog->queue_list =
553 g_list_remove(inc_dialog->queue_list, session);
557 inc_progress_dialog_clear(inc_dialog);
558 progress_dialog_scroll_to_row(inc_dialog->dialog,
559 inc_dialog->cur_row);
561 SET_PIXMAP_AND_TEXT(current_pixbuf, _("Retrieving"));
563 /* begin POP3 session */
564 inc_state = inc_pop3_session_do(session);
568 if (pop3_session->cur_total_num > 0)
569 msg = g_strdup_printf(
570 ngettext("Done (%d message (%s) received)",
571 "Done (%d messages (%s) received)",
572 pop3_session->cur_total_num),
573 pop3_session->cur_total_num,
574 to_human_readable(pop3_session->cur_total_recv_bytes));
576 msg = g_strdup_printf(_("Done (no new messages)"));
577 SET_PIXMAP_AND_TEXT(ok_pixbuf, msg);
580 case INC_CONNECT_ERROR:
581 SET_PIXMAP_AND_TEXT(error_pixbuf,
582 _("Connection failed"));
584 case INC_AUTH_FAILED:
585 SET_PIXMAP_AND_TEXT(error_pixbuf, _("Auth failed"));
588 SET_PIXMAP_AND_TEXT(error_pixbuf, _("Locked"));
593 case INC_SOCKET_ERROR:
595 SET_PIXMAP_AND_TEXT(error_pixbuf, _("Error"));
598 SET_PIXMAP_AND_TEXT(error_pixbuf, _("Timeout"));
601 SET_PIXMAP_AND_TEXT(ok_pixbuf, _("Cancelled"));
602 if (!inc_dialog->show_dialog)
609 if (pop3_session->error_val == PS_AUTHFAIL) {
610 if(!prefs_common.no_recv_err_panel) {
611 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
612 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
613 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
617 /* CLAWS: perform filtering actions on dropped message */
618 /* CLAWS: get default inbox (perhaps per account) */
619 if (pop3_session->ac_prefs->inbox) {
620 /* CLAWS: get destination folder / mailbox */
621 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
623 inbox = folder_get_default_inbox();
625 inbox = folder_get_default_inbox();
627 /* get list of messages in processing */
628 processing = folder_get_default_processing();
629 folder_item_scan(processing);
630 msglist = folder_item_get_msg_list(processing);
632 /* process messages */
633 folder_item_update_freeze();
634 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
636 msginfo = (MsgInfo *) msglist_element->data;
637 filename = folder_item_fetch_msg(processing, msginfo->msgnum);
639 if (!pop3_session->ac_prefs->filter_on_recv ||
640 !procmsg_msginfo_filter(msginfo))
641 folder_item_move_msg(inbox, msginfo);
642 procmsg_msginfo_free(msginfo);
644 folder_item_update_thaw();
645 g_slist_free(msglist);
649 new_msgs += pop3_session->cur_total_num;
651 if (pop3_session->error_val == PS_AUTHFAIL &&
652 pop3_session->ac_prefs->tmp_pass) {
653 g_free(pop3_session->ac_prefs->tmp_pass);
654 pop3_session->ac_prefs->tmp_pass = NULL;
657 pop3_write_uidl_list(pop3_session);
659 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
661 if (inc_dialog->show_dialog)
662 manage_window_focus_in
663 (inc_dialog->dialog->window,
665 inc_put_error(inc_state, pop3_session);
666 if (inc_dialog->show_dialog)
667 manage_window_focus_out
668 (inc_dialog->dialog->window,
670 if (inc_state == INC_NO_SPACE ||
671 inc_state == INC_IO_ERROR)
675 inc_session_destroy(session);
676 inc_dialog->queue_list =
677 g_list_remove(inc_dialog->queue_list, session);
680 #undef SET_PIXMAP_AND_TEXT
683 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
684 "Finished (%d new messages)",
685 new_msgs), new_msgs);
687 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
689 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
692 if (error_num && !prefs_common.no_recv_err_panel) {
693 if (inc_dialog->show_dialog)
694 manage_window_focus_in(inc_dialog->dialog->window,
696 alertpanel_error_log(_("Some errors occurred while getting mail."));
697 if (inc_dialog->show_dialog)
698 manage_window_focus_out(inc_dialog->dialog->window,
703 while (inc_dialog->queue_list != NULL) {
704 session = inc_dialog->queue_list->data;
705 inc_session_destroy(session);
706 inc_dialog->queue_list =
707 g_list_remove(inc_dialog->queue_list, session);
710 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
711 inc_progress_dialog_destroy(inc_dialog);
713 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
715 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
724 static IncState inc_pop3_session_do(IncSession *session)
726 Pop3Session *pop3_session = POP3_SESSION(session->session);
727 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
732 debug_print("getting new messages of account %s...\n",
733 pop3_session->ac_prefs->account_name);
735 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
737 buf = g_strdup_printf(_("%s: Retrieving new messages"),
738 pop3_session->ac_prefs->recv_server);
739 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
742 server = pop3_session->ac_prefs->recv_server;
744 port = pop3_session->ac_prefs->set_popport ?
745 pop3_session->ac_prefs->popport :
746 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
747 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
748 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
749 SESSION(pop3_session)->nonblocking =
750 pop3_session->ac_prefs->use_nonblocking_ssl;
752 port = pop3_session->ac_prefs->set_popport ?
753 pop3_session->ac_prefs->popport : 110;
756 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
757 log_message("%s\n", buf);
759 progress_dialog_set_label(inc_dialog->dialog, buf);
762 session_set_timeout(SESSION(pop3_session),
763 prefs_common.io_timeout_secs * 1000);
765 if (session_connect(SESSION(pop3_session), server, port) < 0) {
766 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
768 if(!prefs_common.no_recv_err_panel) {
769 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
770 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
771 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
773 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
775 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
777 session->inc_state = INC_CONNECT_ERROR;
779 return INC_CONNECT_ERROR;
782 while (session_is_connected(SESSION(pop3_session)) &&
783 session->inc_state != INC_CANCEL)
784 gtk_main_iteration();
786 if (session->inc_state == INC_SUCCESS) {
787 switch (pop3_session->error_val) {
789 switch (SESSION(pop3_session)->state) {
791 if (pop3_session->state == POP3_READY)
792 session->inc_state = INC_CONNECT_ERROR;
794 session->inc_state = INC_ERROR;
797 session->inc_state = INC_EOF;
799 case SESSION_TIMEOUT:
800 session->inc_state = INC_TIMEOUT;
803 session->inc_state = INC_SUCCESS;
808 session->inc_state = INC_AUTH_FAILED;
811 session->inc_state = INC_IO_ERROR;
814 session->inc_state = INC_SOCKET_ERROR;
817 session->inc_state = INC_LOCKED;
820 session->inc_state = INC_ERROR;
825 session_disconnect(SESSION(pop3_session));
828 return session->inc_state;
831 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
832 IncSession *inc_session)
834 inc_progress_dialog_set_label(inc_dialog, inc_session);
835 inc_progress_dialog_set_progress(inc_dialog, inc_session);
838 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
839 IncSession *inc_session)
841 ProgressDialog *dialog = inc_dialog->dialog;
842 Pop3Session *session;
844 g_return_if_fail(inc_session != NULL);
846 session = POP3_SESSION(inc_session->session);
848 switch (session->state) {
851 case POP3_GETAUTH_USER:
852 case POP3_GETAUTH_PASS:
853 case POP3_GETAUTH_APOP:
854 progress_dialog_set_label(dialog, _("Authenticating..."));
855 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
856 SESSION(session)->server,
857 session->ac_prefs->account_name);
859 case POP3_GETRANGE_STAT:
860 progress_dialog_set_label
861 (dialog, _("Getting the number of new messages (STAT)..."));
863 case POP3_GETRANGE_LAST:
864 progress_dialog_set_label
865 (dialog, _("Getting the number of new messages (LAST)..."));
867 case POP3_GETRANGE_UIDL:
868 progress_dialog_set_label
869 (dialog, _("Getting the number of new messages (UIDL)..."));
871 case POP3_GETSIZE_LIST:
872 progress_dialog_set_label
873 (dialog, _("Getting the size of messages (LIST)..."));
880 if (session->msg[session->cur_msg].recv_time <
881 session->current_time) {
882 gchar buf[MSGBUFSIZE];
883 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
885 progress_dialog_set_label(dialog, buf);
890 progress_dialog_set_label(dialog, _("Quitting"));
897 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
898 IncSession *inc_session)
900 gchar buf[MSGBUFSIZE];
901 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
902 gchar *total_size_str;
906 if (!pop3_session->new_msg_exist) return;
908 cur_total = inc_session->cur_total_bytes;
909 total = pop3_session->total_bytes;
910 if (pop3_session->state == POP3_RETR ||
911 pop3_session->state == POP3_RETR_RECV ||
912 pop3_session->state == POP3_DELETE) {
913 Xstrdup_a(total_size_str, to_human_readable(total), return);
914 g_snprintf(buf, sizeof(buf),
915 _("Retrieving message (%d / %d) (%s / %s)"),
916 pop3_session->cur_msg, pop3_session->count,
917 to_human_readable(cur_total), total_size_str);
918 progress_dialog_set_label(inc_dialog->dialog, buf);
921 progress_dialog_set_percentage
922 (inc_dialog->dialog,(gfloat)cur_total / (gfloat)total);
924 gtk_progress_set_show_text
925 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), TRUE);
926 g_snprintf(buf, sizeof(buf), "%d / %d",
927 pop3_session->cur_msg, pop3_session->count);
928 gtk_progress_set_format_string
929 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), buf);
930 gtk_progress_bar_update
931 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
932 (gfloat)cur_total / (gfloat)total);
934 if (pop3_session->cur_total_num > 0) {
935 g_snprintf(buf, sizeof(buf),
936 ngettext("Retrieving (%d message (%s) received)",
937 "Retrieving (%d messages (%s) received)",
938 pop3_session->cur_total_num),
939 pop3_session->cur_total_num,
941 (pop3_session->cur_total_recv_bytes));
942 progress_dialog_set_row_status(inc_dialog->dialog,
943 inc_dialog->cur_row, buf);
947 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
948 IncSession *inc_session)
950 struct timeval tv_cur;
951 struct timeval tv_result;
954 gettimeofday(&tv_cur, NULL);
956 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
957 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
958 if (tv_result.tv_usec < 0) {
960 tv_result.tv_usec += 1000000;
963 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
964 if (msec > PROGRESS_UPDATE_INTERVAL) {
965 inc_progress_dialog_update(inc_dialog, inc_session);
966 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
967 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
971 static gint inc_recv_data_progressive(Session *session, guint cur_len,
972 guint total_len, gpointer data)
974 IncSession *inc_session = (IncSession *)data;
975 Pop3Session *pop3_session = POP3_SESSION(session);
976 IncProgressDialog *inc_dialog;
979 g_return_val_if_fail(inc_session != NULL, -1);
981 if (pop3_session->state != POP3_RETR &&
982 pop3_session->state != POP3_RETR_RECV &&
983 pop3_session->state != POP3_DELETE &&
984 pop3_session->state != POP3_LOGOUT) return 0;
986 if (!pop3_session->new_msg_exist) return 0;
988 cur_total = pop3_session->cur_total_bytes + cur_len;
989 if (cur_total > pop3_session->total_bytes)
990 cur_total = pop3_session->total_bytes;
991 inc_session->cur_total_bytes = cur_total;
993 inc_dialog = (IncProgressDialog *)inc_session->data;
994 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
999 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1001 IncSession *inc_session = (IncSession *)data;
1002 IncProgressDialog *inc_dialog;
1004 g_return_val_if_fail(inc_session != NULL, -1);
1006 inc_dialog = (IncProgressDialog *)inc_session->data;
1008 inc_recv_data_progressive(session, 0, 0, inc_session);
1010 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1011 inc_progress_dialog_update(inc_dialog, inc_session);
1017 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1019 IncSession *inc_session = (IncSession *)data;
1020 IncProgressDialog *inc_dialog;
1022 g_return_val_if_fail(inc_session != NULL, -1);
1024 inc_dialog = (IncProgressDialog *)inc_session->data;
1026 switch (POP3_SESSION(session)->state) {
1027 case POP3_GETAUTH_USER:
1028 case POP3_GETAUTH_PASS:
1029 case POP3_GETAUTH_APOP:
1030 case POP3_GETRANGE_STAT:
1031 case POP3_GETRANGE_LAST:
1032 case POP3_GETRANGE_UIDL:
1033 case POP3_GETSIZE_LIST:
1034 inc_progress_dialog_update(inc_dialog, inc_session);
1037 inc_recv_data_progressive(session, 0, 0, inc_session);
1040 inc_progress_dialog_update(inc_dialog, inc_session);
1049 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1052 FolderItem *dropfolder;
1053 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1056 g_return_val_if_fail(inc_session != NULL, -1);
1058 if (session->ac_prefs->inbox) {
1059 inbox = folder_find_item_from_identifier
1060 (session->ac_prefs->inbox);
1062 inbox = folder_get_default_inbox();
1064 inbox = folder_get_default_inbox();
1070 /* CLAWS: claws uses a global .processing folder for the filtering. */
1071 dropfolder = folder_get_default_processing();
1073 /* add msg file to drop folder */
1074 if ((msgnum = folder_item_add_msg(
1075 dropfolder, file, NULL, TRUE)) < 0) {
1083 static void inc_put_error(IncState istate, Pop3Session *session)
1085 gchar *log_msg = NULL;
1086 gchar *err_msg = NULL;
1087 gboolean fatal_error = FALSE;
1090 case INC_CONNECT_ERROR:
1091 log_msg = _("Connection failed.");
1092 if (prefs_common.no_recv_err_panel)
1094 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1095 SESSION(session)->server,
1096 SESSION(session)->port);
1099 log_msg = _("Error occurred while processing mail.");
1100 if (prefs_common.no_recv_err_panel)
1102 if (session->error_msg)
1103 err_msg = g_strdup_printf
1104 (_("Error occurred while processing mail:\n%s"),
1105 session->error_msg);
1107 err_msg = g_strdup(log_msg);
1110 log_msg = _("No disk space left.");
1111 err_msg = g_strdup(log_msg);
1115 log_msg = _("Can't write file.");
1116 err_msg = g_strdup(log_msg);
1119 case INC_SOCKET_ERROR:
1120 log_msg = _("Socket error.");
1121 if (prefs_common.no_recv_err_panel)
1123 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1124 SESSION(session)->server,
1125 SESSION(session)->port);
1128 log_msg = _("Connection closed by the remote host.");
1129 if (prefs_common.no_recv_err_panel)
1131 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1132 SESSION(session)->server,
1133 SESSION(session)->port);
1136 log_msg = _("Mailbox is locked.");
1137 if (prefs_common.no_recv_err_panel)
1139 if (session->error_msg)
1140 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1141 session->error_msg);
1143 err_msg = g_strdup(log_msg);
1145 case INC_AUTH_FAILED:
1146 log_msg = _("Authentication failed.");
1147 if (prefs_common.no_recv_err_panel)
1149 if (session->error_msg)
1150 err_msg = g_strdup_printf
1151 (_("Authentication failed:\n%s"), session->error_msg);
1153 err_msg = g_strdup(log_msg);
1156 log_msg = _("Session timed out.");
1157 if (prefs_common.no_recv_err_panel)
1159 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1160 SESSION(session)->server,
1161 SESSION(session)->port);
1169 log_error("%s\n", log_msg);
1171 log_warning("%s\n", log_msg);
1174 alertpanel_error_log(err_msg);
1179 static void inc_cancel(IncProgressDialog *dialog)
1181 IncSession *session;
1183 g_return_if_fail(dialog != NULL);
1185 if (dialog->queue_list == NULL) {
1186 inc_progress_dialog_destroy(dialog);
1190 session = dialog->queue_list->data;
1192 session->inc_state = INC_CANCEL;
1194 log_message(_("Incorporation cancelled\n"));
1197 gboolean inc_is_active(void)
1199 return (inc_dialog_list != NULL);
1202 void inc_cancel_all(void)
1206 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1207 inc_cancel((IncProgressDialog *)cur->data);
1210 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1212 inc_cancel((IncProgressDialog *)data);
1215 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1218 IncProgressDialog *dialog = (IncProgressDialog *)data;
1220 if (dialog->queue_list == NULL)
1221 inc_progress_dialog_destroy(dialog);
1226 static gint inc_spool_account(PrefsAccount *account)
1232 if (account->inbox) {
1233 inbox = folder_find_item_from_path(account->inbox);
1235 inbox = folder_get_default_inbox();
1237 inbox = folder_get_default_inbox();
1239 if (is_file_exist(account->local_mbox))
1240 mbox = g_strdup(account->local_mbox);
1241 else if (is_dir_exist(account->local_mbox))
1242 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1243 g_get_user_name(), NULL);
1245 debug_print("%s: local mailbox not found.\n",
1246 account->local_mbox);
1250 result = get_spool(inbox, mbox);
1253 statusbar_pop_all();
1258 static gint inc_all_spool(void)
1262 gint account_new_msgs = 0;
1264 list = account_get_list();
1265 if (!list) return 0;
1267 for (; list != NULL; list = list->next) {
1268 PrefsAccount *account = list->data;
1270 if ((account->protocol == A_LOCAL) &&
1271 (account->recv_at_getall)) {
1272 account_new_msgs = inc_spool_account(account);
1273 if (account_new_msgs > 0)
1274 new_msgs += account_new_msgs;
1281 static gint get_spool(FolderItem *dest, const gchar *mbox)
1285 gchar tmp_mbox[MAXPATHLEN + 1];
1287 g_return_val_if_fail(dest != NULL, -1);
1288 g_return_val_if_fail(mbox != NULL, -1);
1290 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1291 debug_print("%s: no messages in local mailbox.\n", mbox);
1293 } else if (size < 0)
1296 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1299 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1300 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1302 if (copy_mbox(mbox, tmp_mbox) < 0) {
1303 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1307 debug_print("Getting new messages from %s into %s...\n",
1310 msgs = proc_mbox(dest, tmp_mbox, TRUE);
1313 if (msgs >= 0) empty_mbox(mbox);
1314 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1324 void inc_unlock(void)
1326 if (inc_lock_count > 0)
1330 static guint autocheck_timer = 0;
1331 static gpointer autocheck_data = NULL;
1333 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1336 gchar *buf, *numpos, *ret_str;
1337 gssize by_read = 0, by_written = 0;
1339 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1340 *prefs_common.newmail_notify_cmd))
1342 buf = g_strdup(prefs_common.newmail_notify_cmd);
1343 if ((numpos = strstr(buf, "%d")) != NULL) {
1347 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1352 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1354 if (ret_str && by_written) {
1358 debug_print("executing new mail notification command: %s\n", buf);
1359 execute_command_line(buf, TRUE);
1364 void inc_autocheck_timer_init(MainWindow *mainwin)
1366 autocheck_data = mainwin;
1367 inc_autocheck_timer_set();
1370 static void inc_autocheck_timer_set_interval(guint interval)
1372 inc_autocheck_timer_remove();
1373 /* last test is to avoid re-enabling auto_check after modifying
1374 the common preferences */
1375 if (prefs_common.autochk_newmail && autocheck_data
1376 && prefs_common.work_offline == FALSE) {
1377 autocheck_timer = gtk_timeout_add
1378 (interval, inc_autocheck_func, autocheck_data);
1379 debug_print("added timer = %d\n", autocheck_timer);
1383 void inc_autocheck_timer_set(void)
1385 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1388 void inc_autocheck_timer_remove(void)
1390 if (autocheck_timer) {
1391 debug_print("removed timer = %d\n", autocheck_timer);
1392 gtk_timeout_remove(autocheck_timer);
1393 autocheck_timer = 0;
1397 static gint inc_autocheck_func(gpointer data)
1399 MainWindow *mainwin = (MainWindow *)data;
1401 if (inc_lock_count) {
1402 debug_print("autocheck is locked.\n");
1403 inc_autocheck_timer_set_interval(1000);
1407 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);