2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <gtk/gtkmain.h>
28 #include <gtk/gtkwindow.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtkprogressbar.h>
35 #include <sys/types.h>
44 #include "mainwindow.h"
45 #include "folderview.h"
46 #include "summaryview.h"
47 #include "prefs_common.h"
48 #include "prefs_account.h"
58 #include "statusbar.h"
59 #include "manage_window.h"
60 #include "stock_pixmap.h"
61 #include "progressdialog.h"
62 #include "inputdialog.h"
63 #include "alertpanel.h"
65 #include "filtering.h"
69 static GList *inc_dialog_list = NULL;
71 static guint inc_lock_count = 0;
73 static GdkPixmap *currentxpm;
74 static GdkBitmap *currentxpmmask;
75 static GdkPixmap *errorxpm;
76 static GdkBitmap *errorxpmmask;
77 static GdkPixmap *okxpm;
78 static GdkBitmap *okxpmmask;
80 #define MSGBUFSIZE 8192
82 static void inc_finished (MainWindow *mainwin,
83 gboolean new_messages);
84 static gint inc_account_mail (PrefsAccount *account,
87 static IncProgressDialog *inc_progress_dialog_create (void);
88 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
90 static IncSession *inc_session_new (PrefsAccount *account);
91 static void inc_session_destroy (IncSession *session);
92 static gint inc_start (IncProgressDialog *inc_dialog);
93 static IncState inc_pop3_session_do (IncSession *session);
95 static gint inc_recv_data_progressive (Session *session,
99 static gint inc_recv_data_finished (Session *session,
102 static gint inc_recv_message (Session *session,
106 static void inc_put_error (IncState istate,
109 static void inc_cancel_cb (GtkWidget *widget,
111 static gint inc_dialog_delete_cb (GtkWidget *widget,
115 static gint inc_spool (void);
116 static gint get_spool (FolderItem *dest,
119 static gint inc_spool_account(PrefsAccount *account);
120 static gint inc_all_spool(void);
121 static void inc_autocheck_timer_set_interval (guint interval);
122 static gint inc_autocheck_func (gpointer data);
124 static void inc_notify_cmd (gint new_msgs,
127 #define FOLDER_SUMMARY_MISMATCH(f, s) \
128 (f) && (s) ? ((s)->newmsgs != (f)->new_msgs) || ((f)->unread_msgs != (s)->unread) || ((f)->total_msgs != (s)->messages) \
133 * @mainwin: Main window.
134 * @new_messages: TRUE if some messages have been received.
136 * Update the folder view and the summary view after receiving
137 * messages. If @new_messages is FALSE, this function avoids unneeded
140 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
144 if (prefs_common.scan_all_after_inc)
145 folderview_check_new(NULL);
147 if (!new_messages && !prefs_common.scan_all_after_inc) return;
149 if (prefs_common.open_inbox_on_inc) {
150 item = cur_account && cur_account->inbox
151 ? folder_find_item_from_identifier(cur_account->inbox)
152 : folder_get_default_inbox();
153 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
154 folderview_unselect(mainwin->folderview);
155 folderview_select(mainwin->folderview, item);
160 void inc_mail(MainWindow *mainwin, gboolean notify)
163 gint account_new_msgs = 0;
165 if (inc_lock_count) return;
167 if (prefs_common.work_offline)
168 if (alertpanel(_("Offline warning"),
169 _("You're working offline. Override?"),
170 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
174 inc_autocheck_timer_remove();
175 main_window_lock(mainwin);
177 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
178 /* external incorporating program */
179 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
180 main_window_unlock(mainwin);
181 inc_autocheck_timer_set();
185 if (prefs_common.inc_local) {
186 account_new_msgs = inc_spool();
187 if (account_new_msgs > 0)
188 new_msgs += account_new_msgs;
191 if (prefs_common.inc_local) {
192 account_new_msgs = inc_spool();
193 if (account_new_msgs > 0)
194 new_msgs += account_new_msgs;
197 account_new_msgs = inc_account_mail(cur_account, mainwin);
198 if (account_new_msgs > 0)
199 new_msgs += account_new_msgs;
202 inc_finished(mainwin, new_msgs > 0);
203 main_window_unlock(mainwin);
204 inc_notify_cmd(new_msgs, notify);
205 inc_autocheck_timer_set();
209 void inc_pop_before_smtp(PrefsAccount *acc)
211 inc_account_mail(acc, NULL);
214 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
216 IncProgressDialog *inc_dialog;
219 FolderItem *item = NULL;
221 if(mainwin && mainwin->summaryview)
222 item = mainwin->summaryview->folder_item;
224 switch (account->protocol) {
228 * FIXME: it should return foldeview_check_new() value.
229 * TODO: do it when bug [19] is fixed (IMAP folder sets
230 * an incorrect new message count)
232 folderview_check_new(FOLDER(account->folder));
236 session = inc_session_new(account);
237 if (!session) return 0;
239 inc_dialog = inc_progress_dialog_create();
240 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
242 inc_dialog->mainwin = mainwin;
243 session->data = inc_dialog;
246 text[1] = account->account_name;
247 text[2] = _("Standby");
248 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
251 toolbar_main_set_sensitive(mainwin);
252 main_window_set_menu_sensitive(mainwin);
255 return inc_start(inc_dialog);
258 return inc_spool_account(account);
266 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
268 GList *list, *queue_list = NULL;
269 IncProgressDialog *inc_dialog;
271 gint account_new_msgs = 0;
273 if (prefs_common.work_offline)
274 if (alertpanel(_("Offline warning"),
275 _("You're working offline. Override?"),
276 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
279 if (inc_lock_count) return;
281 inc_autocheck_timer_remove();
282 main_window_lock(mainwin);
284 if (prefs_common.inc_local) {
285 account_new_msgs = inc_spool();
286 if (account_new_msgs > 0)
287 new_msgs += account_new_msgs;
290 list = account_get_list();
292 inc_finished(mainwin, new_msgs > 0);
293 main_window_unlock(mainwin);
294 inc_notify_cmd(new_msgs, notify);
295 inc_autocheck_timer_set();
299 /* check local folders */
300 account_new_msgs = inc_all_spool();
301 if (account_new_msgs > 0)
302 new_msgs += account_new_msgs;
304 /* check IMAP4 folders */
305 for (; list != NULL; list = list->next) {
306 PrefsAccount *account = list->data;
307 if ((account->protocol == A_IMAP4 ||
308 account->protocol == A_NNTP) && account->recv_at_getall) {
309 new_msgs += folderview_check_new(FOLDER(account->folder));
313 /* check POP3 accounts */
314 for (list = account_get_list(); list != NULL; list = list->next) {
316 PrefsAccount *account = list->data;
318 if (account->recv_at_getall) {
319 session = inc_session_new(account);
321 queue_list = g_list_append(queue_list, session);
326 inc_finished(mainwin, new_msgs > 0);
327 main_window_unlock(mainwin);
328 inc_notify_cmd(new_msgs, notify);
329 inc_autocheck_timer_set();
333 inc_dialog = inc_progress_dialog_create();
334 inc_dialog->queue_list = queue_list;
335 inc_dialog->mainwin = mainwin;
336 for (list = queue_list; list != NULL; list = list->next) {
337 IncSession *session = list->data;
338 Pop3Session *pop3_session = POP3_SESSION(session->session);
341 session->data = inc_dialog;
344 text[1] = pop3_session->ac_prefs->account_name;
345 text[2] = _("Standby");
346 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
349 toolbar_main_set_sensitive(mainwin);
350 main_window_set_menu_sensitive(mainwin);
352 new_msgs += inc_start(inc_dialog);
353 inc_finished(mainwin, new_msgs > 0);
354 main_window_unlock(mainwin);
355 inc_notify_cmd(new_msgs, notify);
356 inc_autocheck_timer_set();
359 static IncProgressDialog *inc_progress_dialog_create(void)
361 IncProgressDialog *dialog;
362 ProgressDialog *progress;
364 dialog = g_new0(IncProgressDialog, 1);
366 progress = progress_dialog_create();
367 gtk_window_set_title(GTK_WINDOW(progress->window),
368 _("Retrieving new messages"));
369 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
370 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
371 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
372 GTK_SIGNAL_FUNC(inc_dialog_delete_cb), dialog);
373 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
375 progress_dialog_set_value(progress, 0.0);
377 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
379 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
380 ¤txpm, ¤txpmmask);
381 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
382 &errorxpm, &errorxpmmask);
384 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
385 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
386 manage_window_get_focus_window())) {
387 dialog->show_dialog = TRUE;
388 gtk_widget_show_now(progress->window);
391 dialog->dialog = progress;
392 dialog->queue_list = NULL;
394 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
399 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
401 progress_dialog_set_value(inc_dialog->dialog, 0.0);
402 progress_dialog_set_label(inc_dialog->dialog, "");
403 if (inc_dialog->mainwin)
404 gtk_progress_bar_update
405 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
408 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
410 g_return_if_fail(inc_dialog != NULL);
412 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
413 if (inc_dialog->mainwin)
414 gtk_progress_bar_update
415 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
416 progress_dialog_destroy(inc_dialog->dialog);
421 static IncSession *inc_session_new(PrefsAccount *account)
425 g_return_val_if_fail(account != NULL, NULL);
427 if (account->protocol != A_POP3 && account->protocol != A_APOP)
429 if (!account->recv_server || !account->userid)
432 session = g_new0(IncSession, 1);
433 session->session = pop3_session_new(account);
434 session->session->data = session;
439 static void inc_session_destroy(IncSession *session)
441 g_return_if_fail(session != NULL);
443 session_destroy(session->session);
447 static gint inc_start(IncProgressDialog *inc_dialog)
450 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
451 Pop3Session *pop3_session;
458 FolderItem *processing, *inbox;
460 GSList *msglist, *msglist_element;
462 while (inc_dialog->queue_list != NULL) {
463 session = inc_dialog->queue_list->data;
464 pop3_session = POP3_SESSION(session->session);
466 inc_progress_dialog_clear(inc_dialog);
468 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
470 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
471 if (pop3_session->ac_prefs->passwd)
473 g_strdup(pop3_session->ac_prefs->passwd);
474 else if (pop3_session->ac_prefs->tmp_pass)
476 g_strdup(pop3_session->ac_prefs->tmp_pass);
480 pass = input_dialog_query_password
481 (pop3_session->ac_prefs->recv_server,
484 if (inc_dialog->mainwin && inc_dialog->show_dialog)
485 manage_window_focus_in
486 (inc_dialog->mainwin->window,
489 pop3_session->ac_prefs->tmp_pass =
491 pop3_session->pass = pass;
493 inc_session_destroy(session);
494 inc_dialog->queue_list = g_list_remove
495 (inc_dialog->queue_list, session);
500 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
501 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
503 session_set_recv_message_notify(session->session,
504 inc_recv_message, session);
505 session_set_recv_data_progressive_notify
506 (session->session, inc_recv_data_progressive, session);
507 session_set_recv_data_notify(session->session,
508 inc_recv_data_finished, session);
510 /* begin POP3 session */
511 inc_state = inc_pop3_session_do(session);
515 if (pop3_session->cur_total_num > 0)
516 msg = g_strdup_printf
517 (_("Done (%d message(s) (%s) received)"),
518 pop3_session->cur_total_num,
519 to_human_readable(pop3_session->cur_total_recv_bytes));
521 msg = g_strdup_printf(_("Done (no new messages)"));
522 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
523 gtk_clist_set_text(clist, num, 2, msg);
526 case INC_CONNECT_ERROR:
527 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
528 gtk_clist_set_text(clist, num, 2, _("Connection failed"));
530 case INC_AUTH_FAILED:
531 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
532 gtk_clist_set_text(clist, num, 2, _("Auth failed"));
535 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
536 gtk_clist_set_text(clist, num, 2, _("Locked"));
541 case INC_SOCKET_ERROR:
542 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
543 gtk_clist_set_text(clist, num, 2, _("Error"));
546 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
547 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
553 if (pop3_session->error_val == PS_AUTHFAIL) {
554 if(!prefs_common.no_recv_err_panel) {
555 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
556 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
557 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
560 (_("Authorization for %s on %s failed"),
562 pop3_session->ac_prefs->recv_server);
566 /* CLAWS: perform filtering actions on dropped message */
567 /* CLAWS: get default inbox (perhaps per account) */
568 if (pop3_session->ac_prefs->inbox) {
569 /* CLAWS: get destination folder / mailbox */
570 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
572 inbox = folder_get_default_inbox();
574 inbox = folder_get_default_inbox();
576 /* get list of messages in processing */
577 processing = folder_get_default_processing();
578 folder_item_scan(processing);
579 msglist = folder_item_get_msg_list(processing);
581 folder_item_update_freeze();
583 /* process messages */
584 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
585 msginfo = (MsgInfo *) msglist_element->data;
586 if (!pop3_session->ac_prefs->filter_on_recv || !procmsg_msginfo_filter(msginfo))
587 folder_item_move_msg(inbox, msginfo);
588 procmsg_msginfo_free(msginfo);
590 g_slist_free(msglist);
592 folder_item_update_thaw();
595 new_msgs += pop3_session->cur_total_num;
597 if (pop3_session->error_val == PS_AUTHFAIL &&
598 pop3_session->ac_prefs->tmp_pass) {
599 g_free(pop3_session->ac_prefs->tmp_pass);
600 pop3_session->ac_prefs->tmp_pass = NULL;
603 pop3_write_uidl_list(pop3_session);
605 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
607 if (inc_dialog->show_dialog)
608 manage_window_focus_in
609 (inc_dialog->dialog->window,
611 inc_put_error(inc_state, pop3_session->error_msg);
612 if (inc_dialog->show_dialog)
613 manage_window_focus_out
614 (inc_dialog->dialog->window,
616 if (inc_state == INC_NO_SPACE ||
617 inc_state == INC_IO_ERROR)
621 inc_session_destroy(session);
622 inc_dialog->queue_list =
623 g_list_remove(inc_dialog->queue_list, session);
629 fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
632 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
634 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
636 if (error_num && !prefs_common.no_recv_err_panel) {
637 if (inc_dialog->show_dialog)
638 manage_window_focus_in(inc_dialog->dialog->window,
640 alertpanel_error_log(_("Some errors occurred while getting mail."));
641 if (inc_dialog->show_dialog)
642 manage_window_focus_out(inc_dialog->dialog->window,
646 while (inc_dialog->queue_list != NULL) {
647 session = inc_dialog->queue_list->data;
648 inc_session_destroy(session);
649 inc_dialog->queue_list =
650 g_list_remove(inc_dialog->queue_list, session);
653 if (prefs_common.close_recv_dialog)
654 inc_progress_dialog_destroy(inc_dialog);
656 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
658 gtk_label_set_text(GTK_LABEL(GTK_BIN(inc_dialog->dialog->cancel_btn)->child),
667 static IncState inc_pop3_session_do(IncSession *session)
669 Pop3Session *pop3_session = POP3_SESSION(session->session);
670 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
675 debug_print("getting new messages of account %s...\n",
676 pop3_session->ac_prefs->account_name);
678 buf = g_strdup_printf(_("%s: Retrieving new messages"),
679 pop3_session->ac_prefs->recv_server);
680 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
683 server = pop3_session->ac_prefs->recv_server;
685 port = pop3_session->ac_prefs->set_popport ?
686 pop3_session->ac_prefs->popport :
687 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
688 SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
690 port = pop3_session->ac_prefs->set_popport ?
691 pop3_session->ac_prefs->popport : 110;
694 statusbar_verbosity_set(TRUE);
695 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
696 log_message("%s\n", buf);
698 progress_dialog_set_label(inc_dialog->dialog, buf);
702 statusbar_verbosity_set(FALSE);
704 if (session_connect(SESSION(pop3_session), server, port) < 0) {
705 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
707 if(!prefs_common.no_recv_err_panel) {
708 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
709 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
710 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
712 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
714 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
716 session->inc_state = INC_CONNECT_ERROR;
717 return INC_CONNECT_ERROR;
720 statusbar_verbosity_set(TRUE);
722 while (SESSION(pop3_session)->state != SESSION_DISCONNECTED &&
723 SESSION(pop3_session)->state != SESSION_ERROR)
724 gtk_main_iteration();
727 statusbar_verbosity_set(FALSE);
728 if (session->inc_state == INC_SUCCESS) {
729 switch (pop3_session->error_val) {
731 if (SESSION(pop3_session)->state == SESSION_ERROR) {
732 if (pop3_session->state == POP3_READY)
733 session->inc_state = INC_CONNECT_ERROR;
735 session->inc_state = INC_ERROR;
737 session->inc_state = INC_SUCCESS;
740 session->inc_state = INC_AUTH_FAILED;
743 session->inc_state = INC_IO_ERROR;
746 session->inc_state = INC_SOCKET_ERROR;
749 session->inc_state = INC_LOCKED;
752 session->inc_state = INC_ERROR;
757 return session->inc_state;
760 static gint inc_recv_data_progressive(Session *session, guint cur_len,
761 guint total_len, gpointer data)
763 gchar buf[MSGBUFSIZE];
764 IncSession *inc_session = (IncSession *)data;
765 Pop3Session *pop3_session = POP3_SESSION(session);
766 IncProgressDialog *inc_dialog;
767 ProgressDialog *dialog;
771 g_return_val_if_fail(inc_session != NULL, -1);
773 inc_dialog = (IncProgressDialog *)inc_session->data;
774 dialog = inc_dialog->dialog;
776 cur_total = pop3_session->cur_total_bytes + cur_len;
777 if (cur_total > pop3_session->total_bytes)
778 cur_total = pop3_session->total_bytes;
780 Xstrdup_a(total_size, to_human_readable(pop3_session->total_bytes),
782 g_snprintf(buf, sizeof(buf),
783 _("Retrieving message (%d / %d) (%s / %s)"),
784 pop3_session->cur_msg, pop3_session->count,
785 to_human_readable(cur_total), total_size);
786 progress_dialog_set_label(dialog, buf);
787 progress_dialog_set_percentage
788 (dialog, (gfloat)cur_total / (gfloat)pop3_session->total_bytes);
789 if (inc_dialog->mainwin)
790 gtk_progress_bar_update
791 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
792 (gfloat)cur_total / (gfloat)pop3_session->total_bytes);
798 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
800 IncSession *inc_session = (IncSession *)data;
802 g_return_val_if_fail(inc_session != NULL, -1);
804 inc_recv_data_progressive(session, 0, len, inc_session);
808 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
810 gchar buf[MSGBUFSIZE];
811 IncSession *inc_session = (IncSession *)data;
812 Pop3Session *pop3_session = POP3_SESSION(session);
813 IncProgressDialog *inc_dialog;
814 ProgressDialog *dialog;
816 g_return_val_if_fail(inc_session != NULL, -1);
818 inc_dialog = (IncProgressDialog *)inc_session->data;
819 dialog = inc_dialog->dialog;
821 switch (pop3_session->state) {
824 case POP3_GETAUTH_USER:
825 case POP3_GETAUTH_PASS:
826 case POP3_GETAUTH_APOP:
827 progress_dialog_set_label(dialog, _("Authenticating..."));
829 case POP3_GETRANGE_STAT:
830 progress_dialog_set_label
831 (dialog, _("Getting the number of new messages (STAT)..."));
833 case POP3_GETRANGE_LAST:
834 progress_dialog_set_label
835 (dialog, _("Getting the number of new messages (LAST)..."));
837 case POP3_GETRANGE_UIDL:
838 progress_dialog_set_label
839 (dialog, _("Getting the number of new messages (UIDL)..."));
841 case POP3_GETSIZE_LIST:
842 progress_dialog_set_label
843 (dialog, _("Getting the size of messages (LIST)..."));
846 inc_recv_data_progressive
848 pop3_session->msg[pop3_session->cur_msg].size, data);
851 if (pop3_session->msg[pop3_session->cur_msg].recv_time <
852 pop3_session->current_time) {
853 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
854 pop3_session->cur_msg);
855 progress_dialog_set_label(dialog, buf);
859 progress_dialog_set_label(dialog, _("Quitting"));
868 gint inc_drop_message(const gchar *file, Pop3Session *session)
871 FolderItem *dropfolder;
873 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
876 if (session->ac_prefs->inbox) {
877 inbox = folder_find_item_from_identifier
878 (session->ac_prefs->inbox);
880 inbox = folder_get_default_inbox();
882 inbox = folder_get_default_inbox();
888 /* CLAWS: claws uses a global .processing folder for the filtering. */
889 dropfolder = folder_get_default_processing();
891 /* add msg file to drop folder */
892 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
900 static void inc_put_error(IncState istate, const gchar *msg)
904 if (prefs_common.no_recv_err_panel)
907 alertpanel_error(_("Error occurred while processing mail:\n%s"), msg);
909 alertpanel_error(_("Error occurred while processing mail."));
912 alertpanel_error(_("No disk space left."));
915 alertpanel_error(_("Can't write file."));
917 case INC_SOCKET_ERROR:
918 if (prefs_common.no_recv_err_panel)
920 alertpanel_error(_("Socket error."));
923 if (prefs_common.no_recv_err_panel)
926 alertpanel_error(_("Mailbox is locked:\n%s"), msg);
928 alertpanel_error(_("Mailbox is locked."));
930 case INC_AUTH_FAILED:
931 if (prefs_common.no_recv_err_panel)
934 alertpanel_error(_("Authentication failed:\n%s"),
937 alertpanel_error(_("Authentication failed."));
944 static void inc_cancel(IncProgressDialog *dialog)
948 g_return_if_fail(dialog != NULL);
950 if (dialog->queue_list == NULL) {
951 inc_progress_dialog_destroy(dialog);
955 session = dialog->queue_list->data;
957 session->session->state = SESSION_DISCONNECTED;
958 session->inc_state = INC_CANCEL;
960 log_message(_("Incorporation cancelled\n"));
963 gboolean inc_is_active(void)
965 return (inc_dialog_list != NULL);
968 void inc_cancel_all(void)
972 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
973 inc_cancel((IncProgressDialog *)cur->data);
976 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
978 inc_cancel((IncProgressDialog *)data);
981 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
984 IncProgressDialog *dialog = (IncProgressDialog *)data;
986 if (dialog->queue_list == NULL)
987 inc_progress_dialog_destroy(dialog);
992 static gint inc_spool(void)
994 gchar *mbox, *logname;
997 logname = g_get_user_name();
998 mbox = g_strconcat(prefs_common.spool_path
999 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1000 G_DIR_SEPARATOR_S, logname, NULL);
1001 msgs = get_spool(folder_get_default_inbox(), mbox);
1007 static gint inc_spool_account(PrefsAccount *account)
1011 if (account->inbox) {
1012 inbox = folder_find_item_from_path(account->inbox);
1014 inbox = folder_get_default_inbox();
1016 inbox = folder_get_default_inbox();
1018 return get_spool(inbox, account->local_mbox);
1021 static gint inc_all_spool(void)
1025 gint account_new_msgs = 0;
1027 list = account_get_list();
1028 if (!list) return 0;
1030 for (; list != NULL; list = list->next) {
1031 PrefsAccount *account = list->data;
1033 if ((account->protocol == A_LOCAL) &&
1034 (account->recv_at_getall)) {
1035 account_new_msgs = inc_spool_account(account);
1036 if (account_new_msgs > 0)
1037 new_msgs += account_new_msgs;
1044 static gint get_spool(FolderItem *dest, const gchar *mbox)
1048 gchar tmp_mbox[MAXPATHLEN + 1];
1050 g_return_val_if_fail(dest != NULL, -1);
1051 g_return_val_if_fail(mbox != NULL, -1);
1053 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1054 debug_print("no messages in local mailbox.\n");
1056 } else if (size < 0)
1059 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1062 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1063 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1065 if (copy_mbox(mbox, tmp_mbox) < 0) {
1066 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1070 debug_print("Getting new messages from %s into %s...\n",
1073 msgs = proc_mbox(dest, tmp_mbox);
1076 if (msgs >= 0) empty_mbox(mbox);
1077 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1087 void inc_unlock(void)
1089 if (inc_lock_count > 0)
1093 static guint autocheck_timer = 0;
1094 static gpointer autocheck_data = NULL;
1096 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1101 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1102 *prefs_common.newmail_notify_cmd))
1104 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1105 buf[1] == 'd' && !strchr(&buf[1], '%'))
1106 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1109 buf = g_strdup(prefs_common.newmail_notify_cmd);
1111 execute_command_line(buf, TRUE);
1116 void inc_autocheck_timer_init(MainWindow *mainwin)
1118 autocheck_data = mainwin;
1119 inc_autocheck_timer_set();
1122 static void inc_autocheck_timer_set_interval(guint interval)
1124 inc_autocheck_timer_remove();
1125 /* last test is to avoid re-enabling auto_check after modifying
1126 the common preferences */
1127 if (prefs_common.autochk_newmail && autocheck_data
1128 && prefs_common.work_offline == FALSE) {
1129 autocheck_timer = gtk_timeout_add
1130 (interval, inc_autocheck_func, autocheck_data);
1131 debug_print("added timer = %d\n", autocheck_timer);
1135 void inc_autocheck_timer_set(void)
1137 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1140 void inc_autocheck_timer_remove(void)
1142 if (autocheck_timer) {
1143 debug_print("removed timer = %d\n", autocheck_timer);
1144 gtk_timeout_remove(autocheck_timer);
1145 autocheck_timer = 0;
1149 static gint inc_autocheck_func(gpointer data)
1151 MainWindow *mainwin = (MainWindow *)data;
1153 if (inc_lock_count) {
1154 debug_print("autocheck is locked.\n");
1155 inc_autocheck_timer_set_interval(1000);
1159 inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);