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