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"
55 #include "prefs_display_header.h"
60 #include "manage_window.h"
61 #include "alertpanel.h"
62 #include "statusbar.h"
63 #include "addressbook.h"
71 #include "prefs_toolbar.h"
90 static gint lock_socket = -1;
91 static gint lock_socket_tag = 0;
95 ONLINE_MODE_DONT_CHANGE,
100 static struct RemoteCmd {
102 gboolean receive_all;
104 const gchar *compose_mailto;
105 GPtrArray *attach_files;
107 gboolean status_full;
108 GPtrArray *status_folders;
109 GPtrArray *status_full_folders;
116 static void parse_cmd_opt(int argc, char *argv[]);
119 static void idle_function_for_gpgme(void);
120 #endif /* USE_GPGME */
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);
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();
252 if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) !=
253 GPGME_No_Error) { /* Also does some gpgme init */
254 rfc2015_disable_all();
255 debug_print("gpgme_engine_version:\n%s\n",
256 gpgme_get_engine_info());
258 if (prefs_common.gpg_warning) {
261 val = alertpanel_message_with_disable
263 _("GnuPG is not installed properly, or needs to be upgraded.\n"
264 "OpenPGP support disabled."));
265 if (val & G_ALERTDISABLE)
266 prefs_common.gpg_warning = FALSE;
271 gpgme_register_idle(idle_function_for_gpgme);
275 gtkaspell_checkers_init();
278 sock_set_io_timeout(prefs_common.io_timeout_secs);
280 prefs_actions_read_config();
281 prefs_display_header_read_config();
282 /* prefs_filtering_read_config(); */
283 addressbook_read_file();
284 renderer_read_config();
288 mainwin = main_window_create
289 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
290 folderview = mainwin->folderview;
292 /* register the callback of unix domain socket input */
293 lock_socket_tag = gdk_input_add(lock_socket,
294 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
295 lock_socket_input_cb,
298 account_read_config_all();
300 if (folder_read_list() < 0) {
304 if (!account_get_list()) {
309 account_set_missing_folder();
310 folder_set_missing_folders();
311 folderview_set(folderview);
313 /* prefs_scoring_read_config(); */
314 prefs_matcher_read_config();
316 /* make one all-folder processing before using sylpheed */
317 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
319 /* if Sylpheed crashed, rebuild caches */
320 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
321 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
322 folderview_check_new(NULL);
324 /* make the crash-indicator file */
325 str_write_to_file("foo", get_crashfile_name());
327 addressbook_read_file();
329 inc_autocheck_timer_init(mainwin);
331 /* ignore SIGPIPE signal for preventing sudden death of program */
332 signal(SIGPIPE, SIG_IGN);
334 if (cmd.receive_all || prefs_common.chk_on_startup)
335 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
336 else if (cmd.receive)
337 inc_mail(mainwin, prefs_common.newmail_notify_manu);
339 gtk_widget_grab_focus(folderview->ctree);
342 open_compose_new(cmd.compose_mailto, cmd.attach_files);
343 if (cmd.attach_files) {
344 ptr_array_free_strings(cmd.attach_files);
345 g_ptr_array_free(cmd.attach_files, TRUE);
346 cmd.attach_files = NULL;
350 if (cmd.status_folders) {
351 g_ptr_array_free(cmd.status_folders, TRUE);
352 cmd.status_folders = NULL;
354 if (cmd.status_full_folders) {
355 g_ptr_array_free(cmd.status_full_folders, TRUE);
356 cmd.status_full_folders = NULL;
359 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
360 main_window_toggle_work_offline(mainwin, TRUE);
361 if (cmd.online_mode == ONLINE_MODE_ONLINE)
362 main_window_toggle_work_offline(mainwin, FALSE);
364 prefs_toolbar_init();
366 plugin_load_all("GTK");
368 static_mainwindow = mainwin;
371 plugin_unload_all("GTK");
373 prefs_toolbar_done();
375 addressbook_destroy();
378 gtkaspell_checkers_quit();
385 static void parse_cmd_opt(int argc, char *argv[])
389 for (i = 1; i < argc; i++) {
390 if (!strncmp(argv[i], "--receive-all", 13))
391 cmd.receive_all = TRUE;
392 else if (!strncmp(argv[i], "--receive", 9))
394 else if (!strncmp(argv[i], "--compose", 9)) {
395 const gchar *p = argv[i + 1];
398 cmd.compose_mailto = NULL;
399 if (p && *p != '\0' && *p != '-') {
400 if (!strncmp(p, "mailto:", 7))
401 cmd.compose_mailto = p + 7;
403 cmd.compose_mailto = p;
406 } else if (!strncmp(argv[i], "--attach", 8)) {
407 const gchar *p = argv[i + 1];
410 while (p && *p != '\0' && *p != '-') {
411 if (!cmd.attach_files)
412 cmd.attach_files = g_ptr_array_new();
413 if (*p != G_DIR_SEPARATOR)
414 file = g_strconcat(sylpheed_get_startup_dir(),
419 g_ptr_array_add(cmd.attach_files, file);
423 } else if (!strncmp(argv[i], "--send", 6)) {
425 } else if (!strncmp(argv[i], "--version", 9)) {
426 puts("Sylpheed version " VERSION);
428 } else if (!strncmp(argv[i], "--status-full", 13)) {
429 const gchar *p = argv[i + 1];
431 cmd.status_full = TRUE;
432 while (p && *p != '\0' && *p != '-') {
433 if (!cmd.status_full_folders)
434 cmd.status_full_folders =
436 g_ptr_array_add(cmd.status_full_folders,
441 } else if (!strncmp(argv[i], "--status", 8)) {
442 const gchar *p = argv[i + 1];
445 while (p && *p != '\0' && *p != '-') {
446 if (!cmd.status_folders)
447 cmd.status_folders = g_ptr_array_new();
448 g_ptr_array_add(cmd.status_folders,
453 } else if (!strncmp(argv[i], "--online", 8)) {
454 cmd.online_mode = ONLINE_MODE_ONLINE;
455 } else if (!strncmp(argv[i], "--offline", 9)) {
456 cmd.online_mode = ONLINE_MODE_OFFLINE;
457 } else if (!strncmp(argv[i], "--help", 6)) {
458 g_print(_("Usage: %s [OPTION]...\n"),
459 g_basename(argv[0]));
461 puts(_(" --compose [address] open composition window"));
462 puts(_(" --attach file1 [file2]...\n"
463 " open composition window with specified files\n"
465 puts(_(" --receive receive new messages"));
466 puts(_(" --receive-all receive new messages of all accounts"));
467 puts(_(" --send send all queued messages"));
468 puts(_(" --status [folder]... show the total number of messages"));
469 puts(_(" --status-full [folder]...\n"
470 " show the status of each folder"));
471 puts(_(" --online switch to online mode"));
472 puts(_(" --offline switch to offline mode"));
473 puts(_(" --debug debug mode"));
474 puts(_(" --help display this help and exit"));
475 puts(_(" --version output version information and exit"));
476 puts(_(" --config-dir output configuration directory"));
479 } else if (!strncmp(argv[i], "--crash", 7)) {
481 cmd.crash_params = g_strdup(argv[i + 1]);
483 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
490 if (cmd.attach_files && cmd.compose == FALSE) {
492 cmd.compose_mailto = NULL;
496 static gint get_queued_message_num(void)
500 queue = folder_get_default_queue();
501 if (!queue) return -1;
503 folder_item_scan(queue);
504 return queue->total_msgs;
507 static void save_all_caches(FolderItem *item, gpointer data)
511 folder_item_write_cache(item);
514 static void initial_processing(FolderItem *item, gpointer data)
516 MainWindow *mainwin = (MainWindow *)data;
519 g_return_if_fail(item);
520 buf = g_strdup_printf(_("Processing (%s)..."),
523 : _("top level folder"));
524 debug_print("%s\n", buf);
527 main_window_cursor_wait(mainwin);
529 folder_item_apply_processing(item);
531 debug_print("done.\n");
532 STATUSBAR_POP(mainwin);
533 main_window_cursor_normal(mainwin);
536 static void draft_all_messages(void)
538 GList *compose_list = compose_get_compose_list();
542 for (elem = compose_list; elem != NULL && elem->data != NULL;
544 Compose *c = (Compose*)elem->data;
550 void clean_quit(void)
552 /*!< Good idea to have the main window stored in a
553 * static variable so we can check that variable
554 * to see if we're really allowed to do things
555 * that actually the spawner is supposed to
556 * do (like: sending mail, composing messages).
557 * Because, really, if we're the spawnee, and
558 * we touch GTK stuff, we're hosed. See the
561 /* FIXME: Use something else to signal that we're
562 * in the original spawner, and not in a spawned
564 if (!static_mainwindow)
567 draft_all_messages();
569 if (prefs_common.warn_queued_on_exit) {
570 /* disable the popup */
571 prefs_common.warn_queued_on_exit = FALSE;
572 app_will_exit(NULL, static_mainwindow);
573 prefs_common.warn_queued_on_exit = TRUE;
574 prefs_common_save_config();
576 app_will_exit(NULL, static_mainwindow);
581 void app_will_exit(GtkWidget *widget, gpointer data)
583 MainWindow *mainwin = data;
587 if (compose_get_compose_list()) {
588 gint val = alertpanel(_("Notice"),
589 _("Composing message exists."),
590 _("Draft them"), _("Discard them"), _("Don't quit"));
594 case G_ALERTALTERNATE:
597 draft_all_messages();
600 manage_window_focus_in(mainwin->window, NULL, NULL);
603 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
604 if (alertpanel(_("Queued messages"),
605 _("Some unsent messages are queued. Exit now?"),
606 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
608 manage_window_focus_in(mainwin->window, NULL, NULL);
611 inc_autocheck_timer_remove();
614 gpgmegtk_free_passphrase();
617 if (prefs_common.clean_on_exit)
618 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
620 /* save prefs for opened folder */
621 if(mainwin->folderview->opened)
625 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
626 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
629 /* save all state before exiting */
631 folder_func_to_all_folders(save_all_caches, NULL);
632 for (list = folder_get_list(); list != NULL; list = g_list_next(list)) {
633 Folder *folder = FOLDER(list->data);
635 folder_tree_destroy(folder);
638 main_window_get_size(mainwin);
639 main_window_get_position(mainwin);
640 prefs_common_save_config();
641 account_save_config_all();
642 addressbook_export_to_file();
644 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
645 gtk_item_factory_dump_rc(filename, NULL, TRUE);
648 /* delete temporary files */
649 remove_all_files(get_mime_tmp_dir());
653 /* delete crashfile */
655 unlink(get_crashfile_name());
657 lock_socket_remove();
663 static void idle_function_for_gpgme(void)
665 while (gtk_events_pending())
666 gtk_main_iteration();
668 #endif /* USE_GPGME */
671 * CLAWS: want this public so crash dialog can delete the
677 gchar *get_socket_name(void)
679 static gchar *filename = NULL;
681 if (filename == NULL) {
682 filename = g_strdup_printf("%s%csylpheed-%d",
683 g_get_tmp_dir(), G_DIR_SEPARATOR,
690 static gchar *get_crashfile_name(void)
692 static gchar *filename = NULL;
694 if (filename == NULL) {
695 filename = g_strdup_printf("%s%csylpheed-crashed",
696 get_tmp_dir(), G_DIR_SEPARATOR);
702 static gint prohibit_duplicate_launch(void)
707 path = get_socket_name();
708 uxsock = fd_connect_unix(path);
711 return fd_open_unix(path);
714 /* remote command mode */
716 debug_print("another Sylpheed is already running.\n");
719 fd_write_all(uxsock, "receive_all\n", 12);
720 else if (cmd.receive)
721 fd_write_all(uxsock, "receive\n", 8);
722 else if (cmd.compose && cmd.attach_files) {
723 gchar *str, *compose_str;
726 if (cmd.compose_mailto)
727 compose_str = g_strdup_printf("compose_attach %s\n",
730 compose_str = g_strdup("compose_attach\n");
732 fd_write_all(uxsock, compose_str, strlen(compose_str));
735 for (i = 0; i < cmd.attach_files->len; i++) {
736 str = g_ptr_array_index(cmd.attach_files, i);
737 fd_write_all(uxsock, str, strlen(str));
738 fd_write_all(uxsock, "\n", 1);
741 fd_write_all(uxsock, ".\n", 2);
742 } else if (cmd.compose) {
745 if (cmd.compose_mailto)
746 compose_str = g_strdup_printf
747 ("compose %s\n", cmd.compose_mailto);
749 compose_str = g_strdup("compose\n");
751 fd_write_all(uxsock, compose_str, strlen(compose_str));
753 } else if (cmd.send) {
754 fd_write_all(uxsock, "send\n", 5);
755 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
756 fd_write(uxsock, "online\n", 6);
757 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
758 fd_write(uxsock, "offline\n", 7);
759 } else if (cmd.status || cmd.status_full) {
762 const gchar *command;
766 command = cmd.status_full ? "status-full\n" : "status\n";
767 folders = cmd.status_full ? cmd.status_full_folders :
770 fd_write_all(uxsock, command, strlen(command));
771 for (i = 0; folders && i < folders->len; ++i) {
772 folder = g_ptr_array_index(folders, i);
773 fd_write_all(uxsock, folder, strlen(folder));
774 fd_write_all(uxsock, "\n", 1);
776 fd_write_all(uxsock, ".\n", 2);
778 fd_gets(uxsock, buf, sizeof(buf));
779 if (!strncmp(buf, ".\n", 2)) break;
783 fd_write_all(uxsock, "popup\n", 6);
789 static gint lock_socket_remove(void)
793 if (lock_socket < 0) return -1;
795 if (lock_socket_tag > 0)
796 gdk_input_remove(lock_socket_tag);
797 fd_close(lock_socket);
798 filename = get_socket_name();
804 static GPtrArray *get_folder_item_list(gint sock)
808 GPtrArray *folders = NULL;
811 fd_gets(sock, buf, sizeof(buf));
812 if (!strncmp(buf, ".\n", 2)) break;
814 if (!folders) folders = g_ptr_array_new();
815 item = folder_find_item_from_identifier(buf);
817 g_ptr_array_add(folders, item);
819 g_warning("no such folder: %s\n", buf);
825 static void lock_socket_input_cb(gpointer data,
827 GdkInputCondition condition)
829 MainWindow *mainwin = (MainWindow *)data;
833 sock = fd_accept(source);
834 fd_gets(sock, buf, sizeof(buf));
836 if (!strncmp(buf, "popup", 5)) {
837 main_window_popup(mainwin);
838 } else if (!strncmp(buf, "receive_all", 11)) {
839 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
840 } else if (!strncmp(buf, "receive", 7)) {
841 inc_mail(mainwin, prefs_common.newmail_notify_manu);
842 } else if (!strncmp(buf, "compose_attach", 14)) {
846 mailto = g_strdup(buf + strlen("compose_attach") + 1);
847 files = g_ptr_array_new();
848 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
849 if (buf[0] == '.' && buf[1] == '\n') break;
851 g_ptr_array_add(files, g_strdup(buf));
853 open_compose_new(mailto, files);
854 ptr_array_free_strings(files);
855 g_ptr_array_free(files, TRUE);
857 } else if (!strncmp(buf, "compose", 7)) {
858 open_compose_new(buf + strlen("compose") + 1, NULL);
859 } else if (!strncmp(buf, "send", 4)) {
861 } else if (!strncmp(buf, "online", 6)) {
862 main_window_toggle_work_offline(mainwin, FALSE);
863 } else if (!strncmp(buf, "offline", 7)) {
864 main_window_toggle_work_offline(mainwin, TRUE);
865 } else if (!strncmp(buf, "status-full", 11) ||
866 !strncmp(buf, "status", 6)) {
870 folders = get_folder_item_list(sock);
871 status = folder_get_status
872 (folders, !strncmp(buf, "status-full", 11));
873 fd_write_all(sock, status, strlen(status));
874 fd_write_all(sock, ".\n", 2);
876 if (folders) g_ptr_array_free(folders, TRUE);
882 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
887 Xstrdup_a(addr, address, return);
891 compose_new(NULL, addr, attach_files);
894 static void send_queue(void)
897 FolderItem *def_outbox;
899 def_outbox = folder_get_default_outbox();
901 for (list = folder_get_list(); list != NULL; list = list->next) {
902 Folder *folder = list->data;
905 gint res = procmsg_send_queue
906 (folder->queue, prefs_common.savemsg);
909 alertpanel_error(_("Some errors occurred while sending queued messages."));
911 folder_item_scan(folder->queue);
912 if (prefs_common.savemsg && folder->outbox) {
913 if (folder->outbox == def_outbox)
920 static void quit_signal_handler(int sig)
922 debug_print("Quitting on signal %d\n", sig);
926 static void install_basic_sighandlers()
929 struct sigaction act;
934 sigaddset(&mask, SIGTERM);
937 sigaddset(&mask, SIGINT);
940 act.sa_handler = quit_signal_handler;
945 sigaction(SIGTERM, &act, 0);
948 sigaction(SIGINT, &act, 0);
951 sigprocmask(SIG_UNBLOCK, &mask, 0);