2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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 <gtk/gtkmain.h>
28 #include <gtk/gtkwindow.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtkprogressbar.h>
35 #include <sys/types.h>
44 #include "mainwindow.h"
45 #include "folderview.h"
46 #include "summaryview.h"
47 #include "prefs_common.h"
48 #include "prefs_account.h"
58 #include "statusbar.h"
59 #include "manage_window.h"
60 #include "stock_pixmap.h"
61 #include "progressdialog.h"
62 #include "inputdialog.h"
63 #include "alertpanel.h"
65 #include "automaton.h"
67 #include "filtering.h"
68 #include "selective_download.h"
70 static GList *inc_dialog_list = NULL;
72 static guint inc_lock_count = 0;
74 static GdkPixmap *currentxpm;
75 static GdkBitmap *currentxpmmask;
76 static GdkPixmap *errorxpm;
77 static GdkBitmap *errorxpmmask;
78 static GdkPixmap *okxpm;
79 static GdkBitmap *okxpmmask;
81 #define MSGBUFSIZE 8192
83 static void inc_finished (MainWindow *mainwin,
84 gboolean new_messages);
85 static gint inc_account_mail (PrefsAccount *account,
88 static IncProgressDialog *inc_progress_dialog_create (void);
89 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
91 static IncSession *inc_session_new (PrefsAccount *account);
92 static void inc_session_destroy (IncSession *session);
93 static gint inc_start (IncProgressDialog *inc_dialog);
94 static IncState inc_pop3_session_do (IncSession *session);
95 static gint pop3_automaton_terminate (SockInfo *source,
98 static gboolean inc_pop3_recv_func (SockInfo *sock,
103 static void inc_put_error (IncState istate);
105 static void inc_cancel_cb (GtkWidget *widget,
107 static gint inc_dialog_delete_cb (GtkWidget *widget,
111 static gint inc_spool (void);
112 static gint get_spool (FolderItem *dest,
115 static void inc_spool_account(PrefsAccount *account);
116 static void inc_all_spool(void);
117 static void inc_autocheck_timer_set_interval (guint interval);
118 static gint inc_autocheck_func (gpointer data);
120 static void inc_notify_cmd (gint new_msgs,
123 #define FOLDER_SUMMARY_MISMATCH(f, s) \
124 (f) && (s) ? ((s)->newmsgs != (f)->new) || ((f)->unread != (s)->unread) || ((f)->total != (s)->messages) \
129 * @mainwin: Main window.
130 * @new_messages: TRUE if some messages have been received.
132 * Update the folder view and the summary view after receiving
133 * messages. If @new_messages is FALSE, this function avoids unneeded
136 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
140 if (prefs_common.scan_all_after_inc)
141 folderview_check_new(NULL);
143 if (!new_messages && !prefs_common.scan_all_after_inc) return;
145 if (prefs_common.open_inbox_on_inc) {
146 item = cur_account && cur_account->inbox
147 ? folder_find_item_from_identifier(cur_account->inbox)
148 : folder_get_default_inbox();
149 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
150 folderview_unselect(mainwin->folderview);
151 folderview_select(mainwin->folderview, item);
153 } else if (prefs_common.scan_all_after_inc) {
154 item = mainwin->summaryview->folder_item;
155 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
156 folderview_update_item(item, TRUE);
161 void inc_mail(MainWindow *mainwin, gboolean notify)
165 if (inc_lock_count) return;
167 if (prefs_common.work_offline)
168 if (alertpanel(_("Offline warning"),
169 _("You're working offline. Override?"),
170 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
173 inc_autocheck_timer_remove();
174 main_window_lock(mainwin);
176 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
177 /* external incorporating program */
178 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
179 main_window_unlock(mainwin);
180 inc_autocheck_timer_set();
184 if (prefs_common.inc_local)
185 new_msgs = inc_spool();
190 if (prefs_common.inc_local) {
191 new_msgs = inc_spool();
195 cur_account->session = STYPE_NORMAL;
196 new_msgs += inc_account_mail(cur_account, mainwin);
199 inc_finished(mainwin, new_msgs > 0);
200 main_window_unlock(mainwin);
201 inc_notify_cmd(new_msgs, notify);
202 inc_autocheck_timer_set();
205 void inc_selective_download(MainWindow *mainwin, PrefsAccount *acc, gint session)
210 acc->session = session;
211 inc_account_mail(acc, mainwin);
212 acc->session = STYPE_NORMAL;
214 for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
215 HeaderItems *items =(HeaderItems*)cur->data;
217 if (items->state == SD_DOWNLOADED &&
218 items->del_by_old_session == FALSE) {
224 inc_finished(mainwin, TRUE);
225 inc_notify_cmd(new_msgs, prefs_common.newmail_notify_manu);
229 void inc_pop_before_smtp(PrefsAccount *acc)
231 acc->session = STYPE_POP_BEFORE_SMTP;
232 inc_account_mail(acc, NULL);
235 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
237 IncProgressDialog *inc_dialog;
242 if(mainwin && mainwin->summaryview)
243 item = mainwin->summaryview->folder_item;
245 switch (account->protocol) {
248 folderview_check_new(FOLDER(account->folder));
249 if (!prefs_common.scan_all_after_inc && item != NULL &&
250 FOLDER(account->folder) == item->folder)
251 folderview_update_item(item, TRUE);
256 session = inc_session_new(account);
257 if (!session) return 0;
259 inc_dialog = inc_progress_dialog_create();
260 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
262 inc_dialog->mainwin = mainwin;
263 session->data = inc_dialog;
266 text[1] = account->account_name;
267 text[2] = _("Standby");
268 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
271 toolbar_set_sensitive(mainwin);
272 main_window_set_menu_sensitive(mainwin);
275 return inc_start(inc_dialog);
278 inc_spool_account(account);
287 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
289 GList *list, *queue_list = NULL;
290 IncProgressDialog *inc_dialog;
293 if (prefs_common.work_offline)
294 if (alertpanel(_("Offline warning"),
295 _("You're working offline. Override?"),
296 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
299 if (inc_lock_count) return;
301 inc_autocheck_timer_remove();
302 main_window_lock(mainwin);
304 if (prefs_common.inc_local) {
305 new_msgs = inc_spool();
310 list = account_get_list();
312 inc_finished(mainwin, new_msgs > 0);
313 main_window_unlock(mainwin);
314 inc_notify_cmd(new_msgs, notify);
315 inc_autocheck_timer_set();
319 /* check local folders */
322 /* check IMAP4 folders */
323 for (; list != NULL; list = list->next) {
324 PrefsAccount *account = list->data;
325 if ((account->protocol == A_IMAP4 ||
326 account->protocol == A_NNTP) && account->recv_at_getall) {
327 FolderItem *item = mainwin->summaryview->folder_item;
329 folderview_check_new(FOLDER(account->folder));
330 if (!prefs_common.scan_all_after_inc && item != NULL &&
331 FOLDER(account->folder) == item->folder)
332 folderview_update_item(item, TRUE);
336 /* check POP3 accounts */
337 for (list = account_get_list(); list != NULL; list = list->next) {
339 PrefsAccount *account = list->data;
340 account->session = STYPE_NORMAL;
341 if (account->recv_at_getall) {
342 session = inc_session_new(account);
344 queue_list = g_list_append(queue_list, session);
349 inc_finished(mainwin, new_msgs > 0);
350 main_window_unlock(mainwin);
351 inc_notify_cmd(new_msgs, notify);
352 inc_autocheck_timer_set();
356 inc_dialog = inc_progress_dialog_create();
357 inc_dialog->queue_list = queue_list;
358 inc_dialog->mainwin = mainwin;
359 for (list = queue_list; list != NULL; list = list->next) {
360 IncSession *session = list->data;
363 session->data = inc_dialog;
366 text[1] = session->pop3_state->ac_prefs->account_name;
367 text[2] = _("Standby");
368 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
371 toolbar_set_sensitive(mainwin);
372 main_window_set_menu_sensitive(mainwin);
374 new_msgs += inc_start(inc_dialog);
376 inc_finished(mainwin, new_msgs > 0);
377 main_window_unlock(mainwin);
378 inc_notify_cmd(new_msgs, notify);
379 inc_autocheck_timer_set();
382 static IncProgressDialog *inc_progress_dialog_create(void)
384 IncProgressDialog *dialog;
385 ProgressDialog *progress;
387 dialog = g_new0(IncProgressDialog, 1);
389 progress = progress_dialog_create();
390 gtk_window_set_title(GTK_WINDOW(progress->window),
391 _("Retrieving new messages"));
392 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
393 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
394 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
395 GTK_SIGNAL_FUNC(inc_dialog_delete_cb), dialog);
396 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
398 progress_dialog_set_value(progress, 0.0);
400 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
402 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
403 ¤txpm, ¤txpmmask);
404 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
405 &errorxpm, &errorxpmmask);
407 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
408 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
409 manage_window_get_focus_window())) {
410 dialog->show_dialog = TRUE;
411 gtk_widget_show_now(progress->window);
414 dialog->dialog = progress;
415 dialog->queue_list = NULL;
417 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
422 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
424 progress_dialog_set_value(inc_dialog->dialog, 0.0);
425 progress_dialog_set_label(inc_dialog->dialog, "");
426 if (inc_dialog->mainwin)
427 gtk_progress_bar_update
428 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
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);
436 if (inc_dialog->mainwin)
437 gtk_progress_bar_update
438 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
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 && account->protocol != A_APOP)
452 if (!account->recv_server || !account->userid)
455 session = g_new0(IncSession, 1);
456 session->pop3_state = pop3_state_new(account);
457 session->pop3_state->data = session;
458 session->folder_table = g_hash_table_new(NULL, NULL);
463 static void inc_session_destroy(IncSession *session)
465 g_return_if_fail(session != NULL);
467 pop3_state_destroy(session->pop3_state);
468 g_hash_table_destroy(session->folder_table);
472 static gint inc_start(IncProgressDialog *inc_dialog)
475 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
476 Pop3State *pop3_state;
484 while (inc_dialog->queue_list != NULL) {
485 session = inc_dialog->queue_list->data;
486 pop3_state = session->pop3_state;
488 inc_progress_dialog_clear(inc_dialog);
490 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
492 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
493 if (pop3_state->ac_prefs->passwd)
495 g_strdup(pop3_state->ac_prefs->passwd);
496 else if (pop3_state->ac_prefs->tmp_pass)
498 g_strdup(pop3_state->ac_prefs->tmp_pass);
502 pass = input_dialog_query_password
503 (pop3_state->ac_prefs->recv_server,
506 if (inc_dialog->mainwin && inc_dialog->show_dialog)
507 manage_window_focus_in
508 (inc_dialog->mainwin->window,
511 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
512 pop3_state->pass = pass;
514 inc_session_destroy(session);
515 inc_dialog->queue_list = g_list_remove
516 (inc_dialog->queue_list, session);
521 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
522 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
524 /* begin POP3 session */
525 inc_state = inc_pop3_session_do(session);
529 if (pop3_state->cur_total_num > 0)
530 msg = g_strdup_printf
531 (_("Done (%d message(s) (%s) received)"),
532 pop3_state->cur_total_num,
533 to_human_readable(pop3_state->cur_total_recv_bytes));
535 msg = g_strdup_printf(_("Done (no new messages)"));
536 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
537 gtk_clist_set_text(clist, num, 2, msg);
540 case INC_CONNECT_ERROR:
541 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
542 gtk_clist_set_text(clist, num, 2, _("Connection failed"));
544 case INC_AUTH_FAILED:
545 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
546 gtk_clist_set_text(clist, num, 2, _("Auth failed"));
549 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
550 gtk_clist_set_text(clist, num, 2, _("Locked"));
555 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
556 gtk_clist_set_text(clist, num, 2, _("Error"));
559 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
560 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
566 if (pop3_state->error_val == PS_AUTHFAIL) {
567 if(!prefs_common.no_recv_err_panel) {
568 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
569 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
570 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
573 (_("Authorization for %s on %s failed"),
575 pop3_state->ac_prefs->recv_server);
581 /* CLAWS: perform filtering actions on dropped message */
582 if (global_processing != NULL) {
583 FolderItem *processing, *inbox;
585 GSList *msglist, *msglist_element;
587 /* CLAWS: get default inbox (perhaps per account) */
588 if (pop3_state->ac_prefs->inbox) {
589 /* CLAWS: get destination folder / mailbox */
590 inbox = folder_find_item_from_identifier(pop3_state->ac_prefs->inbox);
592 inbox = folder_get_default_inbox();
594 inbox = folder_get_default_inbox();
596 /* get list of messages in processing */
597 processing = folder_get_default_processing();
598 folder_item_scan(processing);
599 msglist = folder_item_get_msg_list(processing);
601 /* process messages */
602 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
603 msginfo = (MsgInfo *) msglist_element->data;
604 /* filter if enabled in prefs or move to inbox if not */
605 if(pop3_state->ac_prefs->filter_on_recv) {
606 filter_message_by_msginfo_with_inbox(global_processing, msginfo,
607 session->folder_table,
610 folder_item_move_msg(inbox, msginfo);
611 g_hash_table_insert(session->folder_table, inbox,
614 procmsg_msginfo_free(msginfo);
616 g_slist_free(msglist);
620 new_msgs += pop3_state->cur_total_num;
622 if (!prefs_common.scan_all_after_inc) {
623 folder_item_scan_foreach(session->folder_table);
624 folderview_update_item_foreach
625 (session->folder_table,
626 !prefs_common.open_inbox_on_inc);
629 if (pop3_state->error_val == PS_AUTHFAIL &&
630 pop3_state->ac_prefs->tmp_pass) {
631 g_free(pop3_state->ac_prefs->tmp_pass);
632 pop3_state->ac_prefs->tmp_pass = NULL;
635 pop3_write_uidl_list(pop3_state);
637 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
639 if (inc_state == INC_NOSPACE || inc_state == INC_SOCKERR) {
640 inc_put_error(inc_state);
645 inc_session_destroy(session);
646 inc_dialog->queue_list =
647 g_list_remove(inc_dialog->queue_list, session);
653 fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
656 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
658 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
660 if (error_num && !prefs_common.no_recv_err_panel) {
661 if (inc_dialog->show_dialog)
662 manage_window_focus_in(inc_dialog->dialog->window,
664 alertpanel_error(_("Some errors occurred while getting mail."));
665 if (inc_dialog->show_dialog)
666 manage_window_focus_out(inc_dialog->dialog->window,
670 while (inc_dialog->queue_list != NULL) {
671 session = inc_dialog->queue_list->data;
672 inc_session_destroy(session);
673 inc_dialog->queue_list =
674 g_list_remove(inc_dialog->queue_list, session);
677 if (prefs_common.close_recv_dialog)
678 inc_progress_dialog_destroy(inc_dialog);
680 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
682 gtk_label_set_text(GTK_LABEL(GTK_BIN(inc_dialog->dialog->cancel_btn)->child),
691 static IncState inc_pop3_session_do(IncSession *session)
693 Pop3State *pop3_state = session->pop3_state;
694 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
701 static AtmHandler handlers[] = {
704 pop3_stls_send , pop3_stls_recv,
706 pop3_getauth_user_send , pop3_getauth_user_recv,
707 pop3_getauth_pass_send , pop3_getauth_pass_recv,
708 pop3_getauth_apop_send , pop3_getauth_apop_recv,
709 pop3_getrange_stat_send , pop3_getrange_stat_recv,
710 pop3_getrange_last_send , pop3_getrange_last_recv,
711 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
712 pop3_getsize_list_send , pop3_getsize_list_recv,
713 pop3_top_send , pop3_top_recv,
714 pop3_retr_send , pop3_retr_recv,
715 pop3_delete_send , pop3_delete_recv,
716 pop3_logout_send , pop3_logout_recv
719 debug_print("getting new messages of account %s...\n",
720 pop3_state->ac_prefs->account_name);
722 pop3_state->ac_prefs->last_pop_login_time = time(NULL);
723 atm = automaton_create(N_POP3_PHASE);
726 atm->data = pop3_state;
728 buf = g_strdup_printf(_("%s: Retrieving new messages"),
729 pop3_state->ac_prefs->recv_server);
730 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
733 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
734 atm->state[i].handler = handlers[i];
735 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
736 for (i = POP3_GREETING_RECV + 1; i < N_POP3_PHASE; ) {
737 atm->state[i++].condition = GDK_INPUT_WRITE;
738 atm->state[i++].condition = GDK_INPUT_READ;
741 atm->terminate = (AtmHandler)pop3_automaton_terminate;
742 atm->ui_func = (AtmUIFunc)inc_progress_update;
744 atm->num = POP3_GREETING_RECV;
746 server = pop3_state->ac_prefs->recv_server;
748 port = pop3_state->ac_prefs->set_popport ?
749 pop3_state->ac_prefs->popport :
750 pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
752 port = pop3_state->ac_prefs->set_popport ?
753 pop3_state->ac_prefs->popport : 110;
756 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
757 log_message("%s\n", buf);
758 progress_dialog_set_label(inc_dialog->dialog, buf);
763 if ((sockinfo = sock_connect(server, port)) == NULL) {
764 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
766 if(!prefs_common.no_recv_err_panel) {
767 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
768 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
769 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
771 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
773 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
775 pop3_automaton_terminate(NULL, atm);
776 automaton_destroy(atm);
777 session->inc_state = INC_CONNECT_ERROR;
778 return INC_CONNECT_ERROR;
782 if (pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL &&
783 !ssl_init_socket(sockinfo)) {
784 pop3_automaton_terminate(NULL, atm);
785 automaton_destroy(atm);
786 session->inc_state = INC_CONNECT_ERROR;
787 return INC_CONNECT_ERROR;
791 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
792 * to the sock structure - implement a reference counter?? */
793 pop3_state->sockinfo = sockinfo;
794 atm->help_sock = sockinfo;
796 log_verbosity_set(TRUE);
797 /* oha: this messes up inc_progress update:
798 disabling this would avoid the label "Retrieve Header"
799 being overwritten by "Retrieve Message"
800 Setting inc_pop3_recv_func is not necessary
801 since atm already handles the progress dialog ui
804 recv_set_ui_func(inc_pop3_recv_func, session);
806 atm->tag = sock_gdk_input_add(sockinfo,
807 atm->state[atm->num].condition,
808 automaton_input_cb, atm);
810 while (!atm->terminated)
811 gtk_main_iteration();
813 log_verbosity_set(FALSE);
815 recv_set_ui_func(NULL, NULL);
817 automaton_destroy(atm);
819 if (session->inc_state != INC_SUCCESS)
820 return session->inc_state;
822 switch (pop3_state->error_val) {
824 session->inc_state = INC_SUCCESS;
827 session->inc_state = INC_AUTH_FAILED;
830 session->inc_state = INC_NOSPACE;
833 session->inc_state = INC_SOCKERR;
836 session->inc_state = INC_LOCKED;
839 session->inc_state = INC_ERROR;
843 return session->inc_state;
846 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
848 if (atm->terminated) return 0;
851 gdk_input_remove(atm->tag);
854 if (atm->timeout_tag > 0) {
855 gtk_timeout_remove(atm->timeout_tag);
856 atm->timeout_tag = 0;
861 atm->terminated = TRUE;
866 static gboolean inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
869 gchar buf[MSGBUFSIZE];
870 IncSession *session = (IncSession *)data;
871 Pop3State *state = session->pop3_state;
872 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
873 ProgressDialog *dialog = inc_dialog->dialog;
877 cur_total = state->cur_total_bytes + read_bytes;
878 if (cur_total > state->total_bytes)
879 cur_total = state->total_bytes;
881 Xstrdup_a(total_size, to_human_readable(state->total_bytes),
883 g_snprintf(buf, sizeof(buf),
884 _("Retrieving message (%d / %d) (%s / %s)"),
885 state->cur_msg, state->count,
886 to_human_readable(cur_total), total_size);
887 progress_dialog_set_label(dialog, buf);
889 progress_dialog_set_percentage
890 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
891 if (inc_dialog->mainwin)
892 gtk_progress_bar_update
893 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
894 (gfloat)cur_total / (gfloat)state->total_bytes);
897 if (session->inc_state == INC_CANCEL)
903 void inc_progress_update(Pop3State *state, Pop3Phase phase)
905 gchar buf[MSGBUFSIZE];
906 IncSession *session = (IncSession *)state->data;
907 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
908 ProgressDialog *dialog = inc_dialog->dialog;
912 case POP3_GREETING_RECV:
914 case POP3_GETAUTH_USER_SEND:
915 case POP3_GETAUTH_PASS_SEND:
916 case POP3_GETAUTH_APOP_SEND:
917 progress_dialog_set_label(dialog, _("Authenticating..."));
919 case POP3_GETRANGE_STAT_SEND:
920 progress_dialog_set_label
921 (dialog, _("Getting the number of new messages (STAT)..."));
923 case POP3_GETRANGE_LAST_SEND:
924 progress_dialog_set_label
925 (dialog, _("Getting the number of new messages (LAST)..."));
927 case POP3_GETRANGE_UIDL_SEND:
928 progress_dialog_set_label
929 (dialog, _("Getting the number of new messages (UIDL)..."));
931 case POP3_GETSIZE_LIST_SEND:
932 progress_dialog_set_label
933 (dialog, _("Getting the size of messages (LIST)..."));
936 g_snprintf(buf, sizeof(buf),
937 _("Retrieving header (%d / %d)"),
938 state->cur_msg, state->count);
939 progress_dialog_set_label (dialog, buf);
940 progress_dialog_set_percentage
942 (gfloat)(state->cur_msg) /
943 (gfloat)(state->count));
944 if (inc_dialog->mainwin)
945 gtk_progress_bar_update
946 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
947 (gfloat)(state->cur_msg) /
948 (gfloat)(state->count));
951 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
952 g_snprintf(buf, sizeof(buf),
953 _("Retrieving message (%d / %d) (%s / %s)"),
954 state->cur_msg, state->count,
955 to_human_readable(state->cur_total_bytes),
957 progress_dialog_set_label(dialog, buf);
958 progress_dialog_set_percentage
960 (gfloat)(state->cur_total_bytes) /
961 (gfloat)(state->total_bytes));
962 if (inc_dialog->mainwin)
963 gtk_progress_bar_update
964 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
965 (gfloat)(state->cur_total_bytes) /
966 (gfloat)(state->total_bytes));
968 case POP3_DELETE_SEND:
969 if (state->msg[state->cur_msg].recv_time < state->current_time) {
970 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
972 progress_dialog_set_label(dialog, buf);
975 case POP3_LOGOUT_SEND:
976 progress_dialog_set_label(dialog, _("Quitting"));
983 gint inc_drop_message(const gchar *file, Pop3State *state)
986 FolderItem *dropfolder;
987 IncSession *session = (IncSession *)state->data;
991 /* CLAWS: get default inbox (perhaps per account) */
992 if (state->ac_prefs->inbox) {
993 /* CLAWS: get destination folder / mailbox */
994 inbox = folder_find_item_from_identifier
995 (state->ac_prefs->inbox);
997 inbox = folder_get_default_inbox();
999 inbox = folder_get_default_inbox();
1005 /* CLAWS: claws uses a global .processing folder for the filtering. */
1006 if (global_processing == NULL) {
1007 if (state->ac_prefs->filter_on_recv) {
1009 filter_get_dest_folder(prefs_common.fltlist, file);
1010 if (!dropfolder) dropfolder = inbox;
1011 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
1012 debug_print("a message won't be received\n");
1019 dropfolder = folder_get_default_processing();
1022 val = GPOINTER_TO_INT(g_hash_table_lookup
1023 (session->folder_table, dropfolder));
1025 folder_item_scan(dropfolder);
1026 /* force updating */
1027 if (FOLDER_IS_LOCAL(dropfolder->folder))
1028 dropfolder->mtime = 0;
1029 g_hash_table_insert(session->folder_table, dropfolder,
1030 GINT_TO_POINTER(1));
1033 /* add msg file to drop folder */
1034 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
1042 static void inc_put_error(IncState istate)
1046 if (!prefs_common.no_recv_err_panel)
1048 (_("Error occurred while processing mail."));
1051 alertpanel_error(_("No disk space left."));
1054 alertpanel_error(_("Socket error."));
1057 if (!prefs_common.no_recv_err_panel)
1058 alertpanel_error(_("Mailbox is locked."));
1065 static void inc_cancel(IncProgressDialog *dialog)
1067 IncSession *session;
1070 g_return_if_fail(dialog != NULL);
1072 if (dialog->queue_list == NULL) {
1073 inc_progress_dialog_destroy(dialog);
1077 session = dialog->queue_list->data;
1078 sockinfo = session->pop3_state->sockinfo;
1080 if (!sockinfo || session->atm->terminated == TRUE) return;
1082 session->pop3_state->cancelled = TRUE;
1083 session->inc_state = INC_CANCEL;
1084 session->atm->cancelled = TRUE;
1086 log_message(_("Incorporation cancelled\n"));
1089 gboolean inc_is_active(void)
1091 return (inc_dialog_list != NULL);
1094 void inc_cancel_all(void)
1098 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1099 inc_cancel((IncProgressDialog *)cur->data);
1102 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1104 inc_cancel((IncProgressDialog *)data);
1107 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1110 IncProgressDialog *dialog = (IncProgressDialog *)data;
1112 if (dialog->queue_list == NULL)
1113 inc_progress_dialog_destroy(dialog);
1118 static gint inc_spool(void)
1120 gchar *mbox, *logname;
1123 logname = g_get_user_name();
1124 mbox = g_strconcat(prefs_common.spool_path
1125 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1126 G_DIR_SEPARATOR_S, logname, NULL);
1127 msgs = get_spool(folder_get_default_inbox(), mbox);
1133 static void inc_spool_account(PrefsAccount *account)
1137 if (account->inbox) {
1138 inbox = folder_find_item_from_path(account->inbox);
1140 inbox = folder_get_default_inbox();
1142 inbox = folder_get_default_inbox();
1144 get_spool(inbox, account->local_mbox);
1147 static void inc_all_spool(void)
1151 list = account_get_list();
1154 for (; list != NULL; list = list->next) {
1155 PrefsAccount *account = list->data;
1157 if ((account->protocol == A_LOCAL) &&
1158 (account->recv_at_getall))
1159 inc_spool_account(account);
1163 static gint get_spool(FolderItem *dest, const gchar *mbox)
1167 gchar tmp_mbox[MAXPATHLEN + 1];
1168 GHashTable *folder_table = NULL;
1170 g_return_val_if_fail(dest != NULL, -1);
1171 g_return_val_if_fail(mbox != NULL, -1);
1173 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1174 debug_print("no messages in local mailbox.\n");
1176 } else if (size < 0)
1179 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1182 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1183 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1185 if (copy_mbox(mbox, tmp_mbox) < 0) {
1186 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1190 debug_print("Getting new messages from %s into %s...\n",
1193 if (prefs_common.filter_on_inc)
1194 folder_table = g_hash_table_new(NULL, NULL);
1195 msgs = proc_mbox(dest, tmp_mbox, folder_table);
1198 if (msgs >= 0) empty_mbox(mbox);
1199 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1202 if (!prefs_common.scan_all_after_inc) {
1203 g_hash_table_insert(folder_table, dest,
1204 GINT_TO_POINTER(1));
1205 folderview_update_item_foreach
1206 (folder_table, !prefs_common.open_inbox_on_inc);
1208 g_hash_table_destroy(folder_table);
1209 } else if (!prefs_common.scan_all_after_inc) {
1210 folderview_update_item(dest, TRUE);
1221 void inc_unlock(void)
1223 if (inc_lock_count > 0)
1227 static guint autocheck_timer = 0;
1228 static gpointer autocheck_data = NULL;
1230 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1235 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1236 *prefs_common.newmail_notify_cmd))
1238 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1239 buf[1] == 'd' && !strchr(&buf[1], '%'))
1240 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1243 buf = g_strdup(prefs_common.newmail_notify_cmd);
1245 execute_command_line(buf, TRUE);
1250 void inc_autocheck_timer_init(MainWindow *mainwin)
1252 autocheck_data = mainwin;
1253 inc_autocheck_timer_set();
1256 static void inc_autocheck_timer_set_interval(guint interval)
1258 inc_autocheck_timer_remove();
1259 /* last test is to avoid re-enabling auto_check after modifying
1260 the common preferences */
1261 if (prefs_common.autochk_newmail && autocheck_data
1262 && prefs_common.work_offline == FALSE) {
1263 autocheck_timer = gtk_timeout_add
1264 (interval, inc_autocheck_func, autocheck_data);
1265 debug_print("added timer = %d\n", autocheck_timer);
1269 void inc_autocheck_timer_set(void)
1271 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1274 void inc_autocheck_timer_remove(void)
1276 if (autocheck_timer) {
1277 debug_print("removed timer = %d\n", autocheck_timer);
1278 gtk_timeout_remove(autocheck_timer);
1279 autocheck_timer = 0;
1283 static gint inc_autocheck_func(gpointer data)
1285 MainWindow *mainwin = (MainWindow *)data;
1287 if (inc_lock_count) {
1288 debug_print("autocheck is locked.\n");
1289 inc_autocheck_timer_set_interval(1000);
1293 inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);