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