Statistics
| Revision:

root / src / inc.c @ 56

History | View | Annotate | Download (34.5 kB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2004 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 <gtk/gtkprogressbar.h>
31
#include <stdio.h>
32
#include <unistd.h>
33
#include <string.h>
34
#include <time.h>
35
#include <sys/time.h>
36
#include <sys/types.h>
37
#include <sys/stat.h>
38
#include <sys/wait.h>
39
#include <signal.h>
40
#include <errno.h>
41
42
#include "intl.h"
43
#include "main.h"
44
#include "inc.h"
45
#include "mainwindow.h"
46
#include "folderview.h"
47
#include "summaryview.h"
48
#include "prefs_common.h"
49
#include "prefs_account.h"
50
#include "account.h"
51
#include "procmsg.h"
52
#include "socket.h"
53
#include "ssl.h"
54
#include "pop.h"
55
#include "recv.h"
56
#include "mbox.h"
57
#include "utils.h"
58
#include "gtkutils.h"
59
#include "statusbar.h"
60
#include "manage_window.h"
61
#include "stock_pixmap.h"
62
#include "progressdialog.h"
63
#include "inputdialog.h"
64
#include "alertpanel.h"
65
#include "filter.h"
66
#include "folder.h"
67
68
static GList *inc_dialog_list = NULL;
69
70
static guint inc_lock_count = 0;
71
72
static GdkPixmap *currentxpm;
73
static GdkBitmap *currentxpmmask;
74
static GdkPixmap *errorxpm;
75
static GdkBitmap *errorxpmmask;
76
static GdkPixmap *okxpm;
77
static GdkBitmap *okxpmmask;
78
79
#define MSGBUFSIZE        8192
80
81
static void inc_finished                (MainWindow                *mainwin,
82
                                         gboolean                 new_messages);
83
static gint inc_account_mail_real        (MainWindow                *mainwin,
84
                                         PrefsAccount                *account);
85
86
static IncProgressDialog *inc_progress_dialog_create
87
                                        (gboolean                 autocheck);
88
static void inc_progress_dialog_set_list(IncProgressDialog        *inc_dialog);
89
static void inc_progress_dialog_destroy        (IncProgressDialog        *inc_dialog);
90
91
static IncSession *inc_session_new        (PrefsAccount                *account);
92
static void inc_session_destroy                (IncSession                *session);
93
static gint inc_start                        (IncProgressDialog        *inc_dialog);
94
static IncState inc_pop3_session_do        (IncSession                *session);
95
96
static void inc_progress_dialog_update        (IncProgressDialog        *inc_dialog,
97
                                         IncSession                *inc_session);
98
99
static void inc_progress_dialog_set_label
100
                                        (IncProgressDialog        *inc_dialog,
101
                                         IncSession                *inc_session);
102
static void inc_progress_dialog_set_progress
103
                                        (IncProgressDialog        *inc_dialog,
104
                                         IncSession                *inc_session);
105
106
static void inc_update_folderview        (IncProgressDialog        *inc_dialog,
107
                                         IncSession                *inc_session);
108
109
static void inc_progress_dialog_update_periodic
110
                                        (IncProgressDialog        *inc_dialog,
111
                                         IncSession                *inc_session);
112
static void inc_update_folderview_periodic
113
                                        (IncProgressDialog        *inc_dialog,
114
                                         IncSession                *inc_session);
115
116
static gint inc_recv_data_progressive        (Session        *session,
117
                                         guint                 cur_len,
118
                                         guint                 total_len,
119
                                         gpointer         data);
120
static gint inc_recv_data_finished        (Session        *session,
121
                                         guint                 len,
122
                                         gpointer         data);
123
static gint inc_recv_message                (Session        *session,
124
                                         const gchar        *msg,
125
                                         gpointer         data);
126
static gint inc_drop_message                (Pop3Session        *session,
127
                                         const gchar        *file);
128
129
static void inc_put_error                (IncState         istate,
130
                                         const gchar        *msg);
131
132
static void inc_cancel_cb                (GtkWidget        *widget,
133
                                         gpointer         data);
134
static gint inc_dialog_delete_cb        (GtkWidget        *widget,
135
                                         GdkEventAny        *event,
136
                                         gpointer         data);
137
138
static gint inc_spool                        (void);
139
static gint get_spool                        (FolderItem        *dest,
140
                                         const gchar        *mbox);
141
142
static void inc_autocheck_timer_set_interval        (guint                 interval);
143
static gint inc_autocheck_func                        (gpointer         data);
144
145
/**
146
 * inc_finished:
147
 * @mainwin: Main window.
148
 * @new_messages: TRUE if some messages have been received.
149
 * 
150
 * Update the folder view and the summary view after receiving
151
 * messages.  If @new_messages is FALSE, this function avoids unneeded
152
 * updating.
153
 **/
154
static void inc_finished(MainWindow *mainwin, gboolean new_messages)
155
{
156
        FolderItem *item;
157
158
        if (prefs_common.scan_all_after_inc)
159
                folderview_check_new(NULL);
160
161
        if (!new_messages && !prefs_common.scan_all_after_inc) return;
162
163
        if (prefs_common.open_inbox_on_inc) {
164
                item = cur_account && cur_account->inbox
165
                        ? folder_find_item_from_identifier(cur_account->inbox)
166
                        : folder_get_default_inbox();
167
                folderview_unselect(mainwin->folderview);
168
                folderview_select(mainwin->folderview, item);
169
        } else if (prefs_common.scan_all_after_inc) {
170
                item = mainwin->summaryview->folder_item;
171
                if (item)
172
                        folderview_update_item(item, TRUE);
173
        }
174
}
175
176
void inc_mail(MainWindow *mainwin)
177
{
178
        gint new_msgs = 0;
179
180
        if (inc_lock_count) return;
181
182
        if (!main_window_toggle_online_if_offline(mainwin))
183
                return;
184
185
        inc_autocheck_timer_remove();
186
        summary_write_cache(mainwin->summaryview);
187
        main_window_lock(mainwin);
188
189
        if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
190
                /* external incorporating program */
191
                if (execute_command_line(prefs_common.extinc_cmd, FALSE) != 0) {
192
                        main_window_unlock(mainwin);
193
                        inc_autocheck_timer_set();
194
                        return;
195
                }
196
197
                if (prefs_common.inc_local)
198
                        new_msgs = inc_spool();
199
        } else {
200
                if (prefs_common.inc_local) {
201
                        new_msgs = inc_spool();
202
                        if (new_msgs < 0)
203
                                new_msgs = 0;
204
                }
205
206
                new_msgs += inc_account_mail_real(mainwin, cur_account);
207
        }
208
209
        inc_finished(mainwin, new_msgs > 0);
210
        main_window_unlock(mainwin);
211
        inc_autocheck_timer_set();
212
}
213
214
static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
215
{
216
        IncProgressDialog *inc_dialog;
217
        IncSession *session;
218
219
        if (account->protocol == A_IMAP4 || account->protocol == A_NNTP) {
220
                FolderItem *item = mainwin->summaryview->folder_item;
221
222
                folderview_check_new(FOLDER(account->folder));
223
                if (!prefs_common.scan_all_after_inc && item != NULL &&
224
                    FOLDER(account->folder) == item->folder)
225
                        folderview_update_item(item, TRUE);
226
                return 1;
227
        }
228
229
        session = inc_session_new(account);
230
        if (!session) return 0;
231
232
        inc_dialog = inc_progress_dialog_create(FALSE);
233
        inc_dialog->queue_list = g_list_append(inc_dialog->queue_list, session);
234
        inc_dialog->mainwin = mainwin;
235
        inc_progress_dialog_set_list(inc_dialog);
236
237
        main_window_set_toolbar_sensitive(mainwin);
238
        main_window_set_menu_sensitive(mainwin);
239
240
        return inc_start(inc_dialog);
241
}
242
243
gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
244
{
245
        gint new_msgs;
246
247
        if (inc_lock_count) return 0;
248
249
        if (!main_window_toggle_online_if_offline(mainwin))
250
                return 0;
251
252
        inc_autocheck_timer_remove();
253
        summary_write_cache(mainwin->summaryview);
254
        main_window_lock(mainwin);
255
256
        new_msgs = inc_account_mail_real(mainwin, account);
257
258
        inc_finished(mainwin, new_msgs > 0);
259
        main_window_unlock(mainwin);
260
        inc_autocheck_timer_set();
261
262
        return new_msgs;
263
}
264
265
void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck)
266
{
267
        GList *list, *queue_list = NULL;
268
        IncProgressDialog *inc_dialog;
269
        gint new_msgs = 0;
270
271
        if (inc_lock_count) return;
272
273
        if (!main_window_toggle_online_if_offline(mainwin))
274
                return;
275
276
        inc_autocheck_timer_remove();
277
        summary_write_cache(mainwin->summaryview);
278
        main_window_lock(mainwin);
279
280
        if (prefs_common.inc_local) {
281
                new_msgs = inc_spool();
282
                if (new_msgs < 0)
283
                        new_msgs = 0;
284
        }
285
286
        /* check IMAP4 / News folders */
287
        for (list = account_get_list(); list != NULL; list = list->next) {
288
                PrefsAccount *account = list->data;
289
                if ((account->protocol == A_IMAP4 ||
290
                     account->protocol == A_NNTP) && account->recv_at_getall) {
291
                        FolderItem *item = mainwin->summaryview->folder_item;
292
293
                        folderview_check_new(FOLDER(account->folder));
294
                        if (!prefs_common.scan_all_after_inc && item != NULL &&
295
                            FOLDER(account->folder) == item->folder)
296
                                folderview_update_item(item, TRUE);
297
                }
298
        }
299
300
        /* check POP3 accounts */
301
        for (list = account_get_list(); list != NULL; list = list->next) {
302
                IncSession *session;
303
                PrefsAccount *account = list->data;
304
305
                if (account->recv_at_getall) {
306
                        session = inc_session_new(account);
307
                        if (session)
308
                                queue_list = g_list_append(queue_list, session);
309
                }
310
        }
311
312
        if (queue_list) {
313
                inc_dialog = inc_progress_dialog_create(autocheck);
314
                inc_dialog->queue_list = queue_list;
315
                inc_dialog->mainwin = mainwin;
316
                inc_progress_dialog_set_list(inc_dialog);
317
318
                main_window_set_toolbar_sensitive(mainwin);
319
                main_window_set_menu_sensitive(mainwin);
320
321
                new_msgs += inc_start(inc_dialog);
322
        }
323
324
        inc_finished(mainwin, new_msgs > 0);
325
        main_window_unlock(mainwin);
326
        inc_autocheck_timer_set();
327
}
328
329
static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
330
{
331
        IncProgressDialog *dialog;
332
        ProgressDialog *progress;
333
334
        dialog = g_new0(IncProgressDialog, 1);
335
336
        progress = progress_dialog_create();
337
        gtk_window_set_title(GTK_WINDOW(progress->window),
338
                             _("Retrieving new messages"));
339
        g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
340
                         G_CALLBACK(inc_cancel_cb), dialog);
