2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <gtk/gtkmain.h>
28 #include <gtk/gtkrc.h>
37 #include <sys/types.h>
40 #ifdef HAVE_STARTUP_NOTIFICATION
41 # define SN_API_NOT_YET_FROZEN
42 # include <libsn/sn-launchee.h>
43 # include <gdk/gdkx.h>
49 #include "mainwindow.h"
50 #include "folderview.h"
51 #include "summaryview.h"
52 #include "prefs_common.h"
53 #include "prefs_account.h"
54 #include "prefs_actions.h"
55 #include "prefs_ext_prog.h"
56 #include "prefs_fonts.h"
57 #include "prefs_msg_colors.h"
58 #include "prefs_spelling.h"
59 #include "prefs_themes.h"
60 #include "prefs_wrapping.h"
61 #include "prefs_display_header.h"
66 #include "manage_window.h"
67 #include "alertpanel.h"
68 #include "statusbar.h"
69 #include "addressbook.h"
77 #include "prefs_toolbar.h"
97 #ifdef HAVE_STARTUP_NOTIFICATION
98 static SnLauncheeContext *sn_context = NULL;
99 static SnDisplay *sn_display = NULL;
102 static gint lock_socket = -1;
103 static gint lock_socket_tag = 0;
107 ONLINE_MODE_DONT_CHANGE,
112 static struct RemoteCmd {
114 gboolean receive_all;
116 const gchar *compose_mailto;
117 GPtrArray *attach_files;
119 gboolean status_full;
120 GPtrArray *status_folders;
121 GPtrArray *status_full_folders;
128 static void parse_cmd_opt(int argc, char *argv[]);
130 static gint prohibit_duplicate_launch (void);
131 static gchar * get_crashfile_name (void);
132 static gint lock_socket_remove (void);
133 static void lock_socket_input_cb (gpointer data,
135 GdkInputCondition condition);
139 gchar *get_socket_name (void);
142 static void open_compose_new (const gchar *address,
143 GPtrArray *attach_files);
145 static void send_queue (void);
146 static void initial_processing (FolderItem *item, gpointer data);
147 static void quit_signal_handler (int sig);
148 static void install_basic_sighandlers (void);
149 static void exit_sylpheed (MainWindow *mainwin);
153 _("File `%s' already exists.\n"
154 "Can't create folder.")
157 #define MAKE_DIR_IF_NOT_EXIST(dir) \
159 if (!is_dir_exist(dir)) { \
160 if (is_file_exist(dir)) { \
162 (_("File `%s' already exists.\n" \
163 "Can't create folder."), \
167 if (make_dir(dir) < 0) \
172 static MainWindow *static_mainwindow;
174 #ifdef HAVE_STARTUP_NOTIFICATION
175 static void sn_error_trap_push(SnDisplay *display, Display *xdisplay)
177 gdk_error_trap_push();
180 static void sn_error_trap_pop(SnDisplay *display, Display *xdisplay)
182 gdk_error_trap_pop();
185 static void startup_notification_complete(gboolean with_window)
188 GtkWidget *hack = NULL;
191 /* this is needed to make the startup notification leave,
192 * if we have been launched from a menu.
193 * We have to display a window, so let it be very little */
194 hack = gtk_window_new(GTK_WINDOW_POPUP);
195 gtk_widget_set_uposition(hack, 0, 0);
196 gtk_widget_set_usize(hack, 1, 1);
197 gtk_widget_show(hack);
200 xdisplay = GDK_DISPLAY();
201 sn_display = sn_display_new(xdisplay,
204 sn_context = sn_launchee_context_new_from_environment(sn_display,
205 DefaultScreen(xdisplay));
207 if (sn_context != NULL)
209 sn_launchee_context_complete(sn_context);
210 sn_launchee_context_unref(sn_context);
211 sn_display_unref(sn_display);
214 #endif /* HAVE_STARTUP_NOTIFICATION */
216 void sylpheed_gtk_idle(void)
218 while(gtk_events_pending())
219 gtk_main_iteration();
222 int main(int argc, char *argv[])
227 FolderView *folderview;
229 if (!sylpheed_init(&argc, &argv)) {
233 prog_version = PROG_VERSION;
235 argv0 = g_strdup(argv[0]);
238 parse_cmd_opt(argc, argv);
243 gtk_init(&argc, &argv);
244 crash_main(cmd.crash_params);
247 crash_install_handlers();
249 install_basic_sighandlers();
251 /* check and create unix domain socket */
252 lock_socket = prohibit_duplicate_launch();
253 if (lock_socket < 0) {
254 #ifdef HAVE_STARTUP_NOTIFICATION
255 if(gtk_init_check(&argc, &argv))
256 startup_notification_complete(TRUE);
261 if (cmd.status || cmd.status_full) {
262 puts("0 Sylpheed not running.");
263 lock_socket_remove();
268 gtk_init(&argc, &argv);
271 gtk_widget_set_default_colormap(gdk_rgb_get_cmap());
272 gtk_widget_set_default_visual(gdk_rgb_get_visual());
274 #if USE_THREADS || USE_LDAP
276 if (!g_thread_supported())
277 g_error(_("g_thread is not supported by glib.\n"));
280 /* parse gtkrc files */
281 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
283 gtk_rc_parse(userrc);
285 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
286 G_DIR_SEPARATOR_S, "gtkrc", NULL);
287 gtk_rc_parse(userrc);
289 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
290 gtk_rc_parse(userrc);
293 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
294 gtk_item_factory_parse_rc(userrc);
297 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
299 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
300 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
301 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
302 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
303 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
304 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
306 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
308 folder_system_init();
310 prefs_common_read_config();
314 prefs_ext_prog_init();
315 prefs_wrapping_init();
316 prefs_msg_colors_init();
318 gtkaspell_checkers_init();
319 prefs_spelling_init();
322 sock_set_io_timeout(prefs_common.io_timeout_secs);
324 prefs_actions_read_config();
325 prefs_display_header_read_config();
326 /* prefs_filtering_read_config(); */
327 addressbook_read_file();
328 renderer_read_config();
332 folderview_initialize();
336 mainwin = main_window_create
337 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
338 folderview = mainwin->folderview;
340 /* register the callback of unix domain socket input */
341 lock_socket_tag = gdk_input_add(lock_socket,
342 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
343 lock_socket_input_cb,
346 prefs_account_init();
347 account_read_config_all();
349 if (folder_read_list() < 0) {
353 if (!account_get_list()) {
358 account_set_missing_folder();
359 folder_set_missing_folders();
360 folderview_set(folderview);
362 prefs_matcher_read_config();
364 /* make one all-folder processing before using sylpheed */
365 main_window_cursor_wait(mainwin);
366 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
367 main_window_cursor_normal(mainwin);
369 /* if Sylpheed crashed, rebuild caches */
370 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
371 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
372 folderview_check_new(NULL);
374 /* make the crash-indicator file */
375 str_write_to_file("foo", get_crashfile_name());
377 addressbook_read_file();
379 inc_autocheck_timer_init(mainwin);
381 /* ignore SIGPIPE signal for preventing sudden death of program */
382 signal(SIGPIPE, SIG_IGN);
384 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
385 main_window_toggle_work_offline(mainwin, TRUE);
386 if (cmd.online_mode == ONLINE_MODE_ONLINE)
387 main_window_toggle_work_offline(mainwin, FALSE);
390 inc_all_account_mail(mainwin, FALSE,
391 prefs_common.newmail_notify_manu);
392 else if (prefs_common.chk_on_startup)
393 inc_all_account_mail(mainwin, TRUE,
394 prefs_common.newmail_notify_manu);
395 else if (cmd.receive)
396 inc_mail(mainwin, prefs_common.newmail_notify_manu);
398 gtk_widget_grab_focus(folderview->ctree);
401 open_compose_new(cmd.compose_mailto, cmd.attach_files);
402 if (cmd.attach_files) {
403 ptr_array_free_strings(cmd.attach_files);
404 g_ptr_array_free(cmd.attach_files, TRUE);
405 cmd.attach_files = NULL;
409 if (cmd.status_folders) {
410 g_ptr_array_free(cmd.status_folders, TRUE);
411 cmd.status_folders = NULL;
413 if (cmd.status_full_folders) {
414 g_ptr_array_free(cmd.status_full_folders, TRUE);
415 cmd.status_full_folders = NULL;
418 sylpheed_register_idle_function(sylpheed_gtk_idle);
420 prefs_toolbar_init();
422 plugin_load_all("GTK");
424 static_mainwindow = mainwin;
426 #ifdef HAVE_STARTUP_NOTIFICATION
427 startup_notification_complete(FALSE);
431 exit_sylpheed(mainwin);
436 static void save_all_caches(FolderItem *item, gpointer data)
440 folder_item_write_cache(item);
443 static void exit_sylpheed(MainWindow *mainwin)
447 debug_print("shutting down\n");
449 inc_autocheck_timer_remove();
451 if (prefs_common.clean_on_exit)
452 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
454 /* save prefs for opened folder */
455 if(mainwin->folderview->opened)
459 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
460 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
463 /* save all state before exiting */
465 folder_func_to_all_folders(save_all_caches, NULL);
467 main_window_get_size(mainwin);
468 main_window_get_position(mainwin);
469 prefs_common_write_config();
470 account_write_config_all();
471 addressbook_export_to_file();
473 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
474 gtk_item_factory_dump_rc(filename, NULL, TRUE);
477 /* delete temporary files */
478 remove_all_files(get_mime_tmp_dir());
482 /* delete crashfile */
484 unlink(get_crashfile_name());
486 lock_socket_remove();
488 main_window_destroy(mainwin);
490 plugin_unload_all("GTK");
492 prefs_toolbar_done();
494 addressbook_destroy();
498 prefs_ext_prog_done();
499 prefs_wrapping_done();
500 prefs_msg_colors_done();
502 prefs_spelling_done();
503 gtkaspell_checkers_quit();
509 static void parse_cmd_opt(int argc, char *argv[])
513 for (i = 1; i < argc; i++) {
514 if (!strncmp(argv[i], "--receive-all", 13))
515 cmd.receive_all = TRUE;
516 else if (!strncmp(argv[i], "--receive", 9))
518 else if (!strncmp(argv[i], "--compose", 9)) {
519 const gchar *p = argv[i + 1];
522 cmd.compose_mailto = NULL;
523 if (p && *p != '\0' && *p != '-') {
524 if (!strncmp(p, "mailto:", 7))
525 cmd.compose_mailto = p + 7;
527 cmd.compose_mailto = p;
530 } else if (!strncmp(argv[i], "--attach", 8)) {
531 const gchar *p = argv[i + 1];
534 while (p && *p != '\0' && *p != '-') {
535 if (!cmd.attach_files)
536 cmd.attach_files = g_ptr_array_new();
537 if (*p != G_DIR_SEPARATOR)
538 file = g_strconcat(sylpheed_get_startup_dir(),
543 g_ptr_array_add(cmd.attach_files, file);
547 } else if (!strncmp(argv[i], "--send", 6)) {
549 } else if (!strncmp(argv[i], "--version", 9)) {
550 puts("Sylpheed version " VERSION);
552 } else if (!strncmp(argv[i], "--status-full", 13)) {
553 const gchar *p = argv[i + 1];
555 cmd.status_full = TRUE;
556 while (p && *p != '\0' && *p != '-') {
557 if (!cmd.status_full_folders)
558 cmd.status_full_folders =
560 g_ptr_array_add(cmd.status_full_folders,
565 } else if (!strncmp(argv[i], "--status", 8)) {
566 const gchar *p = argv[i + 1];
569 while (p && *p != '\0' && *p != '-') {
570 if (!cmd.status_folders)
571 cmd.status_folders = g_ptr_array_new();
572 g_ptr_array_add(cmd.status_folders,
577 } else if (!strncmp(argv[i], "--online", 8)) {
578 cmd.online_mode = ONLINE_MODE_ONLINE;
579 } else if (!strncmp(argv[i], "--offline", 9)) {
580 cmd.online_mode = ONLINE_MODE_OFFLINE;
581 } else if (!strncmp(argv[i], "--help", 6)) {
582 g_print(_("Usage: %s [OPTION]...\n"),
583 g_basename(argv[0]));
585 puts(_(" --compose [address] open composition window"));
586 puts(_(" --attach file1 [file2]...\n"
587 " open composition window with specified files\n"
589 puts(_(" --receive receive new messages"));
590 puts(_(" --receive-all receive new messages of all accounts"));
591 puts(_(" --send send all queued messages"));
592 puts(_(" --status [folder]... show the total number of messages"));
593 puts(_(" --status-full [folder]...\n"
594 " show the status of each folder"));
595 puts(_(" --online switch to online mode"));
596 puts(_(" --offline switch to offline mode"));
597 puts(_(" --debug debug mode"));
598 puts(_(" --help display this help and exit"));
599 puts(_(" --version output version information and exit"));
600 puts(_(" --config-dir output configuration directory"));
603 } else if (!strncmp(argv[i], "--crash", 7)) {
605 cmd.crash_params = g_strdup(argv[i + 1]);
607 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
614 if (cmd.attach_files && cmd.compose == FALSE) {
616 cmd.compose_mailto = NULL;
620 static gint get_queued_message_num(void)
624 queue = folder_get_default_queue();
625 if (!queue) return -1;
627 folder_item_scan(queue);
628 return queue->total_msgs;
631 static void initial_processing(FolderItem *item, gpointer data)
633 MainWindow *mainwin = (MainWindow *)data;
636 g_return_if_fail(item);
637 buf = g_strdup_printf(_("Processing (%s)..."),
640 : _("top level folder"));
641 debug_print("%s\n", buf);
644 if (item->prefs->enable_processing)
645 folder_item_apply_processing(item);
647 debug_print("done.\n");
648 STATUSBAR_POP(mainwin);
651 static void draft_all_messages(void)
653 GList *compose_list = compose_get_compose_list();
657 for (elem = compose_list; elem != NULL && elem->data != NULL;
659 Compose *c = (Compose*)elem->data;
665 gboolean clean_quit(gpointer data)
667 static gboolean firstrun = TRUE;
673 /*!< Good idea to have the main window stored in a
674 * static variable so we can check that variable
675 * to see if we're really allowed to do things
676 * that actually the spawner is supposed to
677 * do (like: sending mail, composing messages).
678 * Because, really, if we're the spawnee, and
679 * we touch GTK stuff, we're hosed. See the
682 /* FIXME: Use something else to signal that we're
683 * in the original spawner, and not in a spawned
685 if (!static_mainwindow)
688 draft_all_messages();
690 exit_sylpheed(static_mainwindow);
696 void app_will_exit(GtkWidget *widget, gpointer data)
698 MainWindow *mainwin = data;
700 if (compose_get_compose_list()) {
701 gint val = alertpanel(_("Really quit?"),
702 _("Composing message exists."),
703 _("Draft them"), _("Discard them"), _("Don't quit"));
707 case G_ALERTALTERNATE:
710 draft_all_messages();
713 manage_window_focus_in(mainwin->window, NULL, NULL);
716 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
717 if (alertpanel(_("Queued messages"),
718 _("Some unsent messages are queued. Exit now?"),
719 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
721 manage_window_focus_in(mainwin->window, NULL, NULL);
727 * CLAWS: want this public so crash dialog can delete the
733 gchar *get_socket_name(void)
735 static gchar *filename = NULL;
737 if (filename == NULL) {
738 filename = g_strdup_printf("%s%csylpheed-%d",
739 g_get_tmp_dir(), G_DIR_SEPARATOR,
746 static gchar *get_crashfile_name(void)
748 static gchar *filename = NULL;
750 if (filename == NULL) {
751 filename = g_strdup_printf("%s%csylpheed-crashed",
752 get_tmp_dir(), G_DIR_SEPARATOR);
758 static gint prohibit_duplicate_launch(void)
763 path = get_socket_name();
764 uxsock = fd_connect_unix(path);
767 return fd_open_unix(path);
770 /* remote command mode */
772 debug_print("another Sylpheed is already running.\n");
775 fd_write_all(uxsock, "receive_all\n", 12);
776 else if (cmd.receive)
777 fd_write_all(uxsock, "receive\n", 8);
778 else if (cmd.compose && cmd.attach_files) {
779 gchar *str, *compose_str;
782 if (cmd.compose_mailto)
783 compose_str = g_strdup_printf("compose_attach %s\n",
786 compose_str = g_strdup("compose_attach\n");
788 fd_write_all(uxsock, compose_str, strlen(compose_str));
791 for (i = 0; i < cmd.attach_files->len; i++) {
792 str = g_ptr_array_index(cmd.attach_files, i);
793 fd_write_all(uxsock, str, strlen(str));
794 fd_write_all(uxsock, "\n", 1);
797 fd_write_all(uxsock, ".\n", 2);
798 } else if (cmd.compose) {
801 if (cmd.compose_mailto)
802 compose_str = g_strdup_printf
803 ("compose %s\n", cmd.compose_mailto);
805 compose_str = g_strdup("compose\n");
807 fd_write_all(uxsock, compose_str, strlen(compose_str));
809 } else if (cmd.send) {
810 fd_write_all(uxsock, "send\n", 5);
811 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
812 fd_write(uxsock, "online\n", 6);
813 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
814 fd_write(uxsock, "offline\n", 7);
815 } else if (cmd.status || cmd.status_full) {
818 const gchar *command;
822 command = cmd.status_full ? "status-full\n" : "status\n";
823 folders = cmd.status_full ? cmd.status_full_folders :
826 fd_write_all(uxsock, command, strlen(command));
827 for (i = 0; folders && i < folders->len; ++i) {
828 folder = g_ptr_array_index(folders, i);
829 fd_write_all(uxsock, folder, strlen(folder));
830 fd_write_all(uxsock, "\n", 1);
832 fd_write_all(uxsock, ".\n", 2);
834 fd_gets(uxsock, buf, sizeof(buf));
835 if (!strncmp(buf, ".\n", 2)) break;
839 fd_write_all(uxsock, "popup\n", 6);
845 static gint lock_socket_remove(void)
849 if (lock_socket < 0) return -1;
851 if (lock_socket_tag > 0)
852 gdk_input_remove(lock_socket_tag);
853 fd_close(lock_socket);
854 filename = get_socket_name();
860 static GPtrArray *get_folder_item_list(gint sock)
864 GPtrArray *folders = NULL;
867 fd_gets(sock, buf, sizeof(buf));
868 if (!strncmp(buf, ".\n", 2)) break;
870 if (!folders) folders = g_ptr_array_new();
871 item = folder_find_item_from_identifier(buf);
873 g_ptr_array_add(folders, item);
875 g_warning("no such folder: %s\n", buf);
881 static void lock_socket_input_cb(gpointer data,
883 GdkInputCondition condition)
885 MainWindow *mainwin = (MainWindow *)data;
889 sock = fd_accept(source);
890 fd_gets(sock, buf, sizeof(buf));
892 if (!strncmp(buf, "popup", 5)) {
893 main_window_popup(mainwin);
894 } else if (!strncmp(buf, "receive_all", 11)) {
895 inc_all_account_mail(mainwin, FALSE,
896 prefs_common.newmail_notify_manu);
897 } else if (!strncmp(buf, "receive", 7)) {
898 inc_mail(mainwin, prefs_common.newmail_notify_manu);
899 } else if (!strncmp(buf, "compose_attach", 14)) {
903 mailto = g_strdup(buf + strlen("compose_attach") + 1);
904 files = g_ptr_array_new();
905 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
906 if (buf[0] == '.' && buf[1] == '\n') break;
908 g_ptr_array_add(files, g_strdup(buf));
910 open_compose_new(mailto, files);
911 ptr_array_free_strings(files);
912 g_ptr_array_free(files, TRUE);
914 } else if (!strncmp(buf, "compose", 7)) {
915 open_compose_new(buf + strlen("compose") + 1, NULL);
916 } else if (!strncmp(buf, "send", 4)) {
918 } else if (!strncmp(buf, "online", 6)) {
919 main_window_toggle_work_offline(mainwin, FALSE);
920 } else if (!strncmp(buf, "offline", 7)) {
921 main_window_toggle_work_offline(mainwin, TRUE);
922 } else if (!strncmp(buf, "status-full", 11) ||
923 !strncmp(buf, "status", 6)) {
927 folders = get_folder_item_list(sock);
928 status = folder_get_status
929 (folders, !strncmp(buf, "status-full", 11));
930 fd_write_all(sock, status, strlen(status));
931 fd_write_all(sock, ".\n", 2);
933 if (folders) g_ptr_array_free(folders, TRUE);
939 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
944 Xstrdup_a(addr, address, return);
948 compose_new(NULL, addr, attach_files);
951 static void send_queue(void)
955 for (list = folder_get_list(); list != NULL; list = list->next) {
956 Folder *folder = list->data;
959 gint res = procmsg_send_queue
960 (folder->queue, prefs_common.savemsg);
963 alertpanel_error(_("Some errors occurred while sending queued messages."));
965 folder_item_scan(folder->queue);
970 static void quit_signal_handler(int sig)
972 debug_print("Quitting on signal %d\n", sig);
974 g_timeout_add(0, clean_quit, NULL);
977 static void install_basic_sighandlers()
980 struct sigaction act;
985 sigaddset(&mask, SIGTERM);
988 sigaddset(&mask, SIGINT);
991 sigaddset(&mask, SIGHUP);
994 act.sa_handler = quit_signal_handler;
999 sigaction(SIGTERM, &act, 0);
1002 sigaction(SIGINT, &act, 0);
1005 sigaction(SIGHUP, &act, 0);
1008 sigprocmask(SIG_UNBLOCK, &mask, 0);