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 static MainWindow *static_mainwindow;
162 int main(int argc, char *argv[])
166 FolderView *folderview;
168 setlocale(LC_ALL, "");
169 bindtextdomain(PACKAGE, LOCALEDIR);
172 prog_version = PROG_VERSION;
173 startup_dir = g_get_current_dir();
174 argv0 = g_strdup(argv[0]);
176 parse_cmd_opt(argc, argv);
178 /* check and create unix domain socket */
179 lock_socket = prohibit_duplicate_launch();
180 if (lock_socket < 0) return 0;
183 puts("0 Sylpheed not running.");
188 gtk_init(&argc, &argv);
192 crash_main(cmd.crash_params);
195 crash_install_handlers();
198 #if USE_THREADS || USE_LDAP
200 if (!g_thread_supported())
201 g_error(_("g_thread is not supported by glib.\n"));
206 gtk_widget_push_visual(gdk_imlib_get_visual());
207 gtk_widget_push_colormap(gdk_imlib_get_colormap());
214 srandom((gint)time(NULL));
216 /* parse gtkrc files */
217 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
219 gtk_rc_parse(userrc);
221 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
222 G_DIR_SEPARATOR_S, "gtkrc", NULL);
223 gtk_rc_parse(userrc);
225 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
226 gtk_rc_parse(userrc);
229 gtk_rc_parse("./gtkrc");
231 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
232 gtk_item_factory_parse_rc(userrc);
235 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
237 /* backup if old rc file exists */
238 if (is_file_exist(RC_DIR)) {
239 if (rename(RC_DIR, RC_DIR ".bak") < 0)
240 FILE_OP_ERROR(RC_DIR, "rename");
242 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
243 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
244 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
245 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
246 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
247 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
249 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
250 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
251 RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
252 FILE_OP_ERROR("sylpheed.log", "rename");
254 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
257 prefs_common_read_config();
261 if (gpgme_check_engine()) { /* Also does some gpgme init */
262 rfc2015_disable_all();
263 debug_print("gpgme_engine_version:\n%s\n",
264 gpgme_get_engine_info());
266 if (prefs_common.gpg_warning) {
269 val = alertpanel_message_with_disable
271 _("GnuPG is not installed properly, or needs to be upgraded.\n"
272 "OpenPGP support disabled."));
273 if (val & G_ALERTDISABLE)
274 prefs_common.gpg_warning = FALSE;
279 gpgme_register_idle(idle_function_for_gpgme);
283 gtkaspellcheckers = gtkaspell_checkers_new();
287 prefs_common_save_config();
288 prefs_actions_read_config();
289 prefs_actions_write_config();
290 prefs_display_header_read_config();
291 prefs_display_header_write_config();
292 /* prefs_filtering_read_config(); */
293 addressbook_read_file();
294 renderer_read_config();
298 mainwin = main_window_create
299 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
300 folderview = mainwin->folderview;
302 /* register the callback of unix domain socket input */
303 lock_socket_tag = gdk_input_add(lock_socket,
304 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
305 lock_socket_input_cb,
308 account_read_config_all();
309 account_save_config_all();
311 if (folder_read_list() < 0) {
315 if (!account_get_list()) {
320 account_set_missing_folder();
321 folder_set_missing_folders();
322 folderview_set(folderview);
324 /* prefs_scoring_read_config(); */
325 prefs_matcher_read_config();
327 /* make one all-folder processing before using sylpheed */
328 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
330 /* if Sylpheed crashed, rebuild caches */
331 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
332 debug_print("Sylpheed crashed, checking for new messages\n");
333 folderview_check_new_all();
335 /* make the crash-indicator file */
336 str_write_to_file("foo", get_crashfile_name());
338 addressbook_read_file();
340 inc_autocheck_timer_init(mainwin);
342 /* ignore SIGPIPE signal for preventing sudden death of program */
343 signal(SIGPIPE, SIG_IGN);
345 if (cmd.receive_all || prefs_common.chk_on_startup)
346 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
347 else if (cmd.receive)
348 inc_mail(mainwin, prefs_common.newmail_notify_manu);
350 gtk_widget_grab_focus(folderview->ctree);
353 open_compose_new(cmd.compose_mailto, cmd.attach_files);
354 if (cmd.attach_files) {
355 ptr_array_free_strings(cmd.attach_files);
356 g_ptr_array_free(cmd.attach_files, TRUE);
357 cmd.attach_files = NULL;
362 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
363 main_window_toggle_work_offline(mainwin, TRUE);
364 if (cmd.online_mode == ONLINE_MODE_ONLINE)
365 main_window_toggle_work_offline(mainwin, FALSE);
367 static_mainwindow = mainwin;
370 addressbook_destroy();
373 gtkaspell_checkers_delete();
379 static void parse_cmd_opt(int argc, char *argv[])
383 for (i = 1; i < argc; i++) {
384 if (!strncmp(argv[i], "--debug", 7))
386 else if (!strncmp(argv[i], "--receive-all", 13))
387 cmd.receive_all = TRUE;
388 else if (!strncmp(argv[i], "--receive", 9))
390 else if (!strncmp(argv[i], "--compose", 9)) {
391 const gchar *p = argv[i + 1];
394 cmd.compose_mailto = NULL;
395 if (p && *p != '\0' && *p != '-') {
396 if (!strncmp(p, "mailto:", 7))
397 cmd.compose_mailto = p + 7;
399 cmd.compose_mailto = p;
402 } else if (!strncmp(argv[i], "--attach", 8)) {
403 const gchar *p = argv[i + 1];
406 while (p && *p != '\0' && *p != '-') {
407 if (!cmd.attach_files)
408 cmd.attach_files = g_ptr_array_new();
409 if (*p != G_DIR_SEPARATOR)
410 file = g_strconcat(startup_dir,
415 g_ptr_array_add(cmd.attach_files, file);
419 } else if (!strncmp(argv[i], "--send", 6)) {
421 } else if (!strncmp(argv[i], "--version", 9)) {
422 puts("Sylpheed version " VERSION);
424 } else if (!strncmp(argv[i], "--status", 8)) {
426 } else if (!strncmp(argv[i], "--online", 8)) {
427 cmd.online_mode = ONLINE_MODE_ONLINE;
428 } else if (!strncmp(argv[i], "--offline", 9)) {
429 cmd.online_mode = ONLINE_MODE_OFFLINE;
430 } else if (!strncmp(argv[i], "--help", 6)) {
431 g_print(_("Usage: %s [OPTION]...\n"),
432 g_basename(argv[0]));
434 puts(_(" --compose [address] open composition window"));
435 puts(_(" --attach file1 [file2]...\n"
436 " open composition window with specified files\n"
438 puts(_(" --receive receive new messages"));
439 puts(_(" --receive-all receive new messages of all accounts"));
440 puts(_(" --send send all queued messages"));
441 puts(_(" --status show the total number of messages"));
442 puts(_(" --online switch to online mode"));
443 puts(_(" --offline switch to offline mode"));
444 puts(_(" --debug debug mode"));
445 puts(_(" --help display this help and exit"));
446 puts(_(" --version output version information and exit"));
449 } else if (!strncmp(argv[i], "--crash", 7)) {
451 cmd.crash_params = g_strdup(argv[i + 1]);
457 if (cmd.attach_files && cmd.compose == FALSE) {
459 cmd.compose_mailto = NULL;
463 static gint get_queued_message_num(void)
467 queue = folder_get_default_queue();
468 if (!queue) return -1;
470 folder_item_scan(queue);
474 static void save_all_caches(FolderItem *item, gpointer data)
478 folder_item_write_cache(item);
481 static void initial_processing(FolderItem *item, gpointer data)
483 MainWindow *mainwin = (MainWindow *)data;
486 g_return_if_fail(item);
487 buf = g_strdup_printf(_("Processing (%s)..."),
490 : _("top level folder"));
491 debug_print("%s\n", buf);
494 main_window_cursor_wait(mainwin);
496 folder_item_apply_processing(item);
498 debug_print("done.\n");
499 STATUSBAR_POP(mainwin);
500 main_window_cursor_normal(mainwin);
503 static void draft_all_messages(void)
505 GList * compose_list = compose_get_compose_list();
508 for (elem = compose_list; elem != NULL && elem->data != NULL; elem = elem->next) {
509 Compose *c = (Compose*)elem->data;
515 void clean_quit(void)
517 draft_all_messages();
519 if (prefs_common.warn_queued_on_exit)
520 { /* disable the popup */
521 prefs_common.warn_queued_on_exit = FALSE;
522 app_will_exit(NULL, static_mainwindow);
523 prefs_common.warn_queued_on_exit = TRUE;
524 prefs_common_save_config();
526 app_will_exit(NULL, static_mainwindow);
531 void app_will_exit(GtkWidget *widget, gpointer data)
533 MainWindow *mainwin = data;
536 if (compose_get_compose_list()) {
537 gint val = alertpanel(_("Notice"),
538 _("Composing message exists."),
539 _("Draft them"), _("Discard them"), _("Don't quit"));
543 case G_ALERTALTERNATE:
546 draft_all_messages();
549 manage_window_focus_in(mainwin->window, NULL, NULL);
552 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
553 if (alertpanel(_("Queued messages"),
554 _("Some unsent messages are queued. Exit now?"),
555 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
557 manage_window_focus_in(mainwin->window, NULL, NULL);
560 inc_autocheck_timer_remove();
563 gpgmegtk_free_passphrase();
566 if (prefs_common.clean_on_exit)
567 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
569 /* save prefs for opened folder */
570 if(mainwin->folderview->opened)
574 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
575 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
578 /* save all state before exiting */
580 folder_func_to_all_folders(save_all_caches, NULL);
582 main_window_get_size(mainwin);
583 main_window_get_position(mainwin);
584 prefs_common_save_config();
585 account_save_config_all();
586 addressbook_export_to_file();
588 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
589 gtk_item_factory_dump_rc(filename, NULL, TRUE);
592 /* delete temporary files */
593 remove_all_files(get_mime_tmp_dir());
597 /* delete unix domain socket */
598 gdk_input_remove(lock_socket_tag);
599 fd_close(lock_socket);
600 filename = get_socket_name();
603 /* delete crashfile */
605 unlink(get_crashfile_name());
615 static void idle_function_for_gpgme(void)
617 while (gtk_events_pending())
618 gtk_main_iteration();
620 #endif /* USE_GPGME */
623 * CLAWS: want this public so crash dialog can delete the
629 gchar *get_socket_name(void)
631 static gchar *filename = NULL;
633 if (filename == NULL) {
634 filename = g_strdup_printf("%s%csylpheed-%d",
635 g_get_tmp_dir(), G_DIR_SEPARATOR,
642 static gchar *get_crashfile_name(void)
644 static gchar *filename = NULL;
646 if (filename == NULL) {
647 filename = g_strdup_printf("%s%csylpheed-crashed",
648 get_tmp_dir(), G_DIR_SEPARATOR);
654 static gint prohibit_duplicate_launch(void)
659 path = get_socket_name();
660 uxsock = fd_connect_unix(path);
663 return fd_open_unix(path);
666 /* remote command mode */
668 debug_print("another Sylpheed is already running.\n");
671 fd_write(uxsock, "receive_all\n", 12);
672 else if (cmd.receive)
673 fd_write(uxsock, "receive\n", 8);
674 else if (cmd.compose && cmd.attach_files) {
675 gchar *str, *compose_str;
678 if (cmd.compose_mailto)
679 compose_str = g_strdup_printf("compose_attach %s\n",
682 compose_str = g_strdup("compose_attach\n");
684 fd_write(uxsock, compose_str, strlen(compose_str));
687 for (i = 0; i < cmd.attach_files->len; i++) {
688 str = g_ptr_array_index(cmd.attach_files, i);
689 fd_write(uxsock, str, strlen(str));
690 fd_write(uxsock, "\n", 1);
693 fd_write(uxsock, ".\n", 2);
694 } else if (cmd.compose) {
697 if (cmd.compose_mailto)
698 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
700 compose_str = g_strdup("compose\n");
702 fd_write(uxsock, compose_str, strlen(compose_str));
704 } else if (cmd.send) {
705 fd_write(uxsock, "send\n", 5);
706 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
707 fd_write(uxsock, "online\n", 6);
708 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
709 fd_write(uxsock, "offline\n", 7);
710 } else if (cmd.status) {
713 fd_write(uxsock, "status\n", 7);
714 fd_gets(uxsock, buf, sizeof(buf));
717 fd_write(uxsock, "popup\n", 6);
723 static void lock_socket_input_cb(gpointer data,
725 GdkInputCondition condition)
727 MainWindow *mainwin = (MainWindow *)data;
731 sock = fd_accept(source);
732 fd_gets(sock, buf, sizeof(buf));
734 if (!strncmp(buf, "popup", 5)) {
735 main_window_popup(mainwin);
736 } else if (!strncmp(buf, "receive_all", 11)) {
737 main_window_popup(mainwin);
738 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
739 } else if (!strncmp(buf, "receive", 7)) {
740 main_window_popup(mainwin);
741 inc_mail(mainwin, prefs_common.newmail_notify_manu);
742 } else if (!strncmp(buf, "compose_attach", 14)) {
746 mailto = g_strdup(buf + strlen("compose_attach") + 1);
747 files = g_ptr_array_new();
748 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
749 if (buf[0] == '.' && buf[1] == '\n') break;
751 g_ptr_array_add(files, g_strdup(buf));
753 open_compose_new(mailto, files);
754 ptr_array_free_strings(files);
755 g_ptr_array_free(files, TRUE);
757 } else if (!strncmp(buf, "compose", 7)) {
758 open_compose_new(buf + strlen("compose") + 1, NULL);
759 } else if (!strncmp(buf, "send", 4)) {
761 } else if (!strncmp(buf, "online", 6)) {
762 main_window_toggle_work_offline(mainwin, FALSE);
763 } else if (!strncmp(buf, "offline", 7)) {
764 main_window_toggle_work_offline(mainwin, TRUE);
765 } else if (!strncmp(buf, "status", 6)) {
766 guint new, unread, unreadmarked, total;
768 folder_count_total_msgs(&new, &unread, &unreadmarked, &total);
769 g_snprintf(buf, sizeof(buf), "%d %d %d %d\n", new, unread, unreadmarked, total);
770 fd_write(sock, buf, strlen(buf));
776 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
781 Xstrdup_a(addr, address, return);
785 compose_new(NULL, addr, attach_files);
788 static void send_queue(void)
791 FolderItem *def_outbox;
793 def_outbox = folder_get_default_outbox();
795 for (list = folder_get_list(); list != NULL; list = list->next) {
796 Folder *folder = list->data;
799 if (procmsg_send_queue
800 (folder->queue, prefs_common.savemsg) < 0)
801 alertpanel_error(_("Some errors occurred while sending queued messages."));
803 folder_item_scan(folder->queue);
804 if (prefs_common.savemsg && folder->outbox) {
805 folder_update_item(folder->outbox, TRUE);
806 if (folder->outbox == def_outbox)
812 if (prefs_common.savemsg && def_outbox)
813 folder_update_item(def_outbox, TRUE);