341
        g_signal_connect(G_OBJECT(progress->window), "delete_event",
342
                         G_CALLBACK(inc_dialog_delete_cb), dialog);
343
        /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
344
345
        progress_dialog_set_value(progress, 0.0);
346
347
        stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
348
                         &okxpm, &okxpmmask);
349
        stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
350
                         &currentxpm, &currentxpmmask);
351
        stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
352
                         &errorxpm, &errorxpmmask);
353
354
        if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
355
            (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
356
             !autocheck)) {
357
                dialog->show_dialog = TRUE;
358
                gtk_widget_show_now(progress->window);
359
        }
360
361
        dialog->dialog = progress;
362
        gettimeofday(&dialog->progress_tv, NULL);
363
        gettimeofday(&dialog->folder_tv, NULL);
364
        dialog->queue_list = NULL;
365
        dialog->cur_row = 0;
366
367
        inc_dialog_list = g_list_append(inc_dialog_list, dialog);
368
369
        return dialog;
370
}
371
372
static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
373
{
374
        GList *list;
375
376
        for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
377
                IncSession *session = list->data;
378
                Pop3Session *pop3_session = POP3_SESSION(session->session);
379
                gchar *text[3];
380
381
                session->data = inc_dialog;
382
383
                text[0] = NULL;
384
                text[1] = pop3_session->ac_prefs->account_name;
385
                text[2] = _("Standby");
386
                gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
387
        }
