2005-02-25 [paul] 1.0.1cvs15.12
[claws.git] / src / main.c
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
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <unistd.h>
36 #include <time.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <signal.h>
40 #include "wizard.h"
41 #ifdef HAVE_STARTUP_NOTIFICATION
42 # define SN_API_NOT_YET_FROZEN
43 # include <libsn/sn-launchee.h>
44 # include <gdk/gdkx.h>
45 #endif
46
47 #include "sylpheed.h"
48 #include "main.h"
49 #include "mainwindow.h"
50 #include "folderview.h"
51 #include "summaryview.h"
52 #include "prefs_common.h"
53 #include "prefs_account.h"
54 #include "prefs_actions.h"
55 #include "prefs_ext_prog.h"
56 #include "prefs_fonts.h"
57 #include "prefs_msg_colors.h"
58 #include "prefs_spelling.h"
59 #include "prefs_themes.h"
60 #include "prefs_wrapping.h"
61 #include "prefs_display_header.h"
62 #include "account.h"
63 #include "procmsg.h"
64 #include "inc.h"
65 #include "import.h"
66 #include "manage_window.h"
67 #include "alertpanel.h"
68 #include "statusbar.h"
69 #include "addressbook.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 "log.h"
77 #include "prefs_toolbar.h"
78 #include "plugin.h"
79 #include "mh_gtk.h"
80 #include "imap_gtk.h"
81 #include "news_gtk.h"
82 #include "matcher.h"
83
84 #if USE_OPENSSL
85 #  include "ssl.h"
86 #endif
87
88 #include "version.h"
89
90 #include "crash.h"
91
92 gchar *prog_version;
93 #ifdef CRASH_DIALOG
94 gchar *argv0;
95 #endif
96
97 #ifdef HAVE_STARTUP_NOTIFICATION
98 static SnLauncheeContext *sn_context = NULL;
99 static SnDisplay *sn_display = NULL;
100 #endif
101
102 static gint lock_socket = -1;
103 static gint lock_socket_tag = 0;
104
105 typedef enum 
106 {
107         ONLINE_MODE_DONT_CHANGE,
108         ONLINE_MODE_ONLINE,
109         ONLINE_MODE_OFFLINE
110 } OnlineMode;
111
112 static struct RemoteCmd {
113         gboolean receive;
114         gboolean receive_all;
115         gboolean compose;
116         const gchar *compose_mailto;
117         GPtrArray *attach_files;
118         gboolean status;
119         gboolean status_full;
120         GPtrArray *status_folders;
121         GPtrArray *status_full_folders;
122         gboolean send;
123         gboolean crash;
124         int online_mode;
125         gchar   *crash_params;
126 } cmd;
127
128 static void parse_cmd_opt(int argc, char *argv[]);
129
130 static gint prohibit_duplicate_launch   (void);
131 static gchar * get_crashfile_name       (void);
132 static gint lock_socket_remove          (void);
133 static void lock_socket_input_cb        (gpointer          data,
134                                          gint              source,
135                                          GdkInputCondition condition);
136 #ifndef CLAWS                                    
137 static 
138 #endif
139 gchar *get_socket_name          (void);
140
141
142 static void open_compose_new            (const gchar    *address,
143                                          GPtrArray      *attach_files);
144
145 static void send_queue                  (void);
146 static void initial_processing          (FolderItem *item, gpointer data);
147 static void quit_signal_handler         (int sig);
148 static void install_basic_sighandlers   (void);
149 static void exit_sylpheed               (MainWindow *mainwin);
150
151 #if 0
152 /* for gettext */
153 _("File `%s' already exists.\n"
154   "Can't create folder.")
155 #endif
156
157 #define MAKE_DIR_IF_NOT_EXIST(dir) \
158 { \
159         if (!is_dir_exist(dir)) { \
160                 if (is_file_exist(dir)) { \
161                         alertpanel_warning \
162                                 (_("File `%s' already exists.\n" \
163                                    "Can't create folder."), \
164                                  dir); \
165                         return 1; \
166                 } \
167                 if (make_dir(dir) < 0) \
168                         return 1; \
169         } \
170 }
171
172 static MainWindow *static_mainwindow;
173
174 #ifdef HAVE_STARTUP_NOTIFICATION
175 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
176 {
177         gdk_error_trap_push();
178 }
179
180 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
181 {
182         gdk_error_trap_pop();
183 }
184
185 static void startup_notification_complete(gboolean with_window)
186 {
187         Display *xdisplay;
188         GtkWidget *hack = NULL;
189
190         if (with_window) {
191                 /* this is needed to make the startup notification leave,
192                  * if we have been launched from a menu.
193                  * We have to display a window, so let it be very little */
194                 hack = gtk_window_new(GTK_WINDOW_POPUP);
195                 gtk_widget_set_uposition(hack, 0, 0);
196                 gtk_widget_set_usize(hack, 1, 1);
197                 gtk_widget_show(hack);
198         }
199
200         xdisplay = GDK_DISPLAY();
201         sn_display = sn_display_new(xdisplay,
202                                 sn_error_trap_push,
203                                 sn_error_trap_pop);
204         sn_context = sn_launchee_context_new_from_environment(sn_display,
205                                                  DefaultScreen(xdisplay));
206
207         if (sn_context != NULL)
208         {
209                 sn_launchee_context_complete(sn_context);
210                 sn_launchee_context_unref(sn_context);
211                 sn_display_unref(sn_display);
212         }
213 }
214 #endif /* HAVE_STARTUP_NOTIFICATION */
215
216 void sylpheed_gtk_idle(void) 
217 {
218         while(gtk_events_pending())
219                 gtk_main_iteration();
220 }
221
222 int main(int argc, char *argv[])
223 {
224         gchar *userrc;
225         MainWindow *mainwin;
226         FolderView *folderview;
227
228         if (!sylpheed_init(&argc, &argv)) {
229                 return 0;
230         }
231
232         prog_version = PROG_VERSION;
233 #ifdef CRASH_DIALOG
234         argv0 = g_strdup(argv[0]);
235 #endif
236
237         parse_cmd_opt(argc, argv);
238
239 #ifdef CRASH_DIALOG
240         if (cmd.crash) {
241                 gtk_set_locale();
242                 gtk_init(&argc, &argv);
243                 crash_main(cmd.crash_params);
244                 return 0;
245         }
246         crash_install_handlers();
247 #endif
248         install_basic_sighandlers();
249
250         /* check and create unix domain socket */
251         lock_socket = prohibit_duplicate_launch();
252         if (lock_socket < 0) {
253 #ifdef HAVE_STARTUP_NOTIFICATION
254                 if(gtk_init_check(&argc, &argv))
255                         startup_notification_complete(TRUE);
256 #endif
257                 return 0;
258         }
259
260         if (cmd.status || cmd.status_full) {
261                 puts("0 Sylpheed not running.");
262                 lock_socket_remove();
263                 return 0;
264         }
265
266         gtk_set_locale();
267         gtk_init(&argc, &argv);
268
269         gdk_rgb_init();
270         gtk_widget_set_default_colormap(gdk_rgb_get_cmap());
271         gtk_widget_set_default_visual(gdk_rgb_get_visual());
272
273 #if USE_THREADS || USE_LDAP
274         g_thread_init(NULL);
275         if (!g_thread_supported())
276                 g_error(_("g_thread is not supported by glib.\n"));
277 #endif
278
279         /* parse gtkrc files */
280         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
281                              NULL);
282         gtk_rc_parse(userrc);
283         g_free(userrc);
284         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
285                              G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
286         gtk_rc_parse(userrc);
287         g_free(userrc);
288         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
289         gtk_rc_parse(userrc);
290         g_free(userrc);
291
292         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
293         gtk_accel_map_load (userrc);
294         g_free(userrc);
295
296         gtk_settings_set_long_property(gtk_settings_get_default(), 
297                                        "gtk-can-change-accels",
298                                        (glong)TRUE, "XProperty");
299
300         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
301
302         MAKE_DIR_IF_NOT_EXIST(RC_DIR);
303         MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
304         MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
305         MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
306         MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
307         MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
308
309         set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
310
311         folder_system_init();
312         prefs_common_init();
313         prefs_common_read_config();
314
315         prefs_themes_init();
316         prefs_fonts_init();
317         prefs_ext_prog_init();
318         prefs_wrapping_init();
319         prefs_msg_colors_init();
320 #ifdef USE_ASPELL
321         gtkaspell_checkers_init();
322         prefs_spelling_init();
323 #endif
324         
325         sock_set_io_timeout(prefs_common.io_timeout_secs);
326
327         prefs_actions_read_config();
328         prefs_display_header_read_config();
329         /* prefs_filtering_read_config(); */
330         addressbook_read_file();
331         renderer_read_config();
332
333         gtkut_widget_init();
334
335         folderview_initialize();
336         mh_gtk_init();
337         imap_gtk_init();
338         news_gtk_init();
339                 
340         mainwin = main_window_create
341                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
342         folderview = mainwin->folderview;
343
344         /* register the callback of unix domain socket input */
345         lock_socket_tag = gdk_input_add(lock_socket,
346                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
347                                         lock_socket_input_cb,
348                                         mainwin);
349
350         prefs_account_init();
351         account_read_config_all();
352
353         if (folder_read_list() < 0) {
354                 if (!run_wizard(mainwin, TRUE))
355                         exit(1);
356                 folder_write_list();
357         }
358
359         if (!account_get_list()) {
360                 if (!run_wizard(mainwin, FALSE))
361                         exit(1);
362                 account_read_config_all();
363                 if(!account_get_list())
364                         exit_sylpheed(mainwin);
365         }
366
367         account_set_missing_folder();
368         folder_set_missing_folders();
369         folderview_set(folderview);
370
371         prefs_matcher_read_config();
372
373         /* make one all-folder processing before using sylpheed */
374         main_window_cursor_wait(mainwin);
375         folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
376         main_window_cursor_normal(mainwin);
377
378         /* if Sylpheed crashed, rebuild caches */
379         if (!cmd.crash && is_file_exist(get_crashfile_name())) {
380                 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
381                 folderview_check_new(NULL);
382         }
383         /* make the crash-indicator file */
384         str_write_to_file("foo", get_crashfile_name());
385
386         addressbook_read_file();
387
388         inc_autocheck_timer_init(mainwin);
389
390         /* ignore SIGPIPE signal for preventing sudden death of program */
391         signal(SIGPIPE, SIG_IGN);
392
393         if (cmd.online_mode == ONLINE_MODE_OFFLINE)
394                 main_window_toggle_work_offline(mainwin, TRUE);
395         if (cmd.online_mode == ONLINE_MODE_ONLINE)
396                 main_window_toggle_work_offline(mainwin, FALSE);
397
398         if (cmd.status_folders) {
399                 g_ptr_array_free(cmd.status_folders, TRUE);
400                 cmd.status_folders = NULL;
401         }
402         if (cmd.status_full_folders) {
403                 g_ptr_array_free(cmd.status_full_folders, TRUE);
404                 cmd.status_full_folders = NULL;
405         }
406
407         sylpheed_register_idle_function(sylpheed_gtk_idle);
408
409         prefs_toolbar_init();
410
411         plugin_load_all("GTK2");
412         
413         static_mainwindow = mainwin;
414
415 #ifdef HAVE_STARTUP_NOTIFICATION
416         startup_notification_complete(FALSE);
417 #endif
418
419         if (cmd.receive_all)
420                 inc_all_account_mail(mainwin, FALSE, 
421                                      prefs_common.newmail_notify_manu);
422         else if (prefs_common.chk_on_startup)
423                 inc_all_account_mail(mainwin, TRUE, 
424                                      prefs_common.newmail_notify_manu);
425         else if (cmd.receive)
426                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
427         else
428                 gtk_widget_grab_focus(folderview->ctree);
429
430         if (cmd.compose)
431                 open_compose_new(cmd.compose_mailto, cmd.attach_files);
432         if (cmd.attach_files) {
433                 ptr_array_free_strings(cmd.attach_files);
434                 g_ptr_array_free(cmd.attach_files, TRUE);
435                 cmd.attach_files = NULL;
436         }
437         if (cmd.send)
438                 send_queue();
439         
440         gtk_main();
441
442         exit_sylpheed(mainwin);
443
444         return 0;
445 }
446
447 static void save_all_caches(FolderItem *item, gpointer data)
448 {
449         if (!item->cache)
450                 return;
451         folder_item_write_cache(item);
452 }
453
454 static void exit_sylpheed(MainWindow *mainwin)
455 {
456         gchar *filename;
457
458         debug_print("shutting down\n");
459
460         inc_autocheck_timer_remove();
461
462         if (prefs_common.clean_on_exit)
463                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
464
465         /* save prefs for opened folder */
466         if(mainwin->folderview->opened)
467         {
468                 FolderItem *item;
469
470                 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
471                 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
472         }
473
474         /* save all state before exiting */
475         folder_write_list();
476         folder_func_to_all_folders(save_all_caches, NULL);
477
478         main_window_get_size(mainwin);
479         main_window_get_position(mainwin);
480         prefs_common_write_config();
481         account_write_config_all();
482         addressbook_export_to_file();
483
484         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
485         gtk_accel_map_save(filename);
486         g_free(filename);
487
488         /* delete temporary files */
489         remove_all_files(get_mime_tmp_dir());
490
491         close_log_file();
492
493         /* delete crashfile */
494         if (!cmd.crash)
495                 unlink(get_crashfile_name());
496
497         lock_socket_remove();
498
499         main_window_destroy(mainwin);
500         
501         plugin_unload_all("GTK2");
502
503         prefs_toolbar_done();
504
505         addressbook_destroy();
506
507         prefs_themes_done();
508         prefs_fonts_done();
509         prefs_ext_prog_done();
510         prefs_wrapping_done();
511         prefs_msg_colors_done();
512 #ifdef USE_ASPELL       
513         prefs_spelling_done();
514         gtkaspell_checkers_quit();
515 #endif
516         sylpheed_done();
517
518 }
519
520 static void parse_cmd_opt(int argc, char *argv[])
521 {
522         gint i;
523
524         for (i = 1; i < argc; i++) {
525                 if (!strncmp(argv[i], "--receive-all", 13))
526                         cmd.receive_all = TRUE;
527                 else if (!strncmp(argv[i], "--receive", 9))
528                         cmd.receive = TRUE;
529                 else if (!strncmp(argv[i], "--compose", 9)) {
530                         const gchar *p = argv[i + 1];
531
532                         cmd.compose = TRUE;
533                         cmd.compose_mailto = NULL;
534                         if (p && *p != '\0' && *p != '-') {
535                                 if (!strncmp(p, "mailto:", 7))
536                                         cmd.compose_mailto = p + 7;
537                                 else
538                                         cmd.compose_mailto = p;
539                                 i++;
540                         }
541                 } else if (!strncmp(argv[i], "--attach", 8)) {
542                         const gchar *p = argv[i + 1];
543                         gchar *file;
544
545                         while (p && *p != '\0' && *p != '-') {
546                                 if (!cmd.attach_files)
547                                         cmd.attach_files = g_ptr_array_new();
548                                 if (*p != G_DIR_SEPARATOR)
549                                         file = g_strconcat(sylpheed_get_startup_dir(),
550                                                            G_DIR_SEPARATOR_S,
551                                                            p, NULL);
552                                 else
553                                         file = g_strdup(p);
554                                 g_ptr_array_add(cmd.attach_files, file);
555                                 i++;
556                                 p = argv[i + 1];
557                         }
558                 } else if (!strncmp(argv[i], "--send", 6)) {
559                         cmd.send = TRUE;
560                 } else if (!strncmp(argv[i], "--version", 9)) {
561                         puts("Sylpheed version " VERSION);
562                         exit(0);
563                 } else if (!strncmp(argv[i], "--status-full", 13)) {
564                         const gchar *p = argv[i + 1];
565  
566                         cmd.status_full = TRUE;
567                         while (p && *p != '\0' && *p != '-') {
568                                 if (!cmd.status_full_folders)
569                                         cmd.status_full_folders =
570                                                 g_ptr_array_new();
571                                 g_ptr_array_add(cmd.status_full_folders,
572                                                 g_strdup(p));
573                                 i++;
574                                 p = argv[i + 1];
575                         }
576                 } else if (!strncmp(argv[i], "--status", 8)) {
577                         const gchar *p = argv[i + 1];
578  
579                         cmd.status = TRUE;
580                         while (p && *p != '\0' && *p != '-') {
581                                 if (!cmd.status_folders)
582                                         cmd.status_folders = g_ptr_array_new();
583                                 g_ptr_array_add(cmd.status_folders,
584                                                 g_strdup(p));
585                                 i++;
586                                 p = argv[i + 1];
587                         }
588                 } else if (!strncmp(argv[i], "--online", 8)) {
589                         cmd.online_mode = ONLINE_MODE_ONLINE;
590                 } else if (!strncmp(argv[i], "--offline", 9)) {
591                         cmd.online_mode = ONLINE_MODE_OFFLINE;
592                 } else if (!strncmp(argv[i], "--help", 6)) {
593                         gchar *base = g_path_get_basename(argv[0]);
594                         g_print(_("Usage: %s [OPTION]...\n"), base);
595
596                         g_print("%s\n", _("  --compose [address]    open composition window"));
597                         g_print("%s\n", _("  --attach file1 [file2]...\n"
598                                   "                         open composition window with specified files\n"
599                                   "                         attached"));
600                         g_print("%s\n", _("  --receive              receive new messages"));
601                         g_print("%s\n", _("  --receive-all          receive new messages of all accounts"));
602                         g_print("%s\n", _("  --send                 send all queued messages"));
603                         g_print("%s\n", _("  --status [folder]...   show the total number of messages"));
604                         g_print("%s\n", _("  --status-full [folder]...\n"
605                                "                         show the status of each folder"));
606                         g_print("%s\n", _("  --online               switch to online mode"));
607                         g_print("%s\n", _("  --offline              switch to offline mode"));
608                         g_print("%s\n", _("  --debug                debug mode"));
609                         g_print("%s\n", _("  --help                 display this help and exit"));
610                         g_print("%s\n", _("  --version              output version information and exit"));
611                         g_print("%s\n", _("  --config-dir           output configuration directory"));
612
613                         g_free(base);
614                         exit(1);
615                 } else if (!strncmp(argv[i], "--crash", 7)) {
616                         cmd.crash = TRUE;
617                         cmd.crash_params = g_strdup(argv[i + 1]);
618                         i++;
619                 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
620                         puts(RC_DIR);
621                         exit(0);
622                 }
623                 
624         }
625
626         if (cmd.attach_files && cmd.compose == FALSE) {
627                 cmd.compose = TRUE;
628                 cmd.compose_mailto = NULL;
629         }
630 }
631
632 static gint get_queued_message_num(void)
633 {
634         FolderItem *queue;
635
636         queue = folder_get_default_queue();
637         if (!queue) return -1;
638
639         folder_item_scan(queue);
640         return queue->total_msgs;
641 }
642
643 static void initial_processing(FolderItem *item, gpointer data)
644 {
645         MainWindow *mainwin = (MainWindow *)data;
646         gchar *buf;
647
648         g_return_if_fail(item);
649         buf = g_strdup_printf(_("Processing (%s)..."), 
650                               item->path 
651                               ? item->path 
652                               : _("top level folder"));
653         debug_print("%s\n", buf);
654         g_free(buf);
655
656         
657         if (item->prefs->enable_processing)
658                 folder_item_apply_processing(item);
659
660         debug_print("done.\n");
661         STATUSBAR_POP(mainwin);
662 }
663
664 static void draft_all_messages(void)
665 {
666         GList *compose_list = compose_get_compose_list();
667         GList *elem = NULL;
668         
669         if (compose_list) {
670                 for (elem = compose_list; elem != NULL && elem->data != NULL; 
671                      elem = elem->next) {
672                         Compose *c = (Compose*)elem->data;
673                         compose_draft(c);
674                 }
675         }       
676 }
677
678 gboolean clean_quit(gpointer data)
679 {
680         static gboolean firstrun = TRUE;
681
682         if (!firstrun)
683                 return FALSE;
684         firstrun = FALSE;
685
686         /*!< Good idea to have the main window stored in a 
687          *   static variable so we can check that variable
688          *   to see if we're really allowed to do things
689          *   that actually the spawner is supposed to 
690          *   do (like: sending mail, composing messages).
691          *   Because, really, if we're the spawnee, and
692          *   we touch GTK stuff, we're hosed. See the 
693          *   next fixme. */
694
695         /* FIXME: Use something else to signal that we're
696          * in the original spawner, and not in a spawned
697          * child. */
698         if (!static_mainwindow) 
699                 return FALSE;
700                 
701         draft_all_messages();
702
703         exit_sylpheed(static_mainwindow);
704         exit(0);
705
706         return FALSE;
707 }
708
709 void app_will_exit(GtkWidget *widget, gpointer data)
710 {
711         MainWindow *mainwin = data;
712         
713         if (compose_get_compose_list()) {
714                 gint val = alertpanel(_("Really quit?"),
715                                _("Composing message exists."),
716                                _("Draft them"), _("Discard them"), _("Don't quit"));
717                 switch (val) {
718                         case G_ALERTOTHER:
719                                 return;
720                         case G_ALERTALTERNATE:
721                                 break;
722                         default:
723                                 draft_all_messages();
724                 }
725                 
726                 manage_window_focus_in(mainwin->window, NULL, NULL);
727         }
728
729         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
730                 if (alertpanel(_("Queued messages"),
731                                _("Some unsent messages are queued. Exit now?"),
732                                GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL)
733                     != G_ALERTDEFAULT)
734                         return;
735                 manage_window_focus_in(mainwin->window, NULL, NULL);
736         }
737         gtk_main_quit();
738 }
739
740 /*
741  * CLAWS: want this public so crash dialog can delete the
742  * lock file too
743  */
744 #ifndef CLAWS
745 static
746 #endif
747 gchar *get_socket_name(void)
748 {
749         static gchar *filename = NULL;
750
751         if (filename == NULL) {
752                 filename = g_strdup_printf("%s%csylpheed-%d",
753                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
754                                            getuid());
755         }
756
757         return filename;
758 }
759
760 static gchar *get_crashfile_name(void)
761 {
762         static gchar *filename = NULL;
763
764         if (filename == NULL) {
765                 filename = g_strdup_printf("%s%csylpheed-crashed",
766                                            get_tmp_dir(), G_DIR_SEPARATOR);
767         }
768
769         return filename;
770 }
771
772 static gint prohibit_duplicate_launch(void)
773 {
774         gint uxsock;
775         gchar *path;
776
777         path = get_socket_name();
778         uxsock = fd_connect_unix(path);
779         if (uxsock < 0) {
780                 unlink(path);
781                 return fd_open_unix(path);
782         }
783
784         /* remote command mode */
785
786         debug_print("another Sylpheed is already running.\n");
787
788         if (cmd.receive_all)
789                 fd_write_all(uxsock, "receive_all\n", 12);
790         else if (cmd.receive)
791                 fd_write_all(uxsock, "receive\n", 8);
792         else if (cmd.compose && cmd.attach_files) {
793                 gchar *str, *compose_str;
794                 gint i;
795
796                 if (cmd.compose_mailto)
797                         compose_str = g_strdup_printf("compose_attach %s\n",
798                                                       cmd.compose_mailto);
799                 else
800                         compose_str = g_strdup("compose_attach\n");
801
802                 fd_write_all(uxsock, compose_str, strlen(compose_str));
803                 g_free(compose_str);
804
805                 for (i = 0; i < cmd.attach_files->len; i++) {
806                         str = g_ptr_array_index(cmd.attach_files, i);
807                         fd_write_all(uxsock, str, strlen(str));
808                         fd_write_all(uxsock, "\n", 1);
809                 }
810
811                 fd_write_all(uxsock, ".\n", 2);
812         } else if (cmd.compose) {
813                 gchar *compose_str;
814
815                 if (cmd.compose_mailto)
816                         compose_str = g_strdup_printf
817                                 ("compose %s\n", cmd.compose_mailto);
818                 else
819                         compose_str = g_strdup("compose\n");
820
821                 fd_write_all(uxsock, compose_str, strlen(compose_str));
822                 g_free(compose_str);
823         } else if (cmd.send) {
824                 fd_write_all(uxsock, "send\n", 5);
825         } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
826                 fd_write(uxsock, "online\n", 6);
827         } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
828                 fd_write(uxsock, "offline\n", 7);
829         } else if (cmd.status || cmd.status_full) {
830                 gchar buf[BUFFSIZE];
831                 gint i;
832                 const gchar *command;
833                 GPtrArray *folders;
834                 gchar *folder;
835  
836                 command = cmd.status_full ? "status-full\n" : "status\n";
837                 folders = cmd.status_full ? cmd.status_full_folders :
838                         cmd.status_folders;
839  
840                 fd_write_all(uxsock, command, strlen(command));
841                 for (i = 0; folders && i < folders->len; ++i) {
842                         folder = g_ptr_array_index(folders, i);
843                         fd_write_all(uxsock, folder, strlen(folder));
844                         fd_write_all(uxsock, "\n", 1);
845                 }
846                 fd_write_all(uxsock, ".\n", 2);
847                 for (;;) {
848                         fd_gets(uxsock, buf, sizeof(buf));
849                         if (!strncmp(buf, ".\n", 2)) break;
850                         fputs(buf, stdout);
851                 }
852         } else
853                 fd_write_all(uxsock, "popup\n", 6);
854
855         fd_close(uxsock);
856         return -1;
857 }
858
859 static gint lock_socket_remove(void)
860 {
861         gchar *filename;
862
863         if (lock_socket < 0) return -1;
864
865         if (lock_socket_tag > 0)
866                 gdk_input_remove(lock_socket_tag);
867         fd_close(lock_socket);
868         filename = get_socket_name();
869         unlink(filename);
870
871         return 0;
872 }
873
874 static GPtrArray *get_folder_item_list(gint sock)
875 {
876         gchar buf[BUFFSIZE];
877         FolderItem *item;
878         GPtrArray *folders = NULL;
879
880         for (;;) {
881                 fd_gets(sock, buf, sizeof(buf));
882                 if (!strncmp(buf, ".\n", 2)) break;
883                 strretchomp(buf);
884                 if (!folders) folders = g_ptr_array_new();
885                 item = folder_find_item_from_identifier(buf);
886                 if (item)
887                         g_ptr_array_add(folders, item);
888                 else
889                         g_warning("no such folder: %s\n", buf);
890         }
891
892         return folders;
893 }
894
895 static void lock_socket_input_cb(gpointer data,
896                                  gint source,
897                                  GdkInputCondition condition)
898 {
899         MainWindow *mainwin = (MainWindow *)data;
900         gint sock;
901         gchar buf[BUFFSIZE];
902
903         sock = fd_accept(source);
904         fd_gets(sock, buf, sizeof(buf));
905
906         if (!strncmp(buf, "popup", 5)) {
907                 main_window_popup(mainwin);
908         } else if (!strncmp(buf, "receive_all", 11)) {
909                 inc_all_account_mail(mainwin, FALSE,
910                                      prefs_common.newmail_notify_manu);
911         } else if (!strncmp(buf, "receive", 7)) {
912                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
913         } else if (!strncmp(buf, "compose_attach", 14)) {
914                 GPtrArray *files;
915                 gchar *mailto;
916
917                 mailto = g_strdup(buf + strlen("compose_attach") + 1);
918                 files = g_ptr_array_new();
919                 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
920                         if (buf[0] == '.' && buf[1] == '\n') break;
921                         strretchomp(buf);
922                         g_ptr_array_add(files, g_strdup(buf));
923                 }
924                 open_compose_new(mailto, files);
925                 ptr_array_free_strings(files);
926                 g_ptr_array_free(files, TRUE);
927                 g_free(mailto);
928         } else if (!strncmp(buf, "compose", 7)) {
929                 open_compose_new(buf + strlen("compose") + 1, NULL);
930         } else if (!strncmp(buf, "send", 4)) {
931                 send_queue();
932         } else if (!strncmp(buf, "online", 6)) {
933                 main_window_toggle_work_offline(mainwin, FALSE);
934         } else if (!strncmp(buf, "offline", 7)) {
935                 main_window_toggle_work_offline(mainwin, TRUE);
936         } else if (!strncmp(buf, "status-full", 11) ||
937                    !strncmp(buf, "status", 6)) {
938                 gchar *status;
939                 GPtrArray *folders;
940  
941                 folders = get_folder_item_list(sock);
942                 status = folder_get_status
943                         (folders, !strncmp(buf, "status-full", 11));
944                 fd_write_all(sock, status, strlen(status));
945                 fd_write_all(sock, ".\n", 2);
946                 g_free(status);
947                 if (folders) g_ptr_array_free(folders, TRUE);
948         }
949
950         fd_close(sock);
951 }
952
953 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
954 {
955         gchar *addr = NULL;
956
957         if (address) {
958                 Xstrdup_a(addr, address, return);
959                 g_strstrip(addr);
960         }
961
962         compose_new(NULL, addr, attach_files);
963 }
964
965 static void send_queue(void)
966 {
967         GList *list;
968
969         for (list = folder_get_list(); list != NULL; list = list->next) {
970                 Folder *folder = list->data;
971
972                 if (folder->queue) {
973                         gint res = procmsg_send_queue
974                                 (folder->queue, prefs_common.savemsg);
975
976                         if (res < 0)    
977                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
978                         if (res)        
979                                 folder_item_scan(folder->queue);
980                 }
981         }
982 }
983
984 static void quit_signal_handler(int sig)
985 {
986         debug_print("Quitting on signal %d\n", sig);
987
988         g_timeout_add(0, clean_quit, NULL);
989 }
990
991 static void install_basic_sighandlers()
992 {
993         sigset_t    mask;
994         struct sigaction act;
995
996         sigemptyset(&mask);
997
998 #ifdef SIGTERM
999         sigaddset(&mask, SIGTERM);
1000 #endif
1001 #ifdef SIGINT
1002         sigaddset(&mask, SIGINT);
1003 #endif
1004 #ifdef SIGHUP
1005         sigaddset(&mask, SIGHUP);
1006 #endif
1007
1008         act.sa_handler = quit_signal_handler;
1009         act.sa_mask    = mask;
1010         act.sa_flags   = 0;
1011
1012 #ifdef SIGTERM
1013         sigaction(SIGTERM, &act, 0);
1014 #endif
1015 #ifdef SIGINT
1016         sigaction(SIGINT, &act, 0);
1017 #endif  
1018 #ifdef SIGHUP
1019         sigaction(SIGHUP, &act, 0);
1020 #endif  
1021
1022         sigprocmask(SIG_UNBLOCK, &mask, 0);
1023 }