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);
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();
190 /* check and create unix domain socket */
191 lock_socket = prohibit_duplicate_launch();
192 if (lock_socket < 0) return 0;
194 if (cmd.status || cmd.status_full) {
195 puts("0 Sylpheed not running.");
196 lock_socket_remove();
201 gtk_init(&argc, &argv);
204 gtk_widget_set_default_colormap(gdk_rgb_get_cmap());
205 gtk_widget_set_default_visual(gdk_rgb_get_visual());
207 #if USE_THREADS || USE_LDAP
209 if (!g_thread_supported())
210 g_error(_("g_thread is not supported by glib.\n"));
213 /* parse gtkrc files */
214 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
216 gtk_rc_parse(userrc);
218 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
219 G_DIR_SEPARATOR_S, "gtkrc", NULL);
220 gtk_rc_parse(userrc);
222 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
223 gtk_rc_parse(userrc);
226 gtk_rc_parse("./gtkrc");
228 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
229 gtk_item_factory_parse_rc(userrc);
232 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
234 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
235 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
236 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
237 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
238 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
239 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
241 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
243 folder_system_init();
245 prefs_common_read_config();
249 if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) !=
250 GPGME_No_Error) { /* Also does some gpgme init */
251 rfc2015_disable_all();
252 debug_print("gpgme_engine_version:\n%s\n",
253 gpgme_get_engine_info());
255 if (prefs_common.gpg_warning) {
258 val = alertpanel_message_with_disable
260 _("GnuPG is not installed properly, or needs to be upgraded.\n"
261 "OpenPGP support disabled."));
262 if (val & G_ALERTDISABLE)
263 prefs_common.gpg_warning = FALSE;
268 gpgme_register_idle(idle_function_for_gpgme);
272 gtkaspell_checkers_init();
275 sock_set_io_timeout(prefs_common.io_timeout_secs);
277 prefs_common_save_config();
278 prefs_actions_read_config();
279 prefs_actions_write_config();
280 prefs_display_header_read_config();
281 prefs_display_header_write_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();
299 account_save_config_all();
301 if (folder_read_list() < 0) {
305 if (!account_get_list()) {
310 account_set_missing_folder();
311 folder_set_missing_folders();
312 folderview_set(folderview);
314 /* prefs_scoring_read_config(); */
315 prefs_matcher_read_config();
317 /* make one all-folder processing before using sylpheed */
318 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
320 /* if Sylpheed crashed, rebuild caches */
321 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
322 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
323 folderview_check_new(NULL);
325 /* make the crash-indicator file */
326 str_write_to_file("foo", get_crashfile_name());
328 addressbook_read_file();
330 inc_autocheck_timer_init(mainwin);
332 /* ignore SIGPIPE signal for preventing sudden death of program */
333 signal(SIGPIPE, SIG_IGN);
335 if (cmd.receive_all || prefs_common.chk_on_startup)
336 inc_all_account_mail(mainwin, 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 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
361 main_window_toggle_work_offline(mainwin, TRUE);
362 if (cmd.online_mode == ONLINE_MODE_ONLINE)
363 main_window_toggle_work_offline(mainwin, FALSE);
365 prefs_toolbar_init();
367 plugin_load_all("GTK");
369 static_mainwindow = mainwin;
372 plugin_unload_all("GTK");
374 prefs_toolbar_done();
376 addressbook_destroy();
379 gtkaspell_checkers_quit();
386 static void parse_cmd_opt(int argc, char *argv[])
390 for (i = 1; i < argc; i++) {
391 if (!strncmp(argv[i], "--receive-all", 13))
392 cmd.receive_all = TRUE;
393 else if (!strncmp(argv[i], "--receive", 9))
395 else if (!strncmp(argv[i], "--compose", 9)) {
396 const gchar *p = argv[i + 1];
399 cmd.compose_mailto = NULL;
400 if (p && *p != '\0' && *p != '-') {
401 if (!strncmp(p, "mailto:", 7))
402 cmd.compose_mailto = p + 7;
404 cmd.compose_mailto = p;
407 } else if (!strncmp(argv[i], "--attach", 8)) {
408 const gchar *p = argv[i + 1];
411 while (p && *p != '\0' && *p != '-') {
412 if (!cmd.attach_files)
413 cmd.attach_files = g_ptr_array_new();
414 if (*p != G_DIR_SEPARATOR)
415 file = g_strconcat(sylpheed_get_startup_dir(),
420 g_ptr_array_add(cmd.attach_files, file);
424 } else if (!strncmp(argv[i], "--send", 6)) {
426 } else if (!strncmp(argv[i], "--version", 9)) {
427 puts("Sylpheed version " VERSION);
429 } else if (!strncmp(argv[i], "--status-full", 13)) {
430 const gchar *p = argv[i + 1];
432 cmd.status_full = TRUE;
433 while (p && *p != '\0' && *p != '-') {
434 if (!cmd.status_full_folders)
435 cmd.status_full_folders =
437 g_ptr_array_add(cmd.status_full_folders,
442 } else if (!strncmp(argv[i], "--status", 8)) {
443 const gchar *p = argv[i + 1];
446 while (p && *p != '\0' && *p != '-') {
447 if (!cmd.status_folders)
448 cmd.status_folders = g_ptr_array_new();
449 g_ptr_array_add(cmd.status_folders,
454 } else if (!strncmp(argv[i], "--online", 8)) {
455 cmd.online_mode = ONLINE_MODE_ONLINE;
456 } else if (!strncmp(argv[i], "--offline", 9)) {
457 cmd.online_mode = ONLINE_MODE_OFFLINE;
458 } else if (!strncmp(argv[i], "--help", 6)) {
459 g_print(_("Usage: %s [OPTION]...\n"),
460 g_basename(argv[0]));
462 puts(_(" --compose [address] open composition window"));
463 puts(_(" --attach file1 [file2]...\n"
464 " open composition window with specified files\n"
466 puts(_(" --receive receive new messages"));
467 puts(_(" --receive-all receive new messages of all accounts"));
468 puts(_(" --send send all queued messages"));
469 puts(_(" --status [folder]... show the total number of messages"));
470 puts(_(" --status-full [folder]...\n"
471 " show the status of each folder"));
472 puts(_(" --online switch to online mode"));
473 puts(_(" --offline switch to offline mode"));
474 puts(_(" --debug debug mode"));
475 puts(_(" --help display this help and exit"));
476 puts(_(" --version output version information and exit"));
479 } else if (!strncmp(argv[i], "--crash", 7)) {
481 cmd.crash_params = g_strdup(argv[i + 1]);
487 if (cmd.attach_files && cmd.compose == FALSE) {
489 cmd.compose_mailto = NULL;
493 static gint get_queued_message_num(void)
497 queue = folder_get_default_queue();
498 if (!queue) return -1;
500 folder_item_scan(queue);
501 return queue->total_msgs;
504 static void save_all_caches(FolderItem *item, gpointer data)
508 folder_item_write_cache(item);
511 static void initial_processing(FolderItem *item, gpointer data)
513 MainWindow *mainwin = (MainWindow *)data;
516 g_return_if_fail(item);
517 buf = g_strdup_printf(_("Processing (%s)..."),
520 : _("top level folder"));
521 debug_print("%s\n", buf);
524 main_window_cursor_wait(mainwin);
526 folder_item_apply_processing(item);
528 debug_print("done.\n");
529 STATUSBAR_POP(mainwin);
530 main_window_cursor_normal(mainwin);
533 static void draft_all_messages(void)
535 GList *compose_list = compose_get_compose_list();
539 for (elem = compose_list; elem != NULL && elem->data != NULL;
541 Compose *c = (Compose*)elem->data;
547 void clean_quit(void)
549 /*!< Good idea to have the main window stored in a
550 * static variable so we can check that variable
551 * to see if we're really allowed to do things
552 * that actually the spawner is supposed to
553 * do (like: sending mail, composing messages).
554 * Because, really, if we're the spawnee, and
555 * we touch GTK stuff, we're hosed. See the
558 /* FIXME: Use something else to signal that we're
559 * in the original spawner, and not in a spawned
561 if (!static_mainwindow)
564 draft_all_messages();
566 if (prefs_common.warn_queued_on_exit) {
567 /* disable the popup */
568 prefs_common.warn_queued_on_exit = FALSE;
569 app_will_exit(NULL, static_mainwindow);
570 prefs_common.warn_queued_on_exit = TRUE;
571 prefs_common_save_config();
573 app_will_exit(NULL, static_mainwindow);
578 void app_will_exit(GtkWidget *widget, gpointer data)
580 MainWindow *mainwin = data;
584 if (compose_get_compose_list()) {
585 gint val = alertpanel(_("Notice"),
586 _("Composing message exists."),
587 _("Draft them"), _("Discard them"), _("Don't quit"));
591 case G_ALERTALTERNATE:
594 draft_all_messages();
597 manage_window_focus_in(mainwin->window, NULL, NULL);
600 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
601 if (alertpanel(_("Queued messages"),
602 _("Some unsent messages are queued. Exit now?"),
603 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
605 manage_window_focus_in(mainwin->window, NULL, NULL);
608 inc_autocheck_timer_remove();
611 gpgmegtk_free_passphrase();
614 if (prefs_common.clean_on_exit)
615 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
617 /* save prefs for opened folder */
618 if(mainwin->folderview->opened)
622 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
623 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
626 /* save all state before exiting */
628 folder_func_to_all_folders(save_all_caches, NULL);
629 for (list = folder_get_list(); list != NULL; list = g_list_next(list)) {
630 Folder *folder = FOLDER(list->data);
632 folder_tree_destroy(folder);
635 main_window_get_size(mainwin);
636 main_window_get_position(mainwin);
637 prefs_common_save_config();
638 account_save_config_all();
639 addressbook_export_to_file();
641 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
642 gtk_item_factory_dump_rc(filename, NULL, TRUE);
645 /* delete temporary files */
646 remove_all_files(get_mime_tmp_dir());
650 /* delete crashfile */
652 unlink(get_crashfile_name());
654 lock_socket_remove();
660 static void idle_function_for_gpgme(void)
662 while (gtk_events_pending())
663 gtk_main_iteration();
665 #endif /* USE_GPGME */
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, prefs_common.newmail_notify_manu);
837 } else if (!strncmp(buf, "receive", 7)) {
838 inc_mail(mainwin, prefs_common.newmail_notify_manu);
839 } else if (!strncmp(buf, "compose_attach", 14)) {
843 mailto = g_strdup(buf + strlen("compose_attach") + 1);
844 files = g_ptr_array_new();
845 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
846 if (buf[0] == '.' && buf[1] == '\n') break;
848 g_ptr_array_add(files, g_strdup(buf));
850 open_compose_new(mailto, files);
851 ptr_array_free_strings(files);
852 g_ptr_array_free(files, TRUE);
854 } else if (!strncmp(buf, "compose", 7)) {
855 open_compose_new(buf + strlen("compose") + 1, NULL);
856 } else if (!strncmp(buf, "send", 4)) {
858 } else if (!strncmp(buf, "online", 6)) {
859 main_window_toggle_work_offline(mainwin, FALSE);
860 } else if (!strncmp(buf, "offline", 7)) {
861 main_window_toggle_work_offline(mainwin, TRUE);
862 } else if (!strncmp(buf, "status-full", 11) ||
863 !strncmp(buf, "status", 6)) {
867 folders = get_folder_item_list(sock);
868 status = folder_get_status
869 (folders, !strncmp(buf, "status-full", 11));
870 fd_write_all(sock, status, strlen(status));
871 fd_write_all(sock, ".\n", 2);
873 if (folders) g_ptr_array_free(folders, TRUE);
879 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
884 Xstrdup_a(addr, address, return);
888 compose_new(NULL, addr, attach_files);
891 static void send_queue(void)
894 FolderItem *def_outbox;
896 def_outbox = folder_get_default_outbox();
898 for (list = folder_get_list(); list != NULL; list = list->next) {
899 Folder *folder = list->data;
902 gint res = procmsg_send_queue
903 (folder->queue, prefs_common.savemsg);
906 alertpanel_error(_("Some errors occurred while sending queued messages."));
908 folder_item_scan(folder->queue);
909 if (prefs_common.savemsg && folder->outbox) {
910 if (folder->outbox == def_outbox)