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>
35 #include "mainwindow.h"
36 #include "folderview.h"
37 #include "summaryview.h"
38 #include "prefs_common.h"
39 #include "prefs_account.h"
49 #include "statusbar.h"
51 #include "manage_window.h"
52 #include "stock_pixmap.h"
53 #include "progressdialog.h"
54 #include "inputdialog.h"
55 #include "alertpanel.h"
57 #include "filtering.h"
63 #include <hildon/hildon-banner.h>
64 #include <hildon/hildon-sound.h>
66 #include <hildon-widgets/hildon-banner.h>
67 #include <hildon-widgets/hildon-system-sound.h>
72 #include <conicconnection.h>
73 #include <conicconnectionevent.h>
75 static ConIcConnection *maemo_connection = NULL;
76 static gboolean maemo_warned_offline = FALSE;
80 static GList *inc_dialog_list = NULL;
82 guint inc_lock_count = 0;
84 static GdkPixbuf *currentpix;
85 static GdkPixbuf *errorpix;
86 static GdkPixbuf *okpix;
88 #define MSGBUFSIZE 8192
90 static void inc_finished (MainWindow *mainwin,
91 gboolean new_messages,
93 static gint inc_account_mail_real (MainWindow *mainwin,
94 PrefsAccount *account);
96 static IncProgressDialog *inc_progress_dialog_create
98 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
99 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
101 static IncSession *inc_session_new (PrefsAccount *account);
102 static void inc_session_destroy (IncSession *session);
103 static gint inc_start (IncProgressDialog *inc_dialog);
104 static IncState inc_pop3_session_do (IncSession *session);
106 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
107 IncSession *inc_session);
109 static void inc_progress_dialog_set_label
110 (IncProgressDialog *inc_dialog,
111 IncSession *inc_session);
112 static void inc_progress_dialog_set_progress
113 (IncProgressDialog *inc_dialog,
114 IncSession *inc_session);
116 static void inc_progress_dialog_update_periodic
117 (IncProgressDialog *inc_dialog,
118 IncSession *inc_session);
120 static gint inc_recv_data_progressive (Session *session,
124 static gint inc_recv_data_finished (Session *session,
127 static gint inc_recv_message (Session *session,
130 static gint inc_drop_message (Pop3Session *session,
133 static void inc_put_error (IncState istate,
134 Pop3Session *session);
136 static void inc_cancel_cb (GtkWidget *widget,
138 static gint inc_dialog_delete_cb (GtkWidget *widget,
142 static gint get_spool (FolderItem *dest,
144 PrefsAccount *account);
146 static gint inc_spool_account(PrefsAccount *account);
147 static gint inc_all_spool(void);
148 static void inc_autocheck_timer_set_interval (guint interval);
149 static gint inc_autocheck_func (gpointer data);
151 static void inc_notify_cmd (gint new_msgs,
156 * @mainwin: Main window.
157 * @new_messages: TRUE if some messages have been received.
159 * Update the folder view and the summary view after receiving
160 * messages. If @new_messages is FALSE, this function avoids unneeded
163 static void inc_finished(MainWindow *mainwin, gboolean new_messages, gboolean autocheck)
165 if (prefs_common.scan_all_after_inc)
166 folderview_check_new(NULL);
168 if (!autocheck && new_messages && prefs_common.open_inbox_on_inc) {
169 FolderItem *item = NULL;
171 if (cur_account && cur_account->inbox)
172 item = folder_find_item_from_identifier(cur_account->inbox);
173 if (item == NULL && cur_account->folder)
174 item = cur_account->folder->inbox;
176 item = folder_get_default_inbox();
178 folderview_unselect(mainwin->folderview);
179 folderview_select(mainwin->folderview, item);
183 void inc_mail(MainWindow *mainwin, gboolean notify)
186 gint account_new_msgs = 0;
188 if (inc_lock_count) return;
190 if (prefs_common.work_offline &&
191 !inc_offline_should_override(TRUE,
192 _("Claws Mail needs network access in order "
197 inc_autocheck_timer_remove();
198 main_window_lock(mainwin);
200 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
201 /* external incorporating program */
202 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
203 main_window_unlock(mainwin);
204 inc_autocheck_timer_set();
209 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
210 if (account_new_msgs > 0)
211 new_msgs += account_new_msgs;
214 inc_finished(mainwin, new_msgs > 0, FALSE);
215 main_window_unlock(mainwin);
216 inc_notify_cmd(new_msgs, notify);
217 inc_autocheck_timer_set();
221 void inc_pop_before_smtp(PrefsAccount *acc)
223 IncProgressDialog *inc_dialog;
227 mainwin = mainwindow_get_mainwindow();
229 session = inc_session_new(acc);
230 if (!session) return;
231 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
233 inc_dialog = inc_progress_dialog_create(FALSE);
234 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
236 /* FIXME: assumes to attach to first main window */
237 inc_dialog->mainwin = mainwin;
238 inc_progress_dialog_set_list(inc_dialog);
241 toolbar_main_set_sensitive(mainwin);
242 main_window_set_menu_sensitive(mainwin);
245 inc_start(inc_dialog);
248 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
250 IncProgressDialog *inc_dialog;
253 switch (account->protocol) {
257 * FIXME: it should return foldeview_check_new() value.
258 * TODO: do it when bug [19] is fixed (IMAP folder sets
259 * an incorrect new message count)
261 folderview_check_new(FOLDER(account->folder));
265 session = inc_session_new(account);
266 if (!session) return 0;
268 inc_dialog = inc_progress_dialog_create(FALSE);
269 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
271 inc_dialog->mainwin = mainwin;
272 inc_progress_dialog_set_list(inc_dialog);
275 toolbar_main_set_sensitive(mainwin);
276 main_window_set_menu_sensitive(mainwin);
279 return inc_start(inc_dialog);
282 return inc_spool_account(account);
290 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
294 if (inc_lock_count) return 0;
296 if (prefs_common.work_offline &&
297 !inc_offline_should_override(TRUE,
298 _("Claws Mail needs network access in order "
302 inc_autocheck_timer_remove();
303 main_window_lock(mainwin);
305 new_msgs = inc_account_mail_real(mainwin, account);
307 inc_finished(mainwin, new_msgs > 0, FALSE);
308 main_window_unlock(mainwin);
309 inc_autocheck_timer_set();
314 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
317 GList *list, *queue_list = NULL;
318 IncProgressDialog *inc_dialog;
320 gint account_new_msgs = 0;
322 if (prefs_common.work_offline &&
323 !inc_offline_should_override( (autocheck == FALSE),
324 _("Claws Mail needs network access in order "
328 if (inc_lock_count) return;
330 inc_autocheck_timer_remove();
331 main_window_lock(mainwin);
333 list = account_get_list();
335 inc_finished(mainwin, new_msgs > 0, autocheck);
336 main_window_unlock(mainwin);
337 inc_notify_cmd(new_msgs, notify);
338 inc_autocheck_timer_set();
342 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
343 /* external incorporating program */
344 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
345 log_error(LOG_PROTOCOL, _("%s failed\n"), prefs_common.extinc_cmd);
347 main_window_unlock(mainwin);
348 inc_autocheck_timer_set();
353 /* check local folders */
354 account_new_msgs = inc_all_spool();
355 if (account_new_msgs > 0)
356 new_msgs += account_new_msgs;
358 /* check IMAP4 / News folders */
359 for (list = account_get_list(); list != NULL; list = list->next) {
360 PrefsAccount *account = list->data;
361 if ((account->protocol == A_IMAP4 ||
362 account->protocol == A_NNTP) && account->recv_at_getall) {
363 new_msgs += folderview_check_new(FOLDER(account->folder));
367 /* check POP3 accounts */
368 for (list = account_get_list(); list != NULL; list = list->next) {
370 PrefsAccount *account = list->data;
372 if (account->recv_at_getall) {
373 session = inc_session_new(account);
375 queue_list = g_list_append(queue_list, session);
380 inc_dialog = inc_progress_dialog_create(autocheck);
381 inc_dialog->queue_list = queue_list;
382 inc_dialog->mainwin = mainwin;
383 inc_progress_dialog_set_list(inc_dialog);
385 toolbar_main_set_sensitive(mainwin);
386 main_window_set_menu_sensitive(mainwin);
387 new_msgs += inc_start(inc_dialog);
390 inc_finished(mainwin, new_msgs > 0, autocheck);
391 main_window_unlock(mainwin);
392 inc_notify_cmd(new_msgs, notify);
393 inc_autocheck_timer_set();
396 static void inc_progress_dialog_size_allocate_cb(GtkWidget *widget,
397 GtkAllocation *allocation)
399 g_return_if_fail(allocation != NULL);
401 prefs_common.receivewin_width = allocation->width;
402 prefs_common.receivewin_height = allocation->height;
405 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
407 IncProgressDialog *dialog;
408 ProgressDialog *progress;
409 static GdkGeometry geometry;
411 dialog = g_new0(IncProgressDialog, 1);
413 progress = progress_dialog_create();
414 gtk_window_set_title(GTK_WINDOW(progress->window),
415 _("Retrieving new messages"));
416 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
417 G_CALLBACK(inc_cancel_cb), dialog);
418 g_signal_connect(G_OBJECT(progress->window), "delete_event",
419 G_CALLBACK(inc_dialog_delete_cb), dialog);
420 g_signal_connect(G_OBJECT(progress->window), "size_allocate",
421 G_CALLBACK(inc_progress_dialog_size_allocate_cb), NULL);
422 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
424 progress_dialog_get_fraction(progress);
426 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_COMPLETE,
428 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_CONTINUE,
430 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_ERROR,
433 if (!geometry.min_height) {
434 geometry.min_width = 460;
435 geometry.min_height = 250;
438 gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
440 gtk_widget_set_size_request(progress->window, prefs_common.receivewin_width,
441 prefs_common.receivewin_height);
443 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
444 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
446 dialog->show_dialog = TRUE;
447 gtk_widget_show_now(progress->window);
450 dialog->dialog = progress;
451 g_get_current_time(&dialog->progress_tv);
452 g_get_current_time(&dialog->folder_tv);
453 dialog->queue_list = NULL;
456 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
461 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
465 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
466 IncSession *session = list->data;
467 Pop3Session *pop3_session = POP3_SESSION(session->session);
469 session->data = inc_dialog;
471 progress_dialog_list_set(inc_dialog->dialog,
473 pop3_session->ac_prefs->account_name,
478 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
480 progress_dialog_get_fraction(inc_dialog->dialog);
481 progress_dialog_set_label(inc_dialog->dialog, "");
482 if (inc_dialog->mainwin)
483 main_window_progress_off(inc_dialog->mainwin);
486 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
488 g_return_if_fail(inc_dialog != NULL);
490 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
492 if (inc_dialog->mainwin)
493 main_window_progress_off(inc_dialog->mainwin);
494 progress_dialog_destroy(inc_dialog->dialog);
499 static IncSession *inc_session_new(PrefsAccount *account)
503 g_return_val_if_fail(account != NULL, NULL);
505 if (account->protocol != A_POP3)
507 if (!account->recv_server || !account->userid)
510 session = g_new0(IncSession, 1);
512 session->session = pop3_session_new(account);
513 session->session->data = session;
514 POP3_SESSION(session->session)->drop_message = inc_drop_message;
515 session_set_recv_message_notify(session->session,
516 inc_recv_message, session);
517 session_set_recv_data_progressive_notify(session->session,
518 inc_recv_data_progressive,
520 session_set_recv_data_notify(session->session,
521 inc_recv_data_finished, session);
526 static void inc_session_destroy(IncSession *session)
528 g_return_if_fail(session != NULL);
530 session_destroy(session->session);
534 static gint inc_start(IncProgressDialog *inc_dialog)
538 Pop3Session *pop3_session;
544 FolderItem *processing, *inbox;
545 GSList *msglist, *msglist_element;
546 gboolean cancelled = FALSE;
548 qlist = inc_dialog->queue_list;
549 while (qlist != NULL) {
550 GList *next = qlist->next;
552 session = qlist->data;
553 pop3_session = POP3_SESSION(session->session);
554 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
555 if (pop3_session->ac_prefs->passwd)
557 g_strdup(pop3_session->ac_prefs->passwd);
558 else if (pop3_session->ac_prefs->tmp_pass)
560 g_strdup(pop3_session->ac_prefs->tmp_pass);
564 if (inc_dialog->show_dialog)
565 manage_window_focus_in
566 (inc_dialog->dialog->window,
569 pass = input_dialog_query_password
570 (pop3_session->ac_prefs->recv_server,
573 if (inc_dialog->show_dialog)
574 manage_window_focus_out
575 (inc_dialog->dialog->window,
579 pop3_session->ac_prefs->tmp_pass =
581 pop3_session->pass = pass;
588 #define SET_PIXMAP_AND_TEXT(pix, str) \
590 progress_dialog_list_set(inc_dialog->dialog, \
591 inc_dialog->cur_row, \
597 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
598 session = inc_dialog->queue_list->data;
599 pop3_session = POP3_SESSION(session->session);
600 GSList *filtered, *unfiltered;
602 if (pop3_session->pass == NULL) {
603 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
604 inc_session_destroy(session);
605 inc_dialog->queue_list =
606 g_list_remove(inc_dialog->queue_list, session);
610 inc_progress_dialog_clear(inc_dialog);
611 progress_dialog_scroll_to_row(inc_dialog->dialog,
612 inc_dialog->cur_row);
614 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
616 /* begin POP3 session */
617 inc_state = inc_pop3_session_do(session);
621 if (pop3_session->cur_total_num > 0)
622 msg = g_strdup_printf(
623 ngettext("Done (%d message (%s) received)",
624 "Done (%d messages (%s) received)",
625 pop3_session->cur_total_num),
626 pop3_session->cur_total_num,
627 to_human_readable((goffset)pop3_session->cur_total_recv_bytes));
629 msg = g_strdup_printf(_("Done (no new messages)"));
630 SET_PIXMAP_AND_TEXT(okpix, msg);
633 case INC_CONNECT_ERROR:
634 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
636 case INC_AUTH_FAILED:
637 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
640 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
645 case INC_SOCKET_ERROR:
647 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
650 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
653 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
654 if (!inc_dialog->show_dialog)
661 if (pop3_session->error_val == PS_AUTHFAIL) {
662 if(!prefs_common.no_recv_err_panel) {
663 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
664 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
665 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
669 /* CLAWS: perform filtering actions on dropped message */
670 /* CLAWS: get default inbox (perhaps per account) */
671 if (pop3_session->ac_prefs->inbox) {
672 /* CLAWS: get destination folder / mailbox */
673 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
675 inbox = folder_get_default_inbox();
677 inbox = folder_get_default_inbox();
679 /* get list of messages in processing */
680 processing = folder_get_default_processing();
681 folder_item_scan(processing);
682 msglist = folder_item_get_msg_list(processing);
684 /* process messages */
685 folder_item_update_freeze();
687 procmsg_msglist_filter(msglist, pop3_session->ac_prefs,
688 &filtered, &unfiltered,
689 pop3_session->ac_prefs->filter_on_recv);
691 filtering_move_and_copy_msgs(msglist);
692 if (unfiltered != NULL)
693 folder_item_move_msgs(inbox, unfiltered);
695 for(msglist_element = msglist; msglist_element != NULL;
696 msglist_element = msglist_element->next) {
697 MsgInfo *msginfo = (MsgInfo *)msglist_element->data;
698 procmsg_msginfo_free(msginfo);
700 folder_item_update_thaw();
702 g_slist_free(msglist);
703 g_slist_free(filtered);
704 g_slist_free(unfiltered);
708 new_msgs += pop3_session->cur_total_num;
710 if (pop3_session->error_val == PS_AUTHFAIL &&
711 pop3_session->ac_prefs->tmp_pass) {
712 g_free(pop3_session->ac_prefs->tmp_pass);
713 pop3_session->ac_prefs->tmp_pass = NULL;
716 pop3_write_uidl_list(pop3_session);
718 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
720 if (inc_dialog->show_dialog)
721 manage_window_focus_in
722 (inc_dialog->dialog->window,
724 inc_put_error(inc_state, pop3_session);
725 if (inc_dialog->show_dialog)
726 manage_window_focus_out
727 (inc_dialog->dialog->window,
729 if (inc_state == INC_NO_SPACE ||
730 inc_state == INC_IO_ERROR)
733 folder_item_free_cache(processing, TRUE);
735 inc_session_destroy(session);
736 inc_dialog->queue_list =
737 g_list_remove(inc_dialog->queue_list, session);
740 #undef SET_PIXMAP_AND_TEXT
743 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
744 "Finished (%d new messages)",
745 new_msgs), new_msgs);
747 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
749 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
751 while (inc_dialog->queue_list != NULL) {
752 session = inc_dialog->queue_list->data;
753 inc_session_destroy(session);
754 inc_dialog->queue_list =
755 g_list_remove(inc_dialog->queue_list, session);
758 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
759 inc_progress_dialog_destroy(inc_dialog);
761 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
763 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
772 static IncState inc_pop3_session_do(IncSession *session)
774 Pop3Session *pop3_session = POP3_SESSION(session->session);
775 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
780 debug_print("getting new messages of account %s...\n",
781 pop3_session->ac_prefs->account_name);
783 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
785 buf = g_strdup_printf(_("%s: Retrieving new messages"),
786 pop3_session->ac_prefs->recv_server);
787 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
790 server = pop3_session->ac_prefs->recv_server;
791 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
792 port = pop3_session->ac_prefs->set_popport ?
793 pop3_session->ac_prefs->popport :
794 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
795 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
796 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
797 SESSION(pop3_session)->nonblocking =
798 pop3_session->ac_prefs->use_nonblocking_ssl;
800 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE) {
801 if (alertpanel_full(_("Insecure connection"),
802 _("This connection is configured to be secured "
803 "using SSL, but SSL is not available in this "
804 "build of Claws Mail. \n\n"
805 "Do you want to continue connecting to this "
806 "server? The communication would not be "
808 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
809 NULL, FALSE, NULL, ALERT_WARNING,
810 G_ALERTDEFAULT) != G_ALERTALTERNATE)
813 port = pop3_session->ac_prefs->set_popport ?
814 pop3_session->ac_prefs->popport : 110;
817 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
818 statuswindow_print_all("%s", buf);
819 log_message(LOG_PROTOCOL, "%s\n", buf);
821 progress_dialog_set_label(inc_dialog->dialog, buf);
825 session_set_timeout(SESSION(pop3_session),
826 prefs_common.io_timeout_secs * 1000);
828 if (session_connect(SESSION(pop3_session), server, port) < 0) {
829 if(!prefs_common.no_recv_err_panel) {
830 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
831 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
832 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
834 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
836 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
838 log_error(LOG_PROTOCOL, _("Can't connect to POP3 server: %s:%d\n"),
841 session->inc_state = INC_CONNECT_ERROR;
842 statuswindow_pop_all();
843 return INC_CONNECT_ERROR;
846 while (session_is_connected(SESSION(pop3_session)) &&
847 session->inc_state != INC_CANCEL)
848 gtk_main_iteration();
850 if (session->inc_state == INC_SUCCESS) {
851 switch (pop3_session->error_val) {
853 switch (SESSION(pop3_session)->state) {
855 if (pop3_session->state == POP3_READY)
856 session->inc_state = INC_CONNECT_ERROR;
858 session->inc_state = INC_ERROR;
861 session->inc_state = INC_EOF;
863 case SESSION_TIMEOUT:
864 session->inc_state = INC_TIMEOUT;
867 session->inc_state = INC_SUCCESS;
872 session->inc_state = INC_AUTH_FAILED;
875 session->inc_state = INC_IO_ERROR;
878 session->inc_state = INC_SOCKET_ERROR;
881 session->inc_state = INC_LOCKED;
884 session->inc_state = INC_ERROR;
889 session_disconnect(SESSION(pop3_session));
892 return session->inc_state;
895 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
896 IncSession *inc_session)
898 inc_progress_dialog_set_label(inc_dialog, inc_session);
899 inc_progress_dialog_set_progress(inc_dialog, inc_session);
902 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
903 IncSession *inc_session)
905 ProgressDialog *dialog = inc_dialog->dialog;
906 Pop3Session *session;
908 g_return_if_fail(inc_session != NULL);
910 session = POP3_SESSION(inc_session->session);
912 switch (session->state) {
915 case POP3_GETAUTH_USER:
916 case POP3_GETAUTH_PASS:
917 case POP3_GETAUTH_APOP:
918 progress_dialog_set_label(dialog, _("Authenticating..."));
919 statuswindow_pop_all();
920 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
921 SESSION(session)->server,
922 session->ac_prefs->account_name);
924 case POP3_GETRANGE_STAT:
925 progress_dialog_set_label
926 (dialog, _("Getting the number of new messages (STAT)..."));
928 case POP3_GETRANGE_LAST:
929 progress_dialog_set_label
930 (dialog, _("Getting the number of new messages (LAST)..."));
932 case POP3_GETRANGE_UIDL:
933 progress_dialog_set_label
934 (dialog, _("Getting the number of new messages (UIDL)..."));
936 case POP3_GETSIZE_LIST:
937 progress_dialog_set_label
938 (dialog, _("Getting the size of messages (LIST)..."));
945 progress_dialog_set_label(dialog, _("Quitting"));
952 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
953 IncSession *inc_session)
955 gchar buf[MSGBUFSIZE];
956 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
957 gchar *total_size_str;
961 if (!pop3_session->new_msg_exist) return;
963 cur_total = inc_session->cur_total_bytes;
964 total = pop3_session->total_bytes;
965 if (pop3_session->state == POP3_RETR ||
966 pop3_session->state == POP3_RETR_RECV ||
967 pop3_session->state == POP3_DELETE) {
968 Xstrdup_a(total_size_str, to_human_readable((goffset)total), return);
969 g_snprintf(buf, sizeof(buf),
970 _("Retrieving message (%d / %d) (%s / %s)"),
971 pop3_session->cur_msg, pop3_session->count,
972 to_human_readable((goffset)cur_total), total_size_str);
973 progress_dialog_set_label(inc_dialog->dialog, buf);
976 progress_dialog_set_fraction
977 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
979 g_snprintf(buf, sizeof(buf), "%d / %d",
980 pop3_session->cur_msg, pop3_session->count);
981 gtk_progress_bar_set_text
982 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), buf);
983 gtk_progress_bar_set_fraction
984 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
985 (total == 0) ? 0 : (gfloat)cur_total / (gfloat)total);
987 if (pop3_session->cur_total_num > 0) {
988 g_snprintf(buf, sizeof(buf),
989 ngettext("Retrieving (%d message (%s) received)",
990 "Retrieving (%d messages (%s) received)",
991 pop3_session->cur_total_num),
992 pop3_session->cur_total_num,
994 ((goffset)pop3_session->cur_total_recv_bytes));
995 progress_dialog_list_set_status(inc_dialog->dialog,
1001 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
1002 IncSession *inc_session)
1008 g_get_current_time(&tv_cur);
1010 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
1011 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
1012 if (tv_result.tv_usec < 0) {
1014 tv_result.tv_usec += G_USEC_PER_SEC;
1017 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
1018 if (msec > PROGRESS_UPDATE_INTERVAL) {
1019 inc_progress_dialog_update(inc_dialog, inc_session);
1020 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
1021 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
1025 static gint inc_recv_data_progressive(Session *session, guint cur_len,
1026 guint total_len, gpointer data)
1028 IncSession *inc_session = (IncSession *)data;
1029 Pop3Session *pop3_session = POP3_SESSION(session);
1030 IncProgressDialog *inc_dialog;
1033 g_return_val_if_fail(inc_session != NULL, -1);
1035 if (pop3_session->state != POP3_RETR &&
1036 pop3_session->state != POP3_RETR_RECV &&
1037 pop3_session->state != POP3_DELETE &&
1038 pop3_session->state != POP3_LOGOUT) return 0;
1040 if (!pop3_session->new_msg_exist) return 0;
1042 cur_total = pop3_session->cur_total_bytes + cur_len;
1043 if (cur_total > pop3_session->total_bytes)
1044 cur_total = pop3_session->total_bytes;
1045 inc_session->cur_total_bytes = cur_total;
1047 inc_dialog = (IncProgressDialog *)inc_session->data;
1048 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1053 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1055 IncSession *inc_session = (IncSession *)data;
1056 IncProgressDialog *inc_dialog;
1058 g_return_val_if_fail(inc_session != NULL, -1);
1060 inc_dialog = (IncProgressDialog *)inc_session->data;
1062 inc_recv_data_progressive(session, 0, 0, inc_session);
1064 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1065 inc_progress_dialog_update(inc_dialog, inc_session);
1071 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1073 IncSession *inc_session = (IncSession *)data;
1074 IncProgressDialog *inc_dialog;
1076 g_return_val_if_fail(inc_session != NULL, -1);
1078 inc_dialog = (IncProgressDialog *)inc_session->data;
1080 switch (POP3_SESSION(session)->state) {
1081 case POP3_GETAUTH_USER:
1082 case POP3_GETAUTH_PASS:
1083 case POP3_GETAUTH_APOP:
1084 case POP3_GETRANGE_STAT:
1085 case POP3_GETRANGE_LAST:
1086 case POP3_GETRANGE_UIDL:
1087 case POP3_GETSIZE_LIST:
1088 inc_progress_dialog_update(inc_dialog, inc_session);
1091 inc_recv_data_progressive(session, 0, 0, inc_session);
1094 inc_progress_dialog_update(inc_dialog, inc_session);
1103 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1106 FolderItem *dropfolder;
1107 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1110 g_return_val_if_fail(inc_session != NULL, -1);
1112 if (session->ac_prefs->inbox) {
1113 inbox = folder_find_item_from_identifier
1114 (session->ac_prefs->inbox);
1116 inbox = folder_get_default_inbox();
1118 inbox = folder_get_default_inbox();
1124 /* CLAWS: claws uses a global .processing folder for the filtering. */
1125 dropfolder = folder_get_default_processing();
1127 /* add msg file to drop folder */
1128 if ((msgnum = folder_item_add_msg(
1129 dropfolder, file, NULL, TRUE)) < 0) {
1137 static void inc_put_error(IncState istate, Pop3Session *session)
1139 gchar *log_msg = NULL;
1140 gchar *err_msg = NULL;
1141 gboolean fatal_error = FALSE;
1144 case INC_CONNECT_ERROR:
1145 log_msg = _("Connection failed.");
1147 if (prefs_common.no_recv_err_panel)
1149 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1150 SESSION(session)->server,
1151 SESSION(session)->port);
1154 log_msg = _("Error occurred while processing mail.");
1156 if (prefs_common.no_recv_err_panel)
1158 if (session->error_msg)
1159 err_msg = g_strdup_printf
1160 (_("Error occurred while processing mail:\n%s"),
1161 session->error_msg);
1163 err_msg = g_strdup(log_msg);
1166 log_msg = _("No disk space left.");
1167 err_msg = g_strdup(log_msg);
1171 log_msg = _("Can't write file.");
1172 err_msg = g_strdup(log_msg);
1175 case INC_SOCKET_ERROR:
1176 log_msg = _("Socket error.");
1177 if (prefs_common.no_recv_err_panel)
1179 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1180 SESSION(session)->server,
1181 SESSION(session)->port);
1184 log_msg = _("Connection closed by the remote host.");
1185 if (prefs_common.no_recv_err_panel)
1187 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1188 SESSION(session)->server,
1189 SESSION(session)->port);
1192 log_msg = _("Mailbox is locked.");
1193 if (prefs_common.no_recv_err_panel)
1195 if (session->error_msg)
1196 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1197 session->error_msg);
1199 err_msg = g_strdup(log_msg);
1201 case INC_AUTH_FAILED:
1202 log_msg = _("Authentication failed.");
1204 if (prefs_common.no_recv_err_panel)
1206 if (session->error_msg)
1207 err_msg = g_strdup_printf
1208 (_("Authentication failed:\n%s"), session->error_msg);
1210 err_msg = g_strdup(log_msg);
1213 log_msg = _("Session timed out. You may be able to "
1214 "recover by increasing the timeout value in "
1215 "Preferences/Other.");
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);
1380 claws_unlink(tmp_mbox);
1381 if (msgs >= 0) empty_mbox(mbox);
1382 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1387 void inc_lock_real(void)
1392 void inc_unlock_real(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 #if GLIB_CHECK_VERSION(2,14,0)
1505 if (interval % 1000 == 0)
1507 g_timeout_add_seconds(interval/1000, inc_autocheck_func, autocheck_data);
1510 autocheck_timer = g_timeout_add
1511 (interval, inc_autocheck_func, autocheck_data);
1512 debug_print("added timer = %d\n", autocheck_timer);
1516 void inc_autocheck_timer_set(void)
1518 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1519 #if (defined(MAEMO) && defined(CONIC))
1520 con_ic_connection_connect (maemo_connection,
1521 CON_IC_CONNECT_FLAG_AUTOMATICALLY_TRIGGERED);
1525 void inc_autocheck_timer_remove(void)
1527 if (autocheck_timer) {
1528 debug_print("removed timer = %d\n", autocheck_timer);
1529 g_source_remove(autocheck_timer);
1530 autocheck_timer = 0;
1534 static gint inc_autocheck_func(gpointer data)
1536 MainWindow *mainwin = (MainWindow *)data;
1538 if (inc_lock_count) {
1539 debug_print("autocheck is locked.\n");
1540 inc_autocheck_timer_set_interval(1000);
1544 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1549 gboolean inc_offline_should_override(gboolean force_ask, const gchar *msg)
1551 static time_t overridden_yes = 0;
1552 static time_t overridden_no = 0;
1553 int length = 10; /* minutes */
1554 gint answer = G_ALERTDEFAULT;
1556 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1557 /* If no network connection is available, override is not possible */
1558 if(!networkmanager_is_online(NULL))
1562 #if (defined(MAEMO) && defined(CONIC))
1563 if (prefs_common.work_offline) {
1564 if (force_ask && !maemo_warned_offline) {
1565 if (mainwindow_get_mainwindow())
1566 hildon_banner_show_information(
1567 mainwindow_get_mainwindow()->window,
1569 _("Unable to connect: you are offline."));
1570 maemo_warned_offline = TRUE;
1578 if (prefs_common.autochk_newmail)
1579 length = prefs_common.autochk_itv; /* minutes */
1582 overridden_no = (time_t)0;
1585 if (prefs_common.work_offline) {
1588 if (time(NULL) - overridden_yes < length * 60) /* seconds */
1590 else if (time(NULL) - overridden_no < length * 60) /* seconds */
1594 tmp = g_strdup_printf(
1595 _("%s%sYou're working offline. Override for %d minutes?"),
1600 tmp = g_strdup_printf(
1601 _("%s%sYou're working offline. Override?"),
1605 answer = alertpanel(_("Offline warning"),
1607 GTK_STOCK_NO, "+" GTK_STOCK_YES,
1608 !force_ask? _("On_ly once"):NULL);
1610 if (answer == G_ALERTALTERNATE) {
1611 overridden_yes = time(NULL);
1613 } else if (answer == G_ALERTDEFAULT) {
1615 overridden_no = time(NULL);
1618 overridden_yes = (time_t)0;
1619 overridden_no = (time_t)0;