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 3 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, see <http://www.gnu.org/licenses/>.
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"
66 #include <hildon/hildon-banner.h>
67 #include <hildon/hildon-sound.h>
69 #include <hildon-widgets/hildon-banner.h>
70 #include <hildon-widgets/hildon-system-sound.h>
75 #include <conicconnection.h>
76 #include <conicconnectionevent.h>
78 static ConIcConnection *maemo_connection = NULL;
79 static gboolean maemo_warned_offline = FALSE;
83 static GList *inc_dialog_list = NULL;
85 guint inc_lock_count = 0;
87 static GdkPixbuf *currentpix;
88 static GdkPixbuf *errorpix;
89 static GdkPixbuf *okpix;
91 #define MSGBUFSIZE 8192
93 static void inc_finished (MainWindow *mainwin,
94 gboolean new_messages,
96 static gint inc_account_mail_real (MainWindow *mainwin,
97 PrefsAccount *account);
99 static IncProgressDialog *inc_progress_dialog_create
100 (gboolean autocheck);
101 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
102 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
104 static IncSession *inc_session_new (PrefsAccount *account);
105 static void inc_session_destroy (IncSession *session);
106 static gint inc_start (IncProgressDialog *inc_dialog);
107 static IncState inc_pop3_session_do (IncSession *session);
109 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
110 IncSession *inc_session);
112 static void inc_progress_dialog_set_label
113 (IncProgressDialog *inc_dialog,
114 IncSession *inc_session);
115 static void inc_progress_dialog_set_progress
116 (IncProgressDialog *inc_dialog,
117 IncSession *inc_session);
119 static void inc_progress_dialog_update_periodic
120 (IncProgressDialog *inc_dialog,
121 IncSession *inc_session);
123 static gint inc_recv_data_progressive (Session *session,
127 static gint inc_recv_data_finished (Session *session,
130 static gint inc_recv_message (Session *session,
133 static gint inc_drop_message (Pop3Session *session,
136 static void inc_put_error (IncState istate,
137 Pop3Session *session);
139 static void inc_cancel_cb (GtkWidget *widget,
141 static gint inc_dialog_delete_cb (GtkWidget *widget,
145 static gint get_spool (FolderItem *dest,
147 PrefsAccount *account);
149 static gint inc_spool_account(PrefsAccount *account);
150 static gint inc_all_spool(void);
151 static void inc_autocheck_timer_set_interval (guint interval);
152 static gint inc_autocheck_func (gpointer data);
154 static void inc_notify_cmd (gint new_msgs,
159 * @mainwin: Main window.
160 * @new_messages: TRUE if some messages have been received.
162 * Update the folder view and the summary view after receiving
163 * messages. If @new_messages is FALSE, this function avoids unneeded
166 static void inc_finished(MainWindow *mainwin, gboolean new_messages, gboolean autocheck)
168 if (prefs_common.scan_all_after_inc)
169 folderview_check_new(NULL);
171 if (!autocheck && prefs_common.open_inbox_on_inc) {
172 FolderItem *item = NULL;
174 if (cur_account && cur_account->inbox)
175 item = folder_find_item_from_identifier(cur_account->inbox);
176 if (item == NULL && cur_account->folder)
177 item = cur_account->folder->inbox;
179 item = folder_get_default_inbox();
181 folderview_unselect(mainwin->folderview);
182 folderview_select(mainwin->folderview, item);
186 void inc_mail(MainWindow *mainwin, gboolean notify)
189 gint account_new_msgs = 0;
191 if (inc_lock_count) return;
193 if (prefs_common.work_offline &&
194 !inc_offline_should_override(TRUE,
195 _("Claws Mail needs network access in order "
200 inc_autocheck_timer_remove();
201 main_window_lock(mainwin);
203 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
204 /* external incorporating program */
205 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
206 main_window_unlock(mainwin);
207 inc_autocheck_timer_set();
212 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
213 if (account_new_msgs > 0)
214 new_msgs += account_new_msgs;
217 inc_finished(mainwin, new_msgs > 0, FALSE);
218 main_window_unlock(mainwin);
219 inc_notify_cmd(new_msgs, notify);
220 inc_autocheck_timer_set();
224 void inc_pop_before_smtp(PrefsAccount *acc)
226 IncProgressDialog *inc_dialog;
230 mainwin = mainwindow_get_mainwindow();
232 session = inc_session_new(acc);
233 if (!session) return;
234 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
236 inc_dialog = inc_progress_dialog_create(FALSE);
237 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
239 /* FIXME: assumes to attach to first main window */
240 inc_dialog->mainwin = mainwin;
241 inc_progress_dialog_set_list(inc_dialog);
244 toolbar_main_set_sensitive(mainwin);
245 main_window_set_menu_sensitive(mainwin);
248 inc_start(inc_dialog);
251 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
253 IncProgressDialog *inc_dialog;
256 switch (account->protocol) {
260 * FIXME: it should return foldeview_check_new() value.
261 * TODO: do it when bug [19] is fixed (IMAP folder sets
262 * an incorrect new message count)
264 folderview_check_new(FOLDER(account->folder));
268 session = inc_session_new(account);
269 if (!session) return 0;
271 inc_dialog = inc_progress_dialog_create(FALSE);
272 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
274 inc_dialog->mainwin = mainwin;
275 inc_progress_dialog_set_list(inc_dialog);
278 toolbar_main_set_sensitive(mainwin);
279 main_window_set_menu_sensitive(mainwin);
282 return inc_start(inc_dialog);
285 return inc_spool_account(account);
293 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
297 if (inc_lock_count) return 0;
299 if (prefs_common.work_offline &&
300 !inc_offline_should_override(TRUE,
301 _("Claws Mail needs network access in order "
305 inc_autocheck_timer_remove();
306 main_window_lock(mainwin);
308 new_msgs = inc_account_mail_real(mainwin, account);
310 inc_finished(mainwin, new_msgs > 0, FALSE);
311 main_window_unlock(mainwin);
312 inc_autocheck_timer_set();
317 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
320 GList *list, *queue_list = NULL;
321 IncProgressDialog *inc_dialog;
323 gint account_new_msgs = 0;
325 if (prefs_common.work_offline &&
326 !inc_offline_should_override( (autocheck == FALSE),
327 _("Claws Mail needs network access in order "
331 if (inc_lock_count) return;
333 inc_autocheck_timer_remove();
334 main_window_lock(mainwin);
336 list = account_get_list();
338 inc_finished(mainwin, new_msgs > 0, autocheck);
339 main_window_unlock(mainwin);
340 inc_notify_cmd(new_msgs, notify);
341 inc_autocheck_timer_set();
345 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
346 /* external incorporating program */
347 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
348 log_error(LOG_PROTOCOL, _("%s failed\n"), prefs_common.extinc_cmd);
350 main_window_unlock(mainwin);
351 inc_autocheck_timer_set();
356 /* check local folders */
357 account_new_msgs = inc_all_spool();
358 if (account_new_msgs > 0)
359 new_msgs += account_new_msgs;
361 /* check IMAP4 / News folders */
362 for (list = account_get_list(); list != NULL; list = list->next) {
363 PrefsAccount *account = list->data;
364 if ((account->protocol == A_IMAP4 ||
365 account->protocol == A_NNTP) && account->recv_at_getall) {
366 new_msgs += folderview_check_new(FOLDER(account->folder));
370 /* check POP3 accounts */
371 for (list = account_get_list(); list != NULL; list = list->next) {
373 PrefsAccount *account = list->data;
375 if (account->recv_at_getall) {
376 session = inc_session_new(account);
378 queue_list = g_list_append(queue_list, session);
383 inc_dialog = inc_progress_dialog_create(autocheck);
384 inc_dialog->queue_list = queue_list;
385 inc_dialog->mainwin = mainwin;
386 inc_progress_dialog_set_list(inc_dialog);
388 toolbar_main_set_sensitive(mainwin);
389 main_window_set_menu_sensitive(mainwin);
390 new_msgs += inc_start(inc_dialog);
393 inc_finished(mainwin, new_msgs > 0, autocheck);
394 main_window_unlock(mainwin);
395 inc_notify_cmd(new_msgs, notify);
396 inc_autocheck_timer_set();
399 static void inc_progress_dialog_size_allocate_cb(GtkWidget *widget,
400 GtkAllocation *allocation)
402 g_return_if_fail(allocation != NULL);
404 prefs_common.receivewin_width = allocation->width;
405 prefs_common.receivewin_height = allocation->height;
408 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
410 IncProgressDialog *dialog;
411 ProgressDialog *progress;
412 static GdkGeometry geometry;
414 dialog = g_new0(IncProgressDialog, 1);
416 progress = progress_dialog_create();
417 gtk_window_set_title(GTK_WINDOW(progress->window),
418 _("Retrieving new messages"));
419 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
420 G_CALLBACK(inc_cancel_cb), dialog);
421 g_signal_connect(G_OBJECT(progress->window), "delete_event",
422 G_CALLBACK(inc_dialog_delete_cb), dialog);
423 g_signal_connect(G_OBJECT(progress->window), "size_allocate",
424 G_CALLBACK(inc_progress_dialog_size_allocate_cb), NULL);
425 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
427 progress_dialog_get_fraction(progress);
429 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_COMPLETE,
431 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_CONTINUE,
433 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_ERROR,
436 if (!geometry.min_height) {
437 geometry.min_width = 460;
438 geometry.min_height = 250;
441 gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
443 gtk_widget_set_size_request(progress->window, prefs_common.receivewin_width,
444 prefs_common.receivewin_height);
446 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
447 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
449 dialog->show_dialog = TRUE;
450 gtk_widget_show_now(progress->window);
453 dialog->dialog = progress;
454 g_get_current_time(&dialog->progress_tv);
455 g_get_current_time(&dialog->folder_tv);
456 dialog->queue_list = NULL;
459 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
464 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
468 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
469 IncSession *session = list->data;
470 Pop3Session *pop3_session = POP3_SESSION(session->session);
472 session->data = inc_dialog;
474 progress_dialog_list_set(inc_dialog->dialog,
476 pop3_session->ac_prefs->account_name,
481 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
483 progress_dialog_get_fraction(inc_dialog->dialog);
484 progress_dialog_set_label(inc_dialog->dialog, "");
485 if (inc_dialog->mainwin)
486 main_window_progress_off(inc_dialog->mainwin);
489 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
491 g_return_if_fail(inc_dialog != NULL);
493 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
495 if (inc_dialog->mainwin)
496 main_window_progress_off(inc_dialog->mainwin);
497 progress_dialog_destroy(inc_dialog->dialog);
502 static IncSession *inc_session_new(PrefsAccount *account)
506 g_return_val_if_fail(account != NULL, NULL);
508 if (account->protocol != A_POP3)
510 if (!account->recv_server || !account->userid)
513 session = g_new0(IncSession, 1);
515 session->session = pop3_session_new(account);
516 session->session->data = session;
517 POP3_SESSION(session->session)->drop_message = inc_drop_message;
518 session_set_recv_message_notify(session->session,
519 inc_recv_message, session);
520 session_set_recv_data_progressive_notify(session->session,
521 inc_recv_data_progressive,
523 session_set_recv_data_notify(session->session,
524 inc_recv_data_finished, session);
529 static void inc_session_destroy(IncSession *session)
531 g_return_if_fail(session != NULL);
533 session_destroy(session->session);
537 static gint inc_start(IncProgressDialog *inc_dialog)
541 Pop3Session *pop3_session;
547 FolderItem *processing, *inbox;
548 GSList *msglist, *msglist_element;
549 gboolean cancelled = FALSE;
551 qlist = inc_dialog->queue_list;
552 while (qlist != NULL) {
553 GList *next = qlist->next;
555 session = qlist->data;
556 pop3_session = POP3_SESSION(session->session);
557 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
558 if (pop3_session->ac_prefs->passwd)
560 g_strdup(pop3_session->ac_prefs->passwd);
561 else if (pop3_session->ac_prefs->tmp_pass)
563 g_strdup(pop3_session->ac_prefs->tmp_pass);
567 if (inc_dialog->show_dialog)
568 manage_window_focus_in
569 (inc_dialog->dialog->window,
572 pass = input_dialog_query_password
573 (pop3_session->ac_prefs->recv_server,
576 if (inc_dialog->show_dialog)
577 manage_window_focus_out
578 (inc_dialog->dialog->window,
582 pop3_session->ac_prefs->tmp_pass =
584 pop3_session->pass = pass;
591 #define SET_PIXMAP_AND_TEXT(pix, str) \
593 progress_dialog_list_set(inc_dialog->dialog, \
594 inc_dialog->cur_row, \
600 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
601 session = inc_dialog->queue_list->data;
602 pop3_session = POP3_SESSION(session->session);
603 GSList *filtered, *unfiltered;
605 if (pop3_session->pass == NULL) {
606 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
607 inc_session_destroy(session);
608 inc_dialog->queue_list =
609 g_list_remove(inc_dialog->queue_list, session);
613 inc_progress_dialog_clear(inc_dialog);
614 progress_dialog_scroll_to_row(inc_dialog->dialog,
615 inc_dialog->cur_row);
617 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
619 /* begin POP3 session */
620 inc_state = inc_pop3_session_do(session);
624 if (pop3_session->cur_total_num > 0)
625 msg = g_strdup_printf(
626 ngettext("Done (%d message (%s) received)",
627 "Done (%d messages (%s) received)",
628 pop3_session->cur_total_num),
629 pop3_session->cur_total_num,
630 to_human_readable((goffset)pop3_session->cur_total_recv_bytes));
632 msg = g_strdup_printf(_("Done (no new messages)"));
633 SET_PIXMAP_AND_TEXT(okpix, msg);
636 case INC_CONNECT_ERROR:
637 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
639 case INC_AUTH_FAILED:
640 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
643 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
648 case INC_SOCKET_ERROR:
650 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
653 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
656 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
657 if (!inc_dialog->show_dialog)
664 if (pop3_session->error_val == PS_AUTHFAIL) {
665 if(!prefs_common.no_recv_err_panel) {
666 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
667 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
668 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
672 /* CLAWS: perform filtering actions on dropped message */
673 /* CLAWS: get default inbox (perhaps per account) */
674 if (pop3_session->ac_prefs->inbox) {
675 /* CLAWS: get destination folder / mailbox */
676 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
678 inbox = folder_get_default_inbox();
680 inbox = folder_get_default_inbox();
682 /* get list of messages in processing */
683 processing = folder_get_default_processing();
684 folder_item_scan(processing);
685 msglist = folder_item_get_msg_list(processing);
687 /* process messages */
688 folder_item_update_freeze();
690 procmsg_msglist_filter(msglist, pop3_session->ac_prefs,
691 &filtered, &unfiltered,
692 pop3_session->ac_prefs->filter_on_recv);
694 filtering_move_and_copy_msgs(msglist);
695 if (unfiltered != NULL)
696 folder_item_move_msgs(inbox, unfiltered);
698 for(msglist_element = msglist; msglist_element != NULL;
699 msglist_element = msglist_element->next) {
700 MsgInfo *msginfo = (MsgInfo *)msglist_element->data;
701 procmsg_msginfo_free(msginfo);
703 folder_item_update_thaw();
705 g_slist_free(msglist);
706 g_slist_free(filtered);
707 g_slist_free(unfiltered);
711 new_msgs += pop3_session->cur_total_num;
713 if (pop3_session->error_val == PS_AUTHFAIL &&
714 pop3_session->ac_prefs->tmp_pass) {
715 g_free(pop3_session->ac_prefs->tmp_pass);
716 pop3_session->ac_prefs->tmp_pass = NULL;
719 pop3_write_uidl_list(pop3_session);
721 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
723 if (inc_dialog->show_dialog)
724 manage_window_focus_in
725 (inc_dialog->dialog->window,
727 inc_put_error(inc_state, pop3_session);
728 if (inc_dialog->show_dialog)
729 manage_window_focus_out
730 (inc_dialog->dialog->window,
732 if (inc_state == INC_NO_SPACE ||
733 inc_state == INC_IO_ERROR)
736 folder_item_free_cache(processing, TRUE);
738 inc_session_destroy(session);
739 inc_dialog->queue_list =
740 g_list_remove(inc_dialog->queue_list, session);
743 #undef SET_PIXMAP_AND_TEXT
746 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
747 "Finished (%d new messages)",
748 new_msgs), new_msgs);
750 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
752 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
754 while (inc_dialog->queue_list != NULL) {
755 session = inc_dialog->queue_list->data;
756 inc_session_destroy(session);
757 inc_dialog->queue_list =
758 g_list_remove(inc_dialog->queue_list, session);
761 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
762 inc_progress_dialog_destroy(inc_dialog);
764 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
766 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
775 static IncState inc_pop3_session_do(IncSession *session)
777 Pop3Session *pop3_session = POP3_SESSION(session->session);
778 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
783 debug_print("getting new messages of account %s...\n",
784 pop3_session->ac_prefs->account_name);
786 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
788 buf = g_strdup_printf(_("%s: Retrieving new messages"),
789 pop3_session->ac_prefs->recv_server);
790 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
793 server = pop3_session->ac_prefs->recv_server;
794 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
795 port = pop3_session->ac_prefs->set_popport ?
796 pop3_session->ac_prefs->popport :
797 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
798 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
799 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
800 SESSION(pop3_session)->nonblocking =
801 pop3_session->ac_prefs->use_nonblocking_ssl;
803 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE) {
804 if (alertpanel_full(_("Insecure connection"),
805 _("This connection is configured to be secured "
806 "using SSL, but SSL is not available in this "
807 "build of Claws Mail. \n\n"
808 "Do you want to continue connecting to this "
809 "server? The communication would not be "
811 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
812 NULL, FALSE, NULL, ALERT_WARNING,
813 G_ALERTDEFAULT) != G_ALERTALTERNATE)
816 port = pop3_session->ac_prefs->set_popport ?
817 pop3_session->ac_prefs->popport : 110;
820 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
821 statuswindow_print_all("%s", buf);
822 log_message(LOG_PROTOCOL, "%s\n", buf);
824 progress_dialog_set_label(inc_dialog->dialog, buf);
828 session_set_timeout(SESSION(pop3_session),
829 prefs_common.io_timeout_secs * 1000);
831 if (session_connect(SESSION(pop3_session), server, port) < 0) {
832 if(!prefs_common.no_recv_err_panel) {
833 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
834 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
835 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
837 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
839 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
841 log_error(LOG_PROTOCOL, _("Can't connect to POP3 server: %s:%d\n"),
844 session->inc_state = INC_CONNECT_ERROR;
845 statuswindow_pop_all();
846 return INC_CONNECT_ERROR;
849 while (session_is_connected(SESSION(pop3_session)) &&
850 session->inc_state != INC_CANCEL)
851 gtk_main_iteration();
853 if (session->inc_state == INC_SUCCESS) {
854 switch (pop3_session->error_val) {
856 switch (SESSION(pop3_session)->state) {
858 if (pop3_session->state == POP3_READY)
859 session->inc_state = INC_CONNECT_ERROR;
861 session->inc_state = INC_ERROR;
864 session->inc_state = INC_EOF;
866 case SESSION_TIMEOUT:
867 session->inc_state = INC_TIMEOUT;
870 session->inc_state = INC_SUCCESS;
875 session->inc_state = INC_AUTH_FAILED;
878 session->inc_state = INC_IO_ERROR;
881 session->inc_state = INC_SOCKET_ERROR;
884 session->inc_state = INC_LOCKED;
887 session->inc_state = INC_ERROR;
892 session_disconnect(SESSION(pop3_session));
895 return session->inc_state;
898 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
899 IncSession *inc_session)
901 inc_progress_dialog_set_label(inc_dialog, inc_session);
902 inc_progress_dialog_set_progress(inc_dialog, inc_session);
905 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
906 IncSession *inc_session)
908 ProgressDialog *dialog = inc_dialog->dialog;
909 Pop3Session *session;
911 g_return_if_fail(inc_session != NULL);
913 session = POP3_SESSION(inc_session->session);
915 switch (session->state) {
918 case POP3_GETAUTH_USER:
919 case POP3_GETAUTH_PASS:
920 case POP3_GETAUTH_APOP:
921 progress_dialog_set_label(dialog, _("Authenticating..."));
922 statuswindow_pop_all();
923 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
924 SESSION(session)->server,
925 session->ac_prefs->account_name);
927 case POP3_GETRANGE_STAT:
928 progress_dialog_set_label
929 (dialog, _("Getting the number of new messages (STAT)..."));
931 case POP3_GETRANGE_LAST:
932 progress_dialog_set_label
933 (dialog, _("Getting the number of new messages (LAST)..."));
935 case POP3_GETRANGE_UIDL:
936 progress_dialog_set_label
937 (dialog, _("Getting the number of new messages (UIDL)..."));
939 case POP3_GETSIZE_LIST:
940 progress_dialog_set_label
941 (dialog, _("Getting the size of messages (LIST)..."));
948 progress_dialog_set_label(dialog, _("Quitting"));
955 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
956 IncSession *inc_session)
958 gchar buf[MSGBUFSIZE];
959 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
960 gchar *total_size_str;
964 if (!pop3_session->new_msg_exist) return;
966 cur_total = inc_session->cur_total_bytes;
967 total = pop3_session->total_bytes;
968 if (pop3_session->state == POP3_RETR ||
969 pop3_session->state == POP3_RETR_RECV ||
970 pop3_session->state == POP3_DELETE) {
971 Xstrdup_a(total_size_str, to_human_readable((goffset)total), return);
972 g_snprintf(buf, sizeof(buf),
973 _("Retrieving message (%d / %d) (%s / %s)"),
974 pop3_session->cur_msg, pop3_session->count,
975 to_human_readable((goffset)cur_total), total_size_str);
976 progress_dialog_set_label(inc_dialog->dialog, buf);
979 progress_dialog_set_fraction
980 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
982 g_snprintf(buf, sizeof(buf), "%d / %d",
983 pop3_session->cur_msg, pop3_session->count);
984 gtk_progress_bar_set_text
985 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), buf);
986 gtk_progress_bar_set_fraction
987 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
988 (total == 0) ? 0 : (gfloat)cur_total / (gfloat)total);
990 if (pop3_session->cur_total_num > 0) {
991 g_snprintf(buf, sizeof(buf),
992 ngettext("Retrieving (%d message (%s) received)",
993 "Retrieving (%d messages (%s) received)",
994 pop3_session->cur_total_num),
995 pop3_session->cur_total_num,
997 ((goffset)pop3_session->cur_total_recv_bytes));
998 progress_dialog_list_set_status(inc_dialog->dialog,
1004 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
1005 IncSession *inc_session)
1011 g_get_current_time(&tv_cur);
1013 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
1014 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
1015 if (tv_result.tv_usec < 0) {
1017 tv_result.tv_usec += G_USEC_PER_SEC;
1020 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
1021 if (msec > PROGRESS_UPDATE_INTERVAL) {
1022 inc_progress_dialog_update(inc_dialog, inc_session);
1023 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
1024 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
1028 static gint inc_recv_data_progressive(Session *session, guint cur_len,
1029 guint total_len, gpointer data)
1031 IncSession *inc_session = (IncSession *)data;
1032 Pop3Session *pop3_session = POP3_SESSION(session);
1033 IncProgressDialog *inc_dialog;
1036 g_return_val_if_fail(inc_session != NULL, -1);
1038 if (pop3_session->state != POP3_RETR &&
1039 pop3_session->state != POP3_RETR_RECV &&
1040 pop3_session->state != POP3_DELETE &&
1041 pop3_session->state != POP3_LOGOUT) return 0;
1043 if (!pop3_session->new_msg_exist) return 0;
1045 cur_total = pop3_session->cur_total_bytes + cur_len;
1046 if (cur_total > pop3_session->total_bytes)
1047 cur_total = pop3_session->total_bytes;
1048 inc_session->cur_total_bytes = cur_total;
1050 inc_dialog = (IncProgressDialog *)inc_session->data;
1051 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1056 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1058 IncSession *inc_session = (IncSession *)data;
1059 IncProgressDialog *inc_dialog;
1061 g_return_val_if_fail(inc_session != NULL, -1);
1063 inc_dialog = (IncProgressDialog *)inc_session->data;
1065 inc_recv_data_progressive(session, 0, 0, inc_session);
1067 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1068 inc_progress_dialog_update(inc_dialog, inc_session);
1074 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1076 IncSession *inc_session = (IncSession *)data;
1077 IncProgressDialog *inc_dialog;
1079 g_return_val_if_fail(inc_session != NULL, -1);
1081 inc_dialog = (IncProgressDialog *)inc_session->data;
1083 switch (POP3_SESSION(session)->state) {
1084 case POP3_GETAUTH_USER:
1085 case POP3_GETAUTH_PASS:
1086 case POP3_GETAUTH_APOP:
1087 case POP3_GETRANGE_STAT:
1088 case POP3_GETRANGE_LAST:
1089 case POP3_GETRANGE_UIDL:
1090 case POP3_GETSIZE_LIST:
1091 inc_progress_dialog_update(inc_dialog, inc_session);
1094 inc_recv_data_progressive(session, 0, 0, inc_session);
1097 inc_progress_dialog_update(inc_dialog, inc_session);
1106 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1109 FolderItem *dropfolder;
1110 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1113 g_return_val_if_fail(inc_session != NULL, -1);
1115 if (session->ac_prefs->inbox) {
1116 inbox = folder_find_item_from_identifier
1117 (session->ac_prefs->inbox);
1119 inbox = folder_get_default_inbox();
1121 inbox = folder_get_default_inbox();
1127 /* CLAWS: claws uses a global .processing folder for the filtering. */
1128 dropfolder = folder_get_default_processing();
1130 /* add msg file to drop folder */
1131 if ((msgnum = folder_item_add_msg(
1132 dropfolder, file, NULL, TRUE)) < 0) {
1140 static void inc_put_error(IncState istate, Pop3Session *session)
1142 gchar *log_msg = NULL;
1143 gchar *err_msg = NULL;
1144 gboolean fatal_error = FALSE;
1147 case INC_CONNECT_ERROR:
1148 log_msg = _("Connection failed.");
1150 if (prefs_common.no_recv_err_panel)
1152 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1153 SESSION(session)->server,
1154 SESSION(session)->port);
1157 log_msg = _("Error occurred while processing mail.");
1159 if (prefs_common.no_recv_err_panel)
1161 if (session->error_msg)
1162 err_msg = g_strdup_printf
1163 (_("Error occurred while processing mail:\n%s"),
1164 session->error_msg);
1166 err_msg = g_strdup(log_msg);
1169 log_msg = _("No disk space left.");
1170 err_msg = g_strdup(log_msg);
1174 log_msg = _("Can't write file.");
1175 err_msg = g_strdup(log_msg);
1178 case INC_SOCKET_ERROR:
1179 log_msg = _("Socket error.");
1180 if (prefs_common.no_recv_err_panel)
1182 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1183 SESSION(session)->server,
1184 SESSION(session)->port);
1187 log_msg = _("Connection closed by the remote host.");
1188 if (prefs_common.no_recv_err_panel)
1190 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1191 SESSION(session)->server,
1192 SESSION(session)->port);
1195 log_msg = _("Mailbox is locked.");
1196 if (prefs_common.no_recv_err_panel)
1198 if (session->error_msg)
1199 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1200 session->error_msg);
1202 err_msg = g_strdup(log_msg);
1204 case INC_AUTH_FAILED:
1205 log_msg = _("Authentication failed.");
1207 if (prefs_common.no_recv_err_panel)
1209 if (session->error_msg)
1210 err_msg = g_strdup_printf
1211 (_("Authentication failed:\n%s"), session->error_msg);
1213 err_msg = g_strdup(log_msg);
1216 log_msg = _("Session timed out. You may be able to "
1217 "recover by increasing the timeout value in "
1218 "Preferences/Other.");
1219 if (prefs_common.no_recv_err_panel)
1221 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1222 SESSION(session)->server,
1223 SESSION(session)->port);
1231 log_error(LOG_PROTOCOL, "%s\n", log_msg);
1233 log_warning(LOG_PROTOCOL, "%s\n", log_msg);
1235 if (prefs_common.no_recv_err_panel && fatal_error)
1236 mainwindow_show_error();
1239 alertpanel_error_log("%s", err_msg);
1244 static void inc_cancel(IncProgressDialog *dialog)
1246 IncSession *session;
1248 g_return_if_fail(dialog != NULL);
1250 if (dialog->queue_list == NULL) {
1251 inc_progress_dialog_destroy(dialog);
1255 session = dialog->queue_list->data;
1257 session->inc_state = INC_CANCEL;
1259 log_message(LOG_PROTOCOL, _("Incorporation cancelled\n"));
1262 gboolean inc_is_active(void)
1264 return (inc_dialog_list != NULL);
1267 void inc_cancel_all(void)
1271 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1272 inc_cancel((IncProgressDialog *)cur->data);
1275 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1277 inc_cancel((IncProgressDialog *)data);
1280 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1283 IncProgressDialog *dialog = (IncProgressDialog *)data;
1285 if (dialog->queue_list == NULL)
1286 inc_progress_dialog_destroy(dialog);
1291 static gint inc_spool_account(PrefsAccount *account)
1297 if (account->local_inbox) {
1298 inbox = folder_find_item_from_identifier(account->local_inbox);
1300 inbox = folder_get_default_inbox();
1302 inbox = folder_get_default_inbox();
1304 if (account->local_mbox) {
1305 if (is_file_exist(account->local_mbox))
1306 mbox = g_strdup(account->local_mbox);
1307 else if (is_dir_exist(account->local_mbox))
1308 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1309 g_get_user_name(), NULL);
1311 debug_print("%s: local mailbox not found.\n",
1312 account->local_mbox);
1316 debug_print("local mailbox not set in account info.\n");
1320 result = get_spool(inbox, mbox, account);
1323 statusbar_pop_all();
1328 static gint inc_all_spool(void)
1332 gint account_new_msgs = 0;
1334 list = account_get_list();
1335 if (!list) return 0;
1337 for (; list != NULL; list = list->next) {
1338 PrefsAccount *account = list->data;
1340 if ((account->protocol == A_LOCAL) &&
1341 (account->recv_at_getall)) {
1342 account_new_msgs = inc_spool_account(account);
1343 if (account_new_msgs > 0)
1344 new_msgs += account_new_msgs;
1351 static gint get_spool(FolderItem *dest, const gchar *mbox, PrefsAccount *account)
1355 gchar tmp_mbox[MAXPATHLEN + 1];
1357 g_return_val_if_fail(dest != NULL, -1);
1358 g_return_val_if_fail(mbox != NULL, -1);
1359 g_return_val_if_fail(account != NULL, -1);
1361 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1362 debug_print("%s: no messages in local mailbox.\n", mbox);
1364 } else if (size < 0)
1367 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1370 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%p",
1371 get_tmp_dir(), G_DIR_SEPARATOR, mbox);
1373 if (copy_mbox(lockfd, tmp_mbox) < 0) {
1374 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1378 debug_print("Getting new messages from %s into %s...\n",
1381 msgs = proc_mbox(dest, tmp_mbox, account->filter_on_recv, account);
1383 claws_unlink(tmp_mbox);
1384 if (msgs >= 0) empty_mbox(mbox);
1385 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1390 void inc_lock_real(void)
1395 void inc_unlock_real(void)
1397 if (inc_lock_count > 0)
1401 static guint autocheck_timer = 0;
1402 static gpointer autocheck_data = NULL;
1405 osso_context_t *get_osso_context(void);
1408 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1412 gchar *buf, *numpos, *ret_str;
1413 gssize by_read = 0, by_written = 0;
1415 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1416 *prefs_common.newmail_notify_cmd))
1419 buf = g_strdup(prefs_common.newmail_notify_cmd);
1420 if ((numpos = strstr(buf, "%d")) != NULL) {
1424 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1429 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1431 if (ret_str && by_written) {
1435 debug_print("executing new mail notification command: %s\n", buf);
1436 execute_command_line(buf, TRUE);
1442 if (prefs_common.maemo_play_sound)
1443 hildon_play_system_sound("/usr/share/sounds/ui-new_email.wav");
1444 if (prefs_common.maemo_show_banner) {
1445 gchar *info = g_strdup_printf(ngettext("Claws Mail: %d new message",
1446 "Claws Mail: %d new messages",
1447 new_msgs), new_msgs);
1448 osso_system_note_infoprint(get_osso_context(), info, NULL);
1455 #if (defined(MAEMO) && defined(CONIC))
1456 static void maemo_connection_event(ConIcConnection *connection,
1457 ConIcConnectionEvent *event,
1460 ConIcConnectionStatus status =
1461 con_ic_connection_event_get_status(event);
1462 MainWindow *mainwin = (MainWindow *)user_data;
1464 case CON_IC_STATUS_CONNECTED:
1465 debug_print("we're connected\n");
1466 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1469 debug_print("we're disconnected\n");
1470 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1471 maemo_warned_offline = FALSE;
1478 void inc_autocheck_timer_init(MainWindow *mainwin)
1480 #if (defined(MAEMO) && defined(CONIC))
1481 GValue *val = g_new0(GValue, 1);
1482 maemo_connection = con_ic_connection_new();
1484 g_value_init(val, G_TYPE_BOOLEAN);
1485 g_value_set_boolean(val, TRUE);
1486 g_object_set_property(G_OBJECT(maemo_connection),
1487 "automatic-connection-events", val);
1489 g_signal_connect (maemo_connection, "connection-event",
1490 G_CALLBACK(maemo_connection_event), mainwin);
1491 con_ic_connection_connect (maemo_connection,
1492 CON_IC_CONNECT_FLAG_AUTOMATICALLY_TRIGGERED);
1493 autocheck_data = mainwin;
1495 autocheck_data = mainwin;
1496 inc_autocheck_timer_set();
1500 static void inc_autocheck_timer_set_interval(guint interval)
1502 inc_autocheck_timer_remove();
1503 /* last test is to avoid re-enabling auto_check after modifying
1504 the common preferences */
1505 if (prefs_common.autochk_newmail && autocheck_data
1506 && prefs_common.work_offline == FALSE) {
1507 autocheck_timer = g_timeout_add
1508 (interval, inc_autocheck_func, autocheck_data);
1509 debug_print("added timer = %d\n", autocheck_timer);
1513 void inc_autocheck_timer_set(void)
1515 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1516 #if (defined(MAEMO) && defined(CONIC))
1517 con_ic_connection_connect (maemo_connection,
1518 CON_IC_CONNECT_FLAG_AUTOMATICALLY_TRIGGERED);
1522 void inc_autocheck_timer_remove(void)
1524 if (autocheck_timer) {
1525 debug_print("removed timer = %d\n", autocheck_timer);
1526 g_source_remove(autocheck_timer);
1527 autocheck_timer = 0;
1531 static gint inc_autocheck_func(gpointer data)
1533 MainWindow *mainwin = (MainWindow *)data;
1535 if (inc_lock_count) {
1536 debug_print("autocheck is locked.\n");
1537 inc_autocheck_timer_set_interval(1000);
1541 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1546 gboolean inc_offline_should_override(gboolean force_ask, const gchar *msg)
1548 static time_t overridden_yes = 0;
1549 static time_t overridden_no = 0;
1550 int length = 10; /* minutes */
1551 gint answer = G_ALERTDEFAULT;
1553 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1554 /* If no network connection is available, override is not possible */
1555 if(!networkmanager_is_online(NULL))
1559 #if (defined(MAEMO) && defined(CONIC))
1560 if (prefs_common.work_offline) {
1561 if (force_ask && !maemo_warned_offline) {
1562 if (mainwindow_get_mainwindow())
1563 hildon_banner_show_information(
1564 mainwindow_get_mainwindow()->window,
1566 _("Unable to connect: you are offline."));
1567 maemo_warned_offline = TRUE;
1575 if (prefs_common.autochk_newmail)
1576 length = prefs_common.autochk_itv; /* minutes */
1579 overridden_no = (time_t)0;
1582 if (prefs_common.work_offline) {
1585 if (time(NULL) - overridden_yes < length * 60) /* seconds */
1587 else if (time(NULL) - overridden_no < length * 60) /* seconds */
1591 tmp = g_strdup_printf(
1592 _("%s%sYou're working offline. Override for %d minutes?"),
1597 tmp = g_strdup_printf(
1598 _("%s%sYou're working offline. Override?"),
1602 answer = alertpanel(_("Offline warning"),
1604 GTK_STOCK_NO, "+" GTK_STOCK_YES,
1605 !force_ask? _("On_ly once"):NULL);
1607 if (answer == G_ALERTALTERNATE) {
1608 overridden_yes = time(NULL);
1610 } else if (answer == G_ALERTDEFAULT) {
1612 overridden_no = time(NULL);
1615 overridden_yes = (time_t)0;
1616 overridden_no = (time_t)0;