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"
94 static gint lock_socket = -1;
95 static gint lock_socket_tag = 0;
99 ONLINE_MODE_DONT_CHANGE,
104 static struct RemoteCmd {
106 gboolean receive_all;
108 const gchar *compose_mailto;
109 GPtrArray *attach_files;
111 gboolean status_full;
112 GPtrArray *status_folders;
113 GPtrArray *status_full_folders;
120 static void parse_cmd_opt(int argc, char *argv[]);
122 static gint prohibit_duplicate_launch (void);
123 static gchar * get_crashfile_name (void);
124 static gint lock_socket_remove (void);
125 static void lock_socket_input_cb (gpointer data,
127 GdkInputCondition condition);
131 gchar *get_socket_name (void);
134 static void open_compose_new (const gchar *address,
135 GPtrArray *attach_files);
137 static void send_queue (void);
138 static void initial_processing (FolderItem *item, gpointer data);
139 static void quit_signal_handler (int sig);
140 static void install_basic_sighandlers (void);
141 static void exit_sylpheed (MainWindow *mainwin);
145 _("File `%s' already exists.\n"
146 "Can't create folder.")
149 #define MAKE_DIR_IF_NOT_EXIST(dir) \
151 if (!is_dir_exist(dir)) { \
152 if (is_file_exist(dir)) { \
154 (_("File `%s' already exists.\n" \
155 "Can't create folder."), \
159 if (make_dir(dir) < 0) \
164 static MainWindow *static_mainwindow;
166 int main(int argc, char *argv[])
170 FolderView *folderview;
172 if (!sylpheed_init(&argc, &argv)) {
176 prog_version = PROG_VERSION;
178 argv0 = g_strdup(argv[0]);
181 parse_cmd_opt(argc, argv);
186 gtk_init(&argc, &argv);
187 crash_main(cmd.crash_params);
190 crash_install_handlers();
192 install_basic_sighandlers();
194 /* check and create unix domain socket */
195 lock_socket = prohibit_duplicate_launch();
196 if (lock_socket < 0) return 0;
198 if (cmd.status || cmd.status_full) {
199 puts("0 Sylpheed not running.");
200 lock_socket_remove();
205 gtk_init(&argc, &argv);
208 gtk_widget_set_default_colormap(gdk_rgb_get_cmap());
209 gtk_widget_set_default_visual(gdk_rgb_get_visual());
211 #if USE_THREADS || USE_LDAP
213 if (!g_thread_supported())
214 g_error(_("g_thread is not supported by glib.\n"));
217 /* parse gtkrc files */
218 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
220 gtk_rc_parse(userrc);
222 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
223 G_DIR_SEPARATOR_S, "gtkrc", NULL);
224 gtk_rc_parse(userrc);
226 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
227 gtk_rc_parse(userrc);
230 gtk_rc_parse("./gtkrc");
232 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
233 gtk_item_factory_parse_rc(userrc);
236 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
238 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
239 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
240 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
241 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
242 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
243 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
245 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
247 folder_system_init();
249 prefs_common_read_config();
255 prefs_ext_prog_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 mainwin = main_window_create
274 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
275 folderview = mainwin->folderview;
277 /* register the callback of unix domain socket input */
278 lock_socket_tag = gdk_input_add(lock_socket,
279 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
280 lock_socket_input_cb,
283 account_read_config_all();
285 if (folder_read_list() < 0) {
289 if (!account_get_list()) {
294 account_set_missing_folder();
295 folder_set_missing_folders();
296 folderview_set(folderview);
298 prefs_matcher_read_config();
300 /* make one all-folder processing before using sylpheed */
301 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
303 /* if Sylpheed crashed, rebuild caches */
304 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
305 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
306 folderview_check_new(NULL);
308 /* make the crash-indicator file */
309 str_write_to_file("foo", get_crashfile_name());
311 addressbook_read_file();
313 inc_autocheck_timer_init(mainwin);
315 /* ignore SIGPIPE signal for preventing sudden death of program */
316 signal(SIGPIPE, SIG_IGN);
318 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
319 main_window_toggle_work_offline(mainwin, TRUE);
320 if (cmd.online_mode == ONLINE_MODE_ONLINE)
321 main_window_toggle_work_offline(mainwin, FALSE);
324 inc_all_account_mail(mainwin, FALSE,
325 prefs_common.newmail_notify_manu);
326 else if (prefs_common.chk_on_startup)
327 inc_all_account_mail(mainwin, TRUE,
328 prefs_common.newmail_notify_manu);
329 else if (cmd.receive)
330 inc_mail(mainwin, prefs_common.newmail_notify_manu);
332 gtk_widget_grab_focus(folderview->ctree);
335 open_compose_new(cmd.compose_mailto, cmd.attach_files);
336 if (cmd.attach_files) {
337 ptr_array_free_strings(cmd.attach_files);
338 g_ptr_array_free(cmd.attach_files, TRUE);
339 cmd.attach_files = NULL;
343 if (cmd.status_folders) {
344 g_ptr_array_free(cmd.status_folders, TRUE);
345 cmd.status_folders = NULL;
347 if (cmd.status_full_folders) {
348 g_ptr_array_free(cmd.status_full_folders, TRUE);
349 cmd.status_full_folders = NULL;
352 prefs_toolbar_init();
354 plugin_load_all("GTK");
356 static_mainwindow = mainwin;
359 exit_sylpheed(mainwin);
364 static void save_all_caches(FolderItem *item, gpointer data)
368 folder_item_write_cache(item);
371 static void exit_sylpheed(MainWindow *mainwin)
376 debug_print("shutting down\n");
378 inc_autocheck_timer_remove();
380 if (prefs_common.clean_on_exit)
381 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
383 /* save prefs for opened folder */
384 if(mainwin->folderview->opened)
388 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
389 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
392 /* save all state before exiting */
394 folder_func_to_all_folders(save_all_caches, NULL);
396 main_window_get_size(mainwin);
397 main_window_get_position(mainwin);
398 prefs_common_save_config();
399 account_save_config_all();
400 addressbook_export_to_file();
402 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
403 gtk_item_factory_dump_rc(filename, NULL, TRUE);
406 /* delete temporary files */
407 remove_all_files(get_mime_tmp_dir());
411 /* delete crashfile */
413 unlink(get_crashfile_name());
415 lock_socket_remove();
417 main_window_destroy(mainwin);
419 plugin_unload_all("GTK");
421 prefs_toolbar_done();
423 addressbook_destroy();
429 prefs_ext_prog_done();
433 prefs_spelling_done();
434 gtkaspell_checkers_quit();
440 static void parse_cmd_opt(int argc, char *argv[])
444 for (i = 1; i < argc; i++) {
445 if (!strncmp(argv[i], "--receive-all", 13))
446 cmd.receive_all = TRUE;
447 else if (!strncmp(argv[i], "--receive", 9))
449 else if (!strncmp(argv[i], "--compose", 9)) {
450 const gchar *p = argv[i + 1];
453 cmd.compose_mailto = NULL;
454 if (p && *p != '\0' && *p != '-') {
455 if (!strncmp(p, "mailto:", 7))
456 cmd.compose_mailto = p + 7;
458 cmd.compose_mailto = p;
461 } else if (!strncmp(argv[i], "--attach", 8)) {
462 const gchar *p = argv[i + 1];
465 while (p && *p != '\0' && *p != '-') {
466 if (!cmd.attach_files)
467 cmd.attach_files = g_ptr_array_new();
468 if (*p != G_DIR_SEPARATOR)
469 file = g_strconcat(sylpheed_get_startup_dir(),
474 g_ptr_array_add(cmd.attach_files, file);
478 } else if (!strncmp(argv[i], "--send", 6)) {
480 } else if (!strncmp(argv[i], "--version", 9)) {
481 puts("Sylpheed version " VERSION);
483 } else if (!strncmp(argv[i], "--status-full", 13)) {
484 const gchar *p = argv[i + 1];
486 cmd.status_full = TRUE;
487 while (p && *p != '\0' && *p != '-') {
488 if (!cmd.status_full_folders)
489 cmd.status_full_folders =
491 g_ptr_array_add(cmd.status_full_folders,
496 } else if (!strncmp(argv[i], "--status", 8)) {
497 const gchar *p = argv[i + 1];
500 while (p && *p != '\0' && *p != '-') {
501 if (!cmd.status_folders)
502 cmd.status_folders = g_ptr_array_new();
503 g_ptr_array_add(cmd.status_folders,
508 } else if (!strncmp(argv[i], "--online", 8)) {
509 cmd.online_mode = ONLINE_MODE_ONLINE;
510 } else if (!strncmp(argv[i], "--offline", 9)) {
511 cmd.online_mode = ONLINE_MODE_OFFLINE;
512 } else if (!strncmp(argv[i], "--help", 6)) {
513 g_print(_("Usage: %s [OPTION]...\n"),
514 g_basename(argv[0]));
516 puts(_(" --compose [address] open composition window"));
517 puts(_(" --attach file1 [file2]...\n"
518 " open composition window with specified files\n"
520 puts(_(" --receive receive new messages"));
521 puts(_(" --receive-all receive new messages of all accounts"));
522 puts(_(" --send send all queued messages"));
523 puts(_(" --status [folder]... show the total number of messages"));
524 puts(_(" --status-full [folder]...\n"
525 " show the status of each folder"));
526 puts(_(" --online switch to online mode"));
527 puts(_(" --offline switch to offline mode"));
528 puts(_(" --debug debug mode"));
529 puts(_(" --help display this help and exit"));
530 puts(_(" --version output version information and exit"));
531 puts(_(" --config-dir output configuration directory"));
534 } else if (!strncmp(argv[i], "--crash", 7)) {
536 cmd.crash_params = g_strdup(argv[i + 1]);
538 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
545 if (cmd.attach_files && cmd.compose == FALSE) {
547 cmd.compose_mailto = NULL;
551 static gint get_queued_message_num(void)
555 queue = folder_get_default_queue();
556 if (!queue) return -1;
558 folder_item_scan(queue);
559 return queue->total_msgs;
562 static void initial_processing(FolderItem *item, gpointer data)
564 MainWindow *mainwin = (MainWindow *)data;
567 g_return_if_fail(item);
568 buf = g_strdup_printf(_("Processing (%s)..."),
571 : _("top level folder"));
572 debug_print("%s\n", buf);
575 main_window_cursor_wait(mainwin);
577 if (item->prefs->enable_processing)
578 folder_item_apply_processing(item);
580 debug_print("done.\n");
581 STATUSBAR_POP(mainwin);
582 main_window_cursor_normal(mainwin);
585 static void draft_all_messages(void)
587 GList *compose_list = compose_get_compose_list();
591 for (elem = compose_list; elem != NULL && elem->data != NULL;
593 Compose *c = (Compose*)elem->data;
599 gboolean clean_quit(gpointer data)
601 static gboolean firstrun = TRUE;
607 /*!< Good idea to have the main window stored in a
608 * static variable so we can check that variable
609 * to see if we're really allowed to do things
610 * that actually the spawner is supposed to
611 * do (like: sending mail, composing messages).
612 * Because, really, if we're the spawnee, and
613 * we touch GTK stuff, we're hosed. See the
616 /* FIXME: Use something else to signal that we're
617 * in the original spawner, and not in a spawned
619 if (!static_mainwindow)
622 draft_all_messages();
624 exit_sylpheed(static_mainwindow);
630 void app_will_exit(GtkWidget *widget, gpointer data)
632 MainWindow *mainwin = data;
634 if (compose_get_compose_list()) {
635 gint val = alertpanel(_("Notice"),
636 _("Composing message exists."),
637 _("Draft them"), _("Discard them"), _("Don't quit"));
641 case G_ALERTALTERNATE:
644 draft_all_messages();
647 manage_window_focus_in(mainwin->window, NULL, NULL);
650 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
651 if (alertpanel(_("Queued messages"),
652 _("Some unsent messages are queued. Exit now?"),
653 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
655 manage_window_focus_in(mainwin->window, NULL, NULL);
661 * CLAWS: want this public so crash dialog can delete the
667 gchar *get_socket_name(void)
669 static gchar *filename = NULL;
671 if (filename == NULL) {
672 filename = g_strdup_printf("%s%csylpheed-%d",
673 g_get_tmp_dir(), G_DIR_SEPARATOR,
680 static gchar *get_crashfile_name(void)
682 static gchar *filename = NULL;
684 if (filename == NULL) {
685 filename = g_strdup_printf("%s%csylpheed-crashed",
686 get_tmp_dir(), G_DIR_SEPARATOR);
692 static gint prohibit_duplicate_launch(void)
697 path = get_socket_name();
698 uxsock = fd_connect_unix(path);
701 return fd_open_unix(path);
704 /* remote command mode */
706 debug_print("another Sylpheed is already running.\n");
709 fd_write_all(uxsock, "receive_all\n", 12);
710 else if (cmd.receive)
711 fd_write_all(uxsock, "receive\n", 8);
712 else if (cmd.compose && cmd.attach_files) {
713 gchar *str, *compose_str;
716 if (cmd.compose_mailto)
717 compose_str = g_strdup_printf("compose_attach %s\n",
720 compose_str = g_strdup("compose_attach\n");
722 fd_write_all(uxsock, compose_str, strlen(compose_str));
725 for (i = 0; i < cmd.attach_files->len; i++) {
726 str = g_ptr_array_index(cmd.attach_files, i);
727 fd_write_all(uxsock, str, strlen(str));
728 fd_write_all(uxsock, "\n", 1);
731 fd_write_all(uxsock, ".\n", 2);
732 } else if (cmd.compose) {
735 if (cmd.compose_mailto)
736 compose_str = g_strdup_printf
737 ("compose %s\n", cmd.compose_mailto);
739 compose_str = g_strdup("compose\n");
741 fd_write_all(uxsock, compose_str, strlen(compose_str));
743 } else if (cmd.send) {
744 fd_write_all(uxsock, "send\n", 5);
745 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
746 fd_write(uxsock, "online\n", 6);
747 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
748 fd_write(uxsock, "offline\n", 7);
749 } else if (cmd.status || cmd.status_full) {
752 const gchar *command;
756 command = cmd.status_full ? "status-full\n" : "status\n";
757 folders = cmd.status_full ? cmd.status_full_folders :
760 fd_write_all(uxsock, command, strlen(command));
761 for (i = 0; folders && i < folders->len; ++i) {
762 folder = g_ptr_array_index(folders, i);
763 fd_write_all(uxsock, folder, strlen(folder));
764 fd_write_all(uxsock, "\n", 1);
766 fd_write_all(uxsock, ".\n", 2);
768 fd_gets(uxsock, buf, sizeof(buf));
769 if (!strncmp(buf, ".\n", 2)) break;
773 fd_write_all(uxsock, "popup\n", 6);
779 static gint lock_socket_remove(void)
783 if (lock_socket < 0) return -1;
785 if (lock_socket_tag > 0)
786 gdk_input_remove(lock_socket_tag);
787 fd_close(lock_socket);
788 filename = get_socket_name();
794 static GPtrArray *get_folder_item_list(gint sock)
798 GPtrArray *folders = NULL;
801 fd_gets(sock, buf, sizeof(buf));
802 if (!strncmp(buf, ".\n", 2)) break;
804 if (!folders) folders = g_ptr_array_new();
805 item = folder_find_item_from_identifier(buf);
807 g_ptr_array_add(folders, item);
809 g_warning("no such folder: %s\n", buf);
815 static void lock_socket_input_cb(gpointer data,
817 GdkInputCondition condition)
819 MainWindow *mainwin = (MainWindow *)data;
823 sock = fd_accept(source);
824 fd_gets(sock, buf, sizeof(buf));
826 if (!strncmp(buf, "popup", 5)) {
827 main_window_popup(mainwin);
828 } else if (!strncmp(buf, "receive_all", 11)) {
829 inc_all_account_mail(mainwin, FALSE,
830 prefs_common.newmail_notify_manu);
831 } else if (!strncmp(buf, "receive", 7)) {
832 inc_mail(mainwin, prefs_common.newmail_notify_manu);
833 } else if (!strncmp(buf, "compose_attach", 14)) {
837 mailto = g_strdup(buf + strlen("compose_attach") + 1);
838 files = g_ptr_array_new();
839 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
840 if (buf[0] == '.' && buf[1] == '\n') break;
842 g_ptr_array_add(files, g_strdup(buf));
844 open_compose_new(mailto, files);
845 ptr_array_free_strings(files);
846 g_ptr_array_free(files, TRUE);
848 } else if (!strncmp(buf, "compose", 7)) {
849 open_compose_new(buf + strlen("compose") + 1, NULL);
850 } else if (!strncmp(buf, "send", 4)) {
852 } else if (!strncmp(buf, "online", 6)) {
853 main_window_toggle_work_offline(mainwin, FALSE);
854 } else if (!strncmp(buf, "offline", 7)) {
855 main_window_toggle_work_offline(mainwin, TRUE);
856 } else if (!strncmp(buf, "status-full", 11) ||
857 !strncmp(buf, "status", 6)) {
861 folders = get_folder_item_list(sock);
862 status = folder_get_status
863 (folders, !strncmp(buf, "status-full", 11));
864 fd_write_all(sock, status, strlen(status));
865 fd_write_all(sock, ".\n", 2);
867 if (folders) g_ptr_array_free(folders, TRUE);
873 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
878 Xstrdup_a(addr, address, return);
882 compose_new(NULL, addr, attach_files);
885 static void send_queue(void)
889 for (list = folder_get_list(); list != NULL; list = list->next) {
890 Folder *folder = list->data;
893 gint res = procmsg_send_queue
894 (folder->queue, prefs_common.savemsg);
897 alertpanel_error(_("Some errors occurred while sending queued messages."));
899 folder_item_scan(folder->queue);
904 static void quit_signal_handler(int sig)
906 debug_print("Quitting on signal %d\n", sig);
908 g_timeout_add(0, clean_quit, NULL);
911 static void install_basic_sighandlers()
914 struct sigaction act;
919 sigaddset(&mask, SIGTERM);
922 sigaddset(&mask, SIGINT);
925 act.sa_handler = quit_signal_handler;
930 sigaction(SIGTERM, &act, 0);
933 sigaction(SIGINT, &act, 0);
936 sigprocmask(SIG_UNBLOCK, &mask, 0);