sync with 0.4.66cvs4, and incorporated EricL's no dialog patch.
[claws.git] / src / inc.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gtk/gtkmain.h>
28 #include <gtk/gtkwindow.h>
29 #include <gtk/gtksignal.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/wait.h>
36 #include <signal.h>
37 #include <errno.h>
38
39 #include "intl.h"
40 #include "main.h"
41 #include "inc.h"
42 #include "mainwindow.h"
43 #include "folderview.h"
44 #include "summaryview.h"
45 #include "prefs_common.h"
46 #include "prefs_account.h"
47 #include "account.h"
48 #include "procmsg.h"
49 #include "socket.h"
50 #include "pop.h"
51 #include "recv.h"
52 #include "mbox.h"
53 #include "utils.h"
54 #include "gtkutils.h"
55 #include "statusbar.h"
56 #include "manage_window.h"
57 #include "progressdialog.h"
58 #include "inputdialog.h"
59 #include "alertpanel.h"
60 #include "filter.h"
61 #include "automaton.h"
62 #include "folder.h"
63
64 #include "pixmaps/continue.xpm"
65 #include "pixmaps/complete.xpm"
66 #include "pixmaps/error.xpm"
67
68 GdkPixmap *currentxpm;
69 GdkBitmap *currentxpmmask;
70 GdkPixmap *errorxpm;
71 GdkBitmap *errorxpmmask;
72 GdkPixmap *okxpm;
73 GdkBitmap *okxpmmask;
74
75 #define MSGBUFSIZE      8192
76
77 static void inc_finished                (MainWindow             *mainwin);
78 static void inc_account_mail            (PrefsAccount           *account,
79                                          MainWindow             *mainwin);
80
81 static IncProgressDialog *inc_progress_dialog_create    (void);
82 static void inc_progress_dialog_destroy (IncProgressDialog      *inc_dialog);
83
84 static IncSession *inc_session_new      (PrefsAccount           *account);
85 static void inc_session_destroy         (IncSession             *session);
86 static Pop3State *inc_pop3_state_new    (PrefsAccount           *account);
87 static void inc_pop3_state_destroy      (Pop3State              *state);
88 static void inc_start                   (IncProgressDialog      *inc_dialog);
89 static IncState inc_pop3_session_do     (IncSession             *session);
90 static gint pop3_automaton_terminate    (SockInfo               *source,
91                                          Automaton              *atm);
92
93 static GHashTable *inc_get_uidl_table   (PrefsAccount           *ac_prefs);
94 static void inc_write_uidl_list         (Pop3State              *state);
95
96 #if USE_THREADS
97 static gint connection_check_cb         (Automaton      *atm);
98 #endif
99
100 static void inc_pop3_recv_func          (SockInfo       *sock,
101                                          gint            read_bytes,
102                                          gpointer        data);
103
104 static void inc_put_error               (IncState        istate);
105
106 static void inc_cancel                  (GtkWidget      *widget,
107                                          gpointer        data);
108
109 static gint inc_spool                   (void);
110 static gint get_spool                   (FolderItem     *dest,
111                                          const gchar    *mbox);
112
113 static void inc_all_spool(void);
114
115 static gint inc_autocheck_func          (gpointer        data);
116
117 static void inc_finished(MainWindow *mainwin)
118 {
119         FolderItem *item;
120
121         if (prefs_common.open_inbox_on_inc) {
122                 item = cur_account && cur_account->inbox
123                         ? folder_find_item_from_path(cur_account->inbox)
124                         : folder_get_default_inbox();
125                 folderview_unselect(mainwin->folderview);
126                 folderview_select(mainwin->folderview, item);
127         } else {
128                 item = mainwin->summaryview->folder_item;
129                 folderview_unselect(mainwin->folderview);
130                 folderview_select(mainwin->folderview, item);
131         }
132 }
133
134 void inc_mail(MainWindow *mainwin)
135 {
136         inc_autocheck_timer_remove();
137         summary_write_cache(mainwin->summaryview);
138
139         if (prefs_common.use_extinc && prefs_common.extinc_path) {
140                 gint pid;
141
142                 /* external incorporating program */
143                 if ((pid = fork()) < 0) {
144                         perror("fork");
145                         inc_autocheck_timer_set();                      
146                         return;
147                 }
148
149                 if (pid == 0) {
150                         execlp(prefs_common.extinc_path,
151                                g_basename(prefs_common.extinc_path),
152                                NULL);
153
154                         /* this will be called when failed */
155                         perror("exec");
156                         _exit(1);
157                 }
158
159                 /* wait until child process is terminated */
160                 waitpid(pid, NULL, 0);
161
162                 if (prefs_common.inc_local) inc_spool();
163                 inc_all_spool();
164         } else {
165                 if (prefs_common.inc_local) inc_spool();
166                 inc_all_spool();
167
168                 inc_account_mail(cur_account, mainwin);
169         }
170
171         inc_finished(mainwin);
172         inc_autocheck_timer_set();      
173 }
174
175 static void inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
176 {
177         IncProgressDialog *inc_dialog;
178         IncSession *session;
179         gchar *text[3];
180
181         session = inc_session_new(account);
182         if (!session) return;
183
184         inc_dialog = inc_progress_dialog_create();
185         inc_dialog->queue_list = g_list_append(inc_dialog->queue_list, session);
186         inc_dialog->mainwin = mainwin;
187         session->data = inc_dialog;
188
189         text[0] = NULL;
190         text[1] = account->account_name;
191         text[2] = _("Standby");
192         gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
193
194         inc_start(inc_dialog);
195 }
196
197 void inc_all_account_mail(MainWindow *mainwin)
198 {
199         GList *list, *queue_list = NULL;
200         IncProgressDialog *inc_dialog;
201
202         inc_autocheck_timer_remove();
203         summary_write_cache(mainwin->summaryview);
204
205         if (prefs_common.inc_local) inc_spool();
206
207         list = account_get_list();
208         if (!list) {
209                 inc_autocheck_timer_set();
210                 return;
211         }               
212
213         for (; list != NULL; list = list->next) {
214                 IncSession *session;
215                 PrefsAccount *account = list->data;
216
217                 if (account->recv_at_getall) {
218                         session = inc_session_new(account);
219                         if (session)
220                                 queue_list = g_list_append(queue_list, session);
221                 }
222         }
223
224         if (!queue_list) {
225                 inc_autocheck_timer_set();
226                 return;
227         }       
228
229         inc_dialog = inc_progress_dialog_create();
230         inc_dialog->queue_list = queue_list;
231         inc_dialog->mainwin = mainwin;
232         for (list = queue_list; list != NULL; list = list->next) {
233                 IncSession *session = list->data;
234                 gchar *text[3];
235
236                 session->data = inc_dialog;
237
238                 text[0] = NULL;
239                 text[1] = session->pop3_state->ac_prefs->account_name;
240                 text[2] = _("Standby");
241                 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
242         }
243
244         inc_start(inc_dialog);
245
246         inc_finished(mainwin);
247         inc_autocheck_timer_set();
248 }
249
250 static IncProgressDialog *inc_progress_dialog_create(void)
251 {
252         IncProgressDialog *dialog;
253         ProgressDialog *progress;
254
255         dialog = g_new0(IncProgressDialog, 1);
256
257         progress = progress_dialog_create();
258         gtk_window_set_title(GTK_WINDOW(progress->window),
259                              _("Retrieving new messages"));
260         gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
261                            GTK_SIGNAL_FUNC(inc_cancel), dialog);
262         gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
263                            GTK_SIGNAL_FUNC(gtk_true), NULL);
264         manage_window_set_transient(GTK_WINDOW(progress->window));
265
266
267         progress_dialog_set_value(progress, 0.0);
268
269         if (prefs_common.show_retrieve_dialog)
270         {
271           gtk_widget_show(progress->window);
272
273           PIXMAP_CREATE(progress->clist, okxpm, okxpmmask, complete_xpm);
274           PIXMAP_CREATE(progress->clist,
275                       currentxpm, currentxpmmask, continue_xpm);
276           PIXMAP_CREATE(progress->clist, errorxpm, errorxpmmask, error_xpm);
277
278           gtk_widget_show_now(progress->window);
279         }
280
281         dialog->dialog = progress;
282         dialog->queue_list = NULL;
283
284         return dialog;
285 }
286
287 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
288 {
289         progress_dialog_set_value(inc_dialog->dialog, 0.0);
290         progress_dialog_set_label(inc_dialog->dialog, "");
291 }
292
293 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
294 {
295         g_return_if_fail(inc_dialog != NULL);
296
297         progress_dialog_destroy(inc_dialog->dialog);
298
299         g_free(inc_dialog);
300 }
301
302 static IncSession *inc_session_new(PrefsAccount *account)
303 {
304         IncSession *session;
305
306         g_return_val_if_fail(account != NULL, NULL);
307
308         if (account->protocol != A_POP3 && account->protocol != A_APOP)
309                 return NULL;
310         if (!account->recv_server || !account->userid)
311                 return NULL;
312
313         session = g_new0(IncSession, 1);
314         session->pop3_state = inc_pop3_state_new(account);
315         session->pop3_state->session = session;
316
317         return session;
318 }
319
320 static void inc_session_destroy(IncSession *session)
321 {
322         g_return_if_fail(session != NULL);
323
324         inc_pop3_state_destroy(session->pop3_state);
325         g_free(session);
326 }
327
328 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
329 {
330         Pop3State *state;
331
332         state = g_new0(Pop3State, 1);
333
334         state->ac_prefs = account;
335         state->folder_table = g_hash_table_new(NULL, NULL);
336         state->id_table = inc_get_uidl_table(account);
337         state->id_list = NULL;
338         state->new_id_list = NULL;
339         state->inc_state = INC_SUCCESS;
340
341         return state;
342 }
343
344 static void inc_pop3_state_destroy(Pop3State *state)
345 {
346         g_hash_table_destroy(state->folder_table);
347         g_free(state->sizes);
348
349         if (state->id_table) {
350                 hash_free_strings(state->id_table);
351                 g_hash_table_destroy(state->id_table);
352         }
353         slist_free_strings(state->id_list);
354         slist_free_strings(state->new_id_list);
355         g_slist_free(state->id_list);
356         g_slist_free(state->new_id_list);
357
358         g_free(state->greeting);
359         g_free(state->user);
360         g_free(state->pass);
361         g_free(state->prev_folder);
362
363         g_free(state);
364 }
365
366 static void inc_start(IncProgressDialog *inc_dialog)
367 {
368         IncSession *session;
369         GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
370         Pop3State *pop3_state;
371         IncState inc_state;
372         gint num = 0;
373
374         while (inc_dialog->queue_list != NULL) {
375                 session = inc_dialog->queue_list->data;
376                 pop3_state = session->pop3_state;
377
378                 inc_progress_dialog_clear(inc_dialog);
379
380                 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
381                 if (pop3_state->ac_prefs->passwd)
382                         pop3_state->pass =
383                                 g_strdup(pop3_state->ac_prefs->passwd);
384                 else if (pop3_state->ac_prefs->tmp_pass)
385                         pop3_state->pass =
386                                 g_strdup(pop3_state->ac_prefs->tmp_pass);
387                 else {
388                         gchar *pass;
389                         gchar *message;
390
391                         message = g_strdup_printf
392                                 (_("Input password for %s on %s:"),
393                                  pop3_state->user,
394                                  pop3_state->ac_prefs->recv_server);
395
396                         pass = input_dialog_with_invisible(_("Input password"),
397                                                            message, NULL);
398                         g_free(message);
399                         manage_window_focus_in(inc_dialog->mainwin->window,
400                                                NULL, NULL);
401                         if (pass) {
402                                 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
403                                 pop3_state->pass = pass;
404                         } else {
405                                 inc_session_destroy(session);
406                                 inc_dialog->queue_list = g_list_remove
407                                         (inc_dialog->queue_list, session);
408                                 continue;
409                         }
410                 }
411
412                 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
413                 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
414
415                 /* begin POP3 session */
416                 inc_state = inc_pop3_session_do(session);
417
418                 if (inc_state == INC_SUCCESS || inc_state == INC_CANCEL) {
419                         gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
420                         gtk_clist_set_text(clist, num, 2, _("Done"));
421                 } else {
422                         gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
423                         gtk_clist_set_text(clist, num, 2, _("Error"));
424                 }
425
426                 if (pop3_state->error_val == PS_AUTHFAIL) {
427                         manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
428                         alertpanel_error
429                                 (_("Authorization for %s on %s failed"),
430                                  pop3_state->user,
431                                  pop3_state->ac_prefs->recv_server);
432                 }
433
434                 statusbar_pop_all();
435                 manage_window_focus_in(inc_dialog->mainwin->window, NULL, NULL);
436
437                 folder_item_scan_foreach(pop3_state->folder_table);
438                 folderview_update_item_foreach(pop3_state->folder_table);
439
440                 if (pop3_state->error_val == PS_AUTHFAIL &&
441                     pop3_state->ac_prefs->tmp_pass) {
442                         g_free(pop3_state->ac_prefs->tmp_pass);
443                         pop3_state->ac_prefs->tmp_pass = NULL;
444                 }
445
446                 inc_write_uidl_list(pop3_state);
447
448                 if (inc_state != INC_SUCCESS) {
449                         inc_put_error(inc_state);
450                         break;
451                 }
452
453                 inc_session_destroy(session);
454                 inc_dialog->queue_list =
455                         g_list_remove(inc_dialog->queue_list, session);
456
457                 num++;
458         }
459
460         while (inc_dialog->queue_list != NULL) {
461                 session = inc_dialog->queue_list->data;
462                 inc_session_destroy(session);
463                 inc_dialog->queue_list =
464                         g_list_remove(inc_dialog->queue_list, session);
465         }
466
467         inc_progress_dialog_destroy(inc_dialog);
468 }
469
470
471 static IncState inc_pop3_session_do(IncSession *session)
472 {
473         Pop3State *pop3_state = session->pop3_state;
474         IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
475         Automaton *atm;
476         SockInfo *sockinfo;
477         gint i;
478         gchar *server;
479         gushort port;
480         gchar *buf;
481         static AtmHandler handlers[] = {
482                 pop3_greeting_recv      ,
483                 pop3_getauth_user_send  , pop3_getauth_user_recv,
484                 pop3_getauth_pass_send  , pop3_getauth_pass_recv,
485                 pop3_getauth_apop_send  , pop3_getauth_apop_recv,
486                 pop3_getrange_stat_send , pop3_getrange_stat_recv,
487                 pop3_getrange_last_send , pop3_getrange_last_recv,
488                 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
489                 pop3_getsize_list_send  , pop3_getsize_list_recv,
490                 pop3_retr_send          , pop3_retr_recv,
491                 pop3_delete_send        , pop3_delete_recv,
492                 pop3_logout_send        , pop3_logout_recv
493         };
494
495         debug_print(_("getting new messages of account %s...\n"),
496                     pop3_state->ac_prefs->account_name);
497
498         atm = automaton_create(N_POP3_PHASE);
499
500         session->atm = atm;
501         atm->data = pop3_state;
502
503         buf = g_strdup_printf(_("%s: Retrieving new messages"),
504                               pop3_state->ac_prefs->recv_server);
505         gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
506         g_free(buf);
507
508         for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
509                 atm->state[i].handler = handlers[i];
510         atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
511         for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
512                 atm->state[i++].condition = GDK_INPUT_WRITE;
513                 atm->state[i++].condition = GDK_INPUT_READ;
514         }
515
516         atm->terminate = (AtmHandler)pop3_automaton_terminate;
517
518         atm->num = POP3_GREETING_RECV;
519
520         server = pop3_state->ac_prefs->recv_server;
521         port = pop3_state->ac_prefs->set_popport ?
522                 pop3_state->ac_prefs->popport : 110;
523
524         buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
525         log_message("%s\n", buf);
526         progress_dialog_set_label(inc_dialog->dialog, buf);
527         g_free(buf);
528         GTK_EVENTS_FLUSH();
529
530 #if USE_THREADS
531         if ((sockinfo = sock_connect_with_thread(server, port)) == NULL) {
532 #else
533         if ((sockinfo = sock_connect(server, port)) == NULL) {
534 #endif
535                 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
536                             server, port);
537                 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
538                 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
539                                  server, port);
540                 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
541                 pop3_automaton_terminate(NULL, atm);
542                 automaton_destroy(atm);
543
544                 return INC_ERROR;
545         }
546
547         /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
548          * to the sock structure - implement a reference counter?? */
549         pop3_state->sockinfo = sockinfo;
550         atm->help_sock = sockinfo;
551
552         recv_set_ui_func(inc_pop3_recv_func, session);
553
554 #if USE_THREADS
555         atm->timeout_tag = gtk_timeout_add
556                 (TIMEOUT_ITV, (GtkFunction)connection_check_cb, atm);
557 #else
558         atm->tag = sock_gdk_input_add(sockinfo,
559                                       atm->state[atm->num].condition,
560                                       automaton_input_cb, atm);
561 #endif
562
563         gtk_main();
564
565         recv_set_ui_func(NULL, NULL);
566
567 #if USE_THREADS
568         //pthread_join(sockinfo->connect_thr, NULL);
569 #endif
570         automaton_destroy(atm);
571
572         return pop3_state->inc_state;
573 }
574
575 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
576 {
577         if (atm->tag > 0) {
578                 gdk_input_remove(atm->tag);
579                 atm->tag = 0;
580         }
581         if (atm->timeout_tag > 0) {
582                 gtk_timeout_remove(atm->timeout_tag);
583                 atm->timeout_tag = 0;
584         }
585         if (source) {
586                 sock_close(source);
587                 gtk_main_quit();
588         }
589
590         atm->terminated = TRUE;
591
592         return 0;
593 }
594
595 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
596 {
597         GHashTable *table;
598         gchar *path;
599         FILE *fp;
600         gchar buf[IDLEN + 3];
601
602         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
603                            "uidl-", ac_prefs->recv_server,
604                            "-", ac_prefs->userid, NULL);
605         if ((fp = fopen(path, "r")) == NULL) {
606                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
607                 g_free(path);
608                 return NULL;
609         }
610         g_free(path);
611
612         table = g_hash_table_new(g_str_hash, g_str_equal);
613
614         while (fgets(buf, sizeof(buf), fp) != NULL) {
615                 strretchomp(buf);
616                 g_hash_table_insert(table, g_strdup(buf), GINT_TO_POINTER(1));
617         }
618
619         fclose(fp);
620
621         return table;
622 }
623
624 static void inc_write_uidl_list(Pop3State *state)
625 {
626         gchar *path;
627         FILE *fp;
628         GSList *cur;
629
630         if (!state->id_list) return;
631
632         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
633                            "uidl-", state->ac_prefs->recv_server,
634                            "-", state->user, NULL);
635         if ((fp = fopen(path, "w")) == NULL) {
636                 FILE_OP_ERROR(path, "fopen");
637                 g_free(path);
638                 return;
639         }
640
641         for (cur = state->id_list; cur != NULL; cur = cur->next) {
642                 if (fputs((gchar *)cur->data, fp) == EOF) {
643                         FILE_OP_ERROR(path, "fputs");
644                         break;
645                 }
646                 if (fputc('\n', fp) == EOF) {
647                         FILE_OP_ERROR(path, "fputc");
648                         break;
649                 }
650         }
651
652         if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
653         g_free(path);
654 }
655
656 #if USE_THREADS
657 static gint connection_check_cb(Automaton *atm)
658 {
659         Pop3State *state = atm->data;
660         IncProgressDialog *inc_dialog = state->session->data;
661         SockInfo *sockinfo = state->sockinfo;
662
663         //g_print("connection check\n");
664
665         if (sockinfo->state == CONN_LOOKUPFAILED ||
666             sockinfo->state == CONN_FAILED) {
667                 atm->timeout_tag = 0;
668                 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
669                             sockinfo->hostname, sockinfo->port);
670                 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
671                 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
672                                  sockinfo->hostname, sockinfo->port);
673                 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
674                 pop3_automaton_terminate(sockinfo, atm);
675                 return FALSE;
676         } else if (sockinfo->state == CONN_ESTABLISHED) {
677                 atm->timeout_tag = 0;
678                 atm->tag = sock_gdk_input_add(sockinfo,
679                                               atm->state[atm->num].condition,
680                                               automaton_input_cb, atm);
681                 return FALSE;
682         } else {
683                 return TRUE;
684         }
685 }
686 #endif
687
688 static void inc_pop3_recv_func(SockInfo *sock, gint read_bytes, gpointer data)
689 {
690         gchar buf[MSGBUFSIZE];
691         IncSession *session = (IncSession *)data;
692         Pop3State *state = session->pop3_state;
693         IncProgressDialog *inc_dialog = session->data;
694         ProgressDialog *dialog = inc_dialog->dialog;
695         gint cur_total;
696
697         cur_total = state->cur_total_bytes + read_bytes;
698         if (cur_total > state->total_bytes)
699                 cur_total = state->total_bytes;
700
701         g_snprintf(buf, sizeof(buf),
702                    _("Retrieving message (%d / %d) (%d / %d bytes)"),
703                    state->cur_msg, state->count,
704                    cur_total, state->total_bytes);
705         progress_dialog_set_label(dialog, buf);
706
707         progress_dialog_set_percentage
708                 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
709         GTK_EVENTS_FLUSH();
710 }
711
712 void inc_progress_update(Pop3State *state, Pop3Phase phase)
713 {
714         gchar buf[MSGBUFSIZE];
715         IncProgressDialog *inc_dialog = state->session->data;
716         ProgressDialog *dialog = inc_dialog->dialog;
717
718         switch (phase) {
719         case POP3_GREETING_RECV:
720                 break;
721         case POP3_GETAUTH_USER_SEND:
722         case POP3_GETAUTH_USER_RECV:
723         case POP3_GETAUTH_PASS_SEND:
724         case POP3_GETAUTH_PASS_RECV:
725         case POP3_GETAUTH_APOP_SEND:
726         case POP3_GETAUTH_APOP_RECV:
727                 progress_dialog_set_label(dialog, _("Authorizing..."));         
728                 break;
729         case POP3_GETRANGE_STAT_SEND:
730         case POP3_GETRANGE_STAT_RECV:
731                 progress_dialog_set_label
732                         (dialog, _("Getting the number of new messages (STAT)..."));
733                 break;
734         case POP3_GETRANGE_LAST_SEND:
735         case POP3_GETRANGE_LAST_RECV:
736                 progress_dialog_set_label
737                         (dialog, _("Getting the number of new messages (LAST)..."));
738                 break;
739         case POP3_GETRANGE_UIDL_SEND:
740         case POP3_GETRANGE_UIDL_RECV:
741                 progress_dialog_set_label
742                         (dialog, _("Getting the number of new messages (UIDL)..."));
743                 break;
744         case POP3_GETSIZE_LIST_SEND:
745         case POP3_GETSIZE_LIST_RECV:
746                 progress_dialog_set_label
747                         (dialog, _("Getting the size of messages (LIST)..."));
748                 break;
749         case POP3_RETR_SEND:
750         case POP3_RETR_RECV:
751                 g_snprintf(buf, sizeof(buf),
752                            _("Retrieving message (%d / %d) (%d / %d bytes)"),
753                            state->cur_msg, state->count,
754                            state->cur_total_bytes, state->total_bytes);
755                 progress_dialog_set_label(dialog, buf);
756                 progress_dialog_set_percentage
757                         (dialog,
758                          (gfloat)(state->cur_total_bytes) /
759                          (gfloat)(state->total_bytes));
760                 break;
761         case POP3_DELETE_SEND:
762         case POP3_DELETE_RECV:
763                 progress_dialog_set_label(dialog, _("Deleting message"));
764                 break;
765         case POP3_LOGOUT_SEND:
766         case POP3_LOGOUT_RECV:
767                 progress_dialog_set_label(dialog, _("Quitting"));
768                 break;
769         default:
770         }
771 }
772
773 gint inc_drop_message(const gchar *file, Pop3State *state)
774 {
775         FolderItem *inbox;
776         FolderItem *dropfolder;
777         gint val;
778
779         if (state->ac_prefs->inbox) {
780                 inbox = folder_find_item_from_path(state->ac_prefs->inbox);
781                 if (!inbox)
782                         inbox = folder_get_default_inbox();
783         } else
784                 inbox = folder_get_default_inbox();
785         if (!inbox) {
786                 unlink(file);
787                 return -1;
788         }
789
790         if (state->ac_prefs->filter_on_recv) {
791                 dropfolder =
792                         filter_get_dest_folder(prefs_common.fltlist, file);
793                 if (!dropfolder) dropfolder = inbox;
794                 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
795                         g_warning(_("a message won't be received\n"));
796                         return 1;
797                 }
798         } else
799                 dropfolder = inbox;
800
801         val = GPOINTER_TO_INT(g_hash_table_lookup
802                               (state->folder_table, dropfolder));
803         if (val == 0) {
804                 folder_item_scan(dropfolder);
805                 g_hash_table_insert(state->folder_table, dropfolder,
806                                     GINT_TO_POINTER(1));
807         }
808
809         if (folder_item_add_msg(dropfolder, file) < 0) {
810                 unlink(file);
811                 return -1;
812         }
813
814         unlink(file);
815         return 0;
816 }
817
818 static void inc_put_error(IncState istate)
819 {
820         switch (istate) {
821         case INC_ERROR:
822                 alertpanel_error(_("Error occurred while processing mail."));
823                 break;
824         case INC_NOSPACE:
825                 alertpanel_error(_("No disk space left."));
826                 break;
827         default:
828         }
829 }
830
831 static void inc_cancel(GtkWidget *widget, gpointer data)
832 {
833         IncProgressDialog *dialog = data;
834         IncSession *session = dialog->queue_list->data;
835         SockInfo *sockinfo = session->pop3_state->sockinfo;
836
837 #if USE_THREADS
838         if (sockinfo->state == CONN_READY ||
839             sockinfo->state == CONN_LOOKUPSUCCESS) {
840                 pthread_cancel(sockinfo->connect_thr);
841                 //pthread_kill(sockinfo->connect_thr, SIGINT);
842                 g_print("connection was cancelled.\n");
843         }
844 #endif
845
846         session->pop3_state->inc_state = INC_CANCEL;
847         pop3_automaton_terminate(sockinfo, session->atm);
848 }
849
850 static gint inc_spool(void)
851 {
852         gchar *mbox, *logname;
853         gint msgs;
854
855         logname = g_get_user_name();
856         mbox = g_strconcat(prefs_common.spool_path
857                            ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
858                            G_DIR_SEPARATOR_S, logname, NULL);
859         msgs = get_spool(folder_get_default_inbox(), mbox);
860         g_free(mbox);
861
862         return msgs;
863 }
864
865 static void inc_spool_account(PrefsAccount *account)
866 {
867         FolderItem *inbox;
868         FolderItem *dropfolder;
869         gint val;
870
871         if (account->inbox) {
872                 inbox = folder_find_item_from_path(account->inbox);
873                 if (!inbox)
874                         inbox = folder_get_default_inbox();
875         } else
876                 inbox = folder_get_default_inbox();
877
878         get_spool(inbox, account->local_mbox);
879 }
880
881 static void inc_all_spool(void)
882 {
883         GList *list = NULL;
884
885         list = account_get_list();
886         if (!list) return;
887
888         for (; list != NULL; list = list->next) {
889                 IncSession *session;
890                 PrefsAccount *account = list->data;
891
892                 if (account->protocol == A_LOCAL)
893                         inc_spool_account(account);
894         }
895 }
896
897 static gint get_spool(FolderItem *dest, const gchar *mbox)
898 {
899         gint msgs, size;
900         gint lockfd;
901         gchar tmp_mbox[MAXPATHLEN + 1];
902         GHashTable *folder_table = NULL;
903
904         g_return_val_if_fail(dest != NULL, -1);
905         g_return_val_if_fail(mbox != NULL, -1);
906
907         if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
908                 debug_print(_("no messages in local mailbox.\n"));
909                 return 0;
910         } else if (size < 0)
911                 return -1;
912
913         if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
914                 return -1;
915
916         g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
917                    get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
918
919         if (copy_mbox(mbox, tmp_mbox) < 0)
920                 return -1;
921
922         debug_print(_("Getting new messages from %s into %s...\n"),
923                     mbox, dest->path);
924
925         if (prefs_common.filter_on_inc)
926                 folder_table = g_hash_table_new(NULL, NULL);
927         msgs = proc_mbox(dest, tmp_mbox, folder_table);
928
929         unlink(tmp_mbox);
930         if (msgs >= 0) empty_mbox(mbox);
931         unlock_mbox(mbox, lockfd, LOCK_FLOCK);
932
933         if (folder_table) {
934                 folder_item_scan_foreach(folder_table);
935                 folderview_update_item_foreach(folder_table);
936                 g_hash_table_destroy(folder_table);
937         } else {
938                 folder_item_scan(dest);
939                 folderview_update_item(dest, FALSE);
940         }
941
942         return msgs;
943 }
944
945 static guint autocheck_timer = 0;
946 static gpointer autocheck_data = NULL;
947
948 void inc_autocheck_timer_init(MainWindow *mainwin)
949 {
950         autocheck_data = mainwin;
951         inc_autocheck_timer_set();
952 }
953
954 void inc_autocheck_timer_set(void)
955 {
956         inc_autocheck_timer_remove();
957
958         if (prefs_common.autochk_newmail && autocheck_data) {
959                 autocheck_timer = gtk_timeout_add
960                         (prefs_common.autochk_itv * 60000,
961                          inc_autocheck_func,
962                          autocheck_data);
963         }
964 }
965
966 void inc_autocheck_timer_remove(void)
967 {
968         if (autocheck_timer) {
969                 gtk_timeout_remove(autocheck_timer);
970                 autocheck_timer = 0;
971         }
972 }
973
974 static gint inc_autocheck_func(gpointer data)
975 {
976         MainWindow *mainwin = (MainWindow *)data;
977
978         inc_all_account_mail(mainwin);
979
980         return FALSE;
981 }