2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkrc.h>
38 #include <sys/types.h>
43 #ifdef HAVE_STARTUP_NOTIFICATION
44 # define SN_API_NOT_YET_FROZEN
45 # include <libsn/sn-launchee.h>
46 # include <gdk/gdkx.h>
51 #include "mainwindow.h"
52 #include "folderview.h"
53 #include "image_viewer.h"
54 #include "summaryview.h"
55 #include "prefs_common.h"
56 #include "prefs_account.h"
57 #include "prefs_actions.h"
58 #include "prefs_ext_prog.h"
59 #include "prefs_fonts.h"
60 #include "prefs_image_viewer.h"
61 #include "prefs_message.h"
62 #include "prefs_receive.h"
63 #include "prefs_msg_colors.h"
64 #include "prefs_quote.h"
65 #include "prefs_spelling.h"
66 #include "prefs_summaries.h"
67 #include "prefs_themes.h"
68 #include "prefs_other.h"
69 #include "prefs_send.h"
70 #include "prefs_wrapping.h"
71 #include "prefs_compose_writing.h"
72 #include "prefs_display_header.h"
77 #include "manage_window.h"
78 #include "alertpanel.h"
79 #include "statusbar.h"
80 #include "addressbook.h"
88 #include "prefs_toolbar.h"
95 #include "imap-thread.h"
97 #include "stock_pixmap.h"
112 #ifdef HAVE_STARTUP_NOTIFICATION
113 static SnLauncheeContext *sn_context = NULL;
114 static SnDisplay *sn_display = NULL;
117 static gint lock_socket = -1;
118 static gint lock_socket_tag = 0;
122 ONLINE_MODE_DONT_CHANGE,
127 static struct RemoteCmd {
129 gboolean receive_all;
131 const gchar *compose_mailto;
132 GPtrArray *attach_files;
134 gboolean status_full;
135 GPtrArray *status_folders;
136 GPtrArray *status_full_folders;
143 const gchar *subscribe_uri;
147 static void parse_cmd_opt(int argc, char *argv[]);
149 static gint prohibit_duplicate_launch (void);
150 static gchar * get_crashfile_name (void);
151 static gint lock_socket_remove (void);
152 static void lock_socket_input_cb (gpointer data,
154 GdkInputCondition condition);
158 gchar *get_socket_name (void);
161 static void open_compose_new (const gchar *address,
162 GPtrArray *attach_files);
164 static void send_queue (void);
165 static void initial_processing (FolderItem *item, gpointer data);
166 static void quit_signal_handler (int sig);
167 static void install_basic_sighandlers (void);
168 static void exit_sylpheed (MainWindow *mainwin);
170 #define MAKE_DIR_IF_NOT_EXIST(dir) \
172 if (!is_dir_exist(dir)) { \
173 if (is_file_exist(dir)) { \
175 (_("File '%s' already exists.\n" \
176 "Can't create folder."), \
180 if (make_dir(dir) < 0) \
185 static MainWindow *static_mainwindow;
187 #ifdef HAVE_STARTUP_NOTIFICATION
188 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
190 gdk_error_trap_push();
193 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
195 gdk_error_trap_pop();
198 static void startup_notification_complete(gboolean with_window)
201 GtkWidget *hack = NULL;
204 /* this is needed to make the startup notification leave,
205 * if we have been launched from a menu.
206 * We have to display a window, so let it be very little */
207 hack = gtk_window_new(GTK_WINDOW_POPUP);
208 gtk_widget_set_uposition(hack, 0, 0);
209 gtk_widget_set_size_request(hack, 1, 1);
210 gtk_widget_show(hack);
213 xdisplay = GDK_DISPLAY();
214 sn_display = sn_display_new(xdisplay,
217 sn_context = sn_launchee_context_new_from_environment(sn_display,
218 DefaultScreen(xdisplay));
220 if (sn_context != NULL) {
221 sn_launchee_context_complete(sn_context);
222 sn_launchee_context_unref(sn_context);
223 sn_display_unref(sn_display);
226 gtk_widget_destroy(hack);
229 #endif /* HAVE_STARTUP_NOTIFICATION */
231 void sylpheed_gtk_idle(void)
233 while(gtk_events_pending()) {
234 gtk_main_iteration();
239 gboolean defer_check_all(void *data)
241 gboolean autochk = GPOINTER_TO_INT(data);
243 inc_all_account_mail(static_mainwindow, autochk,
244 prefs_common.newmail_notify_manu);
249 gboolean defer_check(void *data)
251 inc_mail(static_mainwindow, prefs_common.newmail_notify_manu);
256 static gboolean defer_jump(void *data)
258 mainwindow_jump_to(data);
262 static gboolean migrate_old_config(const gchar *old_cfg_dir, const gchar *new_cfg_dir)
264 gchar *message = g_strdup_printf(_("Configuration for Sylpheed-Claws %s found.\n"
265 "Do you want to migrate this configuration?"),
266 !strcmp(old_cfg_dir, OLD_GTK1_RC_DIR)?
267 _("1.0.5 or previous"):_("1.9.15 or previous"));
269 GtkWidget *window = NULL;
270 if (alertpanel(_("Migration of configuration"),
272 GTK_STOCK_NO, GTK_STOCK_YES, NULL) != G_ALERTALTERNATE) {
276 window = label_window_create(_("Copying configuration..."));
278 r = copy_dir(old_cfg_dir, new_cfg_dir);
279 gtk_widget_destroy(window);
281 alertpanel_error(_("Migration failed!"));
286 int main(int argc, char *argv[])
290 FolderView *folderview;
292 gboolean crash_file_present = FALSE;
293 gint num_folder_class = 0;
294 if (!sylpheed_init(&argc, &argv)) {
298 prog_version = PROG_VERSION;
300 argv0 = g_strdup(argv[0]);
303 parse_cmd_opt(argc, argv);
308 gtk_init(&argc, &argv);
309 crash_main(cmd.crash_params);
312 crash_install_handlers();
314 install_basic_sighandlers();
317 /* check and create unix domain socket for remote operation */
319 lock_socket = prohibit_duplicate_launch();
320 if (lock_socket < 0) {
321 #ifdef HAVE_STARTUP_NOTIFICATION
322 if(gtk_init_check(&argc, &argv))
323 startup_notification_complete(TRUE);
328 if (cmd.status || cmd.status_full) {
329 puts("0 Sylpheed-Claws not running.");
330 lock_socket_remove();
337 if (!g_thread_supported())
341 gtk_init(&argc, &argv);
344 gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
345 gtk_widget_set_default_visual(gdk_rgb_get_visual());
347 if (!g_thread_supported()) {
348 g_error(_("g_thread is not supported by glib.\n"));
351 /* parse gtkrc files */
352 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
354 gtk_rc_parse(userrc);
356 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
357 G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
358 gtk_rc_parse(userrc);
361 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
362 if (!is_dir_exist(RC_DIR)) {
364 if (is_dir_exist(OLD_GTK2_RC_DIR))
365 r = migrate_old_config(OLD_GTK2_RC_DIR, RC_DIR);
366 else if (is_dir_exist(OLD_GTK1_RC_DIR))
367 r = migrate_old_config(OLD_GTK1_RC_DIR, RC_DIR);
368 if (r == FALSE && !is_dir_exist(RC_DIR) && make_dir(RC_DIR) < 0)
372 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
373 gtk_rc_parse(userrc);
376 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
377 gtk_accel_map_load (userrc);
380 gtk_settings_set_long_property(gtk_settings_get_default(),
381 "gtk-can-change-accels",
382 (glong)TRUE, "XProperty");
384 CHDIR_RETURN_VAL_IF_FAIL(get_rc_dir(), 1);
386 MAKE_DIR_IF_NOT_EXIST(get_mail_base_dir());
387 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
388 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
389 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
390 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
391 MAKE_DIR_IF_NOT_EXIST(UIDL_DIR);
393 crash_file_present = is_file_exist(get_crashfile_name());
394 /* remove temporary files */
395 remove_all_files(get_tmp_dir());
396 remove_all_files(get_mime_tmp_dir());
398 if (is_file_exist("sylpheed.log")) {
399 if (rename_force("sylpheed.log", "sylpheed.log.bak") < 0)
400 FILE_OP_ERROR("sylpheed.log", "rename");
402 set_log_file("sylpheed.log");
404 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
406 folder_system_init();
407 prefs_common_read_config();
411 prefs_ext_prog_init();
412 prefs_wrapping_init();
413 prefs_compose_writing_init();
414 prefs_msg_colors_init();
416 prefs_image_viewer_init();
418 prefs_summaries_init();
419 prefs_message_init();
421 prefs_receive_init();
424 gtkaspell_checkers_init();
425 prefs_spelling_init();
428 sock_set_io_timeout(prefs_common.io_timeout_secs);
430 imap_main_set_timeout(prefs_common.io_timeout_secs);
432 prefs_actions_read_config();
433 prefs_display_header_read_config();
434 /* prefs_filtering_read_config(); */
435 addressbook_read_file();
436 renderer_read_config();
439 stock_pixbuf_gdk(NULL, STOCK_PIXMAP_SYLPHEED_ICON, &icon);
440 gtk_window_set_default_icon(icon);
442 folderview_initialize();
448 mainwin = main_window_create
449 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
450 folderview = mainwin->folderview;
452 gtk_clist_freeze(GTK_CLIST(mainwin->folderview->ctree));
453 folder_item_update_freeze();
455 /* register the callback of unix domain socket input */
457 lock_socket_tag = gdk_input_add(lock_socket,
458 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
459 lock_socket_input_cb,
463 prefs_account_init();
464 account_read_config_all();
466 if (folder_read_list() < 0) {
467 if (!run_wizard(mainwin, TRUE))
472 if (!account_get_list()) {
473 if (!run_wizard(mainwin, FALSE))
475 account_read_config_all();
476 if(!account_get_list())
477 exit_sylpheed(mainwin);
480 main_window_popup(mainwin);
483 imap_main_init(prefs_common.skip_ssl_cert_check);
485 account_set_missing_folder();
486 folder_set_missing_folders();
487 folderview_set(folderview);
489 prefs_matcher_read_config();
491 /* make one all-folder processing before using sylpheed */
492 main_window_cursor_wait(mainwin);
493 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
495 /* if Sylpheed crashed, rebuild caches */
496 if (!cmd.crash && crash_file_present) {
497 debug_print("Sylpheed-Claws crashed, checking for new messages in local folders\n");
498 folder_item_update_thaw();
499 folderview_check_new(NULL);
500 folder_clean_cache_memory_force();
501 folder_item_update_freeze();
503 /* make the crash-indicator file */
504 str_write_to_file("foo", get_crashfile_name());
506 inc_autocheck_timer_init(mainwin);
508 /* ignore SIGPIPE signal for preventing sudden death of program */
510 signal(SIGPIPE, SIG_IGN);
512 if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
513 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
515 if (cmd.online_mode == ONLINE_MODE_ONLINE) {
516 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
519 if (cmd.status_folders) {
520 g_ptr_array_free(cmd.status_folders, TRUE);
521 cmd.status_folders = NULL;
523 if (cmd.status_full_folders) {
524 g_ptr_array_free(cmd.status_full_folders, TRUE);
525 cmd.status_full_folders = NULL;
528 sylpheed_register_idle_function(sylpheed_gtk_idle);
530 prefs_toolbar_init();
532 num_folder_class = g_list_length(folder_get_list());
534 plugin_load_all("GTK2");
536 if (g_list_length(folder_get_list()) != num_folder_class) {
537 debug_print("new folders loaded, reloading processing rules\n");
538 prefs_matcher_read_config();
541 if (plugin_get_unloaded_list() != NULL) {
542 alertpanel_warning(_("Some plugin(s) failed to load. "
543 "Check the Plugins configuration "
544 "for more information."));
547 if (!folder_have_mailbox()) {
548 alertpanel_error(_("Sylpheed-Claws has detected a configured "
549 "mailbox, but could not load it. It is "
550 "probably provided by an out-of-date "
551 "external plugin. Please reinstall the "
552 "plugin and try again."));
556 static_mainwindow = mainwin;
558 #ifdef HAVE_STARTUP_NOTIFICATION
559 startup_notification_complete(FALSE);
561 folder_item_update_thaw();
562 gtk_clist_thaw(GTK_CLIST(mainwin->folderview->ctree));
563 main_window_cursor_normal(mainwin);
565 if (cmd.receive_all) {
566 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(FALSE));
567 } else if (prefs_common.chk_on_startup) {
568 g_timeout_add(1000, defer_check_all, GINT_TO_POINTER(TRUE));
569 } else if (cmd.receive) {
570 g_timeout_add(1000, defer_check, NULL);
572 gtk_widget_grab_focus(folderview->ctree);
576 open_compose_new(cmd.compose_mailto, cmd.attach_files);
578 if (cmd.attach_files) {
579 ptr_array_free_strings(cmd.attach_files);
580 g_ptr_array_free(cmd.attach_files, TRUE);
581 cmd.attach_files = NULL;
584 folder_subscribe(cmd.subscribe_uri);
592 g_timeout_add(500, defer_jump, (gpointer)cmd.target);
597 exit_sylpheed(mainwin);
602 static void save_all_caches(FolderItem *item, gpointer data)
609 folder_item_close(item);
611 folder_item_free_cache(item);
614 static void exit_sylpheed(MainWindow *mainwin)
618 debug_print("shutting down\n");
619 inc_autocheck_timer_remove();
621 if (prefs_common.clean_on_exit) {
622 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
625 /* save prefs for opened folder */
626 if(mainwin->folderview->opened) {
629 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
630 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
633 /* save all state before exiting */
635 folder_func_to_all_folders(save_all_caches, NULL);
637 main_window_get_size(mainwin);
638 main_window_get_position(mainwin);
639 prefs_common_write_config();
640 account_write_config_all();
641 addressbook_export_to_file();
643 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
644 gtk_accel_map_save(filename);
647 /* delete temporary files */
648 remove_all_files(get_tmp_dir());
649 remove_all_files(get_mime_tmp_dir());
656 /* delete crashfile */
658 g_unlink(get_crashfile_name());
660 lock_socket_remove();
662 main_window_destroy_all();
664 plugin_unload_all("GTK2");
666 prefs_toolbar_done();
668 addressbook_destroy();
672 prefs_ext_prog_done();
673 prefs_wrapping_done();
674 prefs_compose_writing_done();
675 prefs_msg_colors_done();
676 prefs_image_viewer_done();
679 prefs_summaries_done();
680 prefs_message_done();
682 prefs_receive_done();
685 prefs_spelling_done();
686 gtkaspell_checkers_quit();
692 static void parse_cmd_opt(int argc, char *argv[])
696 for (i = 1; i < argc; i++) {
697 if (!strncmp(argv[i], "--receive-all", 13)) {
698 cmd.receive_all = TRUE;
699 } else if (!strncmp(argv[i], "--receive", 9)) {
701 } else if (!strncmp(argv[i], "--compose", 9)) {
702 const gchar *p = argv[i + 1];
705 cmd.compose_mailto = NULL;
706 if (p && *p != '\0' && *p != '-') {
707 if (!strncmp(p, "mailto:", 7)) {
708 cmd.compose_mailto = p + 7;
710 cmd.compose_mailto = p;
714 } else if (!strncmp(argv[i], "--subscribe", 11)) {
715 const gchar *p = argv[i + 1];
716 if (p && *p != '\0' && *p != '-') {
717 cmd.subscribe = TRUE;
718 cmd.subscribe_uri = p;
720 } else if (!strncmp(argv[i], "--attach", 8)) {
721 const gchar *p = argv[i + 1];
724 while (p && *p != '\0' && *p != '-') {
725 if (!cmd.attach_files) {
726 cmd.attach_files = g_ptr_array_new();
728 if (*p != G_DIR_SEPARATOR) {
729 file = g_strconcat(sylpheed_get_startup_dir(),
735 g_ptr_array_add(cmd.attach_files, file);
739 } else if (!strncmp(argv[i], "--send", 6)) {
741 } else if (!strncmp(argv[i], "--version", 9)) {
742 puts("Sylpheed-Claws version " VERSION);
744 } else if (!strncmp(argv[i], "--status-full", 13)) {
745 const gchar *p = argv[i + 1];
747 cmd.status_full = TRUE;
748 while (p && *p != '\0' && *p != '-') {
749 if (!cmd.status_full_folders) {
750 cmd.status_full_folders =
753 g_ptr_array_add(cmd.status_full_folders,
758 } else if (!strncmp(argv[i], "--status", 8)) {
759 const gchar *p = argv[i + 1];
762 while (p && *p != '\0' && *p != '-') {
763 if (!cmd.status_folders)
764 cmd.status_folders = g_ptr_array_new();
765 g_ptr_array_add(cmd.status_folders,
770 } else if (!strncmp(argv[i], "--online", 8)) {
771 cmd.online_mode = ONLINE_MODE_ONLINE;
772 } else if (!strncmp(argv[i], "--offline", 9)) {
773 cmd.online_mode = ONLINE_MODE_OFFLINE;
774 } else if (!strncmp(argv[i], "--help", 6)) {
775 gchar *base = g_path_get_basename(argv[0]);
776 g_print(_("Usage: %s [OPTION]...\n"), base);
778 g_print("%s\n", _(" --compose [address] open composition window"));
779 g_print("%s\n", _(" --subscribe [uri] subscribe to the given URI if possible"));
780 g_print("%s\n", _(" --attach file1 [file2]...\n"
781 " open composition window with specified files\n"
783 g_print("%s\n", _(" --receive receive new messages"));
784 g_print("%s\n", _(" --receive-all receive new messages of all accounts"));
785 g_print("%s\n", _(" --send send all queued messages"));
786 g_print("%s\n", _(" --status [folder]... show the total number of messages"));
787 g_print("%s\n", _(" --status-full [folder]...\n"
788 " show the status of each folder"));
789 g_print("%s\n", _(" --select folder[/msg] jumps to the specified folder/message\n"
790 " folder is a folder id like '#mh/Mailbox/inbox'"));
791 g_print("%s\n", _(" --online switch to online mode"));
792 g_print("%s\n", _(" --offline switch to offline mode"));
793 g_print("%s\n", _(" --exit exit Sylpheed-Claws"));
794 g_print("%s\n", _(" --debug debug mode"));
795 g_print("%s\n", _(" --help display this help and exit"));
796 g_print("%s\n", _(" --version output version information and exit"));
797 g_print("%s\n", _(" --config-dir output configuration directory"));
801 } else if (!strncmp(argv[i], "--crash", 7)) {
803 cmd.crash_params = g_strdup(argv[i + 1]);
805 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
808 } else if (!strncmp(argv[i], "--exit", 6)) {
810 } else if (!strncmp(argv[i], "--select", 8) && i+1 < argc) {
811 cmd.target = argv[i+1];
812 } else if (i == 1 && argc == 2) {
813 /* only one parameter. Do something intelligent about it */
814 if (strstr(argv[i], "@") && !strstr(argv[i], "://")) {
815 const gchar *p = argv[i];
818 cmd.compose_mailto = NULL;
819 if (p && *p != '\0' && *p != '-') {
820 if (!strncmp(p, "mailto:", 7)) {
821 cmd.compose_mailto = p + 7;
823 cmd.compose_mailto = p;
826 } else if (strstr(argv[i], "://")) {
827 const gchar *p = argv[i];
828 if (p && *p != '\0' && *p != '-') {
829 cmd.subscribe = TRUE;
830 cmd.subscribe_uri = p;
837 if (cmd.attach_files && cmd.compose == FALSE) {
839 cmd.compose_mailto = NULL;
843 static gint get_queued_message_num(void)
847 queue = folder_get_default_queue();
852 folder_item_scan(queue);
853 return queue->total_msgs;
856 static void initial_processing(FolderItem *item, gpointer data)
858 MainWindow *mainwin = (MainWindow *)data;
861 g_return_if_fail(item);
862 buf = g_strdup_printf(_("Processing (%s)..."),
865 : _("top level folder"));
866 debug_print("%s\n", buf);
870 if (item->prefs->enable_processing) {
871 folder_item_apply_processing(item);
874 debug_print("done.\n");
875 STATUSBAR_POP(mainwin);
878 static void draft_all_messages(void)
880 GList *compose_list = NULL;
882 while ((compose_list = compose_get_compose_list()) != NULL) {
883 Compose *c = (Compose*)compose_list->data;
888 gboolean clean_quit(gpointer data)
890 static gboolean firstrun = TRUE;
897 /*!< Good idea to have the main window stored in a
898 * static variable so we can check that variable
899 * to see if we're really allowed to do things
900 * that actually the spawner is supposed to
901 * do (like: sending mail, composing messages).
902 * Because, really, if we're the spawnee, and
903 * we touch GTK stuff, we're hosed. See the
906 /* FIXME: Use something else to signal that we're
907 * in the original spawner, and not in a spawned
909 if (!static_mainwindow) {
913 draft_all_messages();
915 exit_sylpheed(static_mainwindow);
921 void app_will_exit(GtkWidget *widget, gpointer data)
923 MainWindow *mainwin = data;
925 if (compose_get_compose_list()) {
926 gint val = alertpanel(_("Really quit?"),
927 _("Composing message exists."),
928 _("_Save to Draft"), _("_Discard them"), _("Do_n't quit"));
932 case G_ALERTALTERNATE:
935 draft_all_messages();
938 manage_window_focus_in(mainwin->window, NULL, NULL);
941 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
942 if (alertpanel(_("Queued messages"),
943 _("Some unsent messages are queued. Exit now?"),
944 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL)
945 != G_ALERTALTERNATE) {
948 manage_window_focus_in(mainwin->window, NULL, NULL);
952 if (folderview_get_selected_item(mainwin->folderview))
953 folder_item_close(folderview_get_selected_item(mainwin->folderview));
958 * CLAWS: want this public so crash dialog can delete the
964 gchar *get_socket_name(void)
966 static gchar *filename = NULL;
968 if (filename == NULL) {
969 filename = g_strdup_printf("%s%csylpheed-claws-%d",
970 g_get_tmp_dir(), G_DIR_SEPARATOR,
981 static gchar *get_crashfile_name(void)
983 static gchar *filename = NULL;
985 if (filename == NULL) {
986 filename = g_strdup_printf("%s%csylpheed-crashed",
987 get_tmp_dir(), G_DIR_SEPARATOR);
993 static gint prohibit_duplicate_launch(void)
998 path = get_socket_name();
999 uxsock = fd_connect_unix(path);
1002 return fd_open_unix(path);
1005 /* remote command mode */
1007 debug_print("another Sylpheed-Claws instance is already running.\n");
1009 if (cmd.receive_all) {
1010 fd_write_all(uxsock, "receive_all\n", 12);
1011 } else if (cmd.receive) {
1012 fd_write_all(uxsock, "receive\n", 8);
1013 } else if (cmd.compose && cmd.attach_files) {
1014 gchar *str, *compose_str;
1017 if (cmd.compose_mailto) {
1018 compose_str = g_strdup_printf("compose_attach %s\n",
1019 cmd.compose_mailto);
1021 compose_str = g_strdup("compose_attach\n");
1024 fd_write_all(uxsock, compose_str, strlen(compose_str));
1025 g_free(compose_str);
1027 for (i = 0; i < cmd.attach_files->len; i++) {
1028 str = g_ptr_array_index(cmd.attach_files, i);
1029 fd_write_all(uxsock, str, strlen(str));
1030 fd_write_all(uxsock, "\n", 1);
1033 fd_write_all(uxsock, ".\n", 2);
1034 } else if (cmd.compose) {
1037 if (cmd.compose_mailto) {
1038 compose_str = g_strdup_printf
1039 ("compose %s\n", cmd.compose_mailto);
1041 compose_str = g_strdup("compose\n");
1044 fd_write_all(uxsock, compose_str, strlen(compose_str));
1045 g_free(compose_str);
1046 } else if (cmd.subscribe) {
1047 gchar *str = g_strdup_printf("subscribe %s\n", cmd.subscribe_uri);
1048 fd_write_all(uxsock, str, strlen(str));
1050 } else if (cmd.send) {
1051 fd_write_all(uxsock, "send\n", 5);
1052 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
1053 fd_write(uxsock, "online\n", 6);
1054 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
1055 fd_write(uxsock, "offline\n", 7);
1056 } else if (cmd.status || cmd.status_full) {
1057 gchar buf[BUFFSIZE];
1059 const gchar *command;
1063 command = cmd.status_full ? "status-full\n" : "status\n";
1064 folders = cmd.status_full ? cmd.status_full_folders :
1067 fd_write_all(uxsock, command, strlen(command));
1068 for (i = 0; folders && i < folders->len; ++i) {
1069 folder = g_ptr_array_index(folders, i);
1070 fd_write_all(uxsock, folder, strlen(folder));
1071 fd_write_all(uxsock, "\n", 1);
1073 fd_write_all(uxsock, ".\n", 2);
1075 fd_gets(uxsock, buf, sizeof(buf));
1076 if (!strncmp(buf, ".\n", 2)) break;
1079 } else if (cmd.exit) {
1080 fd_write_all(uxsock, "exit\n", 5);
1081 } else if (cmd.target) {
1082 gchar *str = g_strdup_printf("select %s\n", cmd.target);
1083 fd_write_all(uxsock, str, strlen(str));
1086 fd_write_all(uxsock, "popup\n", 6);
1092 static gint lock_socket_remove(void)
1097 if (lock_socket < 0) {
1101 if (lock_socket_tag > 0) {
1102 gdk_input_remove(lock_socket_tag);
1104 fd_close(lock_socket);
1105 filename = get_socket_name();
1112 static GPtrArray *get_folder_item_list(gint sock)
1114 gchar buf[BUFFSIZE];
1116 GPtrArray *folders = NULL;
1119 fd_gets(sock, buf, sizeof(buf));
1120 if (!strncmp(buf, ".\n", 2)) {
1125 folders = g_ptr_array_new();
1127 item = folder_find_item_from_identifier(buf);
1129 g_ptr_array_add(folders, item);
1131 g_warning("no such folder: %s\n", buf);
1138 static void lock_socket_input_cb(gpointer data,
1140 GdkInputCondition condition)
1142 MainWindow *mainwin = (MainWindow *)data;
1144 gchar buf[BUFFSIZE];
1146 sock = fd_accept(source);
1147 fd_gets(sock, buf, sizeof(buf));
1149 if (!strncmp(buf, "popup", 5)) {
1150 main_window_popup(mainwin);
1151 } else if (!strncmp(buf, "receive_all", 11)) {
1152 inc_all_account_mail(mainwin, FALSE,
1153 prefs_common.newmail_notify_manu);
1154 } else if (!strncmp(buf, "receive", 7)) {
1155 inc_mail(mainwin, prefs_common.newmail_notify_manu);
1156 } else if (!strncmp(buf, "compose_attach", 14)) {
1160 mailto = g_strdup(buf + strlen("compose_attach") + 1);
1161 files = g_ptr_array_new();
1162 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
1163 if (buf[0] == '.' && buf[1] == '\n') {
1167 g_ptr_array_add(files, g_strdup(buf));
1169 open_compose_new(mailto, files);
1170 ptr_array_free_strings(files);
1171 g_ptr_array_free(files, TRUE);
1173 } else if (!strncmp(buf, "compose", 7)) {
1174 open_compose_new(buf + strlen("compose") + 1, NULL);
1175 } else if (!strncmp(buf, "subscribe", 9)) {
1176 main_window_popup(mainwin);
1177 folder_subscribe(buf + strlen("subscribe") + 1);
1178 } else if (!strncmp(buf, "send", 4)) {
1180 } else if (!strncmp(buf, "online", 6)) {
1181 main_window_toggle_work_offline(mainwin, FALSE, FALSE);
1182 } else if (!strncmp(buf, "offline", 7)) {
1183 main_window_toggle_work_offline(mainwin, TRUE, FALSE);
1184 } else if (!strncmp(buf, "status-full", 11) ||
1185 !strncmp(buf, "status", 6)) {
1189 folders = get_folder_item_list(sock);
1190 status = folder_get_status
1191 (folders, !strncmp(buf, "status-full", 11));
1192 fd_write_all(sock, status, strlen(status));
1193 fd_write_all(sock, ".\n", 2);
1195 if (folders) g_ptr_array_free(folders, TRUE);
1196 } else if (!strncmp(buf, "select ", 7)) {
1197 const gchar *target = buf+7;
1198 mainwindow_jump_to(target);
1199 } else if (!strncmp(buf, "exit", 4)) {
1200 app_will_exit(NULL, mainwin);
1206 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
1211 Xstrdup_a(addr, address, return);
1215 compose_new(NULL, addr, attach_files);
1218 static void send_queue(void)
1222 for (list = folder_get_list(); list != NULL; list = list->next) {
1223 Folder *folder = list->data;
1225 if (folder->queue) {
1226 gint res = procmsg_send_queue
1227 (folder->queue, prefs_common.savemsg);
1230 alertpanel_error(_("Some errors occurred while sending queued messages."));
1233 folder_item_scan(folder->queue);
1239 static void quit_signal_handler(int sig)
1241 debug_print("Quitting on signal %d\n", sig);
1243 g_timeout_add(0, clean_quit, NULL);
1246 static void install_basic_sighandlers()
1250 struct sigaction act;
1255 sigaddset(&mask, SIGTERM);
1258 sigaddset(&mask, SIGINT);
1261 sigaddset(&mask, SIGHUP);
1264 act.sa_handler = quit_signal_handler;
1269 sigaction(SIGTERM, &act, 0);
1272 sigaction(SIGINT, &act, 0);
1275 sigaction(SIGHUP, &act, 0);
1278 sigprocmask(SIG_UNBLOCK, &mask, 0);
1279 #endif /* !G_OS_WIN32 */