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"
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;
114 static void parse_cmd_opt(int argc, char *argv[]);
117 static void idle_function_for_gpgme(void);
118 #endif /* USE_GPGME */
120 static gint prohibit_duplicate_launch (void);
121 static gchar * get_crashfile_name (void);
122 static gint lock_socket_remove (void);
123 static void lock_socket_input_cb (gpointer data,
125 GdkInputCondition condition);
129 gchar *get_socket_name (void);
132 static void open_compose_new (const gchar *address,
133 GPtrArray *attach_files);
135 static void send_queue (void);
136 static void initial_processing (FolderItem *item, gpointer data);
140 _("File `%s' already exists.\n"
141 "Can't create folder.")
144 #define MAKE_DIR_IF_NOT_EXIST(dir) \
146 if (!is_dir_exist(dir)) { \
147 if (is_file_exist(dir)) { \
149 (_("File `%s' already exists.\n" \
150 "Can't create folder."), \
154 if (make_dir(dir) < 0) \
159 static MainWindow *static_mainwindow;
161 int main(int argc, char *argv[])
165 FolderView *folderview;
167 if (!sylpheed_init(&argc, &argv)) {
171 prog_version = PROG_VERSION;
172 startup_dir = g_get_current_dir();
174 argv0 = g_strdup(argv[0]);
177 parse_cmd_opt(argc, argv);
182 gtk_init(&argc, &argv);
183 crash_main(cmd.crash_params);
186 crash_install_handlers();
189 /* check and create unix domain socket */
190 lock_socket = prohibit_duplicate_launch();
191 if (lock_socket < 0) return 0;
194 puts("0 Sylpheed not running.");
195 lock_socket_remove();
200 gtk_init(&argc, &argv);
203 gtk_widget_set_default_colormap(gdk_rgb_get_cmap());
204 gtk_widget_set_default_visual(gdk_rgb_get_visual());
206 #if USE_THREADS || USE_LDAP
208 if (!g_thread_supported())
209 g_error(_("g_thread is not supported by glib.\n"));
212 /* parse gtkrc files */
213 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
215 gtk_rc_parse(userrc);
217 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
218 G_DIR_SEPARATOR_S, "gtkrc", NULL);
219 gtk_rc_parse(userrc);
221 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
222 gtk_rc_parse(userrc);
225 gtk_rc_parse("./gtkrc");
227 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
228 gtk_item_factory_parse_rc(userrc);
231 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
233 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
234 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
235 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
236 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
237 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
238 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
240 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
242 folder_system_init();
244 prefs_common_read_config();
248 if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) !=
249 GPGME_No_Error) { /* Also does some gpgme init */
250 rfc2015_disable_all();
251 debug_print("gpgme_engine_version:\n%s\n",
252 gpgme_get_engine_info());
254 if (prefs_common.gpg_warning) {
257 val = alertpanel_message_with_disable
259 _("GnuPG is not installed properly, or needs to be upgraded.\n"
260 "OpenPGP support disabled."));
261 if (val & G_ALERTDISABLE)
262 prefs_common.gpg_warning = FALSE;
267 gpgme_register_idle(idle_function_for_gpgme);
271 gtkaspellcheckers = gtkaspell_checkers_new();
274 sock_set_io_timeout(prefs_common.io_timeout_secs);
276 prefs_common_save_config();
277 prefs_actions_read_config();
278 prefs_actions_write_config();
279 prefs_display_header_read_config();
280 prefs_display_header_write_config();
281 /* prefs_filtering_read_config(); */
282 addressbook_read_file();
283 renderer_read_config();
287 mainwin = main_window_create
288 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
289 folderview = mainwin->folderview;
291 /* register the callback of unix domain socket input */
292 lock_socket_tag = gdk_input_add(lock_socket,
293 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
294 lock_socket_input_cb,
297 account_read_config_all();
298 account_save_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;
351 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
352 main_window_toggle_work_offline(mainwin, TRUE);
353 if (cmd.online_mode == ONLINE_MODE_ONLINE)
354 main_window_toggle_work_offline(mainwin, FALSE);
356 prefs_toolbar_init();
358 plugin_load_all("GTK");
360 static_mainwindow = mainwin;
363 plugin_unload_all("GTK");
365 prefs_toolbar_done();
367 addressbook_destroy();
370 gtkaspell_checkers_delete();
377 static void parse_cmd_opt(int argc, char *argv[])
381 for (i = 1; i < argc; i++) {
382 if (!strncmp(argv[i], "--receive-all", 13))
383 cmd.receive_all = TRUE;
384 else if (!strncmp(argv[i], "--receive", 9))
386 else if (!strncmp(argv[i], "--compose", 9)) {
387 const gchar *p = argv[i + 1];
390 cmd.compose_mailto = NULL;
391 if (p && *p != '\0' && *p != '-') {
392 if (!strncmp(p, "mailto:", 7))
393 cmd.compose_mailto = p + 7;
395 cmd.compose_mailto = p;
398 } else if (!strncmp(argv[i], "--attach", 8)) {
399 const gchar *p = argv[i + 1];
402 while (p && *p != '\0' && *p != '-') {
403 if (!cmd.attach_files)
404 cmd.attach_files = g_ptr_array_new();
405 if (*p != G_DIR_SEPARATOR)
406 file = g_strconcat(startup_dir,
411 g_ptr_array_add(cmd.attach_files, file);
415 } else if (!strncmp(argv[i], "--send", 6)) {
417 } else if (!strncmp(argv[i], "--version", 9)) {
418 puts("Sylpheed version " VERSION);
420 } else if (!strncmp(argv[i], "--status", 8)) {
422 } else if (!strncmp(argv[i], "--online", 8)) {
423 cmd.online_mode = ONLINE_MODE_ONLINE;
424 } else if (!strncmp(argv[i], "--offline", 9)) {
425 cmd.online_mode = ONLINE_MODE_OFFLINE;
426 } else if (!strncmp(argv[i], "--help", 6)) {
427 g_print(_("Usage: %s [OPTION]...\n"),
428 g_basename(argv[0]));
430 puts(_(" --compose [address] open composition window"));
431 puts(_(" --attach file1 [file2]...\n"
432 " open composition window with specified files\n"
434 puts(_(" --receive receive new messages"));
435 puts(_(" --receive-all receive new messages of all accounts"));
436 puts(_(" --send send all queued messages"));
437 puts(_(" --status show the total number of messages"));
438 puts(_(" --online switch to online mode"));
439 puts(_(" --offline switch to offline mode"));
440 puts(_(" --debug debug mode"));
441 puts(_(" --help display this help and exit"));
442 puts(_(" --version output version information and exit"));
445 } else if (!strncmp(argv[i], "--crash", 7)) {
447 cmd.crash_params = g_strdup(argv[i + 1]);
453 if (cmd.attach_files && cmd.compose == FALSE) {
455 cmd.compose_mailto = NULL;
459 static gint get_queued_message_num(void)
463 queue = folder_get_default_queue();
464 if (!queue) return -1;
466 folder_item_scan(queue);
467 return queue->total_msgs;
470 static void save_all_caches(FolderItem *item, gpointer data)
474 folder_item_write_cache(item);
477 static void initial_processing(FolderItem *item, gpointer data)
479 MainWindow *mainwin = (MainWindow *)data;
482 g_return_if_fail(item);
483 buf = g_strdup_printf(_("Processing (%s)..."),
486 : _("top level folder"));
487 debug_print("%s\n", buf);
490 main_window_cursor_wait(mainwin);
492 folder_item_apply_processing(item);
494 debug_print("done.\n");
495 STATUSBAR_POP(mainwin);
496 main_window_cursor_normal(mainwin);
499 static void draft_all_messages(void)
501 GList *compose_list = compose_get_compose_list();
505 for (elem = compose_list; elem != NULL && elem->data != NULL;
507 Compose *c = (Compose*)elem->data;
513 void clean_quit(void)
515 /*!< Good idea to have the main window stored in a
516 * static variable so we can check that variable
517 * to see if we're really allowed to do things
518 * that actually the spawner is supposed to
519 * do (like: sending mail, composing messages).
520 * Because, really, if we're the spawnee, and
521 * we touch GTK stuff, we're hosed. See the
524 /* FIXME: Use something else to signal that we're
525 * in the original spawner, and not in a spawned
527 if (!static_mainwindow)
530 draft_all_messages();
532 if (prefs_common.warn_queued_on_exit) {
533 /* disable the popup */
534 prefs_common.warn_queued_on_exit = FALSE;
535 app_will_exit(NULL, static_mainwindow);
536 prefs_common.warn_queued_on_exit = TRUE;
537 prefs_common_save_config();
539 app_will_exit(NULL, static_mainwindow);
544 void app_will_exit(GtkWidget *widget, gpointer data)
546 MainWindow *mainwin = data;
549 if (compose_get_compose_list()) {
550 gint val = alertpanel(_("Notice"),
551 _("Composing message exists."),
552 _("Draft them"), _("Discard them"), _("Don't quit"));
556 case G_ALERTALTERNATE:
559 draft_all_messages();
562 manage_window_focus_in(mainwin->window, NULL, NULL);
565 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
566 if (alertpanel(_("Queued messages"),
567 _("Some unsent messages are queued. Exit now?"),
568 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
570 manage_window_focus_in(mainwin->window, NULL, NULL);
573 inc_autocheck_timer_remove();
576 gpgmegtk_free_passphrase();
579 if (prefs_common.clean_on_exit)
580 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
582 /* save prefs for opened folder */
583 if(mainwin->folderview->opened)
587 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
588 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
591 /* save all state before exiting */
593 folder_func_to_all_folders(save_all_caches, NULL);
595 main_window_get_size(mainwin);
596 main_window_get_position(mainwin);
597 prefs_common_save_config();
598 account_save_config_all();
599 addressbook_export_to_file();
601 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
602 gtk_item_factory_dump_rc(filename, NULL, TRUE);
605 /* delete temporary files */
606 remove_all_files(get_mime_tmp_dir());
610 /* delete crashfile */
612 unlink(get_crashfile_name());
614 lock_socket_remove();
620 static void idle_function_for_gpgme(void)
622 while (gtk_events_pending())
623 gtk_main_iteration();
625 #endif /* USE_GPGME */
628 * CLAWS: want this public so crash dialog can delete the
634 gchar *get_socket_name(void)
636 static gchar *filename = NULL;
638 if (filename == NULL) {
639 filename = g_strdup_printf("%s%csylpheed-%d",
640 g_get_tmp_dir(), G_DIR_SEPARATOR,
647 static gchar *get_crashfile_name(void)
649 static gchar *filename = NULL;
651 if (filename == NULL) {
652 filename = g_strdup_printf("%s%csylpheed-crashed",
653 get_tmp_dir(), G_DIR_SEPARATOR);
659 static gint prohibit_duplicate_launch(void)
664 path = get_socket_name();
665 uxsock = fd_connect_unix(path);
668 return fd_open_unix(path);
671 /* remote command mode */
673 debug_print("another Sylpheed is already running.\n");
676 fd_write_all(uxsock, "receive_all\n", 12);
677 else if (cmd.receive)
678 fd_write_all(uxsock, "receive\n", 8);
679 else if (cmd.compose && cmd.attach_files) {
680 gchar *str, *compose_str;
683 if (cmd.compose_mailto)
684 compose_str = g_strdup_printf("compose_attach %s\n",
687 compose_str = g_strdup("compose_attach\n");
689 fd_write_all(uxsock, compose_str, strlen(compose_str));
692 for (i = 0; i < cmd.attach_files->len; i++) {
693 str = g_ptr_array_index(cmd.attach_files, i);
694 fd_write_all(uxsock, str, strlen(str));
695 fd_write_all(uxsock, "\n", 1);
698 fd_write_all(uxsock, ".\n", 2);
699 } else if (cmd.compose) {
702 if (cmd.compose_mailto)
703 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
705 compose_str = g_strdup("compose\n");
707 fd_write_all(uxsock, compose_str, strlen(compose_str));
709 } else if (cmd.send) {
710 fd_write_all(uxsock, "send\n", 5);
711 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
712 fd_write(uxsock, "online\n", 6);
713 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
714 fd_write(uxsock, "offline\n", 7);
715 } else if (cmd.status) {
718 fd_write_all(uxsock, "status\n", 7);
719 fd_gets(uxsock, buf, sizeof(buf));
722 fd_write_all(uxsock, "popup\n", 6);
728 static gint lock_socket_remove(void)
732 if (lock_socket < 0) return -1;
734 if (lock_socket_tag > 0)
735 gdk_input_remove(lock_socket_tag);
736 fd_close(lock_socket);
737 filename = get_socket_name();
743 static void lock_socket_input_cb(gpointer data,
745 GdkInputCondition condition)
747 MainWindow *mainwin = (MainWindow *)data;
751 sock = fd_accept(source);
752 fd_gets(sock, buf, sizeof(buf));
754 if (!strncmp(buf, "popup", 5)) {
755 main_window_popup(mainwin);
756 } else if (!strncmp(buf, "receive_all", 11)) {
757 main_window_popup(mainwin);
758 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
759 } else if (!strncmp(buf, "receive", 7)) {
760 main_window_popup(mainwin);
761 inc_mail(mainwin, prefs_common.newmail_notify_manu);
762 } else if (!strncmp(buf, "compose_attach", 14)) {
766 mailto = g_strdup(buf + strlen("compose_attach") + 1);
767 files = g_ptr_array_new();
768 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
769 if (buf[0] == '.' && buf[1] == '\n') break;
771 g_ptr_array_add(files, g_strdup(buf));
773 open_compose_new(mailto, files);
774 ptr_array_free_strings(files);
775 g_ptr_array_free(files, TRUE);
777 } else if (!strncmp(buf, "compose", 7)) {
778 open_compose_new(buf + strlen("compose") + 1, NULL);
779 } else if (!strncmp(buf, "send", 4)) {
781 } else if (!strncmp(buf, "online", 6)) {
782 main_window_toggle_work_offline(mainwin, FALSE);
783 } else if (!strncmp(buf, "offline", 7)) {
784 main_window_toggle_work_offline(mainwin, TRUE);
785 } else if (!strncmp(buf, "status", 6)) {
786 guint new, unread, unreadmarked, total;
788 folder_count_total_msgs(&new, &unread, &unreadmarked, &total);
789 g_snprintf(buf, sizeof(buf), "%d %d %d %d\n", new, unread, unreadmarked, total);
790 fd_write_all(sock, buf, strlen(buf));
796 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
801 Xstrdup_a(addr, address, return);
805 compose_new(NULL, addr, attach_files);
808 static void send_queue(void)
811 FolderItem *def_outbox;
813 def_outbox = folder_get_default_outbox();
815 for (list = folder_get_list(); list != NULL; list = list->next) {
816 Folder *folder = list->data;
819 gint res = procmsg_send_queue
820 (folder->queue, prefs_common.savemsg);
823 alertpanel_error(_("Some errors occurred while sending queued messages."));
825 folder_item_scan(folder->queue);
826 if (prefs_common.savemsg && folder->outbox) {
827 if (folder->outbox == def_outbox)