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