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_spelling.h"
56 #include "prefs_display_header.h"
61 #include "manage_window.h"
62 #include "alertpanel.h"
63 #include "statusbar.h"
64 #include "addressbook.h"
72 #include "prefs_toolbar.h"
91 static gint lock_socket = -1;
92 static gint lock_socket_tag = 0;
96 ONLINE_MODE_DONT_CHANGE,
101 static struct RemoteCmd {
103 gboolean receive_all;
105 const gchar *compose_mailto;
106 GPtrArray *attach_files;
108 gboolean status_full;
109 GPtrArray *status_folders;
110 GPtrArray *status_full_folders;
117 static void parse_cmd_opt(int argc, char *argv[]);
120 static void idle_function_for_gpgme(void);
121 #endif /* USE_GPGME */
123 static gint prohibit_duplicate_launch (void);
124 static gchar * get_crashfile_name (void);
125 static gint lock_socket_remove (void);
126 static void lock_socket_input_cb (gpointer data,
128 GdkInputCondition condition);
132 gchar *get_socket_name (void);
135 static void open_compose_new (const gchar *address,
136 GPtrArray *attach_files);
138 static void send_queue (void);
139 static void initial_processing (FolderItem *item, gpointer data);
140 static void quit_signal_handler (int sig);
141 static void install_basic_sighandlers (void);
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();
253 if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) !=
254 GPGME_No_Error) { /* Also does some gpgme init */
255 rfc2015_disable_all();
256 debug_print("gpgme_engine_version:\n%s\n",
257 gpgme_get_engine_info());
259 if (prefs_common.gpg_warning) {
262 val = alertpanel_message_with_disable
264 _("GnuPG is not installed properly, or needs to be upgraded.\n"
265 "OpenPGP support disabled."));
266 if (val & G_ALERTDISABLE)
267 prefs_common.gpg_warning = FALSE;
272 gpgme_register_idle(idle_function_for_gpgme);
276 gtkaspell_checkers_init();
277 prefs_spelling_init();
280 sock_set_io_timeout(prefs_common.io_timeout_secs);
282 prefs_actions_read_config();
283 prefs_display_header_read_config();
284 /* prefs_filtering_read_config(); */
285 addressbook_read_file();
286 renderer_read_config();
290 mainwin = main_window_create
291 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
292 folderview = mainwin->folderview;
294 /* register the callback of unix domain socket input */
295 lock_socket_tag = gdk_input_add(lock_socket,
296 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
297 lock_socket_input_cb,
300 account_read_config_all();
302 if (folder_read_list() < 0) {
306 if (!account_get_list()) {
311 account_set_missing_folder();
312 folder_set_missing_folders();
313 folderview_set(folderview);
315 /* prefs_scoring_read_config(); */
316 prefs_matcher_read_config();
318 /* make one all-folder processing before using sylpheed */
319 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
321 /* if Sylpheed crashed, rebuild caches */
322 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
323 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
324 folderview_check_new(NULL);
326 /* make the crash-indicator file */
327 str_write_to_file("foo", get_crashfile_name());
329 addressbook_read_file();
331 inc_autocheck_timer_init(mainwin);
333 /* ignore SIGPIPE signal for preventing sudden death of program */
334 signal(SIGPIPE, SIG_IGN);
336 if (cmd.receive_all || prefs_common.chk_on_startup)
337 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
338 else if (cmd.receive)
339 inc_mail(mainwin, prefs_common.newmail_notify_manu);
341 gtk_widget_grab_focus(folderview->ctree);
344 open_compose_new(cmd.compose_mailto, cmd.attach_files);
345 if (cmd.attach_files) {
346 ptr_array_free_strings(cmd.attach_files);
347 g_ptr_array_free(cmd.attach_files, TRUE);
348 cmd.attach_files = NULL;
352 if (cmd.status_folders) {
353 g_ptr_array_free(cmd.status_folders, TRUE);
354 cmd.status_folders = NULL;
356 if (cmd.status_full_folders) {
357 g_ptr_array_free(cmd.status_full_folders, TRUE);
358 cmd.status_full_folders = NULL;
361 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
362 main_window_toggle_work_offline(mainwin, TRUE);
363 if (cmd.online_mode == ONLINE_MODE_ONLINE)
364 main_window_toggle_work_offline(mainwin, FALSE);
366 prefs_toolbar_init();
368 plugin_load_all("GTK");
370 static_mainwindow = mainwin;
373 plugin_unload_all("GTK");
375 prefs_toolbar_done();
377 addressbook_destroy();
380 prefs_spelling_done();
381 gtkaspell_checkers_quit();
388 static void parse_cmd_opt(int argc, char *argv[])
392 for (i = 1; i < argc; i++) {
393 if (!strncmp(argv[i], "--receive-all", 13))
394 cmd.receive_all = TRUE;
395 else if (!strncmp(argv[i], "--receive", 9))
397 else if (!strncmp(argv[i], "--compose", 9)) {
398 const gchar *p = argv[i + 1];
401 cmd.compose_mailto = NULL;
402 if (p && *p != '\0' && *p != '-') {
403 if (!strncmp(p, "mailto:", 7))
404 cmd.compose_mailto = p + 7;
406 cmd.compose_mailto = p;
409 } else if (!strncmp(argv[i], "--attach", 8)) {
410 const gchar *p = argv[i + 1];
413 while (p && *p != '\0' && *p != '-') {
414 if (!cmd.attach_files)
415 cmd.attach_files = g_ptr_array_new();
416 if (*p != G_DIR_SEPARATOR)
417 file = g_strconcat(sylpheed_get_startup_dir(),
422 g_ptr_array_add(cmd.attach_files, file);
426 } else if (!strncmp(argv[i], "--send", 6)) {
428 } else if (!strncmp(argv[i], "--version", 9)) {
429 puts("Sylpheed version " VERSION);
431 } else if (!strncmp(argv[i], "--status-full", 13)) {
432 const gchar *p = argv[i + 1];
434 cmd.status_full = TRUE;
435 while (p && *p != '\0' && *p != '-') {
436 if (!cmd.status_full_folders)
437 cmd.status_full_folders =
439 g_ptr_array_add(cmd.status_full_folders,
444 } else if (!strncmp(argv[i], "--status", 8)) {
445 const gchar *p = argv[i + 1];
448 while (p && *p != '\0' && *p != '-') {
449 if (!cmd.status_folders)
450 cmd.status_folders = g_ptr_array_new();
451 g_ptr_array_add(cmd.status_folders,
456 } else if (!strncmp(argv[i], "--online", 8)) {
457 cmd.online_mode = ONLINE_MODE_ONLINE;
458 } else if (!strncmp(argv[i], "--offline", 9)) {
459 cmd.online_mode = ONLINE_MODE_OFFLINE;
460 } else if (!strncmp(argv[i], "--help", 6)) {
461 g_print(_("Usage: %s [OPTION]...\n"),
462 g_basename(argv[0]));
464 puts(_(" --compose [address] open composition window"));
465 puts(_(" --attach file1 [file2]...\n"
466 " open composition window with specified files\n"
468 puts(_(" --receive receive new messages"));
469 puts(_(" --receive-all receive new messages of all accounts"));
470 puts(_(" --send send all queued messages"));
471 puts(_(" --status [folder]... show the total number of messages"));
472 puts(_(" --status-full [folder]...\n"
473 " show the status of each folder"));
474 puts(_(" --online switch to online mode"));
475 puts(_(" --offline switch to offline mode"));
476 puts(_(" --debug debug mode"));
477 puts(_(" --help display this help and exit"));
478 puts(_(" --version output version information and exit"));
479 puts(_(" --config-dir output configuration directory"));
482 } else if (!strncmp(argv[i], "--crash", 7)) {
484 cmd.crash_params = g_strdup(argv[i + 1]);
486 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
493 if (cmd.attach_files && cmd.compose == FALSE) {
495 cmd.compose_mailto = NULL;
499 static gint get_queued_message_num(void)
503 queue = folder_get_default_queue();
504 if (!queue) return -1;
506 folder_item_scan(queue);
507 return queue->total_msgs;
510 static void save_all_caches(FolderItem *item, gpointer data)
514 folder_item_write_cache(item);
517 static void initial_processing(FolderItem *item, gpointer data)
519 MainWindow *mainwin = (MainWindow *)data;
522 g_return_if_fail(item);
523 buf = g_strdup_printf(_("Processing (%s)..."),
526 : _("top level folder"));
527 debug_print("%s\n", buf);
530 main_window_cursor_wait(mainwin);
532 folder_item_apply_processing(item);
534 debug_print("done.\n");
535 STATUSBAR_POP(mainwin);
536 main_window_cursor_normal(mainwin);
539 static void draft_all_messages(void)
541 GList *compose_list = compose_get_compose_list();
545 for (elem = compose_list; elem != NULL && elem->data != NULL;
547 Compose *c = (Compose*)elem->data;
553 void clean_quit(void)
555 /*!< Good idea to have the main window stored in a
556 * static variable so we can check that variable
557 * to see if we're really allowed to do things
558 * that actually the spawner is supposed to
559 * do (like: sending mail, composing messages).
560 * Because, really, if we're the spawnee, and
561 * we touch GTK stuff, we're hosed. See the
564 /* FIXME: Use something else to signal that we're
565 * in the original spawner, and not in a spawned
567 if (!static_mainwindow)
570 draft_all_messages();
572 if (prefs_common.warn_queued_on_exit) {
573 /* disable the popup */
574 prefs_common.warn_queued_on_exit = FALSE;
575 app_will_exit(NULL, static_mainwindow);
576 prefs_common.warn_queued_on_exit = TRUE;
577 prefs_common_save_config();
579 app_will_exit(NULL, static_mainwindow);
584 void app_will_exit(GtkWidget *widget, gpointer data)
586 MainWindow *mainwin = data;
590 if (compose_get_compose_list()) {
591 gint val = alertpanel(_("Notice"),
592 _("Composing message exists."),
593 _("Draft them"), _("Discard them"), _("Don't quit"));
597 case G_ALERTALTERNATE:
600 draft_all_messages();
603 manage_window_focus_in(mainwin->window, NULL, NULL);
606 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
607 if (alertpanel(_("Queued messages"),
608 _("Some unsent messages are queued. Exit now?"),
609 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
611 manage_window_focus_in(mainwin->window, NULL, NULL);
614 inc_autocheck_timer_remove();
617 gpgmegtk_free_passphrase();
620 if (prefs_common.clean_on_exit)
621 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
623 /* save prefs for opened folder */
624 if(mainwin->folderview->opened)
628 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
629 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
632 /* save all state before exiting */
634 folder_func_to_all_folders(save_all_caches, NULL);
635 for (list = folder_get_list(); list != NULL; list = g_list_next(list)) {
636 Folder *folder = FOLDER(list->data);
638 folder_tree_destroy(folder);
641 main_window_get_size(mainwin);
642 main_window_get_position(mainwin);
643 prefs_common_save_config();
644 account_save_config_all();
645 addressbook_export_to_file();
647 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
648 gtk_item_factory_dump_rc(filename, NULL, TRUE);
651 /* delete temporary files */
652 remove_all_files(get_mime_tmp_dir());
656 /* delete crashfile */
658 unlink(get_crashfile_name());
660 lock_socket_remove();
666 static void idle_function_for_gpgme(void)
668 while (gtk_events_pending())
669 gtk_main_iteration();
671 #endif /* USE_GPGME */
674 * CLAWS: want this public so crash dialog can delete the
680 gchar *get_socket_name(void)
682 static gchar *filename = NULL;
684 if (filename == NULL) {
685 filename = g_strdup_printf("%s%csylpheed-%d",
686 g_get_tmp_dir(), G_DIR_SEPARATOR,
693 static gchar *get_crashfile_name(void)
695 static gchar *filename = NULL;
697 if (filename == NULL) {
698 filename = g_strdup_printf("%s%csylpheed-crashed",
699 get_tmp_dir(), G_DIR_SEPARATOR);
705 static gint prohibit_duplicate_launch(void)
710 path = get_socket_name();
711 uxsock = fd_connect_unix(path);
714 return fd_open_unix(path);
717 /* remote command mode */
719 debug_print("another Sylpheed is already running.\n");
722 fd_write_all(uxsock, "receive_all\n", 12);
723 else if (cmd.receive)
724 fd_write_all(uxsock, "receive\n", 8);
725 else if (cmd.compose && cmd.attach_files) {
726 gchar *str, *compose_str;
729 if (cmd.compose_mailto)
730 compose_str = g_strdup_printf("compose_attach %s\n",
733 compose_str = g_strdup("compose_attach\n");
735 fd_write_all(uxsock, compose_str, strlen(compose_str));
738 for (i = 0; i < cmd.attach_files->len; i++) {
739 str = g_ptr_array_index(cmd.attach_files, i);
740 fd_write_all(uxsock, str, strlen(str));
741 fd_write_all(uxsock, "\n", 1);
744 fd_write_all(uxsock, ".\n", 2);
745 } else if (cmd.compose) {
748 if (cmd.compose_mailto)
749 compose_str = g_strdup_printf
750 ("compose %s\n", cmd.compose_mailto);
752 compose_str = g_strdup("compose\n");
754 fd_write_all(uxsock, compose_str, strlen(compose_str));
756 } else if (cmd.send) {
757 fd_write_all(uxsock, "send\n", 5);
758 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
759 fd_write(uxsock, "online\n", 6);
760 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
761 fd_write(uxsock, "offline\n", 7);
762 } else if (cmd.status || cmd.status_full) {
765 const gchar *command;
769 command = cmd.status_full ? "status-full\n" : "status\n";
770 folders = cmd.status_full ? cmd.status_full_folders :
773 fd_write_all(uxsock, command, strlen(command));
774 for (i = 0; folders && i < folders->len; ++i) {
775 folder = g_ptr_array_index(folders, i);
776 fd_write_all(uxsock, folder, strlen(folder));
777 fd_write_all(uxsock, "\n", 1);
779 fd_write_all(uxsock, ".\n", 2);
781 fd_gets(uxsock, buf, sizeof(buf));
782 if (!strncmp(buf, ".\n", 2)) break;
786 fd_write_all(uxsock, "popup\n", 6);
792 static gint lock_socket_remove(void)
796 if (lock_socket < 0) return -1;
798 if (lock_socket_tag > 0)
799 gdk_input_remove(lock_socket_tag);
800 fd_close(lock_socket);
801 filename = get_socket_name();
807 static GPtrArray *get_folder_item_list(gint sock)
811 GPtrArray *folders = NULL;
814 fd_gets(sock, buf, sizeof(buf));
815 if (!strncmp(buf, ".\n", 2)) break;
817 if (!folders) folders = g_ptr_array_new();
818 item = folder_find_item_from_identifier(buf);
820 g_ptr_array_add(folders, item);
822 g_warning("no such folder: %s\n", buf);
828 static void lock_socket_input_cb(gpointer data,
830 GdkInputCondition condition)
832 MainWindow *mainwin = (MainWindow *)data;
836 sock = fd_accept(source);
837 fd_gets(sock, buf, sizeof(buf));
839 if (!strncmp(buf, "popup", 5)) {
840 main_window_popup(mainwin);
841 } else if (!strncmp(buf, "receive_all", 11)) {
842 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
843 } else if (!strncmp(buf, "receive", 7)) {
844 inc_mail(mainwin, prefs_common.newmail_notify_manu);
845 } else if (!strncmp(buf, "compose_attach", 14)) {
849 mailto = g_strdup(buf + strlen("compose_attach") + 1);
850 files = g_ptr_array_new();
851 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
852 if (buf[0] == '.' && buf[1] == '\n') break;
854 g_ptr_array_add(files, g_strdup(buf));
856 open_compose_new(mailto, files);
857 ptr_array_free_strings(files);
858 g_ptr_array_free(files, TRUE);
860 } else if (!strncmp(buf, "compose", 7)) {
861 open_compose_new(buf + strlen("compose") + 1, NULL);
862 } else if (!strncmp(buf, "send", 4)) {
864 } else if (!strncmp(buf, "online", 6)) {
865 main_window_toggle_work_offline(mainwin, FALSE);
866 } else if (!strncmp(buf, "offline", 7)) {
867 main_window_toggle_work_offline(mainwin, TRUE);
868 } else if (!strncmp(buf, "status-full", 11) ||
869 !strncmp(buf, "status", 6)) {
873 folders = get_folder_item_list(sock);
874 status = folder_get_status
875 (folders, !strncmp(buf, "status-full", 11));
876 fd_write_all(sock, status, strlen(status));
877 fd_write_all(sock, ".\n", 2);
879 if (folders) g_ptr_array_free(folders, TRUE);
885 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
890 Xstrdup_a(addr, address, return);
894 compose_new(NULL, addr, attach_files);
897 static void send_queue(void)
900 FolderItem *def_outbox;
902 def_outbox = folder_get_default_outbox();
904 for (list = folder_get_list(); list != NULL; list = list->next) {
905 Folder *folder = list->data;
908 gint res = procmsg_send_queue
909 (folder->queue, prefs_common.savemsg);
912 alertpanel_error(_("Some errors occurred while sending queued messages."));
914 folder_item_scan(folder->queue);
915 if (prefs_common.savemsg && folder->outbox) {
916 if (folder->outbox == def_outbox)
923 static void quit_signal_handler(int sig)
925 debug_print("Quitting on signal %d\n", sig);
929 static void install_basic_sighandlers()
932 struct sigaction act;
937 sigaddset(&mask, SIGTERM);
940 sigaddset(&mask, SIGINT);
943 act.sa_handler = quit_signal_handler;
948 sigaction(SIGTERM, &act, 0);
951 sigaction(SIGINT, &act, 0);
954 sigprocmask(SIG_UNBLOCK, &mask, 0);