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 account_read_config_all();
348 if (folder_read_list() < 0) {
352 if (!account_get_list()) {
357 account_set_missing_folder();
358 folder_set_missing_folders();
359 folderview_set(folderview);
361 prefs_matcher_read_config();
363 /* make one all-folder processing before using sylpheed */
364 main_window_cursor_wait(mainwin);
365 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
366 main_window_cursor_normal(mainwin);
368 /* if Sylpheed crashed, rebuild caches */
369 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
370 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
371 folderview_check_new(NULL);
373 /* make the crash-indicator file */
374 str_write_to_file("foo", get_crashfile_name());
376 addressbook_read_file();
378 inc_autocheck_timer_init(mainwin);
380 /* ignore SIGPIPE signal for preventing sudden death of program */
381 signal(SIGPIPE, SIG_IGN);
383 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
384 main_window_toggle_work_offline(mainwin, TRUE);
385 if (cmd.online_mode == ONLINE_MODE_ONLINE)
386 main_window_toggle_work_offline(mainwin, FALSE);
389 inc_all_account_mail(mainwin, FALSE,
390 prefs_common.newmail_notify_manu);
391 else if (prefs_common.chk_on_startup)
392 inc_all_account_mail(mainwin, TRUE,
393 prefs_common.newmail_notify_manu);
394 else if (cmd.receive)
395 inc_mail(mainwin, prefs_common.newmail_notify_manu);
397 gtk_widget_grab_focus(folderview->ctree);
400 open_compose_new(cmd.compose_mailto, cmd.attach_files);
401 if (cmd.attach_files) {
402 ptr_array_free_strings(cmd.attach_files);
403 g_ptr_array_free(cmd.attach_files, TRUE);
404 cmd.attach_files = NULL;
408 if (cmd.status_folders) {
409 g_ptr_array_free(cmd.status_folders, TRUE);
410 cmd.status_folders = NULL;
412 if (cmd.status_full_folders) {
413 g_ptr_array_free(cmd.status_full_folders, TRUE);
414 cmd.status_full_folders = NULL;
417 sylpheed_register_idle_function(sylpheed_gtk_idle);
419 prefs_toolbar_init();
421 plugin_load_all("GTK");
423 static_mainwindow = mainwin;
425 #ifdef HAVE_STARTUP_NOTIFICATION
426 startup_notification_complete(FALSE);
430 exit_sylpheed(mainwin);
435 static void save_all_caches(FolderItem *item, gpointer data)
439 folder_item_write_cache(item);
442 static void exit_sylpheed(MainWindow *mainwin)
446 debug_print("shutting down\n");
448 inc_autocheck_timer_remove();
450 if (prefs_common.clean_on_exit)
451 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
453 /* save prefs for opened folder */
454 if(mainwin->folderview->opened)
458 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
459 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
462 /* save all state before exiting */
464 folder_func_to_all_folders(save_all_caches, NULL);
466 main_window_get_size(mainwin);
467 main_window_get_position(mainwin);
468 prefs_common_write_config();
469 account_write_config_all();
470 addressbook_export_to_file();
472 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
473 gtk_item_factory_dump_rc(filename, NULL, TRUE);
476 /* delete temporary files */
477 remove_all_files(get_mime_tmp_dir());
481 /* delete crashfile */
483 unlink(get_crashfile_name());
485 lock_socket_remove();
487 main_window_destroy(mainwin);
489 plugin_unload_all("GTK");
491 prefs_toolbar_done();
493 addressbook_destroy();
497 prefs_ext_prog_done();
498 prefs_wrapping_done();
499 prefs_msg_colors_done();
501 prefs_spelling_done();
502 gtkaspell_checkers_quit();
508 static void parse_cmd_opt(int argc, char *argv[])
512 for (i = 1; i < argc; i++) {
513 if (!strncmp(argv[i], "--receive-all", 13))
514 cmd.receive_all = TRUE;
515 else if (!strncmp(argv[i], "--receive", 9))
517 else if (!strncmp(argv[i], "--compose", 9)) {
518 const gchar *p = argv[i + 1];
521 cmd.compose_mailto = NULL;
522 if (p && *p != '\0' && *p != '-') {
523 if (!strncmp(p, "mailto:", 7))
524 cmd.compose_mailto = p + 7;
526 cmd.compose_mailto = p;
529 } else if (!strncmp(argv[i], "--attach", 8)) {
530 const gchar *p = argv[i + 1];
533 while (p && *p != '\0' && *p != '-') {
534 if (!cmd.attach_files)
535 cmd.attach_files = g_ptr_array_new();
536 if (*p != G_DIR_SEPARATOR)
537 file = g_strconcat(sylpheed_get_startup_dir(),
542 g_ptr_array_add(cmd.attach_files, file);
546 } else if (!strncmp(argv[i], "--send", 6)) {
548 } else if (!strncmp(argv[i], "--version", 9)) {
549 puts("Sylpheed version " VERSION);
551 } else if (!strncmp(argv[i], "--status-full", 13)) {
552 const gchar *p = argv[i + 1];
554 cmd.status_full = TRUE;
555 while (p && *p != '\0' && *p != '-') {
556 if (!cmd.status_full_folders)
557 cmd.status_full_folders =
559 g_ptr_array_add(cmd.status_full_folders,
564 } else if (!strncmp(argv[i], "--status", 8)) {
565 const gchar *p = argv[i + 1];
568 while (p && *p != '\0' && *p != '-') {
569 if (!cmd.status_folders)
570 cmd.status_folders = g_ptr_array_new();
571 g_ptr_array_add(cmd.status_folders,
576 } else if (!strncmp(argv[i], "--online", 8)) {
577 cmd.online_mode = ONLINE_MODE_ONLINE;
578 } else if (!strncmp(argv[i], "--offline", 9)) {
579 cmd.online_mode = ONLINE_MODE_OFFLINE;
580 } else if (!strncmp(argv[i], "--help", 6)) {
581 g_print(_("Usage: %s [OPTION]...\n"),
582 g_basename(argv[0]));
584 puts(_(" --compose [address] open composition window"));
585 puts(_(" --attach file1 [file2]...\n"
586 " open composition window with specified files\n"
588 puts(_(" --receive receive new messages"));
589 puts(_(" --receive-all receive new messages of all accounts"));
590 puts(_(" --send send all queued messages"));
591 puts(_(" --status [folder]... show the total number of messages"));
592 puts(_(" --status-full [folder]...\n"
593 " show the status of each folder"));
594 puts(_(" --online switch to online mode"));
595 puts(_(" --offline switch to offline mode"));
596 puts(_(" --debug debug mode"));
597 puts(_(" --help display this help and exit"));
598 puts(_(" --version output version information and exit"));
599 puts(_(" --config-dir output configuration directory"));
602 } else if (!strncmp(argv[i], "--crash", 7)) {
604 cmd.crash_params = g_strdup(argv[i + 1]);
606 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
613 if (cmd.attach_files && cmd.compose == FALSE) {
615 cmd.compose_mailto = NULL;
619 static gint get_queued_message_num(void)
623 queue = folder_get_default_queue();
624 if (!queue) return -1;
626 folder_item_scan(queue);
627 return queue->total_msgs;
630 static void initial_processing(FolderItem *item, gpointer data)
632 MainWindow *mainwin = (MainWindow *)data;
635 g_return_if_fail(item);
636 buf = g_strdup_printf(_("Processing (%s)..."),
639 : _("top level folder"));
640 debug_print("%s\n", buf);
643 if (item->prefs->enable_processing)
644 folder_item_apply_processing(item);
646 debug_print("done.\n");
647 STATUSBAR_POP(mainwin);
650 static void draft_all_messages(void)
652 GList *compose_list = compose_get_compose_list();
656 for (elem = compose_list; elem != NULL && elem->data != NULL;
658 Compose *c = (Compose*)elem->data;
664 gboolean clean_quit(gpointer data)
666 static gboolean firstrun = TRUE;
672 /*!< Good idea to have the main window stored in a
673 * static variable so we can check that variable
674 * to see if we're really allowed to do things
675 * that actually the spawner is supposed to
676 * do (like: sending mail, composing messages).
677 * Because, really, if we're the spawnee, and
678 * we touch GTK stuff, we're hosed. See the
681 /* FIXME: Use something else to signal that we're
682 * in the original spawner, and not in a spawned
684 if (!static_mainwindow)
687 draft_all_messages();
689 exit_sylpheed(static_mainwindow);
695 void app_will_exit(GtkWidget *widget, gpointer data)
697 MainWindow *mainwin = data;
699 if (compose_get_compose_list()) {
700 gint val = alertpanel(_("Really quit?"),
701 _("Composing message exists."),
702 _("Draft them"), _("Discard them"), _("Don't quit"));
706 case G_ALERTALTERNATE:
709 draft_all_messages();
712 manage_window_focus_in(mainwin->window, NULL, NULL);
715 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
716 if (alertpanel(_("Queued messages"),
717 _("Some unsent messages are queued. Exit now?"),
718 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
720 manage_window_focus_in(mainwin->window, NULL, NULL);
726 * CLAWS: want this public so crash dialog can delete the
732 gchar *get_socket_name(void)
734 static gchar *filename = NULL;
736 if (filename == NULL) {
737 filename = g_strdup_printf("%s%csylpheed-%d",
738 g_get_tmp_dir(), G_DIR_SEPARATOR,
745 static gchar *get_crashfile_name(void)
747 static gchar *filename = NULL;
749 if (filename == NULL) {
750 filename = g_strdup_printf("%s%csylpheed-crashed",
751 get_tmp_dir(), G_DIR_SEPARATOR);
757 static gint prohibit_duplicate_launch(void)
762 path = get_socket_name();
763 uxsock = fd_connect_unix(path);
766 return fd_open_unix(path);
769 /* remote command mode */
771 debug_print("another Sylpheed is already running.\n");
774 fd_write_all(uxsock, "receive_all\n", 12);
775 else if (cmd.receive)
776 fd_write_all(uxsock, "receive\n", 8);
777 else if (cmd.compose && cmd.attach_files) {
778 gchar *str, *compose_str;
781 if (cmd.compose_mailto)
782 compose_str = g_strdup_printf("compose_attach %s\n",
785 compose_str = g_strdup("compose_attach\n");
787 fd_write_all(uxsock, compose_str, strlen(compose_str));
790 for (i = 0; i < cmd.attach_files->len; i++) {
791 str = g_ptr_array_index(cmd.attach_files, i);
792 fd_write_all(uxsock, str, strlen(str));
793 fd_write_all(uxsock, "\n", 1);
796 fd_write_all(uxsock, ".\n", 2);
797 } else if (cmd.compose) {
800 if (cmd.compose_mailto)
801 compose_str = g_strdup_printf
802 ("compose %s\n", cmd.compose_mailto);
804 compose_str = g_strdup("compose\n");
806 fd_write_all(uxsock, compose_str, strlen(compose_str));
808 } else if (cmd.send) {
809 fd_write_all(uxsock, "send\n", 5);
810 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
811 fd_write(uxsock, "online\n", 6);
812 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
813 fd_write(uxsock, "offline\n", 7);
814 } else if (cmd.status || cmd.status_full) {
817 const gchar *command;
821 command = cmd.status_full ? "status-full\n" : "status\n";
822 folders = cmd.status_full ? cmd.status_full_folders :
825 fd_write_all(uxsock, command, strlen(command));
826 for (i = 0; folders && i < folders->len; ++i) {
827 folder = g_ptr_array_index(folders, i);
828 fd_write_all(uxsock, folder, strlen(folder));
829 fd_write_all(uxsock, "\n", 1);
831 fd_write_all(uxsock, ".\n", 2);
833 fd_gets(uxsock, buf, sizeof(buf));
834 if (!strncmp(buf, ".\n", 2)) break;
838 fd_write_all(uxsock, "popup\n", 6);
844 static gint lock_socket_remove(void)
848 if (lock_socket < 0) return -1;
850 if (lock_socket_tag > 0)
851 gdk_input_remove(lock_socket_tag);
852 fd_close(lock_socket);
853 filename = get_socket_name();
859 static GPtrArray *get_folder_item_list(gint sock)
863 GPtrArray *folders = NULL;
866 fd_gets(sock, buf, sizeof(buf));
867 if (!strncmp(buf, ".\n", 2)) break;
869 if (!folders) folders = g_ptr_array_new();
870 item = folder_find_item_from_identifier(buf);
872 g_ptr_array_add(folders, item);
874 g_warning("no such folder: %s\n", buf);
880 static void lock_socket_input_cb(gpointer data,
882 GdkInputCondition condition)
884 MainWindow *mainwin = (MainWindow *)data;
888 sock = fd_accept(source);
889 fd_gets(sock, buf, sizeof(buf));
891 if (!strncmp(buf, "popup", 5)) {
892 main_window_popup(mainwin);
893 } else if (!strncmp(buf, "receive_all", 11)) {
894 inc_all_account_mail(mainwin, FALSE,
895 prefs_common.newmail_notify_manu);
896 } else if (!strncmp(buf, "receive", 7)) {
897 inc_mail(mainwin, prefs_common.newmail_notify_manu);
898 } else if (!strncmp(buf, "compose_attach", 14)) {
902 mailto = g_strdup(buf + strlen("compose_attach") + 1);
903 files = g_ptr_array_new();
904 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
905 if (buf[0] == '.' && buf[1] == '\n') break;
907 g_ptr_array_add(files, g_strdup(buf));
909 open_compose_new(mailto, files);
910 ptr_array_free_strings(files);
911 g_ptr_array_free(files, TRUE);
913 } else if (!strncmp(buf, "compose", 7)) {
914 open_compose_new(buf + strlen("compose") + 1, NULL);
915 } else if (!strncmp(buf, "send", 4)) {
917 } else if (!strncmp(buf, "online", 6)) {
918 main_window_toggle_work_offline(mainwin, FALSE);
919 } else if (!strncmp(buf, "offline", 7)) {
920 main_window_toggle_work_offline(mainwin, TRUE);
921 } else if (!strncmp(buf, "status-full", 11) ||
922 !strncmp(buf, "status", 6)) {
926 folders = get_folder_item_list(sock);
927 status = folder_get_status
928 (folders, !strncmp(buf, "status-full", 11));
929 fd_write_all(sock, status, strlen(status));
930 fd_write_all(sock, ".\n", 2);
932 if (folders) g_ptr_array_free(folders, TRUE);
938 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
943 Xstrdup_a(addr, address, return);
947 compose_new(NULL, addr, attach_files);
950 static void send_queue(void)
954 for (list = folder_get_list(); list != NULL; list = list->next) {
955 Folder *folder = list->data;
958 gint res = procmsg_send_queue
959 (folder->queue, prefs_common.savemsg);
962 alertpanel_error(_("Some errors occurred while sending queued messages."));
964 folder_item_scan(folder->queue);
969 static void quit_signal_handler(int sig)
971 debug_print("Quitting on signal %d\n", sig);
973 g_timeout_add(0, clean_quit, NULL);
976 static void install_basic_sighandlers()
979 struct sigaction act;
984 sigaddset(&mask, SIGTERM);
987 sigaddset(&mask, SIGINT);
990 sigaddset(&mask, SIGHUP);
993 act.sa_handler = quit_signal_handler;
998 sigaction(SIGTERM, &act, 0);
1001 sigaction(SIGINT, &act, 0);
1004 sigaction(SIGHUP, &act, 0);
1007 sigprocmask(SIG_UNBLOCK, &mask, 0);