2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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>
31 # include <gdk_imlib.h>
41 #include <sys/types.h>
50 # include "passphrase.h"
55 #include "mainwindow.h"
56 #include "folderview.h"
57 #include "summaryview.h"
58 #include "prefs_common.h"
59 #include "prefs_account.h"
60 #include "prefs_actions.h"
62 #include "prefs_display_header.h"
67 #include "manage_window.h"
68 #include "alertpanel.h"
69 #include "statusbar.h"
70 #include "addressbook.h"
91 gboolean debug_mode = FALSE;
93 static gint lock_socket = -1;
94 static gint lock_socket_tag = 0;
98 ONLINE_MODE_DONT_CHANGE,
106 gboolean receive_all;
108 const gchar *compose_mailto;
109 GPtrArray *attach_files;
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 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 int main(int argc, char *argv[])
165 FolderView *folderview;
167 setlocale(LC_ALL, "");
168 bindtextdomain(PACKAGE, LOCALEDIR);
171 prog_version = PROG_VERSION;
172 startup_dir = g_get_current_dir();
173 argv0 = g_strdup(argv[0]);
175 parse_cmd_opt(argc, argv);
178 gtk_init(&argc, &argv);
181 crash_main(cmd.crash_params);
185 crash_install_handlers();
187 #if USE_THREADS || USE_LDAP
189 if (!g_thread_supported())
190 g_error(_("g_thread is not supported by glib.\n"));
195 gtk_widget_push_visual(gdk_imlib_get_visual());
196 gtk_widget_push_colormap(gdk_imlib_get_colormap());
203 srandom((gint)time(NULL));
205 /* parse gtkrc files */
206 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
208 gtk_rc_parse(userrc);
210 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
211 G_DIR_SEPARATOR_S, "gtkrc", NULL);
212 gtk_rc_parse(userrc);
214 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
215 gtk_rc_parse(userrc);
218 gtk_rc_parse("./gtkrc");
220 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
221 gtk_item_factory_parse_rc(userrc);
224 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
226 /* check and create unix domain socket */
227 lock_socket = prohibit_duplicate_launch();
228 if (lock_socket < 0) return 0;
231 puts("0 Sylpheed not running.");
235 /* backup if old rc file exists */
236 if (is_file_exist(RC_DIR)) {
237 if (rename(RC_DIR, RC_DIR ".bak") < 0)
238 FILE_OP_ERROR(RC_DIR, "rename");
240 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
241 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
242 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
243 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
244 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
245 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
247 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
248 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
249 RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
250 FILE_OP_ERROR("sylpheed.log", "rename");
252 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
254 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
255 !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
256 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
257 RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
258 FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
263 prefs_common_read_config();
267 if (gpgme_check_engine()) { /* Also does some gpgme init */
268 rfc2015_disable_all();
269 debug_print("gpgme_engine_version:\n%s\n",
270 gpgme_get_engine_info());
272 if (prefs_common.gpg_warning) {
275 val = alertpanel_message_with_disable
277 _("GnuPG is not installed properly, or needs to be upgraded.\n"
278 "OpenPGP support disabled."));
279 if (val & G_ALERTDISABLE)
280 prefs_common.gpg_warning = FALSE;
285 gpgme_register_idle(idle_function_for_gpgme);
289 gtkaspellcheckers = gtkaspell_checkers_new();
293 prefs_common_save_config();
294 prefs_actions_read_config();
295 prefs_actions_write_config();
296 prefs_display_header_read_config();
297 prefs_display_header_write_config();
298 /* prefs_filtering_read_config(); */
299 addressbook_read_file();
300 renderer_read_config();
304 mainwin = main_window_create
305 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
306 folderview = mainwin->folderview;
308 /* register the callback of unix domain socket input */
309 lock_socket_tag = gdk_input_add(lock_socket,
310 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
311 lock_socket_input_cb,
314 account_read_config_all();
315 account_save_config_all();
317 if (folder_read_list() < 0) {
321 if (!account_get_list()) {
326 account_set_missing_folder();
327 folder_set_missing_folders();
328 folderview_set(folderview);
330 /* prefs_scoring_read_config(); */
331 prefs_matcher_read_config();
333 /* make one all-folder processing before using sylpheed */
334 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
336 /* if Sylpheed crashed, rebuild caches */
337 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
338 debug_print("Sylpheed crashed, checking for new messages\n");
339 folderview_check_new_all();
341 /* make the crash-indicator file */
342 str_write_to_file("foo", get_crashfile_name());
344 addressbook_read_file();
346 inc_autocheck_timer_init(mainwin);
348 /* ignore SIGPIPE signal for preventing sudden death of program */
349 signal(SIGPIPE, SIG_IGN);
351 if (cmd.receive_all || prefs_common.chk_on_startup)
352 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
353 else if (cmd.receive)
354 inc_mail(mainwin, prefs_common.newmail_notify_manu);
356 gtk_widget_grab_focus(folderview->ctree);
359 open_compose_new(cmd.compose_mailto, cmd.attach_files);
360 if (cmd.attach_files) {
361 ptr_array_free_strings(cmd.attach_files);
362 g_ptr_array_free(cmd.attach_files, TRUE);
363 cmd.attach_files = NULL;
368 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
369 main_window_toggle_work_offline(mainwin, TRUE);
370 if (cmd.online_mode == ONLINE_MODE_ONLINE)
371 main_window_toggle_work_offline(mainwin, FALSE);
375 addressbook_destroy();
378 gtkaspell_checkers_delete();
384 static void parse_cmd_opt(int argc, char *argv[])
388 for (i = 1; i < argc; i++) {
389 if (!strncmp(argv[i], "--debug", 7))
391 else 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(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", 8)) {
431 } else if (!strncmp(argv[i], "--online", 8)) {
432 cmd.online_mode = ONLINE_MODE_ONLINE;
433 } else if (!strncmp(argv[i], "--offline", 9)) {
434 cmd.online_mode = ONLINE_MODE_OFFLINE;
435 } else if (!strncmp(argv[i], "--help", 6)) {
436 g_print(_("Usage: %s [OPTION]...\n"),
437 g_basename(argv[0]));
439 puts(_(" --compose [address] open composition window"));
440 puts(_(" --attach file1 [file2]...\n"
441 " open composition window with specified files\n"
443 puts(_(" --receive receive new messages"));
444 puts(_(" --receive-all receive new messages of all accounts"));
445 puts(_(" --send send all queued messages"));
446 puts(_(" --status show the total number of messages"));
447 puts(_(" --online switch to online mode"));
448 puts(_(" --offline switch to offline mode"));
449 puts(_(" --debug debug mode"));
450 puts(_(" --help display this help and exit"));
451 puts(_(" --version output version information and exit"));
454 } else if (!strncmp(argv[i], "--crash", 7)) {
456 cmd.crash_params = g_strdup(argv[i + 1]);
462 if (cmd.attach_files && cmd.compose == FALSE) {
464 cmd.compose_mailto = NULL;
468 static gint get_queued_message_num(void)
472 queue = folder_get_default_queue();
473 if (!queue) return -1;
475 folder_item_scan(queue);
479 static void save_all_caches(FolderItem *item, gpointer data)
483 folder_item_write_cache(item);
486 static void initial_processing(FolderItem *item, gpointer data)
488 MainWindow *mainwin = (MainWindow *)data;
491 g_return_if_fail(item);
492 buf = g_strdup_printf(_("Processing (%s)..."),
495 : _("top level folder"));
496 debug_print("%s\n", buf);
499 main_window_cursor_wait(mainwin);
501 folder_item_apply_processing(item);
503 debug_print("done.\n");
504 STATUSBAR_POP(mainwin);
505 main_window_cursor_normal(mainwin);
508 void app_will_exit(GtkWidget *widget, gpointer data)
510 MainWindow *mainwin = data;
513 if (compose_get_compose_list()) {
514 if (alertpanel(_("Notice"),
515 _("Composing message exists. Really quit?"),
516 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
518 manage_window_focus_in(mainwin->window, NULL, NULL);
521 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
522 if (alertpanel(_("Queued messages"),
523 _("Some unsent messages are queued. Exit now?"),
524 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
526 manage_window_focus_in(mainwin->window, NULL, NULL);
529 inc_autocheck_timer_remove();
532 gpgmegtk_free_passphrase();
535 if (prefs_common.clean_on_exit)
536 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
538 /* save prefs for opened folder */
539 if(mainwin->folderview->opened)
543 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
544 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
547 /* save all state before exiting */
549 folder_func_to_all_folders(save_all_caches, NULL);
551 main_window_get_size(mainwin);
552 main_window_get_position(mainwin);
553 prefs_common_save_config();
554 account_save_config_all();
555 addressbook_export_to_file();
557 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
558 gtk_item_factory_dump_rc(filename, NULL, TRUE);
561 /* delete temporary files */
562 remove_all_files(get_mime_tmp_dir());
566 /* delete unix domain socket */
567 gdk_input_remove(lock_socket_tag);
568 fd_close(lock_socket);
569 filename = get_socket_name();
572 /* delete crashfile */
574 unlink(get_crashfile_name());
584 static void idle_function_for_gpgme(void)
586 while (gtk_events_pending())
587 gtk_main_iteration();
589 #endif /* USE_GPGME */
592 * CLAWS: want this public so crash dialog can delete the
598 gchar *get_socket_name(void)
600 static gchar *filename = NULL;
602 if (filename == NULL) {
603 filename = g_strdup_printf("%s%csylpheed-%d",
604 g_get_tmp_dir(), G_DIR_SEPARATOR,
611 static gchar *get_crashfile_name(void)
613 static gchar *filename = NULL;
615 if (filename == NULL) {
616 filename = g_strdup_printf("%s%csylpheed-crashed",
617 get_tmp_dir(), G_DIR_SEPARATOR);
623 static gint prohibit_duplicate_launch(void)
628 path = get_socket_name();
629 uxsock = fd_connect_unix(path);
632 return fd_open_unix(path);
635 /* remote command mode */
637 debug_print("another Sylpheed is already running.\n");
640 fd_write(uxsock, "receive_all\n", 12);
641 else if (cmd.receive)
642 fd_write(uxsock, "receive\n", 8);
643 else if (cmd.compose && cmd.attach_files) {
644 gchar *str, *compose_str;
647 if (cmd.compose_mailto)
648 compose_str = g_strdup_printf("compose_attach %s\n",
651 compose_str = g_strdup("compose_attach\n");
653 fd_write(uxsock, compose_str, strlen(compose_str));
656 for (i = 0; i < cmd.attach_files->len; i++) {
657 str = g_ptr_array_index(cmd.attach_files, i);
658 fd_write(uxsock, str, strlen(str));
659 fd_write(uxsock, "\n", 1);
662 fd_write(uxsock, ".\n", 2);
663 } else if (cmd.compose) {
666 if (cmd.compose_mailto)
667 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
669 compose_str = g_strdup("compose\n");
671 fd_write(uxsock, compose_str, strlen(compose_str));
673 } else if (cmd.send) {
674 fd_write(uxsock, "send\n", 5);
675 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
676 fd_write(uxsock, "online\n", 6);
677 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
678 fd_write(uxsock, "offline\n", 7);
679 } else if (cmd.status) {
682 fd_write(uxsock, "status\n", 7);
683 fd_gets(uxsock, buf, sizeof(buf));
686 fd_write(uxsock, "popup\n", 6);
692 static void lock_socket_input_cb(gpointer data,
694 GdkInputCondition condition)
696 MainWindow *mainwin = (MainWindow *)data;
700 sock = fd_accept(source);
701 fd_gets(sock, buf, sizeof(buf));
703 if (!strncmp(buf, "popup", 5)) {
704 main_window_popup(mainwin);
705 } else if (!strncmp(buf, "receive_all", 11)) {
706 main_window_popup(mainwin);
707 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
708 } else if (!strncmp(buf, "receive", 7)) {
709 main_window_popup(mainwin);
710 inc_mail(mainwin, prefs_common.newmail_notify_manu);
711 } else if (!strncmp(buf, "compose_attach", 14)) {
715 mailto = g_strdup(buf + strlen("compose_attach") + 1);
716 files = g_ptr_array_new();
717 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
718 if (buf[0] == '.' && buf[1] == '\n') break;
720 g_ptr_array_add(files, g_strdup(buf));
722 open_compose_new(mailto, files);
723 ptr_array_free_strings(files);
724 g_ptr_array_free(files, TRUE);
726 } else if (!strncmp(buf, "compose", 7)) {
727 open_compose_new(buf + strlen("compose") + 1, NULL);
728 } else if (!strncmp(buf, "send", 4)) {
730 } else if (!strncmp(buf, "online", 6)) {
731 main_window_toggle_work_offline(mainwin, FALSE);
732 } else if (!strncmp(buf, "offline", 7)) {
733 main_window_toggle_work_offline(mainwin, TRUE);
734 } else if (!strncmp(buf, "status", 6)) {
735 guint new, unread, total;
737 folder_count_total_msgs(&new, &unread, &total);
738 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
739 fd_write(sock, buf, strlen(buf));
745 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
750 Xstrdup_a(addr, address, return);
754 compose_new(NULL, addr, attach_files);
757 static void send_queue(void)
760 FolderItem *def_outbox;
762 def_outbox = folder_get_default_outbox();
764 for (list = folder_get_list(); list != NULL; list = list->next) {
765 Folder *folder = list->data;
768 if (procmsg_send_queue
769 (folder->queue, prefs_common.savemsg) < 0)
770 alertpanel_error(_("Some errors occurred while sending queued messages."));
772 folder_item_scan(folder->queue);
773 folderview_update_item(folder->queue, TRUE);
774 if (prefs_common.savemsg && folder->outbox) {
775 folderview_update_item(folder->outbox, TRUE);
776 if (folder->outbox == def_outbox)
782 if (prefs_common.savemsg && def_outbox)
783 folderview_update_item(def_outbox, TRUE);