388
}
389
390
static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
391
{
392
        progress_dialog_set_value(inc_dialog->dialog, 0.0);
393
        progress_dialog_set_label(inc_dialog->dialog, "");
394
        main_window_progress_off(inc_dialog->mainwin);
395
}
396
397
static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
398
{
399
        g_return_if_fail(inc_dialog != NULL);
400
401
        inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
402
403
        main_window_progress_off(inc_dialog->mainwin);
404
        progress_dialog_destroy(inc_dialog->dialog);
405
406
        g_free(inc_dialog);
407
}
408
409
static IncSession *inc_session_new(PrefsAccount *account)
410
{
411
        IncSession *session;
412
413
        g_return_val_if_fail(account != NULL, NULL);
414
415
        if (account->protocol != A_POP3)
416
                return NULL;
417
        if (!account->recv_server || !account->userid)
418
                return NULL;
419
420
        session = g_new0(IncSession, 1);
421
422
        session->session = pop3_session_new(account);
423
        session->session->data = session;
424
        POP3_SESSION(session->session)->drop_message = inc_drop_message;
425
        session_set_recv_message_notify(session->session,
426
                                        inc_recv_message, session);
427
        session_set_recv_data_progressive_notify(session->session,
428
                                                 inc_recv_data_progressive,
429
                                                 session);
430
        session_set_recv_data_notify(session->session,
431
                                     inc_recv_data_finished, session);
432
433
        session->folder_table = g_hash_table_new(NULL, NULL);
434
        session->tmp_folder_table = g_hash_table_new(NULL, NULL);
435
436
        return session;
437
}
438
439
static void inc_session_destroy(IncSession *session)
440
{
441
        g_return_if_fail(session != NULL);
442
443
        session_destroy(session->session);
444
        g_hash_table_destroy(session->folder_table);
445
        g_hash_table_destroy(session->tmp_folder_table);
446
        g_free(session);
447
}
448
449
static gint inc_start(IncProgressDialog *inc_dialog)
450
{
451
        IncSession *session;
452
        GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
453
        GList *qlist;
454
        Pop3Session *pop3_session;
455
        IncState inc_state;
456
        gint error_num = 0;
457
        gint new_msgs = 0;
458
        gchar *msg;
459
        gchar *fin_msg;
460
461
        qlist = inc_dialog->queue_list;
462
        while (qlist != NULL) {
463
                GList *next = qlist->next;
464
465
                session = qlist->data;
466
                pop3_session = POP3_SESSION(session->session); 
467
                pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
468
                if (pop3_session->ac_prefs->passwd)
469
                        pop3_session->pass =
470
                                g_strdup(pop3_session->ac_prefs->passwd);
471
                else if (pop3_session->ac_prefs->tmp_pass)
472
                        pop3_session->pass =
473
                                g_strdup(pop3_session->ac_prefs->tmp_pass);
474
                else {
475
                        gchar *pass;
476
477
                        if (inc_dialog->show_dialog)
478
                                manage_window_focus_in
479
                                        (inc_dialog->dialog->window,
480
                                         NULL, NULL);
481
482
                        pass = input_dialog_query_password
483
                                (pop3_session->ac_prefs->recv_server,
484
                                 pop3_session->user);
485
486
                        if (inc_dialog->show_dialog)
487
                                manage_window_focus_out
488
                                        (inc_dialog->dialog->window,
489
                                         NULL, NULL);
490
491
                        if (pass) {
492
                                pop3_session->ac_prefs->tmp_pass =
493
                                        g_strdup(pass);
494
                                pop3_session->pass = pass;
495
                        }
496
                }
497
498
                qlist = next;
499
        }
500
501
#define SET_PIXMAP_AND_TEXT(xpm, xpmmask, str)                                   \
502
{                                                                           \
503
        gtk_clist_set_pixmap(clist, inc_dialog->cur_row, 0, xpm, xpmmask); \
504
        gtk_clist_set_text(clist, inc_dialog->cur_row, 2, str);                   \
505
}
506
507
        for (; inc_dialog->queue_list != NULL; inc_dialog->cur_row++) {
508
                session = inc_dialog->queue_list->data;
509
                pop3_session = POP3_SESSION(session->session);
510
511
                if (pop3_session->pass == NULL) {
512
                        SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
513
                        inc_session_destroy(session);
514
                        inc_dialog->queue_list =
515
                                g_list_remove(inc_dialog->queue_list, session);
516
                        continue;
517
                }
518
519
                inc_progress_dialog_clear(inc_dialog);
520
                gtk_clist_moveto(clist, inc_dialog->cur_row, -1, 1.0, 0.0);
521
522
                SET_PIXMAP_AND_TEXT(currentxpm, currentxpmmask,
523
                                    _("Retrieving"));
524
525
                /* begin POP3 session */
526
                inc_state = inc_pop3_session_do(session);
527
528
                switch (inc_state) {
529
                case INC_SUCCESS:
530
                        if (pop3_session->cur_total_num > 0)
531
                                msg = g_strdup_printf
532
                                        (_("Done (%d message(s) (%s) received)"),
533
                                         pop3_session->cur_total_num,
534
                                         to_human_readable(pop3_session->cur_total_recv_bytes));
535
                        else
536
                                msg = g_strdup_printf(_("Done (no new messages)"));
537
                        SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, msg);
538
                        g_free(msg);
539
                        break;
540
                case INC_CONNECT_ERROR:
541
                        SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
542
                                            _("Connection failed"));
543
                        break;
544
                case INC_AUTH_FAILED:
545
                        SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
546
                                            _("Auth failed"));
