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>
34 #include <sys/types.h>
43 #include "mainwindow.h"
44 #include "folderview.h"
45 #include "summaryview.h"
46 #include "prefs_common.h"
47 #include "prefs_account.h"
57 #include "statusbar.h"
58 #include "manage_window.h"
59 #include "stock_pixmap.h"
60 #include "progressdialog.h"
61 #include "inputdialog.h"
62 #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 Pop3State *inc_pop3_state_new (PrefsAccount *account);
93 static void inc_pop3_state_destroy (Pop3State *state);
94 static gint inc_start (IncProgressDialog *inc_dialog);
95 static IncState inc_pop3_session_do (IncSession *session);
96 static gint pop3_automaton_terminate (SockInfo *source,
99 static GHashTable *inc_get_uidl_table (PrefsAccount *ac_prefs);
100 static void inc_write_uidl_list (Pop3State *state);
102 static gboolean inc_pop3_recv_func (SockInfo *sock,
107 static void inc_put_error (IncState istate);
109 static void inc_cancel_cb (GtkWidget *widget,
112 static gint inc_spool (void);
113 static gint get_spool (FolderItem *dest,
116 static void inc_spool_account(PrefsAccount *account);
117 static void inc_all_spool(void);
118 static void inc_autocheck_timer_set_interval (guint interval);
119 static gint inc_autocheck_func (gpointer data);
121 static void inc_notify_cmd (gint new_msgs,
124 #define FOLDER_SUMMARY_MISMATCH(f, s) \
125 (f) && (s) ? ((s)->newmsgs != (f)->new) || ((f)->unread != (s)->unread) || ((f)->total != (s)->messages) \
130 * @mainwin: Main window.
131 * @new_messages: TRUE if some messages have been received.
133 * Update the folder view and the summary view after receiving
134 * messages. If @new_messages is FALSE, this function avoids unneeded
137 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
141 if (prefs_common.scan_all_after_inc)
142 folderview_check_new(NULL);
144 if (!new_messages && !prefs_common.scan_all_after_inc) return;
146 if (prefs_common.open_inbox_on_inc) {
147 item = cur_account && cur_account->inbox
148 ? folder_find_item_from_path(cur_account->inbox)
149 : folder_get_default_inbox();
150 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
151 folderview_unselect(mainwin->folderview);
152 folderview_select(mainwin->folderview, item);
155 item = mainwin->summaryview->folder_item;
156 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
157 folderview_unselect(mainwin->folderview);
158 folderview_select(mainwin->folderview, item);
163 void inc_mail(MainWindow *mainwin, gboolean notify)
167 if (inc_lock_count) return;
169 inc_autocheck_timer_remove();
170 summary_write_cache(mainwin->summaryview);
171 main_window_lock(mainwin);
173 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
174 /* external incorporating program */
175 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
176 main_window_unlock(mainwin);
177 inc_autocheck_timer_set();
181 if (prefs_common.inc_local)
182 new_msgs = inc_spool();
184 if (prefs_common.inc_local)
185 new_msgs = inc_spool();
187 new_msgs += inc_account_mail(cur_account, mainwin);
190 inc_finished(mainwin, new_msgs > 0);
191 main_window_unlock(mainwin);
192 inc_notify_cmd(new_msgs, notify);
193 inc_autocheck_timer_set();
196 gint inc_selective_download(MainWindow *mainwin, gint session_type)
198 PrefsAccount *account = cur_account;
201 account->session_type = session_type;
202 new_msgs = inc_account_mail(account, mainwin);
203 account->session_type = RETR_NORMAL;
208 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
210 IncProgressDialog *inc_dialog;
214 switch (account->protocol) {
217 folderview_check_new(FOLDER(account->folder));
222 session = inc_session_new(account);
223 if (!session) return 0;
225 inc_dialog = inc_progress_dialog_create();
226 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
228 inc_dialog->mainwin = mainwin;
229 session->data = inc_dialog;
232 text[1] = account->account_name;
233 text[2] = _("Standby");
234 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
236 main_window_set_toolbar_sensitive(mainwin);
237 main_window_set_menu_sensitive(mainwin);
239 return inc_start(inc_dialog);
242 inc_spool_account(account);
247 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
249 GList *list, *queue_list = NULL;
250 IncProgressDialog *inc_dialog;
253 if (inc_lock_count) return;
255 inc_autocheck_timer_remove();
256 summary_write_cache(mainwin->summaryview);
257 main_window_lock(mainwin);
259 if (prefs_common.inc_local)
260 new_msgs = inc_spool();
262 list = account_get_list();
264 inc_finished(mainwin, new_msgs > 0);
265 main_window_unlock(mainwin);
266 inc_notify_cmd(new_msgs, notify);
267 inc_autocheck_timer_set();
271 /* check local folders */
274 /* check IMAP4 folders */
275 for (; list != NULL; list = list->next) {
276 PrefsAccount *account = list->data;
277 if ((account->protocol == A_IMAP4 ||
278 account->protocol == A_NNTP) && account->recv_at_getall)
279 folderview_check_new(FOLDER(account->folder));
282 /* check POP3 accounts */
283 for (list = account_get_list(); list != NULL; list = list->next) {
285 PrefsAccount *account = list->data;
286 account->session_type = RETR_NORMAL;
287 if (account->recv_at_getall) {
288 session = inc_session_new(account);
290 queue_list = g_list_append(queue_list, session);
295 inc_finished(mainwin, new_msgs > 0);
296 main_window_unlock(mainwin);
297 inc_notify_cmd(new_msgs, notify);
298 inc_autocheck_timer_set();
302 inc_dialog = inc_progress_dialog_create();
303 inc_dialog->queue_list = queue_list;
304 inc_dialog->mainwin = mainwin;
305 for (list = queue_list; list != NULL; list = list->next) {
306 IncSession *session = list->data;
309 session->data = inc_dialog;
312 text[1] = session->pop3_state->ac_prefs->account_name;
313 text[2] = _("Standby");
314 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
317 main_window_set_toolbar_sensitive(mainwin);
318 main_window_set_menu_sensitive(mainwin);
320 new_msgs += inc_start(inc_dialog);
322 inc_finished(mainwin, new_msgs > 0);
323 main_window_unlock(mainwin);
324 inc_notify_cmd(new_msgs, notify);
325 inc_autocheck_timer_set();
328 static IncProgressDialog *inc_progress_dialog_create(void)
330 IncProgressDialog *dialog;
331 ProgressDialog *progress;
333 dialog = g_new0(IncProgressDialog, 1);
335 progress = progress_dialog_create();
336 gtk_window_set_title(GTK_WINDOW(progress->window),
337 _("Retrieving new messages"));
338 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
339 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
340 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
341 GTK_SIGNAL_FUNC(gtk_true), NULL);
342 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
344 progress_dialog_set_value(progress, 0.0);
346 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
348 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
349 ¤txpm, ¤txpmmask);
350 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
351 &errorxpm, &errorxpmmask);
353 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
354 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
355 manage_window_get_focus_window())) {
356 dialog->show_dialog = TRUE;
357 gtk_widget_show_now(progress->window);
360 dialog->dialog = progress;
361 dialog->queue_list = NULL;
363 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
368 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
370 progress_dialog_set_value(inc_dialog->dialog, 0.0);
371 progress_dialog_set_label(inc_dialog->dialog, "");
372 gtk_progress_bar_update
373 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
376 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
378 g_return_if_fail(inc_dialog != NULL);
380 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
382 gtk_progress_bar_update
383 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
384 progress_dialog_destroy(inc_dialog->dialog);
389 static IncSession *inc_session_new(PrefsAccount *account)
393 g_return_val_if_fail(account != NULL, NULL);
395 if (account->protocol != A_POP3 && account->protocol != A_APOP)
397 if (!account->recv_server || !account->userid)
400 session = g_new0(IncSession, 1);
401 session->pop3_state = inc_pop3_state_new(account);
402 session->pop3_state->session = session;
407 static void inc_session_destroy(IncSession *session)
409 g_return_if_fail(session != NULL);
411 inc_pop3_state_destroy(session->pop3_state);
415 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
419 state = g_new0(Pop3State, 1);
421 state->ac_prefs = account;
422 state->folder_table = g_hash_table_new(NULL, NULL);
423 state->uidl_table = inc_get_uidl_table(account);
424 state->inc_state = INC_SUCCESS;
429 static void inc_pop3_state_destroy(Pop3State *state)
433 g_hash_table_destroy(state->folder_table);
435 for (n = 1; n <= state->count; n++)
436 g_free(state->msg[n].uidl);
439 if (state->uidl_table) {
440 hash_free_strings(state->uidl_table);
441 g_hash_table_destroy(state->uidl_table);
444 g_free(state->greeting);
447 g_free(state->prev_folder);
452 static gint inc_start(IncProgressDialog *inc_dialog)
455 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
456 Pop3State *pop3_state;
462 while (inc_dialog->queue_list != NULL) {
463 session = inc_dialog->queue_list->data;
464 pop3_state = session->pop3_state;
466 inc_progress_dialog_clear(inc_dialog);
468 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
470 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
471 if (pop3_state->ac_prefs->passwd)
473 g_strdup(pop3_state->ac_prefs->passwd);
474 else if (pop3_state->ac_prefs->tmp_pass)
476 g_strdup(pop3_state->ac_prefs->tmp_pass);
480 pass = input_dialog_query_password
481 (pop3_state->ac_prefs->recv_server,
484 if (inc_dialog->show_dialog)
485 manage_window_focus_in
486 (inc_dialog->mainwin->window,
489 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
490 pop3_state->pass = pass;
492 inc_session_destroy(session);
493 inc_dialog->queue_list = g_list_remove
494 (inc_dialog->queue_list, session);
499 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
500 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
502 /* begin POP3 session */
503 inc_state = inc_pop3_session_do(session);
505 if (inc_state == INC_SUCCESS) {
506 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
507 gtk_clist_set_text(clist, num, 2, _("Done"));
508 } else if (inc_state == INC_CANCEL) {
509 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
510 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
512 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
513 if (inc_state == INC_CONNECT_ERROR)
514 gtk_clist_set_text(clist, num, 2,
515 _("Connection failed"));
516 else if (inc_state == INC_AUTH_FAILED)
517 gtk_clist_set_text(clist, num, 2,
520 gtk_clist_set_text(clist, num, 2, _("Error"));
523 if (pop3_state->error_val == PS_AUTHFAIL) {
524 if(!prefs_common.noerrorpanel) {
525 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
526 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
527 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
530 (_("Authorization for %s on %s failed"),
532 pop3_state->ac_prefs->recv_server);
538 /* CLAWS: perform filtering actions on dropped message */
539 if (global_processing != NULL) {
540 FolderItem *processing, *inbox;
543 GSList *msglist, *msglist_element;
545 /* CLAWS: get default inbox (perhaps per account) */
546 if (pop3_state->ac_prefs->inbox) {
547 /* CLAWS: get destination folder / mailbox */
548 inbox = folder_find_item_from_identifier(pop3_state->ac_prefs->inbox);
550 inbox = folder_get_default_inbox();
552 inbox = folder_get_default_inbox();
554 /* get list of messages in processing */
555 processing = folder_get_default_processing();
556 folder_item_scan(processing);
557 folder = processing->folder;
558 msglist = folder->get_msg_list(folder, processing, FALSE);
560 /* process messages */
561 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
562 msginfo = (MsgInfo *) msglist_element->data;
563 /* filter if enabled in prefs or move to inbox if not */
564 if(pop3_state->ac_prefs->filter_on_recv) {
565 filter_message_by_msginfo_with_inbox(global_processing, msginfo,
566 pop3_state->folder_table,
569 folder_item_move_msg(inbox, msginfo);
570 g_hash_table_insert(pop3_state->folder_table, inbox,
573 procmsg_msginfo_free(msginfo);
575 g_slist_free(msglist);
579 new_msgs += pop3_state->cur_total_num;
581 if (!prefs_common.scan_all_after_inc) {
582 folder_item_scan_foreach(pop3_state->folder_table);
583 folderview_update_item_foreach
584 (pop3_state->folder_table);
587 if (pop3_state->error_val == PS_AUTHFAIL &&
588 pop3_state->ac_prefs->tmp_pass) {
589 g_free(pop3_state->ac_prefs->tmp_pass);
590 pop3_state->ac_prefs->tmp_pass = NULL;
593 inc_write_uidl_list(pop3_state);
595 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
597 if (inc_state == INC_NOSPACE) {
598 inc_put_error(inc_state);
603 inc_session_destroy(session);
604 inc_dialog->queue_list =
605 g_list_remove(inc_dialog->queue_list, session);
610 if (error_num && !prefs_common.noerrorpanel) {
611 if (inc_dialog->show_dialog)
612 manage_window_focus_in(inc_dialog->dialog->window,
614 alertpanel_error(_("Some errors occured while getting mail."));
615 if (inc_dialog->show_dialog)
616 manage_window_focus_out(inc_dialog->dialog->window,
620 while (inc_dialog->queue_list != NULL) {
621 session = inc_dialog->queue_list->data;
622 inc_session_destroy(session);
623 inc_dialog->queue_list =
624 g_list_remove(inc_dialog->queue_list, session);
627 inc_progress_dialog_destroy(inc_dialog);
632 static IncState inc_pop3_session_do(IncSession *session)
634 Pop3State *pop3_state = session->pop3_state;
635 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
642 static AtmHandler handlers[] = {
644 pop3_getauth_user_send , pop3_getauth_user_recv,
645 pop3_getauth_pass_send , pop3_getauth_pass_recv,
646 pop3_getauth_apop_send , pop3_getauth_apop_recv,
647 pop3_getrange_stat_send , pop3_getrange_stat_recv,
648 pop3_getrange_last_send , pop3_getrange_last_recv,
649 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
650 pop3_getsize_list_send , pop3_getsize_list_recv,
651 pop3_top_send , pop3_top_recv,
652 pop3_retr_send , pop3_retr_recv,
653 pop3_delete_send , pop3_delete_recv,
654 pop3_logout_send , pop3_logout_recv
657 debug_print(_("getting new messages of account %s...\n"),
658 pop3_state->ac_prefs->account_name);
660 atm = automaton_create(N_POP3_PHASE);
663 atm->data = pop3_state;
665 buf = g_strdup_printf(_("%s: Retrieving new messages"),
666 pop3_state->ac_prefs->recv_server);
667 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
670 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
671 atm->state[i].handler = handlers[i];
672 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
673 for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
674 atm->state[i++].condition = GDK_INPUT_WRITE;
675 atm->state[i++].condition = GDK_INPUT_READ;
678 atm->terminate = (AtmHandler)pop3_automaton_terminate;
680 atm->num = POP3_GREETING_RECV;
682 server = pop3_state->ac_prefs->recv_server;
684 port = pop3_state->ac_prefs->set_popport ?
685 pop3_state->ac_prefs->popport :
686 pop3_state->ac_prefs->ssl_pop ? 995 : 110;
688 port = pop3_state->ac_prefs->set_popport ?
689 pop3_state->ac_prefs->popport : 110;
692 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
693 log_message("%s\n", buf);
694 progress_dialog_set_label(inc_dialog->dialog, buf);
699 if ((sockinfo = sock_connect(server, port)) == NULL) {
700 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
702 if(!prefs_common.noerrorpanel) {
703 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
704 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
705 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
707 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
709 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
711 pop3_automaton_terminate(NULL, atm);
712 automaton_destroy(atm);
713 return INC_CONNECT_ERROR;
717 if (pop3_state->ac_prefs->ssl_pop && !ssl_init_socket(sockinfo)) {
718 pop3_automaton_terminate(NULL, atm);
719 automaton_destroy(atm);
720 return INC_CONNECT_ERROR;
724 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
725 * to the sock structure - implement a reference counter?? */
726 pop3_state->sockinfo = sockinfo;
727 atm->help_sock = sockinfo;
729 log_verbosity_set(TRUE);
730 recv_set_ui_func(inc_pop3_recv_func, session);
732 atm->tag = sock_gdk_input_add(sockinfo,
733 atm->state[atm->num].condition,
734 automaton_input_cb, atm);
736 while (!atm->terminated)
737 gtk_main_iteration();
739 log_verbosity_set(FALSE);
740 recv_set_ui_func(NULL, NULL);
742 automaton_destroy(atm);
744 return pop3_state->inc_state;
747 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
749 if (atm->terminated) return 0;
752 gdk_input_remove(atm->tag);
755 if (atm->timeout_tag > 0) {
756 gtk_timeout_remove(atm->timeout_tag);
757 atm->timeout_tag = 0;
762 atm->terminated = TRUE;
767 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
772 gchar buf[IDLEN + 3];
774 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
775 "uidl-", ac_prefs->recv_server,
776 "-", ac_prefs->userid, NULL);
777 if ((fp = fopen(path, "r")) == NULL) {
778 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
784 table = g_hash_table_new(g_str_hash, g_str_equal);
786 while (fgets(buf, sizeof(buf), fp) != NULL) {
788 g_hash_table_insert(table, g_strdup(buf), GINT_TO_POINTER(1));
796 static void inc_write_uidl_list(Pop3State *state)
802 if (!state->uidl_is_valid) return;
804 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
805 "uidl-", state->ac_prefs->recv_server,
806 "-", state->user, NULL);
807 if ((fp = fopen(path, "w")) == NULL) {
808 FILE_OP_ERROR(path, "fopen");
813 for (n = 1; n <= state->count; n++) {
814 if (state->msg[n].uidl && state->msg[n].received &&
815 !state->msg[n].deleted) {
816 if (fputs(state->msg[n].uidl, fp) == EOF) {
817 FILE_OP_ERROR(path, "fputs");
820 if (fputc('\n', fp) == EOF) {
821 FILE_OP_ERROR(path, "fputc");
827 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
831 static gboolean inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
834 gchar buf[MSGBUFSIZE];
835 IncSession *session = (IncSession *)data;
836 Pop3State *state = session->pop3_state;
837 IncProgressDialog *inc_dialog = session->data;
838 ProgressDialog *dialog = inc_dialog->dialog;
842 cur_total = state->cur_total_bytes + read_bytes;
843 if (cur_total > state->total_bytes)
844 cur_total = state->total_bytes;
846 Xstrdup_a(total_size, to_human_readable(state->total_bytes),
848 g_snprintf(buf, sizeof(buf),
849 _("Retrieving message (%d / %d) (%s / %s)"),
850 state->cur_msg, state->count,
851 to_human_readable(cur_total), total_size);
852 progress_dialog_set_label(dialog, buf);
854 progress_dialog_set_percentage
855 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
856 gtk_progress_bar_update
857 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
858 (gfloat)cur_total / (gfloat)state->total_bytes);
861 if (state->inc_state == INC_CANCEL)
867 void inc_progress_update(Pop3State *state, Pop3Phase phase)
869 gchar buf[MSGBUFSIZE];
870 IncProgressDialog *inc_dialog = state->session->data;
871 ProgressDialog *dialog = inc_dialog->dialog;
875 case POP3_GREETING_RECV:
877 case POP3_GETAUTH_USER_SEND:
878 case POP3_GETAUTH_USER_RECV:
879 case POP3_GETAUTH_PASS_SEND:
880 case POP3_GETAUTH_PASS_RECV:
881 case POP3_GETAUTH_APOP_SEND:
882 case POP3_GETAUTH_APOP_RECV:
883 progress_dialog_set_label(dialog, _("Authenticating..."));
885 case POP3_GETRANGE_STAT_SEND:
886 case POP3_GETRANGE_STAT_RECV:
887 progress_dialog_set_label
888 (dialog, _("Getting the number of new messages (STAT)..."));
890 case POP3_GETRANGE_LAST_SEND:
891 case POP3_GETRANGE_LAST_RECV:
892 progress_dialog_set_label
893 (dialog, _("Getting the number of new messages (LAST)..."));
895 case POP3_GETRANGE_UIDL_SEND:
896 case POP3_GETRANGE_UIDL_RECV:
897 progress_dialog_set_label
898 (dialog, _("Getting the number of new messages (UIDL)..."));
900 case POP3_GETSIZE_LIST_SEND:
901 case POP3_GETSIZE_LIST_RECV:
902 progress_dialog_set_label
903 (dialog, _("Getting the size of messages (LIST)..."));
907 g_snprintf(buf, sizeof(buf),
908 _("Retrieving header (%d / %d)"),
909 state->cur_msg, state->count);
910 progress_dialog_set_label (dialog, buf);
911 progress_dialog_set_percentage
913 (gfloat)(state->cur_msg) /
914 (gfloat)(state->count));
915 gtk_progress_bar_update
916 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
917 (gfloat)(state->cur_msg) /
918 (gfloat)(state->count));
922 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
923 g_snprintf(buf, sizeof(buf),
924 _("Retrieving message (%d / %d) (%s / %s)"),
925 state->cur_msg, state->count,
926 to_human_readable(state->cur_total_bytes),
928 progress_dialog_set_label(dialog, buf);
929 progress_dialog_set_percentage
931 (gfloat)(state->cur_total_bytes) /
932 (gfloat)(state->total_bytes));
933 gtk_progress_bar_update
934 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
935 (gfloat)(state->cur_total_bytes) /
936 (gfloat)(state->total_bytes));
938 case POP3_DELETE_SEND:
939 case POP3_DELETE_RECV:
940 progress_dialog_set_label(dialog, _("Deleting message"));
942 case POP3_LOGOUT_SEND:
943 case POP3_LOGOUT_RECV:
944 progress_dialog_set_label(dialog, _("Quitting"));
951 gint inc_drop_message(const gchar *file, Pop3State *state)
954 FolderItem *dropfolder;
958 /* CLAWS: get default inbox (perhaps per account) */
959 if (state->ac_prefs->inbox) {
960 /* CLAWS: get destination folder / mailbox */
961 inbox = folder_find_item_from_identifier(state->ac_prefs->inbox);
963 inbox = folder_get_default_inbox();
965 inbox = folder_get_default_inbox();
971 /* CLAWS: claws uses a global .processing folder for the filtering. */
972 if (global_processing == NULL) {
973 if (state->ac_prefs->filter_on_recv) {
975 filter_get_dest_folder(prefs_common.fltlist, file);
976 if (!dropfolder) dropfolder = inbox;
977 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
978 g_warning(_("a message won't be received\n"));
984 dropfolder = folder_get_default_processing();
987 val = GPOINTER_TO_INT(g_hash_table_lookup
988 (state->folder_table, dropfolder));
990 folder_item_scan(dropfolder);
991 g_hash_table_insert(state->folder_table, dropfolder,
995 /* add msg file to drop folder */
996 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
1004 static void inc_put_error(IncState istate)
1008 if(!prefs_common.noerrorpanel) {
1009 alertpanel_error(_("Error occurred while processing mail."));
1013 alertpanel_error(_("No disk space left."));
1020 static void inc_cancel(IncProgressDialog *dialog)
1022 IncSession *session;
1025 g_return_if_fail(dialog != NULL);
1027 session = dialog->queue_list->data;
1028 sockinfo = session->pop3_state->sockinfo;
1030 if (!sockinfo || session->atm->terminated == TRUE) return;
1032 session->pop3_state->inc_state = INC_CANCEL;
1033 pop3_automaton_terminate(sockinfo, session->atm);
1034 session->pop3_state->sockinfo = NULL;
1037 gboolean inc_is_active(void)
1039 return (inc_dialog_list != NULL);
1042 void inc_cancel_all(void)
1046 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1047 inc_cancel((IncProgressDialog *)cur->data);
1050 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1052 inc_cancel((IncProgressDialog *)data);
1055 static gint inc_spool(void)
1057 gchar *mbox, *logname;
1060 logname = g_get_user_name();
1061 mbox = g_strconcat(prefs_common.spool_path
1062 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1063 G_DIR_SEPARATOR_S, logname, NULL);
1064 msgs = get_spool(folder_get_default_inbox(), mbox);
1070 static void inc_spool_account(PrefsAccount *account)
1073 FolderItem *dropfolder;
1076 if (account->inbox) {
1077 inbox = folder_find_item_from_path(account->inbox);
1079 inbox = folder_get_default_inbox();
1081 inbox = folder_get_default_inbox();
1083 get_spool(inbox, account->local_mbox);
1086 static void inc_all_spool(void)
1090 list = account_get_list();
1093 for (; list != NULL; list = list->next) {
1094 IncSession *session;
1095 PrefsAccount *account = list->data;
1097 if ((account->protocol == A_LOCAL) &&
1098 (account->recv_at_getall))
1099 inc_spool_account(account);
1103 static gint get_spool(FolderItem *dest, const gchar *mbox)
1107 gchar tmp_mbox[MAXPATHLEN + 1];
1108 GHashTable *folder_table = NULL;
1110 g_return_val_if_fail(dest != NULL, -1);
1111 g_return_val_if_fail(mbox != NULL, -1);
1113 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1114 debug_print(_("no messages in local mailbox.\n"));
1116 } else if (size < 0)
1119 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1122 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
1123 get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
1125 if (copy_mbox(mbox, tmp_mbox) < 0) {
1126 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1130 debug_print(_("Getting new messages from %s into %s...\n"),
1133 if (prefs_common.filter_on_inc)
1134 folder_table = g_hash_table_new(NULL, NULL);
1135 msgs = proc_mbox(dest, tmp_mbox, folder_table);
1138 if (msgs >= 0) empty_mbox(mbox);
1139 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1142 if (!prefs_common.scan_all_after_inc) {
1143 g_hash_table_insert(folder_table, dest,
1144 GINT_TO_POINTER(1));
1145 folder_item_scan_foreach(folder_table);
1146 folderview_update_item_foreach(folder_table);
1148 g_hash_table_destroy(folder_table);
1149 } else if (!prefs_common.scan_all_after_inc) {
1150 folder_item_scan(dest);
1151 folderview_update_item(dest, FALSE);
1162 void inc_unlock(void)
1164 if (inc_lock_count > 0)
1168 static guint autocheck_timer = 0;
1169 static gpointer autocheck_data = NULL;
1171 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1176 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1177 *prefs_common.newmail_notify_cmd))
1179 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1180 buf[1] == 'd' && !strchr(&buf[1], '%'))
1181 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1184 buf = g_strdup(prefs_common.newmail_notify_cmd);
1191 void inc_autocheck_timer_init(MainWindow *mainwin)
1193 autocheck_data = mainwin;
1194 inc_autocheck_timer_set();
1197 static void inc_autocheck_timer_set_interval(guint interval)
1199 inc_autocheck_timer_remove();
1201 if (prefs_common.autochk_newmail && autocheck_data) {
1202 autocheck_timer = gtk_timeout_add
1203 (interval, inc_autocheck_func, autocheck_data);
1204 debug_print("added timer = %d\n", autocheck_timer);
1208 void inc_autocheck_timer_set(void)
1210 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1213 void inc_autocheck_timer_remove(void)
1215 if (autocheck_timer) {
1216 debug_print("removed timer = %d\n", autocheck_timer);
1217 gtk_timeout_remove(autocheck_timer);
1218 autocheck_timer = 0;
1222 static gint inc_autocheck_func(gpointer data)
1224 MainWindow *mainwin = (MainWindow *)data;
1226 if (inc_lock_count) {
1227 debug_print("autocheck is locked.\n");
1228 inc_autocheck_timer_set_interval(1000);
1232 inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);