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"
65 #include <hildon-widgets/hildon-banner.h>
66 #include <conicconnection.h>
67 #include <conicconnectionevent.h>
69 static ConIcConnection *maemo_connection = NULL;
70 static gboolean maemo_warned_offline = FALSE;
73 static GList *inc_dialog_list = NULL;
75 static guint inc_lock_count = 0;
77 static GdkPixbuf *currentpix;
78 static GdkPixbuf *errorpix;
79 static GdkPixbuf *okpix;
81 #define MSGBUFSIZE 8192
83 static void inc_finished (MainWindow *mainwin,
84 gboolean new_messages,
86 static gint inc_account_mail_real (MainWindow *mainwin,
87 PrefsAccount *account);
89 static IncProgressDialog *inc_progress_dialog_create
91 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
92 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
94 static IncSession *inc_session_new (PrefsAccount *account);
95 static void inc_session_destroy (IncSession *session);
96 static gint inc_start (IncProgressDialog *inc_dialog);
97 static IncState inc_pop3_session_do (IncSession *session);
99 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
100 IncSession *inc_session);
102 static void inc_progress_dialog_set_label
103 (IncProgressDialog *inc_dialog,
104 IncSession *inc_session);
105 static void inc_progress_dialog_set_progress
106 (IncProgressDialog *inc_dialog,
107 IncSession *inc_session);
109 static void inc_progress_dialog_update_periodic
110 (IncProgressDialog *inc_dialog,
111 IncSession *inc_session);
113 static gint inc_recv_data_progressive (Session *session,
117 static gint inc_recv_data_finished (Session *session,
120 static gint inc_recv_message (Session *session,
123 static gint inc_drop_message (Pop3Session *session,
126 static void inc_put_error (IncState istate,
127 Pop3Session *session);
129 static void inc_cancel_cb (GtkWidget *widget,
131 static gint inc_dialog_delete_cb (GtkWidget *widget,
135 static gint get_spool (FolderItem *dest,
137 PrefsAccount *account);
139 static gint inc_spool_account(PrefsAccount *account);
140 static gint inc_all_spool(void);
141 static void inc_autocheck_timer_set_interval (guint interval);
142 static gint inc_autocheck_func (gpointer data);
144 static void inc_notify_cmd (gint new_msgs,
149 * @mainwin: Main window.
150 * @new_messages: TRUE if some messages have been received.
152 * Update the folder view and the summary view after receiving
153 * messages. If @new_messages is FALSE, this function avoids unneeded
156 static void inc_finished(MainWindow *mainwin, gboolean new_messages, gboolean autocheck)
158 if (prefs_common.scan_all_after_inc)
159 folderview_check_new(NULL);
161 if (!autocheck && prefs_common.open_inbox_on_inc) {
162 FolderItem *item = NULL;
164 if (cur_account && cur_account->inbox)
165 item = folder_find_item_from_identifier(cur_account->inbox);
166 if (item == NULL && cur_account->folder)
167 item = cur_account->folder->inbox;
169 item = folder_get_default_inbox();
171 folderview_unselect(mainwin->folderview);
172 folderview_select(mainwin->folderview, item);
176 void inc_mail(MainWindow *mainwin, gboolean notify)
179 gint account_new_msgs = 0;
181 if (inc_lock_count) return;
183 if (prefs_common.work_offline &&
184 !inc_offline_should_override(TRUE,
185 _("Claws Mail needs network access in order "
190 inc_autocheck_timer_remove();
191 main_window_lock(mainwin);
193 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
194 /* external incorporating program */
195 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
196 main_window_unlock(mainwin);
197 inc_autocheck_timer_set();
201 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
202 if (account_new_msgs > 0)
203 new_msgs += account_new_msgs;
206 inc_finished(mainwin, new_msgs > 0, FALSE);
207 main_window_unlock(mainwin);
208 inc_notify_cmd(new_msgs, notify);
209 inc_autocheck_timer_set();
213 void inc_pop_before_smtp(PrefsAccount *acc)
215 IncProgressDialog *inc_dialog;
219 mainwin = mainwindow_get_mainwindow();
221 session = inc_session_new(acc);
222 if (!session) return;
223 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
225 inc_dialog = inc_progress_dialog_create(FALSE);
226 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
228 /* FIXME: assumes to attach to first main window */
229 inc_dialog->mainwin = mainwin;
230 inc_progress_dialog_set_list(inc_dialog);
233 toolbar_main_set_sensitive(mainwin);
234 main_window_set_menu_sensitive(mainwin);
237 inc_start(inc_dialog);
240 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
242 IncProgressDialog *inc_dialog;
245 switch (account->protocol) {
249 * FIXME: it should return foldeview_check_new() value.
250 * TODO: do it when bug [19] is fixed (IMAP folder sets
251 * an incorrect new message count)
253 folderview_check_new(FOLDER(account->folder));
257 session = inc_session_new(account);
258 if (!session) return 0;
260 inc_dialog = inc_progress_dialog_create(FALSE);
261 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
263 inc_dialog->mainwin = mainwin;
264 inc_progress_dialog_set_list(inc_dialog);
267 toolbar_main_set_sensitive(mainwin);
268 main_window_set_menu_sensitive(mainwin);
271 return inc_start(inc_dialog);
274 return inc_spool_account(account);
282 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
286 if (inc_lock_count) return 0;
288 if (prefs_common.work_offline &&
289 !inc_offline_should_override(TRUE,
290 _("Claws Mail needs network access in order "
294 inc_autocheck_timer_remove();
295 main_window_lock(mainwin);
297 new_msgs = inc_account_mail_real(mainwin, account);
299 inc_finished(mainwin, new_msgs > 0, FALSE);
300 main_window_unlock(mainwin);
301 inc_autocheck_timer_set();
306 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
309 GList *list, *queue_list = NULL;
310 IncProgressDialog *inc_dialog;
312 gint account_new_msgs = 0;
314 if (prefs_common.work_offline &&
315 !inc_offline_should_override( (autocheck == FALSE),
316 _("Claws Mail needs network access in order "
320 if (inc_lock_count) return;
322 inc_autocheck_timer_remove();
323 main_window_lock(mainwin);
325 list = account_get_list();
327 inc_finished(mainwin, new_msgs > 0, autocheck);
328 main_window_unlock(mainwin);
329 inc_notify_cmd(new_msgs, notify);
330 inc_autocheck_timer_set();
334 /* check local folders */
335 account_new_msgs = inc_all_spool();
336 if (account_new_msgs > 0)
337 new_msgs += account_new_msgs;
339 /* check IMAP4 / News folders */
340 for (list = account_get_list(); list != NULL; list = list->next) {
341 PrefsAccount *account = list->data;
342 if ((account->protocol == A_IMAP4 ||
343 account->protocol == A_NNTP) && account->recv_at_getall) {
344 new_msgs += folderview_check_new(FOLDER(account->folder));
348 /* check POP3 accounts */
349 for (list = account_get_list(); list != NULL; list = list->next) {
351 PrefsAccount *account = list->data;
353 if (account->recv_at_getall) {
354 session = inc_session_new(account);
356 queue_list = g_list_append(queue_list, session);
361 inc_dialog = inc_progress_dialog_create(autocheck);
362 inc_dialog->queue_list = queue_list;
363 inc_dialog->mainwin = mainwin;
364 inc_progress_dialog_set_list(inc_dialog);
366 toolbar_main_set_sensitive(mainwin);
367 main_window_set_menu_sensitive(mainwin);
368 new_msgs += inc_start(inc_dialog);
371 inc_finished(mainwin, new_msgs > 0, autocheck);
372 main_window_unlock(mainwin);
373 inc_notify_cmd(new_msgs, notify);
374 inc_autocheck_timer_set();
377 static void inc_progress_dialog_size_allocate_cb(GtkWidget *widget,
378 GtkAllocation *allocation)
380 g_return_if_fail(allocation != NULL);
382 prefs_common.receivewin_width = allocation->width;
383 prefs_common.receivewin_height = allocation->height;
386 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
388 IncProgressDialog *dialog;
389 ProgressDialog *progress;
390 static GdkGeometry geometry;
392 dialog = g_new0(IncProgressDialog, 1);
394 progress = progress_dialog_create();
395 gtk_window_set_title(GTK_WINDOW(progress->window),
396 _("Retrieving new messages"));
397 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
398 G_CALLBACK(inc_cancel_cb), dialog);
399 g_signal_connect(G_OBJECT(progress->window), "delete_event",
400 G_CALLBACK(inc_dialog_delete_cb), dialog);
401 g_signal_connect(G_OBJECT(progress->window), "size_allocate",
402 G_CALLBACK(inc_progress_dialog_size_allocate_cb), NULL);
403 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
405 progress_dialog_get_fraction(progress);
407 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_COMPLETE,
409 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_CONTINUE,
411 stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_ERROR,
414 if (!geometry.min_height) {
415 geometry.min_width = 460;
416 geometry.min_height = 250;
419 gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
421 gtk_widget_set_size_request(progress->window, prefs_common.receivewin_width,
422 prefs_common.receivewin_height);
424 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
425 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
427 dialog->show_dialog = TRUE;
428 gtk_widget_show_now(progress->window);
431 dialog->dialog = progress;
432 g_get_current_time(&dialog->progress_tv);
433 g_get_current_time(&dialog->folder_tv);
434 dialog->queue_list = NULL;
437 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
442 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
446 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
447 IncSession *session = list->data;
448 Pop3Session *pop3_session = POP3_SESSION(session->session);
450 session->data = inc_dialog;
452 progress_dialog_list_set(inc_dialog->dialog,
454 pop3_session->ac_prefs->account_name,
459 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
461 progress_dialog_get_fraction(inc_dialog->dialog);
462 progress_dialog_set_label(inc_dialog->dialog, "");
463 if (inc_dialog->mainwin)
464 main_window_progress_off(inc_dialog->mainwin);
467 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
469 g_return_if_fail(inc_dialog != NULL);
471 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
473 if (inc_dialog->mainwin)
474 main_window_progress_off(inc_dialog->mainwin);
475 progress_dialog_destroy(inc_dialog->dialog);
480 static IncSession *inc_session_new(PrefsAccount *account)
484 g_return_val_if_fail(account != NULL, NULL);
486 if (account->protocol != A_POP3)
488 if (!account->recv_server || !account->userid)
491 session = g_new0(IncSession, 1);
493 session->session = pop3_session_new(account);
494 session->session->data = session;
495 POP3_SESSION(session->session)->drop_message = inc_drop_message;
496 session_set_recv_message_notify(session->session,
497 inc_recv_message, session);
498 session_set_recv_data_progressive_notify(session->session,
499 inc_recv_data_progressive,
501 session_set_recv_data_notify(session->session,
502 inc_recv_data_finished, session);
507 static void inc_session_destroy(IncSession *session)
509 g_return_if_fail(session != NULL);
511 session_destroy(session->session);
515 static gint inc_start(IncProgressDialog *inc_dialog)
519 Pop3Session *pop3_session;
525 FolderItem *processing, *inbox;
526 GSList *msglist, *msglist_element;
527 gboolean cancelled = FALSE;
529 qlist = inc_dialog->queue_list;
530 while (qlist != NULL) {
531 GList *next = qlist->next;
533 session = qlist->data;
534 pop3_session = POP3_SESSION(session->session);
535 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
536 if (pop3_session->ac_prefs->passwd)
538 g_strdup(pop3_session->ac_prefs->passwd);
539 else if (pop3_session->ac_prefs->tmp_pass)
541 g_strdup(pop3_session->ac_prefs->tmp_pass);
545 if (inc_dialog->show_dialog)
546 manage_window_focus_in
547 (inc_dialog->dialog->window,
550 pass = input_dialog_query_password
551 (pop3_session->ac_prefs->recv_server,
554 if (inc_dialog->show_dialog)
555 manage_window_focus_out
556 (inc_dialog->dialog->window,
560 pop3_session->ac_prefs->tmp_pass =
562 pop3_session->pass = pass;
569 #define SET_PIXMAP_AND_TEXT(pix, str) \
571 progress_dialog_list_set(inc_dialog->dialog, \
572 inc_dialog->cur_row, \
578 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
579 session = inc_dialog->queue_list->data;
580 pop3_session = POP3_SESSION(session->session);
581 GSList *filtered, *unfiltered;
583 if (pop3_session->pass == NULL) {
584 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
585 inc_session_destroy(session);
586 inc_dialog->queue_list =
587 g_list_remove(inc_dialog->queue_list, session);
591 inc_progress_dialog_clear(inc_dialog);
592 progress_dialog_scroll_to_row(inc_dialog->dialog,
593 inc_dialog->cur_row);
595 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
597 /* begin POP3 session */
598 inc_state = inc_pop3_session_do(session);
602 if (pop3_session->cur_total_num > 0)
603 msg = g_strdup_printf(
604 ngettext("Done (%d message (%s) received)",
605 "Done (%d messages (%s) received)",
606 pop3_session->cur_total_num),
607 pop3_session->cur_total_num,
608 to_human_readable(pop3_session->cur_total_recv_bytes));
610 msg = g_strdup_printf(_("Done (no new messages)"));
611 SET_PIXMAP_AND_TEXT(okpix, msg);
614 case INC_CONNECT_ERROR:
615 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
617 case INC_AUTH_FAILED:
618 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
621 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
626 case INC_SOCKET_ERROR:
628 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
631 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
634 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
635 if (!inc_dialog->show_dialog)
642 if (pop3_session->error_val == PS_AUTHFAIL) {
643 if(!prefs_common.no_recv_err_panel) {
644 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
645 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
646 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
650 /* CLAWS: perform filtering actions on dropped message */
651 /* CLAWS: get default inbox (perhaps per account) */
652 if (pop3_session->ac_prefs->inbox) {
653 /* CLAWS: get destination folder / mailbox */
654 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
656 inbox = folder_get_default_inbox();
658 inbox = folder_get_default_inbox();
660 /* get list of messages in processing */
661 processing = folder_get_default_processing();
662 folder_item_scan(processing);
663 msglist = folder_item_get_msg_list(processing);
665 /* process messages */
666 folder_item_update_freeze();
668 procmsg_msglist_filter(msglist, pop3_session->ac_prefs,
669 &filtered, &unfiltered,
670 pop3_session->ac_prefs->filter_on_recv);
672 filtering_move_and_copy_msgs(msglist);
673 if (unfiltered != NULL)
674 folder_item_move_msgs(inbox, unfiltered);
676 for(msglist_element = msglist; msglist_element != NULL;
677 msglist_element = msglist_element->next) {
678 MsgInfo *msginfo = (MsgInfo *)msglist_element->data;
679 procmsg_msginfo_free(msginfo);
681 folder_item_update_thaw();
683 g_slist_free(msglist);
684 g_slist_free(filtered);
685 g_slist_free(unfiltered);
689 new_msgs += pop3_session->cur_total_num;
691 if (pop3_session->error_val == PS_AUTHFAIL &&
692 pop3_session->ac_prefs->tmp_pass) {
693 g_free(pop3_session->ac_prefs->tmp_pass);
694 pop3_session->ac_prefs->tmp_pass = NULL;
697 pop3_write_uidl_list(pop3_session);
699 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
701 if (inc_dialog->show_dialog)
702 manage_window_focus_in
703 (inc_dialog->dialog->window,
705 inc_put_error(inc_state, pop3_session);
706 if (inc_dialog->show_dialog)
707 manage_window_focus_out
708 (inc_dialog->dialog->window,
710 if (inc_state == INC_NO_SPACE ||
711 inc_state == INC_IO_ERROR)
714 folder_item_free_cache(processing, TRUE);
716 inc_session_destroy(session);
717 inc_dialog->queue_list =
718 g_list_remove(inc_dialog->queue_list, session);
721 #undef SET_PIXMAP_AND_TEXT
724 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
725 "Finished (%d new messages)",
726 new_msgs), new_msgs);
728 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
730 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
732 while (inc_dialog->queue_list != NULL) {
733 session = inc_dialog->queue_list->data;
734 inc_session_destroy(session);
735 inc_dialog->queue_list =
736 g_list_remove(inc_dialog->queue_list, session);
739 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
740 inc_progress_dialog_destroy(inc_dialog);
742 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
744 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
753 static IncState inc_pop3_session_do(IncSession *session)
755 Pop3Session *pop3_session = POP3_SESSION(session->session);
756 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
761 debug_print("getting new messages of account %s...\n",
762 pop3_session->ac_prefs->account_name);
764 pop3_session->ac_prefs->last_pop_login_time = time(NULL);
766 buf = g_strdup_printf(_("%s: Retrieving new messages"),
767 pop3_session->ac_prefs->recv_server);
768 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
771 server = pop3_session->ac_prefs->recv_server;
773 port = pop3_session->ac_prefs->set_popport ?
774 pop3_session->ac_prefs->popport :
775 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
776 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
777 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
778 SESSION(pop3_session)->nonblocking =
779 pop3_session->ac_prefs->use_nonblocking_ssl;
781 if (pop3_session->ac_prefs->ssl_pop != SSL_NONE) {
782 if (alertpanel_full(_("Insecure connection"),
783 _("This connection is configured to be secured "
784 "using SSL, but SSL is not available in this "
785 "build of Claws Mail. \n\n"
786 "Do you want to continue connecting to this "
787 "server? The communication would not be "
789 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
790 NULL, FALSE, NULL, ALERT_WARNING,
791 G_ALERTDEFAULT) != G_ALERTALTERNATE)
794 port = pop3_session->ac_prefs->set_popport ?
795 pop3_session->ac_prefs->popport : 110;
798 buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
799 statusbar_print_all("%s", buf);
800 log_message(LOG_PROTOCOL, "%s\n", buf);
802 progress_dialog_set_label(inc_dialog->dialog, buf);
806 session_set_timeout(SESSION(pop3_session),
807 prefs_common.io_timeout_secs * 1000);
809 if (session_connect(SESSION(pop3_session), server, port) < 0) {
810 if(!prefs_common.no_recv_err_panel) {
811 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
812 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
813 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
815 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
817 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
819 log_error(LOG_PROTOCOL, _("Can't connect to POP3 server: %s:%d\n"),
822 session->inc_state = INC_CONNECT_ERROR;
824 return INC_CONNECT_ERROR;
827 while (session_is_connected(SESSION(pop3_session)) &&
828 session->inc_state != INC_CANCEL)
829 gtk_main_iteration();
831 if (session->inc_state == INC_SUCCESS) {
832 switch (pop3_session->error_val) {
834 switch (SESSION(pop3_session)->state) {
836 if (pop3_session->state == POP3_READY)
837 session->inc_state = INC_CONNECT_ERROR;
839 session->inc_state = INC_ERROR;
842 session->inc_state = INC_EOF;
844 case SESSION_TIMEOUT:
845 session->inc_state = INC_TIMEOUT;
848 session->inc_state = INC_SUCCESS;
853 session->inc_state = INC_AUTH_FAILED;
856 session->inc_state = INC_IO_ERROR;
859 session->inc_state = INC_SOCKET_ERROR;
862 session->inc_state = INC_LOCKED;
865 session->inc_state = INC_ERROR;
870 session_disconnect(SESSION(pop3_session));
873 return session->inc_state;
876 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
877 IncSession *inc_session)
879 inc_progress_dialog_set_label(inc_dialog, inc_session);
880 inc_progress_dialog_set_progress(inc_dialog, inc_session);
883 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
884 IncSession *inc_session)
886 ProgressDialog *dialog = inc_dialog->dialog;
887 Pop3Session *session;
889 g_return_if_fail(inc_session != NULL);
891 session = POP3_SESSION(inc_session->session);
893 switch (session->state) {
896 case POP3_GETAUTH_USER:
897 case POP3_GETAUTH_PASS:
898 case POP3_GETAUTH_APOP:
899 progress_dialog_set_label(dialog, _("Authenticating..."));
901 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
902 SESSION(session)->server,
903 session->ac_prefs->account_name);
905 case POP3_GETRANGE_STAT:
906 progress_dialog_set_label
907 (dialog, _("Getting the number of new messages (STAT)..."));
909 case POP3_GETRANGE_LAST:
910 progress_dialog_set_label
911 (dialog, _("Getting the number of new messages (LAST)..."));
913 case POP3_GETRANGE_UIDL:
914 progress_dialog_set_label
915 (dialog, _("Getting the number of new messages (UIDL)..."));
917 case POP3_GETSIZE_LIST:
918 progress_dialog_set_label
919 (dialog, _("Getting the size of messages (LIST)..."));
926 progress_dialog_set_label(dialog, _("Quitting"));
933 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
934 IncSession *inc_session)
936 gchar buf[MSGBUFSIZE];
937 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
938 gchar *total_size_str;
942 if (!pop3_session->new_msg_exist) return;
944 cur_total = inc_session->cur_total_bytes;
945 total = pop3_session->total_bytes;
946 if (pop3_session->state == POP3_RETR ||
947 pop3_session->state == POP3_RETR_RECV ||
948 pop3_session->state == POP3_DELETE) {
949 Xstrdup_a(total_size_str, to_human_readable(total), return);
950 g_snprintf(buf, sizeof(buf),
951 _("Retrieving message (%d / %d) (%s / %s)"),
952 pop3_session->cur_msg, pop3_session->count,
953 to_human_readable(cur_total), total_size_str);
954 progress_dialog_set_label(inc_dialog->dialog, buf);
957 progress_dialog_set_fraction
958 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
960 g_snprintf(buf, sizeof(buf), "%d / %d",
961 pop3_session->cur_msg, pop3_session->count);
962 gtk_progress_bar_set_text
963 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), buf);
964 gtk_progress_bar_set_fraction
965 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
966 (total == 0) ? 0 : (gfloat)cur_total / (gfloat)total);
968 if (pop3_session->cur_total_num > 0) {
969 g_snprintf(buf, sizeof(buf),
970 ngettext("Retrieving (%d message (%s) received)",
971 "Retrieving (%d messages (%s) received)",
972 pop3_session->cur_total_num),
973 pop3_session->cur_total_num,
975 (pop3_session->cur_total_recv_bytes));
976 progress_dialog_list_set_status(inc_dialog->dialog,
982 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
983 IncSession *inc_session)
989 g_get_current_time(&tv_cur);
991 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
992 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
993 if (tv_result.tv_usec < 0) {
995 tv_result.tv_usec += G_USEC_PER_SEC;
998 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
999 if (msec > PROGRESS_UPDATE_INTERVAL) {
1000 inc_progress_dialog_update(inc_dialog, inc_session);
1001 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
1002 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
1006 static gint inc_recv_data_progressive(Session *session, guint cur_len,
1007 guint total_len, gpointer data)
1009 IncSession *inc_session = (IncSession *)data;
1010 Pop3Session *pop3_session = POP3_SESSION(session);
1011 IncProgressDialog *inc_dialog;
1014 g_return_val_if_fail(inc_session != NULL, -1);
1016 if (pop3_session->state != POP3_RETR &&
1017 pop3_session->state != POP3_RETR_RECV &&
1018 pop3_session->state != POP3_DELETE &&
1019 pop3_session->state != POP3_LOGOUT) return 0;
1021 if (!pop3_session->new_msg_exist) return 0;
1023 cur_total = pop3_session->cur_total_bytes + cur_len;
1024 if (cur_total > pop3_session->total_bytes)
1025 cur_total = pop3_session->total_bytes;
1026 inc_session->cur_total_bytes = cur_total;
1028 inc_dialog = (IncProgressDialog *)inc_session->data;
1029 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1034 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1036 IncSession *inc_session = (IncSession *)data;
1037 IncProgressDialog *inc_dialog;
1039 g_return_val_if_fail(inc_session != NULL, -1);
1041 inc_dialog = (IncProgressDialog *)inc_session->data;
1043 inc_recv_data_progressive(session, 0, 0, inc_session);
1045 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1046 inc_progress_dialog_update(inc_dialog, inc_session);
1052 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1054 IncSession *inc_session = (IncSession *)data;
1055 IncProgressDialog *inc_dialog;
1057 g_return_val_if_fail(inc_session != NULL, -1);
1059 inc_dialog = (IncProgressDialog *)inc_session->data;
1061 switch (POP3_SESSION(session)->state) {
1062 case POP3_GETAUTH_USER:
1063 case POP3_GETAUTH_PASS:
1064 case POP3_GETAUTH_APOP:
1065 case POP3_GETRANGE_STAT:
1066 case POP3_GETRANGE_LAST:
1067 case POP3_GETRANGE_UIDL:
1068 case POP3_GETSIZE_LIST:
1069 inc_progress_dialog_update(inc_dialog, inc_session);
1072 inc_recv_data_progressive(session, 0, 0, inc_session);
1075 inc_progress_dialog_update(inc_dialog, inc_session);
1084 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1087 FolderItem *dropfolder;
1088 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1091 g_return_val_if_fail(inc_session != NULL, -1);
1093 if (session->ac_prefs->inbox) {
1094 inbox = folder_find_item_from_identifier
1095 (session->ac_prefs->inbox);
1097 inbox = folder_get_default_inbox();
1099 inbox = folder_get_default_inbox();
1105 /* CLAWS: claws uses a global .processing folder for the filtering. */
1106 dropfolder = folder_get_default_processing();
1108 /* add msg file to drop folder */
1109 if ((msgnum = folder_item_add_msg(
1110 dropfolder, file, NULL, TRUE)) < 0) {
1118 static void inc_put_error(IncState istate, Pop3Session *session)
1120 gchar *log_msg = NULL;
1121 gchar *err_msg = NULL;
1122 gboolean fatal_error = FALSE;
1125 case INC_CONNECT_ERROR:
1126 log_msg = _("Connection failed.");
1128 if (prefs_common.no_recv_err_panel)
1130 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1131 SESSION(session)->server,
1132 SESSION(session)->port);
1135 log_msg = _("Error occurred while processing mail.");
1137 if (prefs_common.no_recv_err_panel)
1139 if (session->error_msg)
1140 err_msg = g_strdup_printf
1141 (_("Error occurred while processing mail:\n%s"),
1142 session->error_msg);
1144 err_msg = g_strdup(log_msg);
1147 log_msg = _("No disk space left.");
1148 err_msg = g_strdup(log_msg);
1152 log_msg = _("Can't write file.");
1153 err_msg = g_strdup(log_msg);
1156 case INC_SOCKET_ERROR:
1157 log_msg = _("Socket error.");
1158 if (prefs_common.no_recv_err_panel)
1160 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1161 SESSION(session)->server,
1162 SESSION(session)->port);
1165 log_msg = _("Connection closed by the remote host.");
1166 if (prefs_common.no_recv_err_panel)
1168 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1169 SESSION(session)->server,
1170 SESSION(session)->port);
1173 log_msg = _("Mailbox is locked.");
1174 if (prefs_common.no_recv_err_panel)
1176 if (session->error_msg)
1177 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1178 session->error_msg);
1180 err_msg = g_strdup(log_msg);
1182 case INC_AUTH_FAILED:
1183 log_msg = _("Authentication failed.");
1185 if (prefs_common.no_recv_err_panel)
1187 if (session->error_msg)
1188 err_msg = g_strdup_printf
1189 (_("Authentication failed:\n%s"), session->error_msg);
1191 err_msg = g_strdup(log_msg);
1194 log_msg = _("Session timed out.");
1195 if (prefs_common.no_recv_err_panel)
1197 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1198 SESSION(session)->server,
1199 SESSION(session)->port);
1207 log_error(LOG_PROTOCOL, "%s\n", log_msg);
1209 log_warning(LOG_PROTOCOL, "%s\n", log_msg);
1211 if (prefs_common.no_recv_err_panel && fatal_error)
1212 mainwindow_show_error();
1215 alertpanel_error_log(err_msg);
1220 static void inc_cancel(IncProgressDialog *dialog)
1222 IncSession *session;
1224 g_return_if_fail(dialog != NULL);
1226 if (dialog->queue_list == NULL) {
1227 inc_progress_dialog_destroy(dialog);
1231 session = dialog->queue_list->data;
1233 session->inc_state = INC_CANCEL;
1235 log_message(LOG_PROTOCOL, _("Incorporation cancelled\n"));
1238 gboolean inc_is_active(void)
1240 return (inc_dialog_list != NULL);
1243 void inc_cancel_all(void)
1247 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1248 inc_cancel((IncProgressDialog *)cur->data);
1251 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1253 inc_cancel((IncProgressDialog *)data);
1256 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1259 IncProgressDialog *dialog = (IncProgressDialog *)data;
1261 if (dialog->queue_list == NULL)
1262 inc_progress_dialog_destroy(dialog);
1267 static gint inc_spool_account(PrefsAccount *account)
1273 if (account->local_inbox) {
1274 inbox = folder_find_item_from_identifier(account->local_inbox);
1276 inbox = folder_get_default_inbox();
1278 inbox = folder_get_default_inbox();
1280 if (is_file_exist(account->local_mbox))
1281 mbox = g_strdup(account->local_mbox);
1282 else if (is_dir_exist(account->local_mbox))
1283 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1284 g_get_user_name(), NULL);
1286 debug_print("%s: local mailbox not found.\n",
1287 account->local_mbox);
1291 result = get_spool(inbox, mbox, account);
1294 statusbar_pop_all();
1299 static gint inc_all_spool(void)
1303 gint account_new_msgs = 0;
1305 list = account_get_list();
1306 if (!list) return 0;
1308 for (; list != NULL; list = list->next) {
1309 PrefsAccount *account = list->data;
1311 if ((account->protocol == A_LOCAL) &&
1312 (account->recv_at_getall)) {
1313 account_new_msgs = inc_spool_account(account);
1314 if (account_new_msgs > 0)
1315 new_msgs += account_new_msgs;
1322 static gint get_spool(FolderItem *dest, const gchar *mbox, PrefsAccount *account)
1326 gchar tmp_mbox[MAXPATHLEN + 1];
1328 g_return_val_if_fail(dest != NULL, -1);
1329 g_return_val_if_fail(mbox != NULL, -1);
1330 g_return_val_if_fail(account != NULL, -1);
1332 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1333 debug_print("%s: no messages in local mailbox.\n", mbox);
1335 } else if (size < 0)
1338 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1341 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%p",
1342 get_tmp_dir(), G_DIR_SEPARATOR, mbox);
1344 if (copy_mbox(lockfd, tmp_mbox) < 0) {
1345 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1349 debug_print("Getting new messages from %s into %s...\n",
1352 msgs = proc_mbox(dest, tmp_mbox, account->filter_on_recv, account);
1355 if (msgs >= 0) empty_mbox(mbox);
1356 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1366 void inc_unlock(void)
1368 if (inc_lock_count > 0)
1372 static guint autocheck_timer = 0;
1373 static gpointer autocheck_data = NULL;
1375 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1378 gchar *buf, *numpos, *ret_str;
1379 gssize by_read = 0, by_written = 0;
1381 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1382 *prefs_common.newmail_notify_cmd))
1384 buf = g_strdup(prefs_common.newmail_notify_cmd);
1385 if ((numpos = strstr(buf, "%d")) != NULL) {
1389 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1394 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1396 if (ret_str && by_written) {
1400 debug_print("executing new mail notification command: %s\n", buf);
1401 execute_command_line(buf, TRUE);
1407 static void maemo_connection_event(ConIcConnection *connection,
1408 ConIcConnectionEvent *event,
1411 ConIcConnectionStatus status =
1412 con_ic_connection_event_get_status(event);
1413 MainWindow *mainwin = (MainWindow *)user_data;
1415 case CON_IC_STATUS_CONNECTED:
1416 debug_print("we're connected\n");
1417 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1420 debug_print("we're disconnected\n");
1421 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1422 maemo_warned_offline = FALSE;
1429 void inc_autocheck_timer_init(MainWindow *mainwin)
1432 GValue *val = g_new0(GValue, 1);
1433 maemo_connection = con_ic_connection_new();
1435 g_value_init(val, G_TYPE_BOOLEAN);
1436 g_value_set_boolean(val, TRUE);
1437 g_object_set_property(G_OBJECT(maemo_connection),
1438 "automatic-connection-events", val);
1440 g_signal_connect (maemo_connection, "connection-event",
1441 G_CALLBACK(maemo_connection_event), mainwin);
1442 con_ic_connection_connect (maemo_connection,
1443 CON_IC_CONNECT_FLAG_AUTOMATICALLY_TRIGGERED);
1444 autocheck_data = mainwin;
1446 autocheck_data = mainwin;
1447 inc_autocheck_timer_set();
1451 static void inc_autocheck_timer_set_interval(guint interval)
1453 inc_autocheck_timer_remove();
1454 /* last test is to avoid re-enabling auto_check after modifying
1455 the common preferences */
1456 if (prefs_common.autochk_newmail && autocheck_data
1457 && prefs_common.work_offline == FALSE) {
1458 autocheck_timer = g_timeout_add
1459 (interval, inc_autocheck_func, autocheck_data);
1460 debug_print("added timer = %d\n", autocheck_timer);
1464 void inc_autocheck_timer_set(void)
1466 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1468 con_ic_connection_connect (maemo_connection,
1469 CON_IC_CONNECT_FLAG_AUTOMATICALLY_TRIGGERED);
1473 void inc_autocheck_timer_remove(void)
1475 if (autocheck_timer) {
1476 debug_print("removed timer = %d\n", autocheck_timer);
1477 g_source_remove(autocheck_timer);
1478 autocheck_timer = 0;
1482 static gint inc_autocheck_func(gpointer data)
1484 MainWindow *mainwin = (MainWindow *)data;
1486 if (inc_lock_count) {
1487 debug_print("autocheck is locked.\n");
1488 inc_autocheck_timer_set_interval(1000);
1492 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1497 gboolean inc_offline_should_override(gboolean force_ask, const gchar *msg)
1499 static time_t overridden_yes = 0;
1500 static time_t overridden_no = 0;
1501 int length = 10; /* minutes */
1502 gint answer = G_ALERTDEFAULT;
1505 if (prefs_common.work_offline) {
1506 if (force_ask && !maemo_warned_offline) {
1507 if (mainwindow_get_mainwindow())
1508 hildon_banner_show_information(
1509 mainwindow_get_mainwindow()->window,
1511 _("Unable to connect: you are offline."));
1512 maemo_warned_offline = TRUE;
1520 if (prefs_common.autochk_newmail)
1521 length = prefs_common.autochk_itv; /* minutes */
1524 overridden_no = (time_t)0;
1527 if (prefs_common.work_offline) {
1530 if (time(NULL) - overridden_yes < length * 60) /* seconds */
1532 else if (time(NULL) - overridden_no < length * 60) /* seconds */
1536 tmp = g_strdup_printf(
1537 _("%s%sYou're working offline. Override for %d minutes?"),
1542 tmp = g_strdup_printf(
1543 _("%s%sYou're working offline. Override?"),
1547 answer = alertpanel(_("Offline warning"),
1549 GTK_STOCK_NO, "+" GTK_STOCK_YES,
1550 !force_ask? _("On_ly once"):NULL);
1552 if (answer == G_ALERTALTERNATE) {
1553 overridden_yes = time(NULL);
1555 } else if (answer == G_ALERTDEFAULT) {
1557 overridden_no = time(NULL);
1560 overridden_yes = (time_t)0;
1561 overridden_no = (time_t)0;