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 "progressdialog.h"
60 #include "inputdialog.h"
61 #include "alertpanel.h"
63 #include "automaton.h"
65 #include "filtering.h"
67 #include "pixmaps/continue.xpm"
68 #include "pixmaps/complete.xpm"
69 #include "pixmaps/error.xpm"
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);
103 static gint connection_check_cb (Automaton *atm);
106 static void inc_pop3_recv_func (SockInfo *sock,
111 static void inc_put_error (IncState istate);
113 static void inc_cancel (GtkWidget *widget,
116 static gint inc_spool (void);
117 static gint get_spool (FolderItem *dest,
120 static void inc_all_spool(void);
121 static void inc_autocheck_timer_set_interval (guint interval);
122 static gint inc_autocheck_func (gpointer data);
126 * @mainwin: Main window.
127 * @new_messages: TRUE if some messages have been received.
129 * Update the folder view and the summary view after receiving
130 * messages. If @new_messages is FALSE, this function avoids unneeded
133 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
137 if (prefs_common.scan_all_after_inc)
138 folderview_update_all_node();
139 /* XXX: major problems right here. if we change marks after
140 * incorporation of mail, folderview_select() rewrites it
141 * right under our nose. folderview_select() eventually
142 * calls summary_show(), which rewrites the cache twice:
143 * one for the previously selected FolderItem*, and one
144 * for the newly selected FolderItem*
146 * since filtering also allows changing mark files,
147 * i've solved this by using a global variable (in
148 * SummmaryView*). a better solution is to use the folder
149 * hash table, and see whether the newly and currently
150 * selected FolderItem* where updated by the filtering. */
152 mainwin->summaryview->filtering_happened = TRUE;
154 /* XXX: filtering_happened is reset by summary_show() */
156 if (!new_messages && !prefs_common.scan_all_after_inc) return;
158 if (prefs_common.open_inbox_on_inc) {
159 item = cur_account && cur_account->inbox
160 ? folder_find_item_from_path(cur_account->inbox)
161 : folder_get_default_inbox();
162 folderview_unselect(mainwin->folderview);
163 folderview_select(mainwin->folderview, item);
165 item = mainwin->summaryview->folder_item;
166 folderview_unselect(mainwin->folderview);
167 folderview_select(mainwin->folderview, item);
171 void inc_mail(MainWindow *mainwin)
175 if (inc_lock_count) return;
177 inc_autocheck_timer_remove();
178 summary_write_cache(mainwin->summaryview);
179 main_window_lock(mainwin);
181 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
182 /* external incorporating program */
183 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
184 main_window_unlock(mainwin);
185 inc_autocheck_timer_set();
189 if (prefs_common.inc_local)
190 new_msgs = inc_spool();
192 if (prefs_common.inc_local)
193 new_msgs = inc_spool();
195 new_msgs += inc_account_mail(cur_account, mainwin);
198 inc_finished(mainwin, new_msgs > 0);
199 main_window_unlock(mainwin);
200 inc_autocheck_timer_set();
203 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
205 IncProgressDialog *inc_dialog;
209 session = inc_session_new(account);
210 if (!session) return 0;
212 inc_dialog = inc_progress_dialog_create();
213 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list, session);
214 inc_dialog->mainwin = mainwin;
215 session->data = inc_dialog;
218 text[1] = account->account_name;
219 text[2] = _("Standby");
220 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
222 return inc_start(inc_dialog);
225 void inc_all_account_mail(MainWindow *mainwin)
227 GList *list, *queue_list = NULL;
228 IncProgressDialog *inc_dialog;
231 if (inc_lock_count) return;
233 inc_autocheck_timer_remove();
234 summary_write_cache(mainwin->summaryview);
235 main_window_lock(mainwin);
237 if (prefs_common.inc_local)
238 new_msgs = inc_spool();
240 list = account_get_list();
242 inc_finished(mainwin, new_msgs > 0);
243 main_window_unlock(mainwin);
244 inc_autocheck_timer_set();
248 for (; list != NULL; list = list->next) {
250 PrefsAccount *account = list->data;
252 if (account->recv_at_getall) {
253 session = inc_session_new(account);
255 queue_list = g_list_append(queue_list, session);
260 inc_finished(mainwin, new_msgs > 0);
261 main_window_unlock(mainwin);
262 inc_autocheck_timer_set();
266 inc_dialog = inc_progress_dialog_create();
267 inc_dialog->queue_list = queue_list;
268 inc_dialog->mainwin = mainwin;
269 for (list = queue_list; list != NULL; list = list->next) {
270 IncSession *session = list->data;
273 session->data = inc_dialog;
276 text[1] = session->pop3_state->ac_prefs->account_name;
277 text[2] = _("Standby");
278 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
281 new_msgs += inc_start(inc_dialog);
283 inc_finished(mainwin, new_msgs > 0);
284 main_window_unlock(mainwin);
285 inc_autocheck_timer_set();
288 static IncProgressDialog *inc_progress_dialog_create(void)
290 IncProgressDialog *dialog;
291 ProgressDialog *progress;
293 dialog = g_new0(IncProgressDialog, 1);
295 progress = progress_dialog_create();
296 gtk_window_set_title(GTK_WINDOW(progress->window),
297 _("Retrieving new messages"));
298 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
299 GTK_SIGNAL_FUNC(inc_cancel), dialog);
300 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
301 GTK_SIGNAL_FUNC(gtk_true), NULL);
302 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
304 progress_dialog_set_value(progress, 0.0);
306 PIXMAP_CREATE(progress->clist, okxpm, okxpmmask, complete_xpm);
307 PIXMAP_CREATE(progress->clist,
308 currentxpm, currentxpmmask, continue_xpm);
309 PIXMAP_CREATE(progress->clist, errorxpm, errorxpmmask, error_xpm);
311 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
312 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
313 manage_window_get_focus_window())) {
314 dialog->show_dialog = TRUE;
315 gtk_widget_show_now(progress->window);
318 dialog->dialog = progress;
319 dialog->queue_list = NULL;
324 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
326 progress_dialog_set_value(inc_dialog->dialog, 0.0);
327 progress_dialog_set_label(inc_dialog->dialog, "");
328 gtk_progress_bar_update
329 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
332 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
334 g_return_if_fail(inc_dialog != NULL);
336 gtk_progress_bar_update
337 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
338 progress_dialog_destroy(inc_dialog->dialog);
343 static IncSession *inc_session_new(PrefsAccount *account)
347 g_return_val_if_fail(account != NULL, NULL);
349 if (account->protocol != A_POP3 && account->protocol != A_APOP)
351 if (!account->recv_server || !account->userid)
354 session = g_new0(IncSession, 1);
355 session->pop3_state = inc_pop3_state_new(account);
356 session->pop3_state->session = session;
361 static void inc_session_destroy(IncSession *session)
363 g_return_if_fail(session != NULL);
365 inc_pop3_state_destroy(session->pop3_state);
369 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
373 state = g_new0(Pop3State, 1);
375 state->ac_prefs = account;
376 state->folder_table = g_hash_table_new(NULL, NULL);
377 state->id_table = inc_get_uidl_table(account);
378 state->id_list = NULL;
379 state->new_id_list = NULL;
380 state->inc_state = INC_SUCCESS;
385 static void inc_pop3_state_destroy(Pop3State *state)
387 g_hash_table_destroy(state->folder_table);
388 g_free(state->sizes);
390 if (state->id_table) {
391 hash_free_strings(state->id_table);
392 g_hash_table_destroy(state->id_table);
394 slist_free_strings(state->id_list);
395 slist_free_strings(state->new_id_list);
396 g_slist_free(state->id_list);
397 g_slist_free(state->new_id_list);
399 g_free(state->greeting);
402 g_free(state->prev_folder);
407 static gint inc_start(IncProgressDialog *inc_dialog)
410 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
411 Pop3State *pop3_state;
417 /* gtk_label_set_text(GTK_LABEL(inc_dialog->mainwin->statuslabel),
420 while (inc_dialog->queue_list != NULL) {
421 session = inc_dialog->queue_list->data;
422 pop3_state = session->pop3_state;
424 inc_progress_dialog_clear(inc_dialog);
426 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
428 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
429 if (pop3_state->ac_prefs->passwd)
431 g_strdup(pop3_state->ac_prefs->passwd);
432 else if (pop3_state->ac_prefs->tmp_pass)
434 g_strdup(pop3_state->ac_prefs->tmp_pass);
439 message = g_strdup_printf
440 (_("Input password for %s on %s:"),
442 pop3_state->ac_prefs->recv_server);
444 pass = input_dialog_with_invisible(_("Input password"),
447 if (inc_dialog->show_dialog)
448 manage_window_focus_in
449 (inc_dialog->mainwin->window,
452 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
453 pop3_state->pass = pass;
455 inc_session_destroy(session);
456 inc_dialog->queue_list = g_list_remove
457 (inc_dialog->queue_list, session);
462 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
463 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
465 /* begin POP3 session */
466 inc_state = inc_pop3_session_do(session);
468 if (inc_state == INC_SUCCESS) {
469 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
470 gtk_clist_set_text(clist, num, 2, _("Done"));
471 } else if (inc_state == INC_CANCEL) {
472 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
473 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
475 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
476 if (inc_state == INC_CONNECT_ERROR)
477 gtk_clist_set_text(clist, num, 2,
478 _("Connection failed"));
479 else if (inc_state == INC_AUTH_FAILED)
480 gtk_clist_set_text(clist, num, 2,
483 gtk_clist_set_text(clist, num, 2, _("Error"));
486 if (pop3_state->error_val == PS_AUTHFAIL) {
487 if(!prefs_common.noerrorpanel) {
488 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
489 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
490 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
493 (_("Authorization for %s on %s failed"),
495 pop3_state->ac_prefs->recv_server);
501 new_msgs += pop3_state->cur_total_num;
503 if (!prefs_common.scan_all_after_inc) {
504 folder_item_scan_foreach(pop3_state->folder_table);
505 folderview_update_item_foreach
506 (pop3_state->folder_table);
509 if (pop3_state->error_val == PS_AUTHFAIL &&
510 pop3_state->ac_prefs->tmp_pass) {
511 g_free(pop3_state->ac_prefs->tmp_pass);
512 pop3_state->ac_prefs->tmp_pass = NULL;
515 inc_write_uidl_list(pop3_state);
517 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
519 if (inc_state == INC_NOSPACE) {
520 inc_put_error(inc_state);
525 inc_session_destroy(session);
526 inc_dialog->queue_list =
527 g_list_remove(inc_dialog->queue_list, session);
532 /* gtk_label_set_text(GTK_LABEL(inc_dialog->mainwin->statuslabel), ""); */
534 if (error_num && !prefs_common.noerrorpanel) {
535 if (inc_dialog->show_dialog)
536 manage_window_focus_in(inc_dialog->dialog->window,
538 alertpanel_error(_("Some errors occured while getting mail."));
539 if (inc_dialog->show_dialog)
540 manage_window_focus_out(inc_dialog->dialog->window,
544 while (inc_dialog->queue_list != NULL) {
545 session = inc_dialog->queue_list->data;
546 inc_session_destroy(session);
547 inc_dialog->queue_list =
548 g_list_remove(inc_dialog->queue_list, session);
551 inc_progress_dialog_destroy(inc_dialog);
556 static IncState inc_pop3_session_do(IncSession *session)
558 Pop3State *pop3_state = session->pop3_state;
559 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
566 static AtmHandler handlers[] = {
568 pop3_getauth_user_send , pop3_getauth_user_recv,
569 pop3_getauth_pass_send , pop3_getauth_pass_recv,
570 pop3_getauth_apop_send , pop3_getauth_apop_recv,
571 pop3_getrange_stat_send , pop3_getrange_stat_recv,
572 pop3_getrange_last_send , pop3_getrange_last_recv,
573 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
574 pop3_getsize_list_send , pop3_getsize_list_recv,
575 pop3_retr_send , pop3_retr_recv,
576 pop3_delete_send , pop3_delete_recv,
577 pop3_logout_send , pop3_logout_recv
580 debug_print(_("getting new messages of account %s...\n"),
581 pop3_state->ac_prefs->account_name);
583 atm = automaton_create(N_POP3_PHASE);
586 atm->data = pop3_state;
588 buf = g_strdup_printf(_("%s: Retrieving new messages"),
589 pop3_state->ac_prefs->recv_server);
590 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
593 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
594 atm->state[i].handler = handlers[i];
595 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
596 for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
597 atm->state[i++].condition = GDK_INPUT_WRITE;
598 atm->state[i++].condition = GDK_INPUT_READ;
601 atm->terminate = (AtmHandler)pop3_automaton_terminate;
603 atm->num = POP3_GREETING_RECV;
605 server = pop3_state->ac_prefs->recv_server;
607 port = pop3_state->ac_prefs->set_popport ?
608 pop3_state->ac_prefs->popport :
609 pop3_state->ac_prefs->ssl_pop ? 995 : 110;
611 port = pop3_state->ac_prefs->set_popport ?
612 pop3_state->ac_prefs->popport : 110;
615 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
616 log_message("%s\n", buf);
617 progress_dialog_set_label(inc_dialog->dialog, buf);
623 if ((sockinfo = sock_connect_with_thread(server, port)) == NULL) {
625 if ((sockinfo = sock_connect(server, port)) == NULL) {
627 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
629 if(!prefs_common.noerrorpanel) {
630 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
631 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
632 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
634 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
636 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
638 pop3_automaton_terminate(NULL, atm);
639 automaton_destroy(atm);
640 return INC_CONNECT_ERROR;
644 if (pop3_state->ac_prefs->ssl_pop && !ssl_init_socket(sockinfo)) {
645 pop3_automaton_terminate(NULL, atm);
646 automaton_destroy(atm);
647 return INC_CONNECT_ERROR;
651 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
652 * to the sock structure - implement a reference counter?? */
653 pop3_state->sockinfo = sockinfo;
654 atm->help_sock = sockinfo;
656 log_verbosity_set(TRUE);
657 recv_set_ui_func(inc_pop3_recv_func, session);
660 atm->timeout_tag = gtk_timeout_add
661 (TIMEOUT_ITV, (GtkFunction)connection_check_cb, atm);
663 atm->tag = sock_gdk_input_add(sockinfo,
664 atm->state[atm->num].condition,
665 automaton_input_cb, atm);
668 while (!atm->terminated)
669 gtk_main_iteration();
671 log_verbosity_set(FALSE);
672 recv_set_ui_func(NULL, NULL);
675 //pthread_join(sockinfo->connect_thr, NULL);
677 automaton_destroy(atm);
679 return pop3_state->inc_state;
682 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
684 if (atm->terminated) return 0;
687 gdk_input_remove(atm->tag);
690 if (atm->timeout_tag > 0) {
691 gtk_timeout_remove(atm->timeout_tag);
692 atm->timeout_tag = 0;
697 atm->terminated = TRUE;
702 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
707 gchar buf[IDLEN + 3];
709 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
710 "uidl-", ac_prefs->recv_server,
711 "-", ac_prefs->userid, NULL);
712 if ((fp = fopen(path, "r")) == NULL) {
713 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
719 table = g_hash_table_new(g_str_hash, g_str_equal);
721 while (fgets(buf, sizeof(buf), fp) != NULL) {
723 g_hash_table_insert(table, g_strdup(buf), GINT_TO_POINTER(1));
731 static void inc_write_uidl_list(Pop3State *state)
737 if (!state->id_list) return;
739 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
740 "uidl-", state->ac_prefs->recv_server,
741 "-", state->user, NULL);
742 if ((fp = fopen(path, "w")) == NULL) {
743 FILE_OP_ERROR(path, "fopen");
748 for (cur = state->id_list; cur != NULL; cur = cur->next) {
749 if (fputs((gchar *)cur->data, fp) == EOF) {
750 FILE_OP_ERROR(path, "fputs");
753 if (fputc('\n', fp) == EOF) {
754 FILE_OP_ERROR(path, "fputc");
759 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
764 static gint connection_check_cb(Automaton *atm)
766 Pop3State *state = atm->data;
767 IncProgressDialog *inc_dialog = state->session->data;
768 SockInfo *sockinfo = state->sockinfo;
770 /* g_print("connection check\n"); */
772 if (sockinfo->state == CONN_LOOKUPFAILED ||
773 sockinfo->state == CONN_FAILED) {
774 atm->timeout_tag = 0;
775 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
776 sockinfo->hostname, sockinfo->port);
777 if(!prefs_common.noerrorpanel) {
778 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
779 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
780 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
782 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
783 sockinfo->hostname, sockinfo->port);
784 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
786 pop3_automaton_terminate(sockinfo, atm);
787 state->sockinfo = NULL;
789 } else if (sockinfo->state == CONN_ESTABLISHED) {
790 atm->timeout_tag = 0;
791 atm->tag = sock_gdk_input_add(sockinfo,
792 atm->state[atm->num].condition,
793 automaton_input_cb, atm);
801 static void inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
804 gchar buf[MSGBUFSIZE];
805 IncSession *session = (IncSession *)data;
806 Pop3State *state = session->pop3_state;
807 IncProgressDialog *inc_dialog = session->data;
808 ProgressDialog *dialog = inc_dialog->dialog;
812 cur_total = state->cur_total_bytes + read_bytes;
813 if (cur_total > state->total_bytes)
814 cur_total = state->total_bytes;
816 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
817 g_snprintf(buf, sizeof(buf),
818 _("Retrieving message (%d / %d) (%s / %s)"),
819 state->cur_msg, state->count,
820 to_human_readable(cur_total), total_size);
821 progress_dialog_set_label(dialog, buf);
823 progress_dialog_set_percentage
824 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
825 gtk_progress_bar_update
826 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
827 (gfloat)cur_total / (gfloat)state->total_bytes);
831 void inc_progress_update(Pop3State *state, Pop3Phase phase)
833 gchar buf[MSGBUFSIZE];
834 IncProgressDialog *inc_dialog = state->session->data;
835 ProgressDialog *dialog = inc_dialog->dialog;
839 case POP3_GREETING_RECV:
841 case POP3_GETAUTH_USER_SEND:
842 case POP3_GETAUTH_USER_RECV:
843 case POP3_GETAUTH_PASS_SEND:
844 case POP3_GETAUTH_PASS_RECV:
845 case POP3_GETAUTH_APOP_SEND:
846 case POP3_GETAUTH_APOP_RECV:
847 progress_dialog_set_label(dialog, _("Authenticating..."));
849 case POP3_GETRANGE_STAT_SEND:
850 case POP3_GETRANGE_STAT_RECV:
851 progress_dialog_set_label
852 (dialog, _("Getting the number of new messages (STAT)..."));
854 case POP3_GETRANGE_LAST_SEND:
855 case POP3_GETRANGE_LAST_RECV:
856 progress_dialog_set_label
857 (dialog, _("Getting the number of new messages (LAST)..."));
859 case POP3_GETRANGE_UIDL_SEND:
860 case POP3_GETRANGE_UIDL_RECV:
861 progress_dialog_set_label
862 (dialog, _("Getting the number of new messages (UIDL)..."));
864 case POP3_GETSIZE_LIST_SEND:
865 case POP3_GETSIZE_LIST_RECV:
866 progress_dialog_set_label
867 (dialog, _("Getting the size of messages (LIST)..."));
871 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
872 g_snprintf(buf, sizeof(buf),
873 _("Retrieving message (%d / %d) (%s / %s)"),
874 state->cur_msg, state->count,
875 to_human_readable(state->cur_total_bytes),
877 progress_dialog_set_label(dialog, buf);
878 progress_dialog_set_percentage
880 (gfloat)(state->cur_total_bytes) /
881 (gfloat)(state->total_bytes));
882 gtk_progress_bar_update
883 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
884 (gfloat)(state->cur_total_bytes) /
885 (gfloat)(state->total_bytes));
887 case POP3_DELETE_SEND:
888 case POP3_DELETE_RECV:
889 progress_dialog_set_label(dialog, _("Deleting message"));
891 case POP3_LOGOUT_SEND:
892 case POP3_LOGOUT_RECV:
893 progress_dialog_set_label(dialog, _("Quitting"));
900 gint inc_drop_message(const gchar *file, Pop3State *state)
903 FolderItem *dropfolder;
907 if (state->ac_prefs->inbox) {
908 inbox = folder_find_item_from_path(state->ac_prefs->inbox);
910 inbox = folder_get_default_inbox();
912 inbox = folder_get_default_inbox();
918 if (prefs_filtering == NULL) {
920 if (state->ac_prefs->filter_on_recv) {
922 filter_get_dest_folder(prefs_common.fltlist, file);
923 if (!dropfolder) dropfolder = inbox;
924 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
925 g_warning(_("a message won't be received\n"));
935 val = GPOINTER_TO_INT(g_hash_table_lookup
936 (state->folder_table, dropfolder));
938 folder_item_scan(dropfolder);
939 g_hash_table_insert(state->folder_table, dropfolder,
943 if (prefs_filtering == NULL || !state->ac_prefs->filter_on_recv) {
944 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
950 filter_incoming_message(dropfolder, file, state->folder_table);
956 static void inc_put_error(IncState istate)
960 if(!prefs_common.noerrorpanel) {
961 alertpanel_error(_("Error occurred while processing mail."));
965 alertpanel_error(_("No disk space left."));
972 static void inc_cancel(GtkWidget *widget, gpointer data)
974 IncProgressDialog *dialog = data;
975 IncSession *session = dialog->queue_list->data;
976 SockInfo *sockinfo = session->pop3_state->sockinfo;
978 if (!sockinfo || session->atm->terminated == TRUE) return;
981 if (sockinfo->state == CONN_READY ||
982 sockinfo->state == CONN_LOOKUPSUCCESS) {
983 pthread_cancel(sockinfo->connect_thr);
984 /* pthread_kill(sockinfo->connect_thr, SIGINT); */
985 g_print("connection was cancelled.\n");
989 session->pop3_state->inc_state = INC_CANCEL;
990 pop3_automaton_terminate(sockinfo, session->atm);
991 session->pop3_state->sockinfo = NULL;
994 static gint inc_spool(void)
996 gchar *mbox, *logname;
999 logname = g_get_user_name();
1000 mbox = g_strconcat(prefs_common.spool_path
1001 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1002 G_DIR_SEPARATOR_S, logname, NULL);
1003 msgs = get_spool(folder_get_default_inbox(), mbox);
1009 static void inc_spool_account(PrefsAccount *account)
1012 FolderItem *dropfolder;
1015 if (account->inbox) {
1016 inbox = folder_find_item_from_path(account->inbox);
1018 inbox = folder_get_default_inbox();
1020 inbox = folder_get_default_inbox();
1022 get_spool(inbox, account->local_mbox);
1025 static void inc_all_spool(void)
1029 list = account_get_list();
1032 for (; list != NULL; list = list->next) {
1033 IncSession *session;
1034 PrefsAccount *account = list->data;
1036 if (account->protocol == A_LOCAL)
1037 inc_spool_account(account);
1041 static gint get_spool(FolderItem *dest, const gchar *mbox)
1045 gchar tmp_mbox[MAXPATHLEN + 1];
1046 GHashTable *folder_table = NULL;
1048 g_return_val_if_fail(dest != NULL, -1);
1049 g_return_val_if_fail(mbox != NULL, -1);
1051 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1052 debug_print(_("no messages in local mailbox.\n"));
1054 } else if (size < 0)
1057 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1060 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
1061 get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
1063 if (copy_mbox(mbox, tmp_mbox) < 0)
1066 debug_print(_("Getting new messages from %s into %s...\n"),
1069 if (prefs_common.filter_on_inc)
1070 folder_table = g_hash_table_new(NULL, NULL);
1071 msgs = proc_mbox(dest, tmp_mbox, folder_table);
1074 if (msgs >= 0) empty_mbox(mbox);
1075 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1078 if (!prefs_common.scan_all_after_inc) {
1079 g_hash_table_insert(folder_table, dest,
1080 GINT_TO_POINTER(1));
1081 folder_item_scan_foreach(folder_table);
1082 folderview_update_item_foreach(folder_table);
1084 g_hash_table_destroy(folder_table);
1085 } else if (!prefs_common.scan_all_after_inc) {
1086 folder_item_scan(dest);
1087 folderview_update_item(dest, FALSE);
1098 void inc_unlock(void)
1100 if (inc_lock_count > 0)
1104 static guint autocheck_timer = 0;
1105 static gpointer autocheck_data = NULL;
1107 void inc_autocheck_timer_init(MainWindow *mainwin)
1109 autocheck_data = mainwin;
1110 inc_autocheck_timer_set();
1113 static void inc_autocheck_timer_set_interval(guint interval)
1115 inc_autocheck_timer_remove();
1117 if (prefs_common.autochk_newmail && autocheck_data) {
1118 autocheck_timer = gtk_timeout_add
1119 (interval, inc_autocheck_func, autocheck_data);
1120 debug_print("added timer = %d\n", autocheck_timer);
1124 void inc_autocheck_timer_set(void)
1126 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1129 void inc_autocheck_timer_remove(void)
1131 if (autocheck_timer) {
1132 debug_print("removed timer = %d\n", autocheck_timer);
1133 gtk_timeout_remove(autocheck_timer);
1134 autocheck_timer = 0;
1138 static gint inc_autocheck_func(gpointer data)
1140 MainWindow *mainwin = (MainWindow *)data;
1142 if (inc_lock_count) {
1143 debug_print("autocheck is locked.\n");
1144 inc_autocheck_timer_set_interval(1000);
1148 inc_all_account_mail(mainwin);