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);
418 pass = input_dialog_query_password
419 (pop3_state->ac_prefs->recv_server,
422 if (inc_dialog->show_dialog)
423 manage_window_focus_in
424 (inc_dialog->mainwin->window,
427 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
428 pop3_state->pass = pass;
430 inc_session_destroy(session);
431 inc_dialog->queue_list = g_list_remove
432 (inc_dialog->queue_list, session);
437 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
438 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
440 /* begin POP3 session */
441 inc_state = inc_pop3_session_do(session);
443 if (inc_state == INC_SUCCESS) {
444 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
445 gtk_clist_set_text(clist, num, 2, _("Done"));
446 } else if (inc_state == INC_CANCEL) {
447 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
448 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
450 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
451 if (inc_state == INC_CONNECT_ERROR)
452 gtk_clist_set_text(clist, num, 2,
453 _("Connection failed"));
454 else if (inc_state == INC_AUTH_FAILED)
455 gtk_clist_set_text(clist, num, 2,
458 gtk_clist_set_text(clist, num, 2, _("Error"));
461 if (pop3_state->error_val == PS_AUTHFAIL) {
462 if(!prefs_common.noerrorpanel) {
463 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
464 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
465 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
468 (_("Authorization for %s on %s failed"),
470 pop3_state->ac_prefs->recv_server);
476 new_msgs += pop3_state->cur_total_num;
478 if (!prefs_common.scan_all_after_inc) {
479 folder_item_scan_foreach(pop3_state->folder_table);
480 folderview_update_item_foreach
481 (pop3_state->folder_table);
484 if (pop3_state->error_val == PS_AUTHFAIL &&
485 pop3_state->ac_prefs->tmp_pass) {
486 g_free(pop3_state->ac_prefs->tmp_pass);
487 pop3_state->ac_prefs->tmp_pass = NULL;
490 inc_write_uidl_list(pop3_state);
492 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
494 if (inc_state == INC_NOSPACE) {
495 inc_put_error(inc_state);
500 inc_session_destroy(session);
501 inc_dialog->queue_list =
502 g_list_remove(inc_dialog->queue_list, session);
507 if (error_num && !prefs_common.noerrorpanel) {
508 if (inc_dialog->show_dialog)
509 manage_window_focus_in(inc_dialog->dialog->window,
511 alertpanel_error(_("Some errors occured while getting mail."));
512 if (inc_dialog->show_dialog)
513 manage_window_focus_out(inc_dialog->dialog->window,
517 while (inc_dialog->queue_list != NULL) {
518 session = inc_dialog->queue_list->data;
519 inc_session_destroy(session);
520 inc_dialog->queue_list =
521 g_list_remove(inc_dialog->queue_list, session);
524 inc_progress_dialog_destroy(inc_dialog);
529 static IncState inc_pop3_session_do(IncSession *session)
531 Pop3State *pop3_state = session->pop3_state;
532 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
539 static AtmHandler handlers[] = {
541 pop3_getauth_user_send , pop3_getauth_user_recv,
542 pop3_getauth_pass_send , pop3_getauth_pass_recv,
543 pop3_getauth_apop_send , pop3_getauth_apop_recv,
544 pop3_getrange_stat_send , pop3_getrange_stat_recv,
545 pop3_getrange_last_send , pop3_getrange_last_recv,
546 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
547 pop3_getsize_list_send , pop3_getsize_list_recv,
548 pop3_retr_send , pop3_retr_recv,
549 pop3_delete_send , pop3_delete_recv,
550 pop3_logout_send , pop3_logout_recv
553 debug_print(_("getting new messages of account %s...\n"),
554 pop3_state->ac_prefs->account_name);
556 atm = automaton_create(N_POP3_PHASE);
559 atm->data = pop3_state;
561 buf = g_strdup_printf(_("%s: Retrieving new messages"),
562 pop3_state->ac_prefs->recv_server);
563 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
566 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
567 atm->state[i].handler = handlers[i];
568 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
569 for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
570 atm->state[i++].condition = GDK_INPUT_WRITE;
571 atm->state[i++].condition = GDK_INPUT_READ;
574 atm->terminate = (AtmHandler)pop3_automaton_terminate;
576 atm->num = POP3_GREETING_RECV;
578 server = pop3_state->ac_prefs->recv_server;
580 port = pop3_state->ac_prefs->set_popport ?
581 pop3_state->ac_prefs->popport :
582 pop3_state->ac_prefs->ssl_pop ? 995 : 110;
584 port = pop3_state->ac_prefs->set_popport ?
585 pop3_state->ac_prefs->popport : 110;
588 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
589 log_message("%s\n", buf);
590 progress_dialog_set_label(inc_dialog->dialog, buf);
596 if ((sockinfo = sock_connect_with_thread(server, port)) == NULL) {
598 if ((sockinfo = sock_connect(server, port)) == NULL) {
600 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
602 if(!prefs_common.noerrorpanel) {
603 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
604 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
605 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
607 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
609 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
611 pop3_automaton_terminate(NULL, atm);
612 automaton_destroy(atm);
613 return INC_CONNECT_ERROR;
617 if (pop3_state->ac_prefs->ssl_pop && !ssl_init_socket(sockinfo)) {
618 pop3_automaton_terminate(NULL, atm);
619 automaton_destroy(atm);
620 return INC_CONNECT_ERROR;
624 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
625 * to the sock structure - implement a reference counter?? */
626 pop3_state->sockinfo = sockinfo;
627 atm->help_sock = sockinfo;
629 log_verbosity_set(TRUE);
630 recv_set_ui_func(inc_pop3_recv_func, session);
633 atm->timeout_tag = gtk_timeout_add
634 (TIMEOUT_ITV, (GtkFunction)connection_check_cb, atm);
636 atm->tag = sock_gdk_input_add(sockinfo,
637 atm->state[atm->num].condition,
638 automaton_input_cb, atm);
641 while (!atm->terminated)
642 gtk_main_iteration();
644 log_verbosity_set(FALSE);
645 recv_set_ui_func(NULL, NULL);
648 /* pthread_join(sockinfo->connect_thr, NULL); */
650 automaton_destroy(atm);
652 return pop3_state->inc_state;
655 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
657 if (atm->terminated) return 0;
660 gdk_input_remove(atm->tag);
663 if (atm->timeout_tag > 0) {
664 gtk_timeout_remove(atm->timeout_tag);
665 atm->timeout_tag = 0;
670 atm->terminated = TRUE;
675 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
680 gchar buf[IDLEN + 3];
682 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
683 "uidl-", ac_prefs->recv_server,
684 "-", ac_prefs->userid, NULL);
685 if ((fp = fopen(path, "r")) == NULL) {
686 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
692 table = g_hash_table_new(g_str_hash, g_str_equal);
694 while (fgets(buf, sizeof(buf), fp) != NULL) {
696 g_hash_table_insert(table, g_strdup(buf), GINT_TO_POINTER(1));
704 static void inc_write_uidl_list(Pop3State *state)
710 if (!state->id_list) return;
712 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
713 "uidl-", state->ac_prefs->recv_server,
714 "-", state->user, NULL);
715 if ((fp = fopen(path, "w")) == NULL) {
716 FILE_OP_ERROR(path, "fopen");
721 for (cur = state->id_list; cur != NULL; cur = cur->next) {
722 if (fputs((gchar *)cur->data, fp) == EOF) {
723 FILE_OP_ERROR(path, "fputs");
726 if (fputc('\n', fp) == EOF) {
727 FILE_OP_ERROR(path, "fputc");
732 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
737 static gint connection_check_cb(Automaton *atm)
739 Pop3State *state = atm->data;
740 IncProgressDialog *inc_dialog = state->session->data;
741 SockInfo *sockinfo = state->sockinfo;
743 /* g_print("connection check\n"); */
745 if (sockinfo->state == CONN_LOOKUPFAILED ||
746 sockinfo->state == CONN_FAILED) {
747 atm->timeout_tag = 0;
748 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
749 sockinfo->hostname, sockinfo->port);
750 if(!prefs_common.noerrorpanel) {
751 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
752 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
753 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
755 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
756 sockinfo->hostname, sockinfo->port);
757 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
759 pop3_automaton_terminate(sockinfo, atm);
760 state->sockinfo = NULL;
762 } else if (sockinfo->state == CONN_ESTABLISHED) {
763 atm->timeout_tag = 0;
764 atm->tag = sock_gdk_input_add(sockinfo,
765 atm->state[atm->num].condition,
766 automaton_input_cb, atm);
774 static void inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
777 gchar buf[MSGBUFSIZE];
778 IncSession *session = (IncSession *)data;
779 Pop3State *state = session->pop3_state;
780 IncProgressDialog *inc_dialog = session->data;
781 ProgressDialog *dialog = inc_dialog->dialog;
785 cur_total = state->cur_total_bytes + read_bytes;
786 if (cur_total > state->total_bytes)
787 cur_total = state->total_bytes;
789 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
790 g_snprintf(buf, sizeof(buf),
791 _("Retrieving message (%d / %d) (%s / %s)"),
792 state->cur_msg, state->count,
793 to_human_readable(cur_total), total_size);
794 progress_dialog_set_label(dialog, buf);
796 progress_dialog_set_percentage
797 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
798 gtk_progress_bar_update
799 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
800 (gfloat)cur_total / (gfloat)state->total_bytes);
804 void inc_progress_update(Pop3State *state, Pop3Phase phase)
806 gchar buf[MSGBUFSIZE];
807 IncProgressDialog *inc_dialog = state->session->data;
808 ProgressDialog *dialog = inc_dialog->dialog;
812 case POP3_GREETING_RECV:
814 case POP3_GETAUTH_USER_SEND:
815 case POP3_GETAUTH_USER_RECV:
816 case POP3_GETAUTH_PASS_SEND:
817 case POP3_GETAUTH_PASS_RECV:
818 case POP3_GETAUTH_APOP_SEND:
819 case POP3_GETAUTH_APOP_RECV:
820 progress_dialog_set_label(dialog, _("Authenticating..."));
822 case POP3_GETRANGE_STAT_SEND:
823 case POP3_GETRANGE_STAT_RECV:
824 progress_dialog_set_label
825 (dialog, _("Getting the number of new messages (STAT)..."));
827 case POP3_GETRANGE_LAST_SEND:
828 case POP3_GETRANGE_LAST_RECV:
829 progress_dialog_set_label
830 (dialog, _("Getting the number of new messages (LAST)..."));
832 case POP3_GETRANGE_UIDL_SEND:
833 case POP3_GETRANGE_UIDL_RECV:
834 progress_dialog_set_label
835 (dialog, _("Getting the number of new messages (UIDL)..."));
837 case POP3_GETSIZE_LIST_SEND:
838 case POP3_GETSIZE_LIST_RECV:
839 progress_dialog_set_label
840 (dialog, _("Getting the size of messages (LIST)..."));
844 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
845 g_snprintf(buf, sizeof(buf),
846 _("Retrieving message (%d / %d) (%s / %s)"),
847 state->cur_msg, state->count,
848 to_human_readable(state->cur_total_bytes),
850 progress_dialog_set_label(dialog, buf);
851 progress_dialog_set_percentage
853 (gfloat)(state->cur_total_bytes) /
854 (gfloat)(state->total_bytes));
855 gtk_progress_bar_update
856 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
857 (gfloat)(state->cur_total_bytes) /
858 (gfloat)(state->total_bytes));
860 case POP3_DELETE_SEND:
861 case POP3_DELETE_RECV:
862 progress_dialog_set_label(dialog, _("Deleting message"));
864 case POP3_LOGOUT_SEND:
865 case POP3_LOGOUT_RECV:
866 progress_dialog_set_label(dialog, _("Quitting"));
873 gint inc_drop_message(const gchar *file, Pop3State *state)
876 FolderItem *dropfolder;
880 /* CLAWS: get default inbox (perhaps per account) */
881 if (state->ac_prefs->inbox) {
882 /* CLAWS: get destination folder / mailbox */
883 inbox = folder_find_item_from_identifier(state->ac_prefs->inbox);
885 inbox = folder_get_default_inbox();
887 inbox = folder_get_default_inbox();
893 /* CLAWS: claws uses a global .processing folder for the filtering. */
894 if (global_processing == NULL) {
895 if (state->ac_prefs->filter_on_recv) {
897 filter_get_dest_folder(prefs_common.fltlist, file);
898 if (!dropfolder) dropfolder = inbox;
899 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
900 g_warning(_("a message won't be received\n"));
906 dropfolder = folder_get_default_processing();
909 val = GPOINTER_TO_INT(g_hash_table_lookup
910 (state->folder_table, dropfolder));
912 folder_item_scan(dropfolder);
913 g_hash_table_insert(state->folder_table, dropfolder,
917 /* add msg file to drop folder */
918 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
923 /* CLAWS: perform filtering actions on dropped message */
924 if (global_processing != NULL) {
925 if (state->ac_prefs->filter_on_recv)
926 filter_message(global_processing, inbox, msgnum,
927 state->folder_table);
933 static void inc_put_error(IncState istate)
937 if(!prefs_common.noerrorpanel) {
938 alertpanel_error(_("Error occurred while processing mail."));
942 alertpanel_error(_("No disk space left."));
949 static void inc_cancel(GtkWidget *widget, gpointer data)
951 IncProgressDialog *dialog = data;
952 IncSession *session = dialog->queue_list->data;
953 SockInfo *sockinfo = session->pop3_state->sockinfo;
955 if (!sockinfo || session->atm->terminated == TRUE) return;
958 if (sockinfo->state == CONN_READY ||
959 sockinfo->state == CONN_LOOKUPSUCCESS) {
960 pthread_cancel(sockinfo->connect_thr);
961 /* pthread_kill(sockinfo->connect_thr, SIGINT); */
962 g_print("connection was cancelled.\n");
966 session->pop3_state->inc_state = INC_CANCEL;
967 pop3_automaton_terminate(sockinfo, session->atm);
968 session->pop3_state->sockinfo = NULL;
971 static gint inc_spool(void)
973 gchar *mbox, *logname;
976 logname = g_get_user_name();
977 mbox = g_strconcat(prefs_common.spool_path
978 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
979 G_DIR_SEPARATOR_S, logname, NULL);
980 msgs = get_spool(folder_get_default_inbox(), mbox);
986 static void inc_spool_account(PrefsAccount *account)
989 FolderItem *dropfolder;
992 if (account->inbox) {
993 inbox = folder_find_item_from_path(account->inbox);
995 inbox = folder_get_default_inbox();
997 inbox = folder_get_default_inbox();
999 get_spool(inbox, account->local_mbox);
1002 static void inc_all_spool(void)
1006 list = account_get_list();
1009 for (; list != NULL; list = list->next) {
1010 IncSession *session;
1011 PrefsAccount *account = list->data;
1013 if (account->protocol == A_LOCAL)
1014 inc_spool_account(account);
1018 static gint get_spool(FolderItem *dest, const gchar *mbox)
1022 gchar tmp_mbox[MAXPATHLEN + 1];
1023 GHashTable *folder_table = NULL;
1025 g_return_val_if_fail(dest != NULL, -1);
1026 g_return_val_if_fail(mbox != NULL, -1);
1028 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1029 debug_print(_("no messages in local mailbox.\n"));
1031 } else if (size < 0)
1034 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1037 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
1038 get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
1040 if (copy_mbox(mbox, tmp_mbox) < 0)
1043 debug_print(_("Getting new messages from %s into %s...\n"),
1046 if (prefs_common.filter_on_inc)
1047 folder_table = g_hash_table_new(NULL, NULL);
1048 msgs = proc_mbox(dest, tmp_mbox, folder_table);
1051 if (msgs >= 0) empty_mbox(mbox);
1052 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1055 if (!prefs_common.scan_all_after_inc) {
1056 g_hash_table_insert(folder_table, dest,
1057 GINT_TO_POINTER(1));
1058 folder_item_scan_foreach(folder_table);
1059 folderview_update_item_foreach(folder_table);
1061 g_hash_table_destroy(folder_table);
1062 } else if (!prefs_common.scan_all_after_inc) {
1063 folder_item_scan(dest);
1064 folderview_update_item(dest, FALSE);
1075 void inc_unlock(void)
1077 if (inc_lock_count > 0)
1081 static guint autocheck_timer = 0;
1082 static gpointer autocheck_data = NULL;
1084 void inc_autocheck_timer_init(MainWindow *mainwin)
1086 autocheck_data = mainwin;
1087 inc_autocheck_timer_set();
1090 static void inc_autocheck_timer_set_interval(guint interval)
1092 inc_autocheck_timer_remove();
1094 if (prefs_common.autochk_newmail && autocheck_data) {
1095 autocheck_timer = gtk_timeout_add
1096 (interval, inc_autocheck_func, autocheck_data);
1097 debug_print("added timer = %d\n", autocheck_timer);
1101 void inc_autocheck_timer_set(void)
1103 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1106 void inc_autocheck_timer_remove(void)
1108 if (autocheck_timer) {
1109 debug_print("removed timer = %d\n", autocheck_timer);
1110 gtk_timeout_remove(autocheck_timer);
1111 autocheck_timer = 0;
1115 static gint inc_autocheck_func(gpointer data)
1117 MainWindow *mainwin = (MainWindow *)data;
1119 if (inc_lock_count) {
1120 debug_print("autocheck is locked.\n");
1121 inc_autocheck_timer_set_interval(1000);
1125 inc_all_account_mail(mainwin);