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_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_path(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);
154 item = mainwin->summaryview->folder_item;
155 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
156 folderview_unselect(mainwin->folderview);
157 folderview_select(mainwin->folderview, item);
162 void inc_mail(MainWindow *mainwin, gboolean notify)
166 if (inc_lock_count) return;
168 inc_autocheck_timer_remove();
169 summary_write_cache(mainwin->summaryview);
170 main_window_lock(mainwin);
172 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
173 /* external incorporating program */
174 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
175 main_window_unlock(mainwin);
176 inc_autocheck_timer_set();
180 if (prefs_common.inc_local)
181 new_msgs = inc_spool();
183 if (prefs_common.inc_local)
184 new_msgs = inc_spool();
186 new_msgs += inc_account_mail(cur_account, mainwin);
189 inc_finished(mainwin, new_msgs > 0);
190 main_window_unlock(mainwin);
191 inc_notify_cmd(new_msgs, notify);
192 inc_autocheck_timer_set();
195 gint inc_selective_download(MainWindow *mainwin, gint session_type)
197 PrefsAccount *account = cur_account;
200 account->session_type = session_type;
201 new_msgs = inc_account_mail(account, mainwin);
202 account->session_type = RETR_NORMAL;
207 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
209 IncProgressDialog *inc_dialog;
213 if (account->protocol == A_IMAP4 || account->protocol == A_NNTP) {
214 folderview_check_new(FOLDER(account->folder));
218 session = inc_session_new(account);
219 if (!session) return 0;
221 inc_dialog = inc_progress_dialog_create();
222 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list, session);
223 inc_dialog->mainwin = mainwin;
224 session->data = inc_dialog;
227 text[1] = account->account_name;
228 text[2] = _("Standby");
229 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
231 main_window_set_toolbar_sensitive(mainwin);
232 main_window_set_menu_sensitive(mainwin);
234 return inc_start(inc_dialog);
237 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
239 GList *list, *queue_list = NULL;
240 IncProgressDialog *inc_dialog;
243 if (inc_lock_count) return;
245 inc_autocheck_timer_remove();
246 summary_write_cache(mainwin->summaryview);
247 main_window_lock(mainwin);
249 if (prefs_common.inc_local)
250 new_msgs = inc_spool();
252 list = account_get_list();
254 inc_finished(mainwin, new_msgs > 0);
255 main_window_unlock(mainwin);
256 inc_notify_cmd(new_msgs, notify);
257 inc_autocheck_timer_set();
261 /* check IMAP4 folders */
262 for (; list != NULL; list = list->next) {
263 PrefsAccount *account = list->data;
264 if ((account->protocol == A_IMAP4 ||
265 account->protocol == A_NNTP) && account->recv_at_getall)
266 folderview_check_new(FOLDER(account->folder));
269 /* check POP3 accounts */
270 for (list = account_get_list(); list != NULL; list = list->next) {
272 PrefsAccount *account = list->data;
273 account->session_type = RETR_NORMAL;
274 if (account->recv_at_getall) {
275 session = inc_session_new(account);
277 queue_list = g_list_append(queue_list, session);
282 inc_finished(mainwin, new_msgs > 0);
283 main_window_unlock(mainwin);
284 inc_notify_cmd(new_msgs, notify);
285 inc_autocheck_timer_set();
289 inc_dialog = inc_progress_dialog_create();
290 inc_dialog->queue_list = queue_list;
291 inc_dialog->mainwin = mainwin;
292 for (list = queue_list; list != NULL; list = list->next) {
293 IncSession *session = list->data;
296 session->data = inc_dialog;
299 text[1] = session->pop3_state->ac_prefs->account_name;
300 text[2] = _("Standby");
301 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
304 main_window_set_toolbar_sensitive(mainwin);
305 main_window_set_menu_sensitive(mainwin);
307 new_msgs += inc_start(inc_dialog);
309 inc_finished(mainwin, new_msgs > 0);
310 main_window_unlock(mainwin);
311 inc_notify_cmd(new_msgs, notify);
312 inc_autocheck_timer_set();
315 static IncProgressDialog *inc_progress_dialog_create(void)
317 IncProgressDialog *dialog;
318 ProgressDialog *progress;
320 dialog = g_new0(IncProgressDialog, 1);
322 progress = progress_dialog_create();
323 gtk_window_set_title(GTK_WINDOW(progress->window),
324 _("Retrieving new messages"));
325 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
326 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
327 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
328 GTK_SIGNAL_FUNC(gtk_true), NULL);
329 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
331 progress_dialog_set_value(progress, 0.0);
333 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
335 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
336 ¤txpm, ¤txpmmask);
337 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
338 &errorxpm, &errorxpmmask);
340 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
341 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
342 manage_window_get_focus_window())) {
343 dialog->show_dialog = TRUE;
344 gtk_widget_show_now(progress->window);
347 dialog->dialog = progress;
348 dialog->queue_list = NULL;
350 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
355 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
357 progress_dialog_set_value(inc_dialog->dialog, 0.0);
358 progress_dialog_set_label(inc_dialog->dialog, "");
359 gtk_progress_bar_update
360 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
363 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
365 g_return_if_fail(inc_dialog != NULL);
367 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
369 gtk_progress_bar_update
370 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
371 progress_dialog_destroy(inc_dialog->dialog);
376 static IncSession *inc_session_new(PrefsAccount *account)
380 g_return_val_if_fail(account != NULL, NULL);
382 if (account->protocol != A_POP3 && account->protocol != A_APOP)
384 if (!account->recv_server || !account->userid)
387 session = g_new0(IncSession, 1);
388 session->pop3_state = inc_pop3_state_new(account);
389 session->pop3_state->session = session;
394 static void inc_session_destroy(IncSession *session)
396 g_return_if_fail(session != NULL);
398 inc_pop3_state_destroy(session->pop3_state);
402 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
406 state = g_new0(Pop3State, 1);
408 state->ac_prefs = account;
409 state->folder_table = g_hash_table_new(NULL, NULL);
410 state->uidl_table = inc_get_uidl_table(account);
411 state->inc_state = INC_SUCCESS;
416 static void inc_pop3_state_destroy(Pop3State *state)
420 g_hash_table_destroy(state->folder_table);
422 for (n = 1; n <= state->count; n++)
423 g_free(state->msg[n].uidl);
426 if (state->uidl_table) {
427 hash_free_strings(state->uidl_table);
428 g_hash_table_destroy(state->uidl_table);
431 g_free(state->greeting);
434 g_free(state->prev_folder);
439 static gint inc_start(IncProgressDialog *inc_dialog)
442 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
443 Pop3State *pop3_state;
449 while (inc_dialog->queue_list != NULL) {
450 session = inc_dialog->queue_list->data;
451 pop3_state = session->pop3_state;
453 inc_progress_dialog_clear(inc_dialog);
455 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
457 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
458 if (pop3_state->ac_prefs->passwd)
460 g_strdup(pop3_state->ac_prefs->passwd);
461 else if (pop3_state->ac_prefs->tmp_pass)
463 g_strdup(pop3_state->ac_prefs->tmp_pass);
467 pass = input_dialog_query_password
468 (pop3_state->ac_prefs->recv_server,
471 if (inc_dialog->show_dialog)
472 manage_window_focus_in
473 (inc_dialog->mainwin->window,
476 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
477 pop3_state->pass = pass;
479 inc_session_destroy(session);
480 inc_dialog->queue_list = g_list_remove
481 (inc_dialog->queue_list, session);
486 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
487 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
489 /* begin POP3 session */
490 inc_state = inc_pop3_session_do(session);
492 if (inc_state == INC_SUCCESS) {
493 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
494 gtk_clist_set_text(clist, num, 2, _("Done"));
495 } else if (inc_state == INC_CANCEL) {
496 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
497 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
499 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
500 if (inc_state == INC_CONNECT_ERROR)
501 gtk_clist_set_text(clist, num, 2,
502 _("Connection failed"));
503 else if (inc_state == INC_AUTH_FAILED)
504 gtk_clist_set_text(clist, num, 2,
507 gtk_clist_set_text(clist, num, 2, _("Error"));
510 if (pop3_state->error_val == PS_AUTHFAIL) {
511 if(!prefs_common.noerrorpanel) {
512 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
513 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
514 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
517 (_("Authorization for %s on %s failed"),
519 pop3_state->ac_prefs->recv_server);
525 /* CLAWS: perform filtering actions on dropped message */
526 if (global_processing != NULL) {
527 FolderItem *processing, *inbox;
530 GSList *msglist, *msglist_element;
532 /* CLAWS: get default inbox (perhaps per account) */
533 if (pop3_state->ac_prefs->inbox) {
534 /* CLAWS: get destination folder / mailbox */
535 inbox = folder_find_item_from_identifier(pop3_state->ac_prefs->inbox);
537 inbox = folder_get_default_inbox();
539 inbox = folder_get_default_inbox();
541 /* get list of messages in processing */
542 processing = folder_get_default_processing();
543 folder_item_scan(processing);
544 folder = processing->folder;
545 msglist = folder->get_msg_list(folder, processing, FALSE);
547 /* process messages */
548 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
549 msginfo = (MsgInfo *) msglist_element->data;
550 /* filter if enabled in prefs or move to inbox if not */
551 if(pop3_state->ac_prefs->filter_on_recv) {
552 filter_message_by_msginfo_with_inbox(global_processing, msginfo,
553 pop3_state->folder_table,
556 folder_item_move_msg(inbox, msginfo);
557 g_hash_table_insert(pop3_state->folder_table, inbox,
560 procmsg_msginfo_free(msginfo);
562 g_slist_free(msglist);
566 new_msgs += pop3_state->cur_total_num;
568 if (!prefs_common.scan_all_after_inc) {
569 folder_item_scan_foreach(pop3_state->folder_table);
570 folderview_update_item_foreach
571 (pop3_state->folder_table);
574 if (pop3_state->error_val == PS_AUTHFAIL &&
575 pop3_state->ac_prefs->tmp_pass) {
576 g_free(pop3_state->ac_prefs->tmp_pass);
577 pop3_state->ac_prefs->tmp_pass = NULL;
580 inc_write_uidl_list(pop3_state);
582 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
584 if (inc_state == INC_NOSPACE) {
585 inc_put_error(inc_state);
590 inc_session_destroy(session);
591 inc_dialog->queue_list =
592 g_list_remove(inc_dialog->queue_list, session);
597 if (error_num && !prefs_common.noerrorpanel) {
598 if (inc_dialog->show_dialog)
599 manage_window_focus_in(inc_dialog->dialog->window,
601 alertpanel_error(_("Some errors occured while getting mail."));
602 if (inc_dialog->show_dialog)
603 manage_window_focus_out(inc_dialog->dialog->window,
607 while (inc_dialog->queue_list != NULL) {
608 session = inc_dialog->queue_list->data;
609 inc_session_destroy(session);
610 inc_dialog->queue_list =
611 g_list_remove(inc_dialog->queue_list, session);
614 inc_progress_dialog_destroy(inc_dialog);
619 static IncState inc_pop3_session_do(IncSession *session)
621 Pop3State *pop3_state = session->pop3_state;
622 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
629 static AtmHandler handlers[] = {
631 pop3_getauth_user_send , pop3_getauth_user_recv,
632 pop3_getauth_pass_send , pop3_getauth_pass_recv,
633 pop3_getauth_apop_send , pop3_getauth_apop_recv,
634 pop3_getrange_stat_send , pop3_getrange_stat_recv,
635 pop3_getrange_last_send , pop3_getrange_last_recv,
636 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
637 pop3_getsize_list_send , pop3_getsize_list_recv,
638 pop3_top_send , pop3_top_recv,
639 pop3_retr_send , pop3_retr_recv,
640 pop3_delete_send , pop3_delete_recv,
641 pop3_logout_send , pop3_logout_recv
644 debug_print(_("getting new messages of account %s...\n"),
645 pop3_state->ac_prefs->account_name);
647 atm = automaton_create(N_POP3_PHASE);
650 atm->data = pop3_state;
652 buf = g_strdup_printf(_("%s: Retrieving new messages"),
653 pop3_state->ac_prefs->recv_server);
654 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
657 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
658 atm->state[i].handler = handlers[i];
659 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
660 for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
661 atm->state[i++].condition = GDK_INPUT_WRITE;
662 atm->state[i++].condition = GDK_INPUT_READ;
665 atm->terminate = (AtmHandler)pop3_automaton_terminate;
667 atm->num = POP3_GREETING_RECV;
669 server = pop3_state->ac_prefs->recv_server;
671 port = pop3_state->ac_prefs->set_popport ?
672 pop3_state->ac_prefs->popport :
673 pop3_state->ac_prefs->ssl_pop ? 995 : 110;
675 port = pop3_state->ac_prefs->set_popport ?
676 pop3_state->ac_prefs->popport : 110;
679 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
680 log_message("%s\n", buf);
681 progress_dialog_set_label(inc_dialog->dialog, buf);
686 if ((sockinfo = sock_connect(server, port)) == NULL) {
687 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
689 if(!prefs_common.noerrorpanel) {
690 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
691 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
692 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
694 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
696 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
698 pop3_automaton_terminate(NULL, atm);
699 automaton_destroy(atm);
700 return INC_CONNECT_ERROR;
704 if (pop3_state->ac_prefs->ssl_pop && !ssl_init_socket(sockinfo)) {
705 pop3_automaton_terminate(NULL, atm);
706 automaton_destroy(atm);
707 return INC_CONNECT_ERROR;
711 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
712 * to the sock structure - implement a reference counter?? */
713 pop3_state->sockinfo = sockinfo;
714 atm->help_sock = sockinfo;
716 log_verbosity_set(TRUE);
717 recv_set_ui_func(inc_pop3_recv_func, session);
719 atm->tag = sock_gdk_input_add(sockinfo,
720 atm->state[atm->num].condition,
721 automaton_input_cb, atm);
723 while (!atm->terminated)
724 gtk_main_iteration();
726 log_verbosity_set(FALSE);
727 recv_set_ui_func(NULL, NULL);
729 automaton_destroy(atm);
731 return pop3_state->inc_state;
734 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
736 if (atm->terminated) return 0;
739 gdk_input_remove(atm->tag);
742 if (atm->timeout_tag > 0) {
743 gtk_timeout_remove(atm->timeout_tag);
744 atm->timeout_tag = 0;
749 atm->terminated = TRUE;
754 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
759 gchar buf[IDLEN + 3];
761 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
762 "uidl-", ac_prefs->recv_server,
763 "-", ac_prefs->userid, NULL);
764 if ((fp = fopen(path, "r")) == NULL) {
765 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
771 table = g_hash_table_new(g_str_hash, g_str_equal);
773 while (fgets(buf, sizeof(buf), fp) != NULL) {
775 g_hash_table_insert(table, g_strdup(buf), GINT_TO_POINTER(1));
783 static void inc_write_uidl_list(Pop3State *state)
789 if (!state->uidl_is_valid) return;
791 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
792 "uidl-", state->ac_prefs->recv_server,
793 "-", state->user, NULL);
794 if ((fp = fopen(path, "w")) == NULL) {
795 FILE_OP_ERROR(path, "fopen");
800 for (n = 1; n <= state->count; n++) {
801 if (state->msg[n].uidl && state->msg[n].received &&
802 !state->msg[n].deleted) {
803 if (fputs(state->msg[n].uidl, fp) == EOF) {
804 FILE_OP_ERROR(path, "fputs");
807 if (fputc('\n', fp) == EOF) {
808 FILE_OP_ERROR(path, "fputc");
814 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
818 static gboolean inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
821 gchar buf[MSGBUFSIZE];
822 IncSession *session = (IncSession *)data;
823 Pop3State *state = session->pop3_state;
824 IncProgressDialog *inc_dialog = session->data;
825 ProgressDialog *dialog = inc_dialog->dialog;
829 cur_total = state->cur_total_bytes + read_bytes;
830 if (cur_total > state->total_bytes)
831 cur_total = state->total_bytes;
833 Xstrdup_a(total_size, to_human_readable(state->total_bytes),
835 g_snprintf(buf, sizeof(buf),
836 _("Retrieving message (%d / %d) (%s / %s)"),
837 state->cur_msg, state->count,
838 to_human_readable(cur_total), total_size);
839 progress_dialog_set_label(dialog, buf);
841 progress_dialog_set_percentage
842 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
843 gtk_progress_bar_update
844 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
845 (gfloat)cur_total / (gfloat)state->total_bytes);
848 if (state->inc_state == INC_CANCEL)
854 void inc_progress_update(Pop3State *state, Pop3Phase phase)
856 gchar buf[MSGBUFSIZE];
857 IncProgressDialog *inc_dialog = state->session->data;
858 ProgressDialog *dialog = inc_dialog->dialog;
862 case POP3_GREETING_RECV:
864 case POP3_GETAUTH_USER_SEND:
865 case POP3_GETAUTH_USER_RECV:
866 case POP3_GETAUTH_PASS_SEND:
867 case POP3_GETAUTH_PASS_RECV:
868 case POP3_GETAUTH_APOP_SEND:
869 case POP3_GETAUTH_APOP_RECV:
870 progress_dialog_set_label(dialog, _("Authenticating..."));
872 case POP3_GETRANGE_STAT_SEND:
873 case POP3_GETRANGE_STAT_RECV:
874 progress_dialog_set_label
875 (dialog, _("Getting the number of new messages (STAT)..."));
877 case POP3_GETRANGE_LAST_SEND:
878 case POP3_GETRANGE_LAST_RECV:
879 progress_dialog_set_label
880 (dialog, _("Getting the number of new messages (LAST)..."));
882 case POP3_GETRANGE_UIDL_SEND:
883 case POP3_GETRANGE_UIDL_RECV:
884 progress_dialog_set_label
885 (dialog, _("Getting the number of new messages (UIDL)..."));
887 case POP3_GETSIZE_LIST_SEND:
888 case POP3_GETSIZE_LIST_RECV:
889 progress_dialog_set_label
890 (dialog, _("Getting the size of messages (LIST)..."));
894 g_snprintf(buf, sizeof(buf),
895 _("Retrieving header (%d / %d)"),
896 state->cur_msg, state->count);
897 progress_dialog_set_label (dialog, buf);
898 progress_dialog_set_percentage
900 (gfloat)(state->cur_msg) /
901 (gfloat)(state->count));
902 gtk_progress_bar_update
903 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
904 (gfloat)(state->cur_msg) /
905 (gfloat)(state->count));
909 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
910 g_snprintf(buf, sizeof(buf),
911 _("Retrieving message (%d / %d) (%s / %s)"),
912 state->cur_msg, state->count,
913 to_human_readable(state->cur_total_bytes),
915 progress_dialog_set_label(dialog, buf);
916 progress_dialog_set_percentage
918 (gfloat)(state->cur_total_bytes) /
919 (gfloat)(state->total_bytes));
920 gtk_progress_bar_update
921 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
922 (gfloat)(state->cur_total_bytes) /
923 (gfloat)(state->total_bytes));
925 case POP3_DELETE_SEND:
926 case POP3_DELETE_RECV:
927 progress_dialog_set_label(dialog, _("Deleting message"));
929 case POP3_LOGOUT_SEND:
930 case POP3_LOGOUT_RECV:
931 progress_dialog_set_label(dialog, _("Quitting"));
938 gint inc_drop_message(const gchar *file, Pop3State *state)
941 FolderItem *dropfolder;
945 /* CLAWS: get default inbox (perhaps per account) */
946 if (state->ac_prefs->inbox) {
947 /* CLAWS: get destination folder / mailbox */
948 inbox = folder_find_item_from_identifier(state->ac_prefs->inbox);
950 inbox = folder_get_default_inbox();
952 inbox = folder_get_default_inbox();
958 /* CLAWS: claws uses a global .processing folder for the filtering. */
959 if (global_processing == NULL) {
960 if (state->ac_prefs->filter_on_recv) {
962 filter_get_dest_folder(prefs_common.fltlist, file);
963 if (!dropfolder) dropfolder = inbox;
964 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
965 g_warning(_("a message won't be received\n"));
971 dropfolder = folder_get_default_processing();
974 val = GPOINTER_TO_INT(g_hash_table_lookup
975 (state->folder_table, dropfolder));
977 folder_item_scan(dropfolder);
978 g_hash_table_insert(state->folder_table, dropfolder,
982 /* add msg file to drop folder */
983 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
991 static void inc_put_error(IncState istate)
995 if(!prefs_common.noerrorpanel) {
996 alertpanel_error(_("Error occurred while processing mail."));
1000 alertpanel_error(_("No disk space left."));
1007 static void inc_cancel(IncProgressDialog *dialog)
1009 IncSession *session;
1012 g_return_if_fail(dialog != NULL);
1014 session = dialog->queue_list->data;
1015 sockinfo = session->pop3_state->sockinfo;
1017 if (!sockinfo || session->atm->terminated == TRUE) return;
1019 session->pop3_state->inc_state = INC_CANCEL;
1020 pop3_automaton_terminate(sockinfo, session->atm);
1021 session->pop3_state->sockinfo = NULL;
1024 gboolean inc_is_active(void)
1026 return (inc_dialog_list != NULL);
1029 void inc_cancel_all(void)
1033 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1034 inc_cancel((IncProgressDialog *)cur->data);
1037 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1039 inc_cancel((IncProgressDialog *)data);
1042 static gint inc_spool(void)
1044 gchar *mbox, *logname;
1047 logname = g_get_user_name();
1048 mbox = g_strconcat(prefs_common.spool_path
1049 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1050 G_DIR_SEPARATOR_S, logname, NULL);
1051 msgs = get_spool(folder_get_default_inbox(), mbox);
1057 static void inc_spool_account(PrefsAccount *account)
1060 FolderItem *dropfolder;
1063 if (account->inbox) {
1064 inbox = folder_find_item_from_path(account->inbox);
1066 inbox = folder_get_default_inbox();
1068 inbox = folder_get_default_inbox();
1070 get_spool(inbox, account->local_mbox);
1073 static void inc_all_spool(void)
1077 list = account_get_list();
1080 for (; list != NULL; list = list->next) {
1081 IncSession *session;
1082 PrefsAccount *account = list->data;
1084 if (account->protocol == A_LOCAL)
1085 inc_spool_account(account);
1089 static gint get_spool(FolderItem *dest, const gchar *mbox)
1093 gchar tmp_mbox[MAXPATHLEN + 1];
1094 GHashTable *folder_table = NULL;
1096 g_return_val_if_fail(dest != NULL, -1);
1097 g_return_val_if_fail(mbox != NULL, -1);
1099 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1100 debug_print(_("no messages in local mailbox.\n"));
1102 } else if (size < 0)
1105 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1108 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
1109 get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
1111 if (copy_mbox(mbox, tmp_mbox) < 0) {
1112 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1116 debug_print(_("Getting new messages from %s into %s...\n"),
1119 if (prefs_common.filter_on_inc)
1120 folder_table = g_hash_table_new(NULL, NULL);
1121 msgs = proc_mbox(dest, tmp_mbox, folder_table);
1124 if (msgs >= 0) empty_mbox(mbox);
1125 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1128 if (!prefs_common.scan_all_after_inc) {
1129 g_hash_table_insert(folder_table, dest,
1130 GINT_TO_POINTER(1));
1131 folder_item_scan_foreach(folder_table);
1132 folderview_update_item_foreach(folder_table);
1134 g_hash_table_destroy(folder_table);
1135 } else if (!prefs_common.scan_all_after_inc) {
1136 folder_item_scan(dest);
1137 folderview_update_item(dest, FALSE);
1148 void inc_unlock(void)
1150 if (inc_lock_count > 0)
1154 static guint autocheck_timer = 0;
1155 static gpointer autocheck_data = NULL;
1157 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1162 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1163 *prefs_common.newmail_notify_cmd))
1165 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1166 buf[1] == 'd' && !strchr(&buf[1], '%'))
1167 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1170 buf = g_strdup(prefs_common.newmail_notify_cmd);
1177 void inc_autocheck_timer_init(MainWindow *mainwin)
1179 autocheck_data = mainwin;
1180 inc_autocheck_timer_set();
1183 static void inc_autocheck_timer_set_interval(guint interval)
1185 inc_autocheck_timer_remove();
1187 if (prefs_common.autochk_newmail && autocheck_data) {
1188 autocheck_timer = gtk_timeout_add
1189 (interval, inc_autocheck_func, autocheck_data);
1190 debug_print("added timer = %d\n", autocheck_timer);
1194 void inc_autocheck_timer_set(void)
1196 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1199 void inc_autocheck_timer_remove(void)
1201 if (autocheck_timer) {
1202 debug_print("removed timer = %d\n", autocheck_timer);
1203 gtk_timeout_remove(autocheck_timer);
1204 autocheck_timer = 0;
1208 static gint inc_autocheck_func(gpointer data)
1210 MainWindow *mainwin = (MainWindow *)data;
1212 if (inc_lock_count) {
1213 debug_print("autocheck is locked.\n");
1214 inc_autocheck_timer_set_interval(1000);
1218 inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);