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"
64 #include "automaton.h"
66 #include "filtering.h"
67 #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 folder_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)
174 inc_autocheck_timer_remove();
175 main_window_lock(mainwin);
177 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
178 /* external incorporating program */
179 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
180 main_window_unlock(mainwin);
181 inc_autocheck_timer_set();
185 if (prefs_common.inc_local)
186 new_msgs = inc_spool();
191 if (prefs_common.inc_local) {
192 new_msgs = inc_spool();
196 cur_account->session = STYPE_NORMAL;
197 new_msgs += inc_account_mail(cur_account, mainwin);
200 inc_finished(mainwin, new_msgs > 0);
201 main_window_unlock(mainwin);
202 inc_notify_cmd(new_msgs, notify);
203 inc_autocheck_timer_set();
207 void inc_selective_download(MainWindow *mainwin, PrefsAccount *acc, gint session)
212 acc->session = session;
213 inc_account_mail(acc, mainwin);
214 acc->session = STYPE_NORMAL;
216 for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
217 HeaderItems *items =(HeaderItems*)cur->data;
219 if (items->state == SD_DOWNLOADED &&
220 items->del_by_old_session == FALSE) {
226 inc_finished(mainwin, TRUE);
227 inc_notify_cmd(new_msgs, prefs_common.newmail_notify_manu);
231 void inc_pop_before_smtp(PrefsAccount *acc)
233 acc->session = STYPE_POP_BEFORE_SMTP;
234 inc_account_mail(acc, NULL);
237 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
239 IncProgressDialog *inc_dialog;
242 FolderItem *item = NULL;
244 if(mainwin && mainwin->summaryview)
245 item = mainwin->summaryview->folder_item;
247 switch (account->protocol) {
250 folderview_check_new(FOLDER(account->folder));
251 if (!prefs_common.scan_all_after_inc && item != NULL &&
252 FOLDER(account->folder) == item->folder)
253 folder_update_item(item, TRUE);
258 session = inc_session_new(account);
259 if (!session) return 0;
261 inc_dialog = inc_progress_dialog_create();
262 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
264 inc_dialog->mainwin = mainwin;
265 session->data = inc_dialog;
268 text[1] = account->account_name;
269 text[2] = _("Standby");
270 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
273 toolbar_set_sensitive(mainwin);
274 main_window_set_menu_sensitive(mainwin);
277 return inc_start(inc_dialog);
280 inc_spool_account(account);
289 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
291 GList *list, *queue_list = NULL;
292 IncProgressDialog *inc_dialog;
295 if (prefs_common.work_offline)
296 if (alertpanel(_("Offline warning"),
297 _("You're working offline. Override?"),
298 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
301 if (inc_lock_count) return;
303 inc_autocheck_timer_remove();
304 main_window_lock(mainwin);
306 if (prefs_common.inc_local) {
307 new_msgs = inc_spool();
312 list = account_get_list();
314 inc_finished(mainwin, new_msgs > 0);
315 main_window_unlock(mainwin);
316 inc_notify_cmd(new_msgs, notify);
317 inc_autocheck_timer_set();
321 /* check local folders */
324 /* check IMAP4 folders */
325 for (; list != NULL; list = list->next) {
326 PrefsAccount *account = list->data;
327 if ((account->protocol == A_IMAP4 ||
328 account->protocol == A_NNTP) && account->recv_at_getall) {
329 FolderItem *item = mainwin->summaryview->folder_item;
331 folderview_check_new(FOLDER(account->folder));
332 if (!prefs_common.scan_all_after_inc && item != NULL &&
333 FOLDER(account->folder) == item->folder)
334 folder_update_item(item, TRUE);
338 /* check POP3 accounts */
339 for (list = account_get_list(); list != NULL; list = list->next) {
341 PrefsAccount *account = list->data;
342 account->session = STYPE_NORMAL;
343 if (account->recv_at_getall) {
344 session = inc_session_new(account);
346 queue_list = g_list_append(queue_list, session);
351 inc_finished(mainwin, new_msgs > 0);
352 main_window_unlock(mainwin);
353 inc_notify_cmd(new_msgs, notify);
354 inc_autocheck_timer_set();
358 inc_dialog = inc_progress_dialog_create();
359 inc_dialog->queue_list = queue_list;
360 inc_dialog->mainwin = mainwin;
361 for (list = queue_list; list != NULL; list = list->next) {
362 IncSession *session = list->data;
365 session->data = inc_dialog;
368 text[1] = session->pop3_state->ac_prefs->account_name;
369 text[2] = _("Standby");
370 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
373 toolbar_set_sensitive(mainwin);
374 main_window_set_menu_sensitive(mainwin);
376 new_msgs += inc_start(inc_dialog);
378 inc_finished(mainwin, new_msgs > 0);
379 main_window_unlock(mainwin);
380 inc_notify_cmd(new_msgs, notify);
381 inc_autocheck_timer_set();
384 static IncProgressDialog *inc_progress_dialog_create(void)
386 IncProgressDialog *dialog;
387 ProgressDialog *progress;
389 dialog = g_new0(IncProgressDialog, 1);
391 progress = progress_dialog_create();
392 gtk_window_set_title(GTK_WINDOW(progress->window),
393 _("Retrieving new messages"));
394 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
395 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
396 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
397 GTK_SIGNAL_FUNC(inc_dialog_delete_cb), dialog);
398 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
400 progress_dialog_set_value(progress, 0.0);
402 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
404 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
405 ¤txpm, ¤txpmmask);
406 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
407 &errorxpm, &errorxpmmask);
409 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
410 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
411 manage_window_get_focus_window())) {
412 dialog->show_dialog = TRUE;
413 gtk_widget_show_now(progress->window);
416 dialog->dialog = progress;
417 dialog->queue_list = NULL;
419 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
424 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
426 progress_dialog_set_value(inc_dialog->dialog, 0.0);
427 progress_dialog_set_label(inc_dialog->dialog, "");
428 if (inc_dialog->mainwin)
429 gtk_progress_bar_update
430 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
433 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
435 g_return_if_fail(inc_dialog != NULL);
437 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
438 if (inc_dialog->mainwin)
439 gtk_progress_bar_update
440 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
441 progress_dialog_destroy(inc_dialog->dialog);
446 static IncSession *inc_session_new(PrefsAccount *account)
450 g_return_val_if_fail(account != NULL, NULL);
452 if (account->protocol != A_POP3 && account->protocol != A_APOP)
454 if (!account->recv_server || !account->userid)
457 session = g_new0(IncSession, 1);
458 session->pop3_state = pop3_state_new(account);
459 session->pop3_state->data = session;
464 static void inc_session_destroy(IncSession *session)
466 g_return_if_fail(session != NULL);
468 pop3_state_destroy(session->pop3_state);
472 static gint inc_start(IncProgressDialog *inc_dialog)
475 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
476 Pop3State *pop3_state;
483 FolderItem *processing, *inbox;
485 GSList *msglist, *msglist_element;
487 while (inc_dialog->queue_list != NULL) {
488 session = inc_dialog->queue_list->data;
489 pop3_state = session->pop3_state;
491 inc_progress_dialog_clear(inc_dialog);
493 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
495 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
496 if (pop3_state->ac_prefs->passwd)
498 g_strdup(pop3_state->ac_prefs->passwd);
499 else if (pop3_state->ac_prefs->tmp_pass)
501 g_strdup(pop3_state->ac_prefs->tmp_pass);
505 pass = input_dialog_query_password
506 (pop3_state->ac_prefs->recv_server,
509 if (inc_dialog->mainwin && inc_dialog->show_dialog)
510 manage_window_focus_in
511 (inc_dialog->mainwin->window,
514 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
515 pop3_state->pass = pass;
517 inc_session_destroy(session);
518 inc_dialog->queue_list = g_list_remove
519 (inc_dialog->queue_list, session);
524 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
525 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
527 /* begin POP3 session */
528 inc_state = inc_pop3_session_do(session);
532 if (pop3_state->cur_total_num > 0)
533 msg = g_strdup_printf
534 (_("Done (%d message(s) (%s) received)"),
535 pop3_state->cur_total_num,
536 to_human_readable(pop3_state->cur_total_recv_bytes));
538 msg = g_strdup_printf(_("Done (no new messages)"));
539 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
540 gtk_clist_set_text(clist, num, 2, msg);
543 case INC_CONNECT_ERROR:
544 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
545 gtk_clist_set_text(clist, num, 2, _("Connection failed"));
547 case INC_AUTH_FAILED:
548 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
549 gtk_clist_set_text(clist, num, 2, _("Auth failed"));
552 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
553 gtk_clist_set_text(clist, num, 2, _("Locked"));
558 case INC_SOCKET_ERROR:
559 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
560 gtk_clist_set_text(clist, num, 2, _("Error"));
563 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
564 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
570 if (pop3_state->error_val == PS_AUTHFAIL) {
571 if(!prefs_common.no_recv_err_panel) {
572 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
573 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
574 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
577 (_("Authorization for %s on %s failed"),
579 pop3_state->ac_prefs->recv_server);
583 /* CLAWS: perform filtering actions on dropped message */
584 /* CLAWS: get default inbox (perhaps per account) */
585 if (pop3_state->ac_prefs->inbox) {
586 /* CLAWS: get destination folder / mailbox */
587 inbox = folder_find_item_from_identifier(pop3_state->ac_prefs->inbox);
589 inbox = folder_get_default_inbox();
591 inbox = folder_get_default_inbox();
593 /* get list of messages in processing */
594 processing = folder_get_default_processing();
595 folder_item_scan(processing);
596 msglist = folder_item_get_msg_list(processing);
598 /* process messages */
599 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
600 msginfo = (MsgInfo *) msglist_element->data;
601 /* filter if enabled in prefs or move to inbox if not */
602 if(global_processing && pop3_state->ac_prefs->filter_on_recv) {
603 filter_message_by_msginfo_with_inbox(global_processing, msginfo,
606 folder_item_move_msg(inbox, msginfo);
608 procmsg_msginfo_free(msginfo);
610 g_slist_free(msglist);
613 new_msgs += pop3_state->cur_total_num;
615 if (!prefs_common.scan_all_after_inc) {
616 folder_update_items_when_required
617 (!prefs_common.open_inbox_on_inc);
620 if (pop3_state->error_val == PS_AUTHFAIL &&
621 pop3_state->ac_prefs->tmp_pass) {
622 g_free(pop3_state->ac_prefs->tmp_pass);
623 pop3_state->ac_prefs->tmp_pass = NULL;
626 pop3_write_uidl_list(pop3_state);
628 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
630 if (inc_state == INC_ERROR ||
631 inc_state == INC_NO_SPACE ||
632 inc_state == INC_IO_ERROR ||
633 inc_state == INC_SOCKET_ERROR) {
634 inc_put_error(inc_state);
639 inc_session_destroy(session);
640 inc_dialog->queue_list =
641 g_list_remove(inc_dialog->queue_list, session);
647 fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
650 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
652 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
654 if (error_num && !prefs_common.no_recv_err_panel) {
655 if (inc_dialog->show_dialog)
656 manage_window_focus_in(inc_dialog->dialog->window,
658 alertpanel_error(_("Some errors occurred while getting mail."));
659 if (inc_dialog->show_dialog)
660 manage_window_focus_out(inc_dialog->dialog->window,
664 while (inc_dialog->queue_list != NULL) {
665 session = inc_dialog->queue_list->data;
666 inc_session_destroy(session);
667 inc_dialog->queue_list =
668 g_list_remove(inc_dialog->queue_list, session);
671 if (prefs_common.close_recv_dialog)
672 inc_progress_dialog_destroy(inc_dialog);
674 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
676 gtk_label_set_text(GTK_LABEL(GTK_BIN(inc_dialog->dialog->cancel_btn)->child),
685 static IncState inc_pop3_session_do(IncSession *session)
687 Pop3State *pop3_state = session->pop3_state;
688 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
695 static AtmHandler handlers[] = {
698 pop3_stls_send , pop3_stls_recv,
700 pop3_getauth_user_send , pop3_getauth_user_recv,
701 pop3_getauth_pass_send , pop3_getauth_pass_recv,
702 pop3_getauth_apop_send , pop3_getauth_apop_recv,
703 pop3_getrange_stat_send , pop3_getrange_stat_recv,
704 pop3_getrange_last_send , pop3_getrange_last_recv,
705 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
706 pop3_getsize_list_send , pop3_getsize_list_recv,
707 pop3_top_send , pop3_top_recv,
708 pop3_retr_send , pop3_retr_recv,
709 pop3_delete_send , pop3_delete_recv,
710 pop3_logout_send , pop3_logout_recv
713 debug_print("getting new messages of account %s...\n",
714 pop3_state->ac_prefs->account_name);
716 pop3_state->ac_prefs->last_pop_login_time = time(NULL);
717 atm = automaton_create(N_POP3_PHASE);
720 atm->data = pop3_state;
722 buf = g_strdup_printf(_("%s: Retrieving new messages"),
723 pop3_state->ac_prefs->recv_server);
724 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
727 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
728 atm->state[i].handler = handlers[i];
729 atm->state[POP3_GREETING_RECV].condition = (G_IO_IN | G_IO_HUP | G_IO_ERR);
730 for (i = POP3_GREETING_RECV + 1; i < N_POP3_PHASE; ) {
731 atm->state[i++].condition = (G_IO_OUT | G_IO_ERR);
732 atm->state[i++].condition = (G_IO_IN | G_IO_HUP | G_IO_ERR);
735 atm->terminate = (AtmHandler)pop3_automaton_terminate;
736 atm->ui_func = (AtmUIFunc)inc_progress_update;
738 atm->num = POP3_GREETING_RECV;
740 server = pop3_state->ac_prefs->recv_server;
742 port = pop3_state->ac_prefs->set_popport ?
743 pop3_state->ac_prefs->popport :
744 pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
746 port = pop3_state->ac_prefs->set_popport ?
747 pop3_state->ac_prefs->popport : 110;
750 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
751 log_message("%s\n", buf);
752 progress_dialog_set_label(inc_dialog->dialog, buf);
756 if ((sockinfo = sock_connect(server, port)) == NULL) {
757 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
759 if(!prefs_common.no_recv_err_panel) {
760 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
761 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
762 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
764 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
766 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
768 pop3_automaton_terminate(NULL, atm);
769 automaton_destroy(atm);
770 session->inc_state = INC_CONNECT_ERROR;
771 return INC_CONNECT_ERROR;
775 if (pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL &&
776 !ssl_init_socket(sockinfo)) {
777 pop3_automaton_terminate(sockinfo, atm);
778 automaton_destroy(atm);
779 session->inc_state = INC_CONNECT_ERROR;
780 return INC_CONNECT_ERROR;
784 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
785 * to the sock structure - implement a reference counter?? */
786 pop3_state->sockinfo = sockinfo;
787 atm->help_sock = sockinfo;
789 statusbar_verbosity_set(TRUE);
790 /* oha: this messes up inc_progress update:
791 disabling this would avoid the label "Retrieve Header"
792 being overwritten by "Retrieve Message"
793 Setting inc_pop3_recv_func is not necessary
794 since atm already handles the progress dialog ui
797 recv_set_ui_func(inc_pop3_recv_func, session);
799 atm->tag = sock_input_add(sockinfo,
800 atm->state[atm->num].condition,
801 automaton_input_cb, atm);
803 while (!atm->terminated && !atm->cancelled)
804 gtk_main_iteration();
806 if (!atm->terminated)
807 pop3_automaton_terminate(sockinfo, atm);
809 statusbar_verbosity_set(FALSE);
811 recv_set_ui_func(NULL, NULL);
813 automaton_destroy(atm);
815 if (session->inc_state != INC_SUCCESS)
816 return session->inc_state;
818 switch (pop3_state->error_val) {
820 session->inc_state = INC_SUCCESS;
823 session->inc_state = INC_AUTH_FAILED;
826 session->inc_state = INC_IO_ERROR;
829 session->inc_state = INC_SOCKET_ERROR;
832 session->inc_state = INC_LOCKED;
835 session->inc_state = INC_ERROR;
839 return session->inc_state;
842 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
844 if (atm->terminated) return 0;
847 gdk_input_remove(atm->tag);
850 if (atm->timeout_tag > 0) {
851 gtk_timeout_remove(atm->timeout_tag);
852 atm->timeout_tag = 0;
857 atm->terminated = TRUE;
862 static gboolean inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
865 gchar buf[MSGBUFSIZE];
866 IncSession *session = (IncSession *)data;
867 Pop3State *state = session->pop3_state;
868 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
869 ProgressDialog *dialog = inc_dialog->dialog;
873 cur_total = state->cur_total_bytes + read_bytes;
874 if (cur_total > state->total_bytes)
875 cur_total = state->total_bytes;
877 Xstrdup_a(total_size, to_human_readable(state->total_bytes),
879 g_snprintf(buf, sizeof(buf),
880 _("Retrieving message (%d / %d) (%s / %s)"),
881 state->cur_msg, state->count,
882 to_human_readable(cur_total), total_size);
883 progress_dialog_set_label(dialog, buf);
885 progress_dialog_set_percentage
886 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
887 if (inc_dialog->mainwin)
888 gtk_progress_bar_update
889 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
890 (gfloat)cur_total / (gfloat)state->total_bytes);
893 if (session->inc_state == INC_CANCEL)
899 void inc_progress_update(Pop3State *state, Pop3Phase phase)
901 gchar buf[MSGBUFSIZE];
902 IncSession *session = (IncSession *)state->data;
903 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
904 ProgressDialog *dialog = inc_dialog->dialog;
908 case POP3_GREETING_RECV:
910 case POP3_GETAUTH_USER_SEND:
911 case POP3_GETAUTH_PASS_SEND:
912 case POP3_GETAUTH_APOP_SEND:
913 progress_dialog_set_label(dialog, _("Authenticating..."));
915 case POP3_GETRANGE_STAT_SEND:
916 progress_dialog_set_label
917 (dialog, _("Getting the number of new messages (STAT)..."));
919 case POP3_GETRANGE_LAST_SEND:
920 progress_dialog_set_label
921 (dialog, _("Getting the number of new messages (LAST)..."));
923 case POP3_GETRANGE_UIDL_SEND:
924 progress_dialog_set_label
925 (dialog, _("Getting the number of new messages (UIDL)..."));
927 case POP3_GETSIZE_LIST_SEND:
928 progress_dialog_set_label
929 (dialog, _("Getting the size of messages (LIST)..."));
932 g_snprintf(buf, sizeof(buf),
933 _("Retrieving header (%d / %d)"),
934 state->cur_msg, state->count);
935 progress_dialog_set_label (dialog, buf);
936 progress_dialog_set_percentage
938 (gfloat)(state->cur_msg) /
939 (gfloat)(state->count));
940 if (inc_dialog->mainwin)
941 gtk_progress_bar_update
942 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
943 (gfloat)(state->cur_msg) /
944 (gfloat)(state->count));
947 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
948 g_snprintf(buf, sizeof(buf),
949 _("Retrieving message (%d / %d) (%s / %s)"),
950 state->cur_msg, state->count,
951 to_human_readable(state->cur_total_bytes),
953 progress_dialog_set_label(dialog, buf);
954 progress_dialog_set_percentage
956 (gfloat)(state->cur_total_bytes) /
957 (gfloat)(state->total_bytes));
958 if (inc_dialog->mainwin)
959 gtk_progress_bar_update
960 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
961 (gfloat)(state->cur_total_bytes) /
962 (gfloat)(state->total_bytes));
964 case POP3_DELETE_SEND:
965 if (state->msg[state->cur_msg].recv_time < state->current_time) {
966 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
968 progress_dialog_set_label(dialog, buf);
971 case POP3_LOGOUT_SEND:
972 progress_dialog_set_label(dialog, _("Quitting"));
979 gint inc_drop_message(const gchar *file, Pop3State *state)
982 FolderItem *dropfolder;
985 /* CLAWS: get default inbox (perhaps per account) */
986 if (state->ac_prefs->inbox) {
987 /* CLAWS: get destination folder / mailbox */
988 inbox = folder_find_item_from_identifier
989 (state->ac_prefs->inbox);
991 inbox = folder_get_default_inbox();
993 inbox = folder_get_default_inbox();
999 /* CLAWS: claws uses a global .processing folder for the filtering. */
1000 dropfolder = folder_get_default_processing();
1002 /* add msg file to drop folder */
1003 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
1011 static void inc_put_error(IncState istate)
1015 if (!prefs_common.no_recv_err_panel)
1017 (_("Error occurred while processing mail."));
1020 alertpanel_error(_("No disk space left."));
1023 alertpanel_error(_("Can't write file."));
1025 case INC_SOCKET_ERROR:
1026 alertpanel_error(_("Socket error."));
1029 if (!prefs_common.no_recv_err_panel)
1030 alertpanel_error(_("Mailbox is locked."));
1037 static void inc_cancel(IncProgressDialog *dialog)
1039 IncSession *session;
1042 g_return_if_fail(dialog != NULL);
1044 if (dialog->queue_list == NULL) {
1045 inc_progress_dialog_destroy(dialog);
1049 session = dialog->queue_list->data;
1050 sockinfo = session->pop3_state->sockinfo;
1052 if (!sockinfo || session->atm->terminated == TRUE) return;
1054 session->pop3_state->cancelled = TRUE;
1055 session->inc_state = INC_CANCEL;
1056 session->atm->cancelled = TRUE;
1058 log_message(_("Incorporation cancelled\n"));
1061 gboolean inc_is_active(void)
1063 return (inc_dialog_list != NULL);
1066 void inc_cancel_all(void)
1070 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1071 inc_cancel((IncProgressDialog *)cur->data);
1074 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1076 inc_cancel((IncProgressDialog *)data);
1079 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1082 IncProgressDialog *dialog = (IncProgressDialog *)data;
1084 if (dialog->queue_list == NULL)
1085 inc_progress_dialog_destroy(dialog);
1090 static gint inc_spool(void)
1092 gchar *mbox, *logname;
1095 logname = g_get_user_name();
1096 mbox = g_strconcat(prefs_common.spool_path
1097 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1098 G_DIR_SEPARATOR_S, logname, NULL);
1099 msgs = get_spool(folder_get_default_inbox(), mbox);
1105 static void inc_spool_account(PrefsAccount *account)
1109 if (account->inbox) {
1110 inbox = folder_find_item_from_path(account->inbox);
1112 inbox = folder_get_default_inbox();
1114 inbox = folder_get_default_inbox();
1116 get_spool(inbox, account->local_mbox);
1119 static void inc_all_spool(void)
1123 list = account_get_list();
1126 for (; list != NULL; list = list->next) {
1127 PrefsAccount *account = list->data;
1129 if ((account->protocol == A_LOCAL) &&
1130 (account->recv_at_getall))
1131 inc_spool_account(account);
1135 static gint get_spool(FolderItem *dest, const gchar *mbox)
1139 gchar tmp_mbox[MAXPATHLEN + 1];
1141 g_return_val_if_fail(dest != NULL, -1);
1142 g_return_val_if_fail(mbox != NULL, -1);
1144 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1145 debug_print("no messages in local mailbox.\n");
1147 } else if (size < 0)
1150 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1153 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1154 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1156 if (copy_mbox(mbox, tmp_mbox) < 0) {
1157 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1161 debug_print("Getting new messages from %s into %s...\n",
1164 msgs = proc_mbox(dest, tmp_mbox);
1167 if (msgs >= 0) empty_mbox(mbox);
1168 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1170 if (!prefs_common.scan_all_after_inc) {
1171 folder_update_items_when_required
1172 (!prefs_common.open_inbox_on_inc);
1173 } else if (!prefs_common.scan_all_after_inc) {
1174 folder_update_item(dest, TRUE);
1185 void inc_unlock(void)
1187 if (inc_lock_count > 0)
1191 static guint autocheck_timer = 0;
1192 static gpointer autocheck_data = NULL;
1194 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1199 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1200 *prefs_common.newmail_notify_cmd))
1202 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1203 buf[1] == 'd' && !strchr(&buf[1], '%'))
1204 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1207 buf = g_strdup(prefs_common.newmail_notify_cmd);
1209 execute_command_line(buf, TRUE);
1214 void inc_autocheck_timer_init(MainWindow *mainwin)
1216 autocheck_data = mainwin;
1217 inc_autocheck_timer_set();
1220 static void inc_autocheck_timer_set_interval(guint interval)
1222 inc_autocheck_timer_remove();
1223 /* last test is to avoid re-enabling auto_check after modifying
1224 the common preferences */
1225 if (prefs_common.autochk_newmail && autocheck_data
1226 && prefs_common.work_offline == FALSE) {
1227 autocheck_timer = gtk_timeout_add
1228 (interval, inc_autocheck_func, autocheck_data);
1229 debug_print("added timer = %d\n", autocheck_timer);
1233 void inc_autocheck_timer_set(void)
1235 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1238 void inc_autocheck_timer_remove(void)
1240 if (autocheck_timer) {
1241 debug_print("removed timer = %d\n", autocheck_timer);
1242 gtk_timeout_remove(autocheck_timer);
1243 autocheck_timer = 0;
1247 static gint inc_autocheck_func(gpointer data)
1249 MainWindow *mainwin = (MainWindow *)data;
1251 if (inc_lock_count) {
1252 debug_print("autocheck is locked.\n");
1253 inc_autocheck_timer_set_interval(1000);
1257 inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);