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