Statistics
| Revision:

root / src / main.c @ 424

History | View | Annotate | Download (21.3 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2005 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 <glib/gi18n.h>
28
#include <gtk/gtkmain.h>
29
#include <gtk/gtkrc.h>
30
#include <gtk/gtkstock.h>
31
#include <gtk/gtkaccelmap.h>
32

    
33
#include <stdio.h>
34
#include <stdlib.h>
35
#include <string.h>
36
#include <ctype.h>
37
#include <unistd.h>
38
#include <time.h>
39
#include <sys/stat.h>
40
#include <sys/types.h>
41
#include <signal.h>
42
#include <dirent.h>
43

    
44
#if HAVE_LOCALE_H
45
#  include <locale.h>
46
#endif
47

    
48
#if USE_GPGME
49
#  include <gpgme.h>
50
#endif
51

    
52
#include "main.h"
53
#include "mainwindow.h"
54
#include "folderview.h"
55
#include "summaryview.h"
56
#include "prefs_common.h"
57
#include "prefs_account.h"
58
#include "prefs_actions.h"
59
#include "prefs_display_header.h"
60
#include "account.h"
61
#include "procmsg.h"
62
#include "filter.h"
63
#include "inc.h"
64
#include "import.h"
65
#include "manage_window.h"
66
#include "alertpanel.h"
67
#include "statusbar.h"
68
#include "addressbook.h"
69
#include "addrindex.h"
70
#include "compose.h"
71
#include "folder.h"
72
#include "setup.h"
73
#include "utils.h"
74
#include "gtkutils.h"
75
#include "socket.h"
76
#include "stock_pixmap.h"
77

    
78
#if USE_GPGME
79
#  include "rfc2015.h"
80
#endif
81
#if USE_SSL
82
#  include "ssl.h"
83
#endif
84

    
85
#include "version.h"
86

    
87
gchar *prog_version;
88
gchar *startup_dir;
89
gboolean debug_mode = FALSE;
90

    
91
static gint lock_socket = -1;
92
static gint lock_socket_tag = 0;
93

    
94
static struct RemoteCmd {
95
        gboolean receive;
96
        gboolean receive_all;
97
        gboolean compose;
98
        const gchar *compose_mailto;
99
        GPtrArray *attach_files;
100
        gboolean status;
101
        gboolean status_full;
102
        GPtrArray *status_folders;
103
        GPtrArray *status_full_folders;
104
        gboolean send;
105
} cmd;
106

    
107
static void parse_cmd_opt(int argc, char *argv[]);
108

    
109
#if 0
110
#if USE_GPGME
111
static void idle_function_for_gpgme(void);
112
#endif /* USE_GPGME */
113
#endif /* 0 */
114

    
115
static gint prohibit_duplicate_launch        (void);
116
static gint lock_socket_remove                (void);
117
static void lock_socket_input_cb        (gpointer           data,
118
                                         gint                   source,
119
                                         GdkInputCondition condition);
120
static gchar *get_socket_name                (void);
121

    
122
static void migrate_old_config                (void);
123

    
124
static void open_compose_new                (const gchar        *address,
125
                                         GPtrArray        *attach_files);
126

    
127
static void send_queue                        (void);
128

    
129
#define MAKE_DIR_IF_NOT_EXIST(dir) \
130
{ \
131
        if (!is_dir_exist(dir)) { \
132
                if (is_file_exist(dir)) { \
133
                        alertpanel_warning \
134
                                (_("File `%s' already exists.\n" \
135
                                   "Can't create folder."), \
136
                                 dir); \
137
                        return 1; \
138
                } \
139
                if (make_dir(dir) < 0) \
140
                        return 1; \
141
        } \
142
}
143

    
144
int main(int argc, char *argv[])
145
{
146
        gchar *userrc;
147
        MainWindow *mainwin;
148
        FolderView *folderview;
149
        GdkPixbuf *icon;
150

    
151
        setlocale(LC_ALL, "");
152
        bindtextdomain(PACKAGE, LOCALEDIR);
153
        bind_textdomain_codeset(PACKAGE, CS_UTF_8);
154
        textdomain(PACKAGE);
155

    
156
        prog_version = PROG_VERSION;
157
        startup_dir = g_get_current_dir();
158

    
159
        parse_cmd_opt(argc, argv);
160

    
161
        /* check and create unix domain socket for remote operation */
162
        lock_socket = prohibit_duplicate_launch();
163
        if (lock_socket < 0) return 0;
164

    
165
        if (cmd.status || cmd.status_full) {
166
                puts("0 Sylpheed not running.");
167
                lock_socket_remove();
168
                return 0;
169
        }
170

    
171
        gtk_set_locale();
172
        gtk_init(&argc, &argv);
173

    
174
        gdk_rgb_init();
175
        gtk_widget_set_default_colormap(gdk_rgb_get_cmap());
176
        gtk_widget_set_default_visual(gdk_rgb_get_visual());
177

    
178
#if USE_THREADS || USE_LDAP
179
        g_thread_init(NULL);
180
        if (!g_thread_supported())
181
                g_error(_("g_thread is not supported by glib.\n"));
182
#endif
183

    
184
#if USE_SSL
185
        ssl_init();
186
#endif
187

    
188
        srandom((gint)time(NULL));
189

    
190
        /* parse gtkrc files */
191
        userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
192
                             NULL);
193
        gtk_rc_parse(userrc);
194
        g_free(userrc);
195
        userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
196
                             G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
197
        gtk_rc_parse(userrc);
198
        g_free(userrc);
199
        userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
200
        gtk_rc_parse(userrc);
201
        g_free(userrc);
202

    
203
        userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
204
        gtk_accel_map_load(userrc);
205
        g_free(userrc);
206

    
207
        CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
208

    
209
        /* backup if old rc file exists */
210
        if (is_file_exist(RC_DIR)) {
211
                if (rename(RC_DIR, RC_DIR ".bak") < 0)
212
                        FILE_OP_ERROR(RC_DIR, "rename");
213
        }
214

    
215
        /* migration from ~/.sylpheed to ~/.sylpheed-2.0 */
216
        if (!is_dir_exist(RC_DIR)) {
217
                const gchar *envstr;
218
                AlertValue val;
219

    
220
                /* check for filename encoding */
221
                if (conv_get_locale_charset() != C_UTF_8) {
222
                        envstr = g_getenv("G_FILENAME_ENCODING");
223
                        if (!envstr)
224
                                envstr = g_getenv("G_BROKEN_FILENAMES");
225
                        if (!envstr) {
226
                                val = alertpanel(_("Filename encoding"),
227
                                                 _("The locale encoding is not UTF-8, but the environmental variable G_FILENAME_ENCODING is not set.\n"
228
                                                   "If the locale encoding is used for file name or directory name, it will not work correctly.\n"
229
                                                   "In that case, you must set the following environmental variable (see README for detail):\n"
230
                                                   "\n"
231
                                                   "\tG_FILENAME_ENCODING=@locale\n"
232
                                                   "\n"
233
                                                   "Continue?"),
234
                                                 GTK_STOCK_OK, GTK_STOCK_QUIT,
235
                                                 NULL);
236
                                if (G_ALERTDEFAULT != val)
237
                                        return 1;
238
                        }
239
                }
240
                if (make_dir(RC_DIR) < 0)
241
                        return 1;
242
                if (is_dir_exist(OLD_RC_DIR))
243
                        migrate_old_config();
244
        }
245

    
246
        MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
247
        MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
248
        MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
249
        MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
250
        MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S UIDL_DIR);