547
                        break;
548
                case INC_LOCKED:
549
                        SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask,
550
                                            _("Locked"));
551
                        break;
552
                case INC_ERROR:
553
                case INC_NO_SPACE:
554
                case INC_IO_ERROR:
555
                case INC_SOCKET_ERROR:
556
                case INC_EOF:
557
                        SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask, _("Error"));
558
                        break;
559
                case INC_TIMEOUT:
560
                        SET_PIXMAP_AND_TEXT(errorxpm, errorxpmmask, _("Timeout"));
561
                        break;
562
                case INC_CANCEL:
563
                        SET_PIXMAP_AND_TEXT(okxpm, okxpmmask, _("Cancelled"));
564
                        break;
565
                default:
566
                        break;
567
                }
568
569
                new_msgs += pop3_session->cur_total_num;
570
571
                if (!prefs_common.scan_all_after_inc) {
572
                        folder_item_scan_foreach(session->folder_table);
573
                        folderview_update_item_foreach
574
                                (session->folder_table,
575
                                 !prefs_common.open_inbox_on_inc);
576
                }
577
578
                if (pop3_session->error_val == PS_AUTHFAIL &&
579
                    pop3_session->ac_prefs->tmp_pass) {
580
                        g_free(pop3_session->ac_prefs->tmp_pass);
581
                        pop3_session->ac_prefs->tmp_pass = NULL;
582
                }
583
584
                pop3_write_uidl_list(pop3_session);
585
586
                if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
587
                        error_num++;
588
                        if (inc_dialog->show_dialog)
589
                                manage_window_focus_in
590
                                        (inc_dialog->dialog->window,
591
                                         NULL, NULL);
592
                        inc_put_error(inc_state, pop3_session->error_msg);
593
                        if (inc_dialog->show_dialog)
594
                                manage_window_focus_out
595
                                        (inc_dialog->dialog->window,
596
                                         NULL, NULL);
597
                        if (inc_state == INC_NO_SPACE ||
598
                            inc_state == INC_IO_ERROR)
599
                                break;
600
                }
601
602
                inc_session_destroy(session);
603
                inc_dialog->queue_list =
604
                        g_list_remove(inc_dialog->queue_list, session);
605
        }
606
607
#undef SET_PIXMAP_AND_TEXT
608
609
        if (new_msgs > 0)
610
                fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
611
                                          new_msgs);
612
        else
613
                fin_msg = g_strdup_printf(_("Finished (no new messages)"));
614
615
        progress_dialog_set_label(inc_dialog->dialog, fin_msg);
616
617
#if 0
618
        if (error_num && !prefs_common.no_recv_err_panel) {
619
                if (inc_dialog->show_dialog)
620
                        manage_window_focus_in(inc_dialog->dialog->window,
621
                                               NULL, NULL);
622
                alertpanel_error(_("Some errors occurred while getting mail."));
623
                if (inc_dialog->show_dialog)
624
                        manage_window_focus_out(inc_dialog->dialog->window,
625
                                                NULL, NULL);
626
        }
627
#endif
628
629
        while (inc_dialog->queue_list != NULL) {
630
                session = inc_dialog->queue_list->data;
631
                inc_session_destroy(session);
632
                inc_dialog->queue_list =
633
                        g_list_remove(inc_dialog->queue_list, session);
634
        }
635
636
        if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
637
                inc_progress_dialog_destroy(inc_dialog);
638
        else {
639
                gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
640
                                     fin_msg);
641
                gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
642
                                     GTK_STOCK_CLOSE);
643
        }
644
645
        g_free(fin_msg);
646
647
        return new_msgs;
