2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkrc.h>
38 #include <sys/types.h>
43 #ifdef HAVE_STARTUP_NOTIFICATION
44 # define SN_API_NOT_YET_FROZEN
45 # include <libsn/sn-launchee.h>
46 # include <gdk/gdkx.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"
77 #include "manage_window.h"
78 #include "alertpanel.h"
79 #include "statusbar.h"
80 #include "addressbook.h"
88 #include "prefs_toolbar.h"
95 #include "imap-thread.h"
97 #include "stock_pixmap.h"
112 #ifdef HAVE_STARTUP_NOTIFICATION
113 static SnLauncheeContext *sn_context = NULL;
114 static SnDisplay *sn_display = NULL;
117 static gint lock_socket = -1;
118 static gint lock_socket_tag = 0;
122 ONLINE_MODE_DONT_CHANGE,
127 static struct RemoteCmd {
129 gboolean receive_all;
131 const gchar *compose_mailto;
132 GPtrArray *attach_files;
134 gboolean status_full;
135 GPtrArray *status_folders;
136 GPtrArray *status_full_folders;
143 const gchar *subscribe_uri;
147 static void parse_cmd_opt(int argc, char *argv[]);
149 static gint prohibit_duplicate_launch (void);
150 static gchar * get_crashfile_name (void);
151 static gint lock_socket_remove (void);
152 static void lock_socket_input_cb (gpointer data,
154 GdkInputCondition condition);
158 gchar *get_socket_name (void);
161 static void open_compose_new (const gchar *address,
162 GPtrArray *attach_files);
164 static void send_queue (void);
165 static void initial_processing (FolderItem *item, gpointer data);
166 static void quit_signal_handler (int sig);
167 static void install_basic_sighandlers (void);
168 static void exit_sylpheed (MainWindow *mainwin);
170 #define MAKE_DIR_IF_NOT_EXIST(dir) \
172 if (!is_dir_exist(dir)) { \
173 if (is_file_exist(dir)) { \
175 (_("File '%s' already exists.\n" \
176 "Can't create folder."), \
180 if (make_dir(dir) < 0) \
185 static MainWindow *static_mainwindow;
187 #ifdef HAVE_STARTUP_NOTIFICATION
188 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
190 gdk_error_trap_push();
193 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
195 gdk_error_trap_pop();
198 static void startup_notification_complete(gboolean with_window)
201 GtkWidget *hack = NULL;
204 /* this is needed to make the startup notification leave,
205 * if we have been launched from a menu.
206 * We have to display a window, so let it be very little */
207 hack = gtk_window_new(GTK_WINDOW_POPUP);
208 gtk_widget_set_uposition(hack, 0, 0);
209 gtk_widget_set_size_request(hack, 1, 1);
210 gtk_widget_show(hack);
213 xdisplay = GDK_DISPLAY();
214 sn_display = sn_display_new(xdisplay,
217 sn_context = sn_launchee_context_new_from_environment(sn_display,
218 DefaultScreen(xdisplay));
220 if (sn_context != NULL) {
221 sn_launchee_context_complete(sn_context);
222 sn_launchee_context_unref(sn_context);
223 sn_display_unref(sn_display);
226 gtk_widget_destroy(hack);
229 #endif /* HAVE_STARTUP_NOTIFICATION */
231 void sylpheed_gtk_idle(void)
233 while(gtk_events_pending()) {
234 gtk_main_iteration();
239 gboolean defer_check_all(void *data)
241 gboolean autochk = GPOINTER_TO_INT(data);
243 inc_all_account_mail(static_mainwindow, autochk,
244 prefs_common.newmail_notify_manu);
249 gboolean defer_check(void *data)
251 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
256 static gboolean defer_jump(void *data)
258 mainwindow_jump_to(data);
262 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir)
264 gchar *message = g_strdup_printf(_("Configuration for Sylpheed-Claws %s found.\n"
265 "Do you want to migrate this configuration?"),
266 !strcmp(old_cfg_dir, OLD_GTK1_RC_DIR)?
267 _("1.0.5 or previous"):_("1.9.15 or previous"));
269 GtkWidget *window = NULL;
270 if (alertpanel(_("Migration of configuration"),
272 GTK_STOCK_NO, GTK_STOCK_YES, NULL) != G_ALERTALTERNATE) {
276 window = label_window_create(_("Copying configuration..."));
278 r = copy_dir(old_cfg_dir, new_cfg_dir);
279 gtk_widget_destroy(window);
281 alertpanel_error(_("Migration failed!"));
286 int main(int argc, char *argv[])
290 FolderView *folderview;
292 gboolean crash_file_present = FALSE;
293 gint num_folder_class = 0;
294 if (!sylpheed_init(&argc, &argv)) {
298 prog_version = PROG_VERSION;
300 argv0 = g_strdup(argv[0]);
303 parse_cmd_opt(argc, argv);
308 gtk_init(&argc, &argv);
309 crash_main(cmd.crash_params);
312 crash_install_handlers();
314 install_basic_sighandlers();
317 /* check and create unix domain socket for remote operation */
319 lock_socket = prohibit_duplicate_launch();
320 if (lock_socket < 0) {
321 #ifdef HAVE_STARTUP_NOTIFICATION
322 if(gtk_init_check(&argc, &argv))
323 startup_notification_complete(TRUE);
328 if (cmd.status || cmd.status_full) {
329 puts("0 Sylpheed-Claws not running.");
330 lock_socket_remove();
337 if (!g_thread_supported())
341 gtk_init(&argc, &argv);
344 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
345 gtk_widget_set_default_visual(gdk_rgb_get_visual());
347 if (!g_thread_supported()) {
348 g_error(_("g_thread is not supported by glib.\n"));
351 /* check that we're not on a too recent/old gtk+ */
352 #if GTK_CHECK_VERSION(2, 9, 0)
353 if (gtk_check_version(2, 9, 0) != NULL) {
354 alertpanel_error(_("Sylpheed-Claws has been compiled with "
355 "a more recent GTK+ library than is "
356 "currently available. This will cause "
357 "crashes. You need to upgrade GTK+ or "
358 "recompile Sylpheed-Claws."));
362 if (gtk_check_version(2, 9, 0) == NULL) {
363 alertpanel_error(_("Sylpheed-Claws has been compiled with "
364 "an older GTK+ library than is "
365 "currently available. This will cause "
366 "crashes. You need to recompile "
371 /* parse gtkrc files */
372 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
374 gtk_rc_parse(userrc);
376 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
377 G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
378 gtk_rc_parse(userrc);
381 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
382 if (!is_dir_exist(RC_DIR)) {
384 if (is_dir_exist(OLD_GTK2_RC_DIR))
385 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR);
386 else if (is_dir_exist(OLD_GTK1_RC_DIR))
387 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR);
388 if (r == FALSE && !is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0)
392 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
393 gtk_rc_parse(userrc);
396 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
397 gtk_accel_map_load (userrc);
400 gtk_settings_set_long_property(gtk_settings_get_default(),
401 "gtk-can-change-accels",
402 (glong)TRUE, "XProperty");
404 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
406 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
407 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
408 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
409 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
410 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
411 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
413 crash_file_present = is_file_exist(get_crashfile_name());
414 /* remove temporary files */
415 remove_all_files(get_tmp_dir());
416 remove_all_files(get_mime_tmp_dir());
418 if (is_file_exist("sylpheed.log")) {
419 if (rename_force("sylpheed.log", "sylpheed.log.bak") < 0)
420 FILE_OP_ERROR("sylpheed.log", "rename");
422 set_log_file("sylpheed.log");
424 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
426 folder_system_init();
427 prefs_common_read_config();
431 prefs_ext_prog_init();
432 prefs_wrapping_init();
433 prefs_compose_writing_init();
434 prefs_msg_colors_init();
436 prefs_image_viewer_init();
438 prefs_summaries_init();
439 prefs_message_init();
441 prefs_receive_init();
444 gtkaspell_checkers_init();
445 prefs_spelling_init();
448 sock_set_io_timeout(prefs_common.io_timeout_secs);
450 imap_main_set_timeout(prefs_common.io_timeout_secs);
452 prefs_actions_read_config();
453 prefs_display_header_read_config();
454 /* prefs_filtering_read_config(); */
455 addressbook_read_file();
456 renderer_read_config();
459 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_SYLPHEED_CLAWS_ICON, &icon);
460 gtk_window_set_default_icon(icon);
462 folderview_initialize();
468 mainwin = main_window_create
469 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
470 manage_window_focus_in(mainwin->window, NULL, NULL);
471 folderview = mainwin->folderview;
473 gtk_clist_freeze(GTK_CLIST(mainwin->folderview->ctree));
474 folder_item_update_freeze();
476 /* register the callback of unix domain socket input */
478 lock_socket_tag = gdk_input_add(lock_socket,
479 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
480 lock_socket_input_cb,
484 prefs_account_init();
485 account_read_config_all();
487 if (folder_read_list() < 0) {
488 if (!run_wizard(mainwin, TRUE))
490 main_window_reflect_prefs_all_now();
494 if (!account_get_list()) {
495 if (!run_wizard(mainwin, FALSE))
497 account_read_config_all();
498 if(!account_get_list())
499 exit_sylpheed(mainwin);
503 toolbar_main_set_sensitive(mainwin);
504 main_window_set_menu_sensitive(mainwin);
506 main_window_popup(mainwin);
509 imap_main_init(prefs_common.skip_ssl_cert_check);
511 account_set_missing_folder();
512 folder_set_missing_folders();
513 folderview_set(folderview);
515 prefs_matcher_read_config();
517 /* make one all-folder processing before using sylpheed */
518 main_window_cursor_wait(mainwin);
519 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
521 /* if Sylpheed crashed, rebuild caches */
522 if (!cmd.crash && crash_file_present) {
524 debug_print("Sylpheed-Claws crashed, checking for new messages in local folders\n");
525 folder_item_update_thaw();
526 folderview_check_new(NULL);
527 folder_clean_cache_memory_force();
528 folder_item_update_freeze();
530 /* make the crash-indicator file */
531 str_write_to_file("foo", get_crashfile_name());
533 inc_autocheck_timer_init(mainwin);
535 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
536 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
538 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
539 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
542 if (cmd.status_folders) {
543 g_ptr_array_free(cmd.status_folders, TRUE);
544 cmd.status_folders = NULL;
546 if (cmd.status_full_folders) {
547 g_ptr_array_free(cmd.status_full_folders, TRUE);
548 cmd.status_full_folders = NULL;
551 sylpheed_register_idle_function(sylpheed_gtk_idle);
553 prefs_toolbar_init();
555 num_folder_class = g_list_length(folder_get_list());
557 plugin_load_all("GTK2");
559 if (g_list_length(folder_get_list()) != num_folder_class) {
560 debug_print("new folders loaded, reloading processing rules\n");
561 prefs_matcher_read_config();
564 if (plugin_get_unloaded_list() != NULL) {
565 main_window_cursor_normal(mainwin);
566 alertpanel_warning(_("Some plugin(s) failed to load. "
567 "Check the Plugins configuration "
568 "for more information."));
569 main_window_cursor_wait(mainwin);
572 plugin_load_standard_plugins ();
574 if (!folder_have_mailbox()) {
575 main_window_cursor_normal(mainwin);
576 alertpanel_error(_("Sylpheed-Claws has detected a configured "
577 "mailbox, but could not load it. It is "
578 "probably provided by an out-of-date "
579 "external plugin. Please reinstall the "
580 "plugin and try again."));
584 static_mainwindow = mainwin;
586 #ifdef HAVE_STARTUP_NOTIFICATION
587 startup_notification_complete(FALSE);
589 folder_item_update_thaw();
590 gtk_clist_thaw(GTK_CLIST(mainwin->folderview->ctree));
591 main_window_cursor_normal(mainwin);
593 if (cmd.receive_all) {
594 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
595 } else if (prefs_common.chk_on_startup) {
596 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
597 } else if (cmd.receive) {
598 g_timeout_add(1000, defer_check, NULL);
600 gtk_widget_grab_focus(folderview->ctree);
604 open_compose_new(cmd.compose_mailto, cmd.attach_files);
606 if (cmd.attach_files) {
607 ptr_array_free_strings(cmd.attach_files);
608 g_ptr_array_free(cmd.attach_files, TRUE);
609 cmd.attach_files = NULL;
612 folder_subscribe(cmd.subscribe_uri);
620 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
625 exit_sylpheed(mainwin);
630 static void save_all_caches(FolderItem *item, gpointer data)
632 gint free_caches = GPOINTER_TO_INT(data);
638 folder_item_close(item);
641 folder_item_free_cache(item, TRUE);
643 folder_item_write_cache(item);
646 static gboolean sc_exiting = FALSE;
648 static void exit_sylpheed(MainWindow *mainwin)
654 debug_print("shutting down\n");
655 inc_autocheck_timer_remove();
657 if (prefs_common.clean_on_exit) {
658 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
661 /* save prefs for opened folder */
662 if(mainwin->folderview->opened) {
665 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
666 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
669 /* save all state before exiting */
671 folder_func_to_all_folders(save_all_caches, GINT_TO_POINTER(1));
673 main_window_get_size(mainwin);
674 main_window_get_position(mainwin);
675 prefs_common_write_config();
676 account_write_config_all();
677 addressbook_export_to_file();
679 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
680 gtk_accel_map_save(filename);
683 /* delete temporary files */
684 remove_all_files(get_tmp_dir());
685 remove_all_files(get_mime_tmp_dir());
692 /* delete crashfile */
694 g_unlink(get_crashfile_name());
696 lock_socket_remove();
698 main_window_destroy_all();
700 plugin_unload_all("GTK2");
702 prefs_toolbar_done();
704 addressbook_destroy();
708 prefs_ext_prog_done();
709 prefs_wrapping_done();
710 prefs_compose_writing_done();
711 prefs_msg_colors_done();
712 prefs_image_viewer_done();
715 prefs_summaries_done();
716 prefs_message_done();
718 prefs_receive_done();
721 prefs_spelling_done();
722 gtkaspell_checkers_quit();
728 static void parse_cmd_opt(int argc, char *argv[])
732 for (i = 1; i < argc; i++) {
733 if (!strncmp(argv[i], "--receive-all", 13)) {
734 cmd.receive_all = TRUE;
735 } else if (!strncmp(argv[i], "--receive", 9)) {
737 } else if (!strncmp(argv[i], "--compose", 9)) {
738 const gchar *p = argv[i + 1];
741 cmd.compose_mailto = NULL;
742 if (p && *p != '\0' && *p != '-') {
743 if (!strncmp(p, "mailto:", 7)) {
744 cmd.compose_mailto = p + 7;
746 cmd.compose_mailto = p;
750 } else if (!strncmp(argv[i], "--subscribe", 11)) {
751 const gchar *p = argv[i + 1];
752 if (p && *p != '\0' && *p != '-') {
753 cmd.subscribe = TRUE;
754 cmd.subscribe_uri = p;
756 } else if (!strncmp(argv[i], "--attach", 8)) {
757 const gchar *p = argv[i + 1];
760 while (p && *p != '\0' && *p != '-') {
761 if (!cmd.attach_files) {
762 cmd.attach_files = g_ptr_array_new();
764 if (*p != G_DIR_SEPARATOR) {
765 file = g_strconcat(sylpheed_get_startup_dir(),
771 g_ptr_array_add(cmd.attach_files, file);
775 } else if (!strncmp(argv[i], "--send", 6)) {
777 } else if (!strncmp(argv[i], "--version", 9)) {
778 puts("Sylpheed-Claws version " VERSION);
780 } else if (!strncmp(argv[i], "--status-full", 13)) {
781 const gchar *p = argv[i + 1];
783 cmd.status_full = TRUE;
784 while (p && *p != '\0' && *p != '-') {
785 if (!cmd.status_full_folders) {
786 cmd.status_full_folders =
789 g_ptr_array_add(cmd.status_full_folders,
794 } else if (!strncmp(argv[i], "--status", 8)) {
795 const gchar *p = argv[i + 1];
798 while (p && *p != '\0' && *p != '-') {
799 if (!cmd.status_folders)
800 cmd.status_folders = g_ptr_array_new();
801 g_ptr_array_add(cmd.status_folders,
806 } else if (!strncmp(argv[i], "--online", 8)) {
807 cmd.online_mode = ONLINE_MODE_ONLINE;
808 } else if (!strncmp(argv[i], "--offline", 9)) {
809 cmd.online_mode = ONLINE_MODE_OFFLINE;
810 } else if (!strncmp(argv[i], "--help", 6)) {
811 gchar *base = g_path_get_basename(argv[0]);
812 g_print(_("Usage: %s [OPTION]...\n"), base);
814 g_print("%s\n", _(" --compose [address] open composition window"));
815 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
816 g_print("%s\n", _(" --attach file1 [file2]...\n"
817 " open composition window with specified files\n"
819 g_print("%s\n", _(" --receive receive new messages"));
820 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
821 g_print("%s\n", _(" --send send all queued messages"));
822 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
823 g_print("%s\n", _(" --status-full [folder]...\n"
824 " show the status of each folder"));
825 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
826 " folder is a folder id like '#mh/Mailbox/inbox'"));
827 g_print("%s\n", _(" --online switch to online mode"));
828 g_print("%s\n", _(" --offline switch to offline mode"));
829 g_print("%s\n", _(" --exit exit Sylpheed-Claws"));
830 g_print("%s\n", _(" --debug debug mode"));
831 g_print("%s\n", _(" --help display this help and exit"));
832 g_print("%s\n", _(" --version output version information and exit"));
833 g_print("%s\n", _(" --config-dir output configuration directory"));
837 } else if (!strncmp(argv[i], "--crash", 7)) {
839 cmd.crash_params = g_strdup(argv[i + 1]);
841 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
844 } else if (!strncmp(argv[i], "--exit", 6)) {
846 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
847 cmd.target = argv[i+1];
848 } else if (i == 1 && argc == 2) {
849 /* only one parameter. Do something intelligent about it */
850 if (strstr(argv[i], "@") && !strstr(argv[i], "://")) {
851 const gchar *p = argv[i];
854 cmd.compose_mailto = NULL;
855 if (p && *p != '\0' && *p != '-') {
856 if (!strncmp(p, "mailto:", 7)) {
857 cmd.compose_mailto = p + 7;
859 cmd.compose_mailto = p;
862 } else if (strstr(argv[i], "://")) {
863 const gchar *p = argv[i];
864 if (p && *p != '\0' && *p != '-') {
865 cmd.subscribe = TRUE;
866 cmd.subscribe_uri = p;
873 if (cmd.attach_files && cmd.compose == FALSE) {
875 cmd.compose_mailto = NULL;
879 static gint get_queued_message_num(void)
883 queue = folder_get_default_queue();
888 folder_item_scan(queue);
889 return queue->total_msgs;
892 static void initial_processing(FolderItem *item, gpointer data)
894 MainWindow *mainwin = (MainWindow *)data;
897 g_return_if_fail(item);
898 buf = g_strdup_printf(_("Processing (%s)..."),
901 : _("top level folder"));
902 debug_print("%s\n", buf);
906 if (item->prefs->enable_processing) {
907 folder_item_apply_processing(item);
910 debug_print("done.\n");
911 STATUSBAR_POP(mainwin);
914 static void draft_all_messages(void)
916 GList *compose_list = NULL;
918 while ((compose_list = compose_get_compose_list()) != NULL) {
919 Compose *c = (Compose*)compose_list->data;
924 gboolean clean_quit(gpointer data)
926 static gboolean firstrun = TRUE;
933 /*!< Good idea to have the main window stored in a
934 * static variable so we can check that variable
935 * to see if we're really allowed to do things
936 * that actually the spawner is supposed to
937 * do (like: sending mail, composing messages).
938 * Because, really, if we're the spawnee, and
939 * we touch GTK stuff, we're hosed. See the
942 /* FIXME: Use something else to signal that we're
943 * in the original spawner, and not in a spawned
945 if (!static_mainwindow) {
949 draft_all_messages();
951 exit_sylpheed(static_mainwindow);
957 void app_will_exit(GtkWidget *widget, gpointer data)
959 MainWindow *mainwin = data;
961 if (sc_exiting == TRUE) {
962 debug_print("exit pending\n");
966 debug_print("exiting\n");
967 if (compose_get_compose_list()) {
968 gint val = alertpanel(_("Really quit?"),
969 _("Composing message exists."),
970 _("_Save to Draft"), _("_Discard them"), _("Do_n't quit"));
975 case G_ALERTALTERNATE:
978 draft_all_messages();
981 manage_window_focus_in(mainwin->window, NULL, NULL);
984 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
985 if (alertpanel(_("Queued messages"),
986 _("Some unsent messages are queued. Exit now?"),
987 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
988 != G_ALERTALTERNATE) {
992 manage_window_focus_in(mainwin->window, NULL, NULL);
996 if (folderview_get_selected_item(mainwin->folderview))
997 folder_item_close(folderview_get_selected_item(mainwin->folderview));
1001 gboolean sylpheed_is_exiting(void)
1007 * CLAWS: want this public so crash dialog can delete the
1013 gchar *get_socket_name(void)
1015 static gchar *filename = NULL;
1017 if (filename == NULL) {
1018 filename = g_strdup_printf("%s%csylpheed-claws-%d",
1019 g_get_tmp_dir(), G_DIR_SEPARATOR,
1030 static gchar *get_crashfile_name(void)
1032 static gchar *filename = NULL;
1034 if (filename == NULL) {
1035 filename = g_strdup_printf("%s%csylpheed-crashed",
1036 get_tmp_dir(), G_DIR_SEPARATOR);
1042 static gint prohibit_duplicate_launch(void)
1047 path = get_socket_name();
1048 uxsock = fd_connect_unix(path);
1051 return fd_open_unix(path);
1054 /* remote command mode */
1056 debug_print("another Sylpheed-Claws instance is already running.\n");
1058 if (cmd.receive_all) {
1059 fd_write_all(uxsock, "receive_all\n", 12);
1060 } else if (cmd.receive) {
1061 fd_write_all(uxsock, "receive\n", 8);
1062 } else if (cmd.compose && cmd.attach_files) {
1063 gchar *str, *compose_str;
1066 if (cmd.compose_mailto) {
1067 compose_str = g_strdup_printf("compose_attach %s\n",
1068 cmd.compose_mailto);
1070 compose_str = g_strdup("compose_attach\n");
1073 fd_write_all(uxsock, compose_str, strlen(compose_str));
1074 g_free(compose_str);
1076 for (i = 0; i < cmd.attach_files->len; i++) {
1077 str = g_ptr_array_index(cmd.attach_files, i);
1078 fd_write_all(uxsock, str, strlen(str));
1079 fd_write_all(uxsock, "\n", 1);
1082 fd_write_all(uxsock, ".\n", 2);
1083 } else if (cmd.compose) {
1086 if (cmd.compose_mailto) {
1087 compose_str = g_strdup_printf
1088 ("compose %s\n", cmd.compose_mailto);
1090 compose_str = g_strdup("compose\n");
1093 fd_write_all(uxsock, compose_str, strlen(compose_str));
1094 g_free(compose_str);
1095 } else if (cmd.subscribe) {
1096 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
1097 fd_write_all(uxsock, str, strlen(str));
1099 } else if (cmd.send) {
1100 fd_write_all(uxsock, "send\n", 5);
1101 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1102 fd_write(uxsock, "online\n", 6);
1103 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1104 fd_write(uxsock, "offline\n", 7);
1105 } else if (cmd.status || cmd.status_full) {
1106 gchar buf[BUFFSIZE];
1108 const gchar *command;
1112 command = cmd.status_full ? "status-full\n" : "status\n";
1113 folders = cmd.status_full ? cmd.status_full_folders :
1116 fd_write_all(uxsock, command, strlen(command));
1117 for (i = 0; folders && i < folders->len; ++i) {
1118 folder = g_ptr_array_index(folders, i);
1119 fd_write_all(uxsock, folder, strlen(folder));
1120 fd_write_all(uxsock, "\n", 1);
1122 fd_write_all(uxsock, ".\n", 2);
1124 fd_gets(uxsock, buf, sizeof(buf));
1125 if (!strncmp(buf, ".\n", 2)) break;
1128 } else if (cmd.exit) {
1129 fd_write_all(uxsock, "exit\n", 5);
1130 } else if (cmd.target) {
1131 gchar *str = g_strdup_printf("select %s\n", cmd.target);
1132 fd_write_all(uxsock, str, strlen(str));
1135 fd_write_all(uxsock, "popup\n", 6);
1141 static gint lock_socket_remove(void)
1146 if (lock_socket < 0) {
1150 if (lock_socket_tag > 0) {
1151 gdk_input_remove(lock_socket_tag);
1153 fd_close(lock_socket);
1154 filename = get_socket_name();
1161 static GPtrArray *get_folder_item_list(gint sock)
1163 gchar buf[BUFFSIZE];
1165 GPtrArray *folders = NULL;
1168 fd_gets(sock, buf, sizeof(buf));
1169 if (!strncmp(buf, ".\n", 2)) {
1174 folders = g_ptr_array_new();
1176 item = folder_find_item_from_identifier(buf);
1178 g_ptr_array_add(folders, item);
1180 g_warning("no such folder: %s\n", buf);
1187 static void lock_socket_input_cb(gpointer data,
1189 GdkInputCondition condition)
1191 MainWindow *mainwin = (MainWindow *)data;
1193 gchar buf[BUFFSIZE];
1195 sock = fd_accept(source);
1196 fd_gets(sock, buf, sizeof(buf));
1198 if (!strncmp(buf, "popup", 5)) {
1199 main_window_popup(mainwin);
1200 } else if (!strncmp(buf, "receive_all", 11)) {
1201 inc_all_account_mail(mainwin, FALSE,
1202 prefs_common.newmail_notify_manu);
1203 } else if (!strncmp(buf, "receive", 7)) {
1204 inc_mail(mainwin, prefs_common.newmail_notify_manu);
1205 } else if (!strncmp(buf, "compose_attach", 14)) {
1209 mailto = g_strdup(buf + strlen("compose_attach") + 1);
1210 files = g_ptr_array_new();
1211 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
1212 if (buf[0] == '.' && buf[1] == '\n') {
1216 g_ptr_array_add(files, g_strdup(buf));
1218 open_compose_new(mailto, files);
1219 ptr_array_free_strings(files);
1220 g_ptr_array_free(files, TRUE);
1222 } else if (!strncmp(buf, "compose", 7)) {
1223 open_compose_new(buf + strlen("compose") + 1, NULL);
1224 } else if (!strncmp(buf, "subscribe", 9)) {
1225 main_window_popup(mainwin);
1226 folder_subscribe(buf + strlen("subscribe") + 1);
1227 } else if (!strncmp(buf, "send", 4)) {
1229 } else if (!strncmp(buf, "online", 6)) {
1230 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1231 } else if (!strncmp(buf, "offline", 7)) {
1232 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1233 } else if (!strncmp(buf, "status-full", 11) ||
1234 !strncmp(buf, "status", 6)) {
1238 folders = get_folder_item_list(sock);
1239 status = folder_get_status
1240 (folders, !strncmp(buf, "status-full", 11));
1241 fd_write_all(sock, status, strlen(status));
1242 fd_write_all(sock, ".\n", 2);
1244 if (folders) g_ptr_array_free(folders, TRUE);
1245 } else if (!strncmp(buf, "select ", 7)) {
1246 const gchar *target = buf+7;
1247 mainwindow_jump_to(target);
1248 } else if (!strncmp(buf, "exit", 4)) {
1249 app_will_exit(NULL, mainwin);
1255 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
1260 Xstrdup_a(addr, address, return);
1264 compose_new(NULL, addr, attach_files);
1267 static void send_queue(void)
1270 gchar *errstr = NULL;
1271 for (list = folder_get_list(); list != NULL; list = list->next) {
1272 Folder *folder = list->data;
1274 if (folder->queue) {
1275 gint res = procmsg_send_queue
1276 (folder->queue, prefs_common.savemsg,
1280 folder_item_scan(folder->queue);
1285 gchar *tmp = g_strdup_printf(_("Some errors occurred "
1286 "while sending queued messages:\n%s"), errstr);
1288 alertpanel_error_log(tmp);
1291 alertpanel_error_log("Some errors occurred "
1292 "while sending queued messages.");
1296 static void quit_signal_handler(int sig)
1299 if (sig == SIGPIPE) {
1300 debug_print("caugth SIGPIPE, maybe X closing!\n");
1301 signal(SIGPIPE, SIG_IGN); /* ignore following sigpipes */
1302 folder_write_list();
1303 folder_func_to_all_folders(save_all_caches, GINT_TO_POINTER(0));
1307 debug_print("Quitting on signal %d\n", sig);
1309 g_timeout_add(0, clean_quit, NULL);
1312 static void install_basic_sighandlers()
1316 struct sigaction act;
1321 sigaddset(&mask, SIGTERM);
1324 sigaddset(&mask, SIGINT);
1327 sigaddset(&mask, SIGHUP);
1330 sigaddset(&mask, SIGPIPE);
1333 act.sa_handler = quit_signal_handler;
1338 sigaction(SIGTERM, &act, 0);
1341 sigaction(SIGINT, &act, 0);
1344 sigaction(SIGHUP, &act, 0);
1347 sigaction(SIGPIPE, &act, 0);
1350 sigprocmask(SIG_UNBLOCK, &mask, 0);
1351 #endif /* !G_OS_WIN32 */