251

    
252
        if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
253
                if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
254
                           RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
255
                        FILE_OP_ERROR("sylpheed.log", "rename");
256
        }
257
        set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
258

    
259
        prefs_common_read_config();
260

    
261
#if USE_GPGME
262
        if (gpgme_check_version("0.4.5")) {  /* Also does some gpgme init */
263
        gpgme_engine_info_t engineInfo;
264
                rfc2015_disable_all();
265

    
266
        gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
267
        gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
268

    
269
        if (!gpgme_get_engine_info(&engineInfo)) {
270
            while (engineInfo) {
271
                debug_print("GpgME Protocol: %s\n      Version: %s\n",
272
                    gpgme_get_protocol_name(engineInfo->protocol),
273
                    engineInfo->version);
274
                engineInfo = engineInfo->next;
275
            }
276
        }
277
    } else {
278
                if (prefs_common.gpg_warning) {
279
                        AlertValue val;
280

    
281
                        val = alertpanel_message_with_disable
282
                                (_("Warning"),
283
                                 _("GnuPG is not installed properly, or its version is too old.\n"
284
                                   "OpenPGP support disabled."),
285
                                 ALERT_WARNING);
286
                        if (val & G_ALERTDISABLE)
287
                                prefs_common.gpg_warning = FALSE;
288
                }
289
        }
