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>
42 # include "passphrase.h"
48 #include "mainwindow.h"
49 #include "folderview.h"
50 #include "summaryview.h"
51 #include "prefs_common.h"
52 #include "prefs_account.h"
53 #include "prefs_actions.h"
54 #include "prefs_ext_prog.h"
55 #include "prefs_fonts.h"
56 #include "prefs_msg_colors.h"
57 #include "prefs_spelling.h"
58 #include "prefs_themes.h"
59 #include "prefs_wrapping.h"
60 #include "prefs_display_header.h"
65 #include "manage_window.h"
66 #include "alertpanel.h"
67 #include "statusbar.h"
68 #include "addressbook.h"
76 #include "prefs_toolbar.h"
96 static gint lock_socket = -1;
97 static gint lock_socket_tag = 0;
101 ONLINE_MODE_DONT_CHANGE,
106 static struct RemoteCmd {
108 gboolean receive_all;
110 const gchar *compose_mailto;
111 GPtrArray *attach_files;
113 gboolean status_full;
114 GPtrArray *status_folders;
115 GPtrArray *status_full_folders;
122 static void parse_cmd_opt(int argc, char *argv[]);
124 static gint prohibit_duplicate_launch (void);
125 static gchar * get_crashfile_name (void);
126 static gint lock_socket_remove (void);
127 static void lock_socket_input_cb (gpointer data,
129 GdkInputCondition condition);
133 gchar *get_socket_name (void);
136 static void open_compose_new (const gchar *address,
137 GPtrArray *attach_files);
139 static void send_queue (void);
140 static void initial_processing (FolderItem *item, gpointer data);
141 static void quit_signal_handler (int sig);
142 static void install_basic_sighandlers (void);
143 static void exit_sylpheed (MainWindow *mainwin);
147 _("File `%s' already exists.\n"
148 "Can't create folder.")
151 #define MAKE_DIR_IF_NOT_EXIST(dir) \
153 if (!is_dir_exist(dir)) { \
154 if (is_file_exist(dir)) { \
156 (_("File `%s' already exists.\n" \
157 "Can't create folder."), \
161 if (make_dir(dir) < 0) \
166 static MainWindow *static_mainwindow;
168 int main(int argc, char *argv[])
172 FolderView *folderview;
174 if (!sylpheed_init(&argc, &argv)) {
178 prog_version = PROG_VERSION;
180 argv0 = g_strdup(argv[0]);
183 parse_cmd_opt(argc, argv);
188 gtk_init(&argc, &argv);
189 crash_main(cmd.crash_params);
192 crash_install_handlers();
194 install_basic_sighandlers();
196 /* check and create unix domain socket */
197 lock_socket = prohibit_duplicate_launch();
198 if (lock_socket < 0) return 0;
200 if (cmd.status || cmd.status_full) {
201 puts("0 Sylpheed not running.");
202 lock_socket_remove();
207 gtk_init(&argc, &argv);
210 gtk_widget_set_default_colormap(gdk_rgb_get_cmap());
211 gtk_widget_set_default_visual(gdk_rgb_get_visual());
213 #if USE_THREADS || USE_LDAP
215 if (!g_thread_supported())
216 g_error(_("g_thread is not supported by glib.\n"));
219 /* parse gtkrc files */
220 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
222 gtk_rc_parse(userrc);
224 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
225 G_DIR_SEPARATOR_S, "gtkrc", NULL);
226 gtk_rc_parse(userrc);
228 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
229 gtk_rc_parse(userrc);
232 gtk_rc_parse("./gtkrc");
234 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
235 gtk_item_factory_parse_rc(userrc);
238 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
240 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
241 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
242 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
243 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
244 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
245 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
247 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
249 folder_system_init();
251 prefs_common_read_config();
255 prefs_ext_prog_init();
256 prefs_wrapping_init();
257 prefs_msg_colors_init();
259 gtkaspell_checkers_init();
260 prefs_spelling_init();
263 sock_set_io_timeout(prefs_common.io_timeout_secs);
265 prefs_actions_read_config();
266 prefs_display_header_read_config();
267 /* prefs_filtering_read_config(); */
268 addressbook_read_file();
269 renderer_read_config();
273 folderview_initialize();
277 mainwin = main_window_create
278 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
279 folderview = mainwin->folderview;
281 /* register the callback of unix domain socket input */
282 lock_socket_tag = gdk_input_add(lock_socket,
283 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
284 lock_socket_input_cb,
287 account_read_config_all();
289 if (folder_read_list() < 0) {
293 if (!account_get_list()) {
298 account_set_missing_folder();
299 folder_set_missing_folders();
300 folderview_set(folderview);
302 prefs_matcher_read_config();
304 /* make one all-folder processing before using sylpheed */
305 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
307 /* if Sylpheed crashed, rebuild caches */
308 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
309 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
310 folderview_check_new(NULL);
312 /* make the crash-indicator file */
313 str_write_to_file("foo", get_crashfile_name());
315 addressbook_read_file();
317 inc_autocheck_timer_init(mainwin);
319 /* ignore SIGPIPE signal for preventing sudden death of program */
320 signal(SIGPIPE, SIG_IGN);
322 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
323 main_window_toggle_work_offline(mainwin, TRUE);
324 if (cmd.online_mode == ONLINE_MODE_ONLINE)
325 main_window_toggle_work_offline(mainwin, FALSE);
328 inc_all_account_mail(mainwin, FALSE,
329 prefs_common.newmail_notify_manu);
330 else if (prefs_common.chk_on_startup)
331 inc_all_account_mail(mainwin, TRUE,
332 prefs_common.newmail_notify_manu);
333 else if (cmd.receive)
334 inc_mail(mainwin, prefs_common.newmail_notify_manu);
336 gtk_widget_grab_focus(folderview->ctree);
339 open_compose_new(cmd.compose_mailto, cmd.attach_files);
340 if (cmd.attach_files) {
341 ptr_array_free_strings(cmd.attach_files);
342 g_ptr_array_free(cmd.attach_files, TRUE);
343 cmd.attach_files = NULL;
347 if (cmd.status_folders) {
348 g_ptr_array_free(cmd.status_folders, TRUE);
349 cmd.status_folders = NULL;
351 if (cmd.status_full_folders) {
352 g_ptr_array_free(cmd.status_full_folders, TRUE);
353 cmd.status_full_folders = NULL;
356 prefs_toolbar_init();
358 plugin_load_all("GTK");
360 static_mainwindow = mainwin;
363 exit_sylpheed(mainwin);
368 static void save_all_caches(FolderItem *item, gpointer data)
372 folder_item_write_cache(item);
375 static void exit_sylpheed(MainWindow *mainwin)
379 debug_print("shutting down\n");
381 inc_autocheck_timer_remove();
383 if (prefs_common.clean_on_exit)
384 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
386 /* save prefs for opened folder */
387 if(mainwin->folderview->opened)
391 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
392 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
395 /* save all state before exiting */
397 folder_func_to_all_folders(save_all_caches, NULL);
399 main_window_get_size(mainwin);
400 main_window_get_position(mainwin);
401 prefs_common_write_config();
402 account_write_config_all();
403 addressbook_export_to_file();
405 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
406 gtk_item_factory_dump_rc(filename, NULL, TRUE);
409 /* delete temporary files */
410 remove_all_files(get_mime_tmp_dir());
414 /* delete crashfile */
416 unlink(get_crashfile_name());
418 lock_socket_remove();
420 main_window_destroy(mainwin);
422 plugin_unload_all("GTK");
424 prefs_toolbar_done();
426 addressbook_destroy();
430 prefs_ext_prog_done();
431 prefs_wrapping_done();
432 prefs_msg_colors_done();
434 prefs_spelling_done();
435 gtkaspell_checkers_quit();
441 static void parse_cmd_opt(int argc, char *argv[])
445 for (i = 1; i < argc; i++) {
446 if (!strncmp(argv[i], "--receive-all", 13))
447 cmd.receive_all = TRUE;
448 else if (!strncmp(argv[i], "--receive", 9))
450 else if (!strncmp(argv[i], "--compose", 9)) {
451 const gchar *p = argv[i + 1];
454 cmd.compose_mailto = NULL;
455 if (p && *p != '\0' && *p != '-') {
456 if (!strncmp(p, "mailto:", 7))
457 cmd.compose_mailto = p + 7;
459 cmd.compose_mailto = p;
462 } else if (!strncmp(argv[i], "--attach", 8)) {
463 const gchar *p = argv[i + 1];
466 while (p && *p != '\0' && *p != '-') {
467 if (!cmd.attach_files)
468 cmd.attach_files = g_ptr_array_new();
469 if (*p != G_DIR_SEPARATOR)
470 file = g_strconcat(sylpheed_get_startup_dir(),
475 g_ptr_array_add(cmd.attach_files, file);
479 } else if (!strncmp(argv[i], "--send", 6)) {
481 } else if (!strncmp(argv[i], "--version", 9)) {
482 puts("Sylpheed version " VERSION);
484 } else if (!strncmp(argv[i], "--status-full", 13)) {
485 const gchar *p = argv[i + 1];
487 cmd.status_full = TRUE;
488 while (p && *p != '\0' && *p != '-') {
489 if (!cmd.status_full_folders)
490 cmd.status_full_folders =
492 g_ptr_array_add(cmd.status_full_folders,
497 } else if (!strncmp(argv[i], "--status", 8)) {
498 const gchar *p = argv[i + 1];
501 while (p && *p != '\0' && *p != '-') {
502 if (!cmd.status_folders)
503 cmd.status_folders = g_ptr_array_new();
504 g_ptr_array_add(cmd.status_folders,
509 } else if (!strncmp(argv[i], "--online", 8)) {
510 cmd.online_mode = ONLINE_MODE_ONLINE;
511 } else if (!strncmp(argv[i], "--offline", 9)) {
512 cmd.online_mode = ONLINE_MODE_OFFLINE;
513 } else if (!strncmp(argv[i], "--help", 6)) {
514 g_print(_("Usage: %s [OPTION]...\n"),
515 g_basename(argv[0]));
517 puts(_(" --compose [address] open composition window"));
518 puts(_(" --attach file1 [file2]...\n"
519 " open composition window with specified files\n"
521 puts(_(" --receive receive new messages"));
522 puts(_(" --receive-all receive new messages of all accounts"));
523 puts(_(" --send send all queued messages"));
524 puts(_(" --status [folder]... show the total number of messages"));
525 puts(_(" --status-full [folder]...\n"
526 " show the status of each folder"));
527 puts(_(" --online switch to online mode"));
528 puts(_(" --offline switch to offline mode"));
529 puts(_(" --debug debug mode"));
530 puts(_(" --help display this help and exit"));
531 puts(_(" --version output version information and exit"));
532 puts(_(" --config-dir output configuration directory"));
535 } else if (!strncmp(argv[i], "--crash", 7)) {
537 cmd.crash_params = g_strdup(argv[i + 1]);
539 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
546 if (cmd.attach_files && cmd.compose == FALSE) {
548 cmd.compose_mailto = NULL;
552 static gint get_queued_message_num(void)
556 queue = folder_get_default_queue();
557 if (!queue) return -1;
559 folder_item_scan(queue);
560 return queue->total_msgs;
563 static void initial_processing(FolderItem *item, gpointer data)
565 MainWindow *mainwin = (MainWindow *)data;
568 g_return_if_fail(item);
569 buf = g_strdup_printf(_("Processing (%s)..."),
572 : _("top level folder"));
573 debug_print("%s\n", buf);
576 main_window_cursor_wait(mainwin);
578 if (item->prefs->enable_processing)
579 folder_item_apply_processing(item);
581 debug_print("done.\n");
582 STATUSBAR_POP(mainwin);
583 main_window_cursor_normal(mainwin);
586 static void draft_all_messages(void)
588 GList *compose_list = compose_get_compose_list();
592 for (elem = compose_list; elem != NULL && elem->data != NULL;
594 Compose *c = (Compose*)elem->data;
600 gboolean clean_quit(gpointer data)
602 static gboolean firstrun = TRUE;
608 /*!< Good idea to have the main window stored in a
609 * static variable so we can check that variable
610 * to see if we're really allowed to do things
611 * that actually the spawner is supposed to
612 * do (like: sending mail, composing messages).
613 * Because, really, if we're the spawnee, and
614 * we touch GTK stuff, we're hosed. See the
617 /* FIXME: Use something else to signal that we're
618 * in the original spawner, and not in a spawned
620 if (!static_mainwindow)
623 draft_all_messages();
625 exit_sylpheed(static_mainwindow);
631 void app_will_exit(GtkWidget *widget, gpointer data)
633 MainWindow *mainwin = data;
635 if (compose_get_compose_list()) {
636 gint val = alertpanel(_("Really quit?"),
637 _("Composing message exists."),
638 _("Draft them"), _("Discard them"), _("Don't quit"));
642 case G_ALERTALTERNATE:
645 draft_all_messages();
648 manage_window_focus_in(mainwin->window, NULL, NULL);
651 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
652 if (alertpanel(_("Queued messages"),
653 _("Some unsent messages are queued. Exit now?"),
654 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
656 manage_window_focus_in(mainwin->window, NULL, NULL);
662 * CLAWS: want this public so crash dialog can delete the
668 gchar *get_socket_name(void)
670 static gchar *filename = NULL;
672 if (filename == NULL) {
673 filename = g_strdup_printf("%s%csylpheed-%d",
674 g_get_tmp_dir(), G_DIR_SEPARATOR,
681 static gchar *get_crashfile_name(void)
683 static gchar *filename = NULL;
685 if (filename == NULL) {
686 filename = g_strdup_printf("%s%csylpheed-crashed",
687 get_tmp_dir(), G_DIR_SEPARATOR);
693 static gint prohibit_duplicate_launch(void)
698 path = get_socket_name();
699 uxsock = fd_connect_unix(path);
702 return fd_open_unix(path);
705 /* remote command mode */
707 debug_print("another Sylpheed is already running.\n");
710 fd_write_all(uxsock, "receive_all\n", 12);
711 else if (cmd.receive)
712 fd_write_all(uxsock, "receive\n", 8);
713 else if (cmd.compose && cmd.attach_files) {
714 gchar *str, *compose_str;
717 if (cmd.compose_mailto)
718 compose_str = g_strdup_printf("compose_attach %s\n",
721 compose_str = g_strdup("compose_attach\n");
723 fd_write_all(uxsock, compose_str, strlen(compose_str));
726 for (i = 0; i < cmd.attach_files->len; i++) {
727 str = g_ptr_array_index(cmd.attach_files, i);
728 fd_write_all(uxsock, str, strlen(str));
729 fd_write_all(uxsock, "\n", 1);
732 fd_write_all(uxsock, ".\n", 2);
733 } else if (cmd.compose) {
736 if (cmd.compose_mailto)
737 compose_str = g_strdup_printf
738 ("compose %s\n", cmd.compose_mailto);
740 compose_str = g_strdup("compose\n");
742 fd_write_all(uxsock, compose_str, strlen(compose_str));
744 } else if (cmd.send) {
745 fd_write_all(uxsock, "send\n", 5);
746 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
747 fd_write(uxsock, "online\n", 6);
748 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
749 fd_write(uxsock, "offline\n", 7);
750 } else if (cmd.status || cmd.status_full) {
753 const gchar *command;
757 command = cmd.status_full ? "status-full\n" : "status\n";
758 folders = cmd.status_full ? cmd.status_full_folders :
761 fd_write_all(uxsock, command, strlen(command));
762 for (i = 0; folders && i < folders->len; ++i) {
763 folder = g_ptr_array_index(folders, i);
764 fd_write_all(uxsock, folder, strlen(folder));
765 fd_write_all(uxsock, "\n", 1);
767 fd_write_all(uxsock, ".\n", 2);
769 fd_gets(uxsock, buf, sizeof(buf));
770 if (!strncmp(buf, ".\n", 2)) break;
774 fd_write_all(uxsock, "popup\n", 6);
780 static gint lock_socket_remove(void)
784 if (lock_socket < 0) return -1;
786 if (lock_socket_tag > 0)
787 gdk_input_remove(lock_socket_tag);
788 fd_close(lock_socket);
789 filename = get_socket_name();
795 static GPtrArray *get_folder_item_list(gint sock)
799 GPtrArray *folders = NULL;
802 fd_gets(sock, buf, sizeof(buf));
803 if (!strncmp(buf, ".\n", 2)) break;
805 if (!folders) folders = g_ptr_array_new();
806 item = folder_find_item_from_identifier(buf);
808 g_ptr_array_add(folders, item);
810 g_warning("no such folder: %s\n", buf);
816 static void lock_socket_input_cb(gpointer data,
818 GdkInputCondition condition)
820 MainWindow *mainwin = (MainWindow *)data;
824 sock = fd_accept(source);
825 fd_gets(sock, buf, sizeof(buf));
827 if (!strncmp(buf, "popup", 5)) {
828 main_window_popup(mainwin);
829 } else if (!strncmp(buf, "receive_all", 11)) {
830 inc_all_account_mail(mainwin, FALSE,
831 prefs_common.newmail_notify_manu);
832 } else if (!strncmp(buf, "receive", 7)) {
833 inc_mail(mainwin, prefs_common.newmail_notify_manu);
834 } else if (!strncmp(buf, "compose_attach", 14)) {
838 mailto = g_strdup(buf + strlen("compose_attach") + 1);
839 files = g_ptr_array_new();
840 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
841 if (buf[0] == '.' && buf[1] == '\n') break;
843 g_ptr_array_add(files, g_strdup(buf));
845 open_compose_new(mailto, files);
846 ptr_array_free_strings(files);
847 g_ptr_array_free(files, TRUE);
849 } else if (!strncmp(buf, "compose", 7)) {
850 open_compose_new(buf + strlen("compose") + 1, NULL);
851 } else if (!strncmp(buf, "send", 4)) {
853 } else if (!strncmp(buf, "online", 6)) {
854 main_window_toggle_work_offline(mainwin, FALSE);
855 } else if (!strncmp(buf, "offline", 7)) {
856 main_window_toggle_work_offline(mainwin, TRUE);
857 } else if (!strncmp(buf, "status-full", 11) ||
858 !strncmp(buf, "status", 6)) {
862 folders = get_folder_item_list(sock);
863 status = folder_get_status
864 (folders, !strncmp(buf, "status-full", 11));
865 fd_write_all(sock, status, strlen(status));
866 fd_write_all(sock, ".\n", 2);
868 if (folders) g_ptr_array_free(folders, TRUE);
874 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
879 Xstrdup_a(addr, address, return);
883 compose_new(NULL, addr, attach_files);
886 static void send_queue(void)
890 for (list = folder_get_list(); list != NULL; list = list->next) {
891 Folder *folder = list->data;
894 gint res = procmsg_send_queue
895 (folder->queue, prefs_common.savemsg);
898 alertpanel_error(_("Some errors occurred while sending queued messages."));
900 folder_item_scan(folder->queue);
905 static void quit_signal_handler(int sig)
907 debug_print("Quitting on signal %d\n", sig);
909 g_timeout_add(0, clean_quit, NULL);
912 static void install_basic_sighandlers()
915 struct sigaction act;
920 sigaddset(&mask, SIGTERM);
923 sigaddset(&mask, SIGINT);
926 sigaddset(&mask, SIGHUP);
929 act.sa_handler = quit_signal_handler;
934 sigaction(SIGTERM, &act, 0);
937 sigaction(SIGINT, &act, 0);
940 sigaction(SIGHUP, &act, 0);
943 sigprocmask(SIG_UNBLOCK, &mask, 0);