648
}
649
650
static IncState inc_pop3_session_do(IncSession *session)
651
{
652
        Pop3Session *pop3_session = POP3_SESSION(session->session);
653
        IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
654
        gchar *server;
655
        gushort port;
656
        gchar *buf;
657
658
        debug_print(_("getting new messages of account %s...\n"),
659
                    pop3_session->ac_prefs->account_name);
660
661
        buf = g_strdup_printf(_("%s: Retrieving new messages"),
662
                              pop3_session->ac_prefs->recv_server);
663
        gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
664
        g_free(buf);
665
666
        server = pop3_session->ac_prefs->recv_server;
667
#if USE_SSL
668
        port = pop3_session->ac_prefs->set_popport ?
669
                pop3_session->ac_prefs->popport :
670
                pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
671
        SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
672
        if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
673
                SESSION(pop3_session)->nonblocking =
674
                        pop3_session->ac_prefs->use_nonblocking_ssl;
675
#else
676
        port = pop3_session->ac_prefs->set_popport ?
677
                pop3_session->ac_prefs->popport : 110;
678
#endif
679
680
        buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
681
        log_message("%s\n", buf);
682
        progress_dialog_set_label(inc_dialog->dialog, buf);
683
        g_free(buf);
684
685
        session_set_timeout(SESSION(pop3_session),
686
                            prefs_common.io_timeout_secs * 1000);
687
688
        if (session_connect(SESSION(pop3_session), server, port) < 0) {
689
                log_warning(_("Can't connect to POP3 server: %s:%d\n"),
690
                            server, port);
691
                session->inc_state = INC_CONNECT_ERROR;
692
                statusbar_pop_all();
693
                return INC_CONNECT_ERROR;
694
        }
695
696
        while (session_is_connected(SESSION(pop3_session)) &&
697
               session->inc_state != INC_CANCEL)
698
                gtk_main_iteration();
699
700
        if (session->inc_state == INC_SUCCESS) {
701
                switch (pop3_session->error_val) {
702
                case PS_SUCCESS:
703
                        switch (SESSION(pop3_session)->state) {
704
                        case SESSION_ERROR:
705
                                if (pop3_session->state == POP3_READY)
706
                                        session->inc_state = INC_CONNECT_ERROR;
707
                                else
708
                                        session->inc_state = INC_ERROR;
709
                                break;
710
                        case SESSION_EOF:
711
                                session->inc_state = INC_EOF;
712
                                break;
713
                        case SESSION_TIMEOUT:
714
                                session->inc_state = INC_TIMEOUT;
715
                                break;
716
                        default:
717
                                session->inc_state = INC_SUCCESS;
718
                                break;
719
                        }
720
                        break;
721
                case PS_AUTHFAIL:
722
                        session->inc_state = INC_AUTH_FAILED;
723
                        break;
724
                case PS_IOERR:
725
                        session->inc_state = INC_IO_ERROR;
726
                        break;
727
                case PS_SOCKET:
728
                        session->inc_state = INC_SOCKET_ERROR;
729
                        break;
730
                case PS_LOCKBUSY:
731
                        session->inc_state = INC_LOCKED;
732
                        break;
733
                default:
734
                        session->inc_state = INC_ERROR;
735
                        break;
736
                }
737
        }
738
739
        session_disconnect(SESSION(pop3_session));
740
        statusbar_pop_all();
741
742
        return session->inc_state;
743
}
744
745
static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
746
                                       IncSession *inc_session)
747
{
748
        inc_progress_dialog_set_label(inc_dialog, inc_session);
749
        inc_progress_dialog_set_progress(inc_dialog, inc_session);
750
}
751
752
static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
753
                                          IncSession *inc_session)
754
{
755
        ProgressDialog *dialog = inc_dialog->dialog;
756
        Pop3Session *session;
757
758
        g_return_if_fail(inc_session != NULL);
759
760
        session = POP3_SESSION(inc_session->session);
761
762
        switch (session->state) {
763
        case POP3_GREETING:
764
                break;
765
        case POP3_GETAUTH_USER:
766
        case POP3_GETAUTH_PASS:
767
        case POP3_GETAUTH_APOP:
768
                progress_dialog_set_label(dialog, _("Authenticating..."));
769
                statusbar_print_all(_("Retrieving messages from %s..."),
770
                                    SESSION(session)->server);
771
                break;
772
        case POP3_GETRANGE_STAT:
773
                progress_dialog_set_label
774
                        (dialog, _("Getting the number of new messages (STAT)..."));
775
                break;
776
        case POP3_GETRANGE_LAST:
777
                progress_dialog_set_label
778
                        (dialog, _("Getting the number of new messages (LAST)..."));
779
                break;
780
        case POP3_GETRANGE_UIDL:
781
                progress_dialog_set_label
782
                        (dialog, _("Getting the number of new messages (UIDL)..."));
783
                break;
784
        case POP3_GETSIZE_LIST:
785
                progress_dialog_set_label
786
                        (dialog, _("Getting the size of messages (LIST)..."));
787
                break;
788
        case POP3_RETR:
789
        case POP3_RETR_RECV:
790
                break;
791
        case POP3_DELETE:
792
#if 0
793
                if (session->msg[session->cur_msg].recv_time <
794
                        session->current_time) {
795
                        gchar buf[MSGBUFSIZE];
796
                        g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
797
                                   session->cur_msg);
798
                        progress_dialog_set_label(dialog, buf);
799
                }
800
#endif
801
                break;
802
        case POP3_LOGOUT:
803
                progress_dialog_set_label(dialog, _("Quitting"));
804
                break;
805
        default:
806
                break;
807
        }
808
}
809
810
static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
811
                                             IncSession *inc_session)
812
{
813
        gchar buf[MSGBUFSIZE];
814
        Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
815
        gchar *total_size_str;
816
        gint cur_total;
817
        gint total;
818
819
        if (!pop3_session->new_msg_exist) return;
820
821
        cur_total = inc_session->cur_total_bytes;
822
        total = pop3_session->total_bytes;
823
        if (pop3_session->state == POP3_RETR ||
824
            pop3_session->state == POP3_RETR_RECV ||
825
            pop3_session->state == POP3_DELETE) {
826
                Xstrdup_a(total_size_str, to_human_readable(total), return);
827
                g_snprintf(buf, sizeof(buf),
828
                           _("Retrieving message (%d / %d) (%s / %s)"),
829
                           pop3_session->cur_msg, pop3_session->count,
830
                           to_human_readable(cur_total), total_size_str);
831
                progress_dialog_set_label(inc_dialog->dialog, buf);
832
        }
833
834
        progress_dialog_set_percentage
835
                (inc_dialog->dialog,(gfloat)cur_total / (gfloat)total);
836
837
        gtk_progress_set_show_text
838
                (GTK_PROGRESS(inc_dialog->mainwin->progressbar), TRUE);
839
        g_snprintf(buf, sizeof(buf), "%d / %d",
840
                   pop3_session->cur_msg, pop3_session->count);
841
        gtk_progress_set_format_string
842
                (GTK_PROGRESS(inc_dialog->mainwin->progressbar), buf);
843
        gtk_progress_bar_update
844
                (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
845
                 (gfloat)cur_total / (gfloat)total);
846
847
        if (pop3_session->cur_total_num > 0) {
848
                g_snprintf(buf, sizeof(buf),
849
                           _("Retrieving (%d message(s) (%s) received)"),
850
                           pop3_session->cur_total_num,
851
                           to_human_readable
852
                           (pop3_session->cur_total_recv_bytes));
853
                gtk_clist_set_text(GTK_CLIST(inc_dialog->dialog->clist),
854
                                   inc_dialog->cur_row, 2, buf);
855
        }
856
}
857
858
static gboolean hash_remove_func(gpointer key, gpointer value, gpointer data)
859
{
860
        return TRUE;
861
}
862
863
static void inc_update_folderview(IncProgressDialog *inc_dialog,
864
                                  IncSession *inc_session)
