2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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_spelling.h"
57 #include "prefs_themes.h"
58 #include "prefs_display_header.h"
63 #include "manage_window.h"
64 #include "alertpanel.h"
65 #include "statusbar.h"
66 #include "addressbook.h"
74 #include "prefs_toolbar.h"
98 static gint lock_socket = -1;
99 static gint lock_socket_tag = 0;
103 ONLINE_MODE_DONT_CHANGE,
108 static struct RemoteCmd {
110 gboolean receive_all;
112 const gchar *compose_mailto;
113 GPtrArray *attach_files;
115 gboolean status_full;
116 GPtrArray *status_folders;
117 GPtrArray *status_full_folders;
124 static void parse_cmd_opt(int argc, char *argv[]);
126 static gint prohibit_duplicate_launch (void);
127 static gchar * get_crashfile_name (void);
128 static gint lock_socket_remove (void);
129 static void lock_socket_input_cb (gpointer data,
131 GdkInputCondition condition);
135 gchar *get_socket_name (void);
138 static void open_compose_new (const gchar *address,
139 GPtrArray *attach_files);
141 static void send_queue (void);
142 static void initial_processing (FolderItem *item, gpointer data);
143 static void quit_signal_handler (int sig);
144 static void install_basic_sighandlers (void);
145 static void exit_sylpheed (MainWindow *mainwin);
149 _("File `%s' already exists.\n"
150 "Can't create folder.")
153 #define MAKE_DIR_IF_NOT_EXIST(dir) \
155 if (!is_dir_exist(dir)) { \
156 if (is_file_exist(dir)) { \
158 (_("File `%s' already exists.\n" \
159 "Can't create folder."), \
163 if (make_dir(dir) < 0) \
168 static MainWindow *static_mainwindow;
170 int main(int argc, char *argv[])
174 FolderView *folderview;
176 if (!sylpheed_init(&argc, &argv)) {
180 prog_version = PROG_VERSION;
182 argv0 = g_strdup(argv[0]);
185 parse_cmd_opt(argc, argv);
190 gtk_init(&argc, &argv);
191 crash_main(cmd.crash_params);
194 crash_install_handlers();
196 install_basic_sighandlers();
198 /* check and create unix domain socket */
199 lock_socket = prohibit_duplicate_launch();
200 if (lock_socket < 0) return 0;
202 if (cmd.status || cmd.status_full) {
203 puts("0 Sylpheed not running.");
204 lock_socket_remove();
209 gtk_init(&argc, &argv);
212 gtk_widget_set_default_colormap(gdk_rgb_get_cmap());
213 gtk_widget_set_default_visual(gdk_rgb_get_visual());
215 #if USE_THREADS || USE_LDAP
217 if (!g_thread_supported())
218 g_error(_("g_thread is not supported by glib.\n"));
221 /* parse gtkrc files */
222 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
224 gtk_rc_parse(userrc);
226 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
227 G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
228 gtk_rc_parse(userrc);
230 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL);
231 gtk_rc_parse(userrc);
234 gtk_rc_parse("./gtkrc-2.0");
236 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
237 gtk_accel_map_load (userrc);
240 gtk_settings_set_long_property(gtk_settings_get_default(),
241 "gtk-can-change-accels",
242 (glong)TRUE, "XProperty");
244 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
246 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
247 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
248 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
249 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
250 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
251 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
253 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
255 folder_system_init();
257 prefs_common_read_config();
265 prefs_ext_prog_init();
267 gtkaspell_checkers_init();
268 prefs_spelling_init();
271 sock_set_io_timeout(prefs_common.io_timeout_secs);
273 prefs_actions_read_config();
274 prefs_display_header_read_config();
275 /* prefs_filtering_read_config(); */
276 addressbook_read_file();
277 renderer_read_config();
281 folderview_initialize();
285 mainwin = main_window_create
286 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
287 folderview = mainwin->folderview;
289 /* register the callback of unix domain socket input */
290 lock_socket_tag = gdk_input_add(lock_socket,
291 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
292 lock_socket_input_cb,
295 account_read_config_all();
297 if (folder_read_list() < 0) {
301 if (!account_get_list()) {
306 account_set_missing_folder();
307 folder_set_missing_folders();
308 folderview_set(folderview);
310 prefs_matcher_read_config();
312 /* make one all-folder processing before using sylpheed */
313 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
315 /* if Sylpheed crashed, rebuild caches */
316 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
317 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
318 folderview_check_new(NULL);
320 /* make the crash-indicator file */
321 str_write_to_file("foo", get_crashfile_name());
323 addressbook_read_file();
325 inc_autocheck_timer_init(mainwin);
327 /* ignore SIGPIPE signal for preventing sudden death of program */
328 signal(SIGPIPE, SIG_IGN);
330 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
331 main_window_toggle_work_offline(mainwin, TRUE);
332 if (cmd.online_mode == ONLINE_MODE_ONLINE)
333 main_window_toggle_work_offline(mainwin, FALSE);
336 inc_all_account_mail(mainwin, FALSE,
337 prefs_common.newmail_notify_manu);
338 else if (prefs_common.chk_on_startup)
339 inc_all_account_mail(mainwin, TRUE,
340 prefs_common.newmail_notify_manu);
341 else if (cmd.receive)
342 inc_mail(mainwin, prefs_common.newmail_notify_manu);
344 gtk_widget_grab_focus(folderview->ctree);
347 open_compose_new(cmd.compose_mailto, cmd.attach_files);
348 if (cmd.attach_files) {
349 ptr_array_free_strings(cmd.attach_files);
350 g_ptr_array_free(cmd.attach_files, TRUE);
351 cmd.attach_files = NULL;
355 if (cmd.status_folders) {
356 g_ptr_array_free(cmd.status_folders, TRUE);
357 cmd.status_folders = NULL;
359 if (cmd.status_full_folders) {
360 g_ptr_array_free(cmd.status_full_folders, TRUE);
361 cmd.status_full_folders = NULL;
364 prefs_toolbar_init();
366 plugin_load_all("GTK");
368 static_mainwindow = mainwin;
371 exit_sylpheed(mainwin);
376 static void save_all_caches(FolderItem *item, gpointer data)
380 folder_item_write_cache(item);
383 static void exit_sylpheed(MainWindow *mainwin)
387 debug_print("shutting down\n");
389 inc_autocheck_timer_remove();
391 if (prefs_common.clean_on_exit)
392 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
394 /* save prefs for opened folder */
395 if(mainwin->folderview->opened)
399 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
400 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
403 /* save all state before exiting */
405 folder_func_to_all_folders(save_all_caches, NULL);
407 main_window_get_size(mainwin);
408 main_window_get_position(mainwin);
409 prefs_common_save_config();
410 account_save_config_all();
411 addressbook_export_to_file();
413 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
414 gtk_accel_map_save(filename);
417 /* delete temporary files */
418 remove_all_files(get_mime_tmp_dir());
422 /* delete crashfile */
424 unlink(get_crashfile_name());
426 lock_socket_remove();
428 main_window_destroy(mainwin);
430 plugin_unload_all("GTK");
432 prefs_toolbar_done();
434 addressbook_destroy();
442 prefs_ext_prog_done();
444 prefs_spelling_done();
445 gtkaspell_checkers_quit();
451 static void parse_cmd_opt(int argc, char *argv[])
455 for (i = 1; i < argc; i++) {
456 if (!strncmp(argv[i], "--receive-all", 13))
457 cmd.receive_all = TRUE;
458 else if (!strncmp(argv[i], "--receive", 9))
460 else if (!strncmp(argv[i], "--compose", 9)) {
461 const gchar *p = argv[i + 1];
464 cmd.compose_mailto = NULL;
465 if (p && *p != '\0' && *p != '-') {
466 if (!strncmp(p, "mailto:", 7))
467 cmd.compose_mailto = p + 7;
469 cmd.compose_mailto = p;
472 } else if (!strncmp(argv[i], "--attach", 8)) {
473 const gchar *p = argv[i + 1];
476 while (p && *p != '\0' && *p != '-') {
477 if (!cmd.attach_files)
478 cmd.attach_files = g_ptr_array_new();
479 if (*p != G_DIR_SEPARATOR)
480 file = g_strconcat(sylpheed_get_startup_dir(),
485 g_ptr_array_add(cmd.attach_files, file);
489 } else if (!strncmp(argv[i], "--send", 6)) {
491 } else if (!strncmp(argv[i], "--version", 9)) {
492 puts("Sylpheed version " VERSION);
494 } else if (!strncmp(argv[i], "--status-full", 13)) {
495 const gchar *p = argv[i + 1];
497 cmd.status_full = TRUE;
498 while (p && *p != '\0' && *p != '-') {
499 if (!cmd.status_full_folders)
500 cmd.status_full_folders =
502 g_ptr_array_add(cmd.status_full_folders,
507 } else if (!strncmp(argv[i], "--status", 8)) {
508 const gchar *p = argv[i + 1];
511 while (p && *p != '\0' && *p != '-') {
512 if (!cmd.status_folders)
513 cmd.status_folders = g_ptr_array_new();
514 g_ptr_array_add(cmd.status_folders,
519 } else if (!strncmp(argv[i], "--online", 8)) {
520 cmd.online_mode = ONLINE_MODE_ONLINE;
521 } else if (!strncmp(argv[i], "--offline", 9)) {
522 cmd.online_mode = ONLINE_MODE_OFFLINE;
523 } else if (!strncmp(argv[i], "--help", 6)) {
524 g_print(_("Usage: %s [OPTION]...\n"),
525 g_basename(argv[0]));
527 puts(_(" --compose [address] open composition window"));
528 puts(_(" --attach file1 [file2]...\n"
529 " open composition window with specified files\n"
531 puts(_(" --receive receive new messages"));
532 puts(_(" --receive-all receive new messages of all accounts"));
533 puts(_(" --send send all queued messages"));
534 puts(_(" --status [folder]... show the total number of messages"));
535 puts(_(" --status-full [folder]...\n"
536 " show the status of each folder"));
537 puts(_(" --online switch to online mode"));
538 puts(_(" --offline switch to offline mode"));
539 puts(_(" --debug debug mode"));
540 puts(_(" --help display this help and exit"));
541 puts(_(" --version output version information and exit"));
542 puts(_(" --config-dir output configuration directory"));
545 } else if (!strncmp(argv[i], "--crash", 7)) {
547 cmd.crash_params = g_strdup(argv[i + 1]);
549 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
556 if (cmd.attach_files && cmd.compose == FALSE) {
558 cmd.compose_mailto = NULL;
562 static gint get_queued_message_num(void)
566 queue = folder_get_default_queue();
567 if (!queue) return -1;
569 folder_item_scan(queue);
570 return queue->total_msgs;
573 static void initial_processing(FolderItem *item, gpointer data)
575 MainWindow *mainwin = (MainWindow *)data;
578 g_return_if_fail(item);
579 buf = g_strdup_printf(_("Processing (%s)..."),
582 : _("top level folder"));
583 debug_print("%s\n", buf);
586 main_window_cursor_wait(mainwin);
588 if (item->prefs->enable_processing)
589 folder_item_apply_processing(item);
591 debug_print("done.\n");
592 STATUSBAR_POP(mainwin);
593 main_window_cursor_normal(mainwin);
596 static void draft_all_messages(void)
598 GList *compose_list = compose_get_compose_list();
602 for (elem = compose_list; elem != NULL && elem->data != NULL;
604 Compose *c = (Compose*)elem->data;
610 gboolean clean_quit(gpointer data)
612 static gboolean firstrun = TRUE;
618 /*!< Good idea to have the main window stored in a
619 * static variable so we can check that variable
620 * to see if we're really allowed to do things
621 * that actually the spawner is supposed to
622 * do (like: sending mail, composing messages).
623 * Because, really, if we're the spawnee, and
624 * we touch GTK stuff, we're hosed. See the
627 /* FIXME: Use something else to signal that we're
628 * in the original spawner, and not in a spawned
630 if (!static_mainwindow)
633 draft_all_messages();
635 exit_sylpheed(static_mainwindow);
641 void app_will_exit(GtkWidget *widget, gpointer data)
643 MainWindow *mainwin = data;
645 if (compose_get_compose_list()) {
646 gint val = alertpanel(_("Notice"),
647 _("Composing message exists."),
648 _("Draft them"), _("Discard them"), _("Don't quit"));
652 case G_ALERTALTERNATE:
655 draft_all_messages();
658 manage_window_focus_in(mainwin->window, NULL, NULL);
661 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
662 if (alertpanel(_("Queued messages"),
663 _("Some unsent messages are queued. Exit now?"),
664 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
666 manage_window_focus_in(mainwin->window, NULL, NULL);
672 * CLAWS: want this public so crash dialog can delete the
678 gchar *get_socket_name(void)
680 static gchar *filename = NULL;
682 if (filename == NULL) {
683 filename = g_strdup_printf("%s%csylpheed-%d",
684 g_get_tmp_dir(), G_DIR_SEPARATOR,
691 static gchar *get_crashfile_name(void)
693 static gchar *filename = NULL;
695 if (filename == NULL) {
696 filename = g_strdup_printf("%s%csylpheed-crashed",
697 get_tmp_dir(), G_DIR_SEPARATOR);
703 static gint prohibit_duplicate_launch(void)
708 path = get_socket_name();
709 uxsock = fd_connect_unix(path);
712 return fd_open_unix(path);
715 /* remote command mode */
717 debug_print("another Sylpheed is already running.\n");
720 fd_write_all(uxsock, "receive_all\n", 12);
721 else if (cmd.receive)
722 fd_write_all(uxsock, "receive\n", 8);
723 else if (cmd.compose && cmd.attach_files) {
724 gchar *str, *compose_str;
727 if (cmd.compose_mailto)
728 compose_str = g_strdup_printf("compose_attach %s\n",
731 compose_str = g_strdup("compose_attach\n");
733 fd_write_all(uxsock, compose_str, strlen(compose_str));
736 for (i = 0; i < cmd.attach_files->len; i++) {
737 str = g_ptr_array_index(cmd.attach_files, i);
738 fd_write_all(uxsock, str, strlen(str));
739 fd_write_all(uxsock, "\n", 1);
742 fd_write_all(uxsock, ".\n", 2);
743 } else if (cmd.compose) {
746 if (cmd.compose_mailto)
747 compose_str = g_strdup_printf
748 ("compose %s\n", cmd.compose_mailto);
750 compose_str = g_strdup("compose\n");
752 fd_write_all(uxsock, compose_str, strlen(compose_str));
754 } else if (cmd.send) {
755 fd_write_all(uxsock, "send\n", 5);
756 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
757 fd_write(uxsock, "online\n", 6);
758 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
759 fd_write(uxsock, "offline\n", 7);
760 } else if (cmd.status || cmd.status_full) {
763 const gchar *command;
767 command = cmd.status_full ? "status-full\n" : "status\n";
768 folders = cmd.status_full ? cmd.status_full_folders :
771 fd_write_all(uxsock, command, strlen(command));
772 for (i = 0; folders && i < folders->len; ++i) {
773 folder = g_ptr_array_index(folders, i);
774 fd_write_all(uxsock, folder, strlen(folder));
775 fd_write_all(uxsock, "\n", 1);
777 fd_write_all(uxsock, ".\n", 2);
779 fd_gets(uxsock, buf, sizeof(buf));
780 if (!strncmp(buf, ".\n", 2)) break;
784 fd_write_all(uxsock, "popup\n", 6);
790 static gint lock_socket_remove(void)
794 if (lock_socket < 0) return -1;
796 if (lock_socket_tag > 0)
797 gdk_input_remove(lock_socket_tag);
798 fd_close(lock_socket);
799 filename = get_socket_name();
805 static GPtrArray *get_folder_item_list(gint sock)
809 GPtrArray *folders = NULL;
812 fd_gets(sock, buf, sizeof(buf));
813 if (!strncmp(buf, ".\n", 2)) break;
815 if (!folders) folders = g_ptr_array_new();
816 item = folder_find_item_from_identifier(buf);
818 g_ptr_array_add(folders, item);
820 g_warning("no such folder: %s\n", buf);
826 static void lock_socket_input_cb(gpointer data,
828 GdkInputCondition condition)
830 MainWindow *mainwin = (MainWindow *)data;
834 sock = fd_accept(source);
835 fd_gets(sock, buf, sizeof(buf));
837 if (!strncmp(buf, "popup", 5)) {
838 main_window_popup(mainwin);
839 } else if (!strncmp(buf, "receive_all", 11)) {
840 inc_all_account_mail(mainwin, FALSE,
841 prefs_common.newmail_notify_manu);
842 } else if (!strncmp(buf, "receive", 7)) {
843 inc_mail(mainwin, prefs_common.newmail_notify_manu);
844 } else if (!strncmp(buf, "compose_attach", 14)) {
848 mailto = g_strdup(buf + strlen("compose_attach") + 1);
849 files = g_ptr_array_new();
850 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
851 if (buf[0] == '.' && buf[1] == '\n') break;
853 g_ptr_array_add(files, g_strdup(buf));
855 open_compose_new(mailto, files);
856 ptr_array_free_strings(files);
857 g_ptr_array_free(files, TRUE);
859 } else if (!strncmp(buf, "compose", 7)) {
860 open_compose_new(buf + strlen("compose") + 1, NULL);
861 } else if (!strncmp(buf, "send", 4)) {
863 } else if (!strncmp(buf, "online", 6)) {
864 main_window_toggle_work_offline(mainwin, FALSE);
865 } else if (!strncmp(buf, "offline", 7)) {
866 main_window_toggle_work_offline(mainwin, TRUE);
867 } else if (!strncmp(buf, "status-full", 11) ||
868 !strncmp(buf, "status", 6)) {
872 folders = get_folder_item_list(sock);
873 status = folder_get_status
874 (folders, !strncmp(buf, "status-full", 11));
875 fd_write_all(sock, status, strlen(status));
876 fd_write_all(sock, ".\n", 2);
878 if (folders) g_ptr_array_free(folders, TRUE);
884 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
889 Xstrdup_a(addr, address, return);
893 compose_new(NULL, addr, attach_files);
896 static void send_queue(void)
900 for (list = folder_get_list(); list != NULL; list = list->next) {
901 Folder *folder = list->data;
904 gint res = procmsg_send_queue
905 (folder->queue, prefs_common.savemsg);
908 alertpanel_error(_("Some errors occurred while sending queued messages."));
910 folder_item_scan(folder->queue);
915 static void quit_signal_handler(int sig)
917 debug_print("Quitting on signal %d\n", sig);
919 g_timeout_add(0, clean_quit, NULL);
922 static void install_basic_sighandlers()
925 struct sigaction act;
930 sigaddset(&mask, SIGTERM);
933 sigaddset(&mask, SIGINT);
936 act.sa_handler = quit_signal_handler;
941 sigaction(SIGTERM, &act, 0);
944 sigaction(SIGINT, &act, 0);
947 sigprocmask(SIG_UNBLOCK, &mask, 0);