2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail 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
,
77 static gint
inc_account_mail_real (MainWindow
*mainwin
,
78 PrefsAccount
*account
);
80 static IncProgressDialog
*inc_progress_dialog_create
82 static void inc_progress_dialog_set_list(IncProgressDialog
*inc_dialog
);
83 static void inc_progress_dialog_destroy (IncProgressDialog
*inc_dialog
);
85 static IncSession
*inc_session_new (PrefsAccount
*account
);
86 static void inc_session_destroy (IncSession
*session
);
87 static gint
inc_start (IncProgressDialog
*inc_dialog
);
88 static IncState
inc_pop3_session_do (IncSession
*session
);
90 static void inc_progress_dialog_update (IncProgressDialog
*inc_dialog
,
91 IncSession
*inc_session
);
93 static void inc_progress_dialog_set_label
94 (IncProgressDialog
*inc_dialog
,
95 IncSession
*inc_session
);
96 static void inc_progress_dialog_set_progress
97 (IncProgressDialog
*inc_dialog
,
98 IncSession
*inc_session
);
100 static void inc_progress_dialog_update_periodic
101 (IncProgressDialog
*inc_dialog
,
102 IncSession
*inc_session
);
104 static gint
inc_recv_data_progressive (Session
*session
,
108 static gint
inc_recv_data_finished (Session
*session
,
111 static gint
inc_recv_message (Session
*session
,
114 static gint
inc_drop_message (Pop3Session
*session
,
117 static void inc_put_error (IncState istate
,
118 Pop3Session
*session
);
120 static void inc_cancel_cb (GtkWidget
*widget
,
122 static gint
inc_dialog_delete_cb (GtkWidget
*widget
,
126 static gint
get_spool (FolderItem
*dest
,
128 PrefsAccount
*account
);
130 static gint
inc_spool_account(PrefsAccount
*account
);
131 static gint
inc_all_spool(void);
132 static void inc_autocheck_timer_set_interval (guint interval
);
133 static gint
inc_autocheck_func (gpointer data
);
135 static void inc_notify_cmd (gint new_msgs
,
140 * @mainwin: Main window.
141 * @new_messages: TRUE if some messages have been received.
143 * Update the folder view and the summary view after receiving
144 * messages. If @new_messages is FALSE, this function avoids unneeded
147 static void inc_finished(MainWindow
*mainwin
, gboolean new_messages
, gboolean autocheck
)
149 if (prefs_common
.scan_all_after_inc
)
150 folderview_check_new(NULL
);
152 if (!autocheck
&& prefs_common
.open_inbox_on_inc
) {
153 FolderItem
*item
= NULL
;
155 if (cur_account
&& cur_account
->inbox
)
156 item
= folder_find_item_from_identifier(cur_account
->inbox
);
157 if (item
== NULL
&& cur_account
->folder
)
158 item
= cur_account
->folder
->inbox
;
160 item
= folder_get_default_inbox();
162 folderview_unselect(mainwin
->folderview
);
163 folderview_select(mainwin
->folderview
, item
);
167 void inc_mail(MainWindow
*mainwin
, gboolean notify
)
170 gint account_new_msgs
= 0;
172 if (inc_lock_count
) return;
174 if (prefs_common
.work_offline
&&
175 !inc_offline_should_override(TRUE
,
176 _("Claws Mail needs network access in order "
181 inc_autocheck_timer_remove();
182 main_window_lock(mainwin
);
184 if (prefs_common
.use_extinc
&& prefs_common
.extinc_cmd
) {
185 /* external incorporating program */
186 if (execute_command_line(prefs_common
.extinc_cmd
, FALSE
) < 0) {
187 main_window_unlock(mainwin
);
188 inc_autocheck_timer_set();
192 account_new_msgs
= inc_account_mail_real(mainwin
, cur_account
);
193 if (account_new_msgs
> 0)
194 new_msgs
+= account_new_msgs
;
197 inc_finished(mainwin
, new_msgs
> 0, FALSE
);
198 main_window_unlock(mainwin
);
199 inc_notify_cmd(new_msgs
, notify
);
200 inc_autocheck_timer_set();
204 void inc_pop_before_smtp(PrefsAccount
*acc
)
206 IncProgressDialog
*inc_dialog
;
210 mainwin
= mainwindow_get_mainwindow();
212 session
= inc_session_new(acc
);
213 if (!session
) return;
214 POP3_SESSION(session
->session
)->pop_before_smtp
= TRUE
;
216 inc_dialog
= inc_progress_dialog_create(FALSE
);
217 inc_dialog
->queue_list
= g_list_append(inc_dialog
->queue_list
,
219 /* FIXME: assumes to attach to first main window */
220 inc_dialog
->mainwin
= mainwin
;
221 inc_progress_dialog_set_list(inc_dialog
);
224 toolbar_main_set_sensitive(mainwin
);
225 main_window_set_menu_sensitive(mainwin
);
228 inc_start(inc_dialog
);
231 static gint
inc_account_mail_real(MainWindow
*mainwin
, PrefsAccount
*account
)
233 IncProgressDialog
*inc_dialog
;
236 switch (account
->protocol
) {
240 * FIXME: it should return foldeview_check_new() value.
241 * TODO: do it when bug [19] is fixed (IMAP folder sets
242 * an incorrect new message count)
244 folderview_check_new(FOLDER(account
->folder
));
248 session
= inc_session_new(account
);
249 if (!session
) return 0;
251 inc_dialog
= inc_progress_dialog_create(FALSE
);
252 inc_dialog
->queue_list
= g_list_append(inc_dialog
->queue_list
,
254 inc_dialog
->mainwin
= mainwin
;
255 inc_progress_dialog_set_list(inc_dialog
);
258 toolbar_main_set_sensitive(mainwin
);
259 main_window_set_menu_sensitive(mainwin
);
262 return inc_start(inc_dialog
);
265 return inc_spool_account(account
);
273 gint
inc_account_mail(MainWindow
*mainwin
, PrefsAccount
*account
)
277 if (inc_lock_count
) return 0;
279 if (prefs_common
.work_offline
&&
280 !inc_offline_should_override(TRUE
,
281 _("Claws Mail needs network access in order "
285 inc_autocheck_timer_remove();
286 main_window_lock(mainwin
);
288 new_msgs
= inc_account_mail_real(mainwin
, account
);
290 inc_finished(mainwin
, new_msgs
> 0, FALSE
);
291 main_window_unlock(mainwin
);
292 inc_autocheck_timer_set();
297 void inc_all_account_mail(MainWindow
*mainwin
, gboolean autocheck
,
300 GList
*list
, *queue_list
= NULL
;
301 IncProgressDialog
*inc_dialog
;
303 gint account_new_msgs
= 0;
305 if (prefs_common
.work_offline
&&
306 !inc_offline_should_override( (autocheck
== FALSE
),
307 _("Claws Mail needs network access in order "
311 if (inc_lock_count
) return;
313 inc_autocheck_timer_remove();
314 main_window_lock(mainwin
);
316 list
= account_get_list();
318 inc_finished(mainwin
, new_msgs
> 0, autocheck
);
319 main_window_unlock(mainwin
);
320 inc_notify_cmd(new_msgs
, notify
);
321 inc_autocheck_timer_set();
325 /* check local folders */
326 account_new_msgs
= inc_all_spool();
327 if (account_new_msgs
> 0)
328 new_msgs
+= account_new_msgs
;
330 /* check IMAP4 / News folders */
331 for (list
= account_get_list(); list
!= NULL
; list
= list
->next
) {
332 PrefsAccount
*account
= list
->data
;
333 if ((account
->protocol
== A_IMAP4
||
334 account
->protocol
== A_NNTP
) && account
->recv_at_getall
) {
335 new_msgs
+= folderview_check_new(FOLDER(account
->folder
));
339 /* check POP3 accounts */
340 for (list
= account_get_list(); list
!= NULL
; list
= list
->next
) {
342 PrefsAccount
*account
= list
->data
;
344 if (account
->recv_at_getall
) {
345 session
= inc_session_new(account
);
347 queue_list
= g_list_append(queue_list
, session
);
352 inc_dialog
= inc_progress_dialog_create(autocheck
);
353 inc_dialog
->queue_list
= queue_list
;
354 inc_dialog
->mainwin
= mainwin
;
355 inc_progress_dialog_set_list(inc_dialog
);
357 toolbar_main_set_sensitive(mainwin
);
358 main_window_set_menu_sensitive(mainwin
);
359 new_msgs
+= inc_start(inc_dialog
);
362 inc_finished(mainwin
, new_msgs
> 0, autocheck
);
363 main_window_unlock(mainwin
);
364 inc_notify_cmd(new_msgs
, notify
);
365 inc_autocheck_timer_set();
368 static void inc_progress_dialog_size_allocate_cb(GtkWidget
*widget
,
369 GtkAllocation
*allocation
)
371 g_return_if_fail(allocation
!= NULL
);
373 prefs_common
.receivewin_width
= allocation
->width
;
374 prefs_common
.receivewin_height
= allocation
->height
;
377 static IncProgressDialog
*inc_progress_dialog_create(gboolean autocheck
)
379 IncProgressDialog
*dialog
;
380 ProgressDialog
*progress
;
381 static GdkGeometry geometry
;
383 dialog
= g_new0(IncProgressDialog
, 1);
385 progress
= progress_dialog_create();
386 gtk_window_set_title(GTK_WINDOW(progress
->window
),
387 _("Retrieving new messages"));
388 g_signal_connect(G_OBJECT(progress
->cancel_btn
), "clicked",
389 G_CALLBACK(inc_cancel_cb
), dialog
);
390 g_signal_connect(G_OBJECT(progress
->window
), "delete_event",
391 G_CALLBACK(inc_dialog_delete_cb
), dialog
);
392 g_signal_connect(G_OBJECT(progress
->window
), "size_allocate",
393 G_CALLBACK(inc_progress_dialog_size_allocate_cb
), NULL
);
394 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
396 progress_dialog_get_fraction(progress
);
398 stock_pixbuf_gdk(progress
->treeview
, STOCK_PIXMAP_COMPLETE
,
400 stock_pixbuf_gdk(progress
->treeview
, STOCK_PIXMAP_CONTINUE
,
402 stock_pixbuf_gdk(progress
->treeview
, STOCK_PIXMAP_ERROR
,
405 if (!geometry
.min_height
) {
406 geometry
.min_width
= 460;
407 geometry
.min_height
= 250;
410 gtk_window_set_geometry_hints(GTK_WINDOW(progress
->window
), NULL
, &geometry
,
412 gtk_widget_set_size_request(progress
->window
, prefs_common
.receivewin_width
,
413 prefs_common
.receivewin_height
);
415 if (prefs_common
.recv_dialog_mode
== RECV_DIALOG_ALWAYS
||
416 (prefs_common
.recv_dialog_mode
== RECV_DIALOG_MANUAL
&&
418 dialog
->show_dialog
= TRUE
;
419 gtk_widget_show_now(progress
->window
);
422 dialog
->dialog
= progress
;
423 g_get_current_time(&dialog
->progress_tv
);
424 g_get_current_time(&dialog
->folder_tv
);
425 dialog
->queue_list
= NULL
;
428 inc_dialog_list
= g_list_append(inc_dialog_list
, dialog
);
433 static void inc_progress_dialog_set_list(IncProgressDialog
*inc_dialog
)
437 for (list
= inc_dialog
->queue_list
; list
!= NULL
; list
= list
->next
) {
438 IncSession
*session
= list
->data
;
439 Pop3Session
*pop3_session
= POP3_SESSION(session
->session
);
441 session
->data
= inc_dialog
;
443 progress_dialog_list_set(inc_dialog
->dialog
,
445 pop3_session
->ac_prefs
->account_name
,
450 static void inc_progress_dialog_clear(IncProgressDialog
*inc_dialog
)
452 progress_dialog_get_fraction(inc_dialog
->dialog
);
453 progress_dialog_set_label(inc_dialog
->dialog
, "");
454 if (inc_dialog
->mainwin
)
455 main_window_progress_off(inc_dialog
->mainwin
);
458 static void inc_progress_dialog_destroy(IncProgressDialog
*inc_dialog
)
460 g_return_if_fail(inc_dialog
!= NULL
);
462 inc_dialog_list
= g_list_remove(inc_dialog_list
, inc_dialog
);
464 if (inc_dialog
->mainwin
)
465 main_window_progress_off(inc_dialog
->mainwin
);
466 progress_dialog_destroy(inc_dialog
->dialog
);
471 static IncSession
*inc_session_new(PrefsAccount
*account
)
475 g_return_val_if_fail(account
!= NULL
, NULL
);
477 if (account
->protocol
!= A_POP3
)
479 if (!account
->recv_server
|| !account
->userid
)
482 session
= g_new0(IncSession
, 1);
484 session
->session
= pop3_session_new(account
);
485 session
->session
->data
= session
;
486 POP3_SESSION(session
->session
)->drop_message
= inc_drop_message
;
487 session_set_recv_message_notify(session
->session
,
488 inc_recv_message
, session
);
489 session_set_recv_data_progressive_notify(session
->session
,
490 inc_recv_data_progressive
,
492 session_set_recv_data_notify(session
->session
,
493 inc_recv_data_finished
, session
);
498 static void inc_session_destroy(IncSession
*session
)
500 g_return_if_fail(session
!= NULL
);
502 session_destroy(session
->session
);
506 static gint
inc_start(IncProgressDialog
*inc_dialog
)
510 Pop3Session
*pop3_session
;
516 FolderItem
*processing
, *inbox
;
517 GSList
*msglist
, *msglist_element
;
518 gboolean cancelled
= FALSE
;
520 qlist
= inc_dialog
->queue_list
;
521 while (qlist
!= NULL
) {
522 GList
*next
= qlist
->next
;
524 session
= qlist
->data
;
525 pop3_session
= POP3_SESSION(session
->session
);
526 pop3_session
->user
= g_strdup(pop3_session
->ac_prefs
->userid
);
527 if (pop3_session
->ac_prefs
->passwd
)
529 g_strdup(pop3_session
->ac_prefs
->passwd
);
530 else if (pop3_session
->ac_prefs
->tmp_pass
)
532 g_strdup(pop3_session
->ac_prefs
->tmp_pass
);
536 if (inc_dialog
->show_dialog
)
537 manage_window_focus_in
538 (inc_dialog
->dialog
->window
,
541 pass
= input_dialog_query_password
542 (pop3_session
->ac_prefs
->recv_server
,
545 if (inc_dialog
->show_dialog
)
546 manage_window_focus_out
547 (inc_dialog
->dialog
->window
,
551 pop3_session
->ac_prefs
->tmp_pass
=
553 pop3_session
->pass
= pass
;
560 #define SET_PIXMAP_AND_TEXT(pix, str) \
562 progress_dialog_list_set(inc_dialog->dialog, \
563 inc_dialog->cur_row, \
569 for (; inc_dialog
->queue_list
!= NULL
&& !cancelled
; inc_dialog
->cur_row
++) {
570 session
= inc_dialog
->queue_list
->data
;
571 pop3_session
= POP3_SESSION(session
->session
);
572 GSList
*filtered
, *unfiltered
;
574 if (pop3_session
->pass
== NULL
) {
575 SET_PIXMAP_AND_TEXT(okpix
, _("Cancelled"));
576 inc_session_destroy(session
);
577 inc_dialog
->queue_list
=
578 g_list_remove(inc_dialog
->queue_list
, session
);
582 inc_progress_dialog_clear(inc_dialog
);
583 progress_dialog_scroll_to_row(inc_dialog
->dialog
,
584 inc_dialog
->cur_row
);
586 SET_PIXMAP_AND_TEXT(currentpix
, _("Retrieving"));
588 /* begin POP3 session */
589 inc_state
= inc_pop3_session_do(session
);
593 if (pop3_session
->cur_total_num
> 0)
594 msg
= g_strdup_printf(
595 ngettext("Done (%d message (%s) received)",
596 "Done (%d messages (%s) received)",
597 pop3_session
->cur_total_num
),
598 pop3_session
->cur_total_num
,
599 to_human_readable(pop3_session
->cur_total_recv_bytes
));
601 msg
= g_strdup_printf(_("Done (no new messages)"));
602 SET_PIXMAP_AND_TEXT(okpix
, msg
);
605 case INC_CONNECT_ERROR
:
606 SET_PIXMAP_AND_TEXT(errorpix
, _("Connection failed"));
608 case INC_AUTH_FAILED
:
609 SET_PIXMAP_AND_TEXT(errorpix
, _("Auth failed"));
612 SET_PIXMAP_AND_TEXT(errorpix
, _("Locked"));
617 case INC_SOCKET_ERROR
:
619 SET_PIXMAP_AND_TEXT(errorpix
, _("Error"));
622 SET_PIXMAP_AND_TEXT(errorpix
, _("Timeout"));
625 SET_PIXMAP_AND_TEXT(okpix
, _("Cancelled"));
626 if (!inc_dialog
->show_dialog
)
633 if (pop3_session
->error_val
== PS_AUTHFAIL
) {
634 if(!prefs_common
.no_recv_err_panel
) {
635 if((prefs_common
.recv_dialog_mode
== RECV_DIALOG_ALWAYS
) ||
636 ((prefs_common
.recv_dialog_mode
== RECV_DIALOG_MANUAL
) && focus_window
))
637 manage_window_focus_in(inc_dialog
->dialog
->window
, NULL
, NULL
);
641 /* CLAWS: perform filtering actions on dropped message */
642 /* CLAWS: get default inbox (perhaps per account) */
643 if (pop3_session
->ac_prefs
->inbox
) {
644 /* CLAWS: get destination folder / mailbox */
645 inbox
= folder_find_item_from_identifier(pop3_session
->ac_prefs
->inbox
);
647 inbox
= folder_get_default_inbox();
649 inbox
= folder_get_default_inbox();
651 /* get list of messages in processing */
652 processing
= folder_get_default_processing();
653 folder_item_scan(processing
);
654 msglist
= folder_item_get_msg_list(processing
);
656 /* process messages */
657 folder_item_update_freeze();
659 procmsg_msglist_filter(msglist
, pop3_session
->ac_prefs
,
660 &filtered
, &unfiltered
,
661 pop3_session
->ac_prefs
->filter_on_recv
);
663 filtering_move_and_copy_msgs(msglist
);
664 if (unfiltered
!= NULL
)
665 folder_item_move_msgs(inbox
, unfiltered
);
667 for(msglist_element
= msglist
; msglist_element
!= NULL
;
668 msglist_element
= msglist_element
->next
) {
669 MsgInfo
*msginfo
= (MsgInfo
*)msglist_element
->data
;
670 procmsg_msginfo_free(msginfo
);
672 folder_item_update_thaw();
674 g_slist_free(msglist
);
675 g_slist_free(filtered
);
676 g_slist_free(unfiltered
);
680 new_msgs
+= pop3_session
->cur_total_num
;
682 if (pop3_session
->error_val
== PS_AUTHFAIL
&&
683 pop3_session
->ac_prefs
->tmp_pass
) {
684 g_free(pop3_session
->ac_prefs
->tmp_pass
);
685 pop3_session
->ac_prefs
->tmp_pass
= NULL
;
688 pop3_write_uidl_list(pop3_session
);
690 if (inc_state
!= INC_SUCCESS
&& inc_state
!= INC_CANCEL
) {
692 if (inc_dialog
->show_dialog
)
693 manage_window_focus_in
694 (inc_dialog
->dialog
->window
,
696 inc_put_error(inc_state
, pop3_session
);
697 if (inc_dialog
->show_dialog
)
698 manage_window_focus_out
699 (inc_dialog
->dialog
->window
,
701 if (inc_state
== INC_NO_SPACE
||
702 inc_state
== INC_IO_ERROR
)
705 folder_item_free_cache(processing
, TRUE
);
707 inc_session_destroy(session
);
708 inc_dialog
->queue_list
=
709 g_list_remove(inc_dialog
->queue_list
, session
);
712 #undef SET_PIXMAP_AND_TEXT
715 fin_msg
= g_strdup_printf(ngettext("Finished (%d new message)",
716 "Finished (%d new messages)",
717 new_msgs
), new_msgs
);
719 fin_msg
= g_strdup_printf(_("Finished (no new messages)"));
721 progress_dialog_set_label(inc_dialog
->dialog
, fin_msg
);
723 while (inc_dialog
->queue_list
!= NULL
) {
724 session
= inc_dialog
->queue_list
->data
;
725 inc_session_destroy(session
);
726 inc_dialog
->queue_list
=
727 g_list_remove(inc_dialog
->queue_list
, session
);
730 if (prefs_common
.close_recv_dialog
|| !inc_dialog
->show_dialog
)
731 inc_progress_dialog_destroy(inc_dialog
);
733 gtk_window_set_title(GTK_WINDOW(inc_dialog
->dialog
->window
),
735 gtk_button_set_label(GTK_BUTTON(inc_dialog
->dialog
->cancel_btn
),
744 static IncState
inc_pop3_session_do(IncSession
*session
)
746 Pop3Session
*pop3_session
= POP3_SESSION(session
->session
);
747 IncProgressDialog
*inc_dialog
= (IncProgressDialog
*)session
->data
;
752 debug_print("getting new messages of account %s...\n",
753 pop3_session
->ac_prefs
->account_name
);
755 pop3_session
->ac_prefs
->last_pop_login_time
= time(NULL
);
757 buf
= g_strdup_printf(_("%s: Retrieving new messages"),
758 pop3_session
->ac_prefs
->recv_server
);
759 gtk_window_set_title(GTK_WINDOW(inc_dialog
->dialog
->window
), buf
);
762 server
= pop3_session
->ac_prefs
->recv_server
;
764 port
= pop3_session
->ac_prefs
->set_popport
?
765 pop3_session
->ac_prefs
->popport
:
766 pop3_session
->ac_prefs
->ssl_pop
== SSL_TUNNEL
? 995 : 110;
767 SESSION(pop3_session
)->ssl_type
= pop3_session
->ac_prefs
->ssl_pop
;
768 if (pop3_session
->ac_prefs
->ssl_pop
!= SSL_NONE
)
769 SESSION(pop3_session
)->nonblocking
=
770 pop3_session
->ac_prefs
->use_nonblocking_ssl
;
772 if (pop3_session
->ac_prefs
->ssl_pop
!= SSL_NONE
) {
773 if (alertpanel_full(_("Insecure connection"),
774 _("This connection is configured to be secured "
775 "using SSL, but SSL is not available in this "
776 "build of Claws Mail. \n\n"
777 "Do you want to continue connecting to this "
778 "server? The communication would not be "
780 GTK_STOCK_CANCEL
, _("Con_tinue connecting"),
781 NULL
, FALSE
, NULL
, ALERT_WARNING
,
782 G_ALERTDEFAULT
) != G_ALERTALTERNATE
)
785 port
= pop3_session
->ac_prefs
->set_popport
?
786 pop3_session
->ac_prefs
->popport
: 110;
789 buf
= g_strdup_printf(_("Connecting to POP3 server: %s..."), server
);
790 statusbar_print_all("%s", buf
);
791 log_message(LOG_PROTOCOL
, "%s\n", buf
);
793 progress_dialog_set_label(inc_dialog
->dialog
, buf
);
797 session_set_timeout(SESSION(pop3_session
),
798 prefs_common
.io_timeout_secs
* 1000);
800 if (session_connect(SESSION(pop3_session
), server
, port
) < 0) {
801 if(!prefs_common
.no_recv_err_panel
) {
802 if((prefs_common
.recv_dialog_mode
== RECV_DIALOG_ALWAYS
) ||
803 ((prefs_common
.recv_dialog_mode
== RECV_DIALOG_MANUAL
) && focus_window
)) {
804 manage_window_focus_in(inc_dialog
->dialog
->window
, NULL
, NULL
);
806 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
808 manage_window_focus_out(inc_dialog
->dialog
->window
, NULL
, NULL
);
810 log_error(LOG_PROTOCOL
, _("Can't connect to POP3 server: %s:%d\n"),
813 session
->inc_state
= INC_CONNECT_ERROR
;
815 return INC_CONNECT_ERROR
;
818 while (session_is_connected(SESSION(pop3_session
)) &&
819 session
->inc_state
!= INC_CANCEL
)
820 gtk_main_iteration();
822 if (session
->inc_state
== INC_SUCCESS
) {
823 switch (pop3_session
->error_val
) {
825 switch (SESSION(pop3_session
)->state
) {
827 if (pop3_session
->state
== POP3_READY
)
828 session
->inc_state
= INC_CONNECT_ERROR
;
830 session
->inc_state
= INC_ERROR
;
833 session
->inc_state
= INC_EOF
;
835 case SESSION_TIMEOUT
:
836 session
->inc_state
= INC_TIMEOUT
;
839 session
->inc_state
= INC_SUCCESS
;
844 session
->inc_state
= INC_AUTH_FAILED
;
847 session
->inc_state
= INC_IO_ERROR
;
850 session
->inc_state
= INC_SOCKET_ERROR
;
853 session
->inc_state
= INC_LOCKED
;
856 session
->inc_state
= INC_ERROR
;
861 session_disconnect(SESSION(pop3_session
));
864 return session
->inc_state
;
867 static void inc_progress_dialog_update(IncProgressDialog
*inc_dialog
,
868 IncSession
*inc_session
)
870 inc_progress_dialog_set_label(inc_dialog
, inc_session
);
871 inc_progress_dialog_set_progress(inc_dialog
, inc_session
);
874 static void inc_progress_dialog_set_label(IncProgressDialog
*inc_dialog
,
875 IncSession
*inc_session
)
877 ProgressDialog
*dialog
= inc_dialog
->dialog
;
878 Pop3Session
*session
;
880 g_return_if_fail(inc_session
!= NULL
);
882 session
= POP3_SESSION(inc_session
->session
);
884 switch (session
->state
) {
887 case POP3_GETAUTH_USER
:
888 case POP3_GETAUTH_PASS
:
889 case POP3_GETAUTH_APOP
:
890 progress_dialog_set_label(dialog
, _("Authenticating..."));
892 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
893 SESSION(session
)->server
,
894 session
->ac_prefs
->account_name
);
896 case POP3_GETRANGE_STAT
:
897 progress_dialog_set_label
898 (dialog
, _("Getting the number of new messages (STAT)..."));
900 case POP3_GETRANGE_LAST
:
901 progress_dialog_set_label
902 (dialog
, _("Getting the number of new messages (LAST)..."));
904 case POP3_GETRANGE_UIDL
:
905 progress_dialog_set_label
906 (dialog
, _("Getting the number of new messages (UIDL)..."));
908 case POP3_GETSIZE_LIST
:
909 progress_dialog_set_label
910 (dialog
, _("Getting the size of messages (LIST)..."));
917 progress_dialog_set_label(dialog
, _("Quitting"));
924 static void inc_progress_dialog_set_progress(IncProgressDialog
*inc_dialog
,
925 IncSession
*inc_session
)
927 gchar buf
[MSGBUFSIZE
];
928 Pop3Session
*pop3_session
= POP3_SESSION(inc_session
->session
);
929 gchar
*total_size_str
;
933 if (!pop3_session
->new_msg_exist
) return;
935 cur_total
= inc_session
->cur_total_bytes
;
936 total
= pop3_session
->total_bytes
;
937 if (pop3_session
->state
== POP3_RETR
||
938 pop3_session
->state
== POP3_RETR_RECV
||
939 pop3_session
->state
== POP3_DELETE
) {
940 Xstrdup_a(total_size_str
, to_human_readable(total
), return);
941 g_snprintf(buf
, sizeof(buf
),
942 _("Retrieving message (%d / %d) (%s / %s)"),
943 pop3_session
->cur_msg
, pop3_session
->count
,
944 to_human_readable(cur_total
), total_size_str
);
945 progress_dialog_set_label(inc_dialog
->dialog
, buf
);
948 progress_dialog_set_fraction
949 (inc_dialog
->dialog
, (total
== 0) ? 0: (gfloat
)cur_total
/ (gfloat
)total
);
951 g_snprintf(buf
, sizeof(buf
), "%d / %d",
952 pop3_session
->cur_msg
, pop3_session
->count
);
953 gtk_progress_bar_set_text
954 (GTK_PROGRESS_BAR(inc_dialog
->mainwin
->progressbar
), buf
);
955 gtk_progress_bar_set_fraction
956 (GTK_PROGRESS_BAR(inc_dialog
->mainwin
->progressbar
),
957 (total
== 0) ? 0 : (gfloat
)cur_total
/ (gfloat
)total
);
959 if (pop3_session
->cur_total_num
> 0) {
960 g_snprintf(buf
, sizeof(buf
),
961 ngettext("Retrieving (%d message (%s) received)",
962 "Retrieving (%d messages (%s) received)",
963 pop3_session
->cur_total_num
),
964 pop3_session
->cur_total_num
,
966 (pop3_session
->cur_total_recv_bytes
));
967 progress_dialog_list_set_status(inc_dialog
->dialog
,
973 static void inc_progress_dialog_update_periodic(IncProgressDialog
*inc_dialog
,
974 IncSession
*inc_session
)
980 g_get_current_time(&tv_cur
);
982 tv_result
.tv_sec
= tv_cur
.tv_sec
- inc_dialog
->progress_tv
.tv_sec
;
983 tv_result
.tv_usec
= tv_cur
.tv_usec
- inc_dialog
->progress_tv
.tv_usec
;
984 if (tv_result
.tv_usec
< 0) {
986 tv_result
.tv_usec
+= G_USEC_PER_SEC
;
989 msec
= tv_result
.tv_sec
* 1000 + tv_result
.tv_usec
/ 1000;
990 if (msec
> PROGRESS_UPDATE_INTERVAL
) {
991 inc_progress_dialog_update(inc_dialog
, inc_session
);
992 inc_dialog
->progress_tv
.tv_sec
= tv_cur
.tv_sec
;
993 inc_dialog
->progress_tv
.tv_usec
= tv_cur
.tv_usec
;
997 static gint
inc_recv_data_progressive(Session
*session
, guint cur_len
,
998 guint total_len
, gpointer data
)
1000 IncSession
*inc_session
= (IncSession
*)data
;
1001 Pop3Session
*pop3_session
= POP3_SESSION(session
);
1002 IncProgressDialog
*inc_dialog
;
1005 g_return_val_if_fail(inc_session
!= NULL
, -1);
1007 if (pop3_session
->state
!= POP3_RETR
&&
1008 pop3_session
->state
!= POP3_RETR_RECV
&&
1009 pop3_session
->state
!= POP3_DELETE
&&
1010 pop3_session
->state
!= POP3_LOGOUT
) return 0;
1012 if (!pop3_session
->new_msg_exist
) return 0;
1014 cur_total
= pop3_session
->cur_total_bytes
+ cur_len
;
1015 if (cur_total
> pop3_session
->total_bytes
)
1016 cur_total
= pop3_session
->total_bytes
;
1017 inc_session
->cur_total_bytes
= cur_total
;
1019 inc_dialog
= (IncProgressDialog
*)inc_session
->data
;
1020 inc_progress_dialog_update_periodic(inc_dialog
, inc_session
);
1025 static gint
inc_recv_data_finished(Session
*session
, guint len
, gpointer data
)
1027 IncSession
*inc_session
= (IncSession
*)data
;
1028 IncProgressDialog
*inc_dialog
;
1030 g_return_val_if_fail(inc_session
!= NULL
, -1);
1032 inc_dialog
= (IncProgressDialog
*)inc_session
->data
;
1034 inc_recv_data_progressive(session
, 0, 0, inc_session
);
1036 if (POP3_SESSION(session
)->state
== POP3_LOGOUT
) {
1037 inc_progress_dialog_update(inc_dialog
, inc_session
);
1043 static gint
inc_recv_message(Session
*session
, const gchar
*msg
, gpointer data
)
1045 IncSession
*inc_session
= (IncSession
*)data
;
1046 IncProgressDialog
*inc_dialog
;
1048 g_return_val_if_fail(inc_session
!= NULL
, -1);
1050 inc_dialog
= (IncProgressDialog
*)inc_session
->data
;
1052 switch (POP3_SESSION(session
)->state
) {
1053 case POP3_GETAUTH_USER
:
1054 case POP3_GETAUTH_PASS
:
1055 case POP3_GETAUTH_APOP
:
1056 case POP3_GETRANGE_STAT
:
1057 case POP3_GETRANGE_LAST
:
1058 case POP3_GETRANGE_UIDL
:
1059 case POP3_GETSIZE_LIST
:
1060 inc_progress_dialog_update(inc_dialog
, inc_session
);
1063 inc_recv_data_progressive(session
, 0, 0, inc_session
);
1066 inc_progress_dialog_update(inc_dialog
, inc_session
);
1075 static gint
inc_drop_message(Pop3Session
*session
, const gchar
*file
)
1078 FolderItem
*dropfolder
;
1079 IncSession
*inc_session
= (IncSession
*)(SESSION(session
)->data
);
1082 g_return_val_if_fail(inc_session
!= NULL
, -1);
1084 if (session
->ac_prefs
->inbox
) {
1085 inbox
= folder_find_item_from_identifier
1086 (session
->ac_prefs
->inbox
);
1088 inbox
= folder_get_default_inbox();
1090 inbox
= folder_get_default_inbox();
1096 /* CLAWS: claws uses a global .processing folder for the filtering. */
1097 dropfolder
= folder_get_default_processing();
1099 /* add msg file to drop folder */
1100 if ((msgnum
= folder_item_add_msg(
1101 dropfolder
, file
, NULL
, TRUE
)) < 0) {
1109 static void inc_put_error(IncState istate
, Pop3Session
*session
)
1111 gchar
*log_msg
= NULL
;
1112 gchar
*err_msg
= NULL
;
1113 gboolean fatal_error
= FALSE
;
1116 case INC_CONNECT_ERROR
:
1117 log_msg
= _("Connection failed.");
1119 if (prefs_common
.no_recv_err_panel
)
1121 err_msg
= g_strdup_printf(_("Connection to %s:%d failed."),
1122 SESSION(session
)->server
,
1123 SESSION(session
)->port
);
1126 log_msg
= _("Error occurred while processing mail.");
1128 if (prefs_common
.no_recv_err_panel
)
1130 if (session
->error_msg
)
1131 err_msg
= g_strdup_printf
1132 (_("Error occurred while processing mail:\n%s"),
1133 session
->error_msg
);
1135 err_msg
= g_strdup(log_msg
);
1138 log_msg
= _("No disk space left.");
1139 err_msg
= g_strdup(log_msg
);
1143 log_msg
= _("Can't write file.");
1144 err_msg
= g_strdup(log_msg
);
1147 case INC_SOCKET_ERROR
:
1148 log_msg
= _("Socket error.");
1149 if (prefs_common
.no_recv_err_panel
)
1151 err_msg
= g_strdup_printf(_("Socket error on connection to %s:%d."),
1152 SESSION(session
)->server
,
1153 SESSION(session
)->port
);
1156 log_msg
= _("Connection closed by the remote host.");
1157 if (prefs_common
.no_recv_err_panel
)
1159 err_msg
= g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1160 SESSION(session
)->server
,
1161 SESSION(session
)->port
);
1164 log_msg
= _("Mailbox is locked.");
1165 if (prefs_common
.no_recv_err_panel
)
1167 if (session
->error_msg
)
1168 err_msg
= g_strdup_printf(_("Mailbox is locked:\n%s"),
1169 session
->error_msg
);
1171 err_msg
= g_strdup(log_msg
);
1173 case INC_AUTH_FAILED
:
1174 log_msg
= _("Authentication failed.");
1176 if (prefs_common
.no_recv_err_panel
)
1178 if (session
->error_msg
)
1179 err_msg
= g_strdup_printf
1180 (_("Authentication failed:\n%s"), session
->error_msg
);
1182 err_msg
= g_strdup(log_msg
);
1185 log_msg
= _("Session timed out.");
1186 if (prefs_common
.no_recv_err_panel
)
1188 err_msg
= g_strdup_printf(_("Connection to %s:%d timed out."),
1189 SESSION(session
)->server
,
1190 SESSION(session
)->port
);
1198 log_error(LOG_PROTOCOL
, "%s\n", log_msg
);
1200 log_warning(LOG_PROTOCOL
, "%s\n", log_msg
);
1202 if (prefs_common
.no_recv_err_panel
&& fatal_error
)
1203 mainwindow_show_error();
1206 alertpanel_error_log(err_msg
);
1211 static void inc_cancel(IncProgressDialog
*dialog
)
1213 IncSession
*session
;
1215 g_return_if_fail(dialog
!= NULL
);
1217 if (dialog
->queue_list
== NULL
) {
1218 inc_progress_dialog_destroy(dialog
);
1222 session
= dialog
->queue_list
->data
;
1224 session
->inc_state
= INC_CANCEL
;
1226 log_message(LOG_PROTOCOL
, _("Incorporation cancelled\n"));
1229 gboolean
inc_is_active(void)
1231 return (inc_dialog_list
!= NULL
);
1234 void inc_cancel_all(void)
1238 for (cur
= inc_dialog_list
; cur
!= NULL
; cur
= cur
->next
)
1239 inc_cancel((IncProgressDialog
*)cur
->data
);
1242 static void inc_cancel_cb(GtkWidget
*widget
, gpointer data
)
1244 inc_cancel((IncProgressDialog
*)data
);
1247 static gint
inc_dialog_delete_cb(GtkWidget
*widget
, GdkEventAny
*event
,
1250 IncProgressDialog
*dialog
= (IncProgressDialog
*)data
;
1252 if (dialog
->queue_list
== NULL
)
1253 inc_progress_dialog_destroy(dialog
);
1258 static gint
inc_spool_account(PrefsAccount
*account
)
1264 if (account
->local_inbox
) {
1265 inbox
= folder_find_item_from_identifier(account
->local_inbox
);
1267 inbox
= folder_get_default_inbox();
1269 inbox
= folder_get_default_inbox();
1271 if (is_file_exist(account
->local_mbox
))
1272 mbox
= g_strdup(account
->local_mbox
);
1273 else if (is_dir_exist(account
->local_mbox
))
1274 mbox
= g_strconcat(account
->local_mbox
, G_DIR_SEPARATOR_S
,
1275 g_get_user_name(), NULL
);
1277 debug_print("%s: local mailbox not found.\n",
1278 account
->local_mbox
);
1282 result
= get_spool(inbox
, mbox
, account
);
1285 statusbar_pop_all();
1290 static gint
inc_all_spool(void)
1294 gint account_new_msgs
= 0;
1296 list
= account_get_list();
1297 if (!list
) return 0;
1299 for (; list
!= NULL
; list
= list
->next
) {
1300 PrefsAccount
*account
= list
->data
;
1302 if ((account
->protocol
== A_LOCAL
) &&
1303 (account
->recv_at_getall
)) {
1304 account_new_msgs
= inc_spool_account(account
);
1305 if (account_new_msgs
> 0)
1306 new_msgs
+= account_new_msgs
;
1313 static gint
get_spool(FolderItem
*dest
, const gchar
*mbox
, PrefsAccount
*account
)
1317 gchar tmp_mbox
[MAXPATHLEN
+ 1];
1319 g_return_val_if_fail(dest
!= NULL
, -1);
1320 g_return_val_if_fail(mbox
!= NULL
, -1);
1321 g_return_val_if_fail(account
!= NULL
, -1);
1323 if (!is_file_exist(mbox
) || (size
= get_file_size(mbox
)) == 0) {
1324 debug_print("%s: no messages in local mailbox.\n", mbox
);
1326 } else if (size
< 0)
1329 if ((lockfd
= lock_mbox(mbox
, LOCK_FLOCK
)) < 0)
1332 g_snprintf(tmp_mbox
, sizeof(tmp_mbox
), "%s%ctmpmbox.%p",
1333 get_tmp_dir(), G_DIR_SEPARATOR
, mbox
);
1335 if (copy_mbox(lockfd
, tmp_mbox
) < 0) {
1336 unlock_mbox(mbox
, lockfd
, LOCK_FLOCK
);
1340 debug_print("Getting new messages from %s into %s...\n",
1343 msgs
= proc_mbox(dest
, tmp_mbox
, account
->filter_on_recv
, account
);
1346 if (msgs
>= 0) empty_mbox(mbox
);
1347 unlock_mbox(mbox
, lockfd
, LOCK_FLOCK
);
1357 void inc_unlock(void)
1359 if (inc_lock_count
> 0)
1363 static guint autocheck_timer
= 0;
1364 static gpointer autocheck_data
= NULL
;
1366 static void inc_notify_cmd(gint new_msgs
, gboolean notify
)
1369 gchar
*buf
, *numpos
, *ret_str
;
1370 gssize by_read
= 0, by_written
= 0;
1372 if (!(new_msgs
&& notify
&& prefs_common
.newmail_notify_cmd
&&
1373 *prefs_common
.newmail_notify_cmd
))
1375 buf
= g_strdup(prefs_common
.newmail_notify_cmd
);
1376 if ((numpos
= strstr(buf
, "%d")) != NULL
) {
1380 buf2
= g_strdup_printf("%s%d%s", buf
, new_msgs
, numpos
+ 2);
1385 ret_str
= g_locale_from_utf8(buf
, strlen(buf
), &by_read
, &by_written
,
1387 if (ret_str
&& by_written
) {
1391 debug_print("executing new mail notification command: %s\n", buf
);
1392 execute_command_line(buf
, TRUE
);
1397 void inc_autocheck_timer_init(MainWindow
*mainwin
)
1399 autocheck_data
= mainwin
;
1400 inc_autocheck_timer_set();
1403 static void inc_autocheck_timer_set_interval(guint interval
)
1405 inc_autocheck_timer_remove();
1406 /* last test is to avoid re-enabling auto_check after modifying
1407 the common preferences */
1408 if (prefs_common
.autochk_newmail
&& autocheck_data
1409 && prefs_common
.work_offline
== FALSE
) {
1410 autocheck_timer
= g_timeout_add
1411 (interval
, inc_autocheck_func
, autocheck_data
);
1412 debug_print("added timer = %d\n", autocheck_timer
);
1416 void inc_autocheck_timer_set(void)
1418 inc_autocheck_timer_set_interval(prefs_common
.autochk_itv
* 60000);
1421 void inc_autocheck_timer_remove(void)
1423 if (autocheck_timer
) {
1424 debug_print("removed timer = %d\n", autocheck_timer
);
1425 g_source_remove(autocheck_timer
);
1426 autocheck_timer
= 0;
1430 static gint
inc_autocheck_func(gpointer data
)
1432 MainWindow
*mainwin
= (MainWindow
*)data
;
1434 if (inc_lock_count
) {
1435 debug_print("autocheck is locked.\n");
1436 inc_autocheck_timer_set_interval(1000);
1440 inc_all_account_mail(mainwin
, TRUE
, prefs_common
.newmail_notify_auto
);
1445 gboolean
inc_offline_should_override(gboolean force_ask
, const gchar
*msg
)
1447 static time_t overridden_yes
= 0;
1448 static time_t overridden_no
= 0;
1449 int length
= 10; /* minutes */
1450 gint answer
= G_ALERTDEFAULT
;
1452 if (prefs_common
.autochk_newmail
)
1453 length
= prefs_common
.autochk_itv
; /* minutes */
1456 overridden_no
= (time_t)0;
1459 if (prefs_common
.work_offline
) {
1462 if (time(NULL
) - overridden_yes
< length
* 60) /* seconds */
1464 else if (time(NULL
) - overridden_no
< length
* 60) /* seconds */
1468 tmp
= g_strdup_printf(
1469 _("%s%sYou're working offline. Override for %d minutes?"),
1474 tmp
= g_strdup_printf(
1475 _("%s%sYou're working offline. Override?"),
1479 answer
= alertpanel(_("Offline warning"),
1481 GTK_STOCK_NO
, "+" GTK_STOCK_YES
,
1482 !force_ask
? _("On_ly once"):NULL
);
1484 if (answer
== G_ALERTALTERNATE
) {
1485 overridden_yes
= time(NULL
);
1487 } else if (answer
== G_ALERTDEFAULT
) {
1489 overridden_no
= time(NULL
);
1492 overridden_yes
= (time_t)0;
1493 overridden_no
= (time_t)0;