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_fonts.h"
55 #include "prefs_spelling.h"
57 #include "prefs_display_header.h"
62 #include "manage_window.h"
63 #include "alertpanel.h"
64 #include "statusbar.h"
65 #include "addressbook.h"
73 #include "prefs_toolbar.h"
92 static gint lock_socket = -1;
93 static gint lock_socket_tag = 0;
97 ONLINE_MODE_DONT_CHANGE,
102 static struct RemoteCmd {
104 gboolean receive_all;
106 const gchar *compose_mailto;
107 GPtrArray *attach_files;
109 gboolean status_full;
110 GPtrArray *status_folders;
111 GPtrArray *status_full_folders;
118 static void parse_cmd_opt(int argc, char *argv[]);
120 static gint prohibit_duplicate_launch (void);
121 static gchar * get_crashfile_name (void);
122 static gint lock_socket_remove (void);
123 static void lock_socket_input_cb (gpointer data,
125 GdkInputCondition condition);
129 gchar *get_socket_name (void);
132 static void open_compose_new (const gchar *address,
133 GPtrArray *attach_files);
135 static void send_queue (void);
136 static void initial_processing (FolderItem *item, gpointer data);
137 static void quit_signal_handler (int sig);
138 static void install_basic_sighandlers (void);
142 _("File `%s' already exists.\n"
143 "Can't create folder.")
146 #define MAKE_DIR_IF_NOT_EXIST(dir) \
148 if (!is_dir_exist(dir)) { \
149 if (is_file_exist(dir)) { \
151 (_("File `%s' already exists.\n" \
152 "Can't create folder."), \
156 if (make_dir(dir) < 0) \
161 static MainWindow *static_mainwindow;
163 int main(int argc, char *argv[])
167 FolderView *folderview;
169 if (!sylpheed_init(&argc, &argv)) {
173 prog_version = PROG_VERSION;
175 argv0 = g_strdup(argv[0]);
178 parse_cmd_opt(argc, argv);
183 gtk_init(&argc, &argv);
184 crash_main(cmd.crash_params);
187 crash_install_handlers();
189 install_basic_sighandlers();
191 /* check and create unix domain socket */
192 lock_socket = prohibit_duplicate_launch();
193 if (lock_socket < 0) return 0;
195 if (cmd.status || cmd.status_full) {
196 puts("0 Sylpheed not running.");
197 lock_socket_remove();
202 gtk_init(&argc, &argv);
205 gtk_widget_set_default_colormap(gdk_rgb_get_cmap());
206 gtk_widget_set_default_visual(gdk_rgb_get_visual());
208 #if USE_THREADS || USE_LDAP
210 if (!g_thread_supported())
211 g_error(_("g_thread is not supported by glib.\n"));
214 /* parse gtkrc files */
215 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
217 gtk_rc_parse(userrc);
219 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
220 G_DIR_SEPARATOR_S, "gtkrc", NULL);
221 gtk_rc_parse(userrc);
223 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
224 gtk_rc_parse(userrc);
227 gtk_rc_parse("./gtkrc");
229 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
230 gtk_item_factory_parse_rc(userrc);
233 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
235 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
236 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
237 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
238 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
239 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
240 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
242 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
244 folder_system_init();
246 prefs_common_read_config();
254 gtkaspell_checkers_init();
255 prefs_spelling_init();
258 sock_set_io_timeout(prefs_common.io_timeout_secs);
260 prefs_actions_read_config();
261 prefs_display_header_read_config();
262 /* prefs_filtering_read_config(); */
263 addressbook_read_file();
264 renderer_read_config();
268 mainwin = main_window_create
269 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
270 folderview = mainwin->folderview;
272 /* register the callback of unix domain socket input */
273 lock_socket_tag = gdk_input_add(lock_socket,
274 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
275 lock_socket_input_cb,
278 account_read_config_all();
280 if (folder_read_list() < 0) {
284 if (!account_get_list()) {
289 account_set_missing_folder();
290 folder_set_missing_folders();
291 folderview_set(folderview);
293 /* prefs_scoring_read_config(); */
294 prefs_matcher_read_config();
296 /* make one all-folder processing before using sylpheed */
297 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
299 /* if Sylpheed crashed, rebuild caches */
300 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
301 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
302 folderview_check_new(NULL);
304 /* make the crash-indicator file */
305 str_write_to_file("foo", get_crashfile_name());
307 addressbook_read_file();
309 inc_autocheck_timer_init(mainwin);
311 /* ignore SIGPIPE signal for preventing sudden death of program */
312 signal(SIGPIPE, SIG_IGN);
315 inc_all_account_mail(mainwin, FALSE,
316 prefs_common.newmail_notify_manu);
317 else if (prefs_common.chk_on_startup)
318 inc_all_account_mail(mainwin, TRUE,
319 prefs_common.newmail_notify_manu);
320 else if (cmd.receive)
321 inc_mail(mainwin, prefs_common.newmail_notify_manu);
323 gtk_widget_grab_focus(folderview->ctree);
326 open_compose_new(cmd.compose_mailto, cmd.attach_files);
327 if (cmd.attach_files) {
328 ptr_array_free_strings(cmd.attach_files);
329 g_ptr_array_free(cmd.attach_files, TRUE);
330 cmd.attach_files = NULL;
334 if (cmd.status_folders) {
335 g_ptr_array_free(cmd.status_folders, TRUE);
336 cmd.status_folders = NULL;
338 if (cmd.status_full_folders) {
339 g_ptr_array_free(cmd.status_full_folders, TRUE);
340 cmd.status_full_folders = NULL;
343 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
344 main_window_toggle_work_offline(mainwin, TRUE);
345 if (cmd.online_mode == ONLINE_MODE_ONLINE)
346 main_window_toggle_work_offline(mainwin, FALSE);
348 prefs_toolbar_init();
350 plugin_load_all("GTK");
352 static_mainwindow = mainwin;
355 plugin_unload_all("GTK");
357 prefs_toolbar_done();
359 addressbook_destroy();
367 prefs_spelling_done();
368 gtkaspell_checkers_quit();
375 static void parse_cmd_opt(int argc, char *argv[])
379 for (i = 1; i < argc; i++) {
380 if (!strncmp(argv[i], "--receive-all", 13))
381 cmd.receive_all = TRUE;
382 else if (!strncmp(argv[i], "--receive", 9))
384 else if (!strncmp(argv[i], "--compose", 9)) {
385 const gchar *p = argv[i + 1];
388 cmd.compose_mailto = NULL;
389 if (p && *p != '\0' && *p != '-') {
390 if (!strncmp(p, "mailto:", 7))
391 cmd.compose_mailto = p + 7;
393 cmd.compose_mailto = p;
396 } else if (!strncmp(argv[i], "--attach", 8)) {
397 const gchar *p = argv[i + 1];
400 while (p && *p != '\0' && *p != '-') {
401 if (!cmd.attach_files)
402 cmd.attach_files = g_ptr_array_new();
403 if (*p != G_DIR_SEPARATOR)
404 file = g_strconcat(sylpheed_get_startup_dir(),
409 g_ptr_array_add(cmd.attach_files, file);
413 } else if (!strncmp(argv[i], "--send", 6)) {
415 } else if (!strncmp(argv[i], "--version", 9)) {
416 puts("Sylpheed version " VERSION);
418 } else if (!strncmp(argv[i], "--status-full", 13)) {
419 const gchar *p = argv[i + 1];
421 cmd.status_full = TRUE;
422 while (p && *p != '\0' && *p != '-') {
423 if (!cmd.status_full_folders)
424 cmd.status_full_folders =
426 g_ptr_array_add(cmd.status_full_folders,
431 } else if (!strncmp(argv[i], "--status", 8)) {
432 const gchar *p = argv[i + 1];
435 while (p && *p != '\0' && *p != '-') {
436 if (!cmd.status_folders)
437 cmd.status_folders = g_ptr_array_new();
438 g_ptr_array_add(cmd.status_folders,
443 } else if (!strncmp(argv[i], "--online", 8)) {
444 cmd.online_mode = ONLINE_MODE_ONLINE;
445 } else if (!strncmp(argv[i], "--offline", 9)) {
446 cmd.online_mode = ONLINE_MODE_OFFLINE;
447 } else if (!strncmp(argv[i], "--help", 6)) {
448 g_print(_("Usage: %s [OPTION]...\n"),
449 g_basename(argv[0]));
451 puts(_(" --compose [address] open composition window"));
452 puts(_(" --attach file1 [file2]...\n"
453 " open composition window with specified files\n"
455 puts(_(" --receive receive new messages"));
456 puts(_(" --receive-all receive new messages of all accounts"));
457 puts(_(" --send send all queued messages"));
458 puts(_(" --status [folder]... show the total number of messages"));
459 puts(_(" --status-full [folder]...\n"
460 " show the status of each folder"));
461 puts(_(" --online switch to online mode"));
462 puts(_(" --offline switch to offline mode"));
463 puts(_(" --debug debug mode"));
464 puts(_(" --help display this help and exit"));
465 puts(_(" --version output version information and exit"));
466 puts(_(" --config-dir output configuration directory"));
469 } else if (!strncmp(argv[i], "--crash", 7)) {
471 cmd.crash_params = g_strdup(argv[i + 1]);
473 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
480 if (cmd.attach_files && cmd.compose == FALSE) {
482 cmd.compose_mailto = NULL;
486 static gint get_queued_message_num(void)
490 queue = folder_get_default_queue();
491 if (!queue) return -1;
493 folder_item_scan(queue);
494 return queue->total_msgs;
497 static void save_all_caches(FolderItem *item, gpointer data)
501 folder_item_write_cache(item);
504 static void initial_processing(FolderItem *item, gpointer data)
506 MainWindow *mainwin = (MainWindow *)data;
509 g_return_if_fail(item);
510 buf = g_strdup_printf(_("Processing (%s)..."),
513 : _("top level folder"));
514 debug_print("%s\n", buf);
517 main_window_cursor_wait(mainwin);
519 folder_item_apply_processing(item);
521 debug_print("done.\n");
522 STATUSBAR_POP(mainwin);
523 main_window_cursor_normal(mainwin);
526 static void draft_all_messages(void)
528 GList *compose_list = compose_get_compose_list();
532 for (elem = compose_list; elem != NULL && elem->data != NULL;
534 Compose *c = (Compose*)elem->data;
540 void clean_quit(void)
542 /*!< Good idea to have the main window stored in a
543 * static variable so we can check that variable
544 * to see if we're really allowed to do things
545 * that actually the spawner is supposed to
546 * do (like: sending mail, composing messages).
547 * Because, really, if we're the spawnee, and
548 * we touch GTK stuff, we're hosed. See the
551 /* FIXME: Use something else to signal that we're
552 * in the original spawner, and not in a spawned
554 if (!static_mainwindow)
557 draft_all_messages();
559 if (prefs_common.warn_queued_on_exit) {
560 /* disable the popup */
561 prefs_common.warn_queued_on_exit = FALSE;
562 app_will_exit(NULL, static_mainwindow);
563 prefs_common.warn_queued_on_exit = TRUE;
564 prefs_common_save_config();
566 app_will_exit(NULL, static_mainwindow);
571 void app_will_exit(GtkWidget *widget, gpointer data)
573 MainWindow *mainwin = data;
577 if (compose_get_compose_list()) {
578 gint val = alertpanel(_("Notice"),
579 _("Composing message exists."),
580 _("Draft them"), _("Discard them"), _("Don't quit"));
584 case G_ALERTALTERNATE:
587 draft_all_messages();
590 manage_window_focus_in(mainwin->window, NULL, NULL);
593 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
594 if (alertpanel(_("Queued messages"),
595 _("Some unsent messages are queued. Exit now?"),
596 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
598 manage_window_focus_in(mainwin->window, NULL, NULL);
601 inc_autocheck_timer_remove();
603 if (prefs_common.clean_on_exit)
604 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
606 /* save prefs for opened folder */
607 if(mainwin->folderview->opened)
611 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
612 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
615 /* save all state before exiting */
617 folder_func_to_all_folders(save_all_caches, NULL);
618 for (list = folder_get_list(); list != NULL; list = g_list_next(list)) {
619 Folder *folder = FOLDER(list->data);
621 folder_tree_destroy(folder);
624 main_window_get_size(mainwin);
625 main_window_get_position(mainwin);
626 prefs_common_save_config();
627 account_save_config_all();
628 addressbook_export_to_file();
630 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
631 gtk_item_factory_dump_rc(filename, NULL, TRUE);
634 /* delete temporary files */
635 remove_all_files(get_mime_tmp_dir());
639 /* delete crashfile */
641 unlink(get_crashfile_name());
643 lock_socket_remove();
649 * CLAWS: want this public so crash dialog can delete the
655 gchar *get_socket_name(void)
657 static gchar *filename = NULL;
659 if (filename == NULL) {
660 filename = g_strdup_printf("%s%csylpheed-%d",
661 g_get_tmp_dir(), G_DIR_SEPARATOR,
668 static gchar *get_crashfile_name(void)
670 static gchar *filename = NULL;
672 if (filename == NULL) {
673 filename = g_strdup_printf("%s%csylpheed-crashed",
674 get_tmp_dir(), G_DIR_SEPARATOR);
680 static gint prohibit_duplicate_launch(void)
685 path = get_socket_name();
686 uxsock = fd_connect_unix(path);
689 return fd_open_unix(path);
692 /* remote command mode */
694 debug_print("another Sylpheed is already running.\n");
697 fd_write_all(uxsock, "receive_all\n", 12);
698 else if (cmd.receive)
699 fd_write_all(uxsock, "receive\n", 8);
700 else if (cmd.compose && cmd.attach_files) {
701 gchar *str, *compose_str;
704 if (cmd.compose_mailto)
705 compose_str = g_strdup_printf("compose_attach %s\n",
708 compose_str = g_strdup("compose_attach\n");
710 fd_write_all(uxsock, compose_str, strlen(compose_str));
713 for (i = 0; i < cmd.attach_files->len; i++) {
714 str = g_ptr_array_index(cmd.attach_files, i);
715 fd_write_all(uxsock, str, strlen(str));
716 fd_write_all(uxsock, "\n", 1);
719 fd_write_all(uxsock, ".\n", 2);
720 } else if (cmd.compose) {
723 if (cmd.compose_mailto)
724 compose_str = g_strdup_printf
725 ("compose %s\n", cmd.compose_mailto);
727 compose_str = g_strdup("compose\n");
729 fd_write_all(uxsock, compose_str, strlen(compose_str));
731 } else if (cmd.send) {
732 fd_write_all(uxsock, "send\n", 5);
733 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
734 fd_write(uxsock, "online\n", 6);
735 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
736 fd_write(uxsock, "offline\n", 7);
737 } else if (cmd.status || cmd.status_full) {
740 const gchar *command;
744 command = cmd.status_full ? "status-full\n" : "status\n";
745 folders = cmd.status_full ? cmd.status_full_folders :
748 fd_write_all(uxsock, command, strlen(command));
749 for (i = 0; folders && i < folders->len; ++i) {
750 folder = g_ptr_array_index(folders, i);
751 fd_write_all(uxsock, folder, strlen(folder));
752 fd_write_all(uxsock, "\n", 1);
754 fd_write_all(uxsock, ".\n", 2);
756 fd_gets(uxsock, buf, sizeof(buf));
757 if (!strncmp(buf, ".\n", 2)) break;
761 fd_write_all(uxsock, "popup\n", 6);
767 static gint lock_socket_remove(void)
771 if (lock_socket < 0) return -1;
773 if (lock_socket_tag > 0)
774 gdk_input_remove(lock_socket_tag);
775 fd_close(lock_socket);
776 filename = get_socket_name();
782 static GPtrArray *get_folder_item_list(gint sock)
786 GPtrArray *folders = NULL;
789 fd_gets(sock, buf, sizeof(buf));
790 if (!strncmp(buf, ".\n", 2)) break;
792 if (!folders) folders = g_ptr_array_new();
793 item = folder_find_item_from_identifier(buf);
795 g_ptr_array_add(folders, item);
797 g_warning("no such folder: %s\n", buf);
803 static void lock_socket_input_cb(gpointer data,
805 GdkInputCondition condition)
807 MainWindow *mainwin = (MainWindow *)data;
811 sock = fd_accept(source);
812 fd_gets(sock, buf, sizeof(buf));
814 if (!strncmp(buf, "popup", 5)) {
815 main_window_popup(mainwin);
816 } else if (!strncmp(buf, "receive_all", 11)) {
817 inc_all_account_mail(mainwin, FALSE,
818 prefs_common.newmail_notify_manu);
819 } else if (!strncmp(buf, "receive", 7)) {
820 inc_mail(mainwin, prefs_common.newmail_notify_manu);
821 } else if (!strncmp(buf, "compose_attach", 14)) {
825 mailto = g_strdup(buf + strlen("compose_attach") + 1);
826 files = g_ptr_array_new();
827 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
828 if (buf[0] == '.' && buf[1] == '\n') break;
830 g_ptr_array_add(files, g_strdup(buf));
832 open_compose_new(mailto, files);
833 ptr_array_free_strings(files);
834 g_ptr_array_free(files, TRUE);
836 } else if (!strncmp(buf, "compose", 7)) {
837 open_compose_new(buf + strlen("compose") + 1, NULL);
838 } else if (!strncmp(buf, "send", 4)) {
840 } else if (!strncmp(buf, "online", 6)) {
841 main_window_toggle_work_offline(mainwin, FALSE);
842 } else if (!strncmp(buf, "offline", 7)) {
843 main_window_toggle_work_offline(mainwin, TRUE);
844 } else if (!strncmp(buf, "status-full", 11) ||
845 !strncmp(buf, "status", 6)) {
849 folders = get_folder_item_list(sock);
850 status = folder_get_status
851 (folders, !strncmp(buf, "status-full", 11));
852 fd_write_all(sock, status, strlen(status));
853 fd_write_all(sock, ".\n", 2);
855 if (folders) g_ptr_array_free(folders, TRUE);
861 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
866 Xstrdup_a(addr, address, return);
870 compose_new(NULL, addr, attach_files);
873 static void send_queue(void)
876 FolderItem *def_outbox;
878 def_outbox = folder_get_default_outbox();
880 for (list = folder_get_list(); list != NULL; list = list->next) {
881 Folder *folder = list->data;
884 gint res = procmsg_send_queue
885 (folder->queue, prefs_common.savemsg);
888 alertpanel_error(_("Some errors occurred while sending queued messages."));
890 folder_item_scan(folder->queue);
891 if (prefs_common.savemsg && folder->outbox) {
892 if (folder->outbox == def_outbox)
899 static void quit_signal_handler(int sig)
901 debug_print("Quitting on signal %d\n", sig);
905 static void install_basic_sighandlers()
908 struct sigaction act;
913 sigaddset(&mask, SIGTERM);
916 sigaddset(&mask, SIGINT);
919 act.sa_handler = quit_signal_handler;
924 sigaction(SIGTERM, &act, 0);
927 sigaction(SIGINT, &act, 0);
930 sigprocmask(SIG_UNBLOCK, &mask, 0);