2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail 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_claws (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 claws_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, const gchar *oldversion)
270 gchar *message = g_strdup_printf(_("Configuration for %s (or previous) found.\n"
271 "Do you want to migrate this configuration?"), oldversion);
273 GtkWidget *window = NULL;
274 if (alertpanel(_("Migration of configuration"),
276 GTK_STOCK_NO, GTK_STOCK_YES, NULL) != G_ALERTALTERNATE) {
280 window = label_window_create(_("Copying configuration..."));
282 r = copy_dir(old_cfg_dir, new_cfg_dir);
283 gtk_widget_destroy(window);
285 alertpanel_error(_("Migration failed!"));
290 static void migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
293 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
295 oldfp = g_fopen(old_rc, "r");
298 newfp = g_fopen(new_rc, "w");
304 plugin_path = g_strdup(get_plugin_dir());
305 new_plugin_path = g_strdup(plugin_path);
307 if (strstr(plugin_path, "/claws-mail/")) {
308 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
309 *(strstr(plugin_path, "/claws-mail/")) = '\0';
310 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
313 old_plugin_path = g_strdup(new_plugin_path);
315 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
316 while (fgets(buf, sizeof(buf), oldfp)) {
317 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
320 debug_print("->replacing %s", buf);
321 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
322 fputs(new_plugin_path, newfp);
323 fputs(buf+strlen(old_plugin_path), newfp);
327 g_free(new_plugin_path);
328 g_free(old_plugin_path);
335 sc_client_set_value (MainWindow *mainwin,
346 prop.num_vals = num_vals;
350 if (mainwin->smc_conn)
351 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
354 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
359 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
363 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
365 MainWindow *mainwin = (MainWindow *)client_data;
366 if (mainwin->smc_conn)
367 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
370 static void sc_save_yourself_callback (SmcConn smc_conn,
371 SmPointer client_data,
377 MainWindow *mainwin = (MainWindow *)client_data;
378 if (mainwin->smc_conn)
379 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
382 static IceIOErrorHandler sc_ice_installed_handler;
384 static void sc_ice_io_error_handler (IceConn connection)
386 if (sc_ice_installed_handler)
387 (*sc_ice_installed_handler) (connection);
389 static gboolean sc_process_ice_messages (GIOChannel *source,
390 GIOCondition condition,
393 IceConn connection = (IceConn) data;
394 IceProcessMessagesStatus status;
396 status = IceProcessMessages (connection, NULL, NULL);
398 if (status == IceProcessMessagesIOError) {
399 IcePointer context = IceGetConnectionContext (connection);
401 if (context && GTK_IS_OBJECT (context)) {
402 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
404 if (disconnect_id > 0)
405 g_signal_emit (context, disconnect_id, 0);
407 IceSetShutdownNegotiation (connection, False);
408 IceCloseConnection (connection);
415 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
416 IcePointer *watch_data)
422 /* Make sure we don't pass on these file descriptors to any
424 fcntl(IceConnectionNumber(connection),F_SETFD,
425 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
427 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
428 input_id = g_io_add_watch (channel,
429 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
430 sc_process_ice_messages,
432 g_io_channel_unref (channel);
434 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
436 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
437 g_source_remove (input_id);
441 static void sc_session_manager_connect(MainWindow *mainwin)
443 static gboolean connected = FALSE;
444 SmcCallbacks callbacks;
446 IceIOErrorHandler default_handler;
453 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
454 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
456 if (sc_ice_installed_handler == default_handler)
457 sc_ice_installed_handler = NULL;
459 IceAddConnectionWatch (new_ice_connection, NULL);
462 callbacks.save_yourself.callback = sc_save_yourself_callback;
463 callbacks.die.callback = sc_die_callback;
464 callbacks.save_complete.callback = sc_save_complete_callback;
465 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
467 callbacks.save_yourself.client_data =
468 callbacks.die.client_data =
469 callbacks.save_complete.client_data =
470 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
471 if (g_getenv ("SESSION_MANAGER")) {
472 gchar error_string_ret[256] = "";
474 mainwin->smc_conn = (gpointer)
475 SmcOpenConnection (NULL, mainwin,
476 SmProtoMajor, SmProtoMinor,
477 SmcSaveYourselfProcMask | SmcDieProcMask |
478 SmcSaveCompleteProcMask |
479 SmcShutdownCancelledProcMask,
482 256, error_string_ret);
484 if (error_string_ret[0] || mainwin->smc_conn == NULL)
485 g_warning ("While connecting to session manager:\n%s.",
489 vals = g_new (SmPropValue, 1);
490 vals[0].length = strlen(argv0);
491 vals[0].value = argv0;
492 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
493 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
494 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
496 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
497 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
498 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
504 int main(int argc, char *argv[])
508 FolderView *folderview;
510 gboolean crash_file_present = FALSE;
511 gint num_folder_class = 0;
512 START_TIMING("startup");
514 if (!claws_init(&argc, &argv)) {
518 prefs_prepare_cache();
519 prog_version = PROG_VERSION;
520 argv0 = g_strdup(argv[0]);
522 parse_cmd_opt(argc, argv);
527 gtk_init(&argc, &argv);
528 crash_main(cmd.crash_params);
531 crash_install_handlers();
533 install_basic_sighandlers();
536 /* check and create unix domain socket for remote operation */
538 lock_socket = prohibit_duplicate_launch();
539 if (lock_socket < 0) {
540 #ifdef HAVE_STARTUP_NOTIFICATION
541 if(gtk_init_check(&argc, &argv))
542 startup_notification_complete(TRUE);
547 if (cmd.status || cmd.status_full) {
548 puts("0 Claws Mail not running.");
549 lock_socket_remove();
556 if (!g_thread_supported())
560 gtk_init(&argc, &argv);
563 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
564 gtk_widget_set_default_visual(gdk_rgb_get_visual());
566 if (!g_thread_supported()) {
567 g_error(_("g_thread is not supported by glib.\n"));
570 /* check that we're not on a too recent/old gtk+ */
571 #if GTK_CHECK_VERSION(2, 9, 0)
572 if (gtk_check_version(2, 9, 0) != NULL) {
573 alertpanel_error(_("Claws Mail has been compiled with "
574 "a more recent GTK+ library than is "
575 "currently available. This will cause "
576 "crashes. You need to upgrade GTK+ or "
577 "recompile Claws Mail."));
581 if (gtk_check_version(2, 9, 0) == NULL) {
582 alertpanel_error(_("Claws Mail has been compiled with "
583 "an older GTK+ library than is "
584 "currently available. This will cause "
585 "crashes. You need to recompile "
590 /* parse gtkrc files */
591 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
593 gtk_rc_parse(userrc);
595 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
596 G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
597 gtk_rc_parse(userrc);
600 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
601 if (!is_dir_exist(RC_DIR)) {
602 prefs_destroy_cache();
604 if (is_dir_exist(OLD_GTK2_RC_DIR))
605 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 2.6.0"));
606 else if (is_dir_exist(OLDER_GTK2_RC_DIR))
607 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.9.15"));
608 else if (is_dir_exist(OLD_GTK1_RC_DIR))
609 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, _("Sylpheed-Claws 1.0.5"));
610 if (r == FALSE && !is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0)
613 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
614 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
615 /* post 2.6 name change */
616 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
617 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
621 plugin_load_all("Common");
623 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
624 gtk_rc_parse(userrc);
627 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
628 gtk_accel_map_load (userrc);
631 gtk_settings_set_long_property(gtk_settings_get_default(),
632 "gtk-can-change-accels",
633 (glong)TRUE, "XProperty");
635 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
637 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
638 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
639 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
640 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
641 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
642 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
644 crash_file_present = is_file_exist(get_crashfile_name());
645 /* remove temporary files */
646 remove_all_files(get_tmp_dir());
647 remove_all_files(get_mime_tmp_dir());
649 if (is_file_exist("claws.log")) {
650 if (rename_force("claws.log", "claws.log.bak") < 0)
651 FILE_OP_ERROR("claws.log", "rename");
653 set_log_file("claws.log");
655 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
657 folder_system_init();
658 prefs_common_read_config();
662 prefs_ext_prog_init();
663 prefs_wrapping_init();
664 prefs_compose_writing_init();
665 prefs_msg_colors_init();
667 prefs_image_viewer_init();
669 prefs_summaries_init();
670 prefs_message_init();
672 prefs_receive_init();
675 gtkaspell_checkers_init();
676 prefs_spelling_init();
679 sock_set_io_timeout(prefs_common.io_timeout_secs);
681 imap_main_set_timeout(prefs_common.io_timeout_secs);
683 prefs_actions_read_config();
684 prefs_display_header_read_config();
685 /* prefs_filtering_read_config(); */
686 addressbook_read_file();
687 renderer_read_config();
690 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
691 gtk_window_set_default_icon(icon);
693 folderview_initialize();
699 mainwin = main_window_create
700 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
701 manage_window_focus_in(mainwin->window, NULL, NULL);
702 folderview = mainwin->folderview;
704 gtk_clist_freeze(GTK_CLIST(mainwin->folderview->ctree));
705 folder_item_update_freeze();
707 /* register the callback of unix domain socket input */
709 lock_socket_tag = gdk_input_add(lock_socket,
710 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
711 lock_socket_input_cb,
715 prefs_account_init();
716 account_read_config_all();
718 if (folder_read_list() < 0) {
719 prefs_destroy_cache();
720 if (!run_wizard(mainwin, TRUE))
722 main_window_reflect_prefs_all_now();
726 if (!account_get_list()) {
727 prefs_destroy_cache();
728 if (!run_wizard(mainwin, FALSE))
730 account_read_config_all();
731 if(!account_get_list()) {
738 toolbar_main_set_sensitive(mainwin);
739 main_window_set_menu_sensitive(mainwin);
741 main_window_popup(mainwin);
744 imap_main_init(prefs_common.skip_ssl_cert_check);
746 account_set_missing_folder();
747 folder_set_missing_folders();
748 folderview_set(folderview);
750 prefs_matcher_read_config();
752 /* make one all-folder processing before using claws */
753 main_window_cursor_wait(mainwin);
754 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
756 /* if claws crashed, rebuild caches */
757 if (!cmd.crash && crash_file_present) {
759 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
760 folder_item_update_thaw();
761 folderview_check_new(NULL);
762 folder_clean_cache_memory_force();
763 folder_item_update_freeze();
765 /* make the crash-indicator file */
766 str_write_to_file("foo", get_crashfile_name());
768 inc_autocheck_timer_init(mainwin);
770 /* ignore SIGPIPE signal for preventing sudden death of program */
772 signal(SIGPIPE, SIG_IGN);
774 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
775 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
777 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
778 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
781 if (cmd.status_folders) {
782 g_ptr_array_free(cmd.status_folders, TRUE);
783 cmd.status_folders = NULL;
785 if (cmd.status_full_folders) {
786 g_ptr_array_free(cmd.status_full_folders, TRUE);
787 cmd.status_full_folders = NULL;
790 claws_register_idle_function(claws_gtk_idle);
792 prefs_toolbar_init();
794 num_folder_class = g_list_length(folder_get_list());
796 plugin_load_all("GTK2");
798 if (g_list_length(folder_get_list()) != num_folder_class) {
799 debug_print("new folders loaded, reloading processing rules\n");
800 prefs_matcher_read_config();
803 if (plugin_get_unloaded_list() != NULL) {
804 main_window_cursor_normal(mainwin);
805 alertpanel_warning(_("Some plugin(s) failed to load. "
806 "Check the Plugins configuration "
807 "for more information."));
808 main_window_cursor_wait(mainwin);
811 plugin_load_standard_plugins ();
813 if (!folder_have_mailbox()) {
814 prefs_destroy_cache();
815 main_window_cursor_normal(mainwin);
816 if (folder_get_list() != NULL) {
817 alertpanel_error(_("Claws Mail has detected a configured "
818 "mailbox, but is it incomplete. It is "
819 "possibly due to a failing IMAP account. Use "
820 "\"Rebuild folder tree\" on the mailbox's folder "
821 "to try to fix it."));
823 alertpanel_error(_("Claws Mail has detected a configured "
824 "mailbox, but could not load it. It is "
825 "probably provided by an out-of-date "
826 "external plugin. Please reinstall the "
827 "plugin and try again."));
833 static_mainwindow = mainwin;
835 #ifdef HAVE_STARTUP_NOTIFICATION
836 startup_notification_complete(FALSE);
839 sc_session_manager_connect(mainwin);
841 folder_item_update_thaw();
842 gtk_clist_thaw(GTK_CLIST(mainwin->folderview->ctree));
843 main_window_cursor_normal(mainwin);
845 if (cmd.receive_all) {
846 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
847 } else if (prefs_common.chk_on_startup) {
848 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
849 } else if (cmd.receive) {
850 g_timeout_add(1000, defer_check, NULL);
852 gtk_widget_grab_focus(folderview->ctree);
856 open_compose_new(cmd.compose_mailto, cmd.attach_files);
858 if (cmd.attach_files) {
859 ptr_array_free_strings(cmd.attach_files);
860 g_ptr_array_free(cmd.attach_files, TRUE);
861 cmd.attach_files = NULL;
864 folder_subscribe(cmd.subscribe_uri);
872 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
875 prefs_destroy_cache();
884 static void save_all_caches(FolderItem *item, gpointer data)
891 folder_item_close(item);
893 folder_item_free_cache(item, TRUE);
896 static gboolean sc_exiting = FALSE;
898 static void exit_claws(MainWindow *mainwin)
904 debug_print("shutting down\n");
905 inc_autocheck_timer_remove();
907 if (prefs_common.clean_on_exit && !emergency_exit) {
908 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
911 /* save prefs for opened folder */
912 if(mainwin->folderview->opened) {
915 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
916 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
919 /* save all state before exiting */
920 folder_func_to_all_folders(save_all_caches, NULL);
923 main_window_get_size(mainwin);
924 main_window_get_position(mainwin);
925 prefs_common_write_config();
926 account_write_config_all();
927 addressbook_export_to_file();
929 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
930 gtk_accel_map_save(filename);
933 /* delete temporary files */
934 remove_all_files(get_tmp_dir());
935 remove_all_files(get_mime_tmp_dir());
942 /* delete crashfile */
944 g_unlink(get_crashfile_name());
946 lock_socket_remove();
949 if (mainwin->smc_conn)
950 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
951 mainwin->smc_conn = NULL;
954 main_window_destroy_all();
956 plugin_unload_all("GTK2");
958 prefs_toolbar_done();
960 addressbook_destroy();
964 prefs_ext_prog_done();
965 prefs_wrapping_done();
966 prefs_compose_writing_done();
967 prefs_msg_colors_done();
968 prefs_image_viewer_done();
971 prefs_summaries_done();
972 prefs_message_done();
974 prefs_receive_done();
977 prefs_spelling_done();
978 gtkaspell_checkers_quit();
980 plugin_unload_all("Common");
984 static void parse_cmd_opt(int argc, char *argv[])
988 for (i = 1; i < argc; i++) {
989 if (!strncmp(argv[i], "--receive-all", 13)) {
990 cmd.receive_all = TRUE;
991 } else if (!strncmp(argv[i], "--receive", 9)) {
993 } else if (!strncmp(argv[i], "--compose", 9)) {
994 const gchar *p = argv[i + 1];
997 cmd.compose_mailto = NULL;
998 if (p && *p != '\0' && *p != '-') {
999 if (!strncmp(p, "mailto:", 7)) {
1000 cmd.compose_mailto = p + 7;
1002 cmd.compose_mailto = p;
1006 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1007 const gchar *p = argv[i + 1];
1008 if (p && *p != '\0' && *p != '-') {
1009 cmd.subscribe = TRUE;
1010 cmd.subscribe_uri = p;
1012 } else if (!strncmp(argv[i], "--attach", 8)) {
1013 const gchar *p = argv[i + 1];
1016 while (p && *p != '\0' && *p != '-') {
1017 if (!cmd.attach_files) {
1018 cmd.attach_files = g_ptr_array_new();
1020 if (*p != G_DIR_SEPARATOR) {
1021 file = g_strconcat(claws_get_startup_dir(),
1027 g_ptr_array_add(cmd.attach_files, file);
1031 } else if (!strncmp(argv[i], "--send", 6)) {
1033 } else if (!strncmp(argv[i], "--version", 9)) {
1034 puts("Claws Mail version " VERSION);
1036 } else if (!strncmp(argv[i], "--status-full", 13)) {
1037 const gchar *p = argv[i + 1];
1039 cmd.status_full = TRUE;
1040 while (p && *p != '\0' && *p != '-') {
1041 if (!cmd.status_full_folders) {
1042 cmd.status_full_folders =
1045 g_ptr_array_add(cmd.status_full_folders,
1050 } else if (!strncmp(argv[i], "--status", 8)) {
1051 const gchar *p = argv[i + 1];
1054 while (p && *p != '\0' && *p != '-') {
1055 if (!cmd.status_folders)
1056 cmd.status_folders = g_ptr_array_new();
1057 g_ptr_array_add(cmd.status_folders,
1062 } else if (!strncmp(argv[i], "--online", 8)) {
1063 cmd.online_mode = ONLINE_MODE_ONLINE;
1064 } else if (!strncmp(argv[i], "--offline", 9)) {
1065 cmd.online_mode = ONLINE_MODE_OFFLINE;
1066 } else if (!strncmp(argv[i], "--help", 6)) {
1067 gchar *base = g_path_get_basename(argv[0]);
1068 g_print(_("Usage: %s [OPTION]...\n"), base);
1070 g_print("%s\n", _(" --compose [address] open composition window"));
1071 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1072 g_print("%s\n", _(" --attach file1 [file2]...\n"
1073 " open composition window with specified files\n"
1075 g_print("%s\n", _(" --receive receive new messages"));
1076 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1077 g_print("%s\n", _(" --send send all queued messages"));
1078 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1079 g_print("%s\n", _(" --status-full [folder]...\n"
1080 " show the status of each folder"));
1081 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1082 " folder is a folder id like '#mh/Mailbox/inbox'"));
1083 g_print("%s\n", _(" --online switch to online mode"));
1084 g_print("%s\n", _(" --offline switch to offline mode"));
1085 g_print("%s\n", _(" --exit exit Claws Mail"));
1086 g_print("%s\n", _(" --debug debug mode"));
1087 g_print("%s\n", _(" --help display this help and exit"));
1088 g_print("%s\n", _(" --version output version information and exit"));
1089 g_print("%s\n", _(" --config-dir output configuration directory"));
1093 } else if (!strncmp(argv[i], "--crash", 7)) {
1095 cmd.crash_params = g_strdup(argv[i + 1]);
1097 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1100 } else if (!strncmp(argv[i], "--exit", 6)) {
1102 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1103 cmd.target = argv[i+1];
1104 } else if (i == 1 && argc == 2) {
1105 /* only one parameter. Do something intelligent about it */
1106 if (strstr(argv[i], "@") && !strstr(argv[i], "://")) {
1107 const gchar *p = argv[i];
1110 cmd.compose_mailto = NULL;
1111 if (p && *p != '\0' && *p != '-') {
1112 if (!strncmp(p, "mailto:", 7)) {
1113 cmd.compose_mailto = p + 7;
1115 cmd.compose_mailto = p;
1118 } else if (strstr(argv[i], "://")) {
1119 const gchar *p = argv[i];
1120 if (p && *p != '\0' && *p != '-') {
1121 cmd.subscribe = TRUE;
1122 cmd.subscribe_uri = p;
1129 if (cmd.attach_files && cmd.compose == FALSE) {
1131 cmd.compose_mailto = NULL;
1135 static gint get_queued_message_num(void)
1139 queue = folder_get_default_queue();
1144 folder_item_scan(queue);
1145 return queue->total_msgs;
1148 static void initial_processing(FolderItem *item, gpointer data)
1150 MainWindow *mainwin = (MainWindow *)data;
1153 g_return_if_fail(item);
1154 buf = g_strdup_printf(_("Processing (%s)..."),
1157 : _("top level folder"));
1161 if (item->prefs->enable_processing) {
1162 folder_item_apply_processing(item);
1165 STATUSBAR_POP(mainwin);
1168 static void draft_all_messages(void)
1170 GList *compose_list = NULL;
1172 while ((compose_list = compose_get_compose_list()) != NULL) {
1173 Compose *c = (Compose*)compose_list->data;
1177 gboolean clean_quit(gpointer data)
1179 static gboolean firstrun = TRUE;
1186 /*!< Good idea to have the main window stored in a
1187 * static variable so we can check that variable
1188 * to see if we're really allowed to do things
1189 * that actually the spawner is supposed to
1190 * do (like: sending mail, composing messages).
1191 * Because, really, if we're the spawnee, and
1192 * we touch GTK stuff, we're hosed. See the
1195 /* FIXME: Use something else to signal that we're
1196 * in the original spawner, and not in a spawned
1198 if (!static_mainwindow) {
1202 draft_all_messages();
1203 emergency_exit = TRUE;
1204 exit_claws(static_mainwindow);
1210 void app_will_exit(GtkWidget *widget, gpointer data)
1212 MainWindow *mainwin = data;
1214 if (sc_exiting == TRUE) {
1215 debug_print("exit pending\n");
1219 debug_print("exiting\n");
1220 if (compose_get_compose_list()) {
1221 gint val = alertpanel(_("Really quit?"),
1222 _("Composing message exists."),
1223 _("_Save to Draft"), _("_Discard them"), _("Do_n't quit"));
1228 case G_ALERTALTERNATE:
1231 draft_all_messages();
1234 manage_window_focus_in(mainwin->window, NULL, NULL);
1237 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
1238 if (alertpanel(_("Queued messages"),
1239 _("Some unsent messages are queued. Exit now?"),
1240 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
1241 != G_ALERTALTERNATE) {
1245 manage_window_focus_in(mainwin->window, NULL, NULL);
1249 if (folderview_get_selected_item(mainwin->folderview))
1250 folder_item_close(folderview_get_selected_item(mainwin->folderview));
1254 gboolean claws_is_exiting(void)
1260 * CLAWS: want this public so crash dialog can delete the
1266 gchar *get_socket_name(void)
1268 static gchar *filename = NULL;
1270 if (filename == NULL) {
1271 filename = g_strdup_printf("%s%cclaws-mail-%d",
1272 g_get_tmp_dir(), G_DIR_SEPARATOR,
1283 static gchar *get_crashfile_name(void)
1285 static gchar *filename = NULL;
1287 if (filename == NULL) {
1288 filename = g_strdup_printf("%s%cclaws-crashed",
1289 get_tmp_dir(), G_DIR_SEPARATOR);
1295 static gint prohibit_duplicate_launch(void)
1300 path = get_socket_name();
1301 uxsock = fd_connect_unix(path);
1304 return fd_open_unix(path);
1307 /* remote command mode */
1309 debug_print("another Claws Mail instance is already running.\n");
1311 if (cmd.receive_all) {
1312 fd_write_all(uxsock, "receive_all\n", 12);
1313 } else if (cmd.receive) {
1314 fd_write_all(uxsock, "receive\n", 8);
1315 } else if (cmd.compose && cmd.attach_files) {
1316 gchar *str, *compose_str;
1319 if (cmd.compose_mailto) {
1320 compose_str = g_strdup_printf("compose_attach %s\n",
1321 cmd.compose_mailto);
1323 compose_str = g_strdup("compose_attach\n");
1326 fd_write_all(uxsock, compose_str, strlen(compose_str));
1327 g_free(compose_str);
1329 for (i = 0; i < cmd.attach_files->len; i++) {
1330 str = g_ptr_array_index(cmd.attach_files, i);
1331 fd_write_all(uxsock, str, strlen(str));
1332 fd_write_all(uxsock, "\n", 1);
1335 fd_write_all(uxsock, ".\n", 2);
1336 } else if (cmd.compose) {
1339 if (cmd.compose_mailto) {
1340 compose_str = g_strdup_printf
1341 ("compose %s\n", cmd.compose_mailto);
1343 compose_str = g_strdup("compose\n");
1346 fd_write_all(uxsock, compose_str, strlen(compose_str));
1347 g_free(compose_str);
1348 } else if (cmd.subscribe) {
1349 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
1350 fd_write_all(uxsock, str, strlen(str));
1352 } else if (cmd.send) {
1353 fd_write_all(uxsock, "send\n", 5);
1354 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1355 fd_write(uxsock, "online\n", 6);
1356 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1357 fd_write(uxsock, "offline\n", 7);
1358 } else if (cmd.status || cmd.status_full) {
1359 gchar buf[BUFFSIZE];
1361 const gchar *command;
1365 command = cmd.status_full ? "status-full\n" : "status\n";
1366 folders = cmd.status_full ? cmd.status_full_folders :
1369 fd_write_all(uxsock, command, strlen(command));
1370 for (i = 0; folders && i < folders->len; ++i) {
1371 folder = g_ptr_array_index(folders, i);
1372 fd_write_all(uxsock, folder, strlen(folder));
1373 fd_write_all(uxsock, "\n", 1);
1375 fd_write_all(uxsock, ".\n", 2);
1377 fd_gets(uxsock, buf, sizeof(buf));
1378 if (!strncmp(buf, ".\n", 2)) break;
1381 } else if (cmd.exit) {
1382 fd_write_all(uxsock, "exit\n", 5);
1383 } else if (cmd.target) {
1384 gchar *str = g_strdup_printf("select %s\n", cmd.target);
1385 fd_write_all(uxsock, str, strlen(str));
1388 fd_write_all(uxsock, "popup\n", 6);
1394 static gint lock_socket_remove(void)
1399 if (lock_socket < 0) {
1403 if (lock_socket_tag > 0) {
1404 gdk_input_remove(lock_socket_tag);
1406 fd_close(lock_socket);
1407 filename = get_socket_name();
1414 static GPtrArray *get_folder_item_list(gint sock)
1416 gchar buf[BUFFSIZE];
1418 GPtrArray *folders = NULL;
1421 fd_gets(sock, buf, sizeof(buf));
1422 if (!strncmp(buf, ".\n", 2)) {
1427 folders = g_ptr_array_new();
1429 item = folder_find_item_from_identifier(buf);
1431 g_ptr_array_add(folders, item);
1433 g_warning("no such folder: %s\n", buf);
1440 static void lock_socket_input_cb(gpointer data,
1442 GdkInputCondition condition)
1444 MainWindow *mainwin = (MainWindow *)data;
1446 gchar buf[BUFFSIZE];
1448 sock = fd_accept(source);
1449 fd_gets(sock, buf, sizeof(buf));
1451 if (!strncmp(buf, "popup", 5)) {
1452 main_window_popup(mainwin);
1453 } else if (!strncmp(buf, "receive_all", 11)) {
1454 inc_all_account_mail(mainwin, FALSE,
1455 prefs_common.newmail_notify_manu);
1456 } else if (!strncmp(buf, "receive", 7)) {
1457 inc_mail(mainwin, prefs_common.newmail_notify_manu);
1458 } else if (!strncmp(buf, "compose_attach", 14)) {
1462 mailto = g_strdup(buf + strlen("compose_attach") + 1);
1463 files = g_ptr_array_new();
1464 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
1465 if (buf[0] == '.' && buf[1] == '\n') {
1469 g_ptr_array_add(files, g_strdup(buf));
1471 open_compose_new(mailto, files);
1472 ptr_array_free_strings(files);
1473 g_ptr_array_free(files, TRUE);
1475 } else if (!strncmp(buf, "compose", 7)) {
1476 open_compose_new(buf + strlen("compose") + 1, NULL);
1477 } else if (!strncmp(buf, "subscribe", 9)) {
1478 main_window_popup(mainwin);
1479 folder_subscribe(buf + strlen("subscribe") + 1);
1480 } else if (!strncmp(buf, "send", 4)) {
1482 } else if (!strncmp(buf, "online", 6)) {
1483 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1484 } else if (!strncmp(buf, "offline", 7)) {
1485 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1486 } else if (!strncmp(buf, "status-full", 11) ||
1487 !strncmp(buf, "status", 6)) {
1491 folders = get_folder_item_list(sock);
1492 status = folder_get_status
1493 (folders, !strncmp(buf, "status-full", 11));
1494 fd_write_all(sock, status, strlen(status));
1495 fd_write_all(sock, ".\n", 2);
1497 if (folders) g_ptr_array_free(folders, TRUE);
1498 } else if (!strncmp(buf, "select ", 7)) {
1499 const gchar *target = buf+7;
1500 mainwindow_jump_to(target);
1501 } else if (!strncmp(buf, "exit", 4)) {
1502 app_will_exit(NULL, mainwin);
1508 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
1513 Xstrdup_a(addr, address, return);
1517 compose_new(NULL, addr, attach_files);
1520 static void send_queue(void)
1523 gchar *errstr = NULL;
1524 for (list = folder_get_list(); list != NULL; list = list->next) {
1525 Folder *folder = list->data;
1527 if (folder->queue) {
1528 gint res = procmsg_send_queue
1529 (folder->queue, prefs_common.savemsg,
1533 folder_item_scan(folder->queue);
1538 gchar *tmp = g_strdup_printf(_("Some errors occurred "
1539 "while sending queued messages:\n%s"), errstr);
1541 alertpanel_error_log(tmp);
1544 alertpanel_error_log("Some errors occurred "
1545 "while sending queued messages.");
1549 static void quit_signal_handler(int sig)
1551 debug_print("Quitting on signal %d\n", sig);
1553 g_timeout_add(0, clean_quit, NULL);
1556 static void install_basic_sighandlers()
1560 struct sigaction act;
1565 sigaddset(&mask, SIGTERM);
1568 sigaddset(&mask, SIGINT);
1571 sigaddset(&mask, SIGHUP);
1574 act.sa_handler = quit_signal_handler;
1579 sigaction(SIGTERM, &act, 0);
1582 sigaction(SIGINT, &act, 0);
1585 sigaction(SIGHUP, &act, 0);
1588 sigprocmask(SIG_UNBLOCK, &mask, 0);
1589 #endif /* !G_OS_WIN32 */