865
{
866
        if (g_hash_table_size(inc_session->tmp_folder_table) > 0) {
867
                folderview_update_item_foreach(inc_session->tmp_folder_table,
868
                                               FALSE);
869
                g_hash_table_foreach_remove(inc_session->tmp_folder_table,
870
                                            hash_remove_func, NULL);
871
        }
872
}
873
874
static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
875
                                                IncSession *inc_session)
876
{
877
        struct timeval tv_cur;
878
        struct timeval tv_result;
879
        gint msec;
880
881
        gettimeofday(&tv_cur, NULL);
882
883
        tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
884
        tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
885
        if (tv_result.tv_usec < 0) {
886
                tv_result.tv_sec--;
887
                tv_result.tv_usec += 1000000;
888
        }
889
890
        msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
891
        if (msec > PROGRESS_UPDATE_INTERVAL) {
892
                inc_progress_dialog_update(inc_dialog, inc_session);
893
                inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
894
                inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
895
        }
896
}
897
898
static void inc_update_folderview_periodic(IncProgressDialog *inc_dialog,
899
                                           IncSession *inc_session)
900
{
901
        struct timeval tv_cur;
902
        struct timeval tv_result;
903
        gint msec;
904
905
        gettimeofday(&tv_cur, NULL);
906
907
        tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->folder_tv.tv_sec;
908
        tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->folder_tv.tv_usec;
909
        if (tv_result.tv_usec < 0) {
910
                tv_result.tv_sec--;
911
                tv_result.tv_usec += 1000000;
912
        }
913
914
        msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
915
        if (msec > FOLDER_UPDATE_INTERVAL) {
916
                inc_update_folderview(inc_dialog, inc_session);
917
                inc_dialog->folder_tv.tv_sec = tv_cur.tv_sec;
918
                inc_dialog->folder_tv.tv_usec = tv_cur.tv_usec;
919
        }
920
}
921
922
static gint inc_recv_data_progressive(Session *session, guint cur_len,
923
                                      guint total_len, gpointer data)