290
    /* FIXME: This function went away.  We can either block until gpgme
291
     * operations finish (currently implemented) or register callbacks
292
     * with the gtk main loop via the gpgme io callback interface instead.
293
     *
294
         * gpgme_register_idle(idle_function_for_gpgme);
295
     */
296
#endif
297

    
298
        sock_set_io_timeout(prefs_common.io_timeout_secs);
299

    
300
        prefs_filter_read_config();
301
        prefs_actions_read_config();
302
        prefs_display_header_read_config();
303

    
304
        gtkut_widget_init();
305
        stock_pixbuf_gdk(NULL, STOCK_PIXMAP_SYLPHEED, &icon);
306
        gtk_window_set_default_icon(icon);
307

    
308
        mainwin = main_window_create
309
                (prefs_common.sep_folder | prefs_common.sep_msg << 1);
310
        folderview = mainwin->folderview;
311

    
312
        /* register the callback of unix domain socket input */
313
        lock_socket_tag = gdk_input_add(lock_socket,
314
                                        GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
315
                                        lock_socket_input_cb,
316
                                        mainwin);
317

    
318
        account_read_config_all();
319

    
320
        if (folder_read_list() < 0) {
321
                setup(mainwin);
322
                folder_write_list();
323
        }
324
        if (!account_get_list()) {
325
                account_edit_open();
326
                account_add();
327
        }
328

    
329
        account_set_missing_folder();
330
        folder_set_missing_folders();
331
        folderview_set(folderview);
332

    
333
        addressbook_read_file();
334

    
335
        inc_autocheck_timer_init(mainwin);
336

    
337
        /* ignore SIGPIPE signal for preventing sudden death of program */
338
        signal(SIGPIPE, SIG_IGN);
339

    
340
        if (cmd.receive_all)
341
                inc_all_account_mail(mainwin, FALSE);
342
        else if (prefs_common.chk_on_startup)
343
                inc_all_account_mail(mainwin, TRUE);
344
        else if (cmd.receive)
345
                inc_mail(mainwin);
346
        else
347
                gtk_widget_grab_focus(folderview->treeview);
348

    
349
        if (cmd.compose)
350
                open_compose_new(cmd.compose_mailto, cmd.attach_files);
351
        if (cmd.attach_files) {
352
                ptr_array_free_strings(cmd.attach_files);
353
                g_ptr_array_free(cmd.attach_files, TRUE);
354
                cmd.attach_files = NULL;
355
        }
356
        if (cmd.send)
357
                send_queue();
358
        if (cmd.status_folders) {
359
                g_ptr_array_free(cmd.status_folders, TRUE);
360
                cmd.status_folders = NULL;
361
        }
362
        if (cmd.status_full_folders) {
363
                g_ptr_array_free(cmd.status_full_folders, TRUE);
364
                cmd.status_full_folders = NULL;
365
        }
366

    
367
        gtk_main();
368

    
369
        return 0;
