2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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 (PrefsAccount *account,
87 static IncProgressDialog *inc_progress_dialog_create (void);
88 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
89 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
91 static IncSession *inc_session_new (PrefsAccount *account);
92 static void inc_session_destroy (IncSession *session);
93 static gint inc_start (IncProgressDialog *inc_dialog);
94 static IncState inc_pop3_session_do (IncSession *session);
96 static void inc_progress_dialog_set_label
97 (IncProgressDialog *inc_dialog,
98 IncSession *inc_session);
100 static gint inc_recv_data_progressive (Session *session,
104 static gint inc_recv_data_finished (Session *session,
107 static gint inc_recv_message (Session *session,
111 static void inc_put_error (IncState istate,
114 static void inc_cancel_cb (GtkWidget *widget,
116 static gint inc_dialog_delete_cb (GtkWidget *widget,
120 static gint inc_spool (void);
121 static gint get_spool (FolderItem *dest,
124 static gint inc_spool_account(PrefsAccount *account);
125 static gint inc_all_spool(void);
126 static void inc_autocheck_timer_set_interval (guint interval);
127 static gint inc_autocheck_func (gpointer data);
129 static void inc_notify_cmd (gint new_msgs,
132 #define FOLDER_SUMMARY_MISMATCH(f, s) \
133 (f) && (s) ? ((s)->newmsgs != (f)->new_msgs) || ((f)->unread_msgs != (s)->unread) || ((f)->total_msgs != (s)->messages) \
138 * @mainwin: Main window.
139 * @new_messages: TRUE if some messages have been received.
141 * Update the folder view and the summary view after receiving
142 * messages. If @new_messages is FALSE, this function avoids unneeded
145 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
149 if (prefs_common.scan_all_after_inc)
150 folderview_check_new(NULL);
152 if (!new_messages && !prefs_common.scan_all_after_inc) return;
154 if (prefs_common.open_inbox_on_inc) {
155 item = cur_account && cur_account->inbox
156 ? folder_find_item_from_identifier(cur_account->inbox)
157 : folder_get_default_inbox();
158 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
159 folderview_unselect(mainwin->folderview);
160 folderview_select(mainwin->folderview, item);
165 void inc_mail(MainWindow *mainwin, gboolean notify)
168 gint account_new_msgs = 0;
170 if (inc_lock_count) return;
172 if (prefs_common.work_offline)
173 if (alertpanel(_("Offline warning"),
174 _("You're working offline. Override?"),
175 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
179 inc_autocheck_timer_remove();
180 main_window_lock(mainwin);
182 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
183 /* external incorporating program */
184 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
185 main_window_unlock(mainwin);
186 inc_autocheck_timer_set();
190 if (prefs_common.inc_local) {
191 account_new_msgs = inc_spool();
192 if (account_new_msgs > 0)
193 new_msgs += account_new_msgs;
196 if (prefs_common.inc_local) {
197 account_new_msgs = inc_spool();
198 if (account_new_msgs > 0)
199 new_msgs += account_new_msgs;
202 account_new_msgs = inc_account_mail(cur_account, mainwin);
203 if (account_new_msgs > 0)
204 new_msgs += account_new_msgs;
207 inc_finished(mainwin, new_msgs > 0);
208 main_window_unlock(mainwin);
209 inc_notify_cmd(new_msgs, notify);
210 inc_autocheck_timer_set();
214 void inc_pop_before_smtp(PrefsAccount *acc)
216 inc_account_mail(acc, NULL);
219 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
221 IncProgressDialog *inc_dialog;
223 FolderItem *item = NULL;
225 if(mainwin && mainwin->summaryview)
226 item = mainwin->summaryview->folder_item;
228 switch (account->protocol) {
232 * FIXME: it should return foldeview_check_new() value.
233 * TODO: do it when bug [19] is fixed (IMAP folder sets
234 * an incorrect new message count)
236 folderview_check_new(FOLDER(account->folder));
240 session = inc_session_new(account);
241 if (!session) return 0;
243 inc_dialog = inc_progress_dialog_create();
244 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
246 inc_dialog->mainwin = mainwin;
247 inc_progress_dialog_set_list(inc_dialog);
250 toolbar_main_set_sensitive(mainwin);
251 main_window_set_menu_sensitive(mainwin);
254 return inc_start(inc_dialog);
257 return inc_spool_account(account);
265 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
267 GList *list, *queue_list = NULL;
268 IncProgressDialog *inc_dialog;
270 gint account_new_msgs = 0;
272 if (prefs_common.work_offline)
273 if (alertpanel(_("Offline warning"),
274 _("You're working offline. Override?"),
275 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
278 if (inc_lock_count) return;
280 inc_autocheck_timer_remove();
281 main_window_lock(mainwin);
283 if (prefs_common.inc_local) {
284 account_new_msgs = inc_spool();
285 if (account_new_msgs > 0)
286 new_msgs += account_new_msgs;
289 list = account_get_list();
291 inc_finished(mainwin, new_msgs > 0);
292 main_window_unlock(mainwin);
293 inc_notify_cmd(new_msgs, notify);
294 inc_autocheck_timer_set();
298 /* check local folders */
299 account_new_msgs = inc_all_spool();
300 if (account_new_msgs > 0)
301 new_msgs += account_new_msgs;
303 /* check IMAP4 folders */
304 for (; list != NULL; list = list->next) {
305 PrefsAccount *account = list->data;
306 if ((account->protocol == A_IMAP4 ||
307 account->protocol == A_NNTP) && account->recv_at_getall) {
308 new_msgs += folderview_check_new(FOLDER(account->folder));
312 /* check POP3 accounts */
313 for (list = account_get_list(); list != NULL; list = list->next) {
315 PrefsAccount *account = list->data;
317 if (account->recv_at_getall) {
318 session = inc_session_new(account);
320 queue_list = g_list_append(queue_list, session);
325 inc_finished(mainwin, new_msgs > 0);
326 main_window_unlock(mainwin);
327 inc_notify_cmd(new_msgs, notify);
328 inc_autocheck_timer_set();
332 inc_dialog = inc_progress_dialog_create();
333 inc_dialog->queue_list = queue_list;
334 inc_dialog->mainwin = mainwin;
335 inc_progress_dialog_set_list(inc_dialog);
337 toolbar_main_set_sensitive(mainwin);
338 main_window_set_menu_sensitive(mainwin);
340 new_msgs += inc_start(inc_dialog);
341 inc_finished(mainwin, new_msgs > 0);
342 main_window_unlock(mainwin);
343 inc_notify_cmd(new_msgs, notify);
344 inc_autocheck_timer_set();
347 static IncProgressDialog *inc_progress_dialog_create(void)
349 IncProgressDialog *dialog;
350 ProgressDialog *progress;
352 dialog = g_new0(IncProgressDialog, 1);
354 progress = progress_dialog_create();
355 gtk_window_set_title(GTK_WINDOW(progress->window),
356 _("Retrieving new messages"));
357 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
358 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
359 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
360 GTK_SIGNAL_FUNC(inc_dialog_delete_cb), dialog);
361 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
363 progress_dialog_set_value(progress, 0.0);
365 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
367 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
368 ¤txpm, ¤txpmmask);
369 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
370 &errorxpm, &errorxpmmask);
372 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
373 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
374 manage_window_get_focus_window())) {
375 dialog->show_dialog = TRUE;
376 gtk_widget_show_now(progress->window);
379 dialog->dialog = progress;
380 dialog->queue_list = NULL;
383 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
388 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
392 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
393 IncSession *session = list->data;
394 Pop3Session *pop3_session = POP3_SESSION(session->session);
397 session->data = inc_dialog;
400 text[1] = pop3_session->ac_prefs->account_name;
401 text[2] = _("Standby");
402 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
406 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
408 progress_dialog_set_value(inc_dialog->dialog, 0.0);
409 progress_dialog_set_label(inc_dialog->dialog, "");
410 gtk_progress_set_show_text
411 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), FALSE);
412 if (inc_dialog->mainwin)
413 gtk_progress_bar_update
414 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
417 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
419 g_return_if_fail(inc_dialog != NULL);
421 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
423 gtk_progress_set_show_text
424 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), FALSE);
425 if (inc_dialog->mainwin)
426 gtk_progress_bar_update
427 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
428 progress_dialog_destroy(inc_dialog->dialog);
433 static IncSession *inc_session_new(PrefsAccount *account)
437 g_return_val_if_fail(account != NULL, NULL);
439 if (account->protocol != A_POP3 && account->protocol != A_APOP)
441 if (!account->recv_server || !account->userid)
444 session = g_new0(IncSession, 1);
445 session->session = pop3_session_new(account);
446 session->session->data = session;
451 static void inc_session_destroy(IncSession *session)
453 g_return_if_fail(session != NULL);
455 session_destroy(session->session);
459 static gint inc_start(IncProgressDialog *inc_dialog)
462 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
464 Pop3Session *pop3_session;
470 FolderItem *processing, *inbox;
472 GSList *msglist, *msglist_element;
474 qlist = inc_dialog->queue_list;
475 while (qlist != NULL) {
476 GList *next = qlist->next;
478 session = qlist->data;
479 pop3_session = POP3_SESSION(session->session);
480 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
481 if (pop3_session->ac_prefs->passwd)
483 g_strdup(pop3_session->ac_prefs->passwd);
484 else if (pop3_session->ac_prefs->tmp_pass)
486 g_strdup(pop3_session->ac_prefs->tmp_pass);
490 if (inc_dialog->show_dialog)
491 manage_window_focus_in
492 (inc_dialog->dialog->window,
495 pass = input_dialog_query_password
496 (pop3_session->ac_prefs->recv_server,
499 if (inc_dialog->show_dialog)
500 manage_window_focus_out
501 (inc_dialog->dialog->window,
505 pop3_session->ac_prefs->tmp_pass =
507 pop3_session->pass = pass;
514 #define SET_PIXMAP_AND_TEXT(xpm, xpmmask, str) \
516 gtk_clist_set_pixmap(clist, inc_dialog->cur_row, 0, xpm, xpmmask); \
517 gtk_clist_set_text(clist, inc_dialog->cur_row, 2, str); \
520 for (; inc_dialog->queue_list != NULL; inc_dialog->cur_row++) {
521 session = inc_dialog->queue_list->data;
522 pop3_session = POP3_SESSION(session->session);
524 if (pop3_session->pass == NULL) {
525 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
526 inc_session_destroy(session);
527 inc_dialog->queue_list =
528 g_list_remove(inc_dialog->queue_list, session);
532 inc_progress_dialog_clear(inc_dialog);
533 gtk_clist_moveto(clist, inc_dialog->cur_row, -1, 1.0, 0.0);
535 SET_PIXMAP_AND_TEXT(currentxpm, currentxpmmask,
538 session_set_recv_message_notify(session->session,
539 inc_recv_message, session);
540 session_set_recv_data_progressive_notify
541 (session->session, inc_recv_data_progressive, session);
542 session_set_recv_data_notify(session->session,
543 inc_recv_data_finished, session);
545 /* begin POP3 session */
546 inc_state = inc_pop3_session_do(session);
550 if (pop3_session->cur_total_num > 0)
551 msg = g_strdup_printf
552 (_("Done (%d message(s) (%s) received)"),
553 pop3_session->cur_total_num,
554 to_human_readable(pop3_session->cur_total_recv_bytes));
556 msg = g_strdup_printf(_("Done (no new messages)"));
557 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, msg);
560 case INC_CONNECT_ERROR:
561 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
562 _("Connection failed"));
564 case INC_AUTH_FAILED:
565 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
569 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
575 case INC_SOCKET_ERROR:
576 SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask, _("Error"));
579 SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
585 if (pop3_session->error_val == PS_AUTHFAIL) {
586 if(!prefs_common.no_recv_err_panel) {
587 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
588 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
589 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
592 (_("Authorization for %s on %s failed"),
594 pop3_session->ac_prefs->recv_server);
598 /* CLAWS: perform filtering actions on dropped message */
599 /* CLAWS: get default inbox (perhaps per account) */
600 if (pop3_session->ac_prefs->inbox) {
601 /* CLAWS: get destination folder / mailbox */
602 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
604 inbox = folder_get_default_inbox();
606 inbox = folder_get_default_inbox();
608 /* get list of messages in processing */
609 processing = folder_get_default_processing();
610 folder_item_scan(processing);
611 msglist = folder_item_get_msg_list(processing);
613 folder_item_update_freeze();
615 /* process messages */
616 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
617 msginfo = (MsgInfo *) msglist_element->data;
618 if (!pop3_session->ac_prefs->filter_on_recv || !procmsg_msginfo_filter(msginfo))
619 folder_item_move_msg(inbox, msginfo);
620 procmsg_msginfo_free(msginfo);
622 g_slist_free(msglist);
624 folder_item_update_thaw();
628 new_msgs += pop3_session->cur_total_num;
630 if (pop3_session->error_val == PS_AUTHFAIL &&
631 pop3_session->ac_prefs->tmp_pass) {
632 g_free(pop3_session->ac_prefs->tmp_pass);
633 pop3_session->ac_prefs->tmp_pass = NULL;
636 pop3_write_uidl_list(pop3_session);
638 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
640 if (inc_dialog->show_dialog)
641 manage_window_focus_in
642 (inc_dialog->dialog->window,
644 inc_put_error(inc_state, pop3_session->error_msg);
645 if (inc_dialog->show_dialog)
646 manage_window_focus_out
647 (inc_dialog->dialog->window,
649 if (inc_state == INC_NO_SPACE ||
650 inc_state == INC_IO_ERROR)
654 inc_session_destroy(session);
655 inc_dialog->queue_list =
656 g_list_remove(inc_dialog->queue_list, session);
659 #undef SET_PIXMAP_AND_TEXT
662 fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
665 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
667 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
670 if (error_num && !prefs_common.no_recv_err_panel) {
671 if (inc_dialog->show_dialog)
672 manage_window_focus_in(inc_dialog->dialog->window,
674 alertpanel_error_log(_("Some errors occurred while getting mail."));
675 if (inc_dialog->show_dialog)
676 manage_window_focus_out(inc_dialog->dialog->window,
681 while (inc_dialog->queue_list != NULL) {
682 session = inc_dialog->queue_list->data;
683 inc_session_destroy(session);
684 inc_dialog->queue_list =
685 g_list_remove(inc_dialog->queue_list, session);
688 if (prefs_common.close_recv_dialog)
689 inc_progress_dialog_destroy(inc_dialog);
691 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
693 gtk_label_set_text(GTK_LABEL(GTK_BIN(inc_dialog->dialog->cancel_btn)->child),
702 static IncState inc_pop3_session_do(IncSession *session)
704 Pop3Session *pop3_session = POP3_SESSION(session->session);
705 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
710 debug_print("getting new messages of account %s...\n",
711 pop3_session->ac_prefs->account_name);
713 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
715 buf = g_strdup_printf(_("%s: Retrieving new messages"),
716 pop3_session->ac_prefs->recv_server);
717 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
720 server = pop3_session->ac_prefs->recv_server;
722 port = pop3_session->ac_prefs->set_popport ?
723 pop3_session->ac_prefs->popport :
724 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
725 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
727 port = pop3_session->ac_prefs->set_popport ?
728 pop3_session->ac_prefs->popport : 110;
731 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
732 log_message("%s\n", buf);
734 progress_dialog_set_label(inc_dialog->dialog, buf);
739 if (session_connect(SESSION(pop3_session), server, port) < 0) {
740 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
742 if(!prefs_common.no_recv_err_panel) {
743 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
744 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
745 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
747 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
749 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
751 session->inc_state = INC_CONNECT_ERROR;
753 return INC_CONNECT_ERROR;
756 while (SESSION(pop3_session)->state != SESSION_DISCONNECTED &&
757 SESSION(pop3_session)->state != SESSION_ERROR &&
758 session->inc_state != INC_CANCEL)
759 gtk_main_iteration();
763 if (session->inc_state == INC_SUCCESS) {
764 switch (pop3_session->error_val) {
766 if (SESSION(pop3_session)->state == SESSION_ERROR) {
767 if (pop3_session->state == POP3_READY)
768 session->inc_state = INC_CONNECT_ERROR;
770 session->inc_state = INC_ERROR;
772 session->inc_state = INC_SUCCESS;
775 session->inc_state = INC_AUTH_FAILED;
778 session->inc_state = INC_IO_ERROR;
781 session->inc_state = INC_SOCKET_ERROR;
784 session->inc_state = INC_LOCKED;
787 session->inc_state = INC_ERROR;
792 return session->inc_state;
795 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
796 IncSession *inc_session)
798 gchar buf[MSGBUFSIZE];
799 ProgressDialog *dialog = inc_dialog->dialog;
800 Pop3Session *session;
802 g_return_if_fail(inc_session != NULL);
804 session = POP3_SESSION(inc_session->session);
806 switch (session->state) {
809 case POP3_GETAUTH_USER:
810 case POP3_GETAUTH_PASS:
811 case POP3_GETAUTH_APOP:
812 progress_dialog_set_label(dialog, _("Authenticating..."));
813 statusbar_print_all(_("Retrieving messages from %s..."),
814 SESSION(session)->server);
816 case POP3_GETRANGE_STAT:
817 progress_dialog_set_label
818 (dialog, _("Getting the number of new messages (STAT)..."));
820 case POP3_GETRANGE_LAST:
821 progress_dialog_set_label
822 (dialog, _("Getting the number of new messages (LAST)..."));
824 case POP3_GETRANGE_UIDL:
825 progress_dialog_set_label
826 (dialog, _("Getting the number of new messages (UIDL)..."));
828 case POP3_GETSIZE_LIST:
829 progress_dialog_set_label
830 (dialog, _("Getting the size of messages (LIST)..."));
833 gtk_progress_set_show_text
834 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), TRUE);
835 g_snprintf(buf, sizeof(buf), "%d / %d",
836 session->cur_msg, session->count);
837 gtk_progress_set_format_string
838 (GTK_PROGRESS(inc_dialog->mainwin->progressbar), buf);
839 inc_recv_data_progressive
840 (SESSION(session), 0,
841 session->msg[session->cur_msg].size,
845 if (session->msg[session->cur_msg].recv_time <
846 session->current_time) {
847 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
849 progress_dialog_set_label(dialog, buf);
853 progress_dialog_set_label(dialog, _("Quitting"));
860 static gint inc_recv_data_progressive(Session *session, guint cur_len,
861 guint total_len, gpointer data)
863 gchar buf[MSGBUFSIZE];
864 IncSession *inc_session = (IncSession *)data;
865 Pop3Session *pop3_session = POP3_SESSION(session);
866 IncProgressDialog *inc_dialog;
867 ProgressDialog *dialog;
871 g_return_val_if_fail(inc_session != NULL, -1);
873 if (pop3_session->state != POP3_RETR &&
874 pop3_session->state != POP3_RETR_RECV &&
875 pop3_session->state != POP3_DELETE &&
876 pop3_session->state != POP3_LOGOUT) return 0;
878 if (!pop3_session->new_msg_exist) return 0;
880 inc_dialog = (IncProgressDialog *)inc_session->data;
881 dialog = inc_dialog->dialog;
883 cur_total = pop3_session->cur_total_bytes + cur_len;
884 if (cur_total > pop3_session->total_bytes)
885 cur_total = pop3_session->total_bytes;
887 Xstrdup_a(total_size, to_human_readable(pop3_session->total_bytes),
889 g_snprintf(buf, sizeof(buf),
890 _("Retrieving message (%d / %d) (%s / %s)"),
891 pop3_session->cur_msg, pop3_session->count,
892 to_human_readable(cur_total), total_size);
893 progress_dialog_set_label(dialog, buf);
894 progress_dialog_set_percentage
895 (dialog, (gfloat)cur_total / (gfloat)pop3_session->total_bytes);
896 if (inc_dialog->mainwin)
897 gtk_progress_bar_update
898 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
899 (gfloat)cur_total / (gfloat)pop3_session->total_bytes);
905 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
907 IncSession *inc_session = (IncSession *)data;
908 Pop3Session *pop3_session = POP3_SESSION(session);
909 IncProgressDialog *inc_dialog;
912 g_return_val_if_fail(inc_session != NULL, -1);
914 inc_dialog = (IncProgressDialog *)inc_session->data;
915 inc_recv_data_progressive(session, 0, len, inc_session);
916 inc_progress_dialog_set_label(inc_dialog, inc_session);
918 if (pop3_session->cur_total_num == 0) return 0;
920 msg = g_strdup_printf(_("Retrieving (%d message(s) (%s) received)"),
921 pop3_session->cur_total_num,
923 (pop3_session->cur_total_recv_bytes));
924 gtk_clist_set_text(GTK_CLIST(inc_dialog->dialog->clist),
925 inc_dialog->cur_row, 2, msg);
930 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
932 IncSession *inc_session = (IncSession *)data;
933 IncProgressDialog *inc_dialog;
935 g_return_val_if_fail(inc_session != NULL, -1);
937 inc_dialog = (IncProgressDialog *)inc_session->data;
938 inc_progress_dialog_set_label(inc_dialog, inc_session);
943 gint inc_drop_message(const gchar *file, Pop3Session *session)
946 FolderItem *dropfolder;
949 if (session->ac_prefs->inbox) {
950 inbox = folder_find_item_from_identifier
951 (session->ac_prefs->inbox);
953 inbox = folder_get_default_inbox();
955 inbox = folder_get_default_inbox();
961 /* CLAWS: claws uses a global .processing folder for the filtering. */
962 dropfolder = folder_get_default_processing();
964 /* add msg file to drop folder */
965 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
973 static void inc_put_error(IncState istate, const gchar *msg)
976 case INC_CONNECT_ERROR:
977 if (prefs_common.no_recv_err_panel)
979 alertpanel_error(_("Connection failed."));
982 if (prefs_common.no_recv_err_panel)
985 alertpanel_error(_("Error occurred while processing mail:\n%s"), msg);
987 alertpanel_error(_("Error occurred while processing mail."));
990 alertpanel_error(_("No disk space left."));
993 alertpanel_error(_("Can't write file."));
995 case INC_SOCKET_ERROR:
996 if (prefs_common.no_recv_err_panel)
998 alertpanel_error(_("Socket error."));
1001 if (prefs_common.no_recv_err_panel)
1004 alertpanel_error(_("Mailbox is locked:\n%s"), msg);
1006 alertpanel_error(_("Mailbox is locked."));
1008 case INC_AUTH_FAILED:
1009 if (prefs_common.no_recv_err_panel)
1012 alertpanel_error(_("Authentication failed:\n%s"),
1015 alertpanel_error(_("Authentication failed."));
1022 static void inc_cancel(IncProgressDialog *dialog)
1024 IncSession *session;
1026 g_return_if_fail(dialog != NULL);
1028 if (dialog->queue_list == NULL) {
1029 inc_progress_dialog_destroy(dialog);
1033 session = dialog->queue_list->data;
1035 session->inc_state = INC_CANCEL;
1037 log_message(_("Incorporation cancelled\n"));
1040 gboolean inc_is_active(void)
1042 return (inc_dialog_list != NULL);
1045 void inc_cancel_all(void)
1049 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1050 inc_cancel((IncProgressDialog *)cur->data);
1053 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1055 inc_cancel((IncProgressDialog *)data);
1058 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1061 IncProgressDialog *dialog = (IncProgressDialog *)data;
1063 if (dialog->queue_list == NULL)
1064 inc_progress_dialog_destroy(dialog);
1069 static gint inc_spool(void)
1071 gchar *mbox, *logname;
1074 logname = g_get_user_name();
1075 mbox = g_strconcat(prefs_common.spool_path
1076 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1077 G_DIR_SEPARATOR_S, logname, NULL);
1078 msgs = get_spool(folder_get_default_inbox(), mbox);
1084 static gint inc_spool_account(PrefsAccount *account)
1088 if (account->inbox) {
1089 inbox = folder_find_item_from_path(account->inbox);
1091 inbox = folder_get_default_inbox();
1093 inbox = folder_get_default_inbox();
1095 return get_spool(inbox, account->local_mbox);
1098 static gint inc_all_spool(void)
1102 gint account_new_msgs = 0;
1104 list = account_get_list();
1105 if (!list) return 0;
1107 for (; list != NULL; list = list->next) {
1108 PrefsAccount *account = list->data;
1110 if ((account->protocol == A_LOCAL) &&
1111 (account->recv_at_getall)) {
1112 account_new_msgs = inc_spool_account(account);
1113 if (account_new_msgs > 0)
1114 new_msgs += account_new_msgs;
1121 static gint get_spool(FolderItem *dest, const gchar *mbox)
1125 gchar tmp_mbox[MAXPATHLEN + 1];
1127 g_return_val_if_fail(dest != NULL, -1);
1128 g_return_val_if_fail(mbox != NULL, -1);
1130 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1131 debug_print("no messages in local mailbox.\n");
1133 } else if (size < 0)
1136 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1139 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1140 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1142 if (copy_mbox(mbox, tmp_mbox) < 0) {
1143 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1147 debug_print("Getting new messages from %s into %s...\n",
1150 msgs = proc_mbox(dest, tmp_mbox);
1153 if (msgs >= 0) empty_mbox(mbox);
1154 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1164 void inc_unlock(void)
1166 if (inc_lock_count > 0)
1170 static guint autocheck_timer = 0;
1171 static gpointer autocheck_data = NULL;
1173 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1178 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1179 *prefs_common.newmail_notify_cmd))
1181 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1182 buf[1] == 'd' && !strchr(&buf[1], '%'))
1183 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1186 buf = g_strdup(prefs_common.newmail_notify_cmd);
1188 execute_command_line(buf, TRUE);
1193 void inc_autocheck_timer_init(MainWindow *mainwin)
1195 autocheck_data = mainwin;
1196 inc_autocheck_timer_set();
1199 static void inc_autocheck_timer_set_interval(guint interval)
1201 inc_autocheck_timer_remove();
1202 /* last test is to avoid re-enabling auto_check after modifying
1203 the common preferences */
1204 if (prefs_common.autochk_newmail && autocheck_data
1205 && prefs_common.work_offline == FALSE) {
1206 autocheck_timer = gtk_timeout_add
1207 (interval, inc_autocheck_func, autocheck_data);
1208 debug_print("added timer = %d\n", autocheck_timer);
1212 void inc_autocheck_timer_set(void)
1214 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1217 void inc_autocheck_timer_remove(void)
1219 if (autocheck_timer) {
1220 debug_print("removed timer = %d\n", autocheck_timer);
1221 gtk_timeout_remove(autocheck_timer);
1222 autocheck_timer = 0;
1226 static gint inc_autocheck_func(gpointer data)
1228 MainWindow *mainwin = (MainWindow *)data;
1230 if (inc_lock_count) {
1231 debug_print("autocheck is locked.\n");
1232 inc_autocheck_timer_set_interval(1000);
1236 inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);