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>
36 #include <sys/types.h>
45 #include "mainwindow.h"
46 #include "folderview.h"
47 #include "summaryview.h"
48 #include "prefs_common.h"
49 #include "prefs_account.h"
59 #include "statusbar.h"
60 #include "manage_window.h"
61 #include "stock_pixmap.h"
62 #include "progressdialog.h"
63 #include "inputdialog.h"
64 #include "alertpanel.h"
66 #include "filtering.h"
70 static GList *inc_dialog_list = NULL;
72 static guint inc_lock_count = 0;
74 static GdkPixmap *currentxpm;
75 static GdkBitmap *currentxpmmask;
76 static GdkPixmap *errorxpm;
77 static GdkBitmap *errorxpmmask;
78 static GdkPixmap *okxpm;
79 static GdkBitmap *okxpmmask;
81 #define MSGBUFSIZE 8192
83 static void inc_finished (MainWindow *mainwin,
84 gboolean new_messages);
85 static gint inc_account_mail_real (MainWindow *mainwin,
86 PrefsAccount *account);
88 static IncProgressDialog *inc_progress_dialog_create
90 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
91 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
93 static IncSession *inc_session_new (PrefsAccount *account);
94 static void inc_session_destroy (IncSession *session);
95 static gint inc_start (IncProgressDialog *inc_dialog);
96 static IncState inc_pop3_session_do (IncSession *session);
98 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
99 IncSession *inc_session);
101 static void inc_progress_dialog_set_label
102 (IncProgressDialog *inc_dialog,
103 IncSession *inc_session);
104 static void inc_progress_dialog_set_progress
105 (IncProgressDialog *inc_dialog,
106 IncSession *inc_session);
108 static void inc_progress_dialog_update_periodic
109 (IncProgressDialog *inc_dialog,
110 IncSession *inc_session);
112 static gint inc_recv_data_progressive (Session *session,
116 static gint inc_recv_data_finished (Session *session,
119 static gint inc_recv_message (Session *session,
122 static gint inc_drop_message (Pop3Session *session,
125 static void inc_put_error (IncState istate,
128 static void inc_cancel_cb (GtkWidget *widget,
130 static gint inc_dialog_delete_cb (GtkWidget *widget,
134 static gint get_spool (FolderItem *dest,
137 static gint inc_spool_account(PrefsAccount *account);
138 static gint inc_all_spool(void);
139 static void inc_autocheck_timer_set_interval (guint interval);
140 static gint inc_autocheck_func (gpointer data);
142 static void inc_notify_cmd (gint new_msgs,
147 * @mainwin: Main window.
148 * @new_messages: TRUE if some messages have been received.
150 * Update the folder view and the summary view after receiving
151 * messages. If @new_messages is FALSE, this function avoids unneeded
154 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
158 if (prefs_common.scan_all_after_inc)
159 folderview_check_new(NULL);
161 if (!new_messages && !prefs_common.scan_all_after_inc) return;
163 if (prefs_common.open_inbox_on_inc) {
164 item = cur_account && cur_account->inbox
165 ? folder_find_item_from_identifier(cur_account->inbox)
166 : folder_get_default_inbox();
167 folderview_unselect(mainwin->folderview);
168 folderview_select(mainwin->folderview, item);
172 void inc_mail(MainWindow *mainwin, gboolean notify)
175 gint account_new_msgs = 0;
177 if (inc_lock_count) return;
179 if (prefs_common.work_offline)
180 if (alertpanel(_("Offline warning"),
181 _("You're working offline. Override?"),
182 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
186 inc_autocheck_timer_remove();
187 main_window_lock(mainwin);
189 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
190 /* external incorporating program */
191 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
192 main_window_unlock(mainwin);
193 inc_autocheck_timer_set();
197 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
198 if (account_new_msgs > 0)
199 new_msgs += account_new_msgs;
202 inc_finished(mainwin, new_msgs > 0);
203 main_window_unlock(mainwin);
204 inc_notify_cmd(new_msgs, notify);
205 inc_autocheck_timer_set();
209 void inc_pop_before_smtp(PrefsAccount *acc)
211 /* FIXME: assumes to attach to first main window */
212 inc_account_mail(mainwindow_get_mainwindow(), acc);
215 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
217 IncProgressDialog *inc_dialog;
219 FolderItem *item = NULL;
221 if(mainwin && mainwin->summaryview)
222 item = mainwin->summaryview->folder_item;
224 switch (account->protocol) {
228 * FIXME: it should return foldeview_check_new() value.
229 * TODO: do it when bug [19] is fixed (IMAP folder sets
230 * an incorrect new message count)
232 folderview_check_new(FOLDER(account->folder));
236 session = inc_session_new(account);
237 if (!session) return 0;
239 inc_dialog = inc_progress_dialog_create(FALSE);
240 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
242 inc_dialog->mainwin = mainwin;
243 inc_progress_dialog_set_list(inc_dialog);
246 toolbar_main_set_sensitive(mainwin);
247 main_window_set_menu_sensitive(mainwin);
250 return inc_start(inc_dialog);
253 return inc_spool_account(account);
261 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
265 if (inc_lock_count) return 0;
267 if (prefs_common.work_offline)
268 if (alertpanel(_("Offline warning"),
269 _("You're working offline. Override?"),
270 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
273 inc_autocheck_timer_remove();
274 main_window_lock(mainwin);
276 new_msgs = inc_account_mail_real(mainwin, account);
278 inc_finished(mainwin, new_msgs > 0);
279 main_window_unlock(mainwin);
280 inc_autocheck_timer_set();
285 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
288 GList *list, *queue_list = NULL;
289 IncProgressDialog *inc_dialog;
291 gint account_new_msgs = 0;
293 if (prefs_common.work_offline)
294 if (alertpanel(_("Offline warning"),
295 _("You're working offline. Override?"),
296 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
299 if (inc_lock_count) return;
301 inc_autocheck_timer_remove();
302 main_window_lock(mainwin);
304 list = account_get_list();
306 inc_finished(mainwin, new_msgs > 0);
307 main_window_unlock(mainwin);
308 inc_notify_cmd(new_msgs, notify);
309 inc_autocheck_timer_set();
313 /* check local folders */
314 account_new_msgs = inc_all_spool();
315 if (account_new_msgs > 0)
316 new_msgs += account_new_msgs;
318 /* check IMAP4 folders */
319 for (; list != NULL; list = list->next) {
320 PrefsAccount *account = list->data;
321 if ((account->protocol == A_IMAP4 ||
322 account->protocol == A_NNTP) && account->recv_at_getall) {
323 new_msgs += folderview_check_new(FOLDER(account->folder));
327 /* check POP3 accounts */
328 for (list = account_get_list(); list != NULL; list = list->next) {
330 PrefsAccount *account = list->data;
332 if (account->recv_at_getall) {
333 session = inc_session_new(account);
335 queue_list = g_list_append(queue_list, session);
340 inc_finished(mainwin, new_msgs > 0);
341 main_window_unlock(mainwin);
342 inc_notify_cmd(new_msgs, notify);
343 inc_autocheck_timer_set();
347 inc_dialog = inc_progress_dialog_create(autocheck);
348 inc_dialog->queue_list = queue_list;
349 inc_dialog->mainwin = mainwin;
350 inc_progress_dialog_set_list(inc_dialog);
352 toolbar_main_set_sensitive(mainwin);
353 main_window_set_menu_sensitive(mainwin);
355 new_msgs += inc_start(inc_dialog);
356 inc_finished(mainwin, new_msgs > 0);
357 main_window_unlock(mainwin);
358 inc_notify_cmd(new_msgs, notify);
359 inc_autocheck_timer_set();
362 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
364 IncProgressDialog *dialog;
365 ProgressDialog *progress;
367 dialog = g_new0(IncProgressDialog, 1);
369 progress = progress_dialog_create();
370 gtk_window_set_title(GTK_WINDOW(progress->window),
371 _("Retrieving new messages"));
372 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
373 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
374 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
375 GTK_SIGNAL_FUNC(inc_dialog_delete_cb), dialog);
376 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
378 progress_dialog_set_value(progress, 0.0);
380 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
382 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
383 ¤txpm, ¤txpmmask);
384 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
385 &errorxpm, &errorxpmmask);
387 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
388 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
390 dialog->show_dialog = TRUE;
391 gtk_widget_show_now(progress->window);
394 dialog->dialog = progress;
395 gettimeofday(&dialog->progress_tv, NULL);
396 gettimeofday(&dialog->folder_tv, NULL);
397 dialog->queue_list = NULL;
400 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
405 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
409 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
410 IncSession *session = list->data;
411 Pop3Session *pop3_session = POP3_SESSION(session->session);
414 session->data = inc_dialog;
417 text[1] = pop3_session->ac_prefs->account_name;
418 text[2] = _("Standby");
419 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
423 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
425 progress_dialog_set_value(inc_dialog->dialog, 0.0);
426 progress_dialog_set_label(inc_dialog->dialog, "");
427 if (inc_dialog->mainwin)
428 main_window_progress_off(inc_dialog->mainwin);
431 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
433 g_return_if_fail(inc_dialog != NULL);
435 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
437 if (inc_dialog->mainwin)
438 main_window_progress_off(inc_dialog->mainwin);
439 progress_dialog_destroy(inc_dialog->dialog);
444 static IncSession *inc_session_new(PrefsAccount *account)
448 g_return_val_if_fail(account != NULL, NULL);
450 if (account->protocol != A_POP3 && account->protocol != A_APOP)
452 if (!account->recv_server || !account->userid)
455 session = g_new0(IncSession, 1);
457 session->session = pop3_session_new(account);
458 session->session->data = session;
459 POP3_SESSION(session->session)->drop_message = inc_drop_message;
460 session_set_recv_message_notify(session->session,
461 inc_recv_message, session);
462 session_set_recv_data_progressive_notify(session->session,
463 inc_recv_data_progressive,
465 session_set_recv_data_notify(session->session,
466 inc_recv_data_finished, session);
471 static void inc_session_destroy(IncSession *session)
473 g_return_if_fail(session != NULL);
475 session_destroy(session->session);
479 static gint inc_start(IncProgressDialog *inc_dialog)
482 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
484 Pop3Session *pop3_session;
490 FolderItem *processing, *inbox;
492 GSList *msglist, *msglist_element;
494 qlist = inc_dialog->queue_list;
495 while (qlist != NULL) {
496 GList *next = qlist->next;
498 session = qlist->data;
499 pop3_session = POP3_SESSION(session->session);
500 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
501 if (pop3_session->ac_prefs->passwd)
503 g_strdup(pop3_session->ac_prefs->passwd);
504 else if (pop3_session->ac_prefs->tmp_pass)
506 g_strdup(pop3_session->ac_prefs->tmp_pass);
510 if (inc_dialog->show_dialog)
511 manage_window_focus_in
512 (inc_dialog->dialog->window,
515 pass = input_dialog_query_password
516 (pop3_session->ac_prefs->recv_server,
519 if (inc_dialog->show_dialog)
520 manage_window_focus_out
521 (inc_dialog->dialog->window,
525 pop3_session->ac_prefs->tmp_pass =
527 pop3_session->pass = pass;
534 #define SET_PIXMAP_AND_TEXT(xpm, xpmmask, str) \
536 gtk_clist_set_pixmap(clist, inc_dialog->cur_row, 0, xpm, xpmmask); \
537 gtk_clist_set_text(clist, inc_dialog->cur_row, 2, str); \
540 for (; inc_dialog->queue_list != NULL; inc_dialog->cur_row++) {
541 session = inc_dialog->queue_list->data;
542 pop3_session = POP3_SESSION(session->session);
544 if (pop3_session->pass == NULL) {
545 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
546 inc_session_destroy(session);
547 inc_dialog->queue_list =
548 g_list_remove(inc_dialog->queue_list, session);
552 inc_progress_dialog_clear(inc_dialog);
553 gtk_clist_moveto(clist, inc_dialog->cur_row, -1, 1.0, 0.0);
555 SET_PIXMAP_AND_TEXT(currentxpm, currentxpmmask,
558 /* begin POP3 session */
559 inc_state = inc_pop3_session_do(session);
563 if (pop3_session->cur_total_num > 0)
564 msg = g_strdup_printf
565 (_("Done (%d message(s) (%s) received)"),
566 pop3_session->cur_total_num,
567 to_human_readable(pop3_session->cur_total_recv_bytes));
569 msg = g_strdup_printf(_("Done (no new messages)"));
570 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, msg);
573 case INC_CONNECT_ERROR:
574 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
575 _("Connection failed"));
577 case INC_AUTH_FAILED:
578 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
582 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
588 case INC_SOCKET_ERROR:
590 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask, _("Error"));
593 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
599 if (pop3_session->error_val == PS_AUTHFAIL) {
600 if(!prefs_common.no_recv_err_panel) {
601 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
602 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
603 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
607 /* CLAWS: perform filtering actions on dropped message */
608 /* CLAWS: get default inbox (perhaps per account) */
609 if (pop3_session->ac_prefs->inbox) {
610 /* CLAWS: get destination folder / mailbox */
611 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
613 inbox = folder_get_default_inbox();
615 inbox = folder_get_default_inbox();
617 /* get list of messages in processing */
618 processing = folder_get_default_processing();
619 folder_item_scan(processing);
620 msglist = folder_item_get_msg_list(processing);
622 /* process messages */
623 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
624 msginfo = (MsgInfo *) msglist_element->data;
625 if (!pop3_session->ac_prefs->filter_on_recv || !procmsg_msginfo_filter(msginfo))
626 folder_item_move_msg(inbox, msginfo);
627 procmsg_msginfo_free(msginfo);
629 g_slist_free(msglist);
633 new_msgs += pop3_session->cur_total_num;
635 if (pop3_session->error_val == PS_AUTHFAIL &&
636 pop3_session->ac_prefs->tmp_pass) {
637 g_free(pop3_session->ac_prefs->tmp_pass);
638 pop3_session->ac_prefs->tmp_pass = NULL;
641 pop3_write_uidl_list(pop3_session);
643 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
645 if (inc_dialog->show_dialog)
646 manage_window_focus_in
647 (inc_dialog->dialog->window,
649 inc_put_error(inc_state, pop3_session->error_msg);
650 if (inc_dialog->show_dialog)
651 manage_window_focus_out
652 (inc_dialog->dialog->window,
654 if (inc_state == INC_NO_SPACE ||
655 inc_state == INC_IO_ERROR)
659 inc_session_destroy(session);
660 inc_dialog->queue_list =
661 g_list_remove(inc_dialog->queue_list, session);
664 #undef SET_PIXMAP_AND_TEXT
667 fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
670 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
672 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
675 if (error_num && !prefs_common.no_recv_err_panel) {
676 if (inc_dialog->show_dialog)
677 manage_window_focus_in(inc_dialog->dialog->window,
679 alertpanel_error_log(_("Some errors occurred while getting mail."));
680 if (inc_dialog->show_dialog)
681 manage_window_focus_out(inc_dialog->dialog->window,
686 while (inc_dialog->queue_list != NULL) {
687 session = inc_dialog->queue_list->data;
688 inc_session_destroy(session);
689 inc_dialog->queue_list =
690 g_list_remove(inc_dialog->queue_list, session);
693 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
694 inc_progress_dialog_destroy(inc_dialog);
696 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
698 gtk_label_set_text(GTK_LABEL(GTK_BIN(inc_dialog->dialog->cancel_btn)->child),
707 static IncState inc_pop3_session_do(IncSession *session)
709 Pop3Session *pop3_session = POP3_SESSION(session->session);
710 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
715 debug_print("getting new messages of account %s...\n",
716 pop3_session->ac_prefs->account_name);
718 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
720 buf = g_strdup_printf(_("%s: Retrieving new messages"),
721 pop3_session->ac_prefs->recv_server);
722 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
725 server = pop3_session->ac_prefs->recv_server;
727 port = pop3_session->ac_prefs->set_popport ?
728 pop3_session->ac_prefs->popport :
729 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
730 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
731 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
732 SESSION(pop3_session)->nonblocking =
733 pop3_session->ac_prefs->use_nonblocking_ssl;
735 port = pop3_session->ac_prefs->set_popport ?
736 pop3_session->ac_prefs->popport : 110;
739 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
740 log_message("%s\n", buf);
742 progress_dialog_set_label(inc_dialog->dialog, buf);
745 if (session_connect(SESSION(pop3_session), server, port) < 0) {
746 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
748 if(!prefs_common.no_recv_err_panel) {
749 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
750 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
751 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
753 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
755 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
757 session->inc_state = INC_CONNECT_ERROR;
759 return INC_CONNECT_ERROR;
762 while (session_is_connected(SESSION(pop3_session)) &&
763 session->inc_state != INC_CANCEL)
764 gtk_main_iteration();
768 if (session->inc_state == INC_SUCCESS) {
769 switch (pop3_session->error_val) {
771 if (SESSION(pop3_session)->state == SESSION_ERROR) {
772 if (pop3_session->state == POP3_READY)
773 session->inc_state = INC_CONNECT_ERROR;
775 session->inc_state = INC_ERROR;
776 } else if (SESSION(pop3_session)->state == SESSION_EOF)
777 session->inc_state = INC_EOF;
779 session->inc_state = INC_SUCCESS;
782 session->inc_state = INC_AUTH_FAILED;
785 session->inc_state = INC_IO_ERROR;
788 session->inc_state = INC_SOCKET_ERROR;
791 session->inc_state = INC_LOCKED;
794 session->inc_state = INC_ERROR;
799 return session->inc_state;
802 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
803 IncSession *inc_session)
805 inc_progress_dialog_set_label(inc_dialog, inc_session);
806 inc_progress_dialog_set_progress(inc_dialog, inc_session);
809 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
810 IncSession *inc_session)
812 ProgressDialog *dialog = inc_dialog->dialog;
813 Pop3Session *session;
815 g_return_if_fail(inc_session != NULL);
817 session = POP3_SESSION(inc_session->session);
819 switch (session->state) {
822 case POP3_GETAUTH_USER:
823 case POP3_GETAUTH_PASS:
824 case POP3_GETAUTH_APOP:
825 progress_dialog_set_label(dialog, _("Authenticating..."));
826 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
827 SESSION(session)->server,
828 session->ac_prefs->account_name);
830 case POP3_GETRANGE_STAT:
831 progress_dialog_set_label
832 (dialog, _("Getting the number of new messages (STAT)..."));
834 case POP3_GETRANGE_LAST:
835 progress_dialog_set_label
836 (dialog, _("Getting the number of new messages (LAST)..."));
838 case POP3_GETRANGE_UIDL:
839 progress_dialog_set_label
840 (dialog, _("Getting the number of new messages (UIDL)..."));
842 case POP3_GETSIZE_LIST:
843 progress_dialog_set_label
844 (dialog, _("Getting the size of messages (LIST)..."));
851 if (session->msg[session->cur_msg].recv_time <
852 session->current_time) {
853 gchar buf[MSGBUFSIZE];
854 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
856 progress_dialog_set_label(dialog, buf);
861 progress_dialog_set_label(dialog, _("Quitting"));
868 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
869 IncSession *inc_session)
871 gchar buf[MSGBUFSIZE];
872 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
873 gchar *total_size_str;
877 if (!pop3_session->new_msg_exist) return;
879 cur_total = inc_session->cur_total_bytes;
880 total = pop3_session->total_bytes;
881 if (pop3_session->state == POP3_RETR ||
882 pop3_session->state == POP3_RETR_RECV ||
883 pop3_session->state == POP3_DELETE) {
884 Xstrdup_a(total_size_str, to_human_readable(total), return);
885 g_snprintf(buf, sizeof(buf),
886 _("Retrieving message (%d / %d) (%s / %s)"),
887 pop3_session->cur_msg, pop3_session->count,
888 to_human_readable(cur_total), total_size_str);
889 progress_dialog_set_label(inc_dialog->dialog, buf);
892 progress_dialog_set_percentage
893 (inc_dialog->dialog,(gfloat)cur_total / (gfloat)total);
895 gtk_progress_set_show_text
896 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), TRUE);
897 g_snprintf(buf, sizeof(buf), "%d / %d",
898 pop3_session->cur_msg, pop3_session->count);
899 gtk_progress_set_format_string
900 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), buf);
901 gtk_progress_bar_update
902 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
903 (gfloat)cur_total / (gfloat)total);
905 if (pop3_session->cur_total_num > 0) {
906 g_snprintf(buf, sizeof(buf),
907 _("Retrieving (%d message(s) (%s) received)"),
908 pop3_session->cur_total_num,
910 (pop3_session->cur_total_recv_bytes));
911 gtk_clist_set_text(GTK_CLIST(inc_dialog->dialog->clist),
912 inc_dialog->cur_row, 2, buf);
916 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
917 IncSession *inc_session)
919 struct timeval tv_cur;
920 struct timeval tv_result;
923 gettimeofday(&tv_cur, NULL);
925 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
926 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
927 if (tv_result.tv_usec < 0) {
929 tv_result.tv_usec += 1000000;
932 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
933 if (msec > PROGRESS_UPDATE_INTERVAL) {
934 inc_progress_dialog_update(inc_dialog, inc_session);
935 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
936 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
940 static gint inc_recv_data_progressive(Session *session, guint cur_len,
941 guint total_len, gpointer data)
943 IncSession *inc_session = (IncSession *)data;
944 Pop3Session *pop3_session = POP3_SESSION(session);
945 IncProgressDialog *inc_dialog;
948 g_return_val_if_fail(inc_session != NULL, -1);
950 if (pop3_session->state != POP3_RETR &&
951 pop3_session->state != POP3_RETR_RECV &&
952 pop3_session->state != POP3_DELETE &&
953 pop3_session->state != POP3_LOGOUT) return 0;
955 if (!pop3_session->new_msg_exist) return 0;
957 cur_total = pop3_session->cur_total_bytes + cur_len;
958 if (cur_total > pop3_session->total_bytes)
959 cur_total = pop3_session->total_bytes;
960 inc_session->cur_total_bytes = cur_total;
962 inc_dialog = (IncProgressDialog *)inc_session->data;
963 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
968 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
970 IncSession *inc_session = (IncSession *)data;
971 IncProgressDialog *inc_dialog;
973 g_return_val_if_fail(inc_session != NULL, -1);
975 inc_dialog = (IncProgressDialog *)inc_session->data;
977 inc_recv_data_progressive(session, 0, 0, inc_session);
979 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
980 inc_progress_dialog_update(inc_dialog, inc_session);
986 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
988 IncSession *inc_session = (IncSession *)data;
989 IncProgressDialog *inc_dialog;
991 g_return_val_if_fail(inc_session != NULL, -1);
993 inc_dialog = (IncProgressDialog *)inc_session->data;
995 switch (POP3_SESSION(session)->state) {
996 case POP3_GETAUTH_USER:
997 case POP3_GETAUTH_PASS:
998 case POP3_GETAUTH_APOP:
999 case POP3_GETRANGE_STAT:
1000 case POP3_GETRANGE_LAST:
1001 case POP3_GETRANGE_UIDL:
1002 case POP3_GETSIZE_LIST:
1003 inc_progress_dialog_update(inc_dialog, inc_session);
1006 inc_recv_data_progressive(session, 0, 0, inc_session);
1009 inc_progress_dialog_update(inc_dialog, inc_session);
1018 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1021 FolderItem *dropfolder;
1022 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1025 g_return_val_if_fail(inc_session != NULL, -1);
1027 if (session->ac_prefs->inbox) {
1028 inbox = folder_find_item_from_identifier
1029 (session->ac_prefs->inbox);
1031 inbox = folder_get_default_inbox();
1033 inbox = folder_get_default_inbox();
1039 /* CLAWS: claws uses a global .processing folder for the filtering. */
1040 dropfolder = folder_get_default_processing();
1042 /* add msg file to drop folder */
1043 if ((msgnum = folder_item_add_msg(dropfolder, file, NULL, TRUE)) < 0) {
1051 static void inc_put_error(IncState istate, const gchar *msg)
1053 gchar *log_msg = NULL;
1054 gchar *err_msg = NULL;
1055 gboolean fatal_error = FALSE;
1058 case INC_CONNECT_ERROR:
1059 log_msg = _("Connection failed.");
1060 if (prefs_common.no_recv_err_panel)
1062 err_msg = g_strdup(log_msg);
1065 log_msg = _("Error occurred while processing mail.");
1066 if (prefs_common.no_recv_err_panel)
1069 err_msg = g_strdup_printf
1070 (_("Error occurred while processing mail:\n%s"),
1073 err_msg = g_strdup(log_msg);
1076 log_msg = _("No disk space left.");
1077 err_msg = g_strdup(log_msg);
1081 log_msg = _("Can't write file.");
1082 err_msg = g_strdup(log_msg);
1085 case INC_SOCKET_ERROR:
1086 log_msg = _("Socket error.");
1087 if (prefs_common.no_recv_err_panel)
1089 err_msg = g_strdup(log_msg);
1092 log_msg = _("Connection closed by the remote host.");
1093 if (prefs_common.no_recv_err_panel)
1095 err_msg = g_strdup(log_msg);
1098 log_msg = _("Mailbox is locked.");
1099 if (prefs_common.no_recv_err_panel)
1102 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1105 err_msg = g_strdup(log_msg);
1107 case INC_AUTH_FAILED:
1108 log_msg = _("Authentication failed.");
1109 if (prefs_common.no_recv_err_panel)
1112 err_msg = g_strdup_printf
1113 (_("Authentication failed:\n%s"), msg);
1115 err_msg = g_strdup(log_msg);
1123 log_error("%s\n", log_msg);
1125 log_warning("%s\n", log_msg);
1128 alertpanel_error_log(err_msg);
1133 static void inc_cancel(IncProgressDialog *dialog)
1135 IncSession *session;
1137 g_return_if_fail(dialog != NULL);
1139 if (dialog->queue_list == NULL) {
1140 inc_progress_dialog_destroy(dialog);
1144 session = dialog->queue_list->data;
1146 session->inc_state = INC_CANCEL;
1148 log_message(_("Incorporation cancelled\n"));
1151 gboolean inc_is_active(void)
1153 return (inc_dialog_list != NULL);
1156 void inc_cancel_all(void)
1160 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1161 inc_cancel((IncProgressDialog *)cur->data);
1164 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1166 inc_cancel((IncProgressDialog *)data);
1169 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1172 IncProgressDialog *dialog = (IncProgressDialog *)data;
1174 if (dialog->queue_list == NULL)
1175 inc_progress_dialog_destroy(dialog);
1180 static gint inc_spool_account(PrefsAccount *account)
1183 gchar *mbox, *logname;
1186 logname = g_get_user_name();
1188 if (account->inbox) {
1189 inbox = folder_find_item_from_path(account->inbox);
1191 inbox = folder_get_default_inbox();
1193 inbox = folder_get_default_inbox();
1195 if (is_file_exist(account->local_mbox))
1196 mbox = g_strdup(account->local_mbox);
1198 mbox = g_strconcat(account->local_mbox,
1199 G_DIR_SEPARATOR_S, logname, NULL);
1201 result = get_spool(inbox, mbox);
1204 statusbar_pop_all();
1209 static gint inc_all_spool(void)
1213 gint account_new_msgs = 0;
1215 list = account_get_list();
1216 if (!list) return 0;
1218 for (; list != NULL; list = list->next) {
1219 PrefsAccount *account = list->data;
1221 if ((account->protocol == A_LOCAL) &&
1222 (account->recv_at_getall)) {
1223 account_new_msgs = inc_spool_account(account);
1224 if (account_new_msgs > 0)
1225 new_msgs += account_new_msgs;
1232 static gint get_spool(FolderItem *dest, const gchar *mbox)
1236 gchar tmp_mbox[MAXPATHLEN + 1];
1238 g_return_val_if_fail(dest != NULL, -1);
1239 g_return_val_if_fail(mbox != NULL, -1);
1241 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1242 debug_print("no messages in local mailbox.\n");
1244 } else if (size < 0)
1247 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1250 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1251 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1253 if (copy_mbox(mbox, tmp_mbox) < 0) {
1254 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1258 debug_print("Getting new messages from %s into %s...\n",
1261 msgs = proc_mbox(dest, tmp_mbox, TRUE);
1264 if (msgs >= 0) empty_mbox(mbox);
1265 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1275 void inc_unlock(void)
1277 if (inc_lock_count > 0)
1281 static guint autocheck_timer = 0;
1282 static gpointer autocheck_data = NULL;
1284 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1289 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1290 *prefs_common.newmail_notify_cmd))
1292 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1293 buf[1] == 'd' && !strchr(&buf[1], '%'))
1294 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1297 buf = g_strdup(prefs_common.newmail_notify_cmd);
1299 execute_command_line(buf, TRUE);
1304 void inc_autocheck_timer_init(MainWindow *mainwin)
1306 autocheck_data = mainwin;
1307 inc_autocheck_timer_set();
1310 static void inc_autocheck_timer_set_interval(guint interval)
1312 inc_autocheck_timer_remove();
1313 /* last test is to avoid re-enabling auto_check after modifying
1314 the common preferences */
1315 if (prefs_common.autochk_newmail && autocheck_data
1316 && prefs_common.work_offline == FALSE) {
1317 autocheck_timer = gtk_timeout_add
1318 (interval, inc_autocheck_func, autocheck_data);
1319 debug_print("added timer = %d\n", autocheck_timer);
1323 void inc_autocheck_timer_set(void)
1325 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1328 void inc_autocheck_timer_remove(void)
1330 if (autocheck_timer) {
1331 debug_print("removed timer = %d\n", autocheck_timer);
1332 gtk_timeout_remove(autocheck_timer);
1333 autocheck_timer = 0;
1337 static gint inc_autocheck_func(gpointer data)
1339 MainWindow *mainwin = (MainWindow *)data;
1341 if (inc_lock_count) {
1342 debug_print("autocheck is locked.\n");
1343 inc_autocheck_timer_set_interval(1000);
1347 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);