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_filter.h"
60 #include "prefs_account.h"
61 #include "prefs_actions.h"
63 #include "prefs_display_header.h"
68 #include "manage_window.h"
69 #include "alertpanel.h"
70 #include "statusbar.h"
71 #include "addressbook.h"
92 gboolean debug_mode = FALSE;
94 static gint lock_socket = -1;
95 static gint lock_socket_tag = 0;
101 const gchar *compose_mailto;
102 GPtrArray *attach_files;
109 static void parse_cmd_opt(int argc, char *argv[]);
112 static void idle_function_for_gpgme(void);
113 #endif /* USE_GPGME */
115 static gint prohibit_duplicate_launch (void);
116 static gchar * get_crashfile_name (void);
117 static void lock_socket_input_cb (gpointer data,
119 GdkInputCondition condition);
123 gchar *get_socket_name (void);
126 static void open_compose_new (const gchar *address,
127 GPtrArray *attach_files);
129 static void send_queue (void);
130 static void initial_processing (FolderItem *item, gpointer data);
134 _("File `%s' already exists.\n"
135 "Can't create folder.")
138 #define MAKE_DIR_IF_NOT_EXIST(dir) \
140 if (!is_dir_exist(dir)) { \
141 if (is_file_exist(dir)) { \
143 (_("File `%s' already exists.\n" \
144 "Can't create folder."), \
148 if (make_dir(dir) < 0) \
153 int main(int argc, char *argv[])
157 FolderView *folderview;
159 setlocale(LC_ALL, "");
160 bindtextdomain(PACKAGE, LOCALEDIR);
163 prog_version = PROG_VERSION;
164 startup_dir = g_get_current_dir();
165 argv0 = g_strdup(argv[0]);
167 parse_cmd_opt(argc, argv);
170 gtk_init(&argc, &argv);
173 crash_main(cmd.crash_params);
177 crash_install_handlers();
179 #if USE_THREADS || USE_LDAP
181 if (!g_thread_supported())
182 g_error(_("g_thread is not supported by glib.\n"));
187 gtk_widget_push_visual(gdk_imlib_get_visual());
188 gtk_widget_push_colormap(gdk_imlib_get_colormap());
195 srandom((gint)time(NULL));
197 /* parse gtkrc files */
198 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
200 gtk_rc_parse(userrc);
202 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
203 G_DIR_SEPARATOR_S, "gtkrc", NULL);
204 gtk_rc_parse(userrc);
206 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
207 gtk_rc_parse(userrc);
210 gtk_rc_parse("./gtkrc");
212 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
213 gtk_item_factory_parse_rc(userrc);
216 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
218 /* check and create unix domain socket */
219 lock_socket = prohibit_duplicate_launch();
220 if (lock_socket < 0) return 0;
223 puts("0 Sylpheed not running.");
227 /* backup if old rc file exists */
228 if (is_file_exist(RC_DIR)) {
229 if (rename(RC_DIR, RC_DIR ".bak") < 0)
230 FILE_OP_ERROR(RC_DIR, "rename");
232 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
233 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
234 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
235 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
236 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
237 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
239 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
240 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
241 RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
242 FILE_OP_ERROR("sylpheed.log", "rename");
244 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
246 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
247 !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
248 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
249 RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
250 FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
255 prefs_common_read_config();
258 if (gpgme_check_engine()) { /* Also does some gpgme init */
259 rfc2015_disable_all();
260 debug_print("gpgme_engine_version:\n%s\n",
261 gpgme_get_engine_info());
263 if (prefs_common.gpg_warning) {
266 val = alertpanel_message_with_disable
268 _("GnuPG is not installed properly, or needs to be upgraded.\n"
269 "OpenPGP support disabled."));
270 if (val & G_ALERTDISABLE)
271 prefs_common.gpg_warning = FALSE;
274 gpgme_register_idle(idle_function_for_gpgme);
278 gtkaspellcheckers = gtkaspell_checkers_new();
282 prefs_common_save_config();
283 prefs_filter_read_config();
284 prefs_filter_write_config();
285 prefs_actions_read_config();
286 prefs_actions_write_config();
287 prefs_display_header_read_config();
288 prefs_display_header_write_config();
289 /* prefs_filtering_read_config(); */
290 addressbook_read_file();
291 renderer_read_config();
295 mainwin = main_window_create
296 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
297 folderview = mainwin->folderview;
299 /* register the callback of unix domain socket input */
300 lock_socket_tag = gdk_input_add(lock_socket,
301 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
302 lock_socket_input_cb,
305 account_read_config_all();
306 account_save_config_all();
308 if (folder_read_list() < 0) {
312 if (!account_get_list()) {
317 account_set_missing_folder();
318 folder_set_missing_folders();
319 folderview_set(folderview);
321 /* prefs_scoring_read_config(); */
322 prefs_matcher_read_config();
324 /* make one all-folder processing before using sylpheed */
325 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
327 /* if Sylpheed crashed, rebuild caches */
328 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
329 debug_print("Sylpheed crashed, checking for new messages\n");
330 folderview_check_new_all();
332 /* make the crash-indicator file */
333 str_write_to_file("foo", get_crashfile_name());
335 addressbook_read_file();
337 inc_autocheck_timer_init(mainwin);
339 /* ignore SIGPIPE signal for preventing sudden death of program */
340 signal(SIGPIPE, SIG_IGN);
342 if (cmd.receive_all || prefs_common.chk_on_startup)
343 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
344 else if (cmd.receive)
345 inc_mail(mainwin, prefs_common.newmail_notify_manu);
347 gtk_widget_grab_focus(folderview->ctree);
350 open_compose_new(cmd.compose_mailto, cmd.attach_files);
351 if (cmd.attach_files) {
352 ptr_array_free_strings(cmd.attach_files);
353 g_ptr_array_free(cmd.attach_files, TRUE);
354 cmd.attach_files = NULL;
361 addressbook_destroy();
364 gtkaspell_checkers_delete();
370 static void parse_cmd_opt(int argc, char *argv[])
374 for (i = 1; i < argc; i++) {
375 if (!strncmp(argv[i], "--debug", 7))
377 else if (!strncmp(argv[i], "--receive-all", 13))
378 cmd.receive_all = TRUE;
379 else if (!strncmp(argv[i], "--receive", 9))
381 else if (!strncmp(argv[i], "--compose", 9)) {
382 const gchar *p = argv[i + 1];
385 cmd.compose_mailto = NULL;
386 if (p && *p != '\0' && *p != '-') {
387 if (!strncmp(p, "mailto:", 7))
388 cmd.compose_mailto = p + 7;
390 cmd.compose_mailto = p;
393 } else if (!strncmp(argv[i], "--attach", 8)) {
394 const gchar *p = argv[i + 1];
397 while (p && *p != '\0' && *p != '-') {
398 if (!cmd.attach_files)
399 cmd.attach_files = g_ptr_array_new();
400 if (*p != G_DIR_SEPARATOR)
401 file = g_strconcat(startup_dir,
406 g_ptr_array_add(cmd.attach_files, file);
410 } else if (!strncmp(argv[i], "--send", 6)) {
412 } else if (!strncmp(argv[i], "--version", 9)) {
413 puts("Sylpheed version " VERSION);
415 } else if (!strncmp(argv[i], "--status", 8)) {
417 } else if (!strncmp(argv[i], "--help", 6)) {
418 g_print(_("Usage: %s [OPTION]...\n"),
419 g_basename(argv[0]));
421 puts(_(" --compose [address] open composition window"));
422 puts(_(" --attach file1 [file2]...\n"
423 " open composition window with specified files\n"
425 puts(_(" --receive receive new messages"));
426 puts(_(" --receive-all receive new messages of all accounts"));
427 puts(_(" --send send all queued messages"));
428 puts(_(" --status show the total number of messages"));
429 puts(_(" --debug debug mode"));
430 puts(_(" --help display this help and exit"));
431 puts(_(" --version output version information and exit"));
434 } else if (!strncmp(argv[i], "--crash", 7)) {
436 cmd.crash_params = g_strdup(argv[i + 1]);
442 if (cmd.attach_files && cmd.compose == FALSE) {
444 cmd.compose_mailto = NULL;
448 static gint get_queued_message_num(void)
452 queue = folder_get_default_queue();
453 if (!queue) return -1;
455 folder_item_scan(queue);
459 static void save_all_caches(FolderItem *item, gpointer data)
463 folder_item_write_cache(item);
466 static void initial_processing(FolderItem *item, gpointer data)
468 MainWindow *mainwin = (MainWindow *)data;
471 g_return_if_fail(item);
472 buf = g_strdup_printf(_("Processing (%s)..."),
475 : _("top level folder"));
476 debug_print("%s\n", buf);
479 main_window_cursor_wait(mainwin);
481 folder_item_apply_processing(item);
483 debug_print("done.\n");
484 STATUSBAR_POP(mainwin);
485 main_window_cursor_normal(mainwin);
488 void app_will_exit(GtkWidget *widget, gpointer data)
490 MainWindow *mainwin = data;
493 if (compose_get_compose_list()) {
494 if (alertpanel(_("Notice"),
495 _("Composing message exists. Really quit?"),
496 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
498 manage_window_focus_in(mainwin->window, NULL, NULL);
501 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
502 if (alertpanel(_("Queued messages"),
503 _("Some unsent messages are queued. Exit now?"),
504 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
506 manage_window_focus_in(mainwin->window, NULL, NULL);
509 inc_autocheck_timer_remove();
512 gpgmegtk_free_passphrase();
515 if (prefs_common.clean_on_exit)
516 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
518 /* save prefs for opened folder */
519 if(mainwin->folderview->opened)
523 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
524 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
527 /* save all state before exiting */
529 folder_func_to_all_folders(save_all_caches, NULL);
531 main_window_get_size(mainwin);
532 main_window_get_position(mainwin);
533 prefs_common_save_config();
534 prefs_filter_write_config();
535 account_save_config_all();
536 addressbook_export_to_file();
538 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
539 gtk_item_factory_dump_rc(filename, NULL, TRUE);
542 /* delete temporary files */
543 remove_all_files(get_mime_tmp_dir());
547 /* delete unix domain socket */
548 gdk_input_remove(lock_socket_tag);
549 fd_close(lock_socket);
550 filename = get_socket_name();
553 /* delete crashfile */
555 unlink(get_crashfile_name());
565 static void idle_function_for_gpgme(void)
567 while (gtk_events_pending())
568 gtk_main_iteration();
570 #endif /* USE_GPGME */
573 * CLAWS: want this public so crash dialog can delete the
579 gchar *get_socket_name(void)
581 static gchar *filename = NULL;
583 if (filename == NULL) {
584 filename = g_strdup_printf("%s%csylpheed-%d",
585 g_get_tmp_dir(), G_DIR_SEPARATOR,
592 static gchar *get_crashfile_name(void)
594 static gchar *filename = NULL;
596 if (filename == NULL) {
597 filename = g_strdup_printf("%s%csylpheed-crashed",
598 get_tmp_dir(), G_DIR_SEPARATOR);
604 static gint prohibit_duplicate_launch(void)
609 path = get_socket_name();
610 uxsock = fd_connect_unix(path);
613 return fd_open_unix(path);
616 /* remote command mode */
618 debug_print("another Sylpheed is already running.\n");
621 fd_write(uxsock, "receive_all\n", 12);
622 else if (cmd.receive)
623 fd_write(uxsock, "receive\n", 8);
624 else if (cmd.compose && cmd.attach_files) {
625 gchar *str, *compose_str;
628 if (cmd.compose_mailto)
629 compose_str = g_strdup_printf("compose_attach %s\n",
632 compose_str = g_strdup("compose_attach\n");
634 fd_write(uxsock, compose_str, strlen(compose_str));
637 for (i = 0; i < cmd.attach_files->len; i++) {
638 str = g_ptr_array_index(cmd.attach_files, i);
639 fd_write(uxsock, str, strlen(str));
640 fd_write(uxsock, "\n", 1);
643 fd_write(uxsock, ".\n", 2);
644 } else if (cmd.compose) {
647 if (cmd.compose_mailto)
648 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
650 compose_str = g_strdup("compose\n");
652 fd_write(uxsock, compose_str, strlen(compose_str));
654 } else if (cmd.send) {
655 fd_write(uxsock, "send\n", 5);
656 } else if (cmd.status) {
659 fd_write(uxsock, "status\n", 7);
660 fd_gets(uxsock, buf, sizeof(buf));
663 fd_write(uxsock, "popup\n", 6);
669 static void lock_socket_input_cb(gpointer data,
671 GdkInputCondition condition)
673 MainWindow *mainwin = (MainWindow *)data;
677 sock = fd_accept(source);
678 fd_gets(sock, buf, sizeof(buf));
680 if (!strncmp(buf, "popup", 5)) {
681 main_window_popup(mainwin);
682 } else if (!strncmp(buf, "receive_all", 11)) {
683 main_window_popup(mainwin);
684 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
685 } else if (!strncmp(buf, "receive", 7)) {
686 main_window_popup(mainwin);
687 inc_mail(mainwin, prefs_common.newmail_notify_manu);
688 } else if (!strncmp(buf, "compose_attach", 14)) {
692 mailto = g_strdup(buf + strlen("compose_attach") + 1);
693 files = g_ptr_array_new();
694 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
695 if (buf[0] == '.' && buf[1] == '\n') break;
697 g_ptr_array_add(files, g_strdup(buf));
699 open_compose_new(mailto, files);
700 ptr_array_free_strings(files);
701 g_ptr_array_free(files, TRUE);
703 } else if (!strncmp(buf, "compose", 7)) {
704 open_compose_new(buf + strlen("compose") + 1, NULL);
705 } else if (!strncmp(buf, "send", 4)) {
707 } else if (!strncmp(buf, "status", 6)) {
708 guint new, unread, total;
710 folder_count_total_msgs(&new, &unread, &total);
711 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
712 fd_write(sock, buf, strlen(buf));
718 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
723 Xstrdup_a(addr, address, return);
727 compose_new(NULL, addr, attach_files);
730 static void send_queue(void)
733 FolderItem *def_outbox;
735 def_outbox = folder_get_default_outbox();
737 for (list = folder_get_list(); list != NULL; list = list->next) {
738 Folder *folder = list->data;
741 if (procmsg_send_queue
742 (folder->queue, prefs_common.savemsg) < 0)
743 alertpanel_error(_("Some errors occurred while sending queued messages."));
745 folder_item_scan(folder->queue);
746 folderview_update_item(folder->queue, TRUE);
747 if (prefs_common.savemsg && folder->outbox) {
748 folderview_update_item(folder->outbox, TRUE);
749 if (folder->outbox == def_outbox)
755 if (prefs_common.savemsg && def_outbox)
756 folderview_update_item(def_outbox, TRUE);