370
}
371

    
372
static void parse_cmd_opt(int argc, char *argv[])
373
{
374
        gint i;
375

    
376
        for (i = 1; i < argc; i++) {
377
                if (!strncmp(argv[i], "--debug", 7))
378
                        debug_mode = TRUE;
379
                else if (!strncmp(argv[i], "--receive-all", 13))
380
                        cmd.receive_all = TRUE;
381
                else if (!strncmp(argv[i], "--receive", 9))
382
                        cmd.receive = TRUE;
383
                else if (!strncmp(argv[i], "--compose", 9)) {
384
                        const gchar *p = argv[i + 1];
385

    
386
                        cmd.compose = TRUE;
387
                        cmd.compose_mailto = NULL;
388
                        if (p && *p != '\0' && *p != '-') {
389
                                if (!strncmp(p, "mailto:", 7))
390
                                        cmd.compose_mailto = p + 7;
391
                                else
392
                                        cmd.compose_mailto = p;
393
                                i++;
394
                        }
395
                } else if (!strncmp(argv[i], "--attach", 8)) {
396
                        const gchar *p = argv[i + 1];
397
                        gchar *file;
398

    
399
                        while (p && *p != '\0' && *p != '-') {
400
                                if (!cmd.attach_files)
401
                                        cmd.attach_files = g_ptr_array_new();
402
                                if (*p != G_DIR_SEPARATOR)
403
                                        file = g_strconcat(startup_dir,
404
                                                           G_DIR_SEPARATOR_S,
405
                                                           p, NULL);
406
                                else
407
                                        file = g_strdup(p);
408
                                g_ptr_array_add(cmd.attach_files, file);
409
                                i++;
410
                                p = argv[i + 1];
411
                        }
412
                } else if (!strncmp(argv[i], "--send", 6)) {
413
                        cmd.send = TRUE;
414
                } else if (!strncmp(argv[i], "--version", 9)) {
415
                        puts("Sylpheed version " VERSION);
416
                        exit(0);
417
                } else if (!strncmp(argv[i], "--status-full", 13)) {
418
                        const gchar *p = argv[i + 1];
419

    
420
                        cmd.status_full = TRUE;
421
                        while (p && *p != '\0' && *p != '-') {
422
                                if (!cmd.status_full_folders)
423
                                        cmd.status_full_folders =
424
                                                g_ptr_array_new();
425
                                g_ptr_array_add(cmd.status_full_folders,
426
                                                g_strdup(p));
427
                                i++;
428
                                p = argv[i + 1];
429
                        }
430
                } else if (!strncmp(argv[i], "--status", 8)) {
431
                        const gchar *p = argv[i + 1];
432

    
433
                        cmd.status = TRUE;
434
                        while (p && *p != '\0' && *p != '-') {
435
                                if (!cmd.status_folders)
436
                                        cmd.status_folders = g_ptr_array_new();
437
                                g_ptr_array_add(cmd.status_folders,
438
                                                g_strdup(p));
439
                                i++;
440
                                p = argv[i + 1];
441
                        }
442
                } else if (!strncmp(argv[i], "--help", 6)) {
443
                        g_print(_("Usage: %s [OPTION]...\n"),
444
                                g_basename(argv[0]));
445

    
446
                        g_print("%s\n", _("  --compose [address]    open composition window"));
447
                        g_print("%s\n", _("  --attach file1 [file2]...\n"
448
                                "                         open composition window with specified files\n"
449
                                "                         attached"));
450
                        g_print("%s\n", _("  --receive              receive new messages"));
451
                        g_print("%s\n", _("  --receive-all          receive new messages of all accounts"));
452
                        g_print("%s\n", _("  --send                 send all queued messages"));
453
                        g_print("%s\n", _("  --status [folder]...   show the total number of messages"));
454
                        g_print("%s\n", _("  --status-full [folder]...\n"
455
                                "                         show the status of each folder"));
456
                        g_print("%s\n", _("  --debug                debug mode"));
457
                        g_print("%s\n", _("  --help                 display this help and exit"));
458
                        g_print("%s\n", _("  --version              output version information and exit"));
459

    
460
                        exit(1);
461
                }
462
        }
463

    
464
        if (cmd.attach_files && cmd.compose == FALSE) {
465
                cmd.compose = TRUE;
466
                cmd.compose_mailto = NULL;
467
        }
468
}
469

    
470
static gint get_queued_message_num(void)
471
{
472
        FolderItem *queue;
473

    
474
        queue = folder_get_default_queue();
475
        if (!queue) return -1;
476

    
477
        folder_item_scan(queue);
478
        return queue->total;
479
}
480

    
481
void app_will_exit(GtkWidget *widget, gpointer data)
482
{
483
        MainWindow *mainwin = data;
484
        gchar *filename;
485

    
486
        if (compose_get_compose_list()) {
487
                if (alertpanel(_("Notice"),
488
                               _("Composing message exists. Really quit?"),
489
                               GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL)
490
                    != G_ALERTDEFAULT)
491
                        return;
492
                manage_window_focus_in(mainwin->window, NULL, NULL);
493
        }
494

    
495
        if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
496
                if (alertpanel(_("Queued messages"),
497
                               _("Some unsent messages are queued. Exit now?"),
498
                               GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL)
499
                    != G_ALERTDEFAULT)
500
                        return;
501
                manage_window_focus_in(mainwin->window, NULL, NULL);
502
        }
503

    
504
        inc_autocheck_timer_remove();
505

    
506
        if (prefs_common.clean_on_exit)
507
                main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
508

    
509
        /* save all state before exiting */
510
        folder_write_list();
511
        summary_write_cache(mainwin->summaryview);
512

    
513
        main_window_get_size(mainwin);
514
        main_window_get_position(mainwin);
