2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkprogressbar.h>
38 #include "mainwindow.h"
39 #include "folderview.h"
40 #include "summaryview.h"
41 #include "prefs_common.h"
42 #include "prefs_account.h"
52 #include "statusbar.h"
54 #include "manage_window.h"
55 #include "stock_pixmap.h"
56 #include "progressdialog.h"
57 #include "inputdialog.h"
58 #include "alertpanel.h"
60 #include "filtering.h"
64 static GList *inc_dialog_list = NULL;
66 static guint inc_lock_count = 0;
68 static GdkPixbuf *currentpix;
69 static GdkPixbuf *errorpix;
70 static GdkPixbuf *okpix;
72 #define MSGBUFSIZE 8192
74 static void inc_finished (MainWindow *mainwin,
75 gboolean new_messages);
76 static gint inc_account_mail_real (MainWindow *mainwin,
77 PrefsAccount *account);
79 static IncProgressDialog *inc_progress_dialog_create
81 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
82 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
84 static IncSession *inc_session_new (PrefsAccount *account);
85 static void inc_session_destroy (IncSession *session);
86 static gint inc_start (IncProgressDialog *inc_dialog);
87 static IncState inc_pop3_session_do (IncSession *session);
89 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
90 IncSession *inc_session);
92 static void inc_progress_dialog_set_label
93 (IncProgressDialog *inc_dialog,
94 IncSession *inc_session);
95 static void inc_progress_dialog_set_progress
96 (IncProgressDialog *inc_dialog,
97 IncSession *inc_session);
99 static void inc_progress_dialog_update_periodic
100 (IncProgressDialog *inc_dialog,
101 IncSession *inc_session);
103 static gint inc_recv_data_progressive (Session *session,
107 static gint inc_recv_data_finished (Session *session,
110 static gint inc_recv_message (Session *session,
113 static gint inc_drop_message (Pop3Session *session,
116 static void inc_put_error (IncState istate,
117 Pop3Session *session);
119 static void inc_cancel_cb (GtkWidget *widget,
121 static gint inc_dialog_delete_cb (GtkWidget *widget,
125 static gint get_spool (FolderItem *dest,
127 PrefsAccount *account);
129 static gint inc_spool_account(PrefsAccount *account);
130 static gint inc_all_spool(void);
131 static void inc_autocheck_timer_set_interval (guint interval);
132 static gint inc_autocheck_func (gpointer data);
134 static void inc_notify_cmd (gint new_msgs,
139 * @mainwin: Main window.
140 * @new_messages: TRUE if some messages have been received.
142 * Update the folder view and the summary view after receiving
143 * messages. If @new_messages is FALSE, this function avoids unneeded
146 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
150 if (prefs_common.scan_all_after_inc)
151 folderview_check_new(NULL);
153 if (!new_messages && !prefs_common.scan_all_after_inc) return;
155 if (prefs_common.open_inbox_on_inc) {
156 item = cur_account && cur_account->inbox
157 ? folder_find_item_from_identifier(cur_account->inbox)
158 : folder_get_default_inbox();
159 folderview_unselect(mainwin->folderview);
160 folderview_select(mainwin->folderview, item);
164 void inc_mail(MainWindow *mainwin, gboolean notify)
167 gint account_new_msgs = 0;
169 if (inc_lock_count) return;
171 if (prefs_common.work_offline &&
172 !inc_offline_should_override(
173 _("Sylpheed-Claws needs network access in order "
178 inc_autocheck_timer_remove();
179 main_window_lock(mainwin);
181 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
182 /* external incorporating program */
183 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
184 main_window_unlock(mainwin);
185 inc_autocheck_timer_set();
189 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
190 if (account_new_msgs > 0)
191 new_msgs += account_new_msgs;
194 inc_finished(mainwin, new_msgs > 0);
195 main_window_unlock(mainwin);
196 inc_notify_cmd(new_msgs, notify);
197 inc_autocheck_timer_set();
201 void inc_pop_before_smtp(PrefsAccount *acc)
203 IncProgressDialog *inc_dialog;
207 mainwin = mainwindow_get_mainwindow();
209 session = inc_session_new(acc);
210 if (!session) return;
211 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
213 inc_dialog = inc_progress_dialog_create(FALSE);
214 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
216 /* FIXME: assumes to attach to first main window */
217 inc_dialog->mainwin = mainwin;
218 inc_progress_dialog_set_list(inc_dialog);
221 toolbar_main_set_sensitive(mainwin);
222 main_window_set_menu_sensitive(mainwin);
225 inc_start(inc_dialog);
228 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
230 IncProgressDialog *inc_dialog;
233 switch (account->protocol) {
237 * FIXME: it should return foldeview_check_new() value.
238 * TODO: do it when bug [19] is fixed (IMAP folder sets
239 * an incorrect new message count)
241 folderview_check_new(FOLDER(account->folder));
245 session = inc_session_new(account);
246 if (!session) return 0;
248 inc_dialog = inc_progress_dialog_create(FALSE);
249 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
251 inc_dialog->mainwin = mainwin;
252 inc_progress_dialog_set_list(inc_dialog);
255 toolbar_main_set_sensitive(mainwin);
256 main_window_set_menu_sensitive(mainwin);
259 return inc_start(inc_dialog);
262 return inc_spool_account(account);
270 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
274 if (inc_lock_count) return 0;
276 if (prefs_common.work_offline &&
277 !inc_offline_should_override(
278 _("Sylpheed-Claws needs network access in order "
282 inc_autocheck_timer_remove();
283 main_window_lock(mainwin);
285 new_msgs = inc_account_mail_real(mainwin, account);
287 inc_finished(mainwin, new_msgs > 0);
288 main_window_unlock(mainwin);
289 inc_autocheck_timer_set();
294 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
297 GList *list, *queue_list = NULL;
298 IncProgressDialog *inc_dialog;
300 gint account_new_msgs = 0;
302 if (prefs_common.work_offline &&
303 !inc_offline_should_override(
304 _("Sylpheed-Claws needs network access in order "
308 if (inc_lock_count) return;
310 inc_autocheck_timer_remove();
311 main_window_lock(mainwin);
313 list = account_get_list();
315 inc_finished(mainwin, new_msgs > 0);
316 main_window_unlock(mainwin);
317 inc_notify_cmd(new_msgs, notify);
318 inc_autocheck_timer_set();
322 /* check local folders */
323 account_new_msgs = inc_all_spool();
324 if (account_new_msgs > 0)
325 new_msgs += account_new_msgs;
327 /* check IMAP4 / News folders */
328 for (list = account_get_list(); list != NULL; list = list->next) {
329 PrefsAccount *account = list->data;
330 if ((account->protocol == A_IMAP4 ||
331 account->protocol == A_NNTP) && account->recv_at_getall) {
332 new_msgs += folderview_check_new(FOLDER(account->folder));
336 /* check POP3 accounts */
337 for (list = account_get_list(); list != NULL; list = list->next) {
339 PrefsAccount *account = list->data;
341 if (account->recv_at_getall) {
342 session = inc_session_new(account);
344 queue_list = g_list_append(queue_list, session);
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);
356 new_msgs += inc_start(inc_dialog);
359 inc_finished(mainwin, new_msgs > 0);
360 main_window_unlock(mainwin);
361 inc_notify_cmd(new_msgs, notify);
362 inc_autocheck_timer_set();
365 static void inc_progress_dialog_size_allocate_cb(GtkWidget *widget,
366 GtkAllocation *allocation)
368 g_return_if_fail(allocation != NULL);
370 prefs_common.receivewin_width = allocation->width;
371 prefs_common.receivewin_height = allocation->height;
374 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
376 IncProgressDialog *dialog;
377 ProgressDialog *progress;
378 static GdkGeometry geometry;
380 dialog = g_new0(IncProgressDialog, 1);
382 progress = progress_dialog_create();
383 gtk_window_set_title(GTK_WINDOW(progress->window),
384 _("Retrieving new messages"));
385 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
386 G_CALLBACK(inc_cancel_cb), dialog);
387 g_signal_connect(G_OBJECT(progress->window), "delete_event",
388 G_CALLBACK(inc_dialog_delete_cb), dialog);
389 g_signal_connect(G_OBJECT(progress->window), "size_allocate",
390 G_CALLBACK(inc_progress_dialog_size_allocate_cb), NULL);
391 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
393 progress_dialog_get_fraction(progress);
395 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_COMPLETE,
397 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_CONTINUE,
399 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_ERROR,
402 if (!geometry.min_height) {
403 geometry.min_width = 460;
404 geometry.min_height = 250;
407 gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
409 gtk_widget_set_size_request(progress->window, prefs_common.receivewin_width,
410 prefs_common.receivewin_height);
412 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
413 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
415 dialog->show_dialog = TRUE;
416 gtk_widget_show_now(progress->window);
419 dialog->dialog = progress;
420 g_get_current_time(&dialog->progress_tv);
421 g_get_current_time(&dialog->folder_tv);
422 dialog->queue_list = NULL;
425 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
430 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
434 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
435 IncSession *session = list->data;
436 Pop3Session *pop3_session = POP3_SESSION(session->session);
438 session->data = inc_dialog;
440 progress_dialog_list_set(inc_dialog->dialog,
442 pop3_session->ac_prefs->account_name,
447 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
449 progress_dialog_get_fraction(inc_dialog->dialog);
450 progress_dialog_set_label(inc_dialog->dialog, "");
451 if (inc_dialog->mainwin)
452 main_window_progress_off(inc_dialog->mainwin);
455 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
457 g_return_if_fail(inc_dialog != NULL);
459 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
461 if (inc_dialog->mainwin)
462 main_window_progress_off(inc_dialog->mainwin);
463 progress_dialog_destroy(inc_dialog->dialog);
468 static IncSession *inc_session_new(PrefsAccount *account)
472 g_return_val_if_fail(account != NULL, NULL);
474 if (account->protocol != A_POP3)
476 if (!account->recv_server || !account->userid)
479 session = g_new0(IncSession, 1);
481 session->session = pop3_session_new(account);
482 session->session->data = session;
483 POP3_SESSION(session->session)->drop_message = inc_drop_message;
484 session_set_recv_message_notify(session->session,
485 inc_recv_message, session);
486 session_set_recv_data_progressive_notify(session->session,
487 inc_recv_data_progressive,
489 session_set_recv_data_notify(session->session,
490 inc_recv_data_finished, session);
495 static void inc_session_destroy(IncSession *session)
497 g_return_if_fail(session != NULL);
499 session_destroy(session->session);
503 static gint inc_start(IncProgressDialog *inc_dialog)
507 Pop3Session *pop3_session;
513 FolderItem *processing, *inbox;
514 GSList *msglist, *msglist_element;
515 gboolean cancelled = FALSE;
517 qlist = inc_dialog->queue_list;
518 while (qlist != NULL) {
519 GList *next = qlist->next;
521 session = qlist->data;
522 pop3_session = POP3_SESSION(session->session);
523 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
524 if (pop3_session->ac_prefs->passwd)
526 g_strdup(pop3_session->ac_prefs->passwd);
527 else if (pop3_session->ac_prefs->tmp_pass)
529 g_strdup(pop3_session->ac_prefs->tmp_pass);
533 if (inc_dialog->show_dialog)
534 manage_window_focus_in
535 (inc_dialog->dialog->window,
538 pass = input_dialog_query_password
539 (pop3_session->ac_prefs->recv_server,
542 if (inc_dialog->show_dialog)
543 manage_window_focus_out
544 (inc_dialog->dialog->window,
548 pop3_session->ac_prefs->tmp_pass =
550 pop3_session->pass = pass;
557 #define SET_PIXMAP_AND_TEXT(pix, str) \
559 progress_dialog_list_set(inc_dialog->dialog, \
560 inc_dialog->cur_row, \
566 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
567 session = inc_dialog->queue_list->data;
568 pop3_session = POP3_SESSION(session->session);
569 GSList *filtered, *unfiltered;
571 if (pop3_session->pass == NULL) {
572 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
573 inc_session_destroy(session);
574 inc_dialog->queue_list =
575 g_list_remove(inc_dialog->queue_list, session);
579 inc_progress_dialog_clear(inc_dialog);
580 progress_dialog_scroll_to_row(inc_dialog->dialog,
581 inc_dialog->cur_row);
583 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
585 /* begin POP3 session */
586 inc_state = inc_pop3_session_do(session);
590 if (pop3_session->cur_total_num > 0)
591 msg = g_strdup_printf(
592 ngettext("Done (%d message (%s) received)",
593 "Done (%d messages (%s) received)",
594 pop3_session->cur_total_num),
595 pop3_session->cur_total_num,
596 to_human_readable(pop3_session->cur_total_recv_bytes));
598 msg = g_strdup_printf(_("Done (no new messages)"));
599 SET_PIXMAP_AND_TEXT(okpix, msg);
602 case INC_CONNECT_ERROR:
603 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
605 case INC_AUTH_FAILED:
606 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
609 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
614 case INC_SOCKET_ERROR:
616 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
619 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
622 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
623 if (!inc_dialog->show_dialog)
630 if (pop3_session->error_val == PS_AUTHFAIL) {
631 if(!prefs_common.no_recv_err_panel) {
632 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
633 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
634 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
638 /* CLAWS: perform filtering actions on dropped message */
639 /* CLAWS: get default inbox (perhaps per account) */
640 if (pop3_session->ac_prefs->inbox) {
641 /* CLAWS: get destination folder / mailbox */
642 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
644 inbox = folder_get_default_inbox();
646 inbox = folder_get_default_inbox();
648 /* get list of messages in processing */
649 processing = folder_get_default_processing();
650 folder_item_scan(processing);
651 msglist = folder_item_get_msg_list(processing);
653 /* process messages */
654 folder_item_update_freeze();
656 procmsg_msglist_filter(msglist, pop3_session->ac_prefs,
657 &filtered, &unfiltered,
658 pop3_session->ac_prefs->filter_on_recv);
660 filtering_move_and_copy_msgs(msglist);
661 if (unfiltered != NULL)
662 folder_item_move_msgs(inbox, unfiltered);
664 for(msglist_element = msglist; msglist_element != NULL;
665 msglist_element = msglist_element->next) {
666 MsgInfo *msginfo = (MsgInfo *)msglist_element->data;
667 procmsg_msginfo_free(msginfo);
669 folder_item_update_thaw();
671 g_slist_free(msglist);
672 g_slist_free(filtered);
673 g_slist_free(unfiltered);
677 new_msgs += pop3_session->cur_total_num;
679 if (pop3_session->error_val == PS_AUTHFAIL &&
680 pop3_session->ac_prefs->tmp_pass) {
681 g_free(pop3_session->ac_prefs->tmp_pass);
682 pop3_session->ac_prefs->tmp_pass = NULL;
685 pop3_write_uidl_list(pop3_session);
687 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
689 if (inc_dialog->show_dialog)
690 manage_window_focus_in
691 (inc_dialog->dialog->window,
693 inc_put_error(inc_state, pop3_session);
694 if (inc_dialog->show_dialog)
695 manage_window_focus_out
696 (inc_dialog->dialog->window,
698 if (inc_state == INC_NO_SPACE ||
699 inc_state == INC_IO_ERROR)
702 folder_item_free_cache(processing, TRUE);
704 inc_session_destroy(session);
705 inc_dialog->queue_list =
706 g_list_remove(inc_dialog->queue_list, session);
709 #undef SET_PIXMAP_AND_TEXT
712 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
713 "Finished (%d new messages)",
714 new_msgs), new_msgs);
716 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
718 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
720 while (inc_dialog->queue_list != NULL) {
721 session = inc_dialog->queue_list->data;
722 inc_session_destroy(session);
723 inc_dialog->queue_list =
724 g_list_remove(inc_dialog->queue_list, session);
727 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
728 inc_progress_dialog_destroy(inc_dialog);
730 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
732 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
741 static IncState inc_pop3_session_do(IncSession *session)
743 Pop3Session *pop3_session = POP3_SESSION(session->session);
744 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
749 debug_print("getting new messages of account %s...\n",
750 pop3_session->ac_prefs->account_name);
752 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
754 buf = g_strdup_printf(_("%s: Retrieving new messages"),
755 pop3_session->ac_prefs->recv_server);
756 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
759 server = pop3_session->ac_prefs->recv_server;
761 port = pop3_session->ac_prefs->set_popport ?
762 pop3_session->ac_prefs->popport :
763 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
764 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
765 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
766 SESSION(pop3_session)->nonblocking =
767 pop3_session->ac_prefs->use_nonblocking_ssl;
769 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE) {
770 if (alertpanel_full(_("Insecure connection"),
771 _("This connection is configured to be secured "
772 "using SSL, but SSL is not available in this "
773 "build of Sylpheed-Claws. \n\n"
774 "Do you want to continue connecting to this "
775 "server? The communication would not be "
777 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
778 NULL, FALSE, NULL, ALERT_WARNING,
779 G_ALERTDEFAULT) != G_ALERTALTERNATE)
782 port = pop3_session->ac_prefs->set_popport ?
783 pop3_session->ac_prefs->popport : 110;
786 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
787 statusbar_print_all("%s", buf);
788 log_message("%s\n", buf);
790 progress_dialog_set_label(inc_dialog->dialog, buf);
794 session_set_timeout(SESSION(pop3_session),
795 prefs_common.io_timeout_secs * 1000);
797 if (session_connect(SESSION(pop3_session), server, port) < 0) {
798 if(!prefs_common.no_recv_err_panel) {
799 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
800 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
801 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
803 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
805 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
807 log_error(_("Can't connect to POP3 server: %s:%d\n"),
810 session->inc_state = INC_CONNECT_ERROR;
812 return INC_CONNECT_ERROR;
815 while (session_is_connected(SESSION(pop3_session)) &&
816 session->inc_state != INC_CANCEL)
817 gtk_main_iteration();
819 if (session->inc_state == INC_SUCCESS) {
820 switch (pop3_session->error_val) {
822 switch (SESSION(pop3_session)->state) {
824 if (pop3_session->state == POP3_READY)
825 session->inc_state = INC_CONNECT_ERROR;
827 session->inc_state = INC_ERROR;
830 session->inc_state = INC_EOF;
832 case SESSION_TIMEOUT:
833 session->inc_state = INC_TIMEOUT;
836 session->inc_state = INC_SUCCESS;
841 session->inc_state = INC_AUTH_FAILED;
844 session->inc_state = INC_IO_ERROR;
847 session->inc_state = INC_SOCKET_ERROR;
850 session->inc_state = INC_LOCKED;
853 session->inc_state = INC_ERROR;
858 session_disconnect(SESSION(pop3_session));
861 return session->inc_state;
864 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
865 IncSession *inc_session)
867 inc_progress_dialog_set_label(inc_dialog, inc_session);
868 inc_progress_dialog_set_progress(inc_dialog, inc_session);
871 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
872 IncSession *inc_session)
874 ProgressDialog *dialog = inc_dialog->dialog;
875 Pop3Session *session;
877 g_return_if_fail(inc_session != NULL);
879 session = POP3_SESSION(inc_session->session);
881 switch (session->state) {
884 case POP3_GETAUTH_USER:
885 case POP3_GETAUTH_PASS:
886 case POP3_GETAUTH_APOP:
887 progress_dialog_set_label(dialog, _("Authenticating..."));
889 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
890 SESSION(session)->server,
891 session->ac_prefs->account_name);
893 case POP3_GETRANGE_STAT:
894 progress_dialog_set_label
895 (dialog, _("Getting the number of new messages (STAT)..."));
897 case POP3_GETRANGE_LAST:
898 progress_dialog_set_label
899 (dialog, _("Getting the number of new messages (LAST)..."));
901 case POP3_GETRANGE_UIDL:
902 progress_dialog_set_label
903 (dialog, _("Getting the number of new messages (UIDL)..."));
905 case POP3_GETSIZE_LIST:
906 progress_dialog_set_label
907 (dialog, _("Getting the size of messages (LIST)..."));
914 progress_dialog_set_label(dialog, _("Quitting"));
921 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
922 IncSession *inc_session)
924 gchar buf[MSGBUFSIZE];
925 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
926 gchar *total_size_str;
930 if (!pop3_session->new_msg_exist) return;
932 cur_total = inc_session->cur_total_bytes;
933 total = pop3_session->total_bytes;
934 if (pop3_session->state == POP3_RETR ||
935 pop3_session->state == POP3_RETR_RECV ||
936 pop3_session->state == POP3_DELETE) {
937 Xstrdup_a(total_size_str, to_human_readable(total), return);
938 g_snprintf(buf, sizeof(buf),
939 _("Retrieving message (%d / %d) (%s / %s)"),
940 pop3_session->cur_msg, pop3_session->count,
941 to_human_readable(cur_total), total_size_str);
942 progress_dialog_set_label(inc_dialog->dialog, buf);
945 progress_dialog_set_fraction
946 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
948 g_snprintf(buf, sizeof(buf), "%d / %d",
949 pop3_session->cur_msg, pop3_session->count);
950 gtk_progress_bar_set_text
951 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), buf);
952 gtk_progress_bar_set_fraction
953 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
954 (total == 0) ? 0 : (gfloat)cur_total / (gfloat)total);
956 if (pop3_session->cur_total_num > 0) {
957 g_snprintf(buf, sizeof(buf),
958 ngettext("Retrieving (%d message (%s) received)",
959 "Retrieving (%d messages (%s) received)",
960 pop3_session->cur_total_num),
961 pop3_session->cur_total_num,
963 (pop3_session->cur_total_recv_bytes));
964 progress_dialog_list_set_status(inc_dialog->dialog,
970 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
971 IncSession *inc_session)
977 g_get_current_time(&tv_cur);
979 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
980 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
981 if (tv_result.tv_usec < 0) {
983 tv_result.tv_usec += G_USEC_PER_SEC;
986 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
987 if (msec > PROGRESS_UPDATE_INTERVAL) {
988 inc_progress_dialog_update(inc_dialog, inc_session);
989 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
990 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
994 static gint inc_recv_data_progressive(Session *session, guint cur_len,
995 guint total_len, gpointer data)
997 IncSession *inc_session = (IncSession *)data;
998 Pop3Session *pop3_session = POP3_SESSION(session);
999 IncProgressDialog *inc_dialog;
1002 g_return_val_if_fail(inc_session != NULL, -1);
1004 if (pop3_session->state != POP3_RETR &&
1005 pop3_session->state != POP3_RETR_RECV &&
1006 pop3_session->state != POP3_DELETE &&
1007 pop3_session->state != POP3_LOGOUT) return 0;
1009 if (!pop3_session->new_msg_exist) return 0;
1011 cur_total = pop3_session->cur_total_bytes + cur_len;
1012 if (cur_total > pop3_session->total_bytes)
1013 cur_total = pop3_session->total_bytes;
1014 inc_session->cur_total_bytes = cur_total;
1016 inc_dialog = (IncProgressDialog *)inc_session->data;
1017 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1022 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1024 IncSession *inc_session = (IncSession *)data;
1025 IncProgressDialog *inc_dialog;
1027 g_return_val_if_fail(inc_session != NULL, -1);
1029 inc_dialog = (IncProgressDialog *)inc_session->data;
1031 inc_recv_data_progressive(session, 0, 0, inc_session);
1033 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1034 inc_progress_dialog_update(inc_dialog, inc_session);
1040 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1042 IncSession *inc_session = (IncSession *)data;
1043 IncProgressDialog *inc_dialog;
1045 g_return_val_if_fail(inc_session != NULL, -1);
1047 inc_dialog = (IncProgressDialog *)inc_session->data;
1049 switch (POP3_SESSION(session)->state) {
1050 case POP3_GETAUTH_USER:
1051 case POP3_GETAUTH_PASS:
1052 case POP3_GETAUTH_APOP:
1053 case POP3_GETRANGE_STAT:
1054 case POP3_GETRANGE_LAST:
1055 case POP3_GETRANGE_UIDL:
1056 case POP3_GETSIZE_LIST:
1057 inc_progress_dialog_update(inc_dialog, inc_session);
1060 inc_recv_data_progressive(session, 0, 0, inc_session);
1063 inc_progress_dialog_update(inc_dialog, inc_session);
1072 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1075 FolderItem *dropfolder;
1076 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1079 g_return_val_if_fail(inc_session != NULL, -1);
1081 if (session->ac_prefs->inbox) {
1082 inbox = folder_find_item_from_identifier
1083 (session->ac_prefs->inbox);
1085 inbox = folder_get_default_inbox();
1087 inbox = folder_get_default_inbox();
1093 /* CLAWS: claws uses a global .processing folder for the filtering. */
1094 dropfolder = folder_get_default_processing();
1096 /* add msg file to drop folder */
1097 if ((msgnum = folder_item_add_msg(
1098 dropfolder, file, NULL, TRUE)) < 0) {
1106 static void inc_put_error(IncState istate, Pop3Session *session)
1108 gchar *log_msg = NULL;
1109 gchar *err_msg = NULL;
1110 gboolean fatal_error = FALSE;
1113 case INC_CONNECT_ERROR:
1114 log_msg = _("Connection failed.");
1116 if (prefs_common.no_recv_err_panel)
1118 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1119 SESSION(session)->server,
1120 SESSION(session)->port);
1123 log_msg = _("Error occurred while processing mail.");
1125 if (prefs_common.no_recv_err_panel)
1127 if (session->error_msg)
1128 err_msg = g_strdup_printf
1129 (_("Error occurred while processing mail:\n%s"),
1130 session->error_msg);
1132 err_msg = g_strdup(log_msg);
1135 log_msg = _("No disk space left.");
1136 err_msg = g_strdup(log_msg);
1140 log_msg = _("Can't write file.");
1141 err_msg = g_strdup(log_msg);
1144 case INC_SOCKET_ERROR:
1145 log_msg = _("Socket error.");
1146 if (prefs_common.no_recv_err_panel)
1148 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1149 SESSION(session)->server,
1150 SESSION(session)->port);
1153 log_msg = _("Connection closed by the remote host.");
1154 if (prefs_common.no_recv_err_panel)
1156 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1157 SESSION(session)->server,
1158 SESSION(session)->port);
1161 log_msg = _("Mailbox is locked.");
1162 if (prefs_common.no_recv_err_panel)
1164 if (session->error_msg)
1165 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1166 session->error_msg);
1168 err_msg = g_strdup(log_msg);
1170 case INC_AUTH_FAILED:
1171 log_msg = _("Authentication failed.");
1173 if (prefs_common.no_recv_err_panel)
1175 if (session->error_msg)
1176 err_msg = g_strdup_printf
1177 (_("Authentication failed:\n%s"), session->error_msg);
1179 err_msg = g_strdup(log_msg);
1182 log_msg = _("Session timed out.");
1183 if (prefs_common.no_recv_err_panel)
1185 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1186 SESSION(session)->server,
1187 SESSION(session)->port);
1195 log_error("%s\n", log_msg);
1197 log_warning("%s\n", log_msg);
1199 if (prefs_common.no_recv_err_panel && fatal_error)
1200 mainwindow_show_error();
1203 alertpanel_error_log(err_msg);
1208 static void inc_cancel(IncProgressDialog *dialog)
1210 IncSession *session;
1212 g_return_if_fail(dialog != NULL);
1214 if (dialog->queue_list == NULL) {
1215 inc_progress_dialog_destroy(dialog);
1219 session = dialog->queue_list->data;
1221 session->inc_state = INC_CANCEL;
1223 log_message(_("Incorporation cancelled\n"));
1226 gboolean inc_is_active(void)
1228 return (inc_dialog_list != NULL);
1231 void inc_cancel_all(void)
1235 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1236 inc_cancel((IncProgressDialog *)cur->data);
1239 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1241 inc_cancel((IncProgressDialog *)data);
1244 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1247 IncProgressDialog *dialog = (IncProgressDialog *)data;
1249 if (dialog->queue_list == NULL)
1250 inc_progress_dialog_destroy(dialog);
1255 static gint inc_spool_account(PrefsAccount *account)
1261 if (account->local_inbox) {
1262 inbox = folder_find_item_from_identifier(account->local_inbox);
1264 inbox = folder_get_default_inbox();
1266 inbox = folder_get_default_inbox();
1268 if (is_file_exist(account->local_mbox))
1269 mbox = g_strdup(account->local_mbox);
1270 else if (is_dir_exist(account->local_mbox))
1271 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1272 g_get_user_name(), NULL);
1274 debug_print("%s: local mailbox not found.\n",
1275 account->local_mbox);
1279 result = get_spool(inbox, mbox, account);
1282 statusbar_pop_all();
1287 static gint inc_all_spool(void)
1291 gint account_new_msgs = 0;
1293 list = account_get_list();
1294 if (!list) return 0;
1296 for (; list != NULL; list = list->next) {
1297 PrefsAccount *account = list->data;
1299 if ((account->protocol == A_LOCAL) &&
1300 (account->recv_at_getall)) {
1301 account_new_msgs = inc_spool_account(account);
1302 if (account_new_msgs > 0)
1303 new_msgs += account_new_msgs;
1310 static gint get_spool(FolderItem *dest, const gchar *mbox, PrefsAccount *account)
1314 gchar tmp_mbox[MAXPATHLEN + 1];
1316 g_return_val_if_fail(dest != NULL, -1);
1317 g_return_val_if_fail(mbox != NULL, -1);
1319 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1320 debug_print("%s: no messages in local mailbox.\n", mbox);
1322 } else if (size < 0)
1325 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1328 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1329 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1331 if (copy_mbox(mbox, tmp_mbox) < 0) {
1332 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1336 debug_print("Getting new messages from %s into %s...\n",
1339 msgs = proc_mbox(dest, tmp_mbox, TRUE, account);
1342 if (msgs >= 0) empty_mbox(mbox);
1343 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1353 void inc_unlock(void)
1355 if (inc_lock_count > 0)
1359 static guint autocheck_timer = 0;
1360 static gpointer autocheck_data = NULL;
1362 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1365 gchar *buf, *numpos, *ret_str;
1366 gssize by_read = 0, by_written = 0;
1368 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1369 *prefs_common.newmail_notify_cmd))
1371 buf = g_strdup(prefs_common.newmail_notify_cmd);
1372 if ((numpos = strstr(buf, "%d")) != NULL) {
1376 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1381 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1383 if (ret_str && by_written) {
1387 debug_print("executing new mail notification command: %s\n", buf);
1388 execute_command_line(buf, TRUE);
1393 void inc_autocheck_timer_init(MainWindow *mainwin)
1395 autocheck_data = mainwin;
1396 inc_autocheck_timer_set();
1399 static void inc_autocheck_timer_set_interval(guint interval)
1401 inc_autocheck_timer_remove();
1402 /* last test is to avoid re-enabling auto_check after modifying
1403 the common preferences */
1404 if (prefs_common.autochk_newmail && autocheck_data
1405 && prefs_common.work_offline == FALSE) {
1406 autocheck_timer = gtk_timeout_add
1407 (interval, inc_autocheck_func, autocheck_data);
1408 debug_print("added timer = %d\n", autocheck_timer);
1412 void inc_autocheck_timer_set(void)
1414 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1417 void inc_autocheck_timer_remove(void)
1419 if (autocheck_timer) {
1420 debug_print("removed timer = %d\n", autocheck_timer);
1421 gtk_timeout_remove(autocheck_timer);
1422 autocheck_timer = 0;
1426 static gint inc_autocheck_func(gpointer data)
1428 MainWindow *mainwin = (MainWindow *)data;
1430 if (inc_lock_count) {
1431 debug_print("autocheck is locked.\n");
1432 inc_autocheck_timer_set_interval(1000);
1436 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1441 gboolean inc_offline_should_override(const gchar *msg)
1443 static time_t overridden_yes = 0;
1444 static time_t overridden_no = 0;
1445 int length = 10; /* minutes */
1446 gint answer = G_ALERTDEFAULT;
1448 if (prefs_common.autochk_newmail)
1449 length = prefs_common.autochk_itv; /* minutes */
1451 if (prefs_common.work_offline) {
1454 if (time(NULL) - overridden_yes < length * 60) /* seconds */
1456 else if (time(NULL) - overridden_no < 3) /* seconds */
1459 tmp = g_strdup_printf(
1460 _("%s%sYou're working offline. Override for %d minutes?"),
1465 answer = alertpanel(_("Offline warning"),
1467 GTK_STOCK_NO, "+" GTK_STOCK_YES, _("On_ly once"));
1469 if (answer == G_ALERTALTERNATE) {
1470 overridden_yes = time(NULL);
1472 } else if (answer == G_ALERTDEFAULT) {
1473 overridden_no = time(NULL);
1476 overridden_yes = (time_t)0;
1477 overridden_no = (time_t)0;