2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 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>
35 #include <sys/types.h>
44 #include "mainwindow.h"
45 #include "folderview.h"
46 #include "summaryview.h"
47 #include "prefs_common.h"
48 #include "prefs_account.h"
58 #include "statusbar.h"
59 #include "manage_window.h"
60 #include "stock_pixmap.h"
61 #include "progressdialog.h"
62 #include "inputdialog.h"
63 #include "alertpanel.h"
65 #include "filtering.h"
69 static GList *inc_dialog_list = NULL;
71 static guint inc_lock_count = 0;
73 static GdkPixmap *currentxpm;
74 static GdkBitmap *currentxpmmask;
75 static GdkPixmap *errorxpm;
76 static GdkBitmap *errorxpmmask;
77 static GdkPixmap *okxpm;
78 static GdkBitmap *okxpmmask;
80 #define MSGBUFSIZE 8192
82 static void inc_finished (MainWindow *mainwin,
83 gboolean new_messages);
84 static gint inc_account_mail_real (MainWindow *mainwin,
85 PrefsAccount *account);
87 static IncProgressDialog *inc_progress_dialog_create
89 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
90 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
92 static IncSession *inc_session_new (PrefsAccount *account);
93 static void inc_session_destroy (IncSession *session);
94 static gint inc_start (IncProgressDialog *inc_dialog);
95 static IncState inc_pop3_session_do (IncSession *session);
97 static void inc_progress_dialog_set_label
98 (IncProgressDialog *inc_dialog,
99 IncSession *inc_session);
101 static gint inc_recv_data_progressive (Session *session,
105 static gint inc_recv_data_finished (Session *session,
108 static gint inc_recv_message (Session *session,
111 static gint inc_drop_message (Pop3Session *session,
114 static void inc_put_error (IncState istate,
117 static void inc_cancel_cb (GtkWidget *widget,
119 static gint inc_dialog_delete_cb (GtkWidget *widget,
123 static gint get_spool (FolderItem *dest,
126 static gint inc_spool_account(PrefsAccount *account);
127 static gint inc_all_spool(void);
128 static void inc_autocheck_timer_set_interval (guint interval);
129 static gint inc_autocheck_func (gpointer data);
131 static void inc_notify_cmd (gint new_msgs,
136 * @mainwin: Main window.
137 * @new_messages: TRUE if some messages have been received.
139 * Update the folder view and the summary view after receiving
140 * messages. If @new_messages is FALSE, this function avoids unneeded
143 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
147 if (prefs_common.scan_all_after_inc)
148 folderview_check_new(NULL);
150 if (!new_messages && !prefs_common.scan_all_after_inc) return;
152 if (prefs_common.open_inbox_on_inc) {
153 item = cur_account && cur_account->inbox
154 ? folder_find_item_from_identifier(cur_account->inbox)
155 : folder_get_default_inbox();
156 folderview_unselect(mainwin->folderview);
157 folderview_select(mainwin->folderview, item);
161 void inc_mail(MainWindow *mainwin, gboolean notify)
164 gint account_new_msgs = 0;
166 if (inc_lock_count) return;
168 if (prefs_common.work_offline)
169 if (alertpanel(_("Offline warning"),
170 _("You're working offline. Override?"),
171 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
175 inc_autocheck_timer_remove();
176 main_window_lock(mainwin);
178 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
179 /* external incorporating program */
180 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
181 main_window_unlock(mainwin);
182 inc_autocheck_timer_set();
186 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
187 if (account_new_msgs > 0)
188 new_msgs += account_new_msgs;
191 inc_finished(mainwin, new_msgs > 0);
192 main_window_unlock(mainwin);
193 inc_notify_cmd(new_msgs, notify);
194 inc_autocheck_timer_set();
198 void inc_pop_before_smtp(PrefsAccount *acc)
200 /* FIXME: assumes to attach to first main window */
201 inc_account_mail(mainwindow_get_mainwindow(), acc);
204 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
206 IncProgressDialog *inc_dialog;
208 FolderItem *item = NULL;
210 if(mainwin && mainwin->summaryview)
211 item = mainwin->summaryview->folder_item;
213 switch (account->protocol) {
217 * FIXME: it should return foldeview_check_new() value.
218 * TODO: do it when bug [19] is fixed (IMAP folder sets
219 * an incorrect new message count)
221 folderview_check_new(FOLDER(account->folder));
225 session = inc_session_new(account);
226 if (!session) return 0;
228 inc_dialog = inc_progress_dialog_create(FALSE);
229 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
231 inc_dialog->mainwin = mainwin;
232 inc_progress_dialog_set_list(inc_dialog);
235 toolbar_main_set_sensitive(mainwin);
236 main_window_set_menu_sensitive(mainwin);
239 return inc_start(inc_dialog);
242 return inc_spool_account(account);
250 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
254 if (inc_lock_count) return 0;
256 if (prefs_common.work_offline)
257 if (alertpanel(_("Offline warning"),
258 _("You're working offline. Override?"),
259 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
262 inc_autocheck_timer_remove();
263 main_window_lock(mainwin);
265 new_msgs = inc_account_mail_real(mainwin, account);
267 inc_finished(mainwin, new_msgs > 0);
268 main_window_unlock(mainwin);
269 inc_autocheck_timer_set();
274 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
277 GList *list, *queue_list = NULL;
278 IncProgressDialog *inc_dialog;
280 gint account_new_msgs = 0;
282 if (prefs_common.work_offline)
283 if (alertpanel(_("Offline warning"),
284 _("You're working offline. Override?"),
285 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
288 if (inc_lock_count) return;
290 inc_autocheck_timer_remove();
291 main_window_lock(mainwin);
293 list = account_get_list();
295 inc_finished(mainwin, new_msgs > 0);
296 main_window_unlock(mainwin);
297 inc_notify_cmd(new_msgs, notify);
298 inc_autocheck_timer_set();
302 /* check local folders */
303 account_new_msgs = inc_all_spool();
304 if (account_new_msgs > 0)
305 new_msgs += account_new_msgs;
307 /* check IMAP4 folders */
308 for (; list != NULL; list = list->next) {
309 PrefsAccount *account = list->data;
310 if ((account->protocol == A_IMAP4 ||
311 account->protocol == A_NNTP) && account->recv_at_getall) {
312 new_msgs += folderview_check_new(FOLDER(account->folder));
316 /* check POP3 accounts */
317 for (list = account_get_list(); list != NULL; list = list->next) {
319 PrefsAccount *account = list->data;
321 if (account->recv_at_getall) {
322 session = inc_session_new(account);
324 queue_list = g_list_append(queue_list, session);
329 inc_finished(mainwin, new_msgs > 0);
330 main_window_unlock(mainwin);
331 inc_notify_cmd(new_msgs, notify);
332 inc_autocheck_timer_set();
336 inc_dialog = inc_progress_dialog_create(autocheck);
337 inc_dialog->queue_list = queue_list;
338 inc_dialog->mainwin = mainwin;
339 inc_progress_dialog_set_list(inc_dialog);
341 toolbar_main_set_sensitive(mainwin);
342 main_window_set_menu_sensitive(mainwin);
344 new_msgs += inc_start(inc_dialog);
345 inc_finished(mainwin, new_msgs > 0);
346 main_window_unlock(mainwin);
347 inc_notify_cmd(new_msgs, notify);
348 inc_autocheck_timer_set();
351 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
353 IncProgressDialog *dialog;
354 ProgressDialog *progress;
356 dialog = g_new0(IncProgressDialog, 1);
358 progress = progress_dialog_create();
359 gtk_window_set_title(GTK_WINDOW(progress->window),
360 _("Retrieving new messages"));
361 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
362 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
363 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
364 GTK_SIGNAL_FUNC(inc_dialog_delete_cb), dialog);
365 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
367 progress_dialog_set_value(progress, 0.0);
369 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
371 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
372 ¤txpm, ¤txpmmask);
373 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
374 &errorxpm, &errorxpmmask);
376 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
377 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
379 dialog->show_dialog = TRUE;
380 gtk_widget_show_now(progress->window);
383 dialog->dialog = progress;
384 dialog->timer_id = 0;
385 dialog->queue_list = NULL;
388 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
393 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
397 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
398 IncSession *session = list->data;
399 Pop3Session *pop3_session = POP3_SESSION(session->session);
402 session->data = inc_dialog;
405 text[1] = pop3_session->ac_prefs->account_name;
406 text[2] = _("Standby");
407 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
411 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
413 progress_dialog_set_value(inc_dialog->dialog, 0.0);
414 progress_dialog_set_label(inc_dialog->dialog, "");
415 if (inc_dialog->mainwin)
416 main_window_progress_off(inc_dialog->mainwin);
419 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
421 g_return_if_fail(inc_dialog != NULL);
423 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
425 if (inc_dialog->mainwin)
426 main_window_progress_off(inc_dialog->mainwin);
427 progress_dialog_destroy(inc_dialog->dialog);
432 static IncSession *inc_session_new(PrefsAccount *account)
436 g_return_val_if_fail(account != NULL, NULL);
438 if (account->protocol != A_POP3 && account->protocol != A_APOP)
440 if (!account->recv_server || !account->userid)
443 session = g_new0(IncSession, 1);
445 session->session = pop3_session_new(account);
446 session->session->data = session;
447 POP3_SESSION(session->session)->drop_message = inc_drop_message;
448 session_set_recv_message_notify(session->session,
449 inc_recv_message, session);
450 session_set_recv_data_progressive_notify(session->session,
451 inc_recv_data_progressive,
453 session_set_recv_data_notify(session->session,
454 inc_recv_data_finished, session);
456 session->folder_table = g_hash_table_new(NULL, NULL);
457 session->tmp_folder_table = g_hash_table_new(NULL, NULL);
462 static void inc_session_destroy(IncSession *session)
464 g_return_if_fail(session != NULL);
466 session_destroy(session->session);
467 g_hash_table_destroy(session->folder_table);
468 g_hash_table_destroy(session->tmp_folder_table);
472 static gint inc_start(IncProgressDialog *inc_dialog)
475 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
477 Pop3Session *pop3_session;
483 FolderItem *processing, *inbox;
485 GSList *msglist, *msglist_element;
487 qlist = inc_dialog->queue_list;
488 while (qlist != NULL) {
489 GList *next = qlist->next;
491 session = qlist->data;
492 pop3_session = POP3_SESSION(session->session);
493 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
494 if (pop3_session->ac_prefs->passwd)
496 g_strdup(pop3_session->ac_prefs->passwd);
497 else if (pop3_session->ac_prefs->tmp_pass)
499 g_strdup(pop3_session->ac_prefs->tmp_pass);
503 if (inc_dialog->show_dialog)
504 manage_window_focus_in
505 (inc_dialog->dialog->window,
508 pass = input_dialog_query_password
509 (pop3_session->ac_prefs->recv_server,
512 if (inc_dialog->show_dialog)
513 manage_window_focus_out
514 (inc_dialog->dialog->window,
518 pop3_session->ac_prefs->tmp_pass =
520 pop3_session->pass = pass;
527 #define SET_PIXMAP_AND_TEXT(xpm, xpmmask, str) \
529 gtk_clist_set_pixmap(clist, inc_dialog->cur_row, 0, xpm, xpmmask); \
530 gtk_clist_set_text(clist, inc_dialog->cur_row, 2, str); \
533 for (; inc_dialog->queue_list != NULL; inc_dialog->cur_row++) {
534 session = inc_dialog->queue_list->data;
535 pop3_session = POP3_SESSION(session->session);
537 if (pop3_session->pass == NULL) {
538 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
539 inc_session_destroy(session);
540 inc_dialog->queue_list =
541 g_list_remove(inc_dialog->queue_list, session);
545 inc_progress_dialog_clear(inc_dialog);
546 gtk_clist_moveto(clist, inc_dialog->cur_row, -1, 1.0, 0.0);
548 SET_PIXMAP_AND_TEXT(currentxpm, currentxpmmask,
551 /* begin POP3 session */
552 inc_state = inc_pop3_session_do(session);
556 if (pop3_session->cur_total_num > 0)
557 msg = g_strdup_printf
558 (_("Done (%d message(s) (%s) received)"),
559 pop3_session->cur_total_num,
560 to_human_readable(pop3_session->cur_total_recv_bytes));
562 msg = g_strdup_printf(_("Done (no new messages)"));
563 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, msg);
566 case INC_CONNECT_ERROR:
567 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
568 _("Connection failed"));
570 case INC_AUTH_FAILED:
571 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
575 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
581 case INC_SOCKET_ERROR:
583 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask, _("Error"));
586 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
592 if (pop3_session->error_val == PS_AUTHFAIL) {
593 if(!prefs_common.no_recv_err_panel) {
594 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
595 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
596 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
599 (_("Authorization for %s on %s failed"),
601 pop3_session->ac_prefs->recv_server);
605 /* CLAWS: perform filtering actions on dropped message */
606 /* CLAWS: get default inbox (perhaps per account) */
607 if (pop3_session->ac_prefs->inbox) {
608 /* CLAWS: get destination folder / mailbox */
609 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
611 inbox = folder_get_default_inbox();
613 inbox = folder_get_default_inbox();
615 /* get list of messages in processing */
616 processing = folder_get_default_processing();
617 folder_item_scan(processing);
618 msglist = folder_item_get_msg_list(processing);
620 /* process messages */
621 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
622 msginfo = (MsgInfo *) msglist_element->data;
623 if (!pop3_session->ac_prefs->filter_on_recv || !procmsg_msginfo_filter(msginfo))
624 folder_item_move_msg(inbox, msginfo);
625 procmsg_msginfo_free(msginfo);
627 g_slist_free(msglist);
631 new_msgs += pop3_session->cur_total_num;
633 if (pop3_session->error_val == PS_AUTHFAIL &&
634 pop3_session->ac_prefs->tmp_pass) {
635 g_free(pop3_session->ac_prefs->tmp_pass);
636 pop3_session->ac_prefs->tmp_pass = NULL;
639 pop3_write_uidl_list(pop3_session);
641 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
643 if (inc_dialog->show_dialog)
644 manage_window_focus_in
645 (inc_dialog->dialog->window,
647 inc_put_error(inc_state, pop3_session->error_msg);
648 if (inc_dialog->show_dialog)
649 manage_window_focus_out
650 (inc_dialog->dialog->window,
652 if (inc_state == INC_NO_SPACE ||
653 inc_state == INC_IO_ERROR)
657 inc_session_destroy(session);
658 inc_dialog->queue_list =
659 g_list_remove(inc_dialog->queue_list, session);
662 #undef SET_PIXMAP_AND_TEXT
665 fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
668 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
670 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
673 if (error_num && !prefs_common.no_recv_err_panel) {
674 if (inc_dialog->show_dialog)
675 manage_window_focus_in(inc_dialog->dialog->window,
677 alertpanel_error_log(_("Some errors occurred while getting mail."));
678 if (inc_dialog->show_dialog)
679 manage_window_focus_out(inc_dialog->dialog->window,
684 while (inc_dialog->queue_list != NULL) {
685 session = inc_dialog->queue_list->data;
686 inc_session_destroy(session);
687 inc_dialog->queue_list =
688 g_list_remove(inc_dialog->queue_list, session);
691 if (prefs_common.close_recv_dialog)
692 inc_progress_dialog_destroy(inc_dialog);
694 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
696 gtk_label_set_text(GTK_LABEL(GTK_BIN(inc_dialog->dialog->cancel_btn)->child),
705 static IncState inc_pop3_session_do(IncSession *session)
707 Pop3Session *pop3_session = POP3_SESSION(session->session);
708 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
713 debug_print("getting new messages of account %s...\n",
714 pop3_session->ac_prefs->account_name);
716 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
718 buf = g_strdup_printf(_("%s: Retrieving new messages"),
719 pop3_session->ac_prefs->recv_server);
720 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
723 server = pop3_session->ac_prefs->recv_server;
725 port = pop3_session->ac_prefs->set_popport ?
726 pop3_session->ac_prefs->popport :
727 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
728 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
729 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
730 SESSION(pop3_session)->nonblocking =
731 pop3_session->ac_prefs->use_nonblocking_ssl;
733 port = pop3_session->ac_prefs->set_popport ?
734 pop3_session->ac_prefs->popport : 110;
737 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
738 log_message("%s\n", buf);
740 progress_dialog_set_label(inc_dialog->dialog, buf);
743 if (session_connect(SESSION(pop3_session), server, port) < 0) {
744 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
746 if(!prefs_common.no_recv_err_panel) {
747 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
748 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
749 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
751 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
753 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
755 session->inc_state = INC_CONNECT_ERROR;
757 return INC_CONNECT_ERROR;
760 while (session_is_connected(SESSION(pop3_session)) &&
761 session->inc_state != INC_CANCEL)
762 gtk_main_iteration();
764 if (inc_dialog->timer_id) {
765 gtk_timeout_remove(inc_dialog->timer_id);
766 inc_dialog->timer_id = 0;
771 if (session->inc_state == INC_SUCCESS) {
772 switch (pop3_session->error_val) {
774 if (SESSION(pop3_session)->state == SESSION_ERROR) {
775 if (pop3_session->state == POP3_READY)
776 session->inc_state = INC_CONNECT_ERROR;
778 session->inc_state = INC_ERROR;
779 } else if (SESSION(pop3_session)->state == SESSION_EOF)
780 session->inc_state = INC_EOF;
782 session->inc_state = INC_SUCCESS;
785 session->inc_state = INC_AUTH_FAILED;
788 session->inc_state = INC_IO_ERROR;
791 session->inc_state = INC_SOCKET_ERROR;
794 session->inc_state = INC_LOCKED;
797 session->inc_state = INC_ERROR;
802 return session->inc_state;
805 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
806 IncSession *inc_session)
808 gchar buf[MSGBUFSIZE];
809 ProgressDialog *dialog = inc_dialog->dialog;
810 Pop3Session *session;
812 g_return_if_fail(inc_session != NULL);
814 session = POP3_SESSION(inc_session->session);
816 switch (session->state) {
819 case POP3_GETAUTH_USER:
820 case POP3_GETAUTH_PASS:
821 case POP3_GETAUTH_APOP:
822 progress_dialog_set_label(dialog, _("Authenticating..."));
823 statusbar_print_all(_("Retrieving messages from %s..."),
824 SESSION(session)->server);
826 case POP3_GETRANGE_STAT:
827 progress_dialog_set_label
828 (dialog, _("Getting the number of new messages (STAT)..."));
830 case POP3_GETRANGE_LAST:
831 progress_dialog_set_label
832 (dialog, _("Getting the number of new messages (LAST)..."));
834 case POP3_GETRANGE_UIDL:
835 progress_dialog_set_label
836 (dialog, _("Getting the number of new messages (UIDL)..."));
838 case POP3_GETSIZE_LIST:
839 progress_dialog_set_label
840 (dialog, _("Getting the size of messages (LIST)..."));
843 gtk_progress_set_show_text
844 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), TRUE);
845 g_snprintf(buf, sizeof(buf), "%d / %d",
846 session->cur_msg, session->count);
847 gtk_progress_set_format_string
848 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), buf);
849 inc_recv_data_progressive
850 (SESSION(session), 0,
851 session->msg[session->cur_msg].size,
855 if (session->msg[session->cur_msg].recv_time <
856 session->current_time) {
857 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
859 progress_dialog_set_label(dialog, buf);
863 progress_dialog_set_label(dialog, _("Quitting"));
870 static gboolean hash_remove_func(gpointer key, gpointer value, gpointer data)
875 static gint inc_timer_func(gpointer data)
877 IncSession *inc_session = (IncSession *)data;
878 IncProgressDialog *inc_dialog;
880 g_return_val_if_fail(inc_session != NULL, -1);
882 inc_dialog = (IncProgressDialog *)inc_session->data;
884 if (g_hash_table_size(inc_session->tmp_folder_table) > 0) {
885 folderview_update_item_foreach(inc_session->tmp_folder_table,
887 g_hash_table_foreach_remove(inc_session->tmp_folder_table,
888 hash_remove_func, NULL);
894 static gint inc_recv_data_progressive(Session *session, guint cur_len,
895 guint total_len, gpointer data)
897 gchar buf[MSGBUFSIZE];
898 IncSession *inc_session = (IncSession *)data;
899 Pop3Session *pop3_session = POP3_SESSION(session);
900 IncProgressDialog *inc_dialog;
901 ProgressDialog *dialog;
905 g_return_val_if_fail(inc_session != NULL, -1);
907 if (pop3_session->state != POP3_RETR &&
908 pop3_session->state != POP3_RETR_RECV &&
909 pop3_session->state != POP3_DELETE &&
910 pop3_session->state != POP3_LOGOUT) return 0;
912 if (!pop3_session->new_msg_exist) return 0;
914 inc_dialog = (IncProgressDialog *)inc_session->data;
915 dialog = inc_dialog->dialog;
917 cur_total = pop3_session->cur_total_bytes + cur_len;
918 if (cur_total > pop3_session->total_bytes)
919 cur_total = pop3_session->total_bytes;
921 Xstrdup_a(total_size, to_human_readable(pop3_session->total_bytes),
923 g_snprintf(buf, sizeof(buf),
924 _("Retrieving message (%d / %d) (%s / %s)"),
925 pop3_session->cur_msg, pop3_session->count,
926 to_human_readable(cur_total), total_size);
927 progress_dialog_set_label(dialog, buf);
928 progress_dialog_set_percentage
929 (dialog, (gfloat)cur_total / (gfloat)pop3_session->total_bytes);
930 if (inc_dialog->mainwin)
931 gtk_progress_bar_update
932 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
933 (gfloat)cur_total / (gfloat)pop3_session->total_bytes);
938 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
940 IncSession *inc_session = (IncSession *)data;
941 Pop3Session *pop3_session = POP3_SESSION(session);
942 IncProgressDialog *inc_dialog;
945 g_return_val_if_fail(inc_session != NULL, -1);
947 inc_dialog = (IncProgressDialog *)inc_session->data;
948 inc_recv_data_progressive(session, 0, len, inc_session);
949 inc_progress_dialog_set_label(inc_dialog, inc_session);
951 if (POP3_SESSION(session)->state == POP3_LOGOUT &&
952 inc_dialog->timer_id) {
953 inc_timer_func(data);
954 gtk_timeout_remove(inc_dialog->timer_id);
955 inc_dialog->timer_id = 0;
958 if (pop3_session->cur_total_num == 0) return 0;
960 msg = g_strdup_printf(_("Retrieving (%d message(s) (%s) received)"),
961 pop3_session->cur_total_num,
963 (pop3_session->cur_total_recv_bytes));
964 gtk_clist_set_text(GTK_CLIST(inc_dialog->dialog->clist),
965 inc_dialog->cur_row, 2, msg);
971 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
973 IncSession *inc_session = (IncSession *)data;
974 IncProgressDialog *inc_dialog;
976 g_return_val_if_fail(inc_session != NULL, -1);
978 inc_dialog = (IncProgressDialog *)inc_session->data;
979 inc_progress_dialog_set_label(inc_dialog, inc_session);
981 if (POP3_SESSION(session)->state == POP3_LOGOUT &&
982 inc_dialog->timer_id) {
983 inc_timer_func(data);
984 gtk_timeout_remove(inc_dialog->timer_id);
985 inc_dialog->timer_id = 0;
991 static gint inc_drop_message(Pop3Session *session, const gchar *file)
994 FolderItem *dropfolder;
995 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
996 IncProgressDialog *inc_dialog;
1000 g_return_val_if_fail(inc_session != NULL, -1);
1002 if (session->ac_prefs->inbox) {
1003 inbox = folder_find_item_from_identifier
1004 (session->ac_prefs->inbox);
1006 inbox = folder_get_default_inbox();
1008 inbox = folder_get_default_inbox();
1014 /* CLAWS: claws uses a global .processing folder for the filtering. */
1015 dropfolder = folder_get_default_processing();
1017 /* add msg file to drop folder */
1018 if ((msgnum = folder_item_add_msg(dropfolder, file, NULL, TRUE)) < 0) {
1023 inc_dialog = (IncProgressDialog *)inc_session->data;
1024 if (inc_dialog->timer_id == 0)
1025 inc_dialog->timer_id = gtk_timeout_add(FOLDER_UPDATE_INTERVAL,
1032 static void inc_put_error(IncState istate, const gchar *msg)
1034 gchar *log_msg = NULL;
1035 gchar *err_msg = NULL;
1036 gboolean fatal_error = FALSE;
1039 case INC_CONNECT_ERROR:
1040 log_msg = _("Connection failed.");
1041 if (prefs_common.no_recv_err_panel)
1043 err_msg = g_strdup(log_msg);
1046 log_msg = _("Error occurred while processing mail.");
1047 if (prefs_common.no_recv_err_panel)
1050 err_msg = g_strdup_printf
1051 (_("Error occurred while processing mail:\n%s"),
1054 err_msg = g_strdup(log_msg);
1057 log_msg = _("No disk space left.");
1058 err_msg = g_strdup(log_msg);
1062 log_msg = _("Can't write file.");
1063 err_msg = g_strdup(log_msg);
1066 case INC_SOCKET_ERROR:
1067 log_msg = _("Socket error.");
1068 if (prefs_common.no_recv_err_panel)
1070 err_msg = g_strdup(log_msg);
1073 log_msg = _("Connection closed by the remote host.");
1074 if (prefs_common.no_recv_err_panel)
1076 err_msg = g_strdup(log_msg);
1079 log_msg = _("Mailbox is locked.");
1080 if (prefs_common.no_recv_err_panel)
1083 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1086 err_msg = g_strdup(log_msg);
1088 case INC_AUTH_FAILED:
1089 log_msg = _("Authentication failed.");
1090 if (prefs_common.no_recv_err_panel)
1093 err_msg = g_strdup_printf
1094 (_("Authentication failed:\n%s"), msg);
1096 err_msg = g_strdup(log_msg);
1104 log_error("%s\n", log_msg);
1106 log_warning("%s\n", log_msg);
1109 alertpanel_error_log(err_msg);
1114 static void inc_cancel(IncProgressDialog *dialog)
1116 IncSession *session;
1118 g_return_if_fail(dialog != NULL);
1120 if (dialog->queue_list == NULL) {
1121 inc_progress_dialog_destroy(dialog);
1125 session = dialog->queue_list->data;
1127 session->inc_state = INC_CANCEL;
1129 log_message(_("Incorporation cancelled\n"));
1132 gboolean inc_is_active(void)
1134 return (inc_dialog_list != NULL);
1137 void inc_cancel_all(void)
1141 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1142 inc_cancel((IncProgressDialog *)cur->data);
1145 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1147 inc_cancel((IncProgressDialog *)data);
1150 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1153 IncProgressDialog *dialog = (IncProgressDialog *)data;
1155 if (dialog->queue_list == NULL)
1156 inc_progress_dialog_destroy(dialog);
1161 static gint inc_spool_account(PrefsAccount *account)
1164 gchar *mbox, *logname;
1167 logname = g_get_user_name();
1169 if (account->inbox) {
1170 inbox = folder_find_item_from_path(account->inbox);
1172 inbox = folder_get_default_inbox();
1174 inbox = folder_get_default_inbox();
1176 if (is_file_exist(account->local_mbox))
1177 mbox = g_strdup(account->local_mbox);
1179 mbox = g_strconcat(account->local_mbox,
1180 G_DIR_SEPARATOR_S, logname, NULL);
1182 result = get_spool(inbox, mbox);
1185 statusbar_pop_all();
1190 static gint inc_all_spool(void)
1194 gint account_new_msgs = 0;
1196 list = account_get_list();
1197 if (!list) return 0;
1199 for (; list != NULL; list = list->next) {
1200 PrefsAccount *account = list->data;
1202 if ((account->protocol == A_LOCAL) &&
1203 (account->recv_at_getall)) {
1204 account_new_msgs = inc_spool_account(account);
1205 if (account_new_msgs > 0)
1206 new_msgs += account_new_msgs;
1213 static gint get_spool(FolderItem *dest, const gchar *mbox)
1217 gchar tmp_mbox[MAXPATHLEN + 1];
1219 g_return_val_if_fail(dest != NULL, -1);
1220 g_return_val_if_fail(mbox != NULL, -1);
1222 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1223 debug_print("no messages in local mailbox.\n");
1225 } else if (size < 0)
1228 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1231 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1232 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1234 if (copy_mbox(mbox, tmp_mbox) < 0) {
1235 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1239 debug_print("Getting new messages from %s into %s...\n",
1242 msgs = proc_mbox(dest, tmp_mbox);
1245 if (msgs >= 0) empty_mbox(mbox);
1246 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1256 void inc_unlock(void)
1258 if (inc_lock_count > 0)
1262 static guint autocheck_timer = 0;
1263 static gpointer autocheck_data = NULL;
1265 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1270 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1271 *prefs_common.newmail_notify_cmd))
1273 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1274 buf[1] == 'd' && !strchr(&buf[1], '%'))
1275 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1278 buf = g_strdup(prefs_common.newmail_notify_cmd);
1280 execute_command_line(buf, TRUE);
1285 void inc_autocheck_timer_init(MainWindow *mainwin)
1287 autocheck_data = mainwin;
1288 inc_autocheck_timer_set();
1291 static void inc_autocheck_timer_set_interval(guint interval)
1293 inc_autocheck_timer_remove();
1294 /* last test is to avoid re-enabling auto_check after modifying
1295 the common preferences */
1296 if (prefs_common.autochk_newmail && autocheck_data
1297 && prefs_common.work_offline == FALSE) {
1298 autocheck_timer = gtk_timeout_add
1299 (interval, inc_autocheck_func, autocheck_data);
1300 debug_print("added timer = %d\n", autocheck_timer);
1304 void inc_autocheck_timer_set(void)
1306 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1309 void inc_autocheck_timer_remove(void)
1311 if (autocheck_timer) {
1312 debug_print("removed timer = %d\n", autocheck_timer);
1313 gtk_timeout_remove(autocheck_timer);
1314 autocheck_timer = 0;
1318 static gint inc_autocheck_func(gpointer data)
1320 MainWindow *mainwin = (MainWindow *)data;
1322 if (inc_lock_count) {
1323 debug_print("autocheck is locked.\n");
1324 inc_autocheck_timer_set_interval(1000);
1328 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);