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_timer_start (IncProgressDialog *inc_dialog,
98 IncSession *inc_session);
99 static void inc_timer_stop (IncProgressDialog *inc_dialog);
101 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
102 IncSession *inc_session);
104 static void inc_progress_dialog_set_label
105 (IncProgressDialog *inc_dialog,
106 IncSession *inc_session);
107 static void inc_progress_dialog_set_progress
108 (IncProgressDialog *inc_dialog,
109 IncSession *inc_session);
111 static gint inc_progress_timer_func (gpointer data);
112 static gint inc_folder_timer_func (gpointer data);
114 static gint inc_recv_data_progressive (Session *session,
118 static gint inc_recv_data_finished (Session *session,
121 static gint inc_recv_message (Session *session,
124 static gint inc_drop_message (Pop3Session *session,
127 static void inc_put_error (IncState istate,
130 static void inc_cancel_cb (GtkWidget *widget,
132 static gint inc_dialog_delete_cb (GtkWidget *widget,
136 static gint get_spool (FolderItem *dest,
139 static gint inc_spool_account(PrefsAccount *account);
140 static gint inc_all_spool(void);
141 static void inc_autocheck_timer_set_interval (guint interval);
142 static gint inc_autocheck_func (gpointer data);
144 static void inc_notify_cmd (gint new_msgs,
149 * @mainwin: Main window.
150 * @new_messages: TRUE if some messages have been received.
152 * Update the folder view and the summary view after receiving
153 * messages. If @new_messages is FALSE, this function avoids unneeded
156 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
160 if (prefs_common.scan_all_after_inc)
161 folderview_check_new(NULL);
163 if (!new_messages && !prefs_common.scan_all_after_inc) return;
165 if (prefs_common.open_inbox_on_inc) {
166 item = cur_account && cur_account->inbox
167 ? folder_find_item_from_identifier(cur_account->inbox)
168 : folder_get_default_inbox();
169 folderview_unselect(mainwin->folderview);
170 folderview_select(mainwin->folderview, item);
174 void inc_mail(MainWindow *mainwin, gboolean notify)
177 gint account_new_msgs = 0;
179 if (inc_lock_count) return;
181 if (prefs_common.work_offline)
182 if (alertpanel(_("Offline warning"),
183 _("You're working offline. Override?"),
184 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
188 inc_autocheck_timer_remove();
189 main_window_lock(mainwin);
191 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
192 /* external incorporating program */
193 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
194 main_window_unlock(mainwin);
195 inc_autocheck_timer_set();
199 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
200 if (account_new_msgs > 0)
201 new_msgs += account_new_msgs;
204 inc_finished(mainwin, new_msgs > 0);
205 main_window_unlock(mainwin);
206 inc_notify_cmd(new_msgs, notify);
207 inc_autocheck_timer_set();
211 void inc_pop_before_smtp(PrefsAccount *acc)
213 /* FIXME: assumes to attach to first main window */
214 inc_account_mail(mainwindow_get_mainwindow(), acc);
217 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
219 IncProgressDialog *inc_dialog;
221 FolderItem *item = NULL;
223 if(mainwin && mainwin->summaryview)
224 item = mainwin->summaryview->folder_item;
226 switch (account->protocol) {
230 * FIXME: it should return foldeview_check_new() value.
231 * TODO: do it when bug [19] is fixed (IMAP folder sets
232 * an incorrect new message count)
234 folderview_check_new(FOLDER(account->folder));
238 session = inc_session_new(account);
239 if (!session) return 0;
241 inc_dialog = inc_progress_dialog_create(FALSE);
242 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
244 inc_dialog->mainwin = mainwin;
245 inc_progress_dialog_set_list(inc_dialog);
248 toolbar_main_set_sensitive(mainwin);
249 main_window_set_menu_sensitive(mainwin);
252 return inc_start(inc_dialog);
255 return inc_spool_account(account);
263 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
267 if (inc_lock_count) return 0;
269 if (prefs_common.work_offline)
270 if (alertpanel(_("Offline warning"),
271 _("You're working offline. Override?"),
272 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
275 inc_autocheck_timer_remove();
276 main_window_lock(mainwin);
278 new_msgs = inc_account_mail_real(mainwin, account);
280 inc_finished(mainwin, new_msgs > 0);
281 main_window_unlock(mainwin);
282 inc_autocheck_timer_set();
287 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
290 GList *list, *queue_list = NULL;
291 IncProgressDialog *inc_dialog;
293 gint account_new_msgs = 0;
295 if (prefs_common.work_offline)
296 if (alertpanel(_("Offline warning"),
297 _("You're working offline. Override?"),
298 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
301 if (inc_lock_count) return;
303 inc_autocheck_timer_remove();
304 main_window_lock(mainwin);
306 list = account_get_list();
308 inc_finished(mainwin, new_msgs > 0);
309 main_window_unlock(mainwin);
310 inc_notify_cmd(new_msgs, notify);
311 inc_autocheck_timer_set();
315 /* check local folders */
316 account_new_msgs = inc_all_spool();
317 if (account_new_msgs > 0)
318 new_msgs += account_new_msgs;
320 /* check IMAP4 folders */
321 for (; list != NULL; list = list->next) {
322 PrefsAccount *account = list->data;
323 if ((account->protocol == A_IMAP4 ||
324 account->protocol == A_NNTP) && account->recv_at_getall) {
325 new_msgs += folderview_check_new(FOLDER(account->folder));
329 /* check POP3 accounts */
330 for (list = account_get_list(); list != NULL; list = list->next) {
332 PrefsAccount *account = list->data;
334 if (account->recv_at_getall) {
335 session = inc_session_new(account);
337 queue_list = g_list_append(queue_list, session);
342 inc_finished(mainwin, new_msgs > 0);
343 main_window_unlock(mainwin);
344 inc_notify_cmd(new_msgs, notify);
345 inc_autocheck_timer_set();
349 inc_dialog = inc_progress_dialog_create(autocheck);
350 inc_dialog->queue_list = queue_list;
351 inc_dialog->mainwin = mainwin;
352 inc_progress_dialog_set_list(inc_dialog);
354 toolbar_main_set_sensitive(mainwin);
355 main_window_set_menu_sensitive(mainwin);
357 new_msgs += inc_start(inc_dialog);
358 inc_finished(mainwin, new_msgs > 0);
359 main_window_unlock(mainwin);
360 inc_notify_cmd(new_msgs, notify);
361 inc_autocheck_timer_set();
364 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
366 IncProgressDialog *dialog;
367 ProgressDialog *progress;
369 dialog = g_new0(IncProgressDialog, 1);
371 progress = progress_dialog_create();
372 gtk_window_set_title(GTK_WINDOW(progress->window),
373 _("Retrieving new messages"));
374 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
375 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
376 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
377 GTK_SIGNAL_FUNC(inc_dialog_delete_cb), dialog);
378 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
380 progress_dialog_set_value(progress, 0.0);
382 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
384 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
385 ¤txpm, ¤txpmmask);
386 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
387 &errorxpm, &errorxpmmask);
389 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
390 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
392 dialog->show_dialog = TRUE;
393 gtk_widget_show_now(progress->window);
396 dialog->dialog = progress;
397 dialog->progress_timer_id = 0;
398 dialog->folder_timer_id = 0;
399 dialog->queue_list = NULL;
402 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
407 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
411 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
412 IncSession *session = list->data;
413 Pop3Session *pop3_session = POP3_SESSION(session->session);
416 session->data = inc_dialog;
419 text[1] = pop3_session->ac_prefs->account_name;
420 text[2] = _("Standby");
421 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
425 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
427 progress_dialog_set_value(inc_dialog->dialog, 0.0);
428 progress_dialog_set_label(inc_dialog->dialog, "");
429 if (inc_dialog->mainwin)
430 main_window_progress_off(inc_dialog->mainwin);
433 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
435 g_return_if_fail(inc_dialog != NULL);
437 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
439 if (inc_dialog->mainwin)
440 main_window_progress_off(inc_dialog->mainwin);
441 progress_dialog_destroy(inc_dialog->dialog);
446 static IncSession *inc_session_new(PrefsAccount *account)
450 g_return_val_if_fail(account != NULL, NULL);
452 if (account->protocol != A_POP3 && account->protocol != A_APOP)
454 if (!account->recv_server || !account->userid)
457 session = g_new0(IncSession, 1);
459 session->session = pop3_session_new(account);
460 session->session->data = session;
461 POP3_SESSION(session->session)->drop_message = inc_drop_message;
462 session_set_recv_message_notify(session->session,
463 inc_recv_message, session);
464 session_set_recv_data_progressive_notify(session->session,
465 inc_recv_data_progressive,
467 session_set_recv_data_notify(session->session,
468 inc_recv_data_finished, session);
470 session->folder_table = g_hash_table_new(NULL, NULL);
471 session->tmp_folder_table = g_hash_table_new(NULL, NULL);
476 static void inc_session_destroy(IncSession *session)
478 g_return_if_fail(session != NULL);
480 session_destroy(session->session);
481 g_hash_table_destroy(session->folder_table);
482 g_hash_table_destroy(session->tmp_folder_table);
486 static gint inc_start(IncProgressDialog *inc_dialog)
489 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
491 Pop3Session *pop3_session;
497 FolderItem *processing, *inbox;
499 GSList *msglist, *msglist_element;
501 qlist = inc_dialog->queue_list;
502 while (qlist != NULL) {
503 GList *next = qlist->next;
505 session = qlist->data;
506 pop3_session = POP3_SESSION(session->session);
507 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
508 if (pop3_session->ac_prefs->passwd)
510 g_strdup(pop3_session->ac_prefs->passwd);
511 else if (pop3_session->ac_prefs->tmp_pass)
513 g_strdup(pop3_session->ac_prefs->tmp_pass);
517 if (inc_dialog->show_dialog)
518 manage_window_focus_in
519 (inc_dialog->dialog->window,
522 pass = input_dialog_query_password
523 (pop3_session->ac_prefs->recv_server,
526 if (inc_dialog->show_dialog)
527 manage_window_focus_out
528 (inc_dialog->dialog->window,
532 pop3_session->ac_prefs->tmp_pass =
534 pop3_session->pass = pass;
541 #define SET_PIXMAP_AND_TEXT(xpm, xpmmask, str) \
543 gtk_clist_set_pixmap(clist, inc_dialog->cur_row, 0, xpm, xpmmask); \
544 gtk_clist_set_text(clist, inc_dialog->cur_row, 2, str); \
547 for (; inc_dialog->queue_list != NULL; inc_dialog->cur_row++) {
548 session = inc_dialog->queue_list->data;
549 pop3_session = POP3_SESSION(session->session);
551 if (pop3_session->pass == NULL) {
552 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
553 inc_session_destroy(session);
554 inc_dialog->queue_list =
555 g_list_remove(inc_dialog->queue_list, session);
559 inc_progress_dialog_clear(inc_dialog);
560 gtk_clist_moveto(clist, inc_dialog->cur_row, -1, 1.0, 0.0);
562 SET_PIXMAP_AND_TEXT(currentxpm, currentxpmmask,
565 /* begin POP3 session */
566 inc_state = inc_pop3_session_do(session);
570 if (pop3_session->cur_total_num > 0)
571 msg = g_strdup_printf
572 (_("Done (%d message(s) (%s) received)"),
573 pop3_session->cur_total_num,
574 to_human_readable(pop3_session->cur_total_recv_bytes));
576 msg = g_strdup_printf(_("Done (no new messages)"));
577 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, msg);
580 case INC_CONNECT_ERROR:
581 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
582 _("Connection failed"));
584 case INC_AUTH_FAILED:
585 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
589 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
595 case INC_SOCKET_ERROR:
597 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask, _("Error"));
600 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
606 if (pop3_session->error_val == PS_AUTHFAIL) {
607 if(!prefs_common.no_recv_err_panel) {
608 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
609 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
610 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
614 /* CLAWS: perform filtering actions on dropped message */
615 /* CLAWS: get default inbox (perhaps per account) */
616 if (pop3_session->ac_prefs->inbox) {
617 /* CLAWS: get destination folder / mailbox */
618 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
620 inbox = folder_get_default_inbox();
622 inbox = folder_get_default_inbox();
624 /* get list of messages in processing */
625 processing = folder_get_default_processing();
626 folder_item_scan(processing);
627 msglist = folder_item_get_msg_list(processing);
629 /* process messages */
630 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
631 msginfo = (MsgInfo *) msglist_element->data;
632 if (!pop3_session->ac_prefs->filter_on_recv || !procmsg_msginfo_filter(msginfo))
633 folder_item_move_msg(inbox, msginfo);
634 procmsg_msginfo_free(msginfo);
636 g_slist_free(msglist);
640 new_msgs += pop3_session->cur_total_num;
642 if (pop3_session->error_val == PS_AUTHFAIL &&
643 pop3_session->ac_prefs->tmp_pass) {
644 g_free(pop3_session->ac_prefs->tmp_pass);
645 pop3_session->ac_prefs->tmp_pass = NULL;
648 pop3_write_uidl_list(pop3_session);
650 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
652 if (inc_dialog->show_dialog)
653 manage_window_focus_in
654 (inc_dialog->dialog->window,
656 inc_put_error(inc_state, pop3_session->error_msg);
657 if (inc_dialog->show_dialog)
658 manage_window_focus_out
659 (inc_dialog->dialog->window,
661 if (inc_state == INC_NO_SPACE ||
662 inc_state == INC_IO_ERROR)
666 inc_session_destroy(session);
667 inc_dialog->queue_list =
668 g_list_remove(inc_dialog->queue_list, session);
671 #undef SET_PIXMAP_AND_TEXT
674 fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
677 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
679 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
682 if (error_num && !prefs_common.no_recv_err_panel) {
683 if (inc_dialog->show_dialog)
684 manage_window_focus_in(inc_dialog->dialog->window,
686 alertpanel_error_log(_("Some errors occurred while getting mail."));
687 if (inc_dialog->show_dialog)
688 manage_window_focus_out(inc_dialog->dialog->window,
693 while (inc_dialog->queue_list != NULL) {
694 session = inc_dialog->queue_list->data;
695 inc_session_destroy(session);
696 inc_dialog->queue_list =
697 g_list_remove(inc_dialog->queue_list, session);
700 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
701 inc_progress_dialog_destroy(inc_dialog);
703 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
705 gtk_label_set_text(GTK_LABEL(GTK_BIN(inc_dialog->dialog->cancel_btn)->child),
714 static IncState inc_pop3_session_do(IncSession *session)
716 Pop3Session *pop3_session = POP3_SESSION(session->session);
717 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
722 debug_print("getting new messages of account %s...\n",
723 pop3_session->ac_prefs->account_name);
725 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
727 buf = g_strdup_printf(_("%s: Retrieving new messages"),
728 pop3_session->ac_prefs->recv_server);
729 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
732 server = pop3_session->ac_prefs->recv_server;
734 port = pop3_session->ac_prefs->set_popport ?
735 pop3_session->ac_prefs->popport :
736 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
737 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
738 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
739 SESSION(pop3_session)->nonblocking =
740 pop3_session->ac_prefs->use_nonblocking_ssl;
742 port = pop3_session->ac_prefs->set_popport ?
743 pop3_session->ac_prefs->popport : 110;
746 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
747 log_message("%s\n", buf);
749 progress_dialog_set_label(inc_dialog->dialog, buf);
752 if (session_connect(SESSION(pop3_session), server, port) < 0) {
753 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
755 if(!prefs_common.no_recv_err_panel) {
756 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
757 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
758 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
760 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
762 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
764 session->inc_state = INC_CONNECT_ERROR;
766 return INC_CONNECT_ERROR;
769 while (session_is_connected(SESSION(pop3_session)) &&
770 session->inc_state != INC_CANCEL)
771 gtk_main_iteration();
773 inc_timer_stop(inc_dialog);
776 if (session->inc_state == INC_SUCCESS) {
777 switch (pop3_session->error_val) {
779 if (SESSION(pop3_session)->state == SESSION_ERROR) {
780 if (pop3_session->state == POP3_READY)
781 session->inc_state = INC_CONNECT_ERROR;
783 session->inc_state = INC_ERROR;
784 } else if (SESSION(pop3_session)->state == SESSION_EOF)
785 session->inc_state = INC_EOF;
787 session->inc_state = INC_SUCCESS;
790 session->inc_state = INC_AUTH_FAILED;
793 session->inc_state = INC_IO_ERROR;
796 session->inc_state = INC_SOCKET_ERROR;
799 session->inc_state = INC_LOCKED;
802 session->inc_state = INC_ERROR;
807 return session->inc_state;
810 static void inc_timer_start(IncProgressDialog *inc_dialog,
811 IncSession *inc_session)
813 if (inc_dialog->progress_timer_id == 0) {
814 inc_dialog->progress_timer_id =
815 gtk_timeout_add(PROGRESS_UPDATE_INTERVAL,
816 inc_progress_timer_func, inc_session);
818 if (inc_dialog->folder_timer_id == 0) {
819 inc_dialog->folder_timer_id =
820 gtk_timeout_add(FOLDER_UPDATE_INTERVAL,
821 inc_folder_timer_func, inc_session);
825 static void inc_timer_stop(IncProgressDialog *inc_dialog)
827 if (inc_dialog->progress_timer_id) {
828 gtk_timeout_remove(inc_dialog->progress_timer_id);
829 inc_dialog->progress_timer_id = 0;
831 if (inc_dialog->folder_timer_id) {
832 gtk_timeout_remove(inc_dialog->folder_timer_id);
833 inc_dialog->folder_timer_id = 0;
837 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
838 IncSession *inc_session)
840 inc_progress_dialog_set_label(inc_dialog, inc_session);
841 inc_progress_dialog_set_progress(inc_dialog, inc_session);
844 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
845 IncSession *inc_session)
847 gchar buf[MSGBUFSIZE];
848 ProgressDialog *dialog = inc_dialog->dialog;
849 Pop3Session *session;
851 g_return_if_fail(inc_session != NULL);
853 session = POP3_SESSION(inc_session->session);
855 switch (session->state) {
858 case POP3_GETAUTH_USER:
859 case POP3_GETAUTH_PASS:
860 case POP3_GETAUTH_APOP:
861 progress_dialog_set_label(dialog, _("Authenticating..."));
862 statusbar_print_all(_("Retrieving messages from %s..."),
863 SESSION(session)->server);
865 case POP3_GETRANGE_STAT:
866 progress_dialog_set_label
867 (dialog, _("Getting the number of new messages (STAT)..."));
869 case POP3_GETRANGE_LAST:
870 progress_dialog_set_label
871 (dialog, _("Getting the number of new messages (LAST)..."));
873 case POP3_GETRANGE_UIDL:
874 progress_dialog_set_label
875 (dialog, _("Getting the number of new messages (UIDL)..."));
877 case POP3_GETSIZE_LIST:
878 progress_dialog_set_label
879 (dialog, _("Getting the size of messages (LIST)..."));
885 if (session->msg[session->cur_msg].recv_time <
886 session->current_time) {
887 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
889 progress_dialog_set_label(dialog, buf);
893 progress_dialog_set_label(dialog, _("Quitting"));
900 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
901 IncSession *inc_session)
903 gchar buf[MSGBUFSIZE];
904 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
905 gchar *total_size_str;
909 if (!pop3_session->new_msg_exist) return;
911 cur_total = inc_session->cur_total_bytes;
912 total = pop3_session->total_bytes;
913 if (pop3_session->state == POP3_RETR ||
914 pop3_session->state == POP3_RETR_RECV) {
915 Xstrdup_a(total_size_str, to_human_readable(total), return);
916 g_snprintf(buf, sizeof(buf),
917 _("Retrieving message (%d / %d) (%s / %s)"),
918 pop3_session->cur_msg, pop3_session->count,
919 to_human_readable(cur_total), total_size_str);
920 progress_dialog_set_label(inc_dialog->dialog, buf);
923 progress_dialog_set_percentage
924 (inc_dialog->dialog,(gfloat)cur_total / (gfloat)total);
926 gtk_progress_set_show_text
927 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), TRUE);
928 g_snprintf(buf, sizeof(buf), "%d / %d",
929 pop3_session->cur_msg, pop3_session->count);
930 gtk_progress_set_format_string
931 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), buf);
932 gtk_progress_bar_update
933 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
934 (gfloat)cur_total / (gfloat)total);
936 if (pop3_session->cur_total_num > 0) {
937 g_snprintf(buf, sizeof(buf),
938 _("Retrieving (%d message(s) (%s) received)"),
939 pop3_session->cur_total_num,
941 (pop3_session->cur_total_recv_bytes));
942 gtk_clist_set_text(GTK_CLIST(inc_dialog->dialog->clist),
943 inc_dialog->cur_row, 2, buf);
947 static gboolean hash_remove_func(gpointer key, gpointer value, gpointer data)
952 static gint inc_progress_timer_func(gpointer data)
954 IncSession *inc_session = (IncSession *)data;
955 IncProgressDialog *inc_dialog;
957 inc_dialog = (IncProgressDialog *)inc_session->data;
959 inc_progress_dialog_update(inc_dialog, inc_session);
964 static gint inc_folder_timer_func(gpointer data)
966 IncSession *inc_session = (IncSession *)data;
967 IncProgressDialog *inc_dialog;
969 inc_dialog = (IncProgressDialog *)inc_session->data;
971 if (g_hash_table_size(inc_session->tmp_folder_table) > 0) {
972 folderview_update_item_foreach(inc_session->tmp_folder_table,
974 g_hash_table_foreach_remove(inc_session->tmp_folder_table,
975 hash_remove_func, NULL);
981 static gint inc_recv_data_progressive(Session *session, guint cur_len,
982 guint total_len, gpointer data)
984 IncSession *inc_session = (IncSession *)data;
985 Pop3Session *pop3_session = POP3_SESSION(session);
988 g_return_val_if_fail(inc_session != NULL, -1);
990 if (pop3_session->state != POP3_RETR &&
991 pop3_session->state != POP3_RETR_RECV &&
992 pop3_session->state != POP3_DELETE &&
993 pop3_session->state != POP3_LOGOUT) return 0;
995 if (!pop3_session->new_msg_exist) return 0;
997 cur_total = pop3_session->cur_total_bytes + cur_len;
998 if (cur_total > pop3_session->total_bytes)
999 cur_total = pop3_session->total_bytes;
1000 inc_session->cur_total_bytes = cur_total;
1005 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1007 IncSession *inc_session = (IncSession *)data;
1008 IncProgressDialog *inc_dialog;
1010 g_return_val_if_fail(inc_session != NULL, -1);
1012 inc_dialog = (IncProgressDialog *)inc_session->data;
1013 inc_recv_data_progressive(session, 0, 0, inc_session);
1015 if (POP3_SESSION(session)->state == POP3_RETR) {
1016 if (inc_dialog->progress_timer_id == 0) {
1017 inc_timer_start(inc_dialog, inc_session);
1018 inc_progress_dialog_update(inc_dialog, inc_session);
1020 } else if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1021 inc_progress_dialog_update(inc_dialog, inc_session);
1022 if (inc_dialog->progress_timer_id) {
1023 inc_folder_timer_func(data);
1024 inc_timer_stop(inc_dialog);
1031 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1033 IncSession *inc_session = (IncSession *)data;
1034 IncProgressDialog *inc_dialog;
1036 g_return_val_if_fail(inc_session != NULL, -1);
1038 inc_dialog = (IncProgressDialog *)inc_session->data;
1040 switch (POP3_SESSION(session)->state) {
1041 case POP3_GETAUTH_USER:
1042 case POP3_GETAUTH_PASS:
1043 case POP3_GETAUTH_APOP:
1044 case POP3_GETRANGE_STAT:
1045 case POP3_GETRANGE_LAST:
1046 case POP3_GETRANGE_UIDL:
1047 case POP3_GETSIZE_LIST:
1048 inc_progress_dialog_update(inc_dialog, inc_session);
1051 inc_recv_data_progressive(session, 0, 0, inc_session);
1052 if (inc_dialog->progress_timer_id == 0) {
1053 inc_timer_start(inc_dialog, inc_session);
1054 inc_progress_dialog_update(inc_dialog, inc_session);
1058 inc_progress_dialog_update(inc_dialog, inc_session);
1059 if (inc_dialog->progress_timer_id) {
1060 inc_folder_timer_func(data);
1061 inc_timer_stop(inc_dialog);
1071 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1074 FolderItem *dropfolder;
1075 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1076 IncProgressDialog *inc_dialog;
1080 g_return_val_if_fail(inc_session != NULL, -1);
1082 if (session->ac_prefs->inbox) {
1083 inbox = folder_find_item_from_identifier
1084 (session->ac_prefs->inbox);
1086 inbox = folder_get_default_inbox();
1088 inbox = folder_get_default_inbox();
1094 /* CLAWS: claws uses a global .processing folder for the filtering. */
1095 dropfolder = folder_get_default_processing();
1097 /* add msg file to drop folder */
1098 if ((msgnum = folder_item_add_msg(dropfolder, file, NULL, TRUE)) < 0) {
1103 inc_dialog = (IncProgressDialog *)inc_session->data;
1108 static void inc_put_error(IncState istate, const gchar *msg)
1110 gchar *log_msg = NULL;
1111 gchar *err_msg = NULL;
1112 gboolean fatal_error = FALSE;
1115 case INC_CONNECT_ERROR:
1116 log_msg = _("Connection failed.");
1117 if (prefs_common.no_recv_err_panel)
1119 err_msg = g_strdup(log_msg);
1122 log_msg = _("Error occurred while processing mail.");
1123 if (prefs_common.no_recv_err_panel)
1126 err_msg = g_strdup_printf
1127 (_("Error occurred while processing mail:\n%s"),
1130 err_msg = g_strdup(log_msg);
1133 log_msg = _("No disk space left.");
1134 err_msg = g_strdup(log_msg);
1138 log_msg = _("Can't write file.");
1139 err_msg = g_strdup(log_msg);
1142 case INC_SOCKET_ERROR:
1143 log_msg = _("Socket error.");
1144 if (prefs_common.no_recv_err_panel)
1146 err_msg = g_strdup(log_msg);
1149 log_msg = _("Connection closed by the remote host.");
1150 if (prefs_common.no_recv_err_panel)
1152 err_msg = g_strdup(log_msg);
1155 log_msg = _("Mailbox is locked.");
1156 if (prefs_common.no_recv_err_panel)
1159 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1162 err_msg = g_strdup(log_msg);
1164 case INC_AUTH_FAILED:
1165 log_msg = _("Authentication failed.");
1166 if (prefs_common.no_recv_err_panel)
1169 err_msg = g_strdup_printf
1170 (_("Authentication failed:\n%s"), msg);
1172 err_msg = g_strdup(log_msg);
1180 log_error("%s\n", log_msg);
1182 log_warning("%s\n", log_msg);
1185 alertpanel_error_log(err_msg);
1190 static void inc_cancel(IncProgressDialog *dialog)
1192 IncSession *session;
1194 g_return_if_fail(dialog != NULL);
1196 if (dialog->queue_list == NULL) {
1197 inc_progress_dialog_destroy(dialog);
1201 session = dialog->queue_list->data;
1203 session->inc_state = INC_CANCEL;
1205 log_message(_("Incorporation cancelled\n"));
1208 gboolean inc_is_active(void)
1210 return (inc_dialog_list != NULL);
1213 void inc_cancel_all(void)
1217 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1218 inc_cancel((IncProgressDialog *)cur->data);
1221 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1223 inc_cancel((IncProgressDialog *)data);
1226 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1229 IncProgressDialog *dialog = (IncProgressDialog *)data;
1231 if (dialog->queue_list == NULL)
1232 inc_progress_dialog_destroy(dialog);
1237 static gint inc_spool_account(PrefsAccount *account)
1240 gchar *mbox, *logname;
1243 logname = g_get_user_name();
1245 if (account->inbox) {
1246 inbox = folder_find_item_from_path(account->inbox);
1248 inbox = folder_get_default_inbox();
1250 inbox = folder_get_default_inbox();
1252 if (is_file_exist(account->local_mbox))
1253 mbox = g_strdup(account->local_mbox);
1255 mbox = g_strconcat(account->local_mbox,
1256 G_DIR_SEPARATOR_S, logname, NULL);
1258 result = get_spool(inbox, mbox);
1261 statusbar_pop_all();
1266 static gint inc_all_spool(void)
1270 gint account_new_msgs = 0;
1272 list = account_get_list();
1273 if (!list) return 0;
1275 for (; list != NULL; list = list->next) {
1276 PrefsAccount *account = list->data;
1278 if ((account->protocol == A_LOCAL) &&
1279 (account->recv_at_getall)) {
1280 account_new_msgs = inc_spool_account(account);
1281 if (account_new_msgs > 0)
1282 new_msgs += account_new_msgs;
1289 static gint get_spool(FolderItem *dest, const gchar *mbox)
1293 gchar tmp_mbox[MAXPATHLEN + 1];
1295 g_return_val_if_fail(dest != NULL, -1);
1296 g_return_val_if_fail(mbox != NULL, -1);
1298 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1299 debug_print("no messages in local mailbox.\n");
1301 } else if (size < 0)
1304 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1307 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1308 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1310 if (copy_mbox(mbox, tmp_mbox) < 0) {
1311 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1315 debug_print("Getting new messages from %s into %s...\n",
1318 msgs = proc_mbox(dest, tmp_mbox, TRUE);
1321 if (msgs >= 0) empty_mbox(mbox);
1322 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1332 void inc_unlock(void)
1334 if (inc_lock_count > 0)
1338 static guint autocheck_timer = 0;
1339 static gpointer autocheck_data = NULL;
1341 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1346 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1347 *prefs_common.newmail_notify_cmd))
1349 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1350 buf[1] == 'd' && !strchr(&buf[1], '%'))
1351 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1354 buf = g_strdup(prefs_common.newmail_notify_cmd);
1356 execute_command_line(buf, TRUE);
1361 void inc_autocheck_timer_init(MainWindow *mainwin)
1363 autocheck_data = mainwin;
1364 inc_autocheck_timer_set();
1367 static void inc_autocheck_timer_set_interval(guint interval)
1369 inc_autocheck_timer_remove();
1370 /* last test is to avoid re-enabling auto_check after modifying
1371 the common preferences */
1372 if (prefs_common.autochk_newmail && autocheck_data
1373 && prefs_common.work_offline == FALSE) {
1374 autocheck_timer = gtk_timeout_add
1375 (interval, inc_autocheck_func, autocheck_data);
1376 debug_print("added timer = %d\n", autocheck_timer);
1380 void inc_autocheck_timer_set(void)
1382 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1385 void inc_autocheck_timer_remove(void)
1387 if (autocheck_timer) {
1388 debug_print("removed timer = %d\n", autocheck_timer);
1389 gtk_timeout_remove(autocheck_timer);
1390 autocheck_timer = 0;
1394 static gint inc_autocheck_func(gpointer data)
1396 MainWindow *mainwin = (MainWindow *)data;
1398 if (inc_lock_count) {
1399 debug_print("autocheck is locked.\n");
1400 inc_autocheck_timer_set_interval(1000);
1404 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);