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 static 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();
211 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
212 if (account_new_msgs > 0)
213 new_msgs += account_new_msgs;
216 inc_finished(mainwin, new_msgs > 0, FALSE);
217 main_window_unlock(mainwin);
218 inc_notify_cmd(new_msgs, notify);
219 inc_autocheck_timer_set();
223 void inc_pop_before_smtp(PrefsAccount *acc)
225 IncProgressDialog *inc_dialog;
229 mainwin = mainwindow_get_mainwindow();
231 session = inc_session_new(acc);
232 if (!session) return;
233 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
235 inc_dialog = inc_progress_dialog_create(FALSE);
236 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
238 /* FIXME: assumes to attach to first main window */
239 inc_dialog->mainwin = mainwin;
240 inc_progress_dialog_set_list(inc_dialog);
243 toolbar_main_set_sensitive(mainwin);
244 main_window_set_menu_sensitive(mainwin);
247 inc_start(inc_dialog);
250 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
252 IncProgressDialog *inc_dialog;
255 switch (account->protocol) {
259 * FIXME: it should return foldeview_check_new() value.
260 * TODO: do it when bug [19] is fixed (IMAP folder sets
261 * an incorrect new message count)
263 folderview_check_new(FOLDER(account->folder));
267 session = inc_session_new(account);
268 if (!session) return 0;
270 inc_dialog = inc_progress_dialog_create(FALSE);
271 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
273 inc_dialog->mainwin = mainwin;
274 inc_progress_dialog_set_list(inc_dialog);
277 toolbar_main_set_sensitive(mainwin);
278 main_window_set_menu_sensitive(mainwin);
281 return inc_start(inc_dialog);
284 return inc_spool_account(account);
292 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
296 if (inc_lock_count) return 0;
298 if (prefs_common.work_offline &&
299 !inc_offline_should_override(TRUE,
300 _("Claws Mail needs network access in order "
304 inc_autocheck_timer_remove();
305 main_window_lock(mainwin);
307 new_msgs = inc_account_mail_real(mainwin, account);
309 inc_finished(mainwin, new_msgs > 0, FALSE);
310 main_window_unlock(mainwin);
311 inc_autocheck_timer_set();
316 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
319 GList *list, *queue_list = NULL;
320 IncProgressDialog *inc_dialog;
322 gint account_new_msgs = 0;
324 if (prefs_common.work_offline &&
325 !inc_offline_should_override( (autocheck == FALSE),
326 _("Claws Mail needs network access in order "
330 if (inc_lock_count) return;
332 inc_autocheck_timer_remove();
333 main_window_lock(mainwin);
335 list = account_get_list();
337 inc_finished(mainwin, new_msgs > 0, autocheck);
338 main_window_unlock(mainwin);
339 inc_notify_cmd(new_msgs, notify);
340 inc_autocheck_timer_set();
344 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
345 /* external incorporating program */
346 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
347 log_error(LOG_PROTOCOL, _("%s failed\n"), prefs_common.extinc_cmd);
349 main_window_unlock(mainwin);
350 inc_autocheck_timer_set();
355 /* check local folders */
356 account_new_msgs = inc_all_spool();
357 if (account_new_msgs > 0)
358 new_msgs += account_new_msgs;
360 /* check IMAP4 / News folders */
361 for (list = account_get_list(); list != NULL; list = list->next) {
362 PrefsAccount *account = list->data;
363 if ((account->protocol == A_IMAP4 ||
364 account->protocol == A_NNTP) && account->recv_at_getall) {
365 new_msgs += folderview_check_new(FOLDER(account->folder));
369 /* check POP3 accounts */
370 for (list = account_get_list(); list != NULL; list = list->next) {
372 PrefsAccount *account = list->data;
374 if (account->recv_at_getall) {
375 session = inc_session_new(account);
377 queue_list = g_list_append(queue_list, session);
382 inc_dialog = inc_progress_dialog_create(autocheck);
383 inc_dialog->queue_list = queue_list;
384 inc_dialog->mainwin = mainwin;
385 inc_progress_dialog_set_list(inc_dialog);
387 toolbar_main_set_sensitive(mainwin);
388 main_window_set_menu_sensitive(mainwin);
389 new_msgs += inc_start(inc_dialog);
392 inc_finished(mainwin, new_msgs > 0, autocheck);
393 main_window_unlock(mainwin);
394 inc_notify_cmd(new_msgs, notify);
395 inc_autocheck_timer_set();
398 static void inc_progress_dialog_size_allocate_cb(GtkWidget *widget,
399 GtkAllocation *allocation)
401 g_return_if_fail(allocation != NULL);
403 prefs_common.receivewin_width = allocation->width;
404 prefs_common.receivewin_height = allocation->height;
407 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
409 IncProgressDialog *dialog;
410 ProgressDialog *progress;
411 static GdkGeometry geometry;
413 dialog = g_new0(IncProgressDialog, 1);
415 progress = progress_dialog_create();
416 gtk_window_set_title(GTK_WINDOW(progress->window),
417 _("Retrieving new messages"));
418 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
419 G_CALLBACK(inc_cancel_cb), dialog);
420 g_signal_connect(G_OBJECT(progress->window), "delete_event",
421 G_CALLBACK(inc_dialog_delete_cb), dialog);
422 g_signal_connect(G_OBJECT(progress->window), "size_allocate",
423 G_CALLBACK(inc_progress_dialog_size_allocate_cb), NULL);
424 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
426 progress_dialog_get_fraction(progress);
428 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_COMPLETE,
430 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_CONTINUE,
432 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_ERROR,
435 if (!geometry.min_height) {
436 geometry.min_width = 460;
437 geometry.min_height = 250;
440 gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
442 gtk_widget_set_size_request(progress->window, prefs_common.receivewin_width,
443 prefs_common.receivewin_height);
445 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
446 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
448 dialog->show_dialog = TRUE;
449 gtk_widget_show_now(progress->window);
452 dialog->dialog = progress;
453 g_get_current_time(&dialog->progress_tv);
454 g_get_current_time(&dialog->folder_tv);
455 dialog->queue_list = NULL;
458 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
463 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
467 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
468 IncSession *session = list->data;
469 Pop3Session *pop3_session = POP3_SESSION(session->session);
471 session->data = inc_dialog;
473 progress_dialog_list_set(inc_dialog->dialog,
475 pop3_session->ac_prefs->account_name,
480 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
482 progress_dialog_get_fraction(inc_dialog->dialog);
483 progress_dialog_set_label(inc_dialog->dialog, "");
484 if (inc_dialog->mainwin)
485 main_window_progress_off(inc_dialog->mainwin);
488 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
490 g_return_if_fail(inc_dialog != NULL);
492 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
494 if (inc_dialog->mainwin)
495 main_window_progress_off(inc_dialog->mainwin);
496 progress_dialog_destroy(inc_dialog->dialog);
501 static IncSession *inc_session_new(PrefsAccount *account)
505 g_return_val_if_fail(account != NULL, NULL);
507 if (account->protocol != A_POP3)
509 if (!account->recv_server || !account->userid)
512 session = g_new0(IncSession, 1);
514 session->session = pop3_session_new(account);
515 session->session->data = session;
516 POP3_SESSION(session->session)->drop_message = inc_drop_message;
517 session_set_recv_message_notify(session->session,
518 inc_recv_message, session);
519 session_set_recv_data_progressive_notify(session->session,
520 inc_recv_data_progressive,
522 session_set_recv_data_notify(session->session,
523 inc_recv_data_finished, session);
528 static void inc_session_destroy(IncSession *session)
530 g_return_if_fail(session != NULL);
532 session_destroy(session->session);
536 static gint inc_start(IncProgressDialog *inc_dialog)
540 Pop3Session *pop3_session;
546 FolderItem *processing, *inbox;
547 GSList *msglist, *msglist_element;
548 gboolean cancelled = FALSE;
550 qlist = inc_dialog->queue_list;
551 while (qlist != NULL) {
552 GList *next = qlist->next;
554 session = qlist->data;
555 pop3_session = POP3_SESSION(session->session);
556 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
557 if (pop3_session->ac_prefs->passwd)
559 g_strdup(pop3_session->ac_prefs->passwd);
560 else if (pop3_session->ac_prefs->tmp_pass)
562 g_strdup(pop3_session->ac_prefs->tmp_pass);
566 if (inc_dialog->show_dialog)
567 manage_window_focus_in
568 (inc_dialog->dialog->window,
571 pass = input_dialog_query_password
572 (pop3_session->ac_prefs->recv_server,
575 if (inc_dialog->show_dialog)
576 manage_window_focus_out
577 (inc_dialog->dialog->window,
581 pop3_session->ac_prefs->tmp_pass =
583 pop3_session->pass = pass;
590 #define SET_PIXMAP_AND_TEXT(pix, str) \
592 progress_dialog_list_set(inc_dialog->dialog, \
593 inc_dialog->cur_row, \
599 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
600 session = inc_dialog->queue_list->data;
601 pop3_session = POP3_SESSION(session->session);
602 GSList *filtered, *unfiltered;
604 if (pop3_session->pass == NULL) {
605 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
606 inc_session_destroy(session);
607 inc_dialog->queue_list =
608 g_list_remove(inc_dialog->queue_list, session);
612 inc_progress_dialog_clear(inc_dialog);
613 progress_dialog_scroll_to_row(inc_dialog->dialog,
614 inc_dialog->cur_row);
616 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
618 /* begin POP3 session */
619 inc_state = inc_pop3_session_do(session);
623 if (pop3_session->cur_total_num > 0)
624 msg = g_strdup_printf(
625 ngettext("Done (%d message (%s) received)",
626 "Done (%d messages (%s) received)",
627 pop3_session->cur_total_num),
628 pop3_session->cur_total_num,
629 to_human_readable(pop3_session->cur_total_recv_bytes));
631 msg = g_strdup_printf(_("Done (no new messages)"));
632 SET_PIXMAP_AND_TEXT(okpix, msg);
635 case INC_CONNECT_ERROR:
636 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
638 case INC_AUTH_FAILED:
639 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
642 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
647 case INC_SOCKET_ERROR:
649 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
652 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
655 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
656 if (!inc_dialog->show_dialog)
663 if (pop3_session->error_val == PS_AUTHFAIL) {
664 if(!prefs_common.no_recv_err_panel) {
665 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
666 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
667 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
671 /* CLAWS: perform filtering actions on dropped message */
672 /* CLAWS: get default inbox (perhaps per account) */
673 if (pop3_session->ac_prefs->inbox) {
674 /* CLAWS: get destination folder / mailbox */
675 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
677 inbox = folder_get_default_inbox();
679 inbox = folder_get_default_inbox();
681 /* get list of messages in processing */
682 processing = folder_get_default_processing();
683 folder_item_scan(processing);
684 msglist = folder_item_get_msg_list(processing);
686 /* process messages */
687 folder_item_update_freeze();
689 procmsg_msglist_filter(msglist, pop3_session->ac_prefs,
690 &filtered, &unfiltered,
691 pop3_session->ac_prefs->filter_on_recv);
693 filtering_move_and_copy_msgs(msglist);
694 if (unfiltered != NULL)
695 folder_item_move_msgs(inbox, unfiltered);
697 for(msglist_element = msglist; msglist_element != NULL;
698 msglist_element = msglist_element->next) {
699 MsgInfo *msginfo = (MsgInfo *)msglist_element->data;
700 procmsg_msginfo_free(msginfo);
702 folder_item_update_thaw();
704 g_slist_free(msglist);
705 g_slist_free(filtered);
706 g_slist_free(unfiltered);
710 new_msgs += pop3_session->cur_total_num;
712 if (pop3_session->error_val == PS_AUTHFAIL &&
713 pop3_session->ac_prefs->tmp_pass) {
714 g_free(pop3_session->ac_prefs->tmp_pass);
715 pop3_session->ac_prefs->tmp_pass = NULL;
718 pop3_write_uidl_list(pop3_session);
720 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
722 if (inc_dialog->show_dialog)
723 manage_window_focus_in
724 (inc_dialog->dialog->window,
726 inc_put_error(inc_state, pop3_session);
727 if (inc_dialog->show_dialog)
728 manage_window_focus_out
729 (inc_dialog->dialog->window,
731 if (inc_state == INC_NO_SPACE ||
732 inc_state == INC_IO_ERROR)
735 folder_item_free_cache(processing, TRUE);
737 inc_session_destroy(session);
738 inc_dialog->queue_list =
739 g_list_remove(inc_dialog->queue_list, session);
742 #undef SET_PIXMAP_AND_TEXT
745 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
746 "Finished (%d new messages)",
747 new_msgs), new_msgs);
749 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
751 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
753 while (inc_dialog->queue_list != NULL) {
754 session = inc_dialog->queue_list->data;
755 inc_session_destroy(session);
756 inc_dialog->queue_list =
757 g_list_remove(inc_dialog->queue_list, session);
760 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
761 inc_progress_dialog_destroy(inc_dialog);
763 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
765 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
774 static IncState inc_pop3_session_do(IncSession *session)
776 Pop3Session *pop3_session = POP3_SESSION(session->session);
777 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
782 debug_print("getting new messages of account %s...\n",
783 pop3_session->ac_prefs->account_name);
785 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
787 buf = g_strdup_printf(_("%s: Retrieving new messages"),
788 pop3_session->ac_prefs->recv_server);
789 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
792 server = pop3_session->ac_prefs->recv_server;
793 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
794 port = pop3_session->ac_prefs->set_popport ?
795 pop3_session->ac_prefs->popport :
796 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
797 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
798 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
799 SESSION(pop3_session)->nonblocking =
800 pop3_session->ac_prefs->use_nonblocking_ssl;
802 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE) {
803 if (alertpanel_full(_("Insecure connection"),
804 _("This connection is configured to be secured "
805 "using SSL, but SSL is not available in this "
806 "build of Claws Mail. \n\n"
807 "Do you want to continue connecting to this "
808 "server? The communication would not be "
810 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
811 NULL, FALSE, NULL, ALERT_WARNING,
812 G_ALERTDEFAULT) != G_ALERTALTERNATE)
815 port = pop3_session->ac_prefs->set_popport ?
816 pop3_session->ac_prefs->popport : 110;
819 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
820 statuswindow_print_all("%s", buf);
821 log_message(LOG_PROTOCOL, "%s\n", buf);
823 progress_dialog_set_label(inc_dialog->dialog, buf);
827 session_set_timeout(SESSION(pop3_session),
828 prefs_common.io_timeout_secs * 1000);
830 if (session_connect(SESSION(pop3_session), server, port) < 0) {
831 if(!prefs_common.no_recv_err_panel) {
832 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
833 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
834 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
836 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
838 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
840 log_error(LOG_PROTOCOL, _("Can't connect to POP3 server: %s:%d\n"),
843 session->inc_state = INC_CONNECT_ERROR;
844 statuswindow_pop_all();
845 return INC_CONNECT_ERROR;
848 while (session_is_connected(SESSION(pop3_session)) &&
849 session->inc_state != INC_CANCEL)
850 gtk_main_iteration();
852 if (session->inc_state == INC_SUCCESS) {
853 switch (pop3_session->error_val) {
855 switch (SESSION(pop3_session)->state) {
857 if (pop3_session->state == POP3_READY)
858 session->inc_state = INC_CONNECT_ERROR;
860 session->inc_state = INC_ERROR;
863 session->inc_state = INC_EOF;
865 case SESSION_TIMEOUT:
866 session->inc_state = INC_TIMEOUT;
869 session->inc_state = INC_SUCCESS;
874 session->inc_state = INC_AUTH_FAILED;
877 session->inc_state = INC_IO_ERROR;
880 session->inc_state = INC_SOCKET_ERROR;
883 session->inc_state = INC_LOCKED;
886 session->inc_state = INC_ERROR;
891 session_disconnect(SESSION(pop3_session));
894 return session->inc_state;
897 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
898 IncSession *inc_session)
900 inc_progress_dialog_set_label(inc_dialog, inc_session);
901 inc_progress_dialog_set_progress(inc_dialog, inc_session);
904 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
905 IncSession *inc_session)
907 ProgressDialog *dialog = inc_dialog->dialog;
908 Pop3Session *session;
910 g_return_if_fail(inc_session != NULL);
912 session = POP3_SESSION(inc_session->session);
914 switch (session->state) {
917 case POP3_GETAUTH_USER:
918 case POP3_GETAUTH_PASS:
919 case POP3_GETAUTH_APOP:
920 progress_dialog_set_label(dialog, _("Authenticating..."));
921 statuswindow_pop_all();
922 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
923 SESSION(session)->server,
924 session->ac_prefs->account_name);
926 case POP3_GETRANGE_STAT:
927 progress_dialog_set_label
928 (dialog, _("Getting the number of new messages (STAT)..."));
930 case POP3_GETRANGE_LAST:
931 progress_dialog_set_label
932 (dialog, _("Getting the number of new messages (LAST)..."));
934 case POP3_GETRANGE_UIDL:
935 progress_dialog_set_label
936 (dialog, _("Getting the number of new messages (UIDL)..."));
938 case POP3_GETSIZE_LIST:
939 progress_dialog_set_label
940 (dialog, _("Getting the size of messages (LIST)..."));
947 progress_dialog_set_label(dialog, _("Quitting"));
954 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
955 IncSession *inc_session)
957 gchar buf[MSGBUFSIZE];
958 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
959 gchar *total_size_str;
963 if (!pop3_session->new_msg_exist) return;
965 cur_total = inc_session->cur_total_bytes;
966 total = pop3_session->total_bytes;
967 if (pop3_session->state == POP3_RETR ||
968 pop3_session->state == POP3_RETR_RECV ||
969 pop3_session->state == POP3_DELETE) {
970 Xstrdup_a(total_size_str, to_human_readable(total), return);
971 g_snprintf(buf, sizeof(buf),
972 _("Retrieving message (%d / %d) (%s / %s)"),
973 pop3_session->cur_msg, pop3_session->count,
974 to_human_readable(cur_total), total_size_str);
975 progress_dialog_set_label(inc_dialog->dialog, buf);
978 progress_dialog_set_fraction
979 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
981 g_snprintf(buf, sizeof(buf), "%d / %d",
982 pop3_session->cur_msg, pop3_session->count);
983 gtk_progress_bar_set_text
984 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), buf);
985 gtk_progress_bar_set_fraction
986 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
987 (total == 0) ? 0 : (gfloat)cur_total / (gfloat)total);
989 if (pop3_session->cur_total_num > 0) {
990 g_snprintf(buf, sizeof(buf),
991 ngettext("Retrieving (%d message (%s) received)",
992 "Retrieving (%d messages (%s) received)",
993 pop3_session->cur_total_num),
994 pop3_session->cur_total_num,
996 (pop3_session->cur_total_recv_bytes));
997 progress_dialog_list_set_status(inc_dialog->dialog,
1003 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
1004 IncSession *inc_session)
1010 g_get_current_time(&tv_cur);
1012 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
1013 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
1014 if (tv_result.tv_usec < 0) {
1016 tv_result.tv_usec += G_USEC_PER_SEC;
1019 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
1020 if (msec > PROGRESS_UPDATE_INTERVAL) {
1021 inc_progress_dialog_update(inc_dialog, inc_session);
1022 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
1023 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
1027 static gint inc_recv_data_progressive(Session *session, guint cur_len,
1028 guint total_len, gpointer data)
1030 IncSession *inc_session = (IncSession *)data;
1031 Pop3Session *pop3_session = POP3_SESSION(session);
1032 IncProgressDialog *inc_dialog;
1035 g_return_val_if_fail(inc_session != NULL, -1);
1037 if (pop3_session->state != POP3_RETR &&
1038 pop3_session->state != POP3_RETR_RECV &&
1039 pop3_session->state != POP3_DELETE &&
1040 pop3_session->state != POP3_LOGOUT) return 0;
1042 if (!pop3_session->new_msg_exist) return 0;
1044 cur_total = pop3_session->cur_total_bytes + cur_len;
1045 if (cur_total > pop3_session->total_bytes)
1046 cur_total = pop3_session->total_bytes;
1047 inc_session->cur_total_bytes = cur_total;
1049 inc_dialog = (IncProgressDialog *)inc_session->data;
1050 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1055 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1057 IncSession *inc_session = (IncSession *)data;
1058 IncProgressDialog *inc_dialog;
1060 g_return_val_if_fail(inc_session != NULL, -1);
1062 inc_dialog = (IncProgressDialog *)inc_session->data;
1064 inc_recv_data_progressive(session, 0, 0, inc_session);
1066 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1067 inc_progress_dialog_update(inc_dialog, inc_session);
1073 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1075 IncSession *inc_session = (IncSession *)data;
1076 IncProgressDialog *inc_dialog;
1078 g_return_val_if_fail(inc_session != NULL, -1);
1080 inc_dialog = (IncProgressDialog *)inc_session->data;
1082 switch (POP3_SESSION(session)->state) {
1083 case POP3_GETAUTH_USER:
1084 case POP3_GETAUTH_PASS:
1085 case POP3_GETAUTH_APOP:
1086 case POP3_GETRANGE_STAT:
1087 case POP3_GETRANGE_LAST:
1088 case POP3_GETRANGE_UIDL:
1089 case POP3_GETSIZE_LIST:
1090 inc_progress_dialog_update(inc_dialog, inc_session);
1093 inc_recv_data_progressive(session, 0, 0, inc_session);
1096 inc_progress_dialog_update(inc_dialog, inc_session);
1105 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1108 FolderItem *dropfolder;
1109 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1112 g_return_val_if_fail(inc_session != NULL, -1);
1114 if (session->ac_prefs->inbox) {
1115 inbox = folder_find_item_from_identifier
1116 (session->ac_prefs->inbox);
1118 inbox = folder_get_default_inbox();
1120 inbox = folder_get_default_inbox();
1126 /* CLAWS: claws uses a global .processing folder for the filtering. */
1127 dropfolder = folder_get_default_processing();
1129 /* add msg file to drop folder */
1130 if ((msgnum = folder_item_add_msg(
1131 dropfolder, file, NULL, TRUE)) < 0) {
1139 static void inc_put_error(IncState istate, Pop3Session *session)
1141 gchar *log_msg = NULL;
1142 gchar *err_msg = NULL;
1143 gboolean fatal_error = FALSE;
1146 case INC_CONNECT_ERROR:
1147 log_msg = _("Connection failed.");
1149 if (prefs_common.no_recv_err_panel)
1151 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1152 SESSION(session)->server,
1153 SESSION(session)->port);
1156 log_msg = _("Error occurred while processing mail.");
1158 if (prefs_common.no_recv_err_panel)
1160 if (session->error_msg)
1161 err_msg = g_strdup_printf
1162 (_("Error occurred while processing mail:\n%s"),
1163 session->error_msg);
1165 err_msg = g_strdup(log_msg);
1168 log_msg = _("No disk space left.");
1169 err_msg = g_strdup(log_msg);
1173 log_msg = _("Can't write file.");
1174 err_msg = g_strdup(log_msg);
1177 case INC_SOCKET_ERROR:
1178 log_msg = _("Socket error.");
1179 if (prefs_common.no_recv_err_panel)
1181 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1182 SESSION(session)->server,
1183 SESSION(session)->port);
1186 log_msg = _("Connection closed by the remote host.");
1187 if (prefs_common.no_recv_err_panel)
1189 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1190 SESSION(session)->server,
1191 SESSION(session)->port);
1194 log_msg = _("Mailbox is locked.");
1195 if (prefs_common.no_recv_err_panel)
1197 if (session->error_msg)
1198 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1199 session->error_msg);
1201 err_msg = g_strdup(log_msg);
1203 case INC_AUTH_FAILED:
1204 log_msg = _("Authentication failed.");
1206 if (prefs_common.no_recv_err_panel)
1208 if (session->error_msg)
1209 err_msg = g_strdup_printf
1210 (_("Authentication failed:\n%s"), session->error_msg);
1212 err_msg = g_strdup(log_msg);
1215 log_msg = _("Session timed out.");
1216 if (prefs_common.no_recv_err_panel)
1218 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1219 SESSION(session)->server,
1220 SESSION(session)->port);
1228 log_error(LOG_PROTOCOL, "%s\n", log_msg);
1230 log_warning(LOG_PROTOCOL, "%s\n", log_msg);
1232 if (prefs_common.no_recv_err_panel && fatal_error)
1233 mainwindow_show_error();
1236 alertpanel_error_log("%s", err_msg);
1241 static void inc_cancel(IncProgressDialog *dialog)
1243 IncSession *session;
1245 g_return_if_fail(dialog != NULL);
1247 if (dialog->queue_list == NULL) {
1248 inc_progress_dialog_destroy(dialog);
1252 session = dialog->queue_list->data;
1254 session->inc_state = INC_CANCEL;
1256 log_message(LOG_PROTOCOL, _("Incorporation cancelled\n"));
1259 gboolean inc_is_active(void)
1261 return (inc_dialog_list != NULL);
1264 void inc_cancel_all(void)
1268 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1269 inc_cancel((IncProgressDialog *)cur->data);
1272 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1274 inc_cancel((IncProgressDialog *)data);
1277 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1280 IncProgressDialog *dialog = (IncProgressDialog *)data;
1282 if (dialog->queue_list == NULL)
1283 inc_progress_dialog_destroy(dialog);
1288 static gint inc_spool_account(PrefsAccount *account)
1294 if (account->local_inbox) {
1295 inbox = folder_find_item_from_identifier(account->local_inbox);
1297 inbox = folder_get_default_inbox();
1299 inbox = folder_get_default_inbox();
1301 if (account->local_mbox) {
1302 if (is_file_exist(account->local_mbox))
1303 mbox = g_strdup(account->local_mbox);
1304 else if (is_dir_exist(account->local_mbox))
1305 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1306 g_get_user_name(), NULL);
1308 debug_print("%s: local mailbox not found.\n",
1309 account->local_mbox);
1313 debug_print("local mailbox not set in account info.\n");
1317 result = get_spool(inbox, mbox, account);
1320 statusbar_pop_all();
1325 static gint inc_all_spool(void)
1329 gint account_new_msgs = 0;
1331 list = account_get_list();
1332 if (!list) return 0;
1334 for (; list != NULL; list = list->next) {
1335 PrefsAccount *account = list->data;
1337 if ((account->protocol == A_LOCAL) &&
1338 (account->recv_at_getall)) {
1339 account_new_msgs = inc_spool_account(account);
1340 if (account_new_msgs > 0)
1341 new_msgs += account_new_msgs;
1348 static gint get_spool(FolderItem *dest, const gchar *mbox, PrefsAccount *account)
1352 gchar tmp_mbox[MAXPATHLEN + 1];
1354 g_return_val_if_fail(dest != NULL, -1);
1355 g_return_val_if_fail(mbox != NULL, -1);
1356 g_return_val_if_fail(account != NULL, -1);
1358 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1359 debug_print("%s: no messages in local mailbox.\n", mbox);
1361 } else if (size < 0)
1364 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1367 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%p",
1368 get_tmp_dir(), G_DIR_SEPARATOR, mbox);
1370 if (copy_mbox(lockfd, tmp_mbox) < 0) {
1371 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1375 debug_print("Getting new messages from %s into %s...\n",
1378 msgs = proc_mbox(dest, tmp_mbox, account->filter_on_recv, account);
1381 if (msgs >= 0) empty_mbox(mbox);
1382 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1392 void inc_unlock(void)
1394 if (inc_lock_count > 0)
1398 static guint autocheck_timer = 0;
1399 static gpointer autocheck_data = NULL;
1402 osso_context_t *get_osso_context(void);
1405 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1409 gchar *buf, *numpos, *ret_str;
1410 gssize by_read = 0, by_written = 0;
1412 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1413 *prefs_common.newmail_notify_cmd))
1416 buf = g_strdup(prefs_common.newmail_notify_cmd);
1417 if ((numpos = strstr(buf, "%d")) != NULL) {
1421 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1426 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1428 if (ret_str && by_written) {
1432 debug_print("executing new mail notification command: %s\n", buf);
1433 execute_command_line(buf, TRUE);
1439 if (prefs_common.maemo_play_sound)
1440 hildon_play_system_sound("/usr/share/sounds/ui-new_email.wav");
1441 if (prefs_common.maemo_show_banner) {
1442 gchar *info = g_strdup_printf(ngettext("Claws Mail: %d new message",
1443 "Claws Mail: %d new messages",
1444 new_msgs), new_msgs);
1445 osso_system_note_infoprint(get_osso_context(), info, NULL);
1452 #if (defined(MAEMO) && defined(CONIC))
1453 static void maemo_connection_event(ConIcConnection *connection,
1454 ConIcConnectionEvent *event,
1457 ConIcConnectionStatus status =
1458 con_ic_connection_event_get_status(event);
1459 MainWindow *mainwin = (MainWindow *)user_data;
1461 case CON_IC_STATUS_CONNECTED:
1462 debug_print("we're connected\n");
1463 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1466 debug_print("we're disconnected\n");
1467 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1468 maemo_warned_offline = FALSE;
1475 void inc_autocheck_timer_init(MainWindow *mainwin)
1477 #if (defined(MAEMO) && defined(CONIC))
1478 GValue *val = g_new0(GValue, 1);
1479 maemo_connection = con_ic_connection_new();
1481 g_value_init(val, G_TYPE_BOOLEAN);
1482 g_value_set_boolean(val, TRUE);
1483 g_object_set_property(G_OBJECT(maemo_connection),
1484 "automatic-connection-events", val);
1486 g_signal_connect (maemo_connection, "connection-event",
1487 G_CALLBACK(maemo_connection_event), mainwin);
1488 con_ic_connection_connect (maemo_connection,
1489 CON_IC_CONNECT_FLAG_AUTOMATICALLY_TRIGGERED);
1490 autocheck_data = mainwin;
1492 autocheck_data = mainwin;
1493 inc_autocheck_timer_set();
1497 static void inc_autocheck_timer_set_interval(guint interval)
1499 inc_autocheck_timer_remove();
1500 /* last test is to avoid re-enabling auto_check after modifying
1501 the common preferences */
1502 if (prefs_common.autochk_newmail && autocheck_data
1503 && prefs_common.work_offline == FALSE) {
1504 autocheck_timer = g_timeout_add
1505 (interval, inc_autocheck_func, autocheck_data);
1506 debug_print("added timer = %d\n", autocheck_timer);
1510 void inc_autocheck_timer_set(void)
1512 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1513 #if (defined(MAEMO) && defined(CONIC))
1514 con_ic_connection_connect (maemo_connection,
1515 CON_IC_CONNECT_FLAG_AUTOMATICALLY_TRIGGERED);
1519 void inc_autocheck_timer_remove(void)
1521 if (autocheck_timer) {
1522 debug_print("removed timer = %d\n", autocheck_timer);
1523 g_source_remove(autocheck_timer);
1524 autocheck_timer = 0;
1528 static gint inc_autocheck_func(gpointer data)
1530 MainWindow *mainwin = (MainWindow *)data;
1532 if (inc_lock_count) {
1533 debug_print("autocheck is locked.\n");
1534 inc_autocheck_timer_set_interval(1000);
1538 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1543 gboolean inc_offline_should_override(gboolean force_ask, const gchar *msg)
1545 static time_t overridden_yes = 0;
1546 static time_t overridden_no = 0;
1547 int length = 10; /* minutes */
1548 gint answer = G_ALERTDEFAULT;
1550 #if (defined(MAEMO) && defined(CONIC))
1551 if (prefs_common.work_offline) {
1552 if (force_ask && !maemo_warned_offline) {
1553 if (mainwindow_get_mainwindow())
1554 hildon_banner_show_information(
1555 mainwindow_get_mainwindow()->window,
1557 _("Unable to connect: you are offline."));
1558 maemo_warned_offline = TRUE;
1566 if (prefs_common.autochk_newmail)
1567 length = prefs_common.autochk_itv; /* minutes */
1570 overridden_no = (time_t)0;
1573 if (prefs_common.work_offline) {
1576 if (time(NULL) - overridden_yes < length * 60) /* seconds */
1578 else if (time(NULL) - overridden_no < length * 60) /* seconds */
1582 tmp = g_strdup_printf(
1583 _("%s%sYou're working offline. Override for %d minutes?"),
1588 tmp = g_strdup_printf(
1589 _("%s%sYou're working offline. Override?"),
1593 answer = alertpanel(_("Offline warning"),
1595 GTK_STOCK_NO, "+" GTK_STOCK_YES,
1596 !force_ask? _("On_ly once"):NULL);
1598 if (answer == G_ALERTALTERNATE) {
1599 overridden_yes = time(NULL);
1601 } else if (answer == G_ALERTDEFAULT) {
1603 overridden_no = time(NULL);
1606 overridden_yes = (time_t)0;
1607 overridden_no = (time_t)0;