sync with 0.4.65cvs11
[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_len,
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
114 static void inc_finished(MainWindow *mainwin)
115 {
116         FolderItem *item;
117
118         if (prefs_common.open_inbox_on_inc) {
119                 item = cur_account && cur_account->inbox
120                         ? folder_find_item_from_path(cur_account->inbox)
121                         : folder_get_default_inbox();
122                 folderview_unselect(mainwin->folderview);
123                 folderview_select(mainwin->folderview, item);
124         } else {
125                 item = mainwin->summaryview->folder_item;
126                 folderview_unselect(mainwin->folderview);
127                 folderview_select(mainwin->folderview, item);
128         }
129 }
130
131 void inc_mail(MainWindow *mainwin)
132 {
133         summary_write_cache(mainwin->summaryview);
134
135         if (prefs_common.use_extinc && prefs_common.extinc_path) {
136                 gint pid;
137
138                 /* external incorporating program */
139                 if ((pid = fork()) < 0) {
140                         perror("fork");
141                         return;
142                 }
143
144                 if (pid == 0) {
145                         execlp(prefs_common.extinc_path,
146                                g_basename(prefs_common.extinc_path),
147                                NULL);
148
149                         /* this will be called when failed */
150                         perror("exec");
151                         _exit(1);
152                 }
153
154                 /* wait until child process is terminated */
155                 waitpid(pid, NULL, 0);
156
157                 if (prefs_common.inc_local) inc_spool();
158         } else {
159                 if (prefs_common.inc_local) inc_spool();
160
161                 inc_account_mail(cur_account, mainwin);
162         }
163
164         inc_finished(mainwin);
165 }
166
167 static void inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
168 {
169         IncProgressDialog *inc_dialog;
170         IncSession *session;
171         gchar *text[3];
172
173         session = inc_session_new(account);
174         if (!session) return;
175
176         inc_dialog = inc_progress_dialog_create();
177         inc_dialog->queue_list = g_list_append(inc_dialog->queue_list, session);
178         inc_dialog->mainwin = mainwin;
179         session->data = inc_dialog;
180
181         text[0] = NULL;
182         text[1] = account->account_name;
183         text[2] = _("Standby");
184         gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
185
186         inc_start(inc_dialog);
187 }
188
189 void inc_all_account_mail(MainWindow *mainwin)
190 {
191         GList *list, *queue_list = NULL;
192         IncProgressDialog *inc_dialog;
193
194         summary_write_cache(mainwin->summaryview);
195
196         if (prefs_common.inc_local) inc_spool();
197
198         list = account_get_list();
199         if (!list) return;
200
201         for (; list != NULL; list = list->next) {
202                 IncSession *session;
203                 PrefsAccount *account = list->data;
204
205                 if (account->recv_at_getall) {
206                         session = inc_session_new(account);
207                         if (session)
208                                 queue_list = g_list_append(queue_list, session);
209                 }
210         }
211
212         if (!queue_list) return;
213
214         inc_dialog = inc_progress_dialog_create();
215         inc_dialog->queue_list = queue_list;
216         inc_dialog->mainwin = mainwin;
217         for (list = queue_list; list != NULL; list = list->next) {
218                 IncSession *session = list->data;
219                 gchar *text[3];
220
221                 session->data = inc_dialog;
222
223                 text[0] = NULL;
224                 text[1] = session->pop3_state->ac_prefs->account_name;
225                 text[2] = _("Standby");
226                 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
227         }
228
229         inc_start(inc_dialog);
230
231         inc_finished(mainwin);
232 }
233
234 static IncProgressDialog *inc_progress_dialog_create(void)
235 {
236         IncProgressDialog *dialog;
237         ProgressDialog *progress;
238
239         dialog = g_new0(IncProgressDialog, 1);
240
241         progress = progress_dialog_create();
242         gtk_window_set_title(GTK_WINDOW(progress->window),
243                              _("Retrieving new messages"));
244         gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
245                            GTK_SIGNAL_FUNC(inc_cancel), dialog);
246         gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
247                            GTK_SIGNAL_FUNC(gtk_true), NULL);
248         manage_window_set_transient(GTK_WINDOW(progress->window));
249
250         progress_dialog_set_value(progress, 0.0);
251
252         gtk_widget_show(progress->window);
253
254         PIXMAP_CREATE(progress->clist, okxpm, okxpmmask, complete_xpm);
255         PIXMAP_CREATE(progress->clist,
256                       currentxpm, currentxpmmask, continue_xpm);
257         PIXMAP_CREATE(progress->clist, errorxpm, errorxpmmask, error_xpm);
258
259         gtk_widget_show_now(progress->window);
260
261         dialog->dialog = progress;
262         dialog->queue_list = NULL;
263
264         return dialog;
265 }
266
267 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
268 {
269         progress_dialog_set_value(inc_dialog->dialog, 0.0);
270         progress_dialog_set_label(inc_dialog->dialog, "");
271 }
272
273 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
274 {
275         g_return_if_fail(inc_dialog != NULL);
276
277         progress_dialog_destroy(inc_dialog->dialog);
278
279         g_free(inc_dialog);
280 }
281
282 static IncSession *inc_session_new(PrefsAccount *account)
283 {
284         IncSession *session;
285
286         g_return_val_if_fail(account != NULL, NULL);
287
288         if (account->protocol != A_POP3 && account->protocol != A_APOP)
289                 return NULL;
290         if (!account->recv_server || !account->userid)
291                 return NULL;
292
293         session = g_new0(IncSession, 1);
294         session->pop3_state = inc_pop3_state_new(account);
295         session->pop3_state->session = session;
296
297         return session;
298 }
299
300 static void inc_session_destroy(IncSession *session)
301 {
302         g_return_if_fail(session != NULL);
303
304         inc_pop3_state_destroy(session->pop3_state);
305         g_free(session);
306 }
307
308 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
309 {
310         Pop3State *state;
311
312         state = g_new0(Pop3State, 1);
313
314         state->ac_prefs = account;
315         state->folder_table = g_hash_table_new(NULL, NULL);
316         state->id_table = inc_get_uidl_table(account);
317         state->id_list = NULL;
318         state->new_id_list = NULL;
319         state->inc_state = INC_SUCCESS;
320
321         return state;
322 }
323
324 static void inc_pop3_state_destroy(Pop3State *state)
325 {
326         g_hash_table_destroy(state->folder_table);
327         g_free(state->sizes);
328
329         if (state->id_table) {
330                 hash_free_strings(state->id_table);
331                 g_hash_table_destroy(state->id_table);
332         }
333         slist_free_strings(state->id_list);
334         slist_free_strings(state->new_id_list);
335         g_slist_free(state->id_list);
336         g_slist_free(state->new_id_list);
337
338         g_free(state->greeting);
339         g_free(state->user);
340         g_free(state->pass);
341         g_free(state->prev_folder);
342
343         g_free(state);
344 }
345
346 static void inc_start(IncProgressDialog *inc_dialog)
347 {
348         IncSession *session;
349         GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
350         Pop3State *pop3_state;
351         IncState inc_state;
352         gint num = 0;
353
354         while (inc_dialog->queue_list != NULL) {
355                 session = inc_dialog->queue_list->data;
356                 pop3_state = session->pop3_state;
357
358                 inc_progress_dialog_clear(inc_dialog);
359
360                 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
361                 if (pop3_state->ac_prefs->passwd)
362                         pop3_state->pass =
363                                 g_strdup(pop3_state->ac_prefs->passwd);
364                 else if (pop3_state->ac_prefs->tmp_pass)
365                         pop3_state->pass =
366                                 g_strdup(pop3_state->ac_prefs->tmp_pass);
367                 else {
368                         gchar *pass;
369                         gchar *message;
370
371                         message = g_strdup_printf
372                                 (_("Input password for %s on %s:"),
373                                  pop3_state->user,
374                                  pop3_state->ac_prefs->recv_server);
375
376                         pass = input_dialog_with_invisible(_("Input password"),
377                                                            message, NULL);
378                         g_free(message);
379                         manage_window_focus_in(inc_dialog->mainwin->window,
380                                                NULL, NULL);
381                         if (pass) {
382                                 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
383                                 pop3_state->pass = pass;
384                         } else {
385                                 inc_session_destroy(session);
386                                 inc_dialog->queue_list = g_list_remove
387                                         (inc_dialog->queue_list, session);
388                                 continue;
389                         }
390                 }
391
392                 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
393                 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
394
395                 /* begin POP3 session */
396                 inc_state = inc_pop3_session_do(session);
397
398                 if (inc_state == INC_SUCCESS || inc_state == INC_CANCEL) {
399                         gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
400                         gtk_clist_set_text(clist, num, 2, _("Done"));
401                 } else {
402                         gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
403                         gtk_clist_set_text(clist, num, 2, _("Error"));
404                 }
405
406                 if (pop3_state->error_val == PS_AUTHFAIL) {
407                         manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
408                         alertpanel_error
409                                 (_("Authorization for %s on %s failed"),
410                                  pop3_state->user,
411                                  pop3_state->ac_prefs->recv_server);
412                 }
413
414                 statusbar_pop_all();
415                 manage_window_focus_in(inc_dialog->mainwin->window, NULL, NULL);
416
417                 folder_item_scan_foreach(pop3_state->folder_table);
418                 folderview_update_item_foreach(pop3_state->folder_table);
419
420                 if (pop3_state->error_val == PS_AUTHFAIL &&
421                     pop3_state->ac_prefs->tmp_pass) {
422                         g_free(pop3_state->ac_prefs->tmp_pass);
423                         pop3_state->ac_prefs->tmp_pass = NULL;
424                 }
425
426                 inc_write_uidl_list(pop3_state);
427
428                 if (inc_state != INC_SUCCESS) {
429                         inc_put_error(inc_state);
430                         break;
431                 }
432
433                 inc_session_destroy(session);
434                 inc_dialog->queue_list =
435                         g_list_remove(inc_dialog->queue_list, session);
436
437                 num++;
438         }
439
440         while (inc_dialog->queue_list != NULL) {
441                 session = inc_dialog->queue_list->data;
442                 inc_session_destroy(session);
443                 inc_dialog->queue_list =
444                         g_list_remove(inc_dialog->queue_list, session);
445         }
446
447         inc_progress_dialog_destroy(inc_dialog);
448 }
449
450
451 static IncState inc_pop3_session_do(IncSession *session)
452 {
453         Pop3State *pop3_state = session->pop3_state;
454         IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
455         Automaton *atm;
456         SockInfo *sockinfo;
457         gint i;
458         gchar *server;
459         gushort port;
460         gchar *buf;
461         static AtmHandler handlers[] = {
462                 pop3_greeting_recv      ,
463                 pop3_getauth_user_send  , pop3_getauth_user_recv,
464                 pop3_getauth_pass_send  , pop3_getauth_pass_recv,
465                 pop3_getauth_apop_send  , pop3_getauth_apop_recv,
466                 pop3_getrange_stat_send , pop3_getrange_stat_recv,
467                 pop3_getrange_last_send , pop3_getrange_last_recv,
468                 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
469                 pop3_getsize_list_send  , pop3_getsize_list_recv,
470                 pop3_retr_send          , pop3_retr_recv,
471                 pop3_delete_send        , pop3_delete_recv,
472                 pop3_logout_send        , pop3_logout_recv
473         };
474
475         debug_print(_("getting new messages of account %s...\n"),
476                     pop3_state->ac_prefs->account_name);
477
478         atm = automaton_create(N_POP3_PHASE);
479
480         session->atm = atm;
481         atm->data = pop3_state;
482
483         buf = g_strdup_printf(_("%s: Retrieving new messages"),
484                               pop3_state->ac_prefs->recv_server);
485         gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
486         g_free(buf);
487
488         for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
489                 atm->state[i].handler = handlers[i];
490         atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
491         for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
492                 atm->state[i++].condition = GDK_INPUT_WRITE;
493                 atm->state[i++].condition = GDK_INPUT_READ;
494         }
495
496         atm->terminate = (AtmHandler)pop3_automaton_terminate;
497
498         atm->num = POP3_GREETING_RECV;
499
500         server = pop3_state->ac_prefs->recv_server;
501         port = pop3_state->ac_prefs->set_popport ?
502                 pop3_state->ac_prefs->popport : 110;
503
504         buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
505         log_message("%s\n", buf);
506         progress_dialog_set_label(inc_dialog->dialog, buf);
507         g_free(buf);
508         GTK_EVENTS_FLUSH();
509
510 #if USE_THREADS
511         if ((sockinfo = sock_connect_with_thread(server, port)) == NULL) {
512 #else
513         if ((sockinfo = sock_connect_nb(server, port)) == NULL) {
514 #endif
515                 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
516                             server, port);
517                 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
518                 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
519                                  server, port);
520                 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
521                 pop3_automaton_terminate(NULL, atm);
522                 automaton_destroy(atm);
523
524                 return INC_ERROR;
525         }
526
527         /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
528          * to the sock structure - implement a reference counter?? */
529         pop3_state->sockinfo = sockinfo;
530         atm->help_sock = sockinfo;
531
532         recv_set_ui_func(inc_pop3_recv_func, session);
533
534 #if USE_THREADS
535         atm->timeout_tag = gtk_timeout_add
536                 (TIMEOUT_ITV, (GtkFunction)connection_check_cb, atm);
537 #else
538         atm->tag = sock_gdk_input_add(sockinfo,
539                                       atm->state[atm->num].condition,
540                                       automaton_input_cb, atm);
541 #endif
542
543         gtk_main();
544
545         recv_set_ui_func(NULL, NULL);
546
547 #if USE_THREADS
548         //pthread_join(sockinfo->connect_thr, NULL);
549 #endif
550         automaton_destroy(atm);
551
552         return pop3_state->inc_state;
553 }
554
555 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
556 {
557         if (atm->tag > 0) {
558                 gdk_input_remove(atm->tag);
559                 atm->tag = 0;
560         }
561         if (atm->timeout_tag > 0) {
562                 gtk_timeout_remove(atm->timeout_tag);
563                 atm->timeout_tag = 0;
564         }
565         if (source) {
566                 sock_close(source);
567                 gtk_main_quit();
568         }
569
570         atm->terminated = TRUE;
571
572         return 0;
573 }
574
575 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
576 {
577         GHashTable *table;
578         gchar *path;
579         FILE *fp;
580         gchar buf[IDLEN + 3];
581
582         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
583                            "uidl-", ac_prefs->recv_server,
584                            "-", ac_prefs->userid, NULL);
585         if ((fp = fopen(path, "r")) == NULL) {
586                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
587                 g_free(path);
588                 return NULL;
589         }
590         g_free(path);
591
592         table = g_hash_table_new(g_str_hash, g_str_equal);
593
594         while (fgets(buf, sizeof(buf), fp) != NULL) {
595                 strretchomp(buf);
596                 g_hash_table_insert(table, g_strdup(buf), GINT_TO_POINTER(1));
597         }
598
599         fclose(fp);
600
601         return table;
602 }
603
604 static void inc_write_uidl_list(Pop3State *state)
605 {
606         gchar *path;
607         FILE *fp;
608         GSList *cur;
609
610         if (!state->id_list) return;
611
612         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
613                            "uidl-", state->ac_prefs->recv_server,
614                            "-", state->user, NULL);
615         if ((fp = fopen(path, "w")) == NULL) {
616                 FILE_OP_ERROR(path, "fopen");
617                 g_free(path);
618                 return;
619         }
620
621         for (cur = state->id_list; cur != NULL; cur = cur->next) {
622                 if (fputs((gchar *)cur->data, fp) == EOF) {
623                         FILE_OP_ERROR(path, "fputs");
624                         break;
625                 }
626                 if (fputc('\n', fp) == EOF) {
627                         FILE_OP_ERROR(path, "fputc");
628                         break;
629                 }
630         }
631
632         if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
633         g_free(path);
634 }
635
636 #if USE_THREADS
637 static gint connection_check_cb(Automaton *atm)
638 {
639         Pop3State *state = atm->data;
640         IncProgressDialog *inc_dialog = state->session->data;
641         SockInfo *sockinfo = state->sockinfo;
642
643         //g_print("connection check\n");
644
645         if (sockinfo->state == CONN_LOOKUPFAILED ||
646             sockinfo->state == CONN_FAILED) {
647                 atm->timeout_tag = 0;
648                 pop3_automaton_terminate(sockinfo->sock, atm);
649                 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
650                             sockinfo->hostname, sockinfo->port);
651                 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
652                 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
653                                  sockinfo->hostname, sockinfo->port);
654                 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
655                 return FALSE;
656         } else if (sockinfo->state == CONN_ESTABLISHED) {
657                 atm->timeout_tag = 0;
658                 atm->tag = gdk_input_add(sockinfo->sock,
659                                          atm->state[atm->num].condition,
660                                          automaton_input_cb, atm);
661                 return FALSE;
662         } else {
663                 return TRUE;
664         }
665 }
666 #endif
667
668 static void inc_pop3_recv_func(SockInfo *sock, gint read_len, gpointer data)
669 {
670         IncSession *session = (IncSession *)data;
671         Pop3State *state = session->pop3_state;
672         IncProgressDialog *inc_dialog = session->data;
673         ProgressDialog *dialog = inc_dialog->dialog;
674
675         state->cur_msg_bytes += read_len;
676
677         //g_snprintf(buf, sizeof(buf),
678         //         _("Retrieving message (%d / %d)"),
679         //         state->cur_msg, state->count);
680         //progress_dialog_set_label(dialog, buf);
681
682         progress_dialog_set_percentage
683                 (dialog,
684                  (gfloat)(state->cur_total_bytes + state->cur_msg_bytes) /
685                  (gfloat)(state->total_bytes));
686         GTK_EVENTS_FLUSH();
687 }
688
689 void inc_progress_update(Pop3State *state, Pop3Phase phase)
690 {
691         gchar buf[MSGBUFSIZE];
692         IncProgressDialog *inc_dialog = state->session->data;
693         ProgressDialog *dialog = inc_dialog->dialog;
694
695         switch (phase) {
696         case POP3_GREETING_RECV:
697                 break;
698         case POP3_GETAUTH_USER_SEND:
699         case POP3_GETAUTH_USER_RECV:
700         case POP3_GETAUTH_PASS_SEND:
701         case POP3_GETAUTH_PASS_RECV:
702         case POP3_GETAUTH_APOP_SEND:
703         case POP3_GETAUTH_APOP_RECV:
704                 progress_dialog_set_label(dialog, _("Authorizing"));
705                 break;
706         case POP3_GETRANGE_STAT_SEND:
707         case POP3_GETRANGE_STAT_RECV:
708         case POP3_GETRANGE_LAST_SEND:
709         case POP3_GETRANGE_LAST_RECV:
710         case POP3_GETRANGE_UIDL_SEND:
711         case POP3_GETRANGE_UIDL_RECV:
712                 progress_dialog_set_label(dialog,
713                                           _("Getting number of new messages"));
714                 break;
715         case POP3_RETR_SEND:
716         case POP3_RETR_RECV:
717                 g_snprintf(buf, sizeof(buf),
718                            _("Retrieving message (%d / %d)"),
719                            state->cur_msg, state->count);
720                 progress_dialog_set_label(dialog, buf);
721                 //progress_dialog_set_percentage
722                 //      (dialog, (gfloat)state->cur_msg / state->count);
723                 progress_dialog_set_percentage
724                         (dialog,
725                          (gfloat)(state->cur_total_bytes) /
726                          (gfloat)(state->total_bytes));
727                 break;
728         case POP3_DELETE_SEND:
729         case POP3_DELETE_RECV:
730                 progress_dialog_set_label(dialog, _("Deleting message"));
731                 break;
732         case POP3_LOGOUT_SEND:
733         case POP3_LOGOUT_RECV:
734                 progress_dialog_set_label(dialog, _("Quitting"));
735                 break;
736         default:
737         }
738 }
739
740 gint inc_drop_message(const gchar *file, Pop3State *state)
741 {
742         FolderItem *inbox;
743         FolderItem *dropfolder;
744         gint val;
745
746         if (state->ac_prefs->inbox) {
747                 inbox = folder_find_item_from_path(state->ac_prefs->inbox);
748                 if (!inbox)
749                         inbox = folder_get_default_inbox();
750         } else
751                 inbox = folder_get_default_inbox();
752         if (!inbox) {
753                 unlink(file);
754                 return -1;
755         }
756
757         if (state->ac_prefs->filter_on_recv) {
758                 dropfolder =
759                         filter_get_dest_folder(prefs_common.fltlist, file);
760                 if (!dropfolder) dropfolder = inbox;
761                 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
762                         g_warning(_("a message won't be received\n"));
763                         return 1;
764                 }
765         } else
766                 dropfolder = inbox;
767
768         val = GPOINTER_TO_INT(g_hash_table_lookup
769                               (state->folder_table, dropfolder));
770         if (val == 0) {
771                 folder_item_scan(dropfolder);
772                 g_hash_table_insert(state->folder_table, dropfolder,
773                                     GINT_TO_POINTER(1));
774         }
775
776         if (folder_item_add_msg(dropfolder, file) < 0) {
777                 unlink(file);
778                 return -1;
779         }
780
781         unlink(file);
782         return 0;
783 }
784
785 static void inc_put_error(IncState istate)
786 {
787         switch (istate) {
788         case INC_ERROR:
789                 alertpanel_error(_("Error occurred while processing mail."));
790                 break;
791         case INC_NOSPACE:
792                 alertpanel_error(_("No disk space left."));
793                 break;
794         default:
795         }
796 }
797
798 static void inc_cancel(GtkWidget *widget, gpointer data)
799 {
800         IncProgressDialog *dialog = data;
801         IncSession *session = dialog->queue_list->data;
802         SockInfo *sockinfo = session->pop3_state->sockinfo;
803
804 #if USE_THREADS
805         if (sockinfo->state == CONN_READY ||
806             sockinfo->state == CONN_LOOKUPSUCCESS) {
807                 pthread_cancel(sockinfo->connect_thr);
808                 //pthread_kill(sockinfo->connect_thr, SIGINT);
809                 g_print("connection was cancelled.\n");
810         }
811 #endif
812
813         session->pop3_state->inc_state = INC_CANCEL;
814         pop3_automaton_terminate(sockinfo, session->atm);
815 }
816
817 static gint inc_spool(void)
818 {
819         gchar *mbox, *logname;
820         gint msgs;
821
822         logname = g_get_user_name();
823         mbox = g_strconcat(prefs_common.spool_path
824                            ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
825                            G_DIR_SEPARATOR_S, logname, NULL);
826         msgs = get_spool(folder_get_default_inbox(), mbox);
827         g_free(mbox);
828
829         return msgs;
830 }
831
832 static gint get_spool(FolderItem *dest, const gchar *mbox)
833 {
834         gint msgs, size;
835         gint lockfd;
836         gchar *tmp_mbox = "/tmp/tmpmbox";
837         GHashTable *folder_table = NULL;
838
839         g_return_val_if_fail(dest != NULL, -1);
840         g_return_val_if_fail(mbox != NULL, -1);
841
842         if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
843                 debug_print(_("no messages in local mailbox.\n"));
844                 return 0;
845         } else if (size < 0)
846                 return -1;
847
848         if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
849                 return -1;
850
851         if (copy_mbox(mbox, tmp_mbox) < 0)
852                 return -1;
853
854         debug_print(_("Getting new messages from %s into %s...\n"),
855                     mbox, dest->path);
856
857         if (prefs_common.filter_on_inc)
858                 folder_table = g_hash_table_new(NULL, NULL);
859         msgs = proc_mbox(dest, tmp_mbox, folder_table);
860
861         unlink(tmp_mbox);
862         if (msgs >= 0) empty_mbox(mbox);
863         unlock_mbox(mbox, lockfd, LOCK_FLOCK);
864
865         if (folder_table) {
866                 folder_item_scan_foreach(folder_table);
867                 folderview_update_item_foreach(folder_table);
868                 g_hash_table_destroy(folder_table);
869         } else {
870                 folder_item_scan(dest);
871                 folderview_update_item(dest, FALSE);
872         }
873
874         return msgs;
875 }