515
        prefs_common_write_config();
516
        prefs_filter_write_config();
517
        account_write_config_all();
518
        addressbook_export_to_file();
519

    
520
        filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
521
        gtk_accel_map_save(filename);
522
        g_free(filename);
523

    
524
        /* delete temporary files */
525
        remove_all_files(get_tmp_dir());
526
        remove_all_files(get_mime_tmp_dir());
527

    
528
        close_log_file();
529
        lock_socket_remove();
530

    
531
#if USE_SSL
532
        ssl_done();
533
#endif
534

    
535
        gtk_main_quit();
536
}
537

    
538
#if 0
539
#if USE_GPGME
540
static void idle_function_for_gpgme(void)
541
{
542
        while (gtk_events_pending())
543
                gtk_main_iteration();
544
}
545
#endif /* USE_GPGME */
546
#endif /* 0 */
547

    
548
static gchar *get_socket_name(void)
549
{
550
        static gchar *filename = NULL;
551

    
552
        if (filename == NULL) {
553
                filename = g_strdup_printf("%s%csylpheed-%d",
554
                                           g_get_tmp_dir(), G_DIR_SEPARATOR,
555
                                           getuid());
556
        }
557

    
558
        return filename;
559
}
560

    
561
static gint prohibit_duplicate_launch(void)
562
{
563
        gint uxsock;
564
        gchar *path;
565

    
566
        path = get_socket_name();
567
        uxsock = fd_connect_unix(path);
568
        if (uxsock < 0) {
569
                unlink(path);
570
                return fd_open_unix(path);
571
        }
572

    
573
        /* remote command mode */
574

    
575
        debug_print(_("another Sylpheed is already running.\n"));
576

    
577
        if (cmd.receive_all)
578
                fd_write_all(uxsock, "receive_all\n", 12);
579
        else if (cmd.receive)
580
                fd_write_all(uxsock, "receive\n", 8);
581
        else if (cmd.compose && cmd.attach_files) {
582
                gchar *str, *compose_str;
583
                gint i;
584

    
585
                if (cmd.compose_mailto)
586
                        compose_str = g_strdup_printf("compose_attach %s\n",
587
                                                      cmd.compose_mailto);
588
                else
589
                        compose_str = g_strdup("compose_attach\n");
590

    
591
                fd_write_all(uxsock, compose_str, strlen(compose_str));
592
                g_free(compose_str);
593

    
594
                for (i = 0; i < cmd.attach_files->len; i++) {
595
                        str = g_ptr_array_index(cmd.attach_files, i);
596
                        fd_write_all(uxsock, str, strlen(str));
597
                        fd_write_all(uxsock, "\n", 1);
598
                }
599

    
600
                fd_write_all(uxsock, ".\n", 2);
601
        } else if (cmd.compose) {
602
                gchar *compose_str;
603

    
604
                if (cmd.compose_mailto)
605
                        compose_str = g_strdup_printf
606
                                ("compose %s\n", cmd.compose_mailto);
607
                else
608
                        compose_str = g_strdup("compose\n");
609

    
610
                fd_write_all(uxsock, compose_str, strlen(compose_str));
611
                g_free(compose_str);
612
        } else if (cmd.send) {
613
                fd_write_all(uxsock, "send\n", 5);
614
        } else if (cmd.status || cmd.status_full) {
615
                gchar buf[BUFFSIZE];
616
                gint i;
617
                const gchar *command;
618
                GPtrArray *folders;
619
                gchar *folder;
620

    
621
                command = cmd.status_full ? "status-full\n" : "status\n";
622
                folders = cmd.status_full ? cmd.status_full_folders :
623
                        cmd.status_folders;
624

    
625
                fd_write_all(uxsock, command, strlen(command));
626
                for (i = 0; folders && i < folders->len; ++i) {
627
                        folder = g_ptr_array_index(folders, i);
628
                        fd_write_all(uxsock, folder, strlen(folder));
629
                        fd_write_all(uxsock, "\n", 1);
630
                }
631
                fd_write_all(uxsock, ".\n", 2);
632
                for (;;) {
633
                        fd_gets(uxsock, buf, sizeof(buf));
634
                        if (!strncmp(buf, ".\n", 2)) break;
635
                        fputs(buf, stdout);
636
                }
637
        } else
638
                fd_write_all(uxsock, "popup\n", 6);
639

    
640
        fd_close(uxsock);
641
        return -1;
642
}
643

    
644
static gint lock_socket_remove(void)
645
{
646
        gchar *filename;
647

    
648
        if (lock_socket < 0) return -1;
649

    
650
        if (lock_socket_tag > 0)
651
                gdk_input_remove(lock_socket_tag);
652
        fd_close(lock_socket);
653
        filename = get_socket_name();
654
        unlink(filename);
655

    
656
        return 0;
657
}
658

    
659
static GPtrArray *get_folder_item_list(gint sock)
660
{
661
        gchar buf[BUFFSIZE];
662
        FolderItem *item;
663
        GPtrArray *folders = NULL;
664

    
665
        for (;;) {
666
                fd_gets(sock, buf, sizeof(buf));
667
                if (!strncmp(buf, ".\n", 2)) break;
668
                strretchomp(buf);
669
                if (!folders) folders = g_ptr_array_new();
670
                item = folder_find_item_from_identifier(buf);
671
                if (item)
672
                        g_ptr_array_add(folders, item);
673
                else
674
                        g_warning("no such folder: %s\n", buf);
675
        }
676

    
677
        return folders;
678
}
679

    
680
static void lock_socket_input_cb(gpointer data,
681
                                 gint source,
682
                                 GdkInputCondition condition)