924
{
925
        IncSession *inc_session = (IncSession *)data;
926
        Pop3Session *pop3_session = POP3_SESSION(session);
927
        IncProgressDialog *inc_dialog;
928
        gint cur_total;
929
930
        g_return_val_if_fail(inc_session != NULL, -1);
931
932
        if (pop3_session->state != POP3_RETR &&
933
            pop3_session->state != POP3_RETR_RECV &&
934
            pop3_session->state != POP3_DELETE &&
935
            pop3_session->state != POP3_LOGOUT) return 0;
936
937
        if (!pop3_session->new_msg_exist) return 0;
938
939
        cur_total = pop3_session->cur_total_bytes + cur_len;
940
        if (cur_total > pop3_session->total_bytes)
941
                cur_total = pop3_session->total_bytes;
942
        inc_session->cur_total_bytes = cur_total;
943
944
        inc_dialog = (IncProgressDialog *)inc_session->data;
945
        inc_progress_dialog_update_periodic(inc_dialog, inc_session);
946
        inc_update_folderview_periodic(inc_dialog, inc_session);
947
948
        return 0;
949
}
950
951
static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
952
{
953
        IncSession *inc_session = (IncSession *)data;
954
        IncProgressDialog *inc_dialog;
955
956
        g_return_val_if_fail(inc_session != NULL, -1);
957
958
        inc_dialog = (IncProgressDialog *)inc_session->data;
959
960
        inc_recv_data_progressive(session, 0, 0, inc_session);
961
962
        if (POP3_SESSION(session)->state == POP3_LOGOUT) {
963
                inc_progress_dialog_update(inc_dialog, inc_session);
964
                inc_update_folderview(inc_dialog, inc_session);
965
        }
966
967
        return 0;
968
}
969
970
static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
971
{
972
        IncSession *inc_session = (IncSession *)data;
973
        IncProgressDialog *inc_dialog;
974
975
        g_return_val_if_fail(inc_session != NULL, -1);
976
977
        inc_dialog = (IncProgressDialog *)inc_session->data;
978
979
        switch (POP3_SESSION(session)->state) {
980
        case POP3_GETAUTH_USER:
981
        case POP3_GETAUTH_PASS:
982
        case POP3_GETAUTH_APOP:
983
        case POP3_GETRANGE_STAT:
984
        case POP3_GETRANGE_LAST:
985
        case POP3_GETRANGE_UIDL:
986
        case POP3_GETSIZE_LIST:
987
                inc_progress_dialog_update(inc_dialog, inc_session);
988
                break;
989
        case POP3_RETR:
990
                inc_recv_data_progressive(session, 0, 0, inc_session);
991
                break;
992
        case POP3_LOGOUT:
993
                inc_progress_dialog_update(inc_dialog, inc_session);
994
                inc_update_folderview(inc_dialog, inc_session);
995
                break;
996
        default:
997
                break;
998
        }
999
1000
        return 0;
1001
}
1002
1003
static gint inc_drop_message(Pop3Session *session, const gchar *file)
1004
{
1005
        FolderItem *inbox;
1006
        GSList *cur;
1007
        FilterInfo *fltinfo;
1008
        IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1009
        gint val;
1010
1011
        g_return_val_if_fail(inc_session != NULL, DROP_ERROR);
1012
1013
        if (session->ac_prefs->inbox) {
1014
                inbox = folder_find_item_from_identifier
1015
                        (session->ac_prefs->inbox);
1016
                if (!inbox)
1017
                        inbox = folder_get_default_inbox();
1018
        } else
1019
                inbox = folder_get_default_inbox();
1020
        if (!inbox)
1021
                return DROP_ERROR;
1022
1023
        fltinfo = filter_info_new();
1024
        fltinfo->account = session->ac_prefs;
1025
        fltinfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1026
        fltinfo->flags.tmp_flags = MSG_RECEIVED;
1027
1028
        if (session->ac_prefs->filter_on_recv)
1029
                filter_apply(prefs_common.fltlist, file, fltinfo);
1030
1031
        if (!fltinfo->drop_done) {
1032
                if (folder_item_add_msg
1033
                        (inbox, file, &fltinfo->flags, FALSE) < 0) {
1034
                        filter_info_free(fltinfo);
1035
                        return DROP_ERROR;
1036
                }
1037
                fltinfo->dest_list = g_slist_append(fltinfo->dest_list, inbox);
1038
        }
1039
1040
        for (cur = fltinfo->dest_list; cur != NULL; cur = cur->next) {
1041
                FolderItem *drop_folder = (FolderItem *)cur->data;
1042
1043
                val = GPOINTER_TO_INT(g_hash_table_lookup
1044
                                      (inc_session->folder_table, drop_folder));
1045
                if (val == 0) {
1046
                        /* force updating */
1047
                        if (FOLDER_IS_LOCAL(drop_folder->folder))
1048
                                drop_folder->mtime = 0;
1049
                        g_hash_table_insert(inc_session->folder_table, drop_folder,
1050
                                            GINT_TO_POINTER(1));
1051
                }
1052
                g_hash_table_insert(inc_session->tmp_folder_table, drop_folder,
1053
                                    GINT_TO_POINTER(1));
1054
        }
1055
1056
        if (fltinfo->actions[FLT_ACTION_NOT_RECEIVE] == TRUE)
1057
                val = DROP_DONT_RECEIVE;
1058
        else if (fltinfo->actions[FLT_ACTION_DELETE] == TRUE)
1059
                val = DROP_DELETE;
1060
        else
1061
                val = DROP_OK;
1062
1063
        filter_info_free(fltinfo);
1064
1065
        return val;
1066
}
1067
1068
static void inc_put_error(IncState istate, const gchar *msg)
1069
{
1070
        gchar *log_msg = NULL;
1071
        gchar *err_msg = NULL;
1072
        gboolean fatal_error = FALSE;
1073
1074
        switch (istate) {
1075
        case INC_CONNECT_ERROR:
1076
                log_msg = _("Connection failed.");
1077
                if (prefs_common.no_recv_err_panel)
1078
                        break;
1079
                err_msg = g_strdup(log_msg);
1080
                break;
1081
        case INC_ERROR:
1082
                log_msg = _("Error occurred while processing mail.");
1083
                if (prefs_common.no_recv_err_panel)
1084
                        break;
1085
                if (msg)
1086
                        err_msg = g_strdup_printf
1087
                                (_("Error occurred while processing mail:\n%s"),
1088
                                 msg);
1089
                else
1090
                        err_msg = g_strdup(log_msg);
1091
                break;
1092
        case INC_NO_SPACE:
1093
                log_msg = _("No disk space left.");
1094
                err_msg = g_strdup(log_msg);
1095
                fatal_error = TRUE;
1096
                break;
1097
        case INC_IO_ERROR:
1098
                log_msg = _("Can't write file.");
1099
                err_msg = g_strdup(log_msg);
1100
                fatal_error = TRUE;
1101
                break;
1102
        case INC_SOCKET_ERROR:
1103
                log_msg = _("Socket error.");
1104
                if (prefs_common.no_recv_err_panel)
1105
                        break;
1106
                err_msg = g_strdup(log_msg);
1107
                break;
1108
        case INC_EOF:
1109
                log_msg = _("Connection closed by the remote host.");
1110
                if (prefs_common.no_recv_err_panel)
1111
                        break;
1112
                err_msg = g_strdup(log_msg);
1113
                break;
1114
        case INC_LOCKED:
1115
                log_msg = _("Mailbox is locked.");
1116
                if (prefs_common.no_recv_err_panel)
1117
                        break;
1118
                if (msg)
1119
                        err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1120
                                                  msg);
1121
                else
1122
                        err_msg = g_strdup(log_msg);
1123
                break;
1124
        case INC_AUTH_FAILED:
1125
                log_msg = _("Authentication failed.");
1126
                if (prefs_common.no_recv_err_panel)
1127
                        break;
1128
                if (msg)
1129
                        err_msg = g_strdup_printf
1130
                                (_("Authentication failed:\n%s"), msg);
1131
                else
1132
                        err_msg = g_strdup(log_msg);
1133
                break;
1134
        case INC_TIMEOUT:
1135
                log_msg = _("Session timed out.");
1136
                if (prefs_common.no_recv_err_panel)
1137
                        break;
1138
                err_msg = g_strdup(log_msg);
1139
                break;
1140
        default:
1141
                break;
1142
        }
1143
1144
        if (log_msg) {
1145
                if (fatal_error)
1146
                        log_error("%s\n", log_msg);
1147
                else
1148
                        log_warning("%s\n", log_msg);
1149
        }
1150
        if (err_msg) {
1151
                alertpanel_error(err_msg);
1152
                g_free(err_msg);
1153
        }
