2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 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);
161 gchar *get_socket_name (void);
164 static void open_compose_new (const gchar *address,
165 GPtrArray *attach_files);
167 static void send_queue (void);
168 static void initial_processing (FolderItem *item, gpointer data);
169 static void quit_signal_handler (int sig);
170 static void install_basic_sighandlers (void);
171 static void exit_claws (MainWindow *mainwin);
173 #define MAKE_DIR_IF_NOT_EXIST(dir) \
175 if (!is_dir_exist(dir)) { \
176 if (is_file_exist(dir)) { \
178 (_("File '%s' already exists.\n" \
179 "Can't create folder."), \
183 if (make_dir(dir) < 0) \
188 static MainWindow *static_mainwindow;
189 static gboolean emergency_exit = FALSE;
191 #ifdef HAVE_STARTUP_NOTIFICATION
192 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
194 gdk_error_trap_push();
197 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
199 gdk_error_trap_pop();
202 static void startup_notification_complete(gboolean with_window)
205 GtkWidget *hack = NULL;
208 /* this is needed to make the startup notification leave,
209 * if we have been launched from a menu.
210 * We have to display a window, so let it be very little */
211 hack = gtk_window_new(GTK_WINDOW_POPUP);
212 gtk_widget_set_uposition(hack, 0, 0);
213 gtk_widget_set_size_request(hack, 1, 1);
214 gtk_widget_show(hack);
217 xdisplay = GDK_DISPLAY();
218 sn_display = sn_display_new(xdisplay,
221 sn_context = sn_launchee_context_new_from_environment(sn_display,
222 DefaultScreen(xdisplay));
224 if (sn_context != NULL) {
225 sn_launchee_context_complete(sn_context);
226 sn_launchee_context_unref(sn_context);
227 sn_display_unref(sn_display);
230 gtk_widget_destroy(hack);
233 #endif /* HAVE_STARTUP_NOTIFICATION */
235 void claws_gtk_idle(void)
237 while(gtk_events_pending()) {
238 gtk_main_iteration();
243 gboolean defer_check_all(void *data)
245 gboolean autochk = GPOINTER_TO_INT(data);
247 inc_all_account_mail(static_mainwindow, autochk,
248 prefs_common.newmail_notify_manu);
253 gboolean defer_check(void *data)
255 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
260 static gboolean defer_jump(void *data)
262 mainwindow_jump_to(data);
266 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir, const gchar *oldversion)
268 gchar *message = g_strdup_printf(_("Configuration for %s (or previous) found.\n"
269 "Do you want to migrate this configuration?"), oldversion);
271 GtkWidget *window = NULL;
272 if (alertpanel(_("Migration of configuration"),
274 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL) != G_ALERTALTERNATE) {
278 window = label_window_create(_("Copying configuration..."));
280 r = copy_dir(old_cfg_dir, new_cfg_dir);
281 gtk_widget_destroy(window);
283 alertpanel_error(_("Migration failed!"));
288 static void migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
291 gchar *plugin_path, *old_plugin_path, *new_plugin_path;
293 oldfp = g_fopen(old_rc, "r");
296 newfp = g_fopen(new_rc, "w");
302 plugin_path = g_strdup(get_plugin_dir());
303 new_plugin_path = g_strdup(plugin_path);
305 if (strstr(plugin_path, "/claws-mail/")) {
306 gchar *end = g_strdup(strstr(plugin_path, "/claws-mail/")+strlen("/claws-mail/"));
307 *(strstr(plugin_path, "/claws-mail/")) = '\0';
308 old_plugin_path = g_strconcat(plugin_path, "/sylpheed-claws/", end, NULL);
311 old_plugin_path = g_strdup(new_plugin_path);
313 debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
314 while (fgets(buf, sizeof(buf), oldfp)) {
315 if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
318 debug_print("->replacing %s", buf);
319 debug_print(" with %s%s", new_plugin_path, buf+strlen(old_plugin_path));
320 fputs(new_plugin_path, newfp);
321 fputs(buf+strlen(old_plugin_path), newfp);
325 g_free(new_plugin_path);
326 g_free(old_plugin_path);
333 sc_client_set_value (MainWindow *mainwin,
344 prop.num_vals = num_vals;
348 if (mainwin->smc_conn)
349 SmcSetProperties ((SmcConn) mainwin->smc_conn, 1, proplist);
352 static void sc_die_callback (SmcConn smc_conn, SmPointer client_data)
357 static void sc_save_complete_callback(SmcConn smc_conn, SmPointer client_data)
361 static void sc_shutdown_cancelled_callback (SmcConn smc_conn, SmPointer client_data)
363 MainWindow *mainwin = (MainWindow *)client_data;
364 if (mainwin->smc_conn)
365 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
368 static void sc_save_yourself_callback (SmcConn smc_conn,
369 SmPointer client_data,
375 MainWindow *mainwin = (MainWindow *)client_data;
376 if (mainwin->smc_conn)
377 SmcSaveYourselfDone ((SmcConn) mainwin->smc_conn, TRUE);
380 static IceIOErrorHandler sc_ice_installed_handler;
382 static void sc_ice_io_error_handler (IceConn connection)
384 if (sc_ice_installed_handler)
385 (*sc_ice_installed_handler) (connection);
387 static gboolean sc_process_ice_messages (GIOChannel *source,
388 GIOCondition condition,
391 IceConn connection = (IceConn) data;
392 IceProcessMessagesStatus status;
394 status = IceProcessMessages (connection, NULL, NULL);
396 if (status == IceProcessMessagesIOError) {
397 IcePointer context = IceGetConnectionContext (connection);
399 if (context && GTK_IS_OBJECT (context)) {
400 guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
402 if (disconnect_id > 0)
403 g_signal_emit (context, disconnect_id, 0);
405 IceSetShutdownNegotiation (connection, False);
406 IceCloseConnection (connection);
413 static void new_ice_connection (IceConn connection, IcePointer client_data, Bool opening,
414 IcePointer *watch_data)
420 /* Make sure we don't pass on these file descriptors to any
422 fcntl(IceConnectionNumber(connection),F_SETFD,
423 fcntl(IceConnectionNumber(connection),F_GETFD,0) | FD_CLOEXEC);
425 channel = g_io_channel_unix_new (IceConnectionNumber (connection));
426 input_id = g_io_add_watch (channel,
427 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
428 sc_process_ice_messages,
430 g_io_channel_unref (channel);
432 *watch_data = (IcePointer) GUINT_TO_POINTER (input_id);
434 input_id = GPOINTER_TO_UINT ((gpointer) *watch_data);
435 g_source_remove (input_id);
439 static void sc_session_manager_connect(MainWindow *mainwin)
441 static gboolean connected = FALSE;
442 SmcCallbacks callbacks;
444 IceIOErrorHandler default_handler;
451 sc_ice_installed_handler = IceSetIOErrorHandler (NULL);
452 default_handler = IceSetIOErrorHandler (sc_ice_io_error_handler);
454 if (sc_ice_installed_handler == default_handler)
455 sc_ice_installed_handler = NULL;
457 IceAddConnectionWatch (new_ice_connection, NULL);
460 callbacks.save_yourself.callback = sc_save_yourself_callback;
461 callbacks.die.callback = sc_die_callback;
462 callbacks.save_complete.callback = sc_save_complete_callback;
463 callbacks.shutdown_cancelled.callback = sc_shutdown_cancelled_callback;
465 callbacks.save_yourself.client_data =
466 callbacks.die.client_data =
467 callbacks.save_complete.client_data =
468 callbacks.shutdown_cancelled.client_data = (SmPointer) mainwin;
469 if (g_getenv ("SESSION_MANAGER")) {
470 gchar error_string_ret[256] = "";
472 mainwin->smc_conn = (gpointer)
473 SmcOpenConnection (NULL, mainwin,
474 SmProtoMajor, SmProtoMinor,
475 SmcSaveYourselfProcMask | SmcDieProcMask |
476 SmcSaveCompleteProcMask |
477 SmcShutdownCancelledProcMask,
480 256, error_string_ret);
482 if (error_string_ret[0] || mainwin->smc_conn == NULL)
483 g_warning ("While connecting to session manager:\n%s.",
487 vals = g_new (SmPropValue, 1);
488 vals[0].length = strlen(argv0);
489 vals[0].value = argv0;
490 sc_client_set_value (mainwin, SmCloneCommand, SmLISTofARRAY8, 1, vals);
491 sc_client_set_value (mainwin, SmRestartCommand, SmLISTofARRAY8, 1, vals);
492 sc_client_set_value (mainwin, SmProgram, SmARRAY8, 1, vals);
494 vals[0].length = strlen(g_get_user_name()?g_get_user_name():"");
495 vals[0].value = g_strdup(g_get_user_name()?g_get_user_name():"");
496 sc_client_set_value (mainwin, SmUserID, SmARRAY8, 1, vals);
502 static gboolean sc_exiting = FALSE;
503 static gboolean sc_starting = FALSE;
505 int main(int argc, char *argv[])
509 FolderView *folderview;
511 gboolean crash_file_present = FALSE;
512 gint num_folder_class = 0;
513 START_TIMING("startup");
517 if (!claws_init(&argc, &argv)) {
521 prefs_prepare_cache();
522 prog_version = PROG_VERSION;
523 argv0 = g_strdup(argv[0]);
525 parse_cmd_opt(argc, argv);
530 gtk_init(&argc, &argv);
531 crash_main(cmd.crash_params);
534 crash_install_handlers();
536 install_basic_sighandlers();
539 /* check and create unix domain socket for remote operation */
541 lock_socket = prohibit_duplicate_launch();
542 if (lock_socket < 0) {
543 #ifdef HAVE_STARTUP_NOTIFICATION
544 if(gtk_init_check(&argc, &argv))
545 startup_notification_complete(TRUE);
550 if (cmd.status || cmd.status_full) {
551 puts("0 Claws Mail not running.");
552 lock_socket_remove();
559 if (!g_thread_supported())
563 gtk_init(&argc, &argv);
566 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
567 gtk_widget_set_default_visual(gdk_rgb_get_visual());
569 if (!g_thread_supported()) {
570 g_error(_("g_thread is not supported by glib.\n"));
573 /* check that we're not on a too recent/old gtk+ */
574 #if GTK_CHECK_VERSION(2, 9, 0)
575 if (gtk_check_version(2, 9, 0) != NULL) {
576 alertpanel_error(_("Claws Mail has been compiled with "
577 "a more recent GTK+ library than is "
578 "currently available. This will cause "
579 "crashes. You need to upgrade GTK+ or "
580 "recompile Claws Mail."));
584 if (gtk_check_version(2, 9, 0) == NULL) {
585 alertpanel_error(_("Claws Mail has been compiled with "
586 "an older GTK+ library than is "
587 "currently available. This will cause "
588 "crashes. You need to recompile "
593 /* parse gtkrc files */
594 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
596 gtk_rc_parse(userrc);
598 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
599 G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
600 gtk_rc_parse(userrc);
603 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
604 if (!is_dir_exist(RC_DIR)) {
605 prefs_destroy_cache();
607 if (is_dir_exist(OLD_GTK2_RC_DIR))
608 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR, "Sylpheed-Claws 2.6.0");
609 else if (is_dir_exist(OLDER_GTK2_RC_DIR))
610 r = migrate_old_config(OLDER_GTK2_RC_DIR, RC_DIR, "Sylpheed-Claws 1.9.15");
611 else if (is_dir_exist(OLD_GTK1_RC_DIR))
612 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR, "Sylpheed-Claws 1.0.5");
613 if (r == FALSE && !is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0)
616 if (!is_file_exist(RC_DIR G_DIR_SEPARATOR_S COMMON_RC) &&
617 is_file_exist(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC)) {
618 /* post 2.6 name change */
619 migrate_common_rc(RC_DIR G_DIR_SEPARATOR_S OLD_COMMON_RC,
620 RC_DIR G_DIR_SEPARATOR_S COMMON_RC);
624 plugin_load_all("Common");
626 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
627 gtk_rc_parse(userrc);
630 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
631 gtk_accel_map_load (userrc);
634 gtk_settings_set_long_property(gtk_settings_get_default(),
635 "gtk-can-change-accels",
636 (glong)TRUE, "XProperty");
638 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
640 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
641 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
642 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
643 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
644 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
645 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
647 crash_file_present = is_file_exist(get_crashfile_name());
648 /* remove temporary files */
649 remove_all_files(get_tmp_dir());
650 remove_all_files(get_mime_tmp_dir());
652 if (is_file_exist("claws.log")) {
653 if (rename_force("claws.log", "claws.log.bak") < 0)
654 FILE_OP_ERROR("claws.log", "rename");
656 set_log_file("claws.log");
658 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
660 folder_system_init();
661 prefs_common_read_config();
665 prefs_ext_prog_init();
666 prefs_wrapping_init();
667 prefs_compose_writing_init();
668 prefs_msg_colors_init();
670 prefs_image_viewer_init();
672 prefs_summaries_init();
673 prefs_message_init();
675 prefs_receive_init();
678 gtkaspell_checkers_init();
679 prefs_spelling_init();
682 sock_set_io_timeout(prefs_common.io_timeout_secs);
684 imap_main_set_timeout(prefs_common.io_timeout_secs);
686 prefs_actions_read_config();
687 prefs_display_header_read_config();
688 /* prefs_filtering_read_config(); */
689 addressbook_read_file();
690 renderer_read_config();
693 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
694 gtk_window_set_default_icon(icon);
696 folderview_initialize();
702 mainwin = main_window_create
703 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
704 manage_window_focus_in(mainwin->window, NULL, NULL);
705 folderview = mainwin->folderview;
707 gtk_clist_freeze(GTK_CLIST(mainwin->folderview->ctree));
708 folder_item_update_freeze();
710 /* register the callback of unix domain socket input */
712 lock_socket_tag = gdk_input_add(lock_socket,
713 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
714 lock_socket_input_cb,
718 prefs_account_init();
719 account_read_config_all();
721 if (folder_read_list() < 0) {
722 prefs_destroy_cache();
723 if (!run_wizard(mainwin, TRUE))
725 main_window_reflect_prefs_all_now();
729 if (!account_get_list()) {
730 prefs_destroy_cache();
731 if (!run_wizard(mainwin, FALSE))
733 account_read_config_all();
734 if(!account_get_list()) {
741 toolbar_main_set_sensitive(mainwin);
742 main_window_set_menu_sensitive(mainwin);
744 main_window_popup(mainwin);
747 imap_main_init(prefs_common.skip_ssl_cert_check);
749 account_set_missing_folder();
750 folder_set_missing_folders();
751 folderview_set(folderview);
753 prefs_matcher_read_config();
755 /* make one all-folder processing before using claws */
756 main_window_cursor_wait(mainwin);
757 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
759 /* if claws crashed, rebuild caches */
760 if (!cmd.crash && crash_file_present) {
762 debug_print("Claws Mail crashed, checking for new messages in local folders\n");
763 folder_item_update_thaw();
764 folderview_check_new(NULL);
765 folder_clean_cache_memory_force();
766 folder_item_update_freeze();
768 /* make the crash-indicator file */
769 str_write_to_file("foo", get_crashfile_name());
771 inc_autocheck_timer_init(mainwin);
773 /* ignore SIGPIPE signal for preventing sudden death of program */
775 signal(SIGPIPE, SIG_IGN);
777 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
778 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
780 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
781 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
784 if (cmd.status_folders) {
785 g_ptr_array_free(cmd.status_folders, TRUE);
786 cmd.status_folders = NULL;
788 if (cmd.status_full_folders) {
789 g_ptr_array_free(cmd.status_full_folders, TRUE);
790 cmd.status_full_folders = NULL;
793 claws_register_idle_function(claws_gtk_idle);
795 prefs_toolbar_init();
797 num_folder_class = g_list_length(folder_get_list());
799 plugin_load_all("GTK2");
801 if (g_list_length(folder_get_list()) != num_folder_class) {
802 debug_print("new folders loaded, reloading processing rules\n");
803 prefs_matcher_read_config();
806 if (plugin_get_unloaded_list() != NULL) {
807 main_window_cursor_normal(mainwin);
808 alertpanel_warning(_("Some plugin(s) failed to load. "
809 "Check the Plugins configuration "
810 "for more information."));
811 main_window_cursor_wait(mainwin);
814 plugin_load_standard_plugins ();
816 if (!folder_have_mailbox()) {
817 prefs_destroy_cache();
818 main_window_cursor_normal(mainwin);
819 if (folder_get_list() != NULL) {
820 alertpanel_error(_("Claws Mail has detected a configured "
821 "mailbox, but is it incomplete. It is "
822 "possibly due to a failing IMAP account. Use "
823 "\"Rebuild folder tree\" on the mailbox's folder "
824 "to try to fix it."));
826 alertpanel_error(_("Claws Mail has detected a configured "
827 "mailbox, but could not load it. It is "
828 "probably provided by an out-of-date "
829 "external plugin. Please reinstall the "
830 "plugin and try again."));
836 static_mainwindow = mainwin;
838 #ifdef HAVE_STARTUP_NOTIFICATION
839 startup_notification_complete(FALSE);
842 sc_session_manager_connect(mainwin);
844 folder_item_update_thaw();
845 gtk_clist_thaw(GTK_CLIST(mainwin->folderview->ctree));
846 main_window_cursor_normal(mainwin);
848 if (cmd.receive_all) {
849 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
850 } else if (prefs_common.chk_on_startup) {
851 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
852 } else if (cmd.receive) {
853 g_timeout_add(1000, defer_check, NULL);
855 gtk_widget_grab_focus(folderview->ctree);
859 open_compose_new(cmd.compose_mailto, cmd.attach_files);
861 if (cmd.attach_files) {
862 ptr_array_free_strings(cmd.attach_files);
863 g_ptr_array_free(cmd.attach_files, TRUE);
864 cmd.attach_files = NULL;
867 folder_subscribe(cmd.subscribe_uri);
875 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
878 prefs_destroy_cache();
890 static void save_all_caches(FolderItem *item, gpointer data)
897 folder_item_close(item);
899 folder_item_free_cache(item, TRUE);
902 static void exit_claws(MainWindow *mainwin)
908 debug_print("shutting down\n");
909 inc_autocheck_timer_remove();
911 if (prefs_common.clean_on_exit && !emergency_exit) {
912 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
915 /* save prefs for opened folder */
916 if(mainwin->folderview->opened) {
919 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
920 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
923 /* save all state before exiting */
924 folder_func_to_all_folders(save_all_caches, NULL);
927 main_window_get_size(mainwin);
928 main_window_get_position(mainwin);
929 prefs_common_write_config();
930 account_write_config_all();
931 addressbook_export_to_file();
933 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
934 gtk_accel_map_save(filename);
937 /* delete temporary files */
938 remove_all_files(get_tmp_dir());
939 remove_all_files(get_mime_tmp_dir());
946 /* delete crashfile */
948 g_unlink(get_crashfile_name());
950 lock_socket_remove();
953 if (mainwin->smc_conn)
954 SmcCloseConnection ((SmcConn)mainwin->smc_conn, 0, NULL);
955 mainwin->smc_conn = NULL;
958 main_window_destroy_all();
960 plugin_unload_all("GTK2");
962 prefs_toolbar_done();
964 addressbook_destroy();
968 prefs_ext_prog_done();
969 prefs_wrapping_done();
970 prefs_compose_writing_done();
971 prefs_msg_colors_done();
972 prefs_image_viewer_done();
975 prefs_summaries_done();
976 prefs_message_done();
978 prefs_receive_done();
981 prefs_spelling_done();
982 gtkaspell_checkers_quit();
984 plugin_unload_all("Common");
988 static void parse_cmd_opt(int argc, char *argv[])
992 for (i = 1; i < argc; i++) {
993 if (!strncmp(argv[i], "--receive-all", 13)) {
994 cmd.receive_all = TRUE;
995 } else if (!strncmp(argv[i], "--receive", 9)) {
997 } else if (!strncmp(argv[i], "--compose", 9)) {
998 const gchar *p = argv[i + 1];
1001 cmd.compose_mailto = NULL;
1002 if (p && *p != '\0' && *p != '-') {
1003 if (!strncmp(p, "mailto:", 7)) {
1004 cmd.compose_mailto = p + 7;
1006 cmd.compose_mailto = p;
1010 } else if (!strncmp(argv[i], "--subscribe", 11)) {
1011 const gchar *p = argv[i + 1];
1012 if (p && *p != '\0' && *p != '-') {
1013 cmd.subscribe = TRUE;
1014 cmd.subscribe_uri = p;
1016 } else if (!strncmp(argv[i], "--attach", 8)) {
1017 const gchar *p = argv[i + 1];
1020 while (p && *p != '\0' && *p != '-') {
1021 if (!cmd.attach_files) {
1022 cmd.attach_files = g_ptr_array_new();
1024 if (*p != G_DIR_SEPARATOR) {
1025 file = g_strconcat(claws_get_startup_dir(),
1031 g_ptr_array_add(cmd.attach_files, file);
1035 } else if (!strncmp(argv[i], "--send", 6)) {
1037 } else if (!strncmp(argv[i], "--version", 9)) {
1038 puts("Claws Mail version " VERSION);
1040 } else if (!strncmp(argv[i], "--status-full", 13)) {
1041 const gchar *p = argv[i + 1];
1043 cmd.status_full = TRUE;
1044 while (p && *p != '\0' && *p != '-') {
1045 if (!cmd.status_full_folders) {
1046 cmd.status_full_folders =
1049 g_ptr_array_add(cmd.status_full_folders,
1054 } else if (!strncmp(argv[i], "--status", 8)) {
1055 const gchar *p = argv[i + 1];
1058 while (p && *p != '\0' && *p != '-') {
1059 if (!cmd.status_folders)
1060 cmd.status_folders = g_ptr_array_new();
1061 g_ptr_array_add(cmd.status_folders,
1066 } else if (!strncmp(argv[i], "--online", 8)) {
1067 cmd.online_mode = ONLINE_MODE_ONLINE;
1068 } else if (!strncmp(argv[i], "--offline", 9)) {
1069 cmd.online_mode = ONLINE_MODE_OFFLINE;
1070 } else if (!strncmp(argv[i], "--help", 6)) {
1071 gchar *base = g_path_get_basename(argv[0]);
1072 g_print(_("Usage: %s [OPTION]...\n"), base);
1074 g_print("%s\n", _(" --compose [address] open composition window"));
1075 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
1076 g_print("%s\n", _(" --attach file1 [file2]...\n"
1077 " open composition window with specified files\n"
1079 g_print("%s\n", _(" --receive receive new messages"));
1080 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
1081 g_print("%s\n", _(" --send send all queued messages"));
1082 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
1083 g_print("%s\n", _(" --status-full [folder]...\n"
1084 " show the status of each folder"));
1085 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
1086 " folder is a folder id like 'folder/sub_folder'"));
1087 g_print("%s\n", _(" --online switch to online mode"));
1088 g_print("%s\n", _(" --offline switch to offline mode"));
1089 g_print("%s\n", _(" --exit exit Claws Mail"));
1090 g_print("%s\n", _(" --debug debug mode"));
1091 g_print("%s\n", _(" --help display this help and exit"));
1092 g_print("%s\n", _(" --version output version information and exit"));
1093 g_print("%s\n", _(" --config-dir output configuration directory"));
1097 } else if (!strncmp(argv[i], "--crash", 7)) {
1099 cmd.crash_params = g_strdup(argv[i + 1]);
1101 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
1104 } else if (!strncmp(argv[i], "--exit", 6)) {
1106 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
1107 cmd.target = argv[i+1];
1108 } else if (i == 1 && argc == 2) {
1109 /* only one parameter. Do something intelligent about it */
1110 if (strstr(argv[i], "@") && !strstr(argv[i], "://")) {
1111 const gchar *p = argv[i];
1114 cmd.compose_mailto = NULL;
1115 if (p && *p != '\0' && *p != '-') {
1116 if (!strncmp(p, "mailto:", 7)) {
1117 cmd.compose_mailto = p + 7;
1119 cmd.compose_mailto = p;
1122 } else if (strstr(argv[i], "://")) {
1123 const gchar *p = argv[i];
1124 if (p && *p != '\0' && *p != '-') {
1125 cmd.subscribe = TRUE;
1126 cmd.subscribe_uri = p;
1133 if (cmd.attach_files && cmd.compose == FALSE) {
1135 cmd.compose_mailto = NULL;
1139 static gint get_queued_message_num(void)
1143 queue = folder_get_default_queue();
1148 folder_item_scan(queue);
1149 return queue->total_msgs;
1152 static void initial_processing(FolderItem *item, gpointer data)
1154 MainWindow *mainwin = (MainWindow *)data;
1157 g_return_if_fail(item);
1158 buf = g_strdup_printf(_("Processing (%s)..."),
1161 : _("top level folder"));
1165 if (item->prefs->enable_processing) {
1166 folder_item_apply_processing(item);
1169 STATUSBAR_POP(mainwin);
1172 static void draft_all_messages(void)
1174 GList *compose_list = NULL;
1176 while ((compose_list = compose_get_compose_list()) != NULL) {
1177 Compose *c = (Compose*)compose_list->data;
1181 gboolean clean_quit(gpointer data)
1183 static gboolean firstrun = TRUE;
1190 /*!< Good idea to have the main window stored in a
1191 * static variable so we can check that variable
1192 * to see if we're really allowed to do things
1193 * that actually the spawner is supposed to
1194 * do (like: sending mail, composing messages).
1195 * Because, really, if we're the spawnee, and
1196 * we touch GTK stuff, we're hosed. See the
1199 /* FIXME: Use something else to signal that we're
1200 * in the original spawner, and not in a spawned
1202 if (!static_mainwindow) {
1206 draft_all_messages();
1207 emergency_exit = TRUE;
1208 exit_claws(static_mainwindow);
1214 void app_will_exit(GtkWidget *widget, gpointer data)
1216 MainWindow *mainwin = data;
1218 if (sc_exiting == TRUE) {
1219 debug_print("exit pending\n");
1223 debug_print("exiting\n");
1224 if (compose_get_compose_list()) {
1225 gint val = alertpanel(_("Really quit?"),
1226 _("Composing message exists."),
1227 _("_Save to Draft"), _("_Discard them"), _("Do_n't quit"));
1232 case G_ALERTALTERNATE:
1235 draft_all_messages();
1238 manage_window_focus_in(mainwin->window, NULL, NULL);
1241 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
1242 if (alertpanel(_("Queued messages"),
1243 _("Some unsent messages are queued. Exit now?"),
1244 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
1245 != G_ALERTALTERNATE) {
1249 manage_window_focus_in(mainwin->window, NULL, NULL);
1253 if (folderview_get_selected_item(mainwin->folderview))
1254 folder_item_close(folderview_get_selected_item(mainwin->folderview));
1258 gboolean claws_is_exiting(void)
1263 gboolean claws_is_starting(void)
1269 * CLAWS: want this public so crash dialog can delete the
1272 gchar *get_socket_name(void)
1274 static gchar *filename = NULL;
1276 if (filename == NULL) {
1277 filename = g_strdup_printf("%s%cclaws-mail-%d",
1278 g_get_tmp_dir(), G_DIR_SEPARATOR,
1289 static gchar *get_crashfile_name(void)
1291 static gchar *filename = NULL;
1293 if (filename == NULL) {
1294 filename = g_strdup_printf("%s%cclaws-crashed",
1295 get_tmp_dir(), G_DIR_SEPARATOR);
1301 static gint prohibit_duplicate_launch(void)
1306 path = get_socket_name();
1307 uxsock = fd_connect_unix(path);
1310 return fd_open_unix(path);
1313 /* remote command mode */
1315 debug_print("another Claws Mail instance is already running.\n");
1317 if (cmd.receive_all) {
1318 fd_write_all(uxsock, "receive_all\n", 12);
1319 } else if (cmd.receive) {
1320 fd_write_all(uxsock, "receive\n", 8);
1321 } else if (cmd.compose && cmd.attach_files) {
1322 gchar *str, *compose_str;
1325 if (cmd.compose_mailto) {
1326 compose_str = g_strdup_printf("compose_attach %s\n",
1327 cmd.compose_mailto);
1329 compose_str = g_strdup("compose_attach\n");
1332 fd_write_all(uxsock, compose_str, strlen(compose_str));
1333 g_free(compose_str);
1335 for (i = 0; i < cmd.attach_files->len; i++) {
1336 str = g_ptr_array_index(cmd.attach_files, i);
1337 fd_write_all(uxsock, str, strlen(str));
1338 fd_write_all(uxsock, "\n", 1);
1341 fd_write_all(uxsock, ".\n", 2);
1342 } else if (cmd.compose) {
1345 if (cmd.compose_mailto) {
1346 compose_str = g_strdup_printf
1347 ("compose %s\n", cmd.compose_mailto);
1349 compose_str = g_strdup("compose\n");
1352 fd_write_all(uxsock, compose_str, strlen(compose_str));
1353 g_free(compose_str);
1354 } else if (cmd.subscribe) {
1355 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
1356 fd_write_all(uxsock, str, strlen(str));
1358 } else if (cmd.send) {
1359 fd_write_all(uxsock, "send\n", 5);
1360 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1361 fd_write(uxsock, "online\n", 6);
1362 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1363 fd_write(uxsock, "offline\n", 7);
1364 } else if (cmd.status || cmd.status_full) {
1365 gchar buf[BUFFSIZE];
1367 const gchar *command;
1371 command = cmd.status_full ? "status-full\n" : "status\n";
1372 folders = cmd.status_full ? cmd.status_full_folders :
1375 fd_write_all(uxsock, command, strlen(command));
1376 for (i = 0; folders && i < folders->len; ++i) {
1377 folder = g_ptr_array_index(folders, i);
1378 fd_write_all(uxsock, folder, strlen(folder));
1379 fd_write_all(uxsock, "\n", 1);
1381 fd_write_all(uxsock, ".\n", 2);
1383 fd_gets(uxsock, buf, sizeof(buf));
1384 if (!strncmp(buf, ".\n", 2)) break;
1387 } else if (cmd.exit) {
1388 fd_write_all(uxsock, "exit\n", 5);
1389 } else if (cmd.target) {
1390 gchar *str = g_strdup_printf("select %s\n", cmd.target);
1391 fd_write_all(uxsock, str, strlen(str));
1394 fd_write_all(uxsock, "popup\n", 6);
1400 static gint lock_socket_remove(void)
1405 if (lock_socket < 0) {
1409 if (lock_socket_tag > 0) {
1410 gdk_input_remove(lock_socket_tag);
1412 fd_close(lock_socket);
1413 filename = get_socket_name();
1420 static GPtrArray *get_folder_item_list(gint sock)
1422 gchar buf[BUFFSIZE];
1424 GPtrArray *folders = NULL;
1427 fd_gets(sock, buf, sizeof(buf));
1428 if (!strncmp(buf, ".\n", 2)) {
1433 folders = g_ptr_array_new();
1435 item = folder_find_item_from_identifier(buf);
1437 g_ptr_array_add(folders, item);
1439 g_warning("no such folder: %s\n", buf);
1446 static void lock_socket_input_cb(gpointer data,
1448 GdkInputCondition condition)
1450 MainWindow *mainwin = (MainWindow *)data;
1452 gchar buf[BUFFSIZE];
1454 sock = fd_accept(source);
1455 fd_gets(sock, buf, sizeof(buf));
1457 if (!strncmp(buf, "popup", 5)) {
1458 main_window_popup(mainwin);
1459 } else if (!strncmp(buf, "receive_all", 11)) {
1460 inc_all_account_mail(mainwin, FALSE,
1461 prefs_common.newmail_notify_manu);
1462 } else if (!strncmp(buf, "receive", 7)) {
1463 inc_mail(mainwin, prefs_common.newmail_notify_manu);
1464 } else if (!strncmp(buf, "compose_attach", 14)) {
1468 mailto = g_strdup(buf + strlen("compose_attach") + 1);
1469 files = g_ptr_array_new();
1470 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
1471 if (buf[0] == '.' && buf[1] == '\n') {
1475 g_ptr_array_add(files, g_strdup(buf));
1477 open_compose_new(mailto, files);
1478 ptr_array_free_strings(files);
1479 g_ptr_array_free(files, TRUE);
1481 } else if (!strncmp(buf, "compose", 7)) {
1482 open_compose_new(buf + strlen("compose") + 1, NULL);
1483 } else if (!strncmp(buf, "subscribe", 9)) {
1484 main_window_popup(mainwin);
1485 folder_subscribe(buf + strlen("subscribe") + 1);
1486 } else if (!strncmp(buf, "send", 4)) {
1488 } else if (!strncmp(buf, "online", 6)) {
1489 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1490 } else if (!strncmp(buf, "offline", 7)) {
1491 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1492 } else if (!strncmp(buf, "status-full", 11) ||
1493 !strncmp(buf, "status", 6)) {
1497 folders = get_folder_item_list(sock);
1498 status = folder_get_status
1499 (folders, !strncmp(buf, "status-full", 11));
1500 fd_write_all(sock, status, strlen(status));
1501 fd_write_all(sock, ".\n", 2);
1503 if (folders) g_ptr_array_free(folders, TRUE);
1504 } else if (!strncmp(buf, "select ", 7)) {
1505 const gchar *target = buf+7;
1506 mainwindow_jump_to(target);
1507 } else if (!strncmp(buf, "exit", 4)) {
1508 app_will_exit(NULL, mainwin);
1514 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
1519 Xstrdup_a(addr, address, return);
1523 compose_new(NULL, addr, attach_files);
1526 static void send_queue(void)
1529 gchar *errstr = NULL;
1530 for (list = folder_get_list(); list != NULL; list = list->next) {
1531 Folder *folder = list->data;
1533 if (folder->queue) {
1534 gint res = procmsg_send_queue
1535 (folder->queue, prefs_common.savemsg,
1539 folder_item_scan(folder->queue);
1544 gchar *tmp = g_strdup_printf(_("Some errors occurred "
1545 "while sending queued messages:\n%s"), errstr);
1547 alertpanel_error_log(tmp);
1550 alertpanel_error_log("Some errors occurred "
1551 "while sending queued messages.");
1555 static void quit_signal_handler(int sig)
1557 debug_print("Quitting on signal %d\n", sig);
1559 g_timeout_add(0, clean_quit, NULL);
1562 static void install_basic_sighandlers()
1566 struct sigaction act;
1571 sigaddset(&mask, SIGTERM);
1574 sigaddset(&mask, SIGINT);
1577 sigaddset(&mask, SIGHUP);
1580 act.sa_handler = quit_signal_handler;
1585 sigaction(SIGTERM, &act, 0);
1588 sigaction(SIGINT, &act, 0);
1591 sigaction(SIGHUP, &act, 0);
1594 sigprocmask(SIG_UNBLOCK, &mask, 0);
1595 #endif /* !G_OS_WIN32 */