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>
33 #include <sys/types.h>
42 #include "mainwindow.h"
43 #include "folderview.h"
44 #include "summaryview.h"
45 #include "prefs_common.h"
46 #include "prefs_account.h"
55 #include "statusbar.h"
56 #include "manage_window.h"
57 #include "progressdialog.h"
58 #include "inputdialog.h"
59 #include "alertpanel.h"
61 #include "automaton.h"
63 #include "filtering.h"
65 #include "pixmaps/continue.xpm"
66 #include "pixmaps/complete.xpm"
67 #include "pixmaps/error.xpm"
69 GdkPixmap *currentxpm;
70 GdkBitmap *currentxpmmask;
72 GdkBitmap *errorxpmmask;
76 #define MSGBUFSIZE 8192
78 static void inc_finished (MainWindow *mainwin);
79 static void inc_account_mail (PrefsAccount *account,
82 static IncProgressDialog *inc_progress_dialog_create (void);
83 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
85 static IncSession *inc_session_new (PrefsAccount *account);
86 static void inc_session_destroy (IncSession *session);
87 static Pop3State *inc_pop3_state_new (PrefsAccount *account);
88 static void inc_pop3_state_destroy (Pop3State *state);
89 static void inc_start (IncProgressDialog *inc_dialog);
90 static IncState inc_pop3_session_do (IncSession *session);
91 static gint pop3_automaton_terminate (SockInfo *source,
94 static GHashTable *inc_get_uidl_table (PrefsAccount *ac_prefs);
95 static void inc_write_uidl_list (Pop3State *state);
98 static gint connection_check_cb (Automaton *atm);
101 static void inc_pop3_recv_func (SockInfo *sock,
105 static void inc_put_error (IncState istate);
107 static void inc_cancel (GtkWidget *widget,
110 static gint inc_spool (void);
111 static gint get_spool (FolderItem *dest,
114 static void inc_all_spool(void);
116 static gint inc_autocheck_func (gpointer data);
118 static void inc_finished(MainWindow *mainwin)
122 if (prefs_common.open_inbox_on_inc) {
123 item = cur_account && cur_account->inbox
124 ? folder_find_item_from_path(cur_account->inbox)
125 : folder_get_default_inbox();
126 folderview_unselect(mainwin->folderview);
127 folderview_select(mainwin->folderview, item);
129 item = mainwin->summaryview->folder_item;
130 folderview_unselect(mainwin->folderview);
131 folderview_select(mainwin->folderview, item);
135 void inc_mail(MainWindow *mainwin)
137 inc_autocheck_timer_remove();
138 summary_write_cache(mainwin->summaryview);
140 if (prefs_common.use_extinc && prefs_common.extinc_path) {
143 /* external incorporating program */
144 if ((pid = fork()) < 0) {
146 inc_autocheck_timer_set();
151 execlp(prefs_common.extinc_path,
152 g_basename(prefs_common.extinc_path),
155 /* this will be called when failed */
160 /* wait until child process is terminated */
161 waitpid(pid, NULL, 0);
163 if (prefs_common.inc_local) inc_spool();
166 if (prefs_common.inc_local) inc_spool();
169 inc_account_mail(cur_account, mainwin);
172 inc_finished(mainwin);
173 inc_autocheck_timer_set();
176 static void inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
178 IncProgressDialog *inc_dialog;
182 session = inc_session_new(account);
183 if (!session) return;
185 inc_dialog = inc_progress_dialog_create();
186 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list, session);
187 inc_dialog->mainwin = mainwin;
188 session->data = inc_dialog;
191 text[1] = account->account_name;
192 text[2] = _("Standby");
193 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
195 inc_start(inc_dialog);
198 void inc_all_account_mail(MainWindow *mainwin)
200 GList *list, *queue_list = NULL;
201 IncProgressDialog *inc_dialog;
203 inc_autocheck_timer_remove();
204 summary_write_cache(mainwin->summaryview);
206 if (prefs_common.inc_local) inc_spool();
208 list = account_get_list();
210 inc_autocheck_timer_set();
214 for (; list != NULL; list = list->next) {
216 PrefsAccount *account = list->data;
218 if (account->recv_at_getall) {
219 session = inc_session_new(account);
221 queue_list = g_list_append(queue_list, session);
226 inc_autocheck_timer_set();
230 inc_dialog = inc_progress_dialog_create();
231 inc_dialog->queue_list = queue_list;
232 inc_dialog->mainwin = mainwin;
233 for (list = queue_list; list != NULL; list = list->next) {
234 IncSession *session = list->data;
237 session->data = inc_dialog;
240 text[1] = session->pop3_state->ac_prefs->account_name;
241 text[2] = _("Standby");
242 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
245 inc_start(inc_dialog);
247 inc_finished(mainwin);
248 inc_autocheck_timer_set();
251 static IncProgressDialog *inc_progress_dialog_create(void)
253 IncProgressDialog *dialog;
254 ProgressDialog *progress;
256 dialog = g_new0(IncProgressDialog, 1);
258 progress = progress_dialog_create();
259 gtk_window_set_title(GTK_WINDOW(progress->window),
260 _("Retrieving new messages"));
261 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
262 GTK_SIGNAL_FUNC(inc_cancel), dialog);
263 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
264 GTK_SIGNAL_FUNC(gtk_true), NULL);
265 if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
266 ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
267 manage_window_set_transient(GTK_WINDOW(progress->window));
270 progress_dialog_set_value(progress, 0.0);
272 if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
273 ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
274 gtk_widget_show(progress->window);
277 PIXMAP_CREATE(progress->clist, okxpm, okxpmmask, complete_xpm);
278 PIXMAP_CREATE(progress->clist,
279 currentxpm, currentxpmmask, continue_xpm);
280 PIXMAP_CREATE(progress->clist, errorxpm, errorxpmmask, error_xpm);
282 if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
283 ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
284 gtk_widget_show_now(progress->window);
287 dialog->dialog = progress;
288 dialog->queue_list = NULL;
293 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
295 progress_dialog_set_value(inc_dialog->dialog, 0.0);
296 progress_dialog_set_label(inc_dialog->dialog, "");
299 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
301 g_return_if_fail(inc_dialog != NULL);
303 progress_dialog_destroy(inc_dialog->dialog);
308 static IncSession *inc_session_new(PrefsAccount *account)
312 g_return_val_if_fail(account != NULL, NULL);
314 if (account->protocol != A_POP3 && account->protocol != A_APOP)
316 if (!account->recv_server || !account->userid)
319 session = g_new0(IncSession, 1);
320 session->pop3_state = inc_pop3_state_new(account);
321 session->pop3_state->session = session;
326 static void inc_session_destroy(IncSession *session)
328 g_return_if_fail(session != NULL);
330 inc_pop3_state_destroy(session->pop3_state);
334 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
338 state = g_new0(Pop3State, 1);
340 state->ac_prefs = account;
341 state->folder_table = g_hash_table_new(NULL, NULL);
342 state->id_table = inc_get_uidl_table(account);
343 state->id_list = NULL;
344 state->new_id_list = NULL;
345 state->inc_state = INC_SUCCESS;
350 static void inc_pop3_state_destroy(Pop3State *state)
352 g_hash_table_destroy(state->folder_table);
353 g_free(state->sizes);
355 if (state->id_table) {
356 hash_free_strings(state->id_table);
357 g_hash_table_destroy(state->id_table);
359 slist_free_strings(state->id_list);
360 slist_free_strings(state->new_id_list);
361 g_slist_free(state->id_list);
362 g_slist_free(state->new_id_list);
364 g_free(state->greeting);
367 g_free(state->prev_folder);
372 static void inc_start(IncProgressDialog *inc_dialog)
375 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
376 Pop3State *pop3_state;
380 while (inc_dialog->queue_list != NULL) {
381 session = inc_dialog->queue_list->data;
382 pop3_state = session->pop3_state;
384 inc_progress_dialog_clear(inc_dialog);
386 gtk_clist_moveto(clist, num, 0, 1.0, 0.0);
388 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
389 if (pop3_state->ac_prefs->passwd)
391 g_strdup(pop3_state->ac_prefs->passwd);
392 else if (pop3_state->ac_prefs->tmp_pass)
394 g_strdup(pop3_state->ac_prefs->tmp_pass);
399 message = g_strdup_printf
400 (_("Input password for %s on %s:"),
402 pop3_state->ac_prefs->recv_server);
404 pass = input_dialog_with_invisible(_("Input password"),
407 if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
408 ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
409 manage_window_focus_in(inc_dialog->mainwin->window,
413 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
414 pop3_state->pass = pass;
416 inc_session_destroy(session);
417 inc_dialog->queue_list = g_list_remove
418 (inc_dialog->queue_list, session);
423 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
424 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
426 /* begin POP3 session */
427 inc_state = inc_pop3_session_do(session);
429 if (inc_state == INC_SUCCESS || inc_state == INC_CANCEL) {
430 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
431 gtk_clist_set_text(clist, num, 2, _("Done"));
433 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
434 gtk_clist_set_text(clist, num, 2, _("Error"));
437 if (pop3_state->error_val == PS_AUTHFAIL) {
438 if(!prefs_common.noerrorpanel) {
439 if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
440 ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
441 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
444 (_("Authorization for %s on %s failed"),
446 pop3_state->ac_prefs->recv_server);
451 if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
452 ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
453 manage_window_focus_in(inc_dialog->mainwin->window, NULL, NULL);
456 folder_item_scan_foreach(pop3_state->folder_table);
457 folderview_update_item_foreach(pop3_state->folder_table);
459 if (pop3_state->error_val == PS_AUTHFAIL &&
460 pop3_state->ac_prefs->tmp_pass) {
461 g_free(pop3_state->ac_prefs->tmp_pass);
462 pop3_state->ac_prefs->tmp_pass = NULL;
465 inc_write_uidl_list(pop3_state);
467 if (inc_state != INC_SUCCESS) {
468 inc_put_error(inc_state);
472 inc_session_destroy(session);
473 inc_dialog->queue_list =
474 g_list_remove(inc_dialog->queue_list, session);
479 while (inc_dialog->queue_list != NULL) {
480 session = inc_dialog->queue_list->data;
481 inc_session_destroy(session);
482 inc_dialog->queue_list =
483 g_list_remove(inc_dialog->queue_list, session);
486 inc_progress_dialog_destroy(inc_dialog);
490 static IncState inc_pop3_session_do(IncSession *session)
492 Pop3State *pop3_state = session->pop3_state;
493 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
500 static AtmHandler handlers[] = {
502 pop3_getauth_user_send , pop3_getauth_user_recv,
503 pop3_getauth_pass_send , pop3_getauth_pass_recv,
504 pop3_getauth_apop_send , pop3_getauth_apop_recv,
505 pop3_getrange_stat_send , pop3_getrange_stat_recv,
506 pop3_getrange_last_send , pop3_getrange_last_recv,
507 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
508 pop3_getsize_list_send , pop3_getsize_list_recv,
509 pop3_retr_send , pop3_retr_recv,
510 pop3_delete_send , pop3_delete_recv,
511 pop3_logout_send , pop3_logout_recv
514 debug_print(_("getting new messages of account %s...\n"),
515 pop3_state->ac_prefs->account_name);
517 atm = automaton_create(N_POP3_PHASE);
520 atm->data = pop3_state;
522 buf = g_strdup_printf(_("%s: Retrieving new messages"),
523 pop3_state->ac_prefs->recv_server);
524 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
527 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
528 atm->state[i].handler = handlers[i];
529 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
530 for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
531 atm->state[i++].condition = GDK_INPUT_WRITE;
532 atm->state[i++].condition = GDK_INPUT_READ;
535 atm->terminate = (AtmHandler)pop3_automaton_terminate;
537 atm->num = POP3_GREETING_RECV;
539 server = pop3_state->ac_prefs->recv_server;
541 port = pop3_state->ac_prefs->set_popport ?
542 pop3_state->ac_prefs->popport : (pop3_state->ac_prefs->pop_ssl ? 995 : 110);
544 port = pop3_state->ac_prefs->set_popport ?
545 pop3_state->ac_prefs->popport : 110;
548 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
549 log_message("%s\n", buf);
550 progress_dialog_set_label(inc_dialog->dialog, buf);
555 if ((sockinfo = sock_connect_with_thread(server, port)) == NULL) {
557 if ((sockinfo = sock_connect(server, port)) == NULL) {
559 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
561 if(!prefs_common.noerrorpanel) {
562 if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
563 ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
564 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
566 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
568 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
570 pop3_automaton_terminate(NULL, atm);
571 automaton_destroy(atm);
576 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
577 * to the sock structure - implement a reference counter?? */
578 pop3_state->sockinfo = sockinfo;
579 atm->help_sock = sockinfo;
582 if(pop3_state->ac_prefs->pop_ssl) {
585 if(ssl_ctx == NULL) {
586 log_warning(_("SSL not available\n"));
588 pop3_automaton_terminate(NULL, atm);
589 automaton_destroy(atm);
594 sockinfo->ssl = SSL_new(ssl_ctx);
595 if(sockinfo->ssl == NULL) {
596 log_warning(_("Error creating ssl context\n"));
598 pop3_automaton_terminate(NULL, atm);
599 automaton_destroy(atm);
603 SSL_set_fd(sockinfo->ssl, sockinfo->sock);
604 if(SSL_connect(sockinfo->ssl) == -1) {
605 log_warning(_("SSL connect failed\n"));
607 pop3_automaton_terminate(NULL, atm);
608 automaton_destroy(atm);
615 log_print(_("SSL connection using %s\n"), SSL_get_cipher(sockinfo->ssl));
617 /* Get server's certificate (note: beware of dynamic allocation) */
619 if((server_cert = SSL_get_peer_certificate(sockinfo->ssl)) != NULL) {
622 log_print(_("Server certificate:\n"));
624 if((str = X509_NAME_oneline(X509_get_subject_name (server_cert),0,0)) != NULL) {
625 log_print(_(" Subject: %s\n"), str);
629 if((str = X509_NAME_oneline(X509_get_issuer_name (server_cert),0,0)) != NULL) {
630 log_print(_(" Issuer: %s\n"), str);
634 X509_free(server_cert);
637 sockinfo->ssl = NULL;
641 recv_set_ui_func(inc_pop3_recv_func, session);
644 atm->timeout_tag = gtk_timeout_add
645 (TIMEOUT_ITV, (GtkFunction)connection_check_cb, atm);
647 atm->tag = sock_gdk_input_add(sockinfo,
648 atm->state[atm->num].condition,
649 automaton_input_cb, atm);
654 recv_set_ui_func(NULL, NULL);
658 pthread_join(sockinfo->connect_thr, NULL);
664 SSL_free(sockinfo->ssl);
668 automaton_destroy(atm);
670 return pop3_state->inc_state;
673 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
676 gdk_input_remove(atm->tag);
679 if (atm->timeout_tag > 0) {
680 gtk_timeout_remove(atm->timeout_tag);
681 atm->timeout_tag = 0;
688 atm->terminated = TRUE;
693 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
698 gchar buf[IDLEN + 3];
700 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
701 "uidl-", ac_prefs->recv_server,
702 "-", ac_prefs->userid, NULL);
703 if ((fp = fopen(path, "r")) == NULL) {
704 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
710 table = g_hash_table_new(g_str_hash, g_str_equal);
712 while (fgets(buf, sizeof(buf), fp) != NULL) {
714 g_hash_table_insert(table, g_strdup(buf), GINT_TO_POINTER(1));
722 static void inc_write_uidl_list(Pop3State *state)
728 if (!state->id_list) return;
730 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
731 "uidl-", state->ac_prefs->recv_server,
732 "-", state->user, NULL);
733 if ((fp = fopen(path, "w")) == NULL) {
734 FILE_OP_ERROR(path, "fopen");
739 for (cur = state->id_list; cur != NULL; cur = cur->next) {
740 if (fputs((gchar *)cur->data, fp) == EOF) {
741 FILE_OP_ERROR(path, "fputs");
744 if (fputc('\n', fp) == EOF) {
745 FILE_OP_ERROR(path, "fputc");
750 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
755 static gint connection_check_cb(Automaton *atm)
757 Pop3State *state = atm->data;
758 IncProgressDialog *inc_dialog = state->session->data;
759 SockInfo *sockinfo = state->sockinfo;
761 /* g_print("connection check\n"); */
763 if (sockinfo->state == CONN_LOOKUPFAILED ||
764 sockinfo->state == CONN_FAILED) {
765 atm->timeout_tag = 0;
766 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
767 sockinfo->hostname, sockinfo->port);
768 if(!prefs_common.noerrorpanel) {
769 if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
770 ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
771 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
773 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
774 sockinfo->hostname, sockinfo->port);
775 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
777 pop3_automaton_terminate(sockinfo, atm);
779 } else if (sockinfo->state == CONN_ESTABLISHED) {
780 atm->timeout_tag = 0;
781 atm->tag = sock_gdk_input_add(sockinfo,
782 atm->state[atm->num].condition,
783 automaton_input_cb, atm);
791 static void inc_pop3_recv_func(SockInfo *sock, gint read_bytes, gpointer data)
793 gchar buf[MSGBUFSIZE];
794 IncSession *session = (IncSession *)data;
795 Pop3State *state = session->pop3_state;
796 IncProgressDialog *inc_dialog = session->data;
797 ProgressDialog *dialog = inc_dialog->dialog;
800 cur_total = state->cur_total_bytes + read_bytes;
801 if (cur_total > state->total_bytes)
802 cur_total = state->total_bytes;
804 g_snprintf(buf, sizeof(buf),
805 _("Retrieving message (%d / %d) (%d / %d bytes)"),
806 state->cur_msg, state->count,
807 cur_total, state->total_bytes);
808 progress_dialog_set_label(dialog, buf);
810 progress_dialog_set_percentage
811 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
815 void inc_progress_update(Pop3State *state, Pop3Phase phase)
817 gchar buf[MSGBUFSIZE];
818 IncProgressDialog *inc_dialog = state->session->data;
819 ProgressDialog *dialog = inc_dialog->dialog;
822 case POP3_GREETING_RECV:
824 case POP3_GETAUTH_USER_SEND:
825 case POP3_GETAUTH_USER_RECV:
826 case POP3_GETAUTH_PASS_SEND:
827 case POP3_GETAUTH_PASS_RECV:
828 case POP3_GETAUTH_APOP_SEND:
829 case POP3_GETAUTH_APOP_RECV:
830 progress_dialog_set_label(dialog, _("Authorizing..."));
832 case POP3_GETRANGE_STAT_SEND:
833 case POP3_GETRANGE_STAT_RECV:
834 progress_dialog_set_label
835 (dialog, _("Getting the number of new messages (STAT)..."));
837 case POP3_GETRANGE_LAST_SEND:
838 case POP3_GETRANGE_LAST_RECV:
839 progress_dialog_set_label
840 (dialog, _("Getting the number of new messages (LAST)..."));
842 case POP3_GETRANGE_UIDL_SEND:
843 case POP3_GETRANGE_UIDL_RECV:
844 progress_dialog_set_label
845 (dialog, _("Getting the number of new messages (UIDL)..."));
847 case POP3_GETSIZE_LIST_SEND:
848 case POP3_GETSIZE_LIST_RECV:
849 progress_dialog_set_label
850 (dialog, _("Getting the size of messages (LIST)..."));
854 g_snprintf(buf, sizeof(buf),
855 _("Retrieving message (%d / %d) (%d / %d bytes)"),
856 state->cur_msg, state->count,
857 state->cur_total_bytes, state->total_bytes);
858 progress_dialog_set_label(dialog, buf);
859 progress_dialog_set_percentage
861 (gfloat)(state->cur_total_bytes) /
862 (gfloat)(state->total_bytes));
864 case POP3_DELETE_SEND:
865 case POP3_DELETE_RECV:
866 progress_dialog_set_label(dialog, _("Deleting message"));
868 case POP3_LOGOUT_SEND:
869 case POP3_LOGOUT_RECV:
870 progress_dialog_set_label(dialog, _("Quitting"));
877 gint inc_drop_message(const gchar *file, Pop3State *state)
880 FolderItem *dropfolder;
884 if (state->ac_prefs->inbox) {
885 inbox = folder_find_item_from_path(state->ac_prefs->inbox);
887 inbox = folder_get_default_inbox();
889 inbox = folder_get_default_inbox();
895 if (prefs_filtering == NULL) {
897 if (state->ac_prefs->filter_on_recv) {
899 filter_get_dest_folder(prefs_common.fltlist, file);
900 if (!dropfolder) dropfolder = inbox;
901 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
902 g_warning(_("a message won't be received\n"));
912 val = GPOINTER_TO_INT(g_hash_table_lookup
913 (state->folder_table, dropfolder));
915 folder_item_scan(dropfolder);
916 g_hash_table_insert(state->folder_table, dropfolder,
920 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
925 if (prefs_filtering != NULL) {
927 if (state->ac_prefs->filter_on_recv) {
928 filter_message(prefs_filtering, dropfolder, msgnum,
929 state->folder_table);
936 static void inc_put_error(IncState istate)
940 if(!prefs_common.noerrorpanel) {
941 alertpanel_error(_("Error occurred while processing mail."));
945 alertpanel_error(_("No disk space left."));
952 static void inc_cancel(GtkWidget *widget, gpointer data)
954 IncProgressDialog *dialog = data;
955 IncSession *session = dialog->queue_list->data;
956 SockInfo *sockinfo = session->pop3_state->sockinfo;
959 if (sockinfo->state == CONN_READY ||
960 sockinfo->state == CONN_LOOKUPSUCCESS) {
961 pthread_cancel(sockinfo->connect_thr);
962 /* pthread_kill(sockinfo->connect_thr, SIGINT); */
963 g_print("connection was cancelled.\n");
967 session->pop3_state->inc_state = INC_CANCEL;
968 pop3_automaton_terminate(sockinfo, session->atm);
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 g_hash_table_insert(folder_table, dest,
1056 GINT_TO_POINTER(1));
1057 folder_item_scan_foreach(folder_table);
1058 folderview_update_item_foreach(folder_table);
1059 g_hash_table_destroy(folder_table);
1061 folder_item_scan(dest);
1062 folderview_update_item(dest, FALSE);
1068 static guint autocheck_timer = 0;
1069 static gpointer autocheck_data = NULL;
1071 void inc_autocheck_timer_init(MainWindow *mainwin)
1073 autocheck_data = mainwin;
1074 inc_autocheck_timer_set();
1077 void inc_autocheck_timer_set(void)
1079 inc_autocheck_timer_remove();
1081 if (prefs_common.autochk_newmail && autocheck_data) {
1082 autocheck_timer = gtk_timeout_add
1083 (prefs_common.autochk_itv * 60000,
1089 void inc_autocheck_timer_remove(void)
1091 if (autocheck_timer) {
1092 gtk_timeout_remove(autocheck_timer);
1093 autocheck_timer = 0;
1097 static gint inc_autocheck_func(gpointer data)
1099 MainWindow *mainwin = (MainWindow *)data;
1101 inc_all_account_mail(mainwin);