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 #include <X11/SM/SMlib.h>
48 #ifdef HAVE_STARTUP_NOTIFICATION
49 # define SN_API_NOT_YET_FROZEN
50 # include <libsn/sn-launchee.h>
51 # include <gdk/gdkx.h>
56 #include "mainwindow.h"
57 #include "folderview.h"
58 #include "image_viewer.h"
59 #include "summaryview.h"
60 #include "prefs_common.h"
61 #include "prefs_account.h"
62 #include "prefs_actions.h"
63 #include "prefs_ext_prog.h"
64 #include "prefs_fonts.h"
65 #include "prefs_image_viewer.h"
66 #include "prefs_message.h"
67 #include "prefs_receive.h"
68 #include "prefs_msg_colors.h"
69 #include "prefs_quote.h"
70 #include "prefs_spelling.h"
71 #include "prefs_summaries.h"
72 #include "prefs_themes.h"
73 #include "prefs_other.h"
74 #include "prefs_send.h"
75 #include "prefs_wrapping.h"
76 #include "prefs_compose_writing.h"
77 #include "prefs_display_header.h"
82 #include "manage_window.h"
83 #include "alertpanel.h"
84 #include "statusbar.h"
85 #include "addressbook.h"
93 #include "prefs_toolbar.h"
100 #include "imap-thread.h"
102 #include "stock_pixmap.h"
117 #ifdef HAVE_STARTUP_NOTIFICATION
118 static SnLauncheeContext *sn_context = NULL;
119 static SnDisplay *sn_display = NULL;
122 static gint lock_socket = -1;
123 static gint lock_socket_tag = 0;
127 ONLINE_MODE_DONT_CHANGE,
132 static struct RemoteCmd {
134 gboolean receive_all;
136 const gchar *compose_mailto;
137 GPtrArray *attach_files;
139 gboolean status_full;
140 GPtrArray *status_folders;
141 GPtrArray *status_full_folders;
148 const gchar *subscribe_uri;
152 static void parse_cmd_opt(int argc, char *argv[]);
154 static gint prohibit_duplicate_launch (void);
155 static gchar * get_crashfile_name (void);
156 static gint lock_socket_remove (void);
157 static void lock_socket_input_cb (gpointer data,
159 GdkInputCondition condition);
163 gchar *get_socket_name (void);
166 static void open_compose_new (const gchar *address,
167 GPtrArray *attach_files);
169 static void send_queue (void);
170 static void initial_processing (FolderItem *item, gpointer data);
171 static void quit_signal_handler (int sig);
172 static void install_basic_sighandlers (void);
173 static void exit_sylpheed (MainWindow *mainwin);
175 #define MAKE_DIR_IF_NOT_EXIST(dir) \
177 if (!is_dir_exist(dir)) { \
178 if (is_file_exist(dir)) { \
180 (_("File '%s' already exists.\n" \
181 "Can't create folder."), \
185 if (make_dir(dir) < 0) \
190 static MainWindow *static_mainwindow;
191 static gboolean emergency_exit = FALSE;
193 #ifdef HAVE_STARTUP_NOTIFICATION
194 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
196 gdk_error_trap_push();
199 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
201 gdk_error_trap_pop();
204 static void startup_notification_complete(gboolean with_window)
207 GtkWidget *hack = NULL;
210 /* this is needed to make the startup notification leave,
211 * if we have been launched from a menu.
212 * We have to display a window, so let it be very little */
213 hack = gtk_window_new(GTK_WINDOW_POPUP);
214 gtk_widget_set_uposition(hack, 0, 0);
215 gtk_widget_set_size_request(hack, 1, 1);
216 gtk_widget_show(hack);
219 xdisplay = GDK_DISPLAY();
220 sn_display = sn_display_new(xdisplay,
223 sn_context = sn_launchee_context_new_from_environment(sn_display,
224 DefaultScreen(xdisplay));
226 if (sn_context != NULL) {
227 sn_launchee_context_complete(sn_context);
228 sn_launchee_context_unref(sn_context);
229 sn_display_unref(sn_display);
232 gtk_widget_destroy(hack);
235 #endif /* HAVE_STARTUP_NOTIFICATION */
237 void sylpheed_gtk_idle(void)
239 while(gtk_events_pending()) {
240 gtk_main_iteration();
245 gboolean defer_check_all(void *data)
247 gboolean autochk = GPOINTER_TO_INT(data);
249 inc_all_account_mail(static_mainwindow, autochk,
250 prefs_common.newmail_notify_manu);
255 gboolean defer_check(void *data)
257 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
262 static gboolean defer_jump(void *data)
264 mainwindow_jump_to(data);
268 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir)
270 gchar *message = g_strdup_printf(_("Configuration for Sylpheed-Claws %s found.\n"
271 "Do you want to migrate this configuration?"),
272 !strcmp(old_cfg_dir, OLD_GTK1_RC_DIR)?
273 _("1.0.5 or previous"):_("1.9.15 or previous"));
275 GtkWidget *window = NULL;
276 if (alertpanel(_("Migration of configuration"),
278 GTK_STOCK_NO, GTK_STOCK_YES, NULL) != G_ALERTALTERNATE) {
282 window = label_window_create(_("Copying configuration..."));
284 r = copy_dir(old_cfg_dir, new_cfg_dir);
285 gtk_widget_destroy(window);
287 alertpanel_error(_("Migration failed!"));
294 sc_client_set_value (MainWindow *mainwin,
305 prop.num_vals = num_vals;
309 if (mainwin->smc_conn)
310 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
313 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
318 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
322 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
324 MainWindow *mainwin = (MainWindow *)client_data;
325 if (mainwin->smc_conn)
326 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
329 static void sc_save_yourself_callback (SmcConn smc_conn,
330 SmPointer client_data,
336 MainWindow *mainwin = (MainWindow *)client_data;
337 if (mainwin->smc_conn)
338 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
341 static IceIOErrorHandler sc_ice_installed_handler;
343 static void sc_ice_io_error_handler (IceConn connection)
345 if (sc_ice_installed_handler)
346 (*sc_ice_installed_handler) (connection);
348 static gboolean sc_process_ice_messages (GIOChannel *source,
349 GIOCondition condition,
352 IceConn connection = (IceConn) data;
353 IceProcessMessagesStatus status;
355 status = IceProcessMessages (connection, NULL, NULL);
357 if (status == IceProcessMessagesIOError) {
358 IcePointer context = IceGetConnectionContext (connection);
360 if (context && GTK_IS_OBJECT (context)) {
361 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
363 if (disconnect_id > 0)
364 g_signal_emit (context, disconnect_id, 0);
366 IceSetShutdownNegotiation (connection, False);
367 IceCloseConnection (connection);
374 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
375 IcePointer *watch_data)
381 /* Make sure we don't pass on these file descriptors to any
383 fcntl(IceConnectionNumber(connection),F_SETFD,
384 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
386 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
387 input_id = g_io_add_watch (channel,
388 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
389 sc_process_ice_messages,
391 g_io_channel_unref (channel);
393 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
395 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
396 g_source_remove (input_id);
400 static void sc_session_manager_connect(MainWindow *mainwin)
402 static gboolean connected = FALSE;
403 SmcCallbacks callbacks;
405 IceIOErrorHandler default_handler;
412 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
413 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
415 if (sc_ice_installed_handler == default_handler)
416 sc_ice_installed_handler = NULL;
418 IceAddConnectionWatch (new_ice_connection, NULL);
421 callbacks.save_yourself.callback = sc_save_yourself_callback;
422 callbacks.die.callback = sc_die_callback;
423 callbacks.save_complete.callback = sc_save_complete_callback;
424 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
426 callbacks.save_yourself.client_data =
427 callbacks.die.client_data =
428 callbacks.save_complete.client_data =
429 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
430 if (g_getenv ("SESSION_MANAGER")) {
431 gchar error_string_ret[256] = "";
433 mainwin->smc_conn = (gpointer)
434 SmcOpenConnection (NULL, mainwin,
435 SmProtoMajor, SmProtoMinor,
436 SmcSaveYourselfProcMask | SmcDieProcMask |
437 SmcSaveCompleteProcMask |
438 SmcShutdownCancelledProcMask,
441 256, error_string_ret);
443 if (error_string_ret[0] || mainwin->smc_conn == NULL)
444 g_warning ("While connecting to session manager:\n%s.",
448 vals = g_new (SmPropValue, 1);
449 vals[0].length = strlen(argv0);
450 vals[0].value = argv0;
451 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
452 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
453 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
455 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
456 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
457 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
463 int main(int argc, char *argv[])
467 FolderView *folderview;
469 gboolean crash_file_present = FALSE;
470 gint num_folder_class = 0;
471 START_TIMING("startup");
473 if (!sylpheed_init(&argc, &argv)) {
477 prefs_prepare_cache();
478 prog_version = PROG_VERSION;
479 argv0 = g_strdup(argv[0]);
481 parse_cmd_opt(argc, argv);
484 plugin_load_all("Common");
489 gtk_init(&argc, &argv);
490 crash_main(cmd.crash_params);
493 crash_install_handlers();
495 install_basic_sighandlers();
498 /* check and create unix domain socket for remote operation */
500 lock_socket = prohibit_duplicate_launch();
501 if (lock_socket < 0) {
502 #ifdef HAVE_STARTUP_NOTIFICATION
503 if(gtk_init_check(&argc, &argv))
504 startup_notification_complete(TRUE);
509 if (cmd.status || cmd.status_full) {
510 puts("0 Sylpheed-Claws not running.");
511 lock_socket_remove();
518 if (!g_thread_supported())
522 gtk_init(&argc, &argv);
525 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
526 gtk_widget_set_default_visual(gdk_rgb_get_visual());
528 if (!g_thread_supported()) {
529 g_error(_("g_thread is not supported by glib.\n"));
532 /* check that we're not on a too recent/old gtk+ */
533 #if GTK_CHECK_VERSION(2, 9, 0)
534 if (gtk_check_version(2, 9, 0) != NULL) {
535 alertpanel_error(_("Sylpheed-Claws has been compiled with "
536 "a more recent GTK+ library than is "
537 "currently available. This will cause "
538 "crashes. You need to upgrade GTK+ or "
539 "recompile Sylpheed-Claws."));
543 if (gtk_check_version(2, 9, 0) == NULL) {
544 alertpanel_error(_("Sylpheed-Claws has been compiled with "
545 "an older GTK+ library than is "
546 "currently available. This will cause "
547 "crashes. You need to recompile "
552 /* parse gtkrc files */
553 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
555 gtk_rc_parse(userrc);
557 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
558 G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
559 gtk_rc_parse(userrc);
562 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
563 if (!is_dir_exist(RC_DIR)) {
564 prefs_destroy_cache();
566 if (is_dir_exist(OLD_GTK2_RC_DIR))
567 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR);
568 else if (is_dir_exist(OLD_GTK1_RC_DIR))
569 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR);
570 if (r == FALSE && !is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0)
574 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
575 gtk_rc_parse(userrc);
578 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
579 gtk_accel_map_load (userrc);
582 gtk_settings_set_long_property(gtk_settings_get_default(),
583 "gtk-can-change-accels",
584 (glong)TRUE, "XProperty");
586 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
588 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
589 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
590 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
591 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
592 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
593 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
595 crash_file_present = is_file_exist(get_crashfile_name());
596 /* remove temporary files */
597 remove_all_files(get_tmp_dir());
598 remove_all_files(get_mime_tmp_dir());
600 if (is_file_exist("sylpheed.log")) {
601 if (rename_force("sylpheed.log", "sylpheed.log.bak") < 0)
602 FILE_OP_ERROR("sylpheed.log", "rename");
604 set_log_file("sylpheed.log");
606 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
608 folder_system_init();
609 prefs_common_read_config();
613 prefs_ext_prog_init();
614 prefs_wrapping_init();
615 prefs_compose_writing_init();
616 prefs_msg_colors_init();
618 prefs_image_viewer_init();
620 prefs_summaries_init();
621 prefs_message_init();
623 prefs_receive_init();
626 gtkaspell_checkers_init();
627 prefs_spelling_init();
630 sock_set_io_timeout(prefs_common.io_timeout_secs);
632 imap_main_set_timeout(prefs_common.io_timeout_secs);
634 prefs_actions_read_config();
635 prefs_display_header_read_config();
636 /* prefs_filtering_read_config(); */
637 addressbook_read_file();
638 renderer_read_config();
641 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_SYLPHEED_CLAWS_ICON, &icon);
642 gtk_window_set_default_icon(icon);
644 folderview_initialize();
650 mainwin = main_window_create
651 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
652 manage_window_focus_in(mainwin->window, NULL, NULL);
653 folderview = mainwin->folderview;
655 gtk_clist_freeze(GTK_CLIST(mainwin->folderview->ctree));
656 folder_item_update_freeze();
658 /* register the callback of unix domain socket input */
660 lock_socket_tag = gdk_input_add(lock_socket,
661 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
662 lock_socket_input_cb,
666 prefs_account_init();
667 account_read_config_all();
669 if (folder_read_list() < 0) {
670 prefs_destroy_cache();
671 if (!run_wizard(mainwin, TRUE))
673 main_window_reflect_prefs_all_now();
677 if (!account_get_list()) {
678 prefs_destroy_cache();
679 if (!run_wizard(mainwin, FALSE))
681 account_read_config_all();
682 if(!account_get_list())
683 exit_sylpheed(mainwin);
687 toolbar_main_set_sensitive(mainwin);
688 main_window_set_menu_sensitive(mainwin);
690 main_window_popup(mainwin);
693 imap_main_init(prefs_common.skip_ssl_cert_check);
695 account_set_missing_folder();
696 folder_set_missing_folders();
697 folderview_set(folderview);
699 prefs_matcher_read_config();
701 /* make one all-folder processing before using sylpheed */
702 main_window_cursor_wait(mainwin);
703 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
705 /* if Sylpheed crashed, rebuild caches */
706 if (!cmd.crash && crash_file_present) {
708 debug_print("Sylpheed-Claws crashed, checking for new messages in local folders\n");
709 folder_item_update_thaw();
710 folderview_check_new(NULL);
711 folder_clean_cache_memory_force();
712 folder_item_update_freeze();
714 /* make the crash-indicator file */
715 str_write_to_file("foo", get_crashfile_name());
717 inc_autocheck_timer_init(mainwin);
719 /* ignore SIGPIPE signal for preventing sudden death of program */
721 signal(SIGPIPE, SIG_IGN);
723 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
724 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
726 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
727 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
730 if (cmd.status_folders) {
731 g_ptr_array_free(cmd.status_folders, TRUE);
732 cmd.status_folders = NULL;
734 if (cmd.status_full_folders) {
735 g_ptr_array_free(cmd.status_full_folders, TRUE);
736 cmd.status_full_folders = NULL;
739 sylpheed_register_idle_function(sylpheed_gtk_idle);
741 prefs_toolbar_init();
743 num_folder_class = g_list_length(folder_get_list());
745 plugin_load_all("GTK2");
747 if (g_list_length(folder_get_list()) != num_folder_class) {
748 debug_print("new folders loaded, reloading processing rules\n");
749 prefs_matcher_read_config();
752 if (plugin_get_unloaded_list() != NULL) {
753 main_window_cursor_normal(mainwin);
754 alertpanel_warning(_("Some plugin(s) failed to load. "
755 "Check the Plugins configuration "
756 "for more information."));
757 main_window_cursor_wait(mainwin);
760 plugin_load_standard_plugins ();
762 if (!folder_have_mailbox()) {
763 prefs_destroy_cache();
764 main_window_cursor_normal(mainwin);
765 alertpanel_error(_("Sylpheed-Claws has detected a configured "
766 "mailbox, but could not load it. It is "
767 "probably provided by an out-of-date "
768 "external plugin. Please reinstall the "
769 "plugin and try again."));
773 static_mainwindow = mainwin;
775 #ifdef HAVE_STARTUP_NOTIFICATION
776 startup_notification_complete(FALSE);
779 sc_session_manager_connect(mainwin);
781 folder_item_update_thaw();
782 gtk_clist_thaw(GTK_CLIST(mainwin->folderview->ctree));
783 main_window_cursor_normal(mainwin);
785 if (cmd.receive_all) {
786 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
787 } else if (prefs_common.chk_on_startup) {
788 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
789 } else if (cmd.receive) {
790 g_timeout_add(1000, defer_check, NULL);
792 gtk_widget_grab_focus(folderview->ctree);
796 open_compose_new(cmd.compose_mailto, cmd.attach_files);
798 if (cmd.attach_files) {
799 ptr_array_free_strings(cmd.attach_files);
800 g_ptr_array_free(cmd.attach_files, TRUE);
801 cmd.attach_files = NULL;
804 folder_subscribe(cmd.subscribe_uri);
812 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
815 prefs_destroy_cache();
819 exit_sylpheed(mainwin);
824 static void save_all_caches(FolderItem *item, gpointer data)
831 folder_item_close(item);
833 folder_item_free_cache(item, TRUE);
836 static gboolean sc_exiting = FALSE;
838 static void exit_sylpheed(MainWindow *mainwin)
844 debug_print("shutting down\n");
845 inc_autocheck_timer_remove();
847 if (prefs_common.clean_on_exit && !emergency_exit) {
848 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
851 /* save prefs for opened folder */
852 if(mainwin->folderview->opened) {
855 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
856 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
859 /* save all state before exiting */
860 folder_func_to_all_folders(save_all_caches, NULL);
863 main_window_get_size(mainwin);
864 main_window_get_position(mainwin);
865 prefs_common_write_config();
866 account_write_config_all();
867 addressbook_export_to_file();
869 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
870 gtk_accel_map_save(filename);
873 /* delete temporary files */
874 remove_all_files(get_tmp_dir());
875 remove_all_files(get_mime_tmp_dir());
882 /* delete crashfile */
884 g_unlink(get_crashfile_name());
886 lock_socket_remove();
889 if (mainwin->smc_conn)
890 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
891 mainwin->smc_conn = NULL;
894 main_window_destroy_all();
896 plugin_unload_all("GTK2");
898 prefs_toolbar_done();
900 addressbook_destroy();
904 prefs_ext_prog_done();
905 prefs_wrapping_done();
906 prefs_compose_writing_done();
907 prefs_msg_colors_done();
908 prefs_image_viewer_done();
911 prefs_summaries_done();
912 prefs_message_done();
914 prefs_receive_done();
917 prefs_spelling_done();
918 gtkaspell_checkers_quit();
920 plugin_unload_all("Common");
924 static void parse_cmd_opt(int argc, char *argv[])
928 for (i = 1; i < argc; i++) {
929 if (!strncmp(argv[i], "--receive-all", 13)) {
930 cmd.receive_all = TRUE;
931 } else if (!strncmp(argv[i], "--receive", 9)) {
933 } else if (!strncmp(argv[i], "--compose", 9)) {
934 const gchar *p = argv[i + 1];
937 cmd.compose_mailto = NULL;
938 if (p && *p != '\0' && *p != '-') {
939 if (!strncmp(p, "mailto:", 7)) {
940 cmd.compose_mailto = p + 7;
942 cmd.compose_mailto = p;
946 } else if (!strncmp(argv[i], "--subscribe", 11)) {
947 const gchar *p = argv[i + 1];
948 if (p && *p != '\0' && *p != '-') {
949 cmd.subscribe = TRUE;
950 cmd.subscribe_uri = p;
952 } else if (!strncmp(argv[i], "--attach", 8)) {
953 const gchar *p = argv[i + 1];
956 while (p && *p != '\0' && *p != '-') {
957 if (!cmd.attach_files) {
958 cmd.attach_files = g_ptr_array_new();
960 if (*p != G_DIR_SEPARATOR) {
961 file = g_strconcat(sylpheed_get_startup_dir(),
967 g_ptr_array_add(cmd.attach_files, file);
971 } else if (!strncmp(argv[i], "--send", 6)) {
973 } else if (!strncmp(argv[i], "--version", 9)) {
974 puts("Sylpheed-Claws version " VERSION);
976 } else if (!strncmp(argv[i], "--status-full", 13)) {
977 const gchar *p = argv[i + 1];
979 cmd.status_full = TRUE;
980 while (p && *p != '\0' && *p != '-') {
981 if (!cmd.status_full_folders) {
982 cmd.status_full_folders =
985 g_ptr_array_add(cmd.status_full_folders,
990 } else if (!strncmp(argv[i], "--status", 8)) {
991 const gchar *p = argv[i + 1];
994 while (p && *p != '\0' && *p != '-') {
995 if (!cmd.status_folders)
996 cmd.status_folders = g_ptr_array_new();
997 g_ptr_array_add(cmd.status_folders,
1002 } else if (!strncmp(argv[i], "--online", 8)) {
1003 cmd.online_mode = ONLINE_MODE_ONLINE;
1004 } else if (!strncmp(argv[i], "--offline", 9)) {
1005 cmd.online_mode = ONLINE_MODE_OFFLINE;
1006 } else if (!strncmp(argv[i], "--help", 6)) {
1007 gchar *base = g_path_get_basename(argv[0]);
1008 g_print(_("Usage: %s [OPTION]...\n"), base);
1010 g_print("%s\n", _(" --compose [address] open composition window"));
1011 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1012 g_print("%s\n", _(" --attach file1 [file2]...\n"
1013 " open composition window with specified files\n"
1015 g_print("%s\n", _(" --receive receive new messages"));
1016 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1017 g_print("%s\n", _(" --send send all queued messages"));
1018 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1019 g_print("%s\n", _(" --status-full [folder]...\n"
1020 " show the status of each folder"));
1021 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1022 " folder is a folder id like '#mh/Mailbox/inbox'"));
1023 g_print("%s\n", _(" --online switch to online mode"));
1024 g_print("%s\n", _(" --offline switch to offline mode"));
1025 g_print("%s\n", _(" --exit exit Sylpheed-Claws"));
1026 g_print("%s\n", _(" --debug debug mode"));
1027 g_print("%s\n", _(" --help display this help and exit"));
1028 g_print("%s\n", _(" --version output version information and exit"));
1029 g_print("%s\n", _(" --config-dir output configuration directory"));
1033 } else if (!strncmp(argv[i], "--crash", 7)) {
1035 cmd.crash_params = g_strdup(argv[i + 1]);
1037 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1040 } else if (!strncmp(argv[i], "--exit", 6)) {
1042 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1043 cmd.target = argv[i+1];
1044 } else if (i == 1 && argc == 2) {
1045 /* only one parameter. Do something intelligent about it */
1046 if (strstr(argv[i], "@") && !strstr(argv[i], "://")) {
1047 const gchar *p = argv[i];
1050 cmd.compose_mailto = NULL;
1051 if (p && *p != '\0' && *p != '-') {
1052 if (!strncmp(p, "mailto:", 7)) {
1053 cmd.compose_mailto = p + 7;
1055 cmd.compose_mailto = p;
1058 } else if (strstr(argv[i], "://")) {
1059 const gchar *p = argv[i];
1060 if (p && *p != '\0' && *p != '-') {
1061 cmd.subscribe = TRUE;
1062 cmd.subscribe_uri = p;
1069 if (cmd.attach_files && cmd.compose == FALSE) {
1071 cmd.compose_mailto = NULL;
1075 static gint get_queued_message_num(void)
1079 queue = folder_get_default_queue();
1084 folder_item_scan(queue);
1085 return queue->total_msgs;
1088 static void initial_processing(FolderItem *item, gpointer data)
1090 MainWindow *mainwin = (MainWindow *)data;
1093 g_return_if_fail(item);
1094 buf = g_strdup_printf(_("Processing (%s)..."),
1097 : _("top level folder"));
1101 if (item->prefs->enable_processing) {
1102 folder_item_apply_processing(item);
1105 STATUSBAR_POP(mainwin);
1108 static void draft_all_messages(void)
1110 GList *compose_list = NULL;
1112 while ((compose_list = compose_get_compose_list()) != NULL) {
1113 Compose *c = (Compose*)compose_list->data;
1117 gboolean clean_quit(gpointer data)
1119 static gboolean firstrun = TRUE;
1126 /*!< Good idea to have the main window stored in a
1127 * static variable so we can check that variable
1128 * to see if we're really allowed to do things
1129 * that actually the spawner is supposed to
1130 * do (like: sending mail, composing messages).
1131 * Because, really, if we're the spawnee, and
1132 * we touch GTK stuff, we're hosed. See the
1135 /* FIXME: Use something else to signal that we're
1136 * in the original spawner, and not in a spawned
1138 if (!static_mainwindow) {
1142 draft_all_messages();
1143 emergency_exit = TRUE;
1144 exit_sylpheed(static_mainwindow);
1150 void app_will_exit(GtkWidget *widget, gpointer data)
1152 MainWindow *mainwin = data;
1154 if (sc_exiting == TRUE) {
1155 debug_print("exit pending\n");
1159 debug_print("exiting\n");
1160 if (compose_get_compose_list()) {
1161 gint val = alertpanel(_("Really quit?"),
1162 _("Composing message exists."),
1163 _("_Save to Draft"), _("_Discard them"), _("Do_n't quit"));
1168 case G_ALERTALTERNATE:
1171 draft_all_messages();
1174 manage_window_focus_in(mainwin->window, NULL, NULL);
1177 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
1178 if (alertpanel(_("Queued messages"),
1179 _("Some unsent messages are queued. Exit now?"),
1180 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
1181 != G_ALERTALTERNATE) {
1185 manage_window_focus_in(mainwin->window, NULL, NULL);
1189 if (folderview_get_selected_item(mainwin->folderview))
1190 folder_item_close(folderview_get_selected_item(mainwin->folderview));
1194 gboolean sylpheed_is_exiting(void)
1200 * CLAWS: want this public so crash dialog can delete the
1206 gchar *get_socket_name(void)
1208 static gchar *filename = NULL;
1210 if (filename == NULL) {
1211 filename = g_strdup_printf("%s%csylpheed-claws-%d",
1212 g_get_tmp_dir(), G_DIR_SEPARATOR,
1223 static gchar *get_crashfile_name(void)
1225 static gchar *filename = NULL;
1227 if (filename == NULL) {
1228 filename = g_strdup_printf("%s%csylpheed-crashed",
1229 get_tmp_dir(), G_DIR_SEPARATOR);
1235 static gint prohibit_duplicate_launch(void)
1240 path = get_socket_name();
1241 uxsock = fd_connect_unix(path);
1244 return fd_open_unix(path);
1247 /* remote command mode */
1249 debug_print("another Sylpheed-Claws instance is already running.\n");
1251 if (cmd.receive_all) {
1252 fd_write_all(uxsock, "receive_all\n", 12);
1253 } else if (cmd.receive) {
1254 fd_write_all(uxsock, "receive\n", 8);
1255 } else if (cmd.compose && cmd.attach_files) {
1256 gchar *str, *compose_str;
1259 if (cmd.compose_mailto) {
1260 compose_str = g_strdup_printf("compose_attach %s\n",
1261 cmd.compose_mailto);
1263 compose_str = g_strdup("compose_attach\n");
1266 fd_write_all(uxsock, compose_str, strlen(compose_str));
1267 g_free(compose_str);
1269 for (i = 0; i < cmd.attach_files->len; i++) {
1270 str = g_ptr_array_index(cmd.attach_files, i);
1271 fd_write_all(uxsock, str, strlen(str));
1272 fd_write_all(uxsock, "\n", 1);
1275 fd_write_all(uxsock, ".\n", 2);
1276 } else if (cmd.compose) {
1279 if (cmd.compose_mailto) {
1280 compose_str = g_strdup_printf
1281 ("compose %s\n", cmd.compose_mailto);
1283 compose_str = g_strdup("compose\n");
1286 fd_write_all(uxsock, compose_str, strlen(compose_str));
1287 g_free(compose_str);
1288 } else if (cmd.subscribe) {
1289 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
1290 fd_write_all(uxsock, str, strlen(str));
1292 } else if (cmd.send) {
1293 fd_write_all(uxsock, "send\n", 5);
1294 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1295 fd_write(uxsock, "online\n", 6);
1296 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1297 fd_write(uxsock, "offline\n", 7);
1298 } else if (cmd.status || cmd.status_full) {
1299 gchar buf[BUFFSIZE];
1301 const gchar *command;
1305 command = cmd.status_full ? "status-full\n" : "status\n";
1306 folders = cmd.status_full ? cmd.status_full_folders :
1309 fd_write_all(uxsock, command, strlen(command));
1310 for (i = 0; folders && i < folders->len; ++i) {
1311 folder = g_ptr_array_index(folders, i);
1312 fd_write_all(uxsock, folder, strlen(folder));
1313 fd_write_all(uxsock, "\n", 1);
1315 fd_write_all(uxsock, ".\n", 2);
1317 fd_gets(uxsock, buf, sizeof(buf));
1318 if (!strncmp(buf, ".\n", 2)) break;
1321 } else if (cmd.exit) {
1322 fd_write_all(uxsock, "exit\n", 5);
1323 } else if (cmd.target) {
1324 gchar *str = g_strdup_printf("select %s\n", cmd.target);
1325 fd_write_all(uxsock, str, strlen(str));
1328 fd_write_all(uxsock, "popup\n", 6);
1334 static gint lock_socket_remove(void)
1339 if (lock_socket < 0) {
1343 if (lock_socket_tag > 0) {
1344 gdk_input_remove(lock_socket_tag);
1346 fd_close(lock_socket);
1347 filename = get_socket_name();
1354 static GPtrArray *get_folder_item_list(gint sock)
1356 gchar buf[BUFFSIZE];
1358 GPtrArray *folders = NULL;
1361 fd_gets(sock, buf, sizeof(buf));
1362 if (!strncmp(buf, ".\n", 2)) {
1367 folders = g_ptr_array_new();
1369 item = folder_find_item_from_identifier(buf);
1371 g_ptr_array_add(folders, item);
1373 g_warning("no such folder: %s\n", buf);
1380 static void lock_socket_input_cb(gpointer data,
1382 GdkInputCondition condition)
1384 MainWindow *mainwin = (MainWindow *)data;
1386 gchar buf[BUFFSIZE];
1388 sock = fd_accept(source);
1389 fd_gets(sock, buf, sizeof(buf));
1391 if (!strncmp(buf, "popup", 5)) {
1392 main_window_popup(mainwin);
1393 } else if (!strncmp(buf, "receive_all", 11)) {
1394 inc_all_account_mail(mainwin, FALSE,
1395 prefs_common.newmail_notify_manu);
1396 } else if (!strncmp(buf, "receive", 7)) {
1397 inc_mail(mainwin, prefs_common.newmail_notify_manu);
1398 } else if (!strncmp(buf, "compose_attach", 14)) {
1402 mailto = g_strdup(buf + strlen("compose_attach") + 1);
1403 files = g_ptr_array_new();
1404 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
1405 if (buf[0] == '.' && buf[1] == '\n') {
1409 g_ptr_array_add(files, g_strdup(buf));
1411 open_compose_new(mailto, files);
1412 ptr_array_free_strings(files);
1413 g_ptr_array_free(files, TRUE);
1415 } else if (!strncmp(buf, "compose", 7)) {
1416 open_compose_new(buf + strlen("compose") + 1, NULL);
1417 } else if (!strncmp(buf, "subscribe", 9)) {
1418 main_window_popup(mainwin);
1419 folder_subscribe(buf + strlen("subscribe") + 1);
1420 } else if (!strncmp(buf, "send", 4)) {
1422 } else if (!strncmp(buf, "online", 6)) {
1423 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1424 } else if (!strncmp(buf, "offline", 7)) {
1425 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1426 } else if (!strncmp(buf, "status-full", 11) ||
1427 !strncmp(buf, "status", 6)) {
1431 folders = get_folder_item_list(sock);
1432 status = folder_get_status
1433 (folders, !strncmp(buf, "status-full", 11));
1434 fd_write_all(sock, status, strlen(status));
1435 fd_write_all(sock, ".\n", 2);
1437 if (folders) g_ptr_array_free(folders, TRUE);
1438 } else if (!strncmp(buf, "select ", 7)) {
1439 const gchar *target = buf+7;
1440 mainwindow_jump_to(target);
1441 } else if (!strncmp(buf, "exit", 4)) {
1442 app_will_exit(NULL, mainwin);
1448 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
1453 Xstrdup_a(addr, address, return);
1457 compose_new(NULL, addr, attach_files);
1460 static void send_queue(void)
1463 gchar *errstr = NULL;
1464 for (list = folder_get_list(); list != NULL; list = list->next) {
1465 Folder *folder = list->data;
1467 if (folder->queue) {
1468 gint res = procmsg_send_queue
1469 (folder->queue, prefs_common.savemsg,
1473 folder_item_scan(folder->queue);
1478 gchar *tmp = g_strdup_printf(_("Some errors occurred "
1479 "while sending queued messages:\n%s"), errstr);
1481 alertpanel_error_log(tmp);
1484 alertpanel_error_log("Some errors occurred "
1485 "while sending queued messages.");
1489 static void quit_signal_handler(int sig)
1491 debug_print("Quitting on signal %d\n", sig);
1493 g_timeout_add(0, clean_quit, NULL);
1496 static void install_basic_sighandlers()
1500 struct sigaction act;
1505 sigaddset(&mask, SIGTERM);
1508 sigaddset(&mask, SIGINT);
1511 sigaddset(&mask, SIGHUP);
1514 act.sa_handler = quit_signal_handler;
1519 sigaction(SIGTERM, &act, 0);
1522 sigaction(SIGINT, &act, 0);
1525 sigaction(SIGHUP, &act, 0);
1528 sigprocmask(SIG_UNBLOCK, &mask, 0);
1529 #endif /* !G_OS_WIN32 */