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"
69 static GList *inc_dialog_list = NULL;
71 static guint inc_lock_count = 0;
73 static GdkPixmap *currentxpm;
74 static GdkBitmap *currentxpmmask;
75 static GdkPixmap *errorxpm;
76 static GdkBitmap *errorxpmmask;
77 static GdkPixmap *okxpm;
78 static GdkBitmap *okxpmmask;
80 #define MSGBUFSIZE 8192
82 static void inc_finished (MainWindow *mainwin,
83 gboolean new_messages);
84 static gint inc_account_mail (PrefsAccount *account,
87 static IncProgressDialog *inc_progress_dialog_create (void);
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);
94 static gint pop3_automaton_terminate (SockInfo *source,
97 static gboolean inc_pop3_recv_func (SockInfo *sock,
102 static void inc_put_error (IncState istate);
104 static void inc_cancel_cb (GtkWidget *widget,
106 static gint inc_dialog_delete_cb (GtkWidget *widget,
110 static gint inc_spool (void);
111 static gint get_spool (FolderItem *dest,
114 static void inc_spool_account(PrefsAccount *account);
115 static void inc_all_spool(void);
116 static void inc_autocheck_timer_set_interval (guint interval);
117 static gint inc_autocheck_func (gpointer data);
119 static void inc_notify_cmd (gint new_msgs,
122 #define FOLDER_SUMMARY_MISMATCH(f, s) \
123 (f) && (s) ? ((s)->newmsgs != (f)->new) || ((f)->unread != (s)->unread) || ((f)->total != (s)->messages) \
128 * @mainwin: Main window.
129 * @new_messages: TRUE if some messages have been received.
131 * Update the folder view and the summary view after receiving
132 * messages. If @new_messages is FALSE, this function avoids unneeded
135 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
139 if (prefs_common.scan_all_after_inc)
140 folderview_check_new(NULL);
142 if (!new_messages && !prefs_common.scan_all_after_inc) return;
144 if (prefs_common.open_inbox_on_inc) {
145 item = cur_account && cur_account->inbox
146 ? folder_find_item_from_identifier(cur_account->inbox)
147 : folder_get_default_inbox();
148 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
149 folderview_unselect(mainwin->folderview);
150 folderview_select(mainwin->folderview, item);
152 } else if (prefs_common.scan_all_after_inc) {
153 item = mainwin->summaryview->folder_item;
154 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
155 folder_update_item(item, TRUE);
160 void inc_mail(MainWindow *mainwin, gboolean notify)
164 if (inc_lock_count) return;
166 if (prefs_common.work_offline)
167 if (alertpanel(_("Offline warning"),
168 _("You're working offline. Override?"),
169 _("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();
206 void inc_selective_download(MainWindow *mainwin, PrefsAccount *acc, gint session)
211 acc->session = session;
212 inc_account_mail(acc, mainwin);
213 acc->session = STYPE_NORMAL;
215 for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
216 HeaderItems *items =(HeaderItems*)cur->data;
218 if (items->state == SD_DOWNLOADED &&
219 items->del_by_old_session == FALSE) {
225 inc_finished(mainwin, TRUE);
226 inc_notify_cmd(new_msgs, prefs_common.newmail_notify_manu);
230 void inc_pop_before_smtp(PrefsAccount *acc)
232 acc->session = STYPE_POP_BEFORE_SMTP;
233 inc_account_mail(acc, NULL);
236 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
238 IncProgressDialog *inc_dialog;
243 if(mainwin && mainwin->summaryview)
244 item = mainwin->summaryview->folder_item;
246 switch (account->protocol) {
249 folderview_check_new(FOLDER(account->folder));
250 if (!prefs_common.scan_all_after_inc && item != NULL &&
251 FOLDER(account->folder) == item->folder)
252 folder_update_item(item, TRUE);
257 session = inc_session_new(account);
258 if (!session) return 0;
260 inc_dialog = inc_progress_dialog_create();
261 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
263 inc_dialog->mainwin = mainwin;
264 session->data = inc_dialog;
267 text[1] = account->account_name;
268 text[2] = _("Standby");
269 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
272 toolbar_set_sensitive(mainwin);
273 main_window_set_menu_sensitive(mainwin);
276 return inc_start(inc_dialog);
279 inc_spool_account(account);
288 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
290 GList *list, *queue_list = NULL;
291 IncProgressDialog *inc_dialog;
294 if (prefs_common.work_offline)
295 if (alertpanel(_("Offline warning"),
296 _("You're working offline. Override?"),
297 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
300 if (inc_lock_count) return;
302 inc_autocheck_timer_remove();
303 main_window_lock(mainwin);
305 if (prefs_common.inc_local) {
306 new_msgs = inc_spool();
311 list = account_get_list();
313 inc_finished(mainwin, new_msgs > 0);
314 main_window_unlock(mainwin);
315 inc_notify_cmd(new_msgs, notify);
316 inc_autocheck_timer_set();
320 /* check local folders */
323 /* check IMAP4 folders */
324 for (; list != NULL; list = list->next) {
325 PrefsAccount *account = list->data;
326 if ((account->protocol == A_IMAP4 ||
327 account->protocol == A_NNTP) && account->recv_at_getall) {
328 FolderItem *item = mainwin->summaryview->folder_item;
330 folderview_check_new(FOLDER(account->folder));
331 if (!prefs_common.scan_all_after_inc && item != NULL &&
332 FOLDER(account->folder) == item->folder)
333 folder_update_item(item, TRUE);
337 /* check POP3 accounts */
338 for (list = account_get_list(); list != NULL; list = list->next) {
340 PrefsAccount *account = list->data;
341 account->session = STYPE_NORMAL;
342 if (account->recv_at_getall) {
343 session = inc_session_new(account);
345 queue_list = g_list_append(queue_list, session);
350 inc_finished(mainwin, new_msgs > 0);
351 main_window_unlock(mainwin);
352 inc_notify_cmd(new_msgs, notify);
353 inc_autocheck_timer_set();
357 inc_dialog = inc_progress_dialog_create();
358 inc_dialog->queue_list = queue_list;
359 inc_dialog->mainwin = mainwin;
360 for (list = queue_list; list != NULL; list = list->next) {
361 IncSession *session = list->data;
364 session->data = inc_dialog;
367 text[1] = session->pop3_state->ac_prefs->account_name;
368 text[2] = _("Standby");
369 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
372 toolbar_set_sensitive(mainwin);
373 main_window_set_menu_sensitive(mainwin);
375 new_msgs += inc_start(inc_dialog);
377 inc_finished(mainwin, new_msgs > 0);
378 main_window_unlock(mainwin);
379 inc_notify_cmd(new_msgs, notify);
380 inc_autocheck_timer_set();
383 static IncProgressDialog *inc_progress_dialog_create(void)
385 IncProgressDialog *dialog;
386 ProgressDialog *progress;
388 dialog = g_new0(IncProgressDialog, 1);
390 progress = progress_dialog_create();
391 gtk_window_set_title(GTK_WINDOW(progress->window),
392 _("Retrieving new messages"));
393 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
394 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
395 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
396 GTK_SIGNAL_FUNC(inc_dialog_delete_cb), dialog);
397 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
399 progress_dialog_set_value(progress, 0.0);
401 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
403 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
404 ¤txpm, ¤txpmmask);
405 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
406 &errorxpm, &errorxpmmask);
408 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
409 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
410 manage_window_get_focus_window())) {
411 dialog->show_dialog = TRUE;
412 gtk_widget_show_now(progress->window);
415 dialog->dialog = progress;
416 dialog->queue_list = NULL;
418 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
423 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
425 progress_dialog_set_value(inc_dialog->dialog, 0.0);
426 progress_dialog_set_label(inc_dialog->dialog, "");
427 if (inc_dialog->mainwin)
428 gtk_progress_bar_update
429 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
432 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
434 g_return_if_fail(inc_dialog != NULL);
436 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
437 if (inc_dialog->mainwin)
438 gtk_progress_bar_update
439 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
440 progress_dialog_destroy(inc_dialog->dialog);
445 static IncSession *inc_session_new(PrefsAccount *account)
449 g_return_val_if_fail(account != NULL, NULL);
451 if (account->protocol != A_POP3 && account->protocol != A_APOP)
453 if (!account->recv_server || !account->userid)
456 session = g_new0(IncSession, 1);
457 session->pop3_state = pop3_state_new(account);
458 session->pop3_state->data = session;
463 static void inc_session_destroy(IncSession *session)
465 g_return_if_fail(session != NULL);
467 pop3_state_destroy(session->pop3_state);
471 static gint inc_start(IncProgressDialog *inc_dialog)
474 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
475 Pop3State *pop3_state;
483 while (inc_dialog->queue_list != NULL) {
484 session = inc_dialog->queue_list->data;
485 pop3_state = session->pop3_state;
487 inc_progress_dialog_clear(inc_dialog);
489 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
491 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
492 if (pop3_state->ac_prefs->passwd)
494 g_strdup(pop3_state->ac_prefs->passwd);
495 else if (pop3_state->ac_prefs->tmp_pass)
497 g_strdup(pop3_state->ac_prefs->tmp_pass);
501 pass = input_dialog_query_password
502 (pop3_state->ac_prefs->recv_server,
505 if (inc_dialog->mainwin && inc_dialog->show_dialog)
506 manage_window_focus_in
507 (inc_dialog->mainwin->window,
510 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
511 pop3_state->pass = pass;
513 inc_session_destroy(session);
514 inc_dialog->queue_list = g_list_remove
515 (inc_dialog->queue_list, session);
520 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
521 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
523 /* begin POP3 session */
524 inc_state = inc_pop3_session_do(session);
528 if (pop3_state->cur_total_num > 0)
529 msg = g_strdup_printf
530 (_("Done (%d message(s) (%s) received)"),
531 pop3_state->cur_total_num,
532 to_human_readable(pop3_state->cur_total_recv_bytes));
534 msg = g_strdup_printf(_("Done (no new messages)"));
535 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
536 gtk_clist_set_text(clist, num, 2, msg);
539 case INC_CONNECT_ERROR:
540 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
541 gtk_clist_set_text(clist, num, 2, _("Connection failed"));
543 case INC_AUTH_FAILED:
544 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
545 gtk_clist_set_text(clist, num, 2, _("Auth failed"));
548 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
549 gtk_clist_set_text(clist, num, 2, _("Locked"));
554 case INC_SOCKET_ERROR:
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,
609 folder_item_move_msg(inbox, msginfo);
611 procmsg_msginfo_free(msginfo);
613 g_slist_free(msglist);
617 new_msgs += pop3_state->cur_total_num;
619 if (!prefs_common.scan_all_after_inc) {
620 folder_update_items_when_required
621 (!prefs_common.open_inbox_on_inc);
624 if (pop3_state->error_val == PS_AUTHFAIL &&
625 pop3_state->ac_prefs->tmp_pass) {
626 g_free(pop3_state->ac_prefs->tmp_pass);
627 pop3_state->ac_prefs->tmp_pass = NULL;
630 pop3_write_uidl_list(pop3_state);
632 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
634 if (inc_state == INC_ERROR ||
635 inc_state == INC_NO_SPACE ||
636 inc_state == INC_IO_ERROR ||
637 inc_state == INC_SOCKET_ERROR) {
638 inc_put_error(inc_state);
643 inc_session_destroy(session);
644 inc_dialog->queue_list =
645 g_list_remove(inc_dialog->queue_list, session);
651 fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
654 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
656 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
658 if (error_num && !prefs_common.no_recv_err_panel) {
659 if (inc_dialog->show_dialog)
660 manage_window_focus_in(inc_dialog->dialog->window,
662 alertpanel_error(_("Some errors occurred while getting mail."));
663 if (inc_dialog->show_dialog)
664 manage_window_focus_out(inc_dialog->dialog->window,
668 while (inc_dialog->queue_list != NULL) {
669 session = inc_dialog->queue_list->data;
670 inc_session_destroy(session);
671 inc_dialog->queue_list =
672 g_list_remove(inc_dialog->queue_list, session);
675 if (prefs_common.close_recv_dialog)
676 inc_progress_dialog_destroy(inc_dialog);
678 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
680 gtk_label_set_text(GTK_LABEL(GTK_BIN(inc_dialog->dialog->cancel_btn)->child),
689 static IncState inc_pop3_session_do(IncSession *session)
691 Pop3State *pop3_state = session->pop3_state;
692 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
699 static AtmHandler handlers[] = {
702 pop3_stls_send , pop3_stls_recv,
704 pop3_getauth_user_send , pop3_getauth_user_recv,
705 pop3_getauth_pass_send , pop3_getauth_pass_recv,
706 pop3_getauth_apop_send , pop3_getauth_apop_recv,
707 pop3_getrange_stat_send , pop3_getrange_stat_recv,
708 pop3_getrange_last_send , pop3_getrange_last_recv,
709 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
710 pop3_getsize_list_send , pop3_getsize_list_recv,
711 pop3_top_send , pop3_top_recv,
712 pop3_retr_send , pop3_retr_recv,
713 pop3_delete_send , pop3_delete_recv,
714 pop3_logout_send , pop3_logout_recv
717 debug_print("getting new messages of account %s...\n",
718 pop3_state->ac_prefs->account_name);
720 pop3_state->ac_prefs->last_pop_login_time = time(NULL);
721 atm = automaton_create(N_POP3_PHASE);
724 atm->data = pop3_state;
726 buf = g_strdup_printf(_("%s: Retrieving new messages"),
727 pop3_state->ac_prefs->recv_server);
728 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
731 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
732 atm->state[i].handler = handlers[i];
733 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
734 for (i = POP3_GREETING_RECV + 1; i < N_POP3_PHASE; ) {
735 atm->state[i++].condition = GDK_INPUT_WRITE;
736 atm->state[i++].condition = GDK_INPUT_READ;
739 atm->terminate = (AtmHandler)pop3_automaton_terminate;
740 atm->ui_func = (AtmUIFunc)inc_progress_update;
742 atm->num = POP3_GREETING_RECV;
744 server = pop3_state->ac_prefs->recv_server;
746 port = pop3_state->ac_prefs->set_popport ?
747 pop3_state->ac_prefs->popport :
748 pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
750 port = pop3_state->ac_prefs->set_popport ?
751 pop3_state->ac_prefs->popport : 110;
754 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
755 log_message("%s\n", buf);
756 progress_dialog_set_label(inc_dialog->dialog, buf);
761 if ((sockinfo = sock_connect(server, port)) == NULL) {
762 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
764 if(!prefs_common.no_recv_err_panel) {
765 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
766 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
767 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
769 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
771 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
773 pop3_automaton_terminate(NULL, atm);
774 automaton_destroy(atm);
775 session->inc_state = INC_CONNECT_ERROR;
776 return INC_CONNECT_ERROR;
780 if (pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL &&
781 !ssl_init_socket(sockinfo)) {
782 pop3_automaton_terminate(sockinfo, atm);
783 automaton_destroy(atm);
784 session->inc_state = INC_CONNECT_ERROR;
785 return INC_CONNECT_ERROR;
789 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
790 * to the sock structure - implement a reference counter?? */
791 pop3_state->sockinfo = sockinfo;
792 atm->help_sock = sockinfo;
794 log_verbosity_set(TRUE);
795 /* oha: this messes up inc_progress update:
796 disabling this would avoid the label "Retrieve Header"
797 being overwritten by "Retrieve Message"
798 Setting inc_pop3_recv_func is not necessary
799 since atm already handles the progress dialog ui
802 recv_set_ui_func(inc_pop3_recv_func, session);
804 atm->tag = sock_gdk_input_add(sockinfo,
805 atm->state[atm->num].condition,
806 automaton_input_cb, atm);
808 while (!atm->terminated && !atm->cancelled)
809 gtk_main_iteration();
811 if (!atm->terminated)
812 pop3_automaton_terminate(sockinfo, atm);
814 log_verbosity_set(FALSE);
816 recv_set_ui_func(NULL, NULL);
818 automaton_destroy(atm);
820 if (session->inc_state != INC_SUCCESS)
821 return session->inc_state;
823 switch (pop3_state->error_val) {
825 session->inc_state = INC_SUCCESS;
828 session->inc_state = INC_AUTH_FAILED;
831 session->inc_state = INC_IO_ERROR;
834 session->inc_state = INC_SOCKET_ERROR;
837 session->inc_state = INC_LOCKED;
840 session->inc_state = INC_ERROR;
844 return session->inc_state;
847 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
849 if (atm->terminated) return 0;
852 gdk_input_remove(atm->tag);
855 if (atm->timeout_tag > 0) {
856 gtk_timeout_remove(atm->timeout_tag);
857 atm->timeout_tag = 0;
862 atm->terminated = TRUE;
867 static gboolean inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
870 gchar buf[MSGBUFSIZE];
871 IncSession *session = (IncSession *)data;
872 Pop3State *state = session->pop3_state;
873 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
874 ProgressDialog *dialog = inc_dialog->dialog;
878 cur_total = state->cur_total_bytes + read_bytes;
879 if (cur_total > state->total_bytes)
880 cur_total = state->total_bytes;
882 Xstrdup_a(total_size, to_human_readable(state->total_bytes),
884 g_snprintf(buf, sizeof(buf),
885 _("Retrieving message (%d / %d) (%s / %s)"),
886 state->cur_msg, state->count,
887 to_human_readable(cur_total), total_size);
888 progress_dialog_set_label(dialog, buf);
890 progress_dialog_set_percentage
891 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
892 if (inc_dialog->mainwin)
893 gtk_progress_bar_update
894 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
895 (gfloat)cur_total / (gfloat)state->total_bytes);
898 if (session->inc_state == INC_CANCEL)
904 void inc_progress_update(Pop3State *state, Pop3Phase phase)
906 gchar buf[MSGBUFSIZE];
907 IncSession *session = (IncSession *)state->data;
908 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
909 ProgressDialog *dialog = inc_dialog->dialog;
913 case POP3_GREETING_RECV:
915 case POP3_GETAUTH_USER_SEND:
916 case POP3_GETAUTH_PASS_SEND:
917 case POP3_GETAUTH_APOP_SEND:
918 progress_dialog_set_label(dialog, _("Authenticating..."));
920 case POP3_GETRANGE_STAT_SEND:
921 progress_dialog_set_label
922 (dialog, _("Getting the number of new messages (STAT)..."));
924 case POP3_GETRANGE_LAST_SEND:
925 progress_dialog_set_label
926 (dialog, _("Getting the number of new messages (LAST)..."));
928 case POP3_GETRANGE_UIDL_SEND:
929 progress_dialog_set_label
930 (dialog, _("Getting the number of new messages (UIDL)..."));
932 case POP3_GETSIZE_LIST_SEND:
933 progress_dialog_set_label
934 (dialog, _("Getting the size of messages (LIST)..."));
937 g_snprintf(buf, sizeof(buf),
938 _("Retrieving header (%d / %d)"),
939 state->cur_msg, state->count);
940 progress_dialog_set_label (dialog, buf);
941 progress_dialog_set_percentage
943 (gfloat)(state->cur_msg) /
944 (gfloat)(state->count));
945 if (inc_dialog->mainwin)
946 gtk_progress_bar_update
947 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
948 (gfloat)(state->cur_msg) /
949 (gfloat)(state->count));
952 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
953 g_snprintf(buf, sizeof(buf),
954 _("Retrieving message (%d / %d) (%s / %s)"),
955 state->cur_msg, state->count,
956 to_human_readable(state->cur_total_bytes),
958 progress_dialog_set_label(dialog, buf);
959 progress_dialog_set_percentage
961 (gfloat)(state->cur_total_bytes) /
962 (gfloat)(state->total_bytes));
963 if (inc_dialog->mainwin)
964 gtk_progress_bar_update
965 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
966 (gfloat)(state->cur_total_bytes) /
967 (gfloat)(state->total_bytes));
969 case POP3_DELETE_SEND:
970 if (state->msg[state->cur_msg].recv_time < state->current_time) {
971 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
973 progress_dialog_set_label(dialog, buf);
976 case POP3_LOGOUT_SEND:
977 progress_dialog_set_label(dialog, _("Quitting"));
984 gint inc_drop_message(const gchar *file, Pop3State *state)
987 FolderItem *dropfolder;
990 /* CLAWS: get default inbox (perhaps per account) */
991 if (state->ac_prefs->inbox) {
992 /* CLAWS: get destination folder / mailbox */
993 inbox = folder_find_item_from_identifier
994 (state->ac_prefs->inbox);
996 inbox = folder_get_default_inbox();
998 inbox = folder_get_default_inbox();
1004 /* CLAWS: claws uses a global .processing folder for the filtering. */
1005 dropfolder = folder_get_default_processing();
1007 /* add msg file to drop folder */
1008 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
1016 static void inc_put_error(IncState istate)
1020 if (!prefs_common.no_recv_err_panel)
1022 (_("Error occurred while processing mail."));
1025 alertpanel_error(_("No disk space left."));
1028 alertpanel_error(_("Can't write file."));
1030 case INC_SOCKET_ERROR:
1031 alertpanel_error(_("Socket error."));
1034 if (!prefs_common.no_recv_err_panel)
1035 alertpanel_error(_("Mailbox is locked."));
1042 static void inc_cancel(IncProgressDialog *dialog)
1044 IncSession *session;
1047 g_return_if_fail(dialog != NULL);
1049 if (dialog->queue_list == NULL) {
1050 inc_progress_dialog_destroy(dialog);
1054 session = dialog->queue_list->data;
1055 sockinfo = session->pop3_state->sockinfo;
1057 if (!sockinfo || session->atm->terminated == TRUE) return;
1059 session->pop3_state->cancelled = TRUE;
1060 session->inc_state = INC_CANCEL;
1061 session->atm->cancelled = TRUE;
1063 log_message(_("Incorporation cancelled\n"));
1066 gboolean inc_is_active(void)
1068 return (inc_dialog_list != NULL);
1071 void inc_cancel_all(void)
1075 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1076 inc_cancel((IncProgressDialog *)cur->data);
1079 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1081 inc_cancel((IncProgressDialog *)data);
1084 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1087 IncProgressDialog *dialog = (IncProgressDialog *)data;
1089 if (dialog->queue_list == NULL)
1090 inc_progress_dialog_destroy(dialog);
1095 static gint inc_spool(void)
1097 gchar *mbox, *logname;
1100 logname = g_get_user_name();
1101 mbox = g_strconcat(prefs_common.spool_path
1102 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1103 G_DIR_SEPARATOR_S, logname, NULL);
1104 msgs = get_spool(folder_get_default_inbox(), mbox);
1110 static void inc_spool_account(PrefsAccount *account)
1114 if (account->inbox) {
1115 inbox = folder_find_item_from_path(account->inbox);
1117 inbox = folder_get_default_inbox();
1119 inbox = folder_get_default_inbox();
1121 get_spool(inbox, account->local_mbox);
1124 static void inc_all_spool(void)
1128 list = account_get_list();
1131 for (; list != NULL; list = list->next) {
1132 PrefsAccount *account = list->data;
1134 if ((account->protocol == A_LOCAL) &&
1135 (account->recv_at_getall))
1136 inc_spool_account(account);
1140 static gint get_spool(FolderItem *dest, const gchar *mbox)
1144 gchar tmp_mbox[MAXPATHLEN + 1];
1146 g_return_val_if_fail(dest != NULL, -1);
1147 g_return_val_if_fail(mbox != NULL, -1);
1149 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1150 debug_print("no messages in local mailbox.\n");
1152 } else if (size < 0)
1155 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1158 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1159 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1161 if (copy_mbox(mbox, tmp_mbox) < 0) {
1162 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1166 debug_print("Getting new messages from %s into %s...\n",
1169 msgs = proc_mbox(dest, tmp_mbox);
1172 if (msgs >= 0) empty_mbox(mbox);
1173 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1175 if (!prefs_common.scan_all_after_inc) {
1176 folder_update_items_when_required
1177 (!prefs_common.open_inbox_on_inc);
1178 } else if (!prefs_common.scan_all_after_inc) {
1179 folder_update_item(dest, TRUE);
1190 void inc_unlock(void)
1192 if (inc_lock_count > 0)
1196 static guint autocheck_timer = 0;
1197 static gpointer autocheck_data = NULL;
1199 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1204 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1205 *prefs_common.newmail_notify_cmd))
1207 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1208 buf[1] == 'd' && !strchr(&buf[1], '%'))
1209 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1212 buf = g_strdup(prefs_common.newmail_notify_cmd);
1214 execute_command_line(buf, TRUE);
1219 void inc_autocheck_timer_init(MainWindow *mainwin)
1221 autocheck_data = mainwin;
1222 inc_autocheck_timer_set();
1225 static void inc_autocheck_timer_set_interval(guint interval)
1227 inc_autocheck_timer_remove();
1228 /* last test is to avoid re-enabling auto_check after modifying
1229 the common preferences */
1230 if (prefs_common.autochk_newmail && autocheck_data
1231 && prefs_common.work_offline == FALSE) {
1232 autocheck_timer = gtk_timeout_add
1233 (interval, inc_autocheck_func, autocheck_data);
1234 debug_print("added timer = %d\n", autocheck_timer);
1238 void inc_autocheck_timer_set(void)
1240 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1243 void inc_autocheck_timer_remove(void)
1245 if (autocheck_timer) {
1246 debug_print("removed timer = %d\n", autocheck_timer);
1247 gtk_timeout_remove(autocheck_timer);
1248 autocheck_timer = 0;
1252 static gint inc_autocheck_func(gpointer data)
1254 MainWindow *mainwin = (MainWindow *)data;
1256 if (inc_lock_count) {
1257 debug_print("autocheck is locked.\n");
1258 inc_autocheck_timer_set_interval(1000);
1262 inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);