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();
266 if (gpgme_check_engine()) { /* Also does some gpgme init */
267 rfc2015_disable_all();
268 debug_print("gpgme_engine_version:\n%s\n",
269 gpgme_get_engine_info());
271 if (prefs_common.gpg_warning) {
274 val = alertpanel_message_with_disable
276 _("GnuPG is not installed properly, or needs to be upgraded.\n"
277 "OpenPGP support disabled."));
278 if (val & G_ALERTDISABLE)
279 prefs_common.gpg_warning = FALSE;
282 gpgme_register_idle(idle_function_for_gpgme);
286 gtkaspellcheckers = gtkaspell_checkers_new();
290 prefs_common_save_config();
291 prefs_actions_read_config();
292 prefs_actions_write_config();
293 prefs_display_header_read_config();
294 prefs_display_header_write_config();
295 /* prefs_filtering_read_config(); */
296 addressbook_read_file();
297 renderer_read_config();
301 mainwin = main_window_create
302 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
303 folderview = mainwin->folderview;
305 /* register the callback of unix domain socket input */
306 lock_socket_tag = gdk_input_add(lock_socket,
307 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
308 lock_socket_input_cb,
311 account_read_config_all();
312 account_save_config_all();
314 if (folder_read_list() < 0) {
318 if (!account_get_list()) {
323 account_set_missing_folder();
324 folder_set_missing_folders();
325 folderview_set(folderview);
327 /* prefs_scoring_read_config(); */
328 prefs_matcher_read_config();
330 /* make one all-folder processing before using sylpheed */
331 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
333 /* if Sylpheed crashed, rebuild caches */
334 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
335 debug_print("Sylpheed crashed, checking for new messages\n");
336 folderview_check_new_all();
338 /* make the crash-indicator file */
339 str_write_to_file("foo", get_crashfile_name());
341 addressbook_read_file();
343 inc_autocheck_timer_init(mainwin);
345 /* ignore SIGPIPE signal for preventing sudden death of program */
346 signal(SIGPIPE, SIG_IGN);
348 if (cmd.receive_all || prefs_common.chk_on_startup)
349 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
350 else if (cmd.receive)
351 inc_mail(mainwin, prefs_common.newmail_notify_manu);
353 gtk_widget_grab_focus(folderview->ctree);
356 open_compose_new(cmd.compose_mailto, cmd.attach_files);
357 if (cmd.attach_files) {
358 ptr_array_free_strings(cmd.attach_files);
359 g_ptr_array_free(cmd.attach_files, TRUE);
360 cmd.attach_files = NULL;
365 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
366 main_window_toggle_work_offline(mainwin, TRUE);
367 if (cmd.online_mode == ONLINE_MODE_ONLINE)
368 main_window_toggle_work_offline(mainwin, FALSE);
372 addressbook_destroy();
375 gtkaspell_checkers_delete();
381 static void parse_cmd_opt(int argc, char *argv[])
385 for (i = 1; i < argc; i++) {
386 if (!strncmp(argv[i], "--debug", 7))
388 else if (!strncmp(argv[i], "--receive-all", 13))
389 cmd.receive_all = TRUE;
390 else if (!strncmp(argv[i], "--receive", 9))
392 else if (!strncmp(argv[i], "--compose", 9)) {
393 const gchar *p = argv[i + 1];
396 cmd.compose_mailto = NULL;
397 if (p && *p != '\0' && *p != '-') {
398 if (!strncmp(p, "mailto:", 7))
399 cmd.compose_mailto = p + 7;
401 cmd.compose_mailto = p;
404 } else if (!strncmp(argv[i], "--attach", 8)) {
405 const gchar *p = argv[i + 1];
408 while (p && *p != '\0' && *p != '-') {
409 if (!cmd.attach_files)
410 cmd.attach_files = g_ptr_array_new();
411 if (*p != G_DIR_SEPARATOR)
412 file = g_strconcat(startup_dir,
417 g_ptr_array_add(cmd.attach_files, file);
421 } else if (!strncmp(argv[i], "--send", 6)) {
423 } else if (!strncmp(argv[i], "--version", 9)) {
424 puts("Sylpheed version " VERSION);
426 } else if (!strncmp(argv[i], "--status", 8)) {
428 } else if (!strncmp(argv[i], "--online", 8)) {
429 cmd.online_mode = ONLINE_MODE_ONLINE;
430 } else if (!strncmp(argv[i], "--offline", 9)) {
431 cmd.online_mode = ONLINE_MODE_OFFLINE;
432 } else if (!strncmp(argv[i], "--help", 6)) {
433 g_print(_("Usage: %s [OPTION]...\n"),
434 g_basename(argv[0]));
436 puts(_(" --compose [address] open composition window"));
437 puts(_(" --attach file1 [file2]...\n"
438 " open composition window with specified files\n"
440 puts(_(" --receive receive new messages"));
441 puts(_(" --receive-all receive new messages of all accounts"));
442 puts(_(" --send send all queued messages"));
443 puts(_(" --status show the total number of messages"));
444 puts(_(" --online switch to online mode"));
445 puts(_(" --offline switch to offline mode"));
446 puts(_(" --debug debug mode"));
447 puts(_(" --help display this help and exit"));
448 puts(_(" --version output version information and exit"));
451 } else if (!strncmp(argv[i], "--crash", 7)) {
453 cmd.crash_params = g_strdup(argv[i + 1]);
459 if (cmd.attach_files && cmd.compose == FALSE) {
461 cmd.compose_mailto = NULL;
465 static gint get_queued_message_num(void)
469 queue = folder_get_default_queue();
470 if (!queue) return -1;
472 folder_item_scan(queue);
476 static void save_all_caches(FolderItem *item, gpointer data)
480 folder_item_write_cache(item);
483 static void initial_processing(FolderItem *item, gpointer data)
485 MainWindow *mainwin = (MainWindow *)data;
488 g_return_if_fail(item);
489 buf = g_strdup_printf(_("Processing (%s)..."),
492 : _("top level folder"));
493 debug_print("%s\n", buf);
496 main_window_cursor_wait(mainwin);
498 folder_item_apply_processing(item);
500 debug_print("done.\n");
501 STATUSBAR_POP(mainwin);
502 main_window_cursor_normal(mainwin);
505 void app_will_exit(GtkWidget *widget, gpointer data)
507 MainWindow *mainwin = data;
510 if (compose_get_compose_list()) {
511 if (alertpanel(_("Notice"),
512 _("Composing message exists. Really quit?"),
513 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
515 manage_window_focus_in(mainwin->window, NULL, NULL);
518 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
519 if (alertpanel(_("Queued messages"),
520 _("Some unsent messages are queued. Exit now?"),
521 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
523 manage_window_focus_in(mainwin->window, NULL, NULL);
526 inc_autocheck_timer_remove();
529 gpgmegtk_free_passphrase();
532 if (prefs_common.clean_on_exit)
533 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
535 /* save prefs for opened folder */
536 if(mainwin->folderview->opened)
540 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
541 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
544 /* save all state before exiting */
546 folder_func_to_all_folders(save_all_caches, NULL);
548 main_window_get_size(mainwin);
549 main_window_get_position(mainwin);
550 prefs_common_save_config();
551 account_save_config_all();
552 addressbook_export_to_file();
554 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
555 gtk_item_factory_dump_rc(filename, NULL, TRUE);
558 /* delete temporary files */
559 remove_all_files(get_mime_tmp_dir());
563 /* delete unix domain socket */
564 gdk_input_remove(lock_socket_tag);
565 fd_close(lock_socket);
566 filename = get_socket_name();
569 /* delete crashfile */
571 unlink(get_crashfile_name());
581 static void idle_function_for_gpgme(void)
583 while (gtk_events_pending())
584 gtk_main_iteration();
586 #endif /* USE_GPGME */
589 * CLAWS: want this public so crash dialog can delete the
595 gchar *get_socket_name(void)
597 static gchar *filename = NULL;
599 if (filename == NULL) {
600 filename = g_strdup_printf("%s%csylpheed-%d",
601 g_get_tmp_dir(), G_DIR_SEPARATOR,
608 static gchar *get_crashfile_name(void)
610 static gchar *filename = NULL;
612 if (filename == NULL) {
613 filename = g_strdup_printf("%s%csylpheed-crashed",
614 get_tmp_dir(), G_DIR_SEPARATOR);
620 static gint prohibit_duplicate_launch(void)
625 path = get_socket_name();
626 uxsock = fd_connect_unix(path);
629 return fd_open_unix(path);
632 /* remote command mode */
634 debug_print("another Sylpheed is already running.\n");
637 fd_write(uxsock, "receive_all\n", 12);
638 else if (cmd.receive)
639 fd_write(uxsock, "receive\n", 8);
640 else if (cmd.compose && cmd.attach_files) {
641 gchar *str, *compose_str;
644 if (cmd.compose_mailto)
645 compose_str = g_strdup_printf("compose_attach %s\n",
648 compose_str = g_strdup("compose_attach\n");
650 fd_write(uxsock, compose_str, strlen(compose_str));
653 for (i = 0; i < cmd.attach_files->len; i++) {
654 str = g_ptr_array_index(cmd.attach_files, i);
655 fd_write(uxsock, str, strlen(str));
656 fd_write(uxsock, "\n", 1);
659 fd_write(uxsock, ".\n", 2);
660 } else if (cmd.compose) {
663 if (cmd.compose_mailto)
664 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
666 compose_str = g_strdup("compose\n");
668 fd_write(uxsock, compose_str, strlen(compose_str));
670 } else if (cmd.send) {
671 fd_write(uxsock, "send\n", 5);
672 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
673 fd_write(uxsock, "online\n", 6);
674 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
675 fd_write(uxsock, "offline\n", 7);
676 } else if (cmd.status) {
679 fd_write(uxsock, "status\n", 7);
680 fd_gets(uxsock, buf, sizeof(buf));
683 fd_write(uxsock, "popup\n", 6);
689 static void lock_socket_input_cb(gpointer data,
691 GdkInputCondition condition)
693 MainWindow *mainwin = (MainWindow *)data;
697 sock = fd_accept(source);
698 fd_gets(sock, buf, sizeof(buf));
700 if (!strncmp(buf, "popup", 5)) {
701 main_window_popup(mainwin);
702 } else if (!strncmp(buf, "receive_all", 11)) {
703 main_window_popup(mainwin);
704 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
705 } else if (!strncmp(buf, "receive", 7)) {
706 main_window_popup(mainwin);
707 inc_mail(mainwin, prefs_common.newmail_notify_manu);
708 } else if (!strncmp(buf, "compose_attach", 14)) {
712 mailto = g_strdup(buf + strlen("compose_attach") + 1);
713 files = g_ptr_array_new();
714 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
715 if (buf[0] == '.' && buf[1] == '\n') break;
717 g_ptr_array_add(files, g_strdup(buf));
719 open_compose_new(mailto, files);
720 ptr_array_free_strings(files);
721 g_ptr_array_free(files, TRUE);
723 } else if (!strncmp(buf, "compose", 7)) {
724 open_compose_new(buf + strlen("compose") + 1, NULL);
725 } else if (!strncmp(buf, "send", 4)) {
727 } else if (!strncmp(buf, "online", 6)) {
728 main_window_toggle_work_offline(mainwin, FALSE);
729 } else if (!strncmp(buf, "offline", 7)) {
730 main_window_toggle_work_offline(mainwin, TRUE);
731 } else if (!strncmp(buf, "status", 6)) {
732 guint new, unread, total;
734 folder_count_total_msgs(&new, &unread, &total);
735 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
736 fd_write(sock, buf, strlen(buf));
742 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
747 Xstrdup_a(addr, address, return);
751 compose_new(NULL, addr, attach_files);
754 static void send_queue(void)
757 FolderItem *def_outbox;
759 def_outbox = folder_get_default_outbox();
761 for (list = folder_get_list(); list != NULL; list = list->next) {
762 Folder *folder = list->data;
765 if (procmsg_send_queue
766 (folder->queue, prefs_common.savemsg) < 0)
767 alertpanel_error(_("Some errors occurred while sending queued messages."));
769 folder_item_scan(folder->queue);
770 folderview_update_item(folder->queue, TRUE);
771 if (prefs_common.savemsg && folder->outbox) {
772 folderview_update_item(folder->outbox, TRUE);
773 if (folder->outbox == def_outbox)
779 if (prefs_common.savemsg && def_outbox)
780 folderview_update_item(def_outbox, TRUE);