683
{
684
        MainWindow *mainwin = (MainWindow *)data;
685
        gint sock;
686
        gchar buf[BUFFSIZE];
687

    
688
        sock = fd_accept(source);
689
        fd_gets(sock, buf, sizeof(buf));
690

    
691
        if (!strncmp(buf, "popup", 5)) {
692
                main_window_popup(mainwin);
693
        } else if (!strncmp(buf, "receive_all", 11)) {
694
                main_window_popup(mainwin);
695
                inc_all_account_mail(mainwin, FALSE);
696
        } else if (!strncmp(buf, "receive", 7)) {
697
                main_window_popup(mainwin);
698
                inc_mail(mainwin);
699
        } else if (!strncmp(buf, "compose_attach", 14)) {
700
                GPtrArray *files;
701
                gchar *mailto;
702

    
703
                mailto = g_strdup(buf + strlen("compose_attach") + 1);
704
                files = g_ptr_array_new();
705
                while (fd_gets(sock, buf, sizeof(buf)) > 0) {
706
                        if (buf[0] == '.' && buf[1] == '\n') break;
707
                        strretchomp(buf);
708
                        g_ptr_array_add(files, g_strdup(buf));
709
                }
710
                open_compose_new(mailto, files);
711
                ptr_array_free_strings(files);
712
                g_ptr_array_free(files, TRUE);
713
                g_free(mailto);
714
        } else if (!strncmp(buf, "compose", 7)) {
715
                open_compose_new(buf + strlen("compose") + 1, NULL);
716
        } else if (!strncmp(buf, "send", 4)) {
717
                send_queue();
718
        } else if (!strncmp(buf, "status-full", 11) ||
719
                   !strncmp(buf, "status", 6)) {
720
                gchar *status;
721
                GPtrArray *folders;
722

    
723
                folders = get_folder_item_list(sock);
724
                status = folder_get_status
725
                        (folders, !strncmp(buf, "status-full", 11));
726
                fd_write_all(sock, status, strlen(status));
727
                fd_write_all(sock, ".\n", 2);
728
                g_free(status);
729
                if (folders) g_ptr_array_free(folders, TRUE);
730
        }
731

    
732
        fd_close(sock);
733
}
734

    
735
static void migrate_old_config(void)
736
{
737
        DIR *dp;
738
        struct dirent *d;
739
        GPatternSpec *pspec;
740

    
741
        if (alertpanel(_("Migration of configuration"),
742
                       _("The previous version of configuration found.\n"
743
                         "Do you want to migrate it?"),
744
                       GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT)
745
                return;
746

    
747
        debug_print("Migrating old configuration...\n");
748

    
749
#define COPY_FILE(rcfile)                                                \
750
        if (is_file_exist(OLD_RC_DIR G_DIR_SEPARATOR_S rcfile)) {        \
751
                conv_copy_file(OLD_RC_DIR G_DIR_SEPARATOR_S rcfile,        \
752
                               RC_DIR G_DIR_SEPARATOR_S rcfile,                \
753
                               conv_get_locale_charset_str());                \
754
        }
755

    
756
        COPY_FILE(ACCOUNT_RC);
757
        COPY_FILE(ACTIONS_RC);
758
        COPY_FILE(COMMON_RC);
759
        COPY_FILE(CUSTOM_HEADER_RC);
760
        COPY_FILE(DISPLAY_HEADER_RC);
761
        COPY_FILE(FILTER_HEADER_RC);
762
        COPY_FILE(COMMAND_HISTORY);
763

    
764
#undef COPY_FILE
765

    
766
        if (is_file_exist(OLD_RC_DIR G_DIR_SEPARATOR_S FILTER_LIST))
767
                copy_file(OLD_RC_DIR G_DIR_SEPARATOR_S FILTER_LIST,
768
                          RC_DIR G_DIR_SEPARATOR_S FILTER_LIST, FALSE);
769
        if (is_file_exist(OLD_RC_DIR G_DIR_SEPARATOR_S FOLDER_LIST))
770
                copy_file(OLD_RC_DIR G_DIR_SEPARATOR_S FOLDER_LIST,
771
                          RC_DIR G_DIR_SEPARATOR_S FOLDER_LIST, FALSE);
772
        if (is_file_exist(OLD_RC_DIR G_DIR_SEPARATOR_S "mime.types"))
773
                copy_file(OLD_RC_DIR G_DIR_SEPARATOR_S "mime.types",
774
                          RC_DIR G_DIR_SEPARATOR_S "mime.types", FALSE);
775

    
776
        if (is_dir_exist(OLD_RC_DIR G_DIR_SEPARATOR_S TEMPLATE_DIR))
777
                conv_copy_dir(OLD_RC_DIR G_DIR_SEPARATOR_S TEMPLATE_DIR,
778
                              RC_DIR G_DIR_SEPARATOR_S TEMPLATE_DIR,
779
                              conv_get_locale_charset_str());
780
        if (is_dir_exist(OLD_RC_DIR G_DIR_SEPARATOR_S UIDL_DIR))
781
                copy_dir(OLD_RC_DIR G_DIR_SEPARATOR_S UIDL_DIR,
782
                         RC_DIR G_DIR_SEPARATOR_S UIDL_DIR);
783

    
784
        if (!is_file_exist(OLD_RC_DIR G_DIR_SEPARATOR_S ADDRESSBOOK_INDEX_FILE))
785
                return;
786

    
787
        if ((dp = opendir(OLD_RC_DIR)) == NULL) {
788
                FILE_OP_ERROR(OLD_RC_DIR, "opendir");
789
                return;
790
        }
791

    
792
        pspec = g_pattern_spec_new("addrbook-*.xml");
793

    
794
        while ((d = readdir(dp)) != NULL) {
795
                if (g_pattern_match_string(pspec, d->d_name)) {
796
                        gchar *old_file;
797
                        gchar *new_file;
798

    
799
                        old_file = g_strconcat(OLD_RC_DIR G_DIR_SEPARATOR_S,
800
                                               d->d_name, NULL);
801
                        new_file = g_strconcat(RC_DIR G_DIR_SEPARATOR_S,
802
                                               d->d_name, NULL);
803
                        copy_file(old_file, new_file, FALSE);
804
                        g_free(new_file);
805
                        g_free(old_file);
806
                }
807
        }
808

    
809
        g_pattern_spec_free(pspec);
810

    
811
        closedir(dp);
812
}
813

    
814
static void open_compose_new(const gchar *address, GPtrArray *attach_files)
815
{
816
        gchar *addr = NULL;
817

    
818
        if (address) {
819
                Xstrdup_a(addr, address, return);
820
                g_strstrip(addr);
821
        }
822

    
823
        compose_new(NULL, NULL, addr, attach_files);
824
}
825

    
826
static void send_queue(void)
827
{
828
        GList *list;
829

    
830
        if (!main_window_toggle_online_if_offline(main_window_get()))
831
                return;
832

    
833
        for (list = folder_get_list(); list != NULL; list = list->next) {
834
                Folder *folder = list->data;
835

    
836
                if (folder->queue) {
837
                        gint ret;
838

    
839
                        ret = procmsg_send_queue(folder->queue,
840
                                                 prefs_common.savemsg,
841
                                                 prefs_common.filter_sent);
842
                        statusbar_pop_all();
843
                        if (ret > 0)
844
                                folder_item_scan(folder->queue);
845
                }
846
        }
847

    
848
        folderview_update_all_updated(TRUE);
849
}