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>
46 # include "passphrase.h"
52 #include "mainwindow.h"
53 #include "folderview.h"
54 #include "summaryview.h"
55 #include "prefs_common.h"
56 #include "prefs_account.h"
57 #include "prefs_actions.h"
59 #include "prefs_display_header.h"
64 #include "manage_window.h"
65 #include "alertpanel.h"
66 #include "statusbar.h"
67 #include "addressbook.h"
74 #include "prefs_toolbar.h"
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 static MainWindow *static_mainwindow;
162 int main(int argc, char *argv[])
166 FolderView *folderview;
168 if(!sylpheed_init(&argc, &argv)) {
172 prog_version = PROG_VERSION;
173 startup_dir = g_get_current_dir();
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;
195 puts("0 Sylpheed not running.");
200 gtk_init(&argc, &argv);
202 #if USE_THREADS || USE_LDAP
204 if (!g_thread_supported())
205 g_error(_("g_thread is not supported by glib.\n"));
210 gtk_widget_push_visual(gdk_imlib_get_visual());
211 gtk_widget_push_colormap(gdk_imlib_get_colormap());
214 /* parse gtkrc files */
215 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
217 gtk_rc_parse(userrc);
219 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
220 G_DIR_SEPARATOR_S, "gtkrc", NULL);
221 gtk_rc_parse(userrc);
223 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
224 gtk_rc_parse(userrc);
227 gtk_rc_parse("./gtkrc");
229 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
230 gtk_item_factory_parse_rc(userrc);
233 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
235 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
236 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
237 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
238 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
239 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
240 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
242 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
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 gtkaspellcheckers = gtkaspell_checkers_new();
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 static_mainwindow = mainwin;
361 prefs_toolbar_done();
363 addressbook_destroy();
366 gtkaspell_checkers_delete();
373 static void parse_cmd_opt(int argc, char *argv[])
377 for (i = 1; i < argc; i++) {
378 if (!strncmp(argv[i], "--debug", 7))
379 debug_set_mode(TRUE);
380 else if (!strncmp(argv[i], "--receive-all", 13))
381 cmd.receive_all = TRUE;
382 else if (!strncmp(argv[i], "--receive", 9))
384 else if (!strncmp(argv[i], "--compose", 9)) {
385 const gchar *p = argv[i + 1];
388 cmd.compose_mailto = NULL;
389 if (p && *p != '\0' && *p != '-') {
390 if (!strncmp(p, "mailto:", 7))
391 cmd.compose_mailto = p + 7;
393 cmd.compose_mailto = p;
396 } else if (!strncmp(argv[i], "--attach", 8)) {
397 const gchar *p = argv[i + 1];
400 while (p && *p != '\0' && *p != '-') {
401 if (!cmd.attach_files)
402 cmd.attach_files = g_ptr_array_new();
403 if (*p != G_DIR_SEPARATOR)
404 file = g_strconcat(startup_dir,
409 g_ptr_array_add(cmd.attach_files, file);
413 } else if (!strncmp(argv[i], "--send", 6)) {
415 } else if (!strncmp(argv[i], "--version", 9)) {
416 puts("Sylpheed version " VERSION);
418 } else if (!strncmp(argv[i], "--status", 8)) {
420 } else if (!strncmp(argv[i], "--online", 8)) {
421 cmd.online_mode = ONLINE_MODE_ONLINE;
422 } else if (!strncmp(argv[i], "--offline", 9)) {
423 cmd.online_mode = ONLINE_MODE_OFFLINE;
424 } else if (!strncmp(argv[i], "--help", 6)) {
425 g_print(_("Usage: %s [OPTION]...\n"),
426 g_basename(argv[0]));
428 puts(_(" --compose [address] open composition window"));
429 puts(_(" --attach file1 [file2]...\n"
430 " open composition window with specified files\n"
432 puts(_(" --receive receive new messages"));
433 puts(_(" --receive-all receive new messages of all accounts"));
434 puts(_(" --send send all queued messages"));
435 puts(_(" --status show the total number of messages"));
436 puts(_(" --online switch to online mode"));
437 puts(_(" --offline switch to offline mode"));
438 puts(_(" --debug debug mode"));
439 puts(_(" --help display this help and exit"));
440 puts(_(" --version output version information and exit"));
443 } else if (!strncmp(argv[i], "--crash", 7)) {
445 cmd.crash_params = g_strdup(argv[i + 1]);
451 if (cmd.attach_files && cmd.compose == FALSE) {
453 cmd.compose_mailto = NULL;
457 static gint get_queued_message_num(void)
461 queue = folder_get_default_queue();
462 if (!queue) return -1;
464 folder_item_scan(queue);
468 static void save_all_caches(FolderItem *item, gpointer data)
472 folder_item_write_cache(item);
475 static void initial_processing(FolderItem *item, gpointer data)
477 MainWindow *mainwin = (MainWindow *)data;
480 g_return_if_fail(item);
481 buf = g_strdup_printf(_("Processing (%s)..."),
484 : _("top level folder"));
485 debug_print("%s\n", buf);
488 main_window_cursor_wait(mainwin);
490 folder_item_apply_processing(item);
492 debug_print("done.\n");
493 STATUSBAR_POP(mainwin);
494 main_window_cursor_normal(mainwin);
497 static void draft_all_messages(void)
499 GList * compose_list = compose_get_compose_list();
502 for (elem = compose_list; elem != NULL && elem->data != NULL; elem = elem->next) {
503 Compose *c = (Compose*)elem->data;
509 void clean_quit(void)
511 draft_all_messages();
513 if (prefs_common.warn_queued_on_exit)
514 { /* disable the popup */
515 prefs_common.warn_queued_on_exit = FALSE;
516 app_will_exit(NULL, static_mainwindow);
517 prefs_common.warn_queued_on_exit = TRUE;
518 prefs_common_save_config();
520 app_will_exit(NULL, static_mainwindow);
525 void app_will_exit(GtkWidget *widget, gpointer data)
527 MainWindow *mainwin = data;
530 if (compose_get_compose_list()) {
531 gint val = alertpanel(_("Notice"),
532 _("Composing message exists."),
533 _("Draft them"), _("Discard them"), _("Don't quit"));
537 case G_ALERTALTERNATE:
540 draft_all_messages();
543 manage_window_focus_in(mainwin->window, NULL, NULL);
546 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
547 if (alertpanel(_("Queued messages"),
548 _("Some unsent messages are queued. Exit now?"),
549 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
551 manage_window_focus_in(mainwin->window, NULL, NULL);
554 inc_autocheck_timer_remove();
557 gpgmegtk_free_passphrase();
560 if (prefs_common.clean_on_exit)
561 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
563 /* save prefs for opened folder */
564 if(mainwin->folderview->opened)
568 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
569 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
572 /* save all state before exiting */
574 folder_func_to_all_folders(save_all_caches, NULL);
576 main_window_get_size(mainwin);
577 main_window_get_position(mainwin);
578 prefs_common_save_config();
579 account_save_config_all();
580 addressbook_export_to_file();
582 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
583 gtk_item_factory_dump_rc(filename, NULL, TRUE);
586 /* delete temporary files */
587 remove_all_files(get_mime_tmp_dir());
591 /* delete unix domain socket */
592 gdk_input_remove(lock_socket_tag);
593 fd_close(lock_socket);
594 filename = get_socket_name();
597 /* delete crashfile */
599 unlink(get_crashfile_name());
605 static void idle_function_for_gpgme(void)
607 while (gtk_events_pending())
608 gtk_main_iteration();
610 #endif /* USE_GPGME */
613 * CLAWS: want this public so crash dialog can delete the
619 gchar *get_socket_name(void)
621 static gchar *filename = NULL;
623 if (filename == NULL) {
624 filename = g_strdup_printf("%s%csylpheed-%d",
625 g_get_tmp_dir(), G_DIR_SEPARATOR,
632 static gchar *get_crashfile_name(void)
634 static gchar *filename = NULL;
636 if (filename == NULL) {
637 filename = g_strdup_printf("%s%csylpheed-crashed",
638 get_tmp_dir(), G_DIR_SEPARATOR);
644 static gint prohibit_duplicate_launch(void)
649 path = get_socket_name();
650 uxsock = fd_connect_unix(path);
653 return fd_open_unix(path);
656 /* remote command mode */
658 debug_print("another Sylpheed is already running.\n");
661 fd_write(uxsock, "receive_all\n", 12);
662 else if (cmd.receive)
663 fd_write(uxsock, "receive\n", 8);
664 else if (cmd.compose && cmd.attach_files) {
665 gchar *str, *compose_str;
668 if (cmd.compose_mailto)
669 compose_str = g_strdup_printf("compose_attach %s\n",
672 compose_str = g_strdup("compose_attach\n");
674 fd_write(uxsock, compose_str, strlen(compose_str));
677 for (i = 0; i < cmd.attach_files->len; i++) {
678 str = g_ptr_array_index(cmd.attach_files, i);
679 fd_write(uxsock, str, strlen(str));
680 fd_write(uxsock, "\n", 1);
683 fd_write(uxsock, ".\n", 2);
684 } else if (cmd.compose) {
687 if (cmd.compose_mailto)
688 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
690 compose_str = g_strdup("compose\n");
692 fd_write(uxsock, compose_str, strlen(compose_str));
694 } else if (cmd.send) {
695 fd_write(uxsock, "send\n", 5);
696 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
697 fd_write(uxsock, "online\n", 6);
698 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
699 fd_write(uxsock, "offline\n", 7);
700 } else if (cmd.status) {
703 fd_write(uxsock, "status\n", 7);
704 fd_gets(uxsock, buf, sizeof(buf));
707 fd_write(uxsock, "popup\n", 6);
713 static void lock_socket_input_cb(gpointer data,
715 GdkInputCondition condition)
717 MainWindow *mainwin = (MainWindow *)data;
721 sock = fd_accept(source);
722 fd_gets(sock, buf, sizeof(buf));
724 if (!strncmp(buf, "popup", 5)) {
725 main_window_popup(mainwin);
726 } else if (!strncmp(buf, "receive_all", 11)) {
727 main_window_popup(mainwin);
728 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
729 } else if (!strncmp(buf, "receive", 7)) {
730 main_window_popup(mainwin);
731 inc_mail(mainwin, prefs_common.newmail_notify_manu);
732 } else if (!strncmp(buf, "compose_attach", 14)) {
736 mailto = g_strdup(buf + strlen("compose_attach") + 1);
737 files = g_ptr_array_new();
738 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
739 if (buf[0] == '.' && buf[1] == '\n') break;
741 g_ptr_array_add(files, g_strdup(buf));
743 open_compose_new(mailto, files);
744 ptr_array_free_strings(files);
745 g_ptr_array_free(files, TRUE);
747 } else if (!strncmp(buf, "compose", 7)) {
748 open_compose_new(buf + strlen("compose") + 1, NULL);
749 } else if (!strncmp(buf, "send", 4)) {
751 } else if (!strncmp(buf, "online", 6)) {
752 main_window_toggle_work_offline(mainwin, FALSE);
753 } else if (!strncmp(buf, "offline", 7)) {
754 main_window_toggle_work_offline(mainwin, TRUE);
755 } else if (!strncmp(buf, "status", 6)) {
756 guint new, unread, unreadmarked, total;
758 folder_count_total_msgs(&new, &unread, &unreadmarked, &total);
759 g_snprintf(buf, sizeof(buf), "%d %d %d %d\n", new, unread, unreadmarked, total);
760 fd_write(sock, buf, strlen(buf));
766 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
771 Xstrdup_a(addr, address, return);
775 compose_new(NULL, addr, attach_files);
778 static void send_queue(void)
781 FolderItem *def_outbox;
783 def_outbox = folder_get_default_outbox();
785 for (list = folder_get_list(); list != NULL; list = list->next) {
786 Folder *folder = list->data;
789 if (procmsg_send_queue
790 (folder->queue, prefs_common.savemsg) < 0)
791 alertpanel_error(_("Some errors occurred while sending queued messages."));
792 folder_item_scan(folder->queue);
793 if (prefs_common.savemsg && folder->outbox) {
794 if (folder->outbox == def_outbox)