1154
}
1155
1156
static void inc_cancel(IncProgressDialog *dialog)
1157
{
1158
        IncSession *session;
1159
1160
        g_return_if_fail(dialog != NULL);
1161
1162
        if (dialog->queue_list == NULL) {
1163
                inc_progress_dialog_destroy(dialog);
1164
                return;
1165
        }
1166
1167
        session = dialog->queue_list->data;
1168
1169
        session->inc_state = INC_CANCEL;
1170
1171
        log_message(_("Incorporation cancelled\n"));
1172
}
1173
1174
gboolean inc_is_active(void)
1175
{
1176
        return (inc_dialog_list != NULL);
1177
}
1178
1179
void inc_cancel_all(void)
1180
{
1181
        GList *cur;
1182
1183
        for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1184
                inc_cancel((IncProgressDialog *)cur->data);
1185
}
1186
1187
static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1188
{
1189
        inc_cancel((IncProgressDialog *)data);
1190
}
1191
1192
static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1193
                                 gpointer data)
1194
{
1195
        IncProgressDialog *dialog = (IncProgressDialog *)data;
1196
1197
        if (dialog->queue_list == NULL)
1198
                inc_progress_dialog_destroy(dialog);
1199
1200
        return TRUE;
1201
}
1202
1203
static gint inc_spool(void)
1204
{
1205
        gchar *spool_path;
1206
        gchar *mbox;
1207
        gint msgs;
1208
1209
        spool_path = prefs_common.spool_path
1210
                ? prefs_common.spool_path : DEFAULT_SPOOL_PATH;
1211
        if (is_file_exist(spool_path))
1212
                mbox = g_strdup(spool_path);
1213
        else if (is_dir_exist(spool_path))
1214
                mbox = g_strconcat(spool_path, G_DIR_SEPARATOR_S,
1215
                                   g_get_user_name(), NULL);
1216
        else {
1217
                debug_print("%s: local mailbox not found.\n", spool_path);
1218
                return -1;
1219
        }
1220
1221
        msgs = get_spool(folder_get_default_inbox(), mbox);
1222
        g_free(mbox);
1223
1224
        return msgs;
1225
}
1226
1227
static gint get_spool(FolderItem *dest, const gchar *mbox)
1228
{
1229
        gint msgs, size;
1230
        gint lockfd;
1231
        gchar tmp_mbox[MAXPATHLEN + 1];
1232
        GHashTable *folder_table = NULL;
1233
1234
        g_return_val_if_fail(dest != NULL, -1);
1235
        g_return_val_if_fail(mbox != NULL, -1);
1236
1237
        if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1238
                debug_print("%s: no messages in local mailbox.\n", mbox);
1239
                return 0;
1240
        } else if (size < 0)
1241
                return -1;
1242
1243
        if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1244
                return -1;
1245
1246
        g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%p",
1247
                   get_tmp_dir(), G_DIR_SEPARATOR, mbox);
1248
1249
        if (copy_mbox(mbox, tmp_mbox) < 0) {
1250
                unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1251
                return -1;
1252
        }
1253
1254
        debug_print(_("Getting new messages from %s into %s...\n"),
1255
                    mbox, dest->path);
1256
1257
        if (prefs_common.filter_on_inc)
1258
                folder_table = g_hash_table_new(NULL, NULL);
1259
        msgs = proc_mbox(dest, tmp_mbox, folder_table);
1260
1261
        unlink(tmp_mbox);
1262
        if (msgs >= 0) empty_mbox(mbox);
1263
        unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1264
1265
        if (folder_table) {
1266
                if (!prefs_common.scan_all_after_inc) {
1267
                        folder_item_scan_foreach(folder_table);
1268
                        folderview_update_item_foreach
1269
                                (folder_table, !prefs_common.open_inbox_on_inc);
1270
                }
1271
                g_hash_table_destroy(folder_table);
1272
        } else if (!prefs_common.scan_all_after_inc) {
1273
                folder_item_scan(dest);
1274
                folderview_update_item(dest, TRUE);
1275
        }
1276
1277
        return msgs;
1278
}
1279
1280
void inc_lock(void)
1281
{
1282
        inc_lock_count++;
1283
}
1284
1285
void inc_unlock(void)
1286
{
1287
        if (inc_lock_count > 0)
1288
                inc_lock_count--;
1289
}
1290
1291
static guint autocheck_timer = 0;
1292
static gpointer autocheck_data = NULL;
1293
1294
void inc_autocheck_timer_init(MainWindow *mainwin)
1295
{
1296
        autocheck_data = mainwin;
1297
        inc_autocheck_timer_set();
1298
}
1299
1300
static void inc_autocheck_timer_set_interval(guint interval)
1301
{
1302
        inc_autocheck_timer_remove();
1303
1304
        if (prefs_common.autochk_newmail && autocheck_data) {
1305
                autocheck_timer = gtk_timeout_add
1306
                        (interval, inc_autocheck_func, autocheck_data);
1307
                debug_print("added timer = %d\n", autocheck_timer);
1308
        }
1309
}
1310
1311
void inc_autocheck_timer_set(void)
1312
{
1313
        inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1314
}
1315
1316
void inc_autocheck_timer_remove(void)
1317
{
1318
        if (autocheck_timer) {
1319
                debug_print("removed timer = %d\n", autocheck_timer);
1320
                gtk_timeout_remove(autocheck_timer);
1321
                autocheck_timer = 0;
1322
        }
1323
}
1324
1325
static gint inc_autocheck_func(gpointer data)
1326
{
1327
        MainWindow *mainwin = (MainWindow *)data;
1328
1329
        if (inc_lock_count) {
1330
                debug_print("autocheck is locked.\n");
1331
                inc_autocheck_timer_set_interval(1000);
1332
                return FALSE;
1333
        }
1334
1335
        inc_all_account_mail(mainwin, TRUE);
1336
1337
        return FALSE;
1338
}