2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkrc.h>
38 #include <sys/types.h>
43 #ifdef HAVE_STARTUP_NOTIFICATION
44 # define SN_API_NOT_YET_FROZEN
45 # include <libsn/sn-launchee.h>
46 # include <gdk/gdkx.h>
51 #include "mainwindow.h"
52 #include "folderview.h"
53 #include "image_viewer.h"
54 #include "summaryview.h"
55 #include "prefs_common.h"
56 #include "prefs_account.h"
57 #include "prefs_actions.h"
58 #include "prefs_ext_prog.h"
59 #include "prefs_fonts.h"
60 #include "prefs_image_viewer.h"
61 #include "prefs_message.h"
62 #include "prefs_receive.h"
63 #include "prefs_msg_colors.h"
64 #include "prefs_quote.h"
65 #include "prefs_spelling.h"
66 #include "prefs_summaries.h"
67 #include "prefs_themes.h"
68 #include "prefs_other.h"
69 #include "prefs_send.h"
70 #include "prefs_wrapping.h"
71 #include "prefs_compose_writing.h"
72 #include "prefs_display_header.h"
77 #include "manage_window.h"
78 #include "alertpanel.h"
79 #include "statusbar.h"
80 #include "addressbook.h"
88 #include "prefs_toolbar.h"
95 #include "imap-thread.h"
97 #include "stock_pixmap.h"
112 #ifdef HAVE_STARTUP_NOTIFICATION
113 static SnLauncheeContext *sn_context = NULL;
114 static SnDisplay *sn_display = NULL;
117 static gint lock_socket = -1;
118 static gint lock_socket_tag = 0;
122 ONLINE_MODE_DONT_CHANGE,
127 static struct RemoteCmd {
129 gboolean receive_all;
131 const gchar *compose_mailto;
132 GPtrArray *attach_files;
134 gboolean status_full;
135 GPtrArray *status_folders;
136 GPtrArray *status_full_folders;
143 const gchar *subscribe_uri;
146 static void parse_cmd_opt(int argc, char *argv[]);
148 static gint prohibit_duplicate_launch (void);
149 static gchar * get_crashfile_name (void);
150 static gint lock_socket_remove (void);
151 static void lock_socket_input_cb (gpointer data,
153 GdkInputCondition condition);
157 gchar *get_socket_name (void);
160 static void open_compose_new (const gchar *address,
161 GPtrArray *attach_files);
163 static void send_queue (void);
164 static void initial_processing (FolderItem *item, gpointer data);
165 static void quit_signal_handler (int sig);
166 static void install_basic_sighandlers (void);
167 static void exit_sylpheed (MainWindow *mainwin);
169 #define MAKE_DIR_IF_NOT_EXIST(dir) \
171 if (!is_dir_exist(dir)) { \
172 if (is_file_exist(dir)) { \
174 (_("File '%s' already exists.\n" \
175 "Can't create folder."), \
179 if (make_dir(dir) < 0) \
184 static MainWindow *static_mainwindow;
186 #ifdef HAVE_STARTUP_NOTIFICATION
187 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
189 gdk_error_trap_push();
192 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
194 gdk_error_trap_pop();
197 static void startup_notification_complete(gboolean with_window)
200 GtkWidget *hack = NULL;
203 /* this is needed to make the startup notification leave,
204 * if we have been launched from a menu.
205 * We have to display a window, so let it be very little */
206 hack = gtk_window_new(GTK_WINDOW_POPUP);
207 gtk_widget_set_uposition(hack, 0, 0);
208 gtk_widget_set_size_request(hack, 1, 1);
209 gtk_widget_show(hack);
212 xdisplay = GDK_DISPLAY();
213 sn_display = sn_display_new(xdisplay,
216 sn_context = sn_launchee_context_new_from_environment(sn_display,
217 DefaultScreen(xdisplay));
219 if (sn_context != NULL)
221 sn_launchee_context_complete(sn_context);
222 sn_launchee_context_unref(sn_context);
223 sn_display_unref(sn_display);
226 gtk_widget_destroy(hack);
228 #endif /* HAVE_STARTUP_NOTIFICATION */
230 void sylpheed_gtk_idle(void)
232 while(gtk_events_pending())
233 gtk_main_iteration();
237 gboolean defer_check_all(void *data)
239 gboolean autochk = GPOINTER_TO_INT(data);
241 inc_all_account_mail(static_mainwindow, autochk,
242 prefs_common.newmail_notify_manu);
247 gboolean defer_check(void *data)
249 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
254 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir)
256 gchar *message = g_strdup_printf(_("Configuration for Sylpheed-Claws %s found.\n"
257 "Do you want to migrate this configuration?"),
258 !strcmp(old_cfg_dir, OLD_GTK1_RC_DIR)?
259 _("1.0.5 or previous"):_("1.9.15 or previous"));
261 GtkWidget *window = NULL;
262 if (alertpanel(_("Migration of configuration"),
264 GTK_STOCK_NO, GTK_STOCK_YES, NULL) != G_ALERTALTERNATE)
267 window = label_window_create(_("Copying configuration..."));
269 r = copy_dir(old_cfg_dir, new_cfg_dir);
270 gtk_widget_destroy(window);
272 alertpanel_error(_("Migration failed!"));
277 int main(int argc, char *argv[])
281 FolderView *folderview;
283 gboolean crash_file_present = FALSE;
285 if (!sylpheed_init(&argc, &argv)) {
289 prog_version = PROG_VERSION;
291 argv0 = g_strdup(argv[0]);
294 parse_cmd_opt(argc, argv);
299 gtk_init(&argc, &argv);
300 crash_main(cmd.crash_params);
303 crash_install_handlers();
305 install_basic_sighandlers();
308 /* check and create unix domain socket for remote operation */
310 lock_socket = prohibit_duplicate_launch();
311 if (lock_socket < 0) {
312 #ifdef HAVE_STARTUP_NOTIFICATION
313 if(gtk_init_check(&argc, &argv))
314 startup_notification_complete(TRUE);
319 if (cmd.status || cmd.status_full) {
320 puts("0 Sylpheed-Claws not running.");
321 lock_socket_remove();
329 /* gdk_threads_init(); */
332 gtk_init(&argc, &argv);
335 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
336 gtk_widget_set_default_visual(gdk_rgb_get_visual());
338 if (!g_thread_supported())
339 g_error(_("g_thread is not supported by glib.\n"));
341 /* parse gtkrc files */
342 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
344 gtk_rc_parse(userrc);
346 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
347 G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
348 gtk_rc_parse(userrc);
351 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
352 if (!is_dir_exist(RC_DIR)) {
354 if (is_dir_exist(OLD_GTK2_RC_DIR))
355 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR);
356 else if (is_dir_exist(OLD_GTK1_RC_DIR))
357 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR);
358 if (r == FALSE && !is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0)
362 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
363 gtk_rc_parse(userrc);
366 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
367 gtk_accel_map_load (userrc);
370 gtk_settings_set_long_property(gtk_settings_get_default(),
371 "gtk-can-change-accels",
372 (glong)TRUE, "XProperty");
374 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
376 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
377 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
378 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
379 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
380 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
381 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
383 crash_file_present = is_file_exist(get_crashfile_name());
384 /* remove temporary files */
385 remove_all_files(get_tmp_dir());
386 remove_all_files(get_mime_tmp_dir());
388 if (is_file_exist("sylpheed.log")) {
389 if (rename_force("sylpheed.log", "sylpheed.log.bak") < 0)
390 FILE_OP_ERROR("sylpheed.log", "rename");
392 set_log_file("sylpheed.log");
394 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
396 folder_system_init();
397 prefs_common_read_config();
401 prefs_ext_prog_init();
402 prefs_wrapping_init();
403 prefs_compose_writing_init();
404 prefs_msg_colors_init();
406 prefs_image_viewer_init();
408 prefs_summaries_init();
409 prefs_message_init();
411 prefs_receive_init();
414 gtkaspell_checkers_init();
415 prefs_spelling_init();
418 sock_set_io_timeout(prefs_common.io_timeout_secs);
420 imap_main_set_timeout(prefs_common.io_timeout_secs);
422 prefs_actions_read_config();
423 prefs_display_header_read_config();
424 /* prefs_filtering_read_config(); */
425 addressbook_read_file();
426 renderer_read_config();
429 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_SYLPHEED_ICON, &icon);
430 gtk_window_set_default_icon(icon);
432 folderview_initialize();
438 mainwin = main_window_create
439 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
440 folderview = mainwin->folderview;
442 gtk_clist_freeze(GTK_CLIST(mainwin->folderview->ctree));
443 folder_item_update_freeze();
445 /* register the callback of unix domain socket input */
447 lock_socket_tag = gdk_input_add(lock_socket,
448 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
449 lock_socket_input_cb,
453 prefs_account_init();
454 account_read_config_all();
456 if (folder_read_list() < 0) {
457 if (!run_wizard(mainwin, TRUE))
462 if (!account_get_list()) {
463 if (!run_wizard(mainwin, FALSE))
465 account_read_config_all();
466 if(!account_get_list())
467 exit_sylpheed(mainwin);
470 main_window_popup(mainwin);
475 account_set_missing_folder();
476 folder_set_missing_folders();
477 folderview_set(folderview);
479 prefs_matcher_read_config();
481 /* make one all-folder processing before using sylpheed */
482 main_window_cursor_wait(mainwin);
483 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
485 /* if Sylpheed crashed, rebuild caches */
486 if (!cmd.crash && crash_file_present) {
487 debug_print("Sylpheed-Claws crashed, checking for new messages in local folders\n");
488 folder_item_update_thaw();
489 folderview_check_new(NULL);
490 folder_clean_cache_memory_force();
491 folder_item_update_freeze();
493 /* make the crash-indicator file */
494 str_write_to_file("foo", get_crashfile_name());
496 addressbook_read_file();
498 inc_autocheck_timer_init(mainwin);
500 /* ignore SIGPIPE signal for preventing sudden death of program */
502 signal(SIGPIPE, SIG_IGN);
504 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
505 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
506 if (cmd.online_mode == ONLINE_MODE_ONLINE)
507 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
509 if (cmd.status_folders) {
510 g_ptr_array_free(cmd.status_folders, TRUE);
511 cmd.status_folders = NULL;
513 if (cmd.status_full_folders) {
514 g_ptr_array_free(cmd.status_full_folders, TRUE);
515 cmd.status_full_folders = NULL;
518 sylpheed_register_idle_function(sylpheed_gtk_idle);
520 prefs_toolbar_init();
522 plugin_load_all("GTK2");
524 static_mainwindow = mainwin;
526 #ifdef HAVE_STARTUP_NOTIFICATION
527 startup_notification_complete(FALSE);
529 folder_item_update_thaw();
530 gtk_clist_thaw(GTK_CLIST(mainwin->folderview->ctree));
531 main_window_cursor_normal(mainwin);
534 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
535 else if (prefs_common.chk_on_startup)
536 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
537 else if (cmd.receive)
538 g_timeout_add(1000, defer_check, NULL);
540 gtk_widget_grab_focus(folderview->ctree);
543 open_compose_new(cmd.compose_mailto, cmd.attach_files);
544 if (cmd.attach_files) {
545 ptr_array_free_strings(cmd.attach_files);
546 g_ptr_array_free(cmd.attach_files, TRUE);
547 cmd.attach_files = NULL;
550 folder_subscribe(cmd.subscribe_uri);
557 exit_sylpheed(mainwin);
562 static void save_all_caches(FolderItem *item, gpointer data)
566 folder_item_write_cache(item);
569 static void exit_sylpheed(MainWindow *mainwin)
573 debug_print("shutting down\n");
574 inc_autocheck_timer_remove();
576 if (prefs_common.clean_on_exit)
577 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
579 /* save prefs for opened folder */
580 if(mainwin->folderview->opened)
584 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
585 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
588 /* save all state before exiting */
590 folder_func_to_all_folders(save_all_caches, NULL);
592 main_window_get_size(mainwin);
593 main_window_get_position(mainwin);
594 prefs_common_write_config();
595 account_write_config_all();
596 addressbook_export_to_file();
598 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
599 gtk_accel_map_save(filename);
602 /* delete temporary files */
603 remove_all_files(get_tmp_dir());
604 remove_all_files(get_mime_tmp_dir());
611 /* delete crashfile */
613 g_unlink(get_crashfile_name());
615 lock_socket_remove();
617 main_window_destroy(mainwin);
619 plugin_unload_all("GTK2");
621 prefs_toolbar_done();
623 addressbook_destroy();
627 prefs_ext_prog_done();
628 prefs_wrapping_done();
629 prefs_compose_writing_done();
630 prefs_msg_colors_done();
631 prefs_image_viewer_done();
634 prefs_summaries_done();
635 prefs_message_done();
637 prefs_receive_done();
640 prefs_spelling_done();
641 gtkaspell_checkers_quit();
647 static void parse_cmd_opt(int argc, char *argv[])
651 for (i = 1; i < argc; i++) {
652 if (!strncmp(argv[i], "--receive-all", 13))
653 cmd.receive_all = TRUE;
654 else if (!strncmp(argv[i], "--receive", 9))
656 else if (!strncmp(argv[i], "--compose", 9)) {
657 const gchar *p = argv[i + 1];
660 cmd.compose_mailto = NULL;
661 if (p && *p != '\0' && *p != '-') {
662 if (!strncmp(p, "mailto:", 7))
663 cmd.compose_mailto = p + 7;
665 cmd.compose_mailto = p;
668 } else if (!strncmp(argv[i], "--subscribe", 11)) {
669 const gchar *p = argv[i + 1];
670 if (p && *p != '\0' && *p != '-') {
671 cmd.subscribe = TRUE;
672 cmd.subscribe_uri = p;
674 } else if (!strncmp(argv[i], "--attach", 8)) {
675 const gchar *p = argv[i + 1];
678 while (p && *p != '\0' && *p != '-') {
679 if (!cmd.attach_files)
680 cmd.attach_files = g_ptr_array_new();
681 if (*p != G_DIR_SEPARATOR)
682 file = g_strconcat(sylpheed_get_startup_dir(),
687 g_ptr_array_add(cmd.attach_files, file);
691 } else if (!strncmp(argv[i], "--send", 6)) {
693 } else if (!strncmp(argv[i], "--version", 9)) {
694 puts("Sylpheed-Claws version " VERSION);
696 } else if (!strncmp(argv[i], "--status-full", 13)) {
697 const gchar *p = argv[i + 1];
699 cmd.status_full = TRUE;
700 while (p && *p != '\0' && *p != '-') {
701 if (!cmd.status_full_folders)
702 cmd.status_full_folders =
704 g_ptr_array_add(cmd.status_full_folders,
709 } else if (!strncmp(argv[i], "--status", 8)) {
710 const gchar *p = argv[i + 1];
713 while (p && *p != '\0' && *p != '-') {
714 if (!cmd.status_folders)
715 cmd.status_folders = g_ptr_array_new();
716 g_ptr_array_add(cmd.status_folders,
721 } else if (!strncmp(argv[i], "--online", 8)) {
722 cmd.online_mode = ONLINE_MODE_ONLINE;
723 } else if (!strncmp(argv[i], "--offline", 9)) {
724 cmd.online_mode = ONLINE_MODE_OFFLINE;
725 } else if (!strncmp(argv[i], "--help", 6)) {
726 gchar *base = g_path_get_basename(argv[0]);
727 g_print(_("Usage: %s [OPTION]...\n"), base);
729 g_print("%s\n", _(" --compose [address] open composition window"));
730 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
731 g_print("%s\n", _(" --attach file1 [file2]...\n"
732 " open composition window with specified files\n"
734 g_print("%s\n", _(" --receive receive new messages"));
735 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
736 g_print("%s\n", _(" --send send all queued messages"));
737 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
738 g_print("%s\n", _(" --status-full [folder]...\n"
739 " show the status of each folder"));
740 g_print("%s\n", _(" --online switch to online mode"));
741 g_print("%s\n", _(" --offline switch to offline mode"));
742 g_print("%s\n", _(" --exit exit Sylpheed-Claws"));
743 g_print("%s\n", _(" --debug debug mode"));
744 g_print("%s\n", _(" --help display this help and exit"));
745 g_print("%s\n", _(" --version output version information and exit"));
746 g_print("%s\n", _(" --config-dir output configuration directory"));
750 } else if (!strncmp(argv[i], "--crash", 7)) {
752 cmd.crash_params = g_strdup(argv[i + 1]);
754 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
757 } else if (!strncmp(argv[i], "--exit", 6)) {
759 } else if (i == 1 && argc == 2) {
760 /* only one parameter. Do something intelligent about it */
761 if (strstr(argv[i], "@") && !strstr(argv[i], "://")) {
762 const gchar *p = argv[i];
765 cmd.compose_mailto = NULL;
766 if (p && *p != '\0' && *p != '-') {
767 if (!strncmp(p, "mailto:", 7))
768 cmd.compose_mailto = p + 7;
770 cmd.compose_mailto = p;
772 } else if (strstr(argv[i], "://")) {
773 const gchar *p = argv[i];
774 if (p && *p != '\0' && *p != '-') {
775 cmd.subscribe = TRUE;
776 cmd.subscribe_uri = p;
783 if (cmd.attach_files && cmd.compose == FALSE) {
785 cmd.compose_mailto = NULL;
789 static gint get_queued_message_num(void)
793 queue = folder_get_default_queue();
794 if (!queue) return -1;
796 folder_item_scan(queue);
797 return queue->total_msgs;
800 static void initial_processing(FolderItem *item, gpointer data)
802 MainWindow *mainwin = (MainWindow *)data;
805 g_return_if_fail(item);
806 buf = g_strdup_printf(_("Processing (%s)..."),
809 : _("top level folder"));
810 debug_print("%s\n", buf);
814 if (item->prefs->enable_processing)
815 folder_item_apply_processing(item);
817 debug_print("done.\n");
818 STATUSBAR_POP(mainwin);
821 static void draft_all_messages(void)
823 GList *compose_list = NULL;
825 while ((compose_list = compose_get_compose_list()) != NULL) {
826 Compose *c = (Compose*)compose_list->data;
831 gboolean clean_quit(gpointer data)
833 static gboolean firstrun = TRUE;
839 /*!< Good idea to have the main window stored in a
840 * static variable so we can check that variable
841 * to see if we're really allowed to do things
842 * that actually the spawner is supposed to
843 * do (like: sending mail, composing messages).
844 * Because, really, if we're the spawnee, and
845 * we touch GTK stuff, we're hosed. See the
848 /* FIXME: Use something else to signal that we're
849 * in the original spawner, and not in a spawned
851 if (!static_mainwindow)
854 draft_all_messages();
856 exit_sylpheed(static_mainwindow);
862 void app_will_exit(GtkWidget *widget, gpointer data)
864 MainWindow *mainwin = data;
866 if (compose_get_compose_list()) {
867 gint val = alertpanel(_("Really quit?"),
868 _("Composing message exists."),
869 _("_Save to Draft"), _("_Discard them"), _("Do_n't quit"));
873 case G_ALERTALTERNATE:
876 draft_all_messages();
879 manage_window_focus_in(mainwin->window, NULL, NULL);
882 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
883 if (alertpanel(_("Queued messages"),
884 _("Some unsent messages are queued. Exit now?"),
885 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
888 manage_window_focus_in(mainwin->window, NULL, NULL);
897 * CLAWS: want this public so crash dialog can delete the
903 gchar *get_socket_name(void)
905 static gchar *filename = NULL;
907 if (filename == NULL) {
908 filename = g_strdup_printf("%s%csylpheed-claws-%d",
909 g_get_tmp_dir(), G_DIR_SEPARATOR,
920 static gchar *get_crashfile_name(void)
922 static gchar *filename = NULL;
924 if (filename == NULL) {
925 filename = g_strdup_printf("%s%csylpheed-crashed",
926 get_tmp_dir(), G_DIR_SEPARATOR);
932 static gint prohibit_duplicate_launch(void)
937 path = get_socket_name();
938 uxsock = fd_connect_unix(path);
941 return fd_open_unix(path);
944 /* remote command mode */
946 debug_print("another Sylpheed-Claws instance is already running.\n");
949 fd_write_all(uxsock, "receive_all\n", 12);
950 else if (cmd.receive)
951 fd_write_all(uxsock, "receive\n", 8);
952 else if (cmd.compose && cmd.attach_files) {
953 gchar *str, *compose_str;
956 if (cmd.compose_mailto)
957 compose_str = g_strdup_printf("compose_attach %s\n",
960 compose_str = g_strdup("compose_attach\n");
962 fd_write_all(uxsock, compose_str, strlen(compose_str));
965 for (i = 0; i < cmd.attach_files->len; i++) {
966 str = g_ptr_array_index(cmd.attach_files, i);
967 fd_write_all(uxsock, str, strlen(str));
968 fd_write_all(uxsock, "\n", 1);
971 fd_write_all(uxsock, ".\n", 2);
972 } else if (cmd.compose) {
975 if (cmd.compose_mailto)
976 compose_str = g_strdup_printf
977 ("compose %s\n", cmd.compose_mailto);
979 compose_str = g_strdup("compose\n");
981 fd_write_all(uxsock, compose_str, strlen(compose_str));
983 } else if (cmd.subscribe) {
984 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
985 fd_write_all(uxsock, str, strlen(str));
987 } else if (cmd.send) {
988 fd_write_all(uxsock, "send\n", 5);
989 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
990 fd_write(uxsock, "online\n", 6);
991 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
992 fd_write(uxsock, "offline\n", 7);
993 } else if (cmd.status || cmd.status_full) {
996 const gchar *command;
1000 command = cmd.status_full ? "status-full\n" : "status\n";
1001 folders = cmd.status_full ? cmd.status_full_folders :
1004 fd_write_all(uxsock, command, strlen(command));
1005 for (i = 0; folders && i < folders->len; ++i) {
1006 folder = g_ptr_array_index(folders, i);
1007 fd_write_all(uxsock, folder, strlen(folder));
1008 fd_write_all(uxsock, "\n", 1);
1010 fd_write_all(uxsock, ".\n", 2);
1012 fd_gets(uxsock, buf, sizeof(buf));
1013 if (!strncmp(buf, ".\n", 2)) break;
1016 } else if (cmd.exit) {
1017 fd_write_all(uxsock, "exit\n", 5);
1019 fd_write_all(uxsock, "popup\n", 6);
1025 static gint lock_socket_remove(void)
1030 if (lock_socket < 0) return -1;
1032 if (lock_socket_tag > 0)
1033 gdk_input_remove(lock_socket_tag);
1034 fd_close(lock_socket);
1035 filename = get_socket_name();
1042 static GPtrArray *get_folder_item_list(gint sock)
1044 gchar buf[BUFFSIZE];
1046 GPtrArray *folders = NULL;
1049 fd_gets(sock, buf, sizeof(buf));
1050 if (!strncmp(buf, ".\n", 2)) break;
1052 if (!folders) folders = g_ptr_array_new();
1053 item = folder_find_item_from_identifier(buf);
1055 g_ptr_array_add(folders, item);
1057 g_warning("no such folder: %s\n", buf);
1063 static void lock_socket_input_cb(gpointer data,
1065 GdkInputCondition condition)
1067 MainWindow *mainwin = (MainWindow *)data;
1069 gchar buf[BUFFSIZE];
1071 sock = fd_accept(source);
1072 fd_gets(sock, buf, sizeof(buf));
1074 if (!strncmp(buf, "popup", 5)) {
1075 main_window_popup(mainwin);
1076 } else if (!strncmp(buf, "receive_all", 11)) {
1077 inc_all_account_mail(mainwin, FALSE,
1078 prefs_common.newmail_notify_manu);
1079 } else if (!strncmp(buf, "receive", 7)) {
1080 inc_mail(mainwin, prefs_common.newmail_notify_manu);
1081 } else if (!strncmp(buf, "compose_attach", 14)) {
1085 mailto = g_strdup(buf + strlen("compose_attach") + 1);
1086 files = g_ptr_array_new();
1087 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
1088 if (buf[0] == '.' && buf[1] == '\n') break;
1090 g_ptr_array_add(files, g_strdup(buf));
1092 open_compose_new(mailto, files);
1093 ptr_array_free_strings(files);
1094 g_ptr_array_free(files, TRUE);
1096 } else if (!strncmp(buf, "compose", 7)) {
1097 open_compose_new(buf + strlen("compose") + 1, NULL);
1098 } else if (!strncmp(buf, "subscribe", 9)) {
1099 main_window_popup(mainwin);
1100 folder_subscribe(buf + strlen("subscribe") + 1);
1101 } else if (!strncmp(buf, "send", 4)) {
1103 } else if (!strncmp(buf, "online", 6)) {
1104 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1105 } else if (!strncmp(buf, "offline", 7)) {
1106 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1107 } else if (!strncmp(buf, "status-full", 11) ||
1108 !strncmp(buf, "status", 6)) {
1112 folders = get_folder_item_list(sock);
1113 status = folder_get_status
1114 (folders, !strncmp(buf, "status-full", 11));
1115 fd_write_all(sock, status, strlen(status));
1116 fd_write_all(sock, ".\n", 2);
1118 if (folders) g_ptr_array_free(folders, TRUE);
1119 } else if (!strncmp(buf, "exit", 4)) {
1120 app_will_exit(NULL, mainwin);
1126 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
1131 Xstrdup_a(addr, address, return);
1135 compose_new(NULL, addr, attach_files);
1138 static void send_queue(void)
1142 for (list = folder_get_list(); list != NULL; list = list->next) {
1143 Folder *folder = list->data;
1145 if (folder->queue) {
1146 gint res = procmsg_send_queue
1147 (folder->queue, prefs_common.savemsg);
1150 alertpanel_error(_("Some errors occurred while sending queued messages."));
1152 folder_item_scan(folder->queue);
1157 static void quit_signal_handler(int sig)
1159 debug_print("Quitting on signal %d\n", sig);
1161 g_timeout_add(0, clean_quit, NULL);
1164 static void install_basic_sighandlers()
1168 struct sigaction act;
1173 sigaddset(&mask, SIGTERM);
1176 sigaddset(&mask, SIGINT);
1179 sigaddset(&mask, SIGHUP);
1182 act.sa_handler = quit_signal_handler;
1187 sigaction(SIGTERM, &act, 0);
1190 sigaction(SIGINT, &act, 0);
1193 sigaction(SIGHUP, &act, 0);
1196 sigprocmask(SIG_UNBLOCK, &mask, 0);
1197 #endif /* !G_OS_WIN32 */