fix bug 4773, 'remove obsolescent AC_C_CONST'
[claws.git] / src / inc.c
blob731f2159ff4b2ddcccf439e4dcf98907c663d89f
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2022 the Claws Mail team and Hiroyuki Yamamoto
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <string.h>
33 #include "main.h"
34 #include "inc.h"
35 #include "mainwindow.h"
36 #include "folderview.h"
37 #include "summaryview.h"
38 #include "prefs_common.h"
39 #include "prefs_account.h"
40 #include "account.h"
41 #include "procmsg.h"
42 #include "proxy.h"
43 #include "socket.h"
44 #include "ssl.h"
45 #include "pop.h"
46 #include "recv.h"
47 #include "mbox.h"
48 #include "file-utils.h"
49 #include "utils.h"
50 #include "gtkutils.h"
51 #include "statusbar.h"
52 #include "msgcache.h"
53 #include "manage_window.h"
54 #include "stock_pixmap.h"
55 #include "progressdialog.h"
56 #include "inputdialog.h"
57 #include "alertpanel.h"
58 #include "folder.h"
59 #include "filtering.h"
60 #include "log.h"
61 #include "hooks.h"
62 #include "logwindow.h"
63 #include "passwordstore.h"
64 #ifdef USE_OAUTH2
65 #include "oauth2.h"
66 #endif
68 extern SessionStats session_stats;
70 static GList *inc_dialog_list = NULL;
72 static time_t inc_offline_overridden_yes = 0;
73 static time_t inc_offline_overridden_no = 0;
75 guint inc_lock_count = 0;
77 static GdkPixbuf *currentpix;
78 static GdkPixbuf *errorpix;
79 static GdkPixbuf *okpix;
81 #define MESSAGEBUFSIZE 8192
83 static void inc_update_stats(gint new_msgs);
84 static void inc_finished (MainWindow *mainwin,
85 gboolean new_messages,
86 gboolean autocheck);
87 static gint inc_account_mail_real (MainWindow *mainwin,
88 PrefsAccount *account);
90 static IncProgressDialog *inc_progress_dialog_create
91 (gboolean autocheck);
92 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
93 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
95 static IncSession *inc_session_new (PrefsAccount *account);
96 static void inc_session_destroy (IncSession *session);
97 static gint inc_start (IncProgressDialog *inc_dialog);
98 static IncState inc_pop3_session_do (IncSession *session);
100 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
101 IncSession *inc_session);
103 static void inc_progress_dialog_set_label
104 (IncProgressDialog *inc_dialog,
105 IncSession *inc_session);
106 static void inc_progress_dialog_set_progress
107 (IncProgressDialog *inc_dialog,
108 IncSession *inc_session);
110 static void inc_progress_dialog_update_periodic
111 (IncProgressDialog *inc_dialog,
112 IncSession *inc_session);
114 static gint inc_recv_data_progressive (Session *session,
115 guint cur_len,
116 guint total_len,
117 gpointer data);
118 static gint inc_recv_data_finished (Session *session,
119 guint len,
120 gpointer data);
121 static gint inc_recv_message (Session *session,
122 const gchar *msg,
123 gpointer data);
124 static gint inc_drop_message (Pop3Session *session,
125 const gchar *file);
127 static void inc_put_error (IncState istate,
128 Pop3Session *session);
130 static void inc_showlog_cb (GtkWidget *widget,
131 gpointer data);
132 static void inc_cancel_cb (GtkWidget *widget,
133 gpointer data);
134 static gint inc_dialog_delete_cb (GtkWidget *widget,
135 GdkEventAny *event,
136 gpointer data);
138 static gint get_spool (FolderItem *dest,
139 const gchar *mbox,
140 PrefsAccount *account);
142 static gint inc_spool_account(PrefsAccount *account);
143 static void inc_autocheck_timer_set_interval (guint interval);
144 static gint inc_autocheck_func (gpointer data);
146 static void inc_notify_cmd (gint new_msgs,
147 gboolean notify);
149 static void inc_update_stats(gint new_msgs)
151 /* update session statistics */
152 session_stats.received += new_msgs;
156 * inc_finished:
157 * @mainwin: Main window.
158 * @new_messages: TRUE if some messages have been received.
160 * Update the folder view and the summary view after receiving
161 * messages. If @new_messages is FALSE, this function avoids unneeded
162 * updating.
164 static void inc_finished(MainWindow *mainwin, gboolean new_messages, gboolean autocheck)
166 if (prefs_common.scan_all_after_inc)
167 folderview_check_new(NULL);
169 if (!autocheck && new_messages && prefs_common.open_inbox_on_inc) {
170 FolderItem *item = NULL;
172 if (cur_account && cur_account->inbox)
173 item = folder_find_item_from_identifier(cur_account->inbox);
174 if (item == NULL && cur_account && cur_account->folder)
175 item = cur_account->folder->inbox;
176 if (item == NULL)
177 item = folder_get_default_inbox();
179 folderview_unselect(mainwin->folderview);
180 folderview_select(mainwin->folderview, item);
182 statusbar_progress_all(0,0,0);
185 void inc_mail(MainWindow *mainwin, gboolean notify)
187 gint new_msgs = 0;
188 gint account_new_msgs = 0;
190 if (inc_lock_count) return;
192 if (prefs_common.work_offline &&
193 !inc_offline_should_override(TRUE,
194 _("Claws Mail needs network access in order "
195 "to get mails.")))
196 return;
198 inc_lock();
199 inc_autocheck_timer_remove();
200 main_window_lock(mainwin);
202 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
203 /* external incorporating program */
204 if (execute_command_line(prefs_common.extinc_cmd, FALSE, NULL) < 0) {
205 main_window_unlock(mainwin);
206 inc_autocheck_timer_set();
207 inc_unlock();
208 return;
210 } else {
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_update_stats(new_msgs);
217 inc_finished(mainwin, new_msgs > 0, FALSE);
218 main_window_unlock(mainwin);
219 inc_notify_cmd(new_msgs, notify);
220 inc_autocheck_timer_set();
221 inc_unlock();
224 void inc_pop_before_smtp(PrefsAccount *acc)
226 IncProgressDialog *inc_dialog;
227 IncSession *session;
228 MainWindow *mainwin;
230 mainwin = mainwindow_get_mainwindow();
232 session = inc_session_new(acc);
233 if (!session) return;
234 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
236 inc_dialog = inc_progress_dialog_create(FALSE);
237 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
238 session);
239 /* FIXME: assumes to attach to first main window */
240 inc_dialog->mainwin = mainwin;
241 inc_progress_dialog_set_list(inc_dialog);
243 if (mainwin) {
244 toolbar_main_set_sensitive(mainwin);
245 main_window_set_menu_sensitive(mainwin);
248 inc_start(inc_dialog);
251 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
253 IncProgressDialog *inc_dialog;
254 IncSession *session;
256 switch (account->protocol) {
257 case A_IMAP4:
258 case A_NNTP:
259 /* Melvin: bug [14]
260 * FIXME: it should return foldeview_check_new() value.
261 * TODO: do it when bug [19] is fixed (IMAP folder sets
262 * an incorrect new message count)
264 folderview_check_new(FOLDER(account->folder));
265 return 0;
266 case A_POP3:
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,
272 session);
273 inc_dialog->mainwin = mainwin;
274 inc_progress_dialog_set_list(inc_dialog);
276 if (mainwin) {
277 toolbar_main_set_sensitive(mainwin);
278 main_window_set_menu_sensitive(mainwin);
281 return inc_start(inc_dialog);
283 case A_LOCAL:
284 return inc_spool_account(account);
286 default:
287 break;
289 return 0;
292 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
294 gint new_msgs;
296 if (inc_lock_count) return 0;
298 if (account->receive_in_progress) return 0;
300 if (prefs_common.work_offline &&
301 !inc_offline_should_override(TRUE,
302 _("Claws Mail needs network access in order "
303 "to get mails.")))
304 return 0;
306 inc_autocheck_timer_remove();
307 main_window_lock(mainwin);
309 new_msgs = inc_account_mail_real(mainwin, account);
311 inc_update_stats(new_msgs);
312 inc_finished(mainwin, new_msgs > 0, FALSE);
313 main_window_unlock(mainwin);
314 inc_autocheck_timer_set();
316 return new_msgs;
319 void inc_account_list_mail(MainWindow *mainwin, GList *account_list, gboolean autocheck,
320 gboolean notify)
322 GList *list, *queue_list = NULL;
323 IncProgressDialog *inc_dialog;
324 gint new_msgs = 0, num;
326 if (prefs_common.work_offline &&
327 !inc_offline_should_override( (autocheck == FALSE),
328 _("Claws Mail needs network access in order "
329 "to get mails.")))
330 return;
332 if (inc_lock_count) return;
334 main_window_lock(mainwin);
336 if (!account_list) {
337 inc_update_stats(new_msgs);
338 inc_finished(mainwin, new_msgs > 0, autocheck);
339 main_window_unlock(mainwin);
340 inc_notify_cmd(new_msgs, notify);
341 return;
344 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
345 /* external incorporating program */
346 if (execute_command_line(prefs_common.extinc_cmd, FALSE, NULL) < 0) {
347 log_error(LOG_PROTOCOL, _("%s failed\n"), prefs_common.extinc_cmd);
349 main_window_unlock(mainwin);
350 return;
354 /* Check all accounts in the list, one by one. */
355 for (list = account_list; list != NULL; list = list->next) {
356 PrefsAccount *account = list->data;
358 if (account == NULL) {
359 debug_print("INC: Huh? inc_account_list_mail() got a NULL account, this should not happen!\n");
360 continue;
363 debug_print("INC: checking account %d\n", account->account_id);
364 switch (account->protocol) {
365 case A_POP3:
366 if (!(account->receive_in_progress)) {
367 IncSession *session = inc_session_new(account);
369 if (session != NULL) {
370 debug_print("INC: adding POP3 account %d to inc queue\n",
371 account->account_id);
372 queue_list = g_list_append(queue_list, session);
375 break;
377 case A_IMAP4:
378 case A_NNTP:
379 new_msgs += folderview_check_new(FOLDER(account->folder));
380 break;
382 case A_LOCAL:
383 num = inc_spool_account(account);
384 if (num > 0)
385 new_msgs += num;
386 break;
388 case A_NONE:
389 /* Nothing to do here, it's a SMTP-only account. */
390 break;
392 default:
393 debug_print("INC: encountered account %d with unknown protocol %d, ignoring\n",
394 account->account_id, account->protocol);
395 break;
401 if (queue_list) {
402 inc_dialog = inc_progress_dialog_create(autocheck);
403 inc_dialog->queue_list = queue_list;
404 inc_dialog->mainwin = mainwin;
405 inc_progress_dialog_set_list(inc_dialog);
407 toolbar_main_set_sensitive(mainwin);
408 main_window_set_menu_sensitive(mainwin);
409 new_msgs += inc_start(inc_dialog);
412 inc_update_stats(new_msgs);
413 inc_finished(mainwin, new_msgs > 0, autocheck);
414 main_window_unlock(mainwin);
415 inc_notify_cmd(new_msgs, notify);
418 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
419 gboolean check_at_startup, gboolean notify)
421 GList *list, *list2 = NULL;
422 gboolean condition = FALSE;
423 gboolean hide_dialog = FALSE;
425 debug_print("INC: inc_all_account_mail(), autocheck: %s\n",
426 autocheck ? "YES" : "NO");
428 /* Collect list of accounts which use the global autocheck interval. */
429 for (list = account_get_list(); list != NULL; list = list->next) {
430 PrefsAccount *account = list->data;
432 /* Nothing to do for SMTP-only accounts. */
433 if (account->protocol == A_NONE)
434 continue;
436 /* Set up condition which decides whether or not to check
437 * this account, based on whether we're doing global autocheck
438 * or a check at startup or a manual 'Get all' check. */
439 if (autocheck)
440 condition = prefs_common_get_prefs()->autochk_newmail
441 && account->autochk_use_default;
442 else if (check_at_startup || (!check_at_startup && !autocheck))
443 condition = account->recv_at_getall;
445 if (condition) {
446 debug_print("INC: will check account %d\n", account->account_id);
447 list2 = g_list_append(list2, account);
451 /* Do the check on the collected accounts. */
452 if (list2 != NULL) {
453 if (autocheck || check_at_startup)
454 hide_dialog = TRUE;
455 inc_account_list_mail(mainwin, list2, hide_dialog, notify);
456 g_list_free(list2);
460 static void inc_progress_dialog_size_allocate_cb(GtkWidget *widget,
461 GtkAllocation *allocation)
463 cm_return_if_fail(allocation != NULL);
465 gtk_window_get_size(GTK_WINDOW(widget),
466 &prefs_common.receivewin_width, &prefs_common.receivewin_height);
469 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
471 IncProgressDialog *dialog;
472 ProgressDialog *progress;
473 static GdkGeometry geometry;
475 dialog = g_new0(IncProgressDialog, 1);
477 progress = progress_dialog_create();
478 gtk_window_set_title(GTK_WINDOW(progress->window),
479 _("Retrieving new messages"));
480 g_signal_connect(G_OBJECT(progress->showlog_btn), "clicked",
481 G_CALLBACK(inc_showlog_cb), dialog);
482 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
483 G_CALLBACK(inc_cancel_cb), dialog);
484 g_signal_connect(G_OBJECT(progress->window), "delete_event",
485 G_CALLBACK(inc_dialog_delete_cb), dialog);
486 g_signal_connect(G_OBJECT(progress->window), "size_allocate",
487 G_CALLBACK(inc_progress_dialog_size_allocate_cb), NULL);
488 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
490 progress_dialog_get_fraction(progress);
492 stock_pixbuf_gdk(STOCK_PIXMAP_COMPLETE, &okpix);
493 stock_pixbuf_gdk(STOCK_PIXMAP_CONTINUE, &currentpix);
494 stock_pixbuf_gdk(STOCK_PIXMAP_ERROR, &errorpix);
496 if (!geometry.min_height) {
497 geometry.min_width = 460;
498 geometry.min_height = 250;
501 gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
502 GDK_HINT_MIN_SIZE);
503 gtk_widget_set_size_request(progress->window, prefs_common.receivewin_width,
504 prefs_common.receivewin_height);
506 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
507 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
508 !autocheck)) {
509 dialog->show_dialog = TRUE;
510 gtk_widget_show_now(progress->window);
513 dialog->dialog = progress;
514 dialog->progress_tv = g_date_time_new_now_local();
515 dialog->folder_tv = g_date_time_new_now_local();
516 dialog->queue_list = NULL;
517 dialog->cur_row = 0;
519 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
521 return dialog;
524 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
526 GList *list;
528 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
529 IncSession *session = list->data;
530 Pop3Session *pop3_session = POP3_SESSION(session->session);
532 session->data = inc_dialog;
534 progress_dialog_list_set(inc_dialog->dialog,
535 -1, NULL,
536 pop3_session->ac_prefs->account_name,
537 _("Standby"));
541 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
543 progress_dialog_get_fraction(inc_dialog->dialog);
544 progress_dialog_set_label(inc_dialog->dialog, "");
545 if (inc_dialog->mainwin)
546 main_window_progress_off(inc_dialog->mainwin);
549 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
551 cm_return_if_fail(inc_dialog != NULL);
553 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
555 if (inc_dialog->mainwin)
556 main_window_progress_off(inc_dialog->mainwin);
557 progress_dialog_destroy(inc_dialog->dialog);
559 g_date_time_unref(inc_dialog->progress_tv);
560 g_date_time_unref(inc_dialog->folder_tv);
562 g_free(inc_dialog);
565 static IncSession *inc_session_new(PrefsAccount *account)
567 IncSession *session;
569 cm_return_val_if_fail(account != NULL, NULL);
571 if (account->protocol != A_POP3)
572 return NULL;
573 if (!account->recv_server || !account->userid)
574 return NULL;
576 session = g_new0(IncSession, 1);
578 session->session = pop3_session_new(account);
579 session->session->data = session;
580 POP3_SESSION(session->session)->drop_message = inc_drop_message;
581 session_set_recv_message_notify(session->session,
582 inc_recv_message, session);
583 session_set_recv_data_progressive_notify(session->session,
584 inc_recv_data_progressive,
585 session);
586 session_set_recv_data_notify(session->session,
587 inc_recv_data_finished, session);
589 return session;
592 static void inc_session_destroy(IncSession *session)
594 cm_return_if_fail(session != NULL);
596 session_destroy(session->session);
597 g_free(session);
600 static gint pop3_get_port(Pop3Session *pop3_session)
602 #ifdef USE_GNUTLS
603 return pop3_session->ac_prefs->set_popport ?
604 pop3_session->ac_prefs->popport :
605 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
606 #else
607 return pop3_session->ac_prefs->set_popport ?
608 pop3_session->ac_prefs->popport : 110;
609 #endif
612 static gint inc_start(IncProgressDialog *inc_dialog)
614 IncSession *session;
615 GList *qlist;
616 Pop3Session *pop3_session;
617 IncState inc_state;
618 gint error_num = 0;
619 gint new_msgs = 0;
620 gchar *msg;
621 gchar *fin_msg;
622 FolderItem *processing, *inbox;
623 GSList *msglist, *msglist_element;
624 gboolean cancelled = FALSE;
626 qlist = inc_dialog->queue_list;
627 while (qlist != NULL) {
628 GList *next = qlist->next;
630 session = qlist->data;
631 pop3_session = POP3_SESSION(session->session);
632 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
634 if (inc_dialog->show_dialog)
635 manage_window_focus_in
636 (inc_dialog->dialog->window,
637 NULL, NULL);
638 #ifdef USE_OAUTH2
639 if(pop3_session->ac_prefs->use_pop_auth &&
640 pop3_session->ac_prefs->pop_auth_type == POPAUTH_OAUTH2)
641 oauth2_check_passwds (pop3_session->ac_prefs);
642 #endif
644 if (password_get(pop3_session->user,
645 pop3_session->ac_prefs->recv_server,
646 "pop3", pop3_get_port(pop3_session),
647 &(pop3_session->pass))) {
648 /* NOP */;
649 } else if ((pop3_session->pass = passwd_store_get_account(
650 pop3_session->ac_prefs->account_id, PWS_ACCOUNT_RECV)) == NULL) {
651 gchar *pass;
653 pass = input_dialog_query_password_keep
654 (pop3_session->ac_prefs->recv_server,
655 pop3_session->user,
656 &(pop3_session->ac_prefs->session_passwd));
658 if (pass) {
659 pop3_session->pass = pass;
663 if (inc_dialog->show_dialog)
664 manage_window_focus_out
665 (inc_dialog->dialog->window,
666 NULL, NULL);
668 qlist = next;
671 #define SET_PIXMAP_AND_TEXT(pix, str) \
673 progress_dialog_list_set(inc_dialog->dialog, \
674 inc_dialog->cur_row, \
675 pix, \
676 NULL, \
677 str); \
680 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
681 session = inc_dialog->queue_list->data;
682 pop3_session = POP3_SESSION(session->session);
683 GSList *filtered, *unfiltered;
685 if (pop3_session->pass == NULL) {
686 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
687 inc_dialog->queue_list =
688 g_list_remove(inc_dialog->queue_list, session);
689 inc_session_destroy(session);
690 continue;
693 inc_progress_dialog_clear(inc_dialog);
694 progress_dialog_scroll_to_row(inc_dialog->dialog,
695 inc_dialog->cur_row);
697 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
699 /* begin POP3 session */
700 inc_state = inc_pop3_session_do(session);
702 switch (inc_state) {
703 case INC_SUCCESS:
704 if (pop3_session->cur_total_num > 0)
705 msg = g_strdup_printf(
706 ngettext("Done (%d message (%s) received)",
707 "Done (%d messages (%s) received)",
708 pop3_session->cur_total_num),
709 pop3_session->cur_total_num,
710 to_human_readable((goffset)pop3_session->cur_total_recv_bytes));
711 else
712 msg = g_strdup_printf(_("Done (no new messages)"));
713 SET_PIXMAP_AND_TEXT(okpix, msg);
714 g_free(msg);
715 break;
716 case INC_CONNECT_ERROR:
717 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
718 break;
719 case INC_AUTH_FAILED:
720 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
721 if (pop3_session->ac_prefs->session_passwd) {
722 g_free(pop3_session->ac_prefs->session_passwd);
723 pop3_session->ac_prefs->session_passwd = NULL;
725 break;
726 case INC_LOCKED:
727 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
728 break;
729 case INC_ERROR:
730 case INC_NO_SPACE:
731 case INC_IO_ERROR:
732 case INC_SOCKET_ERROR:
733 case INC_EOF:
734 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
735 break;
736 case INC_TIMEOUT:
737 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
738 break;
739 case INC_CANCEL:
740 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
741 if (!inc_dialog->show_dialog)
742 cancelled = TRUE;
743 break;
744 default:
745 break;
748 if (pop3_session->error_val == PS_AUTHFAIL) {
749 if(!prefs_common.no_recv_err_panel) {
750 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
751 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
752 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
756 /* CLAWS: perform filtering actions on dropped message */
757 /* CLAWS: get default inbox (perhaps per account) */
758 if (pop3_session->ac_prefs->inbox) {
759 /* CLAWS: get destination folder / mailbox */
760 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
761 if (!inbox)
762 inbox = folder_get_default_inbox();
763 } else
764 inbox = folder_get_default_inbox();
766 /* get list of messages in processing */
767 processing = folder_get_default_processing(pop3_session->ac_prefs->account_id);
768 folder_item_scan(processing);
769 msglist = folder_item_get_msg_list(processing);
771 /* process messages */
772 folder_item_update_freeze();
774 procmsg_msglist_filter(msglist, pop3_session->ac_prefs,
775 &filtered, &unfiltered,
776 pop3_session->ac_prefs->filter_on_recv);
778 filtering_move_and_copy_msgs(msglist);
779 if (unfiltered != NULL)
780 folder_item_move_msgs(inbox, unfiltered);
782 for(msglist_element = msglist; msglist_element != NULL;
783 msglist_element = msglist_element->next) {
784 procmsg_msginfo_free((MsgInfo**)&(msglist_element->data));
786 folder_item_update_thaw();
788 g_slist_free(msglist);
789 g_slist_free(filtered);
790 g_slist_free(unfiltered);
792 statusbar_pop_all();
794 new_msgs += pop3_session->cur_total_num;
796 pop3_write_uidl_list(pop3_session);
798 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
799 error_num++;
800 if (inc_dialog->show_dialog)
801 manage_window_focus_in
802 (inc_dialog->dialog->window,
803 NULL, NULL);
804 inc_put_error(inc_state, pop3_session);
805 if (inc_dialog->show_dialog)
806 manage_window_focus_out
807 (inc_dialog->dialog->window,
808 NULL, NULL);
809 if (inc_state == INC_NO_SPACE ||
810 inc_state == INC_IO_ERROR)
811 break;
813 folder_item_free_cache(processing, TRUE);
815 inc_session_destroy(session);
816 inc_dialog->queue_list =
817 g_list_remove(inc_dialog->queue_list, session);
820 #undef SET_PIXMAP_AND_TEXT
822 if (new_msgs > 0)
823 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
824 "Finished (%d new messages)",
825 new_msgs), new_msgs);
826 else
827 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
829 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
831 while (inc_dialog->queue_list != NULL) {
832 session = inc_dialog->queue_list->data;
833 inc_session_destroy(session);
834 inc_dialog->queue_list =
835 g_list_remove(inc_dialog->queue_list, session);
838 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
839 inc_progress_dialog_destroy(inc_dialog);
840 else {
841 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
842 fin_msg);
843 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
844 _("_Close"));
845 gtk_button_set_image(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
846 gtk_image_new_from_icon_name("window-close", GTK_ICON_SIZE_BUTTON));
849 g_free(fin_msg);
851 return new_msgs;
854 static IncState inc_pop3_session_do(IncSession *session)
856 Pop3Session *pop3_session = POP3_SESSION(session->session);
857 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
858 PrefsAccount *ac = pop3_session->ac_prefs;
859 gchar *server;
860 gchar *account_name;
861 gushort port;
862 gchar *buf;
863 ProxyInfo *proxy_info = NULL;
865 debug_print("getting new messages of account %s...\n",
866 ac->account_name);
868 ac->last_pop_login_time = time(NULL);
870 buf = g_strdup_printf(_("%s: Retrieving new messages"),
871 ac->recv_server);
872 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
873 g_free(buf);
875 server = ac->recv_server;
876 account_name = ac->account_name;
877 port = pop3_get_port(pop3_session);
879 #ifdef USE_GNUTLS
880 SESSION(pop3_session)->ssl_type = ac->ssl_pop;
881 if (ac->ssl_pop != SSL_NONE)
882 SESSION(pop3_session)->nonblocking =
883 ac->use_nonblocking_ssl;
884 #else
885 if (ac->ssl_pop != SSL_NONE) {
886 if (alertpanel_full(_("Insecure connection"),
887 _("This connection is configured to be secured "
888 "using TLS, but TLS is not available "
889 "in this build of Claws Mail. \n\n"
890 "Do you want to continue connecting to this "
891 "server? The communication would not be "
892 "secure."),
893 NULL, _("_Cancel"), NULL, _("Con_tinue connecting"), NULL, NULL,
894 ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING) != G_ALERTALTERNATE)
895 return INC_CANCEL;
897 #endif
899 buf = g_strdup_printf(_("Account '%s': Connecting to POP3 server: %s:%d..."),
900 account_name, server, port);
901 statusbar_print_all("%s", buf);
902 log_message(LOG_PROTOCOL, "%s\n", buf);
904 progress_dialog_set_label(inc_dialog->dialog, buf);
906 if (ac->use_proxy) {
907 if (ac->use_default_proxy) {
908 proxy_info = (ProxyInfo *)&(prefs_common.proxy_info);
909 if (proxy_info->use_proxy_auth)
910 proxy_info->proxy_pass = passwd_store_get(PWS_CORE, PWS_CORE_PROXY,
911 PWS_CORE_PROXY_PASS);
912 } else {
913 proxy_info = (ProxyInfo *)&(ac->proxy_info);
914 if (proxy_info->use_proxy_auth)
915 proxy_info->proxy_pass = passwd_store_get_account(ac->account_id,
916 PWS_ACCOUNT_PROXY_PASS);
919 SESSION(pop3_session)->proxy_info = proxy_info;
921 GTK_EVENTS_FLUSH();
922 g_free(buf);
924 session_set_timeout(SESSION(pop3_session),
925 prefs_common.io_timeout_secs * 1000);
927 if (session_connect(SESSION(pop3_session), server, port) < 0) {
928 if(!prefs_common.no_recv_err_panel) {
929 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
930 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
931 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
933 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
934 server, port);
935 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
936 } else {
937 log_error(LOG_PROTOCOL, _("Can't connect to POP3 server: %s:%d\n"),
938 server, port);
940 session->inc_state = INC_CONNECT_ERROR;
941 statusbar_pop_all();
942 return INC_CONNECT_ERROR;
945 while (session_is_running(SESSION(pop3_session)) &&
946 session->inc_state != INC_CANCEL)
947 gtk_main_iteration();
949 if (session->inc_state == INC_SUCCESS) {
950 switch (pop3_session->error_val) {
951 case PS_SUCCESS:
952 switch (SESSION(pop3_session)->state) {
953 case SESSION_ERROR:
954 if (pop3_session->state == POP3_READY)
955 session->inc_state = INC_CONNECT_ERROR;
956 else
957 session->inc_state = INC_ERROR;
958 break;
959 case SESSION_EOF:
960 session->inc_state = INC_EOF;
961 break;
962 case SESSION_TIMEOUT:
963 session->inc_state = INC_TIMEOUT;
964 break;
965 default:
966 session->inc_state = INC_SUCCESS;
967 break;
969 break;
970 case PS_AUTHFAIL:
971 session->inc_state = INC_AUTH_FAILED;
972 break;
973 case PS_IOERR:
974 session->inc_state = INC_IO_ERROR;
975 break;
976 case PS_SOCKET:
977 session->inc_state = INC_SOCKET_ERROR;
978 break;
979 case PS_LOCKBUSY:
980 session->inc_state = INC_LOCKED;
981 break;
982 default:
983 session->inc_state = INC_ERROR;
984 break;
988 session_disconnect(SESSION(pop3_session));
989 statusbar_pop_all();
991 return session->inc_state;
994 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
995 IncSession *inc_session)
997 inc_progress_dialog_set_label(inc_dialog, inc_session);
998 inc_progress_dialog_set_progress(inc_dialog, inc_session);
1001 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
1002 IncSession *inc_session)
1004 ProgressDialog *dialog = inc_dialog->dialog;
1005 Pop3Session *session;
1007 cm_return_if_fail(inc_session != NULL);
1009 session = POP3_SESSION(inc_session->session);
1011 switch (session->state) {
1012 case POP3_GREETING:
1013 break;
1014 case POP3_GETAUTH_USER:
1015 case POP3_GETAUTH_PASS:
1016 case POP3_GETAUTH_APOP:
1017 progress_dialog_set_label(dialog, _("Authenticating..."));
1018 statusbar_pop_all();
1019 statusbar_print_all(_("Retrieving messages from %s (%s)..."),
1020 SESSION(session)->server,
1021 session->ac_prefs->account_name);
1022 break;
1023 case POP3_GETRANGE_STAT:
1024 progress_dialog_set_label
1025 (dialog, _("Getting the number of new messages (STAT)..."));
1026 break;
1027 case POP3_GETRANGE_LAST:
1028 progress_dialog_set_label
1029 (dialog, _("Getting the number of new messages (LAST)..."));
1030 break;
1031 case POP3_GETRANGE_UIDL:
1032 progress_dialog_set_label
1033 (dialog, _("Getting the number of new messages (UIDL)..."));
1034 break;
1035 case POP3_GETSIZE_LIST:
1036 progress_dialog_set_label
1037 (dialog, _("Getting the size of messages (LIST)..."));
1038 break;
1039 case POP3_RETR:
1040 case POP3_RETR_RECV:
1041 case POP3_DELETE:
1042 break;
1043 case POP3_LOGOUT:
1044 progress_dialog_set_label(dialog, _("Quitting"));
1045 break;
1046 default:
1047 break;
1051 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
1052 IncSession *inc_session)
1054 gchar buf[MESSAGEBUFSIZE];
1055 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
1056 gchar *total_size_str;
1057 gint cur_total;
1058 gint total;
1060 if (!pop3_session->new_msg_exist) return;
1062 cur_total = inc_session->cur_total_bytes;
1063 total = pop3_session->total_bytes;
1064 if (pop3_session->state == POP3_RETR ||
1065 pop3_session->state == POP3_RETR_RECV ||
1066 pop3_session->state == POP3_DELETE) {
1067 Xstrdup_a(total_size_str, to_human_readable((goffset)total), return);
1068 g_snprintf(buf, sizeof(buf),
1069 _("Retrieving message (%d / %d) (%s / %s)"),
1070 pop3_session->cur_msg, pop3_session->count,
1071 to_human_readable((goffset)cur_total), total_size_str);
1072 progress_dialog_set_label(inc_dialog->dialog, buf);
1075 progress_dialog_set_fraction
1076 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
1078 statusbar_progress_all(pop3_session->cur_msg, pop3_session->count, 1);
1080 if (pop3_session->cur_total_num > 0) {
1081 g_snprintf(buf, sizeof(buf),
1082 ngettext("Retrieving (%d message (%s) received)",
1083 "Retrieving (%d messages (%s) received)",
1084 pop3_session->cur_total_num),
1085 pop3_session->cur_total_num,
1086 to_human_readable
1087 ((goffset)pop3_session->cur_total_recv_bytes));
1088 progress_dialog_list_set_status(inc_dialog->dialog,
1089 inc_dialog->cur_row,
1090 buf);
1094 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
1095 IncSession *inc_session)
1097 GDateTime *tv_cur = g_date_time_new_now_local();
1098 GTimeSpan tv_result;
1100 tv_result = g_date_time_difference(tv_cur, inc_dialog->progress_tv);
1101 g_date_time_unref(tv_cur);
1102 if (tv_result < 0) {
1103 tv_result += G_USEC_PER_SEC;
1106 if (tv_result > PROGRESS_UPDATE_INTERVAL) {
1107 inc_progress_dialog_update(inc_dialog, inc_session);
1108 tv_cur = g_date_time_add(inc_dialog->progress_tv, tv_result);
1109 g_date_time_unref(inc_dialog->progress_tv);
1110 inc_dialog->progress_tv = tv_cur;
1114 static gint inc_recv_data_progressive(Session *session, guint cur_len,
1115 guint total_len, gpointer data)
1117 IncSession *inc_session = (IncSession *)data;
1118 Pop3Session *pop3_session = POP3_SESSION(session);
1119 IncProgressDialog *inc_dialog;
1120 gint cur_total;
1122 cm_return_val_if_fail(inc_session != NULL, -1);
1124 if (pop3_session->state != POP3_RETR &&
1125 pop3_session->state != POP3_RETR_RECV &&
1126 pop3_session->state != POP3_DELETE &&
1127 pop3_session->state != POP3_LOGOUT) return 0;
1129 if (!pop3_session->new_msg_exist) return 0;
1131 cur_total = pop3_session->cur_total_bytes + cur_len;
1132 if (cur_total > pop3_session->total_bytes)
1133 cur_total = pop3_session->total_bytes;
1134 inc_session->cur_total_bytes = cur_total;
1136 inc_dialog = (IncProgressDialog *)inc_session->data;
1137 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1139 return 0;
1142 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1144 IncSession *inc_session = (IncSession *)data;
1145 IncProgressDialog *inc_dialog;
1147 cm_return_val_if_fail(inc_session != NULL, -1);
1149 inc_dialog = (IncProgressDialog *)inc_session->data;
1151 inc_recv_data_progressive(session, 0, 0, inc_session);
1153 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1154 inc_progress_dialog_update(inc_dialog, inc_session);
1157 return 0;
1160 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1162 IncSession *inc_session = (IncSession *)data;
1163 IncProgressDialog *inc_dialog;
1165 cm_return_val_if_fail(inc_session != NULL, -1);
1167 inc_dialog = (IncProgressDialog *)inc_session->data;
1169 switch (POP3_SESSION(session)->state) {
1170 case POP3_GETAUTH_USER:
1171 case POP3_GETAUTH_PASS:
1172 case POP3_GETAUTH_APOP:
1173 case POP3_GETRANGE_STAT:
1174 case POP3_GETRANGE_LAST:
1175 case POP3_GETRANGE_UIDL:
1176 case POP3_GETSIZE_LIST:
1177 inc_progress_dialog_update(inc_dialog, inc_session);
1178 break;
1179 case POP3_RETR:
1180 inc_recv_data_progressive(session, 0, 0, inc_session);
1181 break;
1182 case POP3_LOGOUT:
1183 inc_progress_dialog_update(inc_dialog, inc_session);
1184 break;
1185 default:
1186 break;
1189 return 0;
1192 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1194 FolderItem *inbox;
1195 FolderItem *dropfolder;
1196 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1197 gint msgnum;
1199 cm_return_val_if_fail(inc_session != NULL, -1);
1201 if (session->ac_prefs->inbox) {
1202 inbox = folder_find_item_from_identifier
1203 (session->ac_prefs->inbox);
1204 if (!inbox)
1205 inbox = folder_get_default_inbox();
1206 } else
1207 inbox = folder_get_default_inbox();
1208 if (!inbox) {
1209 claws_unlink(file);
1210 return -1;
1213 /* CLAWS: claws uses a global .processing folder for the filtering. */
1214 dropfolder = folder_get_default_processing(session->ac_prefs->account_id);
1216 /* add msg file to drop folder */
1217 if ((msgnum = folder_item_add_msg(
1218 dropfolder, file, NULL, TRUE)) < 0) {
1219 claws_unlink(file);
1220 return -1;
1223 return 0;
1226 static void inc_put_error(IncState istate, Pop3Session *session)
1228 gchar *log_msg = NULL;
1229 gchar *err_msg = NULL;
1230 gboolean fatal_error = FALSE;
1232 switch (istate) {
1233 case INC_CONNECT_ERROR:
1234 fatal_error = TRUE;
1235 if (prefs_common.no_recv_err_panel)
1236 break;
1237 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1238 SESSION(session)->server,
1239 SESSION(session)->port);
1240 break;
1241 case INC_ERROR:
1242 log_msg = _("Error occurred while processing mail.");
1243 fatal_error = TRUE;
1244 if (prefs_common.no_recv_err_panel)
1245 break;
1246 if (session->error_msg)
1247 err_msg = g_strdup_printf
1248 (_("Error occurred while processing mail:\n%s"),
1249 session->error_msg);
1250 else
1251 err_msg = g_strdup(log_msg);
1252 break;
1253 case INC_NO_SPACE:
1254 log_msg = _("No disk space left.");
1255 err_msg = g_strdup(log_msg);
1256 fatal_error = TRUE;
1257 break;
1258 case INC_IO_ERROR:
1259 log_msg = _("Can't write file.");
1260 err_msg = g_strdup(log_msg);
1261 fatal_error = TRUE;
1262 break;
1263 case INC_SOCKET_ERROR:
1264 log_msg = _("Socket error.");
1265 if (prefs_common.no_recv_err_panel)
1266 break;
1267 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1268 SESSION(session)->server,
1269 SESSION(session)->port);
1270 break;
1271 case INC_EOF:
1272 log_msg = _("Connection closed by the remote host.");
1273 if (prefs_common.no_recv_err_panel)
1274 break;
1275 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1276 SESSION(session)->server,
1277 SESSION(session)->port);
1278 break;
1279 case INC_LOCKED:
1280 log_msg = _("Mailbox is locked.");
1281 if (prefs_common.no_recv_err_panel)
1282 break;
1283 if (session->error_msg)
1284 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1285 session->error_msg);
1286 else
1287 err_msg = g_strdup(log_msg);
1288 break;
1289 case INC_AUTH_FAILED:
1290 log_msg = _("Authentication failed.");
1291 fatal_error = TRUE;
1292 if (prefs_common.no_recv_err_panel)
1293 break;
1294 if (session->error_msg)
1295 err_msg = g_strdup_printf
1296 (_("Authentication failed:\n%s"), session->error_msg);
1297 else
1298 err_msg = g_strdup(log_msg);
1299 break;
1300 case INC_TIMEOUT:
1301 log_msg = _("Session timed out. You may be able to "
1302 "recover by increasing the timeout value in "
1303 "Preferences/Other/Miscellaneous.");
1304 if (prefs_common.no_recv_err_panel)
1305 break;
1306 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1307 SESSION(session)->server,
1308 SESSION(session)->port);
1309 break;
1310 default:
1311 break;
1314 if (log_msg) {
1315 if (fatal_error)
1316 log_error(LOG_PROTOCOL, "%s\n", log_msg);
1317 else
1318 log_warning(LOG_PROTOCOL, "%s\n", log_msg);
1320 if (prefs_common.no_recv_err_panel && fatal_error)
1321 mainwindow_show_error();
1323 if (err_msg) {
1324 alertpanel_error_log("%s", err_msg);
1325 g_free(err_msg);
1329 static void inc_cancel(IncProgressDialog *dialog)
1331 IncSession *session;
1333 cm_return_if_fail(dialog != NULL);
1335 if (dialog->queue_list == NULL) {
1336 inc_progress_dialog_destroy(dialog);
1337 return;
1340 session = dialog->queue_list->data;
1342 session->inc_state = INC_CANCEL;
1344 log_message(LOG_PROTOCOL, _("Incorporation cancelled\n"));
1347 gboolean inc_is_active(void)
1349 return (inc_dialog_list != NULL);
1352 void inc_cancel_all(void)
1354 GList *cur;
1356 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1357 inc_cancel((IncProgressDialog *)cur->data);
1360 static void inc_showlog_cb(GtkWidget *widget, gpointer data)
1362 MainWindow *mainwin = mainwindow_get_mainwindow();
1364 log_window_show(mainwin->logwin);
1367 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1369 inc_cancel((IncProgressDialog *)data);
1372 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1373 gpointer data)
1375 IncProgressDialog *dialog = (IncProgressDialog *)data;
1377 if (dialog->queue_list == NULL)
1378 inc_progress_dialog_destroy(dialog);
1380 return TRUE;
1383 static gint inc_spool_account(PrefsAccount *account)
1385 FolderItem *inbox;
1386 gchar *mbox;
1387 gint result;
1389 if (account->local_inbox) {
1390 inbox = folder_find_item_from_identifier(account->local_inbox);
1391 if (!inbox)
1392 inbox = folder_get_default_inbox();
1393 } else
1394 inbox = folder_get_default_inbox();
1396 if (account->local_mbox) {
1397 if (is_file_exist(account->local_mbox))
1398 mbox = g_strdup(account->local_mbox);
1399 else if (is_dir_exist(account->local_mbox))
1400 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1401 g_get_user_name(), NULL);
1402 else {
1403 debug_print("%s: local mailbox not found.\n",
1404 account->local_mbox);
1405 return -1;
1407 } else {
1408 debug_print("local mailbox not set in account info.\n");
1409 return -1;
1412 result = get_spool(inbox, mbox, account);
1413 g_free(mbox);
1415 statusbar_pop_all();
1417 return result;
1420 static gint get_spool(FolderItem *dest, const gchar *mbox, PrefsAccount *account)
1422 gint msgs, size;
1423 gint lockfd;
1424 gchar tmp_mbox[MAXPATHLEN + 1];
1426 cm_return_val_if_fail(dest != NULL, -1);
1427 cm_return_val_if_fail(mbox != NULL, -1);
1428 cm_return_val_if_fail(account != NULL, -1);
1430 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1431 debug_print("%s: no messages in local mailbox.\n", mbox);
1432 return 0;
1433 } else if (size < 0)
1434 return -1;
1436 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1437 return -1;
1439 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%p",
1440 get_tmp_dir(), G_DIR_SEPARATOR, mbox);
1442 if (copy_mbox(lockfd, tmp_mbox) < 0) {
1443 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1444 return -1;
1447 debug_print("Getting new messages from %s into %s...\n",
1448 mbox, dest->path);
1450 msgs = proc_mbox(dest, tmp_mbox, account->filter_on_recv, account);
1452 claws_unlink(tmp_mbox);
1453 if (msgs >= 0) empty_mbox(mbox);
1454 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1456 return msgs;
1459 void inc_lock_real(void)
1461 inc_lock_count++;
1464 void inc_unlock_real(void)
1466 if (inc_lock_count > 0)
1467 inc_lock_count--;
1470 static guint autocheck_timer = 0;
1471 static gpointer autocheck_data = NULL;
1473 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1475 gchar *buf, *numpos, *ret_str;
1476 gssize by_read = 0, by_written = 0;
1478 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1479 *prefs_common.newmail_notify_cmd))
1480 return;
1482 buf = g_strdup(prefs_common.newmail_notify_cmd);
1483 if ((numpos = strstr(buf, "%d")) != NULL) {
1484 gchar *buf2;
1486 *numpos = '\0';
1487 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1488 g_free(buf);
1489 buf = buf2;
1492 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1493 NULL);
1494 if (ret_str) {
1495 if (by_written) {
1496 g_free(buf);
1497 buf = ret_str;
1498 } else
1499 g_free(ret_str);
1501 debug_print("executing new mail notification command: %s\n", buf);
1502 execute_command_line(buf, TRUE, NULL);
1504 g_free(buf);
1507 void inc_autocheck_timer_init(MainWindow *mainwin)
1509 autocheck_data = mainwin;
1510 inc_autocheck_timer_set();
1513 static void inc_autocheck_timer_set_interval(guint _interval)
1515 guint interval = _interval;
1517 /* Convert the interval to seconds if needed. */
1518 if (_interval % 1000 == 0)
1519 interval /= 1000;
1521 inc_autocheck_timer_remove();
1522 /* last test is to avoid re-enabling auto_check after modifying
1523 the common preferences */
1524 if (prefs_common.autochk_newmail && autocheck_data
1525 && prefs_common.work_offline == FALSE) {
1526 autocheck_timer =
1527 g_timeout_add_seconds(interval, inc_autocheck_func, autocheck_data);
1528 debug_print("added global inc timer %d at %u seconds\n",
1529 autocheck_timer, interval);
1533 void inc_autocheck_timer_set(void)
1535 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 1000);
1538 void inc_autocheck_timer_remove(void)
1540 if (autocheck_timer) {
1541 debug_print("removed global inc timer %d\n", autocheck_timer);
1542 g_source_remove(autocheck_timer);
1543 autocheck_timer = 0;
1547 static gint inc_autocheck_func(gpointer data)
1549 MainWindow *mainwin = (MainWindow *)data;
1551 if (inc_lock_count) {
1552 debug_print("global inc: autocheck is locked.\n");
1553 inc_autocheck_timer_set_interval(1000);
1554 return FALSE;
1557 inc_all_account_mail(mainwin, TRUE, FALSE, prefs_common.newmail_notify_auto);
1558 inc_autocheck_timer_set();
1560 return FALSE;
1563 static gboolean inc_account_autocheck_func(gpointer data)
1565 PrefsAccount *account = (PrefsAccount *)data;
1566 GList *list = NULL;
1568 cm_return_val_if_fail(account != NULL, FALSE);
1570 debug_print("account %d: inc_account_autocheck_func\n",
1571 account->account_id);
1573 list = g_list_append(list, account);
1574 inc_account_list_mail(mainwindow_get_mainwindow(),
1575 list, TRUE, prefs_common.newmail_notify_auto);
1576 g_list_free(list);
1578 inc_account_autocheck_timer_set_interval(account);
1580 return FALSE;
1583 void inc_account_autocheck_timer_remove(PrefsAccount *account)
1585 cm_return_if_fail(account != NULL);
1587 if (account->autocheck_timer != 0) {
1588 g_source_remove(account->autocheck_timer);
1589 debug_print("INC: account %d: removed inc timer %d\n", account->account_id,
1590 account->autocheck_timer);
1591 account->autocheck_timer = 0;
1595 void inc_account_autocheck_timer_set_interval(PrefsAccount *account)
1597 cm_return_if_fail(account != NULL);
1599 inc_account_autocheck_timer_remove(account);
1601 if (account->autochk_use_default
1602 || !account->autochk_use_custom
1603 || account->autochk_itv == 0)
1604 return;
1606 account->autocheck_timer = g_timeout_add_seconds(
1607 account->autochk_itv, inc_account_autocheck_func, account);
1608 debug_print("INC: account %d: added inc timer %d at %u seconds\n",
1609 account->account_id, account->autocheck_timer, account->autochk_itv);
1612 gboolean inc_offline_should_override(gboolean force_ask, const gchar *msg)
1614 gint length = 10; /* seconds */
1615 gint answer = G_ALERTDEFAULT;
1617 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1618 /* If no network connection is available, override is not possible */
1619 if(!networkmanager_is_online(NULL))
1620 return FALSE;
1621 #endif
1623 if (prefs_common.autochk_newmail)
1624 length = prefs_common.autochk_itv; /* seconds */
1626 if (force_ask) {
1627 inc_offline_overridden_no = (time_t)0;
1630 if (prefs_common.work_offline) {
1631 gchar *tmp = NULL;
1633 if (time(NULL) - inc_offline_overridden_yes < length * 60) /* seconds */
1634 return TRUE;
1635 else if (time(NULL) - inc_offline_overridden_no < length * 60) /* seconds */
1636 return FALSE;
1638 if (!force_ask) {
1639 gchar *unit = _("seconds");
1641 /* show the offline override time (length) using the must appropriate unit:
1642 the biggest unit possible (hours, minutes, seconds), provided that there
1643 is not inferior unit involved: 1 hour, 150 minutes, 25 minutes, 90 minutes,
1644 30 seconds, 90 seconds. */
1645 if ((length / 3600) > 0) { /* hours? */
1646 if (((length % 3600) % 60) == 0) { /* no seconds left? */
1647 if ((length % 3600) > 0) { /* minutes left? */
1648 length = length / 60;
1649 unit = ngettext("minute", "minutes", length);
1650 } else {
1651 length = length / 3600;
1652 unit = ngettext("hour", "hours", length);
1654 } /* else: seconds */
1655 } else {
1656 if ((length / 60) > 0) { /* minutes left? */
1657 if ((length % 60) == 0) {
1658 length = length / 60;
1659 unit = ngettext("minute", "minutes", length);
1661 } /* else: seconds */
1663 tmp = g_strdup_printf(
1664 _("%s%sYou're working offline. Override for %d %s?"),
1665 msg?msg:"",
1666 msg?"\n\n":"",
1667 length, unit);
1668 } else
1669 tmp = g_strdup_printf(
1670 _("%s%sYou're working offline. Override?"),
1671 msg?msg:"",
1672 msg?"\n\n":"");
1674 answer = alertpanel(_("Offline warning"),
1675 tmp,
1676 NULL, _("_No"), NULL, _("_Yes"),
1677 NULL, !force_ask? _("On_ly once"):NULL, ALERTFOCUS_SECOND);
1678 g_free(tmp);
1679 if (answer == G_ALERTALTERNATE) {
1680 inc_offline_overridden_yes = time(NULL);
1681 return TRUE;
1682 } else if (answer == G_ALERTDEFAULT) {
1683 if (!force_ask)
1684 inc_offline_overridden_no = time(NULL);
1685 return FALSE;
1686 } else {
1687 inc_reset_offline_override_timers();
1688 return TRUE;
1691 return TRUE;
1694 void inc_reset_offline_override_timers()
1696 debug_print("resetting offline override timers\n");
1697 inc_offline_overridden_yes = (time_t)0;
1698 inc_offline_overridden_no = (time_t)0;