2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 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"
68 static guint inc_lock_count = 0;
70 static GdkPixmap *currentxpm;
71 static GdkBitmap *currentxpmmask;
72 static GdkPixmap *errorxpm;
73 static GdkBitmap *errorxpmmask;
74 static GdkPixmap *okxpm;
75 static GdkBitmap *okxpmmask;
77 #define MSGBUFSIZE 8192
79 static void inc_finished (MainWindow *mainwin,
80 gboolean new_messages);
81 static gint inc_account_mail (PrefsAccount *account,
84 static IncProgressDialog *inc_progress_dialog_create (void);
85 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
87 static IncSession *inc_session_new (PrefsAccount *account);
88 static void inc_session_destroy (IncSession *session);
89 static Pop3State *inc_pop3_state_new (PrefsAccount *account);
90 static void inc_pop3_state_destroy (Pop3State *state);
91 static gint inc_start (IncProgressDialog *inc_dialog);
92 static IncState inc_pop3_session_do (IncSession *session);
93 static gint pop3_automaton_terminate (SockInfo *source,
96 static GHashTable *inc_get_uidl_table (PrefsAccount *ac_prefs);
97 static void inc_write_uidl_list (Pop3State *state);
100 static gint connection_check_cb (Automaton *atm);
103 static void inc_pop3_recv_func (SockInfo *sock,
108 static void inc_put_error (IncState istate);
110 static void inc_cancel (GtkWidget *widget,
113 static gint inc_spool (void);
114 static gint get_spool (FolderItem *dest,
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);
123 * @mainwin: Main window.
124 * @new_messages: TRUE if some messages have been received.
126 * Update the folder view and the summary view after receiving
127 * messages. If @new_messages is FALSE, this function avoids unneeded
130 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
134 if (prefs_common.scan_all_after_inc)
135 folderview_update_all_node();
137 if (!new_messages && !prefs_common.scan_all_after_inc) return;
139 if (prefs_common.open_inbox_on_inc) {
140 item = cur_account && cur_account->inbox
141 ? folder_find_item_from_path(cur_account->inbox)
142 : folder_get_default_inbox();
143 folderview_unselect(mainwin->folderview);
144 folderview_select(mainwin->folderview, item);
146 item = mainwin->summaryview->folder_item;
147 folderview_unselect(mainwin->folderview);
148 folderview_select(mainwin->folderview, item);
152 void inc_mail(MainWindow *mainwin)
156 if (inc_lock_count) return;
158 inc_autocheck_timer_remove();
159 summary_write_cache(mainwin->summaryview);
160 main_window_lock(mainwin);
162 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
163 /* external incorporating program */
164 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
165 main_window_unlock(mainwin);
166 inc_autocheck_timer_set();
170 if (prefs_common.inc_local)
171 new_msgs = inc_spool();
173 if (prefs_common.inc_local)
174 new_msgs = inc_spool();
176 new_msgs += inc_account_mail(cur_account, mainwin);
179 inc_finished(mainwin, new_msgs > 0);
180 main_window_unlock(mainwin);
181 inc_autocheck_timer_set();
184 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
186 IncProgressDialog *inc_dialog;
190 session = inc_session_new(account);
191 if (!session) return 0;
193 inc_dialog = inc_progress_dialog_create();
194 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list, session);
195 inc_dialog->mainwin = mainwin;
196 session->data = inc_dialog;
199 text[1] = account->account_name;
200 text[2] = _("Standby");
201 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
203 return inc_start(inc_dialog);
206 void inc_all_account_mail(MainWindow *mainwin)
208 GList *list, *queue_list = NULL;
209 IncProgressDialog *inc_dialog;
212 if (inc_lock_count) return;
214 inc_autocheck_timer_remove();
215 summary_write_cache(mainwin->summaryview);
216 main_window_lock(mainwin);
218 if (prefs_common.inc_local)
219 new_msgs = inc_spool();
221 list = account_get_list();
223 inc_finished(mainwin, new_msgs > 0);
224 main_window_unlock(mainwin);
225 inc_autocheck_timer_set();
229 for (; list != NULL; list = list->next) {
231 PrefsAccount *account = list->data;
233 if (account->recv_at_getall) {
234 session = inc_session_new(account);
236 queue_list = g_list_append(queue_list, session);
241 inc_finished(mainwin, new_msgs > 0);
242 main_window_unlock(mainwin);
243 inc_autocheck_timer_set();
247 inc_dialog = inc_progress_dialog_create();
248 inc_dialog->queue_list = queue_list;
249 inc_dialog->mainwin = mainwin;
250 for (list = queue_list; list != NULL; list = list->next) {
251 IncSession *session = list->data;
254 session->data = inc_dialog;
257 text[1] = session->pop3_state->ac_prefs->account_name;
258 text[2] = _("Standby");
259 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
262 new_msgs += inc_start(inc_dialog);
264 inc_finished(mainwin, new_msgs > 0);
265 main_window_unlock(mainwin);
266 inc_autocheck_timer_set();
269 static IncProgressDialog *inc_progress_dialog_create(void)
271 IncProgressDialog *dialog;
272 ProgressDialog *progress;
274 dialog = g_new0(IncProgressDialog, 1);
276 progress = progress_dialog_create();
277 gtk_window_set_title(GTK_WINDOW(progress->window),
278 _("Retrieving new messages"));
279 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
280 GTK_SIGNAL_FUNC(inc_cancel), dialog);
281 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
282 GTK_SIGNAL_FUNC(gtk_true), NULL);
283 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
285 progress_dialog_set_value(progress, 0.0);
287 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
289 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
290 ¤txpm, ¤txpmmask);
291 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
292 &errorxpm, &errorxpmmask);
294 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
295 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
296 manage_window_get_focus_window())) {
297 dialog->show_dialog = TRUE;
298 gtk_widget_show_now(progress->window);
301 dialog->dialog = progress;
302 dialog->queue_list = NULL;
307 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
309 progress_dialog_set_value(inc_dialog->dialog, 0.0);
310 progress_dialog_set_label(inc_dialog->dialog, "");
311 gtk_progress_bar_update
312 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
315 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
317 g_return_if_fail(inc_dialog != NULL);
319 gtk_progress_bar_update
320 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
321 progress_dialog_destroy(inc_dialog->dialog);
326 static IncSession *inc_session_new(PrefsAccount *account)
330 g_return_val_if_fail(account != NULL, NULL);
332 if (account->protocol != A_POP3 && account->protocol != A_APOP)
334 if (!account->recv_server || !account->userid)
337 session = g_new0(IncSession, 1);
338 session->pop3_state = inc_pop3_state_new(account);
339 session->pop3_state->session = session;
344 static void inc_session_destroy(IncSession *session)
346 g_return_if_fail(session != NULL);
348 inc_pop3_state_destroy(session->pop3_state);
352 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
356 state = g_new0(Pop3State, 1);
358 state->ac_prefs = account;
359 state->folder_table = g_hash_table_new(NULL, NULL);
360 state->id_table = inc_get_uidl_table(account);
361 state->id_list = NULL;
362 state->new_id_list = NULL;
363 state->inc_state = INC_SUCCESS;
368 static void inc_pop3_state_destroy(Pop3State *state)
370 g_hash_table_destroy(state->folder_table);
371 g_free(state->sizes);
373 if (state->id_table) {
374 hash_free_strings(state->id_table);
375 g_hash_table_destroy(state->id_table);
377 slist_free_strings(state->id_list);
378 slist_free_strings(state->new_id_list);
379 g_slist_free(state->id_list);
380 g_slist_free(state->new_id_list);
382 g_free(state->greeting);
385 g_free(state->prev_folder);
390 static gint inc_start(IncProgressDialog *inc_dialog)
393 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
394 Pop3State *pop3_state;
400 while (inc_dialog->queue_list != NULL) {
401 session = inc_dialog->queue_list->data;
402 pop3_state = session->pop3_state;
404 inc_progress_dialog_clear(inc_dialog);
406 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
408 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
409 if (pop3_state->ac_prefs->passwd)
411 g_strdup(pop3_state->ac_prefs->passwd);
412 else if (pop3_state->ac_prefs->tmp_pass)
414 g_strdup(pop3_state->ac_prefs->tmp_pass);
419 message = g_strdup_printf
420 (_("Input password for %s on %s:"),
422 pop3_state->ac_prefs->recv_server);
424 pass = input_dialog_with_invisible(_("Input password"),
427 if (inc_dialog->show_dialog)
428 manage_window_focus_in
429 (inc_dialog->mainwin->window,
432 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
433 pop3_state->pass = pass;
435 inc_session_destroy(session);
436 inc_dialog->queue_list = g_list_remove
437 (inc_dialog->queue_list, session);
442 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
443 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
445 /* begin POP3 session */
446 inc_state = inc_pop3_session_do(session);
448 if (inc_state == INC_SUCCESS) {
449 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
450 gtk_clist_set_text(clist, num, 2, _("Done"));
451 } else if (inc_state == INC_CANCEL) {
452 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
453 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
455 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
456 if (inc_state == INC_CONNECT_ERROR)
457 gtk_clist_set_text(clist, num, 2,
458 _("Connection failed"));
459 else if (inc_state == INC_AUTH_FAILED)
460 gtk_clist_set_text(clist, num, 2,
463 gtk_clist_set_text(clist, num, 2, _("Error"));
466 if (pop3_state->error_val == PS_AUTHFAIL) {
467 if(!prefs_common.noerrorpanel) {
468 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
469 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
470 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
473 (_("Authorization for %s on %s failed"),
475 pop3_state->ac_prefs->recv_server);
481 new_msgs += pop3_state->cur_total_num;
483 if (!prefs_common.scan_all_after_inc) {
484 folder_item_scan_foreach(pop3_state->folder_table);
485 folderview_update_item_foreach
486 (pop3_state->folder_table);
489 if (pop3_state->error_val == PS_AUTHFAIL &&
490 pop3_state->ac_prefs->tmp_pass) {
491 g_free(pop3_state->ac_prefs->tmp_pass);
492 pop3_state->ac_prefs->tmp_pass = NULL;
495 inc_write_uidl_list(pop3_state);
497 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
499 if (inc_state == INC_NOSPACE) {
500 inc_put_error(inc_state);
505 inc_session_destroy(session);
506 inc_dialog->queue_list =
507 g_list_remove(inc_dialog->queue_list, session);
512 if (error_num && !prefs_common.noerrorpanel) {
513 if (inc_dialog->show_dialog)
514 manage_window_focus_in(inc_dialog->dialog->window,
516 alertpanel_error(_("Some errors occured while getting mail."));
517 if (inc_dialog->show_dialog)
518 manage_window_focus_out(inc_dialog->dialog->window,
522 while (inc_dialog->queue_list != NULL) {
523 session = inc_dialog->queue_list->data;
524 inc_session_destroy(session);
525 inc_dialog->queue_list =
526 g_list_remove(inc_dialog->queue_list, session);
529 inc_progress_dialog_destroy(inc_dialog);
534 static IncState inc_pop3_session_do(IncSession *session)
536 Pop3State *pop3_state = session->pop3_state;
537 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
544 static AtmHandler handlers[] = {
546 pop3_getauth_user_send , pop3_getauth_user_recv,
547 pop3_getauth_pass_send , pop3_getauth_pass_recv,
548 pop3_getauth_apop_send , pop3_getauth_apop_recv,
549 pop3_getrange_stat_send , pop3_getrange_stat_recv,
550 pop3_getrange_last_send , pop3_getrange_last_recv,
551 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
552 pop3_getsize_list_send , pop3_getsize_list_recv,
553 pop3_retr_send , pop3_retr_recv,
554 pop3_delete_send , pop3_delete_recv,
555 pop3_logout_send , pop3_logout_recv
558 debug_print(_("getting new messages of account %s...\n"),
559 pop3_state->ac_prefs->account_name);
561 atm = automaton_create(N_POP3_PHASE);
564 atm->data = pop3_state;
566 buf = g_strdup_printf(_("%s: Retrieving new messages"),
567 pop3_state->ac_prefs->recv_server);
568 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
571 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
572 atm->state[i].handler = handlers[i];
573 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
574 for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
575 atm->state[i++].condition = GDK_INPUT_WRITE;
576 atm->state[i++].condition = GDK_INPUT_READ;
579 atm->terminate = (AtmHandler)pop3_automaton_terminate;
581 atm->num = POP3_GREETING_RECV;
583 server = pop3_state->ac_prefs->recv_server;
585 port = pop3_state->ac_prefs->set_popport ?
586 pop3_state->ac_prefs->popport :
587 pop3_state->ac_prefs->ssl_pop ? 995 : 110;
589 port = pop3_state->ac_prefs->set_popport ?
590 pop3_state->ac_prefs->popport : 110;
593 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
594 log_message("%s\n", buf);
595 progress_dialog_set_label(inc_dialog->dialog, buf);
601 if ((sockinfo = sock_connect_with_thread(server, port)) == NULL) {
603 if ((sockinfo = sock_connect(server, port)) == NULL) {
605 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
607 if(!prefs_common.noerrorpanel) {
608 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
609 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
610 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
612 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
614 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
616 pop3_automaton_terminate(NULL, atm);
617 automaton_destroy(atm);
618 return INC_CONNECT_ERROR;
622 if (pop3_state->ac_prefs->ssl_pop && !ssl_init_socket(sockinfo)) {
623 pop3_automaton_terminate(NULL, atm);
624 automaton_destroy(atm);
625 return INC_CONNECT_ERROR;
629 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
630 * to the sock structure - implement a reference counter?? */
631 pop3_state->sockinfo = sockinfo;
632 atm->help_sock = sockinfo;
634 log_verbosity_set(TRUE);
635 recv_set_ui_func(inc_pop3_recv_func, session);
638 atm->timeout_tag = gtk_timeout_add
639 (TIMEOUT_ITV, (GtkFunction)connection_check_cb, atm);
641 atm->tag = sock_gdk_input_add(sockinfo,
642 atm->state[atm->num].condition,
643 automaton_input_cb, atm);
646 while (!atm->terminated)
647 gtk_main_iteration();
649 log_verbosity_set(FALSE);
650 recv_set_ui_func(NULL, NULL);
653 /* pthread_join(sockinfo->connect_thr, NULL); */
655 automaton_destroy(atm);
657 return pop3_state->inc_state;
660 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
662 if (atm->terminated) return 0;
665 gdk_input_remove(atm->tag);
668 if (atm->timeout_tag > 0) {
669 gtk_timeout_remove(atm->timeout_tag);
670 atm->timeout_tag = 0;
675 atm->terminated = TRUE;
680 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
685 gchar buf[IDLEN + 3];
687 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
688 "uidl-", ac_prefs->recv_server,
689 "-", ac_prefs->userid, NULL);
690 if ((fp = fopen(path, "r")) == NULL) {
691 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
697 table = g_hash_table_new(g_str_hash, g_str_equal);
699 while (fgets(buf, sizeof(buf), fp) != NULL) {
701 g_hash_table_insert(table, g_strdup(buf), GINT_TO_POINTER(1));
709 static void inc_write_uidl_list(Pop3State *state)
715 if (!state->id_list) return;
717 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
718 "uidl-", state->ac_prefs->recv_server,
719 "-", state->user, NULL);
720 if ((fp = fopen(path, "w")) == NULL) {
721 FILE_OP_ERROR(path, "fopen");
726 for (cur = state->id_list; cur != NULL; cur = cur->next) {
727 if (fputs((gchar *)cur->data, fp) == EOF) {
728 FILE_OP_ERROR(path, "fputs");
731 if (fputc('\n', fp) == EOF) {
732 FILE_OP_ERROR(path, "fputc");
737 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
742 static gint connection_check_cb(Automaton *atm)
744 Pop3State *state = atm->data;
745 IncProgressDialog *inc_dialog = state->session->data;
746 SockInfo *sockinfo = state->sockinfo;
748 /* g_print("connection check\n"); */
750 if (sockinfo->state == CONN_LOOKUPFAILED ||
751 sockinfo->state == CONN_FAILED) {
752 atm->timeout_tag = 0;
753 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
754 sockinfo->hostname, sockinfo->port);
755 if(!prefs_common.noerrorpanel) {
756 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
757 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
758 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
760 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
761 sockinfo->hostname, sockinfo->port);
762 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
764 pop3_automaton_terminate(sockinfo, atm);
765 state->sockinfo = NULL;
767 } else if (sockinfo->state == CONN_ESTABLISHED) {
768 atm->timeout_tag = 0;
769 atm->tag = sock_gdk_input_add(sockinfo,
770 atm->state[atm->num].condition,
771 automaton_input_cb, atm);
779 static void inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
782 gchar buf[MSGBUFSIZE];
783 IncSession *session = (IncSession *)data;
784 Pop3State *state = session->pop3_state;
785 IncProgressDialog *inc_dialog = session->data;
786 ProgressDialog *dialog = inc_dialog->dialog;
790 cur_total = state->cur_total_bytes + read_bytes;
791 if (cur_total > state->total_bytes)
792 cur_total = state->total_bytes;
794 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
795 g_snprintf(buf, sizeof(buf),
796 _("Retrieving message (%d / %d) (%s / %s)"),
797 state->cur_msg, state->count,
798 to_human_readable(cur_total), total_size);
799 progress_dialog_set_label(dialog, buf);
801 progress_dialog_set_percentage
802 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
803 gtk_progress_bar_update
804 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
805 (gfloat)cur_total / (gfloat)state->total_bytes);
809 void inc_progress_update(Pop3State *state, Pop3Phase phase)
811 gchar buf[MSGBUFSIZE];
812 IncProgressDialog *inc_dialog = state->session->data;
813 ProgressDialog *dialog = inc_dialog->dialog;
817 case POP3_GREETING_RECV:
819 case POP3_GETAUTH_USER_SEND:
820 case POP3_GETAUTH_USER_RECV:
821 case POP3_GETAUTH_PASS_SEND:
822 case POP3_GETAUTH_PASS_RECV:
823 case POP3_GETAUTH_APOP_SEND:
824 case POP3_GETAUTH_APOP_RECV:
825 progress_dialog_set_label(dialog, _("Authenticating..."));
827 case POP3_GETRANGE_STAT_SEND:
828 case POP3_GETRANGE_STAT_RECV:
829 progress_dialog_set_label
830 (dialog, _("Getting the number of new messages (STAT)..."));
832 case POP3_GETRANGE_LAST_SEND:
833 case POP3_GETRANGE_LAST_RECV:
834 progress_dialog_set_label
835 (dialog, _("Getting the number of new messages (LAST)..."));
837 case POP3_GETRANGE_UIDL_SEND:
838 case POP3_GETRANGE_UIDL_RECV:
839 progress_dialog_set_label
840 (dialog, _("Getting the number of new messages (UIDL)..."));
842 case POP3_GETSIZE_LIST_SEND:
843 case POP3_GETSIZE_LIST_RECV:
844 progress_dialog_set_label
845 (dialog, _("Getting the size of messages (LIST)..."));
849 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
850 g_snprintf(buf, sizeof(buf),
851 _("Retrieving message (%d / %d) (%s / %s)"),
852 state->cur_msg, state->count,
853 to_human_readable(state->cur_total_bytes),
855 progress_dialog_set_label(dialog, buf);
856 progress_dialog_set_percentage
858 (gfloat)(state->cur_total_bytes) /
859 (gfloat)(state->total_bytes));
860 gtk_progress_bar_update
861 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
862 (gfloat)(state->cur_total_bytes) /
863 (gfloat)(state->total_bytes));
865 case POP3_DELETE_SEND:
866 case POP3_DELETE_RECV:
867 progress_dialog_set_label(dialog, _("Deleting message"));
869 case POP3_LOGOUT_SEND:
870 case POP3_LOGOUT_RECV:
871 progress_dialog_set_label(dialog, _("Quitting"));
878 gint inc_drop_message(const gchar *file, Pop3State *state)
881 FolderItem *dropfolder;
885 /* get default inbox (perhaps per account) */
886 if (state->ac_prefs->inbox) {
887 inbox = folder_find_item_from_path(state->ac_prefs->inbox);
889 inbox = folder_get_default_inbox();
891 inbox = folder_get_default_inbox();
897 if (global_processing == NULL) {
899 if (state->ac_prefs->filter_on_recv) {
901 filter_get_dest_folder(prefs_common.fltlist, file);
902 if (!dropfolder) dropfolder = inbox;
903 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
904 g_warning(_("a message won't be received\n"));
910 /* CLAWS: new filtering */
911 dropfolder = folder_get_default_processing();
914 val = GPOINTER_TO_INT(g_hash_table_lookup
915 (state->folder_table, dropfolder));
917 folder_item_scan(dropfolder);
918 g_hash_table_insert(state->folder_table, dropfolder,
922 /* add msg file to drop folder */
923 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
928 /* CLAWS: perform filtering actions on dropped message */
929 if (global_processing != NULL) {
930 if (state->ac_prefs->filter_on_recv)
931 filter_message(global_processing, inbox, msgnum,
932 state->folder_table);
938 static void inc_put_error(IncState istate)
942 if(!prefs_common.noerrorpanel) {
943 alertpanel_error(_("Error occurred while processing mail."));
947 alertpanel_error(_("No disk space left."));
954 static void inc_cancel(GtkWidget *widget, gpointer data)
956 IncProgressDialog *dialog = data;
957 IncSession *session = dialog->queue_list->data;
958 SockInfo *sockinfo = session->pop3_state->sockinfo;
960 if (!sockinfo || session->atm->terminated == TRUE) return;
963 if (sockinfo->state == CONN_READY ||
964 sockinfo->state == CONN_LOOKUPSUCCESS) {
965 pthread_cancel(sockinfo->connect_thr);
966 /* pthread_kill(sockinfo->connect_thr, SIGINT); */
967 g_print("connection was cancelled.\n");
971 session->pop3_state->inc_state = INC_CANCEL;
972 pop3_automaton_terminate(sockinfo, session->atm);
973 session->pop3_state->sockinfo = NULL;
976 static gint inc_spool(void)
978 gchar *mbox, *logname;
981 logname = g_get_user_name();
982 mbox = g_strconcat(prefs_common.spool_path
983 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
984 G_DIR_SEPARATOR_S, logname, NULL);
985 msgs = get_spool(folder_get_default_inbox(), mbox);
991 static void inc_spool_account(PrefsAccount *account)
994 FolderItem *dropfolder;
997 if (account->inbox) {
998 inbox = folder_find_item_from_path(account->inbox);
1000 inbox = folder_get_default_inbox();
1002 inbox = folder_get_default_inbox();
1004 get_spool(inbox, account->local_mbox);
1007 static void inc_all_spool(void)
1011 list = account_get_list();
1014 for (; list != NULL; list = list->next) {
1015 IncSession *session;
1016 PrefsAccount *account = list->data;
1018 if (account->protocol == A_LOCAL)
1019 inc_spool_account(account);
1023 static gint get_spool(FolderItem *dest, const gchar *mbox)
1027 gchar tmp_mbox[MAXPATHLEN + 1];
1028 GHashTable *folder_table = NULL;
1030 g_return_val_if_fail(dest != NULL, -1);
1031 g_return_val_if_fail(mbox != NULL, -1);
1033 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1034 debug_print(_("no messages in local mailbox.\n"));
1036 } else if (size < 0)
1039 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1042 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
1043 get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
1045 if (copy_mbox(mbox, tmp_mbox) < 0)
1048 debug_print(_("Getting new messages from %s into %s...\n"),
1051 if (prefs_common.filter_on_inc)
1052 folder_table = g_hash_table_new(NULL, NULL);
1053 msgs = proc_mbox(dest, tmp_mbox, folder_table);
1056 if (msgs >= 0) empty_mbox(mbox);
1057 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1060 if (!prefs_common.scan_all_after_inc) {
1061 g_hash_table_insert(folder_table, dest,
1062 GINT_TO_POINTER(1));
1063 folder_item_scan_foreach(folder_table);
1064 folderview_update_item_foreach(folder_table);
1066 g_hash_table_destroy(folder_table);
1067 } else if (!prefs_common.scan_all_after_inc) {
1068 folder_item_scan(dest);
1069 folderview_update_item(dest, FALSE);
1080 void inc_unlock(void)
1082 if (inc_lock_count > 0)
1086 static guint autocheck_timer = 0;
1087 static gpointer autocheck_data = NULL;
1089 void inc_autocheck_timer_init(MainWindow *mainwin)
1091 autocheck_data = mainwin;
1092 inc_autocheck_timer_set();
1095 static void inc_autocheck_timer_set_interval(guint interval)
1097 inc_autocheck_timer_remove();
1099 if (prefs_common.autochk_newmail && autocheck_data) {
1100 autocheck_timer = gtk_timeout_add
1101 (interval, inc_autocheck_func, autocheck_data);
1102 debug_print("added timer = %d\n", autocheck_timer);
1106 void inc_autocheck_timer_set(void)
1108 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1111 void inc_autocheck_timer_remove(void)
1113 if (autocheck_timer) {
1114 debug_print("removed timer = %d\n", autocheck_timer);
1115 gtk_timeout_remove(autocheck_timer);
1116 autocheck_timer = 0;
1120 static gint inc_autocheck_func(gpointer data)
1122 MainWindow *mainwin = (MainWindow *)data;
1124 if (inc_lock_count) {
1125 debug_print("autocheck is locked.\n");
1126 inc_autocheck_timer_set_interval(1000);
1130 inc_all_account_mail(mainwin);