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",
224 gtk_rc_parse(userrc);
226 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
227 G_DIR_SEPARATOR_S, "gtkrc", NULL);
228 gtk_rc_parse(userrc);
230 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
231 gtk_rc_parse(userrc);
234 gtk_rc_parse("./gtkrc");
236 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
237 gtk_item_factory_parse_rc(userrc);
240 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
242 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
243 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
244 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
245 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
246 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
247 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
249 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
251 folder_system_init();
253 prefs_common_read_config();
261 prefs_ext_prog_init();
263 gtkaspell_checkers_init();
264 prefs_spelling_init();
267 sock_set_io_timeout(prefs_common.io_timeout_secs);
269 prefs_actions_read_config();
270 prefs_display_header_read_config();
271 /* prefs_filtering_read_config(); */
272 addressbook_read_file();
273 renderer_read_config();
277 folderview_initialize();
281 mainwin = main_window_create
282 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
283 folderview = mainwin->folderview;
285 /* register the callback of unix domain socket input */
286 lock_socket_tag = gdk_input_add(lock_socket,
287 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
288 lock_socket_input_cb,
291 account_read_config_all();
293 if (folder_read_list() < 0) {
297 if (!account_get_list()) {
302 account_set_missing_folder();
303 folder_set_missing_folders();
304 folderview_set(folderview);
306 prefs_matcher_read_config();
308 /* make one all-folder processing before using sylpheed */
309 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
311 /* if Sylpheed crashed, rebuild caches */
312 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
313 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
314 folderview_check_new(NULL);
316 /* make the crash-indicator file */
317 str_write_to_file("foo", get_crashfile_name());
319 addressbook_read_file();
321 inc_autocheck_timer_init(mainwin);
323 /* ignore SIGPIPE signal for preventing sudden death of program */
324 signal(SIGPIPE, SIG_IGN);
326 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
327 main_window_toggle_work_offline(mainwin, TRUE);
328 if (cmd.online_mode == ONLINE_MODE_ONLINE)
329 main_window_toggle_work_offline(mainwin, FALSE);
332 inc_all_account_mail(mainwin, FALSE,
333 prefs_common.newmail_notify_manu);
334 else if (prefs_common.chk_on_startup)
335 inc_all_account_mail(mainwin, TRUE,
336 prefs_common.newmail_notify_manu);
337 else if (cmd.receive)
338 inc_mail(mainwin, prefs_common.newmail_notify_manu);
340 gtk_widget_grab_focus(folderview->ctree);
343 open_compose_new(cmd.compose_mailto, cmd.attach_files);
344 if (cmd.attach_files) {
345 ptr_array_free_strings(cmd.attach_files);
346 g_ptr_array_free(cmd.attach_files, TRUE);
347 cmd.attach_files = NULL;
351 if (cmd.status_folders) {
352 g_ptr_array_free(cmd.status_folders, TRUE);
353 cmd.status_folders = NULL;
355 if (cmd.status_full_folders) {
356 g_ptr_array_free(cmd.status_full_folders, TRUE);
357 cmd.status_full_folders = NULL;
360 prefs_toolbar_init();
362 plugin_load_all("GTK");
364 static_mainwindow = mainwin;
367 exit_sylpheed(mainwin);
372 static void save_all_caches(FolderItem *item, gpointer data)
376 folder_item_write_cache(item);
379 static void exit_sylpheed(MainWindow *mainwin)
383 debug_print("shutting down\n");
385 inc_autocheck_timer_remove();
387 if (prefs_common.clean_on_exit)
388 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
390 /* save prefs for opened folder */
391 if(mainwin->folderview->opened)
395 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
396 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
399 /* save all state before exiting */
401 folder_func_to_all_folders(save_all_caches, NULL);
403 main_window_get_size(mainwin);
404 main_window_get_position(mainwin);
405 prefs_common_save_config();
406 account_save_config_all();
407 addressbook_export_to_file();
409 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
410 gtk_item_factory_dump_rc(filename, NULL, TRUE);
413 /* delete temporary files */
414 remove_all_files(get_mime_tmp_dir());
418 /* delete crashfile */
420 unlink(get_crashfile_name());
422 lock_socket_remove();
424 main_window_destroy(mainwin);
426 plugin_unload_all("GTK");
428 prefs_toolbar_done();
430 addressbook_destroy();
438 prefs_ext_prog_done();
440 prefs_spelling_done();
441 gtkaspell_checkers_quit();
447 static void parse_cmd_opt(int argc, char *argv[])
451 for (i = 1; i < argc; i++) {
452 if (!strncmp(argv[i], "--receive-all", 13))
453 cmd.receive_all = TRUE;
454 else if (!strncmp(argv[i], "--receive", 9))
456 else if (!strncmp(argv[i], "--compose", 9)) {
457 const gchar *p = argv[i + 1];
460 cmd.compose_mailto = NULL;
461 if (p && *p != '\0' && *p != '-') {
462 if (!strncmp(p, "mailto:", 7))
463 cmd.compose_mailto = p + 7;
465 cmd.compose_mailto = p;
468 } else if (!strncmp(argv[i], "--attach", 8)) {
469 const gchar *p = argv[i + 1];
472 while (p && *p != '\0' && *p != '-') {
473 if (!cmd.attach_files)
474 cmd.attach_files = g_ptr_array_new();
475 if (*p != G_DIR_SEPARATOR)
476 file = g_strconcat(sylpheed_get_startup_dir(),
481 g_ptr_array_add(cmd.attach_files, file);
485 } else if (!strncmp(argv[i], "--send", 6)) {
487 } else if (!strncmp(argv[i], "--version", 9)) {
488 puts("Sylpheed version " VERSION);
490 } else if (!strncmp(argv[i], "--status-full", 13)) {
491 const gchar *p = argv[i + 1];
493 cmd.status_full = TRUE;
494 while (p && *p != '\0' && *p != '-') {
495 if (!cmd.status_full_folders)
496 cmd.status_full_folders =
498 g_ptr_array_add(cmd.status_full_folders,
503 } else if (!strncmp(argv[i], "--status", 8)) {
504 const gchar *p = argv[i + 1];
507 while (p && *p != '\0' && *p != '-') {
508 if (!cmd.status_folders)
509 cmd.status_folders = g_ptr_array_new();
510 g_ptr_array_add(cmd.status_folders,
515 } else if (!strncmp(argv[i], "--online", 8)) {
516 cmd.online_mode = ONLINE_MODE_ONLINE;
517 } else if (!strncmp(argv[i], "--offline", 9)) {
518 cmd.online_mode = ONLINE_MODE_OFFLINE;
519 } else if (!strncmp(argv[i], "--help", 6)) {
520 g_print(_("Usage: %s [OPTION]...\n"),
521 g_basename(argv[0]));
523 puts(_(" --compose [address] open composition window"));
524 puts(_(" --attach file1 [file2]...\n"
525 " open composition window with specified files\n"
527 puts(_(" --receive receive new messages"));
528 puts(_(" --receive-all receive new messages of all accounts"));
529 puts(_(" --send send all queued messages"));
530 puts(_(" --status [folder]... show the total number of messages"));
531 puts(_(" --status-full [folder]...\n"
532 " show the status of each folder"));
533 puts(_(" --online switch to online mode"));
534 puts(_(" --offline switch to offline mode"));
535 puts(_(" --debug debug mode"));
536 puts(_(" --help display this help and exit"));
537 puts(_(" --version output version information and exit"));
538 puts(_(" --config-dir output configuration directory"));
541 } else if (!strncmp(argv[i], "--crash", 7)) {
543 cmd.crash_params = g_strdup(argv[i + 1]);
545 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
552 if (cmd.attach_files && cmd.compose == FALSE) {
554 cmd.compose_mailto = NULL;
558 static gint get_queued_message_num(void)
562 queue = folder_get_default_queue();
563 if (!queue) return -1;
565 folder_item_scan(queue);
566 return queue->total_msgs;
569 static void initial_processing(FolderItem *item, gpointer data)
571 MainWindow *mainwin = (MainWindow *)data;
574 g_return_if_fail(item);
575 buf = g_strdup_printf(_("Processing (%s)..."),
578 : _("top level folder"));
579 debug_print("%s\n", buf);
582 main_window_cursor_wait(mainwin);
584 if (item->prefs->enable_processing)
585 folder_item_apply_processing(item);
587 debug_print("done.\n");
588 STATUSBAR_POP(mainwin);
589 main_window_cursor_normal(mainwin);
592 static void draft_all_messages(void)
594 GList *compose_list = compose_get_compose_list();
598 for (elem = compose_list; elem != NULL && elem->data != NULL;
600 Compose *c = (Compose*)elem->data;
606 gboolean clean_quit(gpointer data)
608 static gboolean firstrun = TRUE;
614 /*!< Good idea to have the main window stored in a
615 * static variable so we can check that variable
616 * to see if we're really allowed to do things
617 * that actually the spawner is supposed to
618 * do (like: sending mail, composing messages).
619 * Because, really, if we're the spawnee, and
620 * we touch GTK stuff, we're hosed. See the
623 /* FIXME: Use something else to signal that we're
624 * in the original spawner, and not in a spawned
626 if (!static_mainwindow)
629 draft_all_messages();
631 exit_sylpheed(static_mainwindow);
637 void app_will_exit(GtkWidget *widget, gpointer data)
639 MainWindow *mainwin = data;
641 if (compose_get_compose_list()) {
642 gint val = alertpanel(_("Notice"),
643 _("Composing message exists."),
644 _("Draft them"), _("Discard them"), _("Don't quit"));
648 case G_ALERTALTERNATE:
651 draft_all_messages();
654 manage_window_focus_in(mainwin->window, NULL, NULL);
657 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
658 if (alertpanel(_("Queued messages"),
659 _("Some unsent messages are queued. Exit now?"),
660 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
662 manage_window_focus_in(mainwin->window, NULL, NULL);
668 * CLAWS: want this public so crash dialog can delete the
674 gchar *get_socket_name(void)
676 static gchar *filename = NULL;
678 if (filename == NULL) {
679 filename = g_strdup_printf("%s%csylpheed-%d",
680 g_get_tmp_dir(), G_DIR_SEPARATOR,
687 static gchar *get_crashfile_name(void)
689 static gchar *filename = NULL;
691 if (filename == NULL) {
692 filename = g_strdup_printf("%s%csylpheed-crashed",
693 get_tmp_dir(), G_DIR_SEPARATOR);
699 static gint prohibit_duplicate_launch(void)
704 path = get_socket_name();
705 uxsock = fd_connect_unix(path);
708 return fd_open_unix(path);
711 /* remote command mode */
713 debug_print("another Sylpheed is already running.\n");
716 fd_write_all(uxsock, "receive_all\n", 12);
717 else if (cmd.receive)
718 fd_write_all(uxsock, "receive\n", 8);
719 else if (cmd.compose && cmd.attach_files) {
720 gchar *str, *compose_str;
723 if (cmd.compose_mailto)
724 compose_str = g_strdup_printf("compose_attach %s\n",
727 compose_str = g_strdup("compose_attach\n");
729 fd_write_all(uxsock, compose_str, strlen(compose_str));
732 for (i = 0; i < cmd.attach_files->len; i++) {
733 str = g_ptr_array_index(cmd.attach_files, i);
734 fd_write_all(uxsock, str, strlen(str));
735 fd_write_all(uxsock, "\n", 1);
738 fd_write_all(uxsock, ".\n", 2);
739 } else if (cmd.compose) {
742 if (cmd.compose_mailto)
743 compose_str = g_strdup_printf
744 ("compose %s\n", cmd.compose_mailto);
746 compose_str = g_strdup("compose\n");
748 fd_write_all(uxsock, compose_str, strlen(compose_str));
750 } else if (cmd.send) {
751 fd_write_all(uxsock, "send\n", 5);
752 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
753 fd_write(uxsock, "online\n", 6);
754 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
755 fd_write(uxsock, "offline\n", 7);
756 } else if (cmd.status || cmd.status_full) {
759 const gchar *command;
763 command = cmd.status_full ? "status-full\n" : "status\n";
764 folders = cmd.status_full ? cmd.status_full_folders :
767 fd_write_all(uxsock, command, strlen(command));
768 for (i = 0; folders && i < folders->len; ++i) {
769 folder = g_ptr_array_index(folders, i);
770 fd_write_all(uxsock, folder, strlen(folder));
771 fd_write_all(uxsock, "\n", 1);
773 fd_write_all(uxsock, ".\n", 2);
775 fd_gets(uxsock, buf, sizeof(buf));
776 if (!strncmp(buf, ".\n", 2)) break;
780 fd_write_all(uxsock, "popup\n", 6);
786 static gint lock_socket_remove(void)
790 if (lock_socket < 0) return -1;
792 if (lock_socket_tag > 0)
793 gdk_input_remove(lock_socket_tag);
794 fd_close(lock_socket);
795 filename = get_socket_name();
801 static GPtrArray *get_folder_item_list(gint sock)
805 GPtrArray *folders = NULL;
808 fd_gets(sock, buf, sizeof(buf));
809 if (!strncmp(buf, ".\n", 2)) break;
811 if (!folders) folders = g_ptr_array_new();
812 item = folder_find_item_from_identifier(buf);
814 g_ptr_array_add(folders, item);
816 g_warning("no such folder: %s\n", buf);
822 static void lock_socket_input_cb(gpointer data,
824 GdkInputCondition condition)
826 MainWindow *mainwin = (MainWindow *)data;
830 sock = fd_accept(source);
831 fd_gets(sock, buf, sizeof(buf));
833 if (!strncmp(buf, "popup", 5)) {
834 main_window_popup(mainwin);
835 } else if (!strncmp(buf, "receive_all", 11)) {
836 inc_all_account_mail(mainwin, FALSE,
837 prefs_common.newmail_notify_manu);
838 } else if (!strncmp(buf, "receive", 7)) {
839 inc_mail(mainwin, prefs_common.newmail_notify_manu);
840 } else if (!strncmp(buf, "compose_attach", 14)) {
844 mailto = g_strdup(buf + strlen("compose_attach") + 1);
845 files = g_ptr_array_new();
846 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
847 if (buf[0] == '.' && buf[1] == '\n') break;
849 g_ptr_array_add(files, g_strdup(buf));
851 open_compose_new(mailto, files);
852 ptr_array_free_strings(files);
853 g_ptr_array_free(files, TRUE);
855 } else if (!strncmp(buf, "compose", 7)) {
856 open_compose_new(buf + strlen("compose") + 1, NULL);
857 } else if (!strncmp(buf, "send", 4)) {
859 } else if (!strncmp(buf, "online", 6)) {
860 main_window_toggle_work_offline(mainwin, FALSE);
861 } else if (!strncmp(buf, "offline", 7)) {
862 main_window_toggle_work_offline(mainwin, TRUE);
863 } else if (!strncmp(buf, "status-full", 11) ||
864 !strncmp(buf, "status", 6)) {
868 folders = get_folder_item_list(sock);
869 status = folder_get_status
870 (folders, !strncmp(buf, "status-full", 11));
871 fd_write_all(sock, status, strlen(status));
872 fd_write_all(sock, ".\n", 2);
874 if (folders) g_ptr_array_free(folders, TRUE);
880 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
885 Xstrdup_a(addr, address, return);
889 compose_new(NULL, addr, attach_files);
892 static void send_queue(void)
896 for (list = folder_get_list(); list != NULL; list = list->next) {
897 Folder *folder = list->data;
900 gint res = procmsg_send_queue
901 (folder->queue, prefs_common.savemsg);
904 alertpanel_error(_("Some errors occurred while sending queued messages."));
906 folder_item_scan(folder->queue);
911 static void quit_signal_handler(int sig)
913 debug_print("Quitting on signal %d\n", sig);
915 g_timeout_add(0, clean_quit, NULL);
918 static void install_basic_sighandlers()
921 struct sigaction act;
926 sigaddset(&mask, SIGTERM);
929 sigaddset(&mask, SIGINT);
932 act.sa_handler = quit_signal_handler;
937 sigaction(SIGTERM, &act, 0);
940 sigaction(SIGINT, &act, 0);
943 sigprocmask(SIG_UNBLOCK, &mask, 0);