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"
56 #include "prefs_themes.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"
93 static gint lock_socket = -1;
94 static gint lock_socket_tag = 0;
98 ONLINE_MODE_DONT_CHANGE,
103 static struct RemoteCmd {
105 gboolean receive_all;
107 const gchar *compose_mailto;
108 GPtrArray *attach_files;
110 gboolean status_full;
111 GPtrArray *status_folders;
112 GPtrArray *status_full_folders;
119 static void parse_cmd_opt(int argc, char *argv[]);
121 static gint prohibit_duplicate_launch (void);
122 static gchar * get_crashfile_name (void);
123 static gint lock_socket_remove (void);
124 static void lock_socket_input_cb (gpointer data,
126 GdkInputCondition condition);
130 gchar *get_socket_name (void);
133 static void open_compose_new (const gchar *address,
134 GPtrArray *attach_files);
136 static void send_queue (void);
137 static void initial_processing (FolderItem *item, gpointer data);
138 static void quit_signal_handler (int sig);
139 static void install_basic_sighandlers (void);
140 static void exit_sylpheed (MainWindow *mainwin);
144 _("File `%s' already exists.\n"
145 "Can't create folder.")
148 #define MAKE_DIR_IF_NOT_EXIST(dir) \
150 if (!is_dir_exist(dir)) { \
151 if (is_file_exist(dir)) { \
153 (_("File `%s' already exists.\n" \
154 "Can't create folder."), \
158 if (make_dir(dir) < 0) \
163 static MainWindow *static_mainwindow;
165 int main(int argc, char *argv[])
169 FolderView *folderview;
171 if (!sylpheed_init(&argc, &argv)) {
175 prog_version = PROG_VERSION;
177 argv0 = g_strdup(argv[0]);
180 parse_cmd_opt(argc, argv);
185 gtk_init(&argc, &argv);
186 crash_main(cmd.crash_params);
189 crash_install_handlers();
191 install_basic_sighandlers();
193 /* check and create unix domain socket */
194 lock_socket = prohibit_duplicate_launch();
195 if (lock_socket < 0) return 0;
197 if (cmd.status || cmd.status_full) {
198 puts("0 Sylpheed not running.");
199 lock_socket_remove();
204 gtk_init(&argc, &argv);
207 gtk_widget_set_default_colormap(gdk_rgb_get_cmap());
208 gtk_widget_set_default_visual(gdk_rgb_get_visual());
210 #if USE_THREADS || USE_LDAP
212 if (!g_thread_supported())
213 g_error(_("g_thread is not supported by glib.\n"));
216 /* parse gtkrc files */
217 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
219 gtk_rc_parse(userrc);
221 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
222 G_DIR_SEPARATOR_S, "gtkrc", NULL);
223 gtk_rc_parse(userrc);
225 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
226 gtk_rc_parse(userrc);
229 gtk_rc_parse("./gtkrc");
231 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
232 gtk_item_factory_parse_rc(userrc);
235 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
237 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
238 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
239 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
240 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
241 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
242 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
244 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
246 folder_system_init();
248 prefs_common_read_config();
257 gtkaspell_checkers_init();
258 prefs_spelling_init();
261 sock_set_io_timeout(prefs_common.io_timeout_secs);
263 prefs_actions_read_config();
264 prefs_display_header_read_config();
265 /* prefs_filtering_read_config(); */
266 addressbook_read_file();
267 renderer_read_config();
271 mainwin = main_window_create
272 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
273 folderview = mainwin->folderview;
275 /* register the callback of unix domain socket input */
276 lock_socket_tag = gdk_input_add(lock_socket,
277 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
278 lock_socket_input_cb,
281 account_read_config_all();
283 if (folder_read_list() < 0) {
287 if (!account_get_list()) {
292 account_set_missing_folder();
293 folder_set_missing_folders();
294 folderview_set(folderview);
296 prefs_matcher_read_config();
298 /* make one all-folder processing before using sylpheed */
299 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
301 /* if Sylpheed crashed, rebuild caches */
302 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
303 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
304 folderview_check_new(NULL);
306 /* make the crash-indicator file */
307 str_write_to_file("foo", get_crashfile_name());
309 addressbook_read_file();
311 inc_autocheck_timer_init(mainwin);
313 /* ignore SIGPIPE signal for preventing sudden death of program */
314 signal(SIGPIPE, SIG_IGN);
316 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
317 main_window_toggle_work_offline(mainwin, TRUE);
318 if (cmd.online_mode == ONLINE_MODE_ONLINE)
319 main_window_toggle_work_offline(mainwin, FALSE);
322 inc_all_account_mail(mainwin, FALSE,
323 prefs_common.newmail_notify_manu);
324 else if (prefs_common.chk_on_startup)
325 inc_all_account_mail(mainwin, TRUE,
326 prefs_common.newmail_notify_manu);
327 else if (cmd.receive)
328 inc_mail(mainwin, prefs_common.newmail_notify_manu);
330 gtk_widget_grab_focus(folderview->ctree);
333 open_compose_new(cmd.compose_mailto, cmd.attach_files);
334 if (cmd.attach_files) {
335 ptr_array_free_strings(cmd.attach_files);
336 g_ptr_array_free(cmd.attach_files, TRUE);
337 cmd.attach_files = NULL;
341 if (cmd.status_folders) {
342 g_ptr_array_free(cmd.status_folders, TRUE);
343 cmd.status_folders = NULL;
345 if (cmd.status_full_folders) {
346 g_ptr_array_free(cmd.status_full_folders, TRUE);
347 cmd.status_full_folders = NULL;
350 prefs_toolbar_init();
352 plugin_load_all("GTK");
354 static_mainwindow = mainwin;
357 exit_sylpheed(mainwin);
362 static void save_all_caches(FolderItem *item, gpointer data)
366 folder_item_write_cache(item);
369 static void exit_sylpheed(MainWindow *mainwin)
374 debug_print("shutting down\n");
376 inc_autocheck_timer_remove();
378 if (prefs_common.clean_on_exit)
379 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
381 /* save prefs for opened folder */
382 if(mainwin->folderview->opened)
386 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
387 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
390 /* save all state before exiting */
392 folder_func_to_all_folders(save_all_caches, NULL);
394 main_window_get_size(mainwin);
395 main_window_get_position(mainwin);
396 prefs_common_save_config();
397 account_save_config_all();
398 addressbook_export_to_file();
400 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
401 gtk_item_factory_dump_rc(filename, NULL, TRUE);
404 /* delete temporary files */
405 remove_all_files(get_mime_tmp_dir());
409 /* delete crashfile */
411 unlink(get_crashfile_name());
413 lock_socket_remove();
415 main_window_destroy(mainwin);
417 plugin_unload_all("GTK");
419 prefs_toolbar_done();
421 addressbook_destroy();
430 prefs_spelling_done();
431 gtkaspell_checkers_quit();
437 static void parse_cmd_opt(int argc, char *argv[])
441 for (i = 1; i < argc; i++) {
442 if (!strncmp(argv[i], "--receive-all", 13))
443 cmd.receive_all = TRUE;
444 else if (!strncmp(argv[i], "--receive", 9))
446 else if (!strncmp(argv[i], "--compose", 9)) {
447 const gchar *p = argv[i + 1];
450 cmd.compose_mailto = NULL;
451 if (p && *p != '\0' && *p != '-') {
452 if (!strncmp(p, "mailto:", 7))
453 cmd.compose_mailto = p + 7;
455 cmd.compose_mailto = p;
458 } else if (!strncmp(argv[i], "--attach", 8)) {
459 const gchar *p = argv[i + 1];
462 while (p && *p != '\0' && *p != '-') {
463 if (!cmd.attach_files)
464 cmd.attach_files = g_ptr_array_new();
465 if (*p != G_DIR_SEPARATOR)
466 file = g_strconcat(sylpheed_get_startup_dir(),
471 g_ptr_array_add(cmd.attach_files, file);
475 } else if (!strncmp(argv[i], "--send", 6)) {
477 } else if (!strncmp(argv[i], "--version", 9)) {
478 puts("Sylpheed version " VERSION);
480 } else if (!strncmp(argv[i], "--status-full", 13)) {
481 const gchar *p = argv[i + 1];
483 cmd.status_full = TRUE;
484 while (p && *p != '\0' && *p != '-') {
485 if (!cmd.status_full_folders)
486 cmd.status_full_folders =
488 g_ptr_array_add(cmd.status_full_folders,
493 } else if (!strncmp(argv[i], "--status", 8)) {
494 const gchar *p = argv[i + 1];
497 while (p && *p != '\0' && *p != '-') {
498 if (!cmd.status_folders)
499 cmd.status_folders = g_ptr_array_new();
500 g_ptr_array_add(cmd.status_folders,
505 } else if (!strncmp(argv[i], "--online", 8)) {
506 cmd.online_mode = ONLINE_MODE_ONLINE;
507 } else if (!strncmp(argv[i], "--offline", 9)) {
508 cmd.online_mode = ONLINE_MODE_OFFLINE;
509 } else if (!strncmp(argv[i], "--help", 6)) {
510 g_print(_("Usage: %s [OPTION]...\n"),
511 g_basename(argv[0]));
513 puts(_(" --compose [address] open composition window"));
514 puts(_(" --attach file1 [file2]...\n"
515 " open composition window with specified files\n"
517 puts(_(" --receive receive new messages"));
518 puts(_(" --receive-all receive new messages of all accounts"));
519 puts(_(" --send send all queued messages"));
520 puts(_(" --status [folder]... show the total number of messages"));
521 puts(_(" --status-full [folder]...\n"
522 " show the status of each folder"));
523 puts(_(" --online switch to online mode"));
524 puts(_(" --offline switch to offline mode"));
525 puts(_(" --debug debug mode"));
526 puts(_(" --help display this help and exit"));
527 puts(_(" --version output version information and exit"));
528 puts(_(" --config-dir output configuration directory"));
531 } else if (!strncmp(argv[i], "--crash", 7)) {
533 cmd.crash_params = g_strdup(argv[i + 1]);
535 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
542 if (cmd.attach_files && cmd.compose == FALSE) {
544 cmd.compose_mailto = NULL;
548 static gint get_queued_message_num(void)
552 queue = folder_get_default_queue();
553 if (!queue) return -1;
555 folder_item_scan(queue);
556 return queue->total_msgs;
559 static void initial_processing(FolderItem *item, gpointer data)
561 MainWindow *mainwin = (MainWindow *)data;
564 g_return_if_fail(item);
565 buf = g_strdup_printf(_("Processing (%s)..."),
568 : _("top level folder"));
569 debug_print("%s\n", buf);
572 main_window_cursor_wait(mainwin);
574 if (item->prefs->enable_processing)
575 folder_item_apply_processing(item);
577 debug_print("done.\n");
578 STATUSBAR_POP(mainwin);
579 main_window_cursor_normal(mainwin);
582 static void draft_all_messages(void)
584 GList *compose_list = compose_get_compose_list();
588 for (elem = compose_list; elem != NULL && elem->data != NULL;
590 Compose *c = (Compose*)elem->data;
596 gboolean clean_quit(gpointer data)
598 static gboolean firstrun = TRUE;
604 /*!< Good idea to have the main window stored in a
605 * static variable so we can check that variable
606 * to see if we're really allowed to do things
607 * that actually the spawner is supposed to
608 * do (like: sending mail, composing messages).
609 * Because, really, if we're the spawnee, and
610 * we touch GTK stuff, we're hosed. See the
613 /* FIXME: Use something else to signal that we're
614 * in the original spawner, and not in a spawned
616 if (!static_mainwindow)
619 draft_all_messages();
621 exit_sylpheed(static_mainwindow);
627 void app_will_exit(GtkWidget *widget, gpointer data)
629 MainWindow *mainwin = data;
631 if (compose_get_compose_list()) {
632 gint val = alertpanel(_("Notice"),
633 _("Composing message exists."),
634 _("Draft them"), _("Discard them"), _("Don't quit"));
638 case G_ALERTALTERNATE:
641 draft_all_messages();
644 manage_window_focus_in(mainwin->window, NULL, NULL);
647 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
648 if (alertpanel(_("Queued messages"),
649 _("Some unsent messages are queued. Exit now?"),
650 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
652 manage_window_focus_in(mainwin->window, NULL, NULL);
658 * CLAWS: want this public so crash dialog can delete the
664 gchar *get_socket_name(void)
666 static gchar *filename = NULL;
668 if (filename == NULL) {
669 filename = g_strdup_printf("%s%csylpheed-%d",
670 g_get_tmp_dir(), G_DIR_SEPARATOR,
677 static gchar *get_crashfile_name(void)
679 static gchar *filename = NULL;
681 if (filename == NULL) {
682 filename = g_strdup_printf("%s%csylpheed-crashed",
683 get_tmp_dir(), G_DIR_SEPARATOR);
689 static gint prohibit_duplicate_launch(void)
694 path = get_socket_name();
695 uxsock = fd_connect_unix(path);
698 return fd_open_unix(path);
701 /* remote command mode */
703 debug_print("another Sylpheed is already running.\n");
706 fd_write_all(uxsock, "receive_all\n", 12);
707 else if (cmd.receive)
708 fd_write_all(uxsock, "receive\n", 8);
709 else if (cmd.compose && cmd.attach_files) {
710 gchar *str, *compose_str;
713 if (cmd.compose_mailto)
714 compose_str = g_strdup_printf("compose_attach %s\n",
717 compose_str = g_strdup("compose_attach\n");
719 fd_write_all(uxsock, compose_str, strlen(compose_str));
722 for (i = 0; i < cmd.attach_files->len; i++) {
723 str = g_ptr_array_index(cmd.attach_files, i);
724 fd_write_all(uxsock, str, strlen(str));
725 fd_write_all(uxsock, "\n", 1);
728 fd_write_all(uxsock, ".\n", 2);
729 } else if (cmd.compose) {
732 if (cmd.compose_mailto)
733 compose_str = g_strdup_printf
734 ("compose %s\n", cmd.compose_mailto);
736 compose_str = g_strdup("compose\n");
738 fd_write_all(uxsock, compose_str, strlen(compose_str));
740 } else if (cmd.send) {
741 fd_write_all(uxsock, "send\n", 5);
742 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
743 fd_write(uxsock, "online\n", 6);
744 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
745 fd_write(uxsock, "offline\n", 7);
746 } else if (cmd.status || cmd.status_full) {
749 const gchar *command;
753 command = cmd.status_full ? "status-full\n" : "status\n";
754 folders = cmd.status_full ? cmd.status_full_folders :
757 fd_write_all(uxsock, command, strlen(command));
758 for (i = 0; folders && i < folders->len; ++i) {
759 folder = g_ptr_array_index(folders, i);
760 fd_write_all(uxsock, folder, strlen(folder));
761 fd_write_all(uxsock, "\n", 1);
763 fd_write_all(uxsock, ".\n", 2);
765 fd_gets(uxsock, buf, sizeof(buf));
766 if (!strncmp(buf, ".\n", 2)) break;
770 fd_write_all(uxsock, "popup\n", 6);
776 static gint lock_socket_remove(void)
780 if (lock_socket < 0) return -1;
782 if (lock_socket_tag > 0)
783 gdk_input_remove(lock_socket_tag);
784 fd_close(lock_socket);
785 filename = get_socket_name();
791 static GPtrArray *get_folder_item_list(gint sock)
795 GPtrArray *folders = NULL;
798 fd_gets(sock, buf, sizeof(buf));
799 if (!strncmp(buf, ".\n", 2)) break;
801 if (!folders) folders = g_ptr_array_new();
802 item = folder_find_item_from_identifier(buf);
804 g_ptr_array_add(folders, item);
806 g_warning("no such folder: %s\n", buf);
812 static void lock_socket_input_cb(gpointer data,
814 GdkInputCondition condition)
816 MainWindow *mainwin = (MainWindow *)data;
820 sock = fd_accept(source);
821 fd_gets(sock, buf, sizeof(buf));
823 if (!strncmp(buf, "popup", 5)) {
824 main_window_popup(mainwin);
825 } else if (!strncmp(buf, "receive_all", 11)) {
826 inc_all_account_mail(mainwin, FALSE,
827 prefs_common.newmail_notify_manu);
828 } else if (!strncmp(buf, "receive", 7)) {
829 inc_mail(mainwin, prefs_common.newmail_notify_manu);
830 } else if (!strncmp(buf, "compose_attach", 14)) {
834 mailto = g_strdup(buf + strlen("compose_attach") + 1);
835 files = g_ptr_array_new();
836 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
837 if (buf[0] == '.' && buf[1] == '\n') break;
839 g_ptr_array_add(files, g_strdup(buf));
841 open_compose_new(mailto, files);
842 ptr_array_free_strings(files);
843 g_ptr_array_free(files, TRUE);
845 } else if (!strncmp(buf, "compose", 7)) {
846 open_compose_new(buf + strlen("compose") + 1, NULL);
847 } else if (!strncmp(buf, "send", 4)) {
849 } else if (!strncmp(buf, "online", 6)) {
850 main_window_toggle_work_offline(mainwin, FALSE);
851 } else if (!strncmp(buf, "offline", 7)) {
852 main_window_toggle_work_offline(mainwin, TRUE);
853 } else if (!strncmp(buf, "status-full", 11) ||
854 !strncmp(buf, "status", 6)) {
858 folders = get_folder_item_list(sock);
859 status = folder_get_status
860 (folders, !strncmp(buf, "status-full", 11));
861 fd_write_all(sock, status, strlen(status));
862 fd_write_all(sock, ".\n", 2);
864 if (folders) g_ptr_array_free(folders, TRUE);
870 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
875 Xstrdup_a(addr, address, return);
879 compose_new(NULL, addr, attach_files);
882 static void send_queue(void)
886 for (list = folder_get_list(); list != NULL; list = list->next) {
887 Folder *folder = list->data;
890 gint res = procmsg_send_queue
891 (folder->queue, prefs_common.savemsg);
894 alertpanel_error(_("Some errors occurred while sending queued messages."));
896 folder_item_scan(folder->queue);
901 static void quit_signal_handler(int sig)
903 debug_print("Quitting on signal %d\n", sig);
905 g_timeout_add(0, clean_quit, NULL);
908 static void install_basic_sighandlers()
911 struct sigaction act;
916 sigaddset(&mask, SIGTERM);
919 sigaddset(&mask, SIGINT);
922 act.sa_handler = quit_signal_handler;
927 sigaction(SIGTERM, &act, 0);
930 sigaction(SIGINT, &act, 0);
933 sigprocmask(SIG_UNBLOCK, &mask, 0);