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 void lock_socket_input_cb (gpointer data,
118 GdkInputCondition condition);
119 static gchar *get_socket_name (void);
121 static void open_compose_new (const gchar *address,
122 GPtrArray *attach_files);
124 static void send_queue (void);
125 static void initial_processing (FolderItem *item, gpointer data);
129 _("File `%s' already exists.\n"
130 "Can't create folder.")
133 #define MAKE_DIR_IF_NOT_EXIST(dir) \
135 if (!is_dir_exist(dir)) { \
136 if (is_file_exist(dir)) { \
138 (_("File `%s' already exists.\n" \
139 "Can't create folder."), \
143 if (make_dir(dir) < 0) \
148 int main(int argc, char *argv[])
152 FolderView *folderview;
154 setlocale(LC_ALL, "");
155 bindtextdomain(PACKAGE, LOCALEDIR);
158 prog_version = PROG_VERSION;
159 startup_dir = g_get_current_dir();
160 argv0 = g_strdup(argv[0]);
162 parse_cmd_opt(argc, argv);
165 gtk_init(&argc, &argv);
168 crash_main(cmd.crash_params);
172 crash_install_handlers();
174 #if USE_THREADS || USE_LDAP
176 if (!g_thread_supported())
177 g_error(_("g_thread is not supported by glib.\n"));
182 gtk_widget_push_visual(gdk_imlib_get_visual());
183 gtk_widget_push_colormap(gdk_imlib_get_colormap());
190 srandom((gint)time(NULL));
192 /* parse gtkrc files */
193 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
195 gtk_rc_parse(userrc);
197 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
198 G_DIR_SEPARATOR_S, "gtkrc", NULL);
199 gtk_rc_parse(userrc);
201 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
202 gtk_rc_parse(userrc);
205 gtk_rc_parse("./gtkrc");
207 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
208 gtk_item_factory_parse_rc(userrc);
211 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
213 /* check and create unix domain socket */
214 lock_socket = prohibit_duplicate_launch();
215 if (lock_socket < 0) return 0;
218 puts("0 Sylpheed not running.");
222 /* backup if old rc file exists */
223 if (is_file_exist(RC_DIR)) {
224 if (rename(RC_DIR, RC_DIR ".bak") < 0)
225 FILE_OP_ERROR(RC_DIR, "rename");
227 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
228 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
229 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
230 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
231 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
232 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
234 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
235 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
236 RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
237 FILE_OP_ERROR("sylpheed.log", "rename");
239 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
241 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
242 !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
243 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
244 RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
245 FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
250 prefs_common_read_config();
253 if (gpgme_check_engine()) { /* Also does some gpgme init */
254 rfc2015_disable_all();
255 debug_print("gpgme_engine_version:\n%s\n",
256 gpgme_get_engine_info());
258 if (prefs_common.gpg_warning) {
261 val = alertpanel_message_with_disable
263 _("GnuPG is not installed properly.\n"
264 "OpenPGP support disabled."));
265 if (val & G_ALERTDISABLE)
266 prefs_common.gpg_warning = FALSE;
269 gpgme_register_idle(idle_function_for_gpgme);
273 gtkpspellcheckers = gtkpspell_checkers_new();
277 prefs_common_save_config();
278 prefs_filter_read_config();
279 prefs_filter_write_config();
280 prefs_actions_read_config();
281 prefs_actions_write_config();
282 prefs_display_header_read_config();
283 prefs_display_header_write_config();
284 /* prefs_filtering_read_config(); */
285 addressbook_read_file();
286 renderer_read_config();
290 mainwin = main_window_create
291 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
292 folderview = mainwin->folderview;
294 /* register the callback of unix domain socket input */
295 lock_socket_tag = gdk_input_add(lock_socket,
296 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
297 lock_socket_input_cb,
300 account_read_config_all();
301 account_save_config_all();
303 if (folder_read_list() < 0) {
307 if (!account_get_list()) {
312 account_set_missing_folder();
313 folder_set_missing_folders();
314 folderview_set(folderview);
316 /* prefs_scoring_read_config(); */
317 prefs_matcher_read_config();
319 /* make one all-folder processing before using sylpheed */
320 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
322 addressbook_read_file();
324 inc_autocheck_timer_init(mainwin);
326 /* ignore SIGPIPE signal for preventing sudden death of program */
327 signal(SIGPIPE, SIG_IGN);
329 if (cmd.receive_all || prefs_common.chk_on_startup)
330 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
331 else if (cmd.receive)
332 inc_mail(mainwin, prefs_common.newmail_notify_manu);
334 gtk_widget_grab_focus(folderview->ctree);
337 open_compose_new(cmd.compose_mailto, cmd.attach_files);
338 if (cmd.attach_files) {
339 ptr_array_free_strings(cmd.attach_files);
340 g_ptr_array_free(cmd.attach_files, TRUE);
341 cmd.attach_files = NULL;
348 addressbook_destroy();
351 gtkpspell_checkers_delete();
357 static void parse_cmd_opt(int argc, char *argv[])
361 for (i = 1; i < argc; i++) {
362 if (!strncmp(argv[i], "--debug", 7))
364 else if (!strncmp(argv[i], "--receive-all", 13))
365 cmd.receive_all = TRUE;
366 else if (!strncmp(argv[i], "--receive", 9))
368 else if (!strncmp(argv[i], "--compose", 9)) {
369 const gchar *p = argv[i + 1];
372 cmd.compose_mailto = NULL;
373 if (p && *p != '\0' && *p != '-') {
374 if (!strncmp(p, "mailto:", 7))
375 cmd.compose_mailto = p + 7;
377 cmd.compose_mailto = p;
380 } else if (!strncmp(argv[i], "--attach", 8)) {
381 const gchar *p = argv[i + 1];
384 while (p && *p != '\0' && *p != '-') {
385 if (!cmd.attach_files)
386 cmd.attach_files = g_ptr_array_new();
387 if (*p != G_DIR_SEPARATOR)
388 file = g_strconcat(startup_dir,
393 g_ptr_array_add(cmd.attach_files, file);
397 } else if (!strncmp(argv[i], "--send", 6)) {
399 } else if (!strncmp(argv[i], "--version", 9)) {
400 puts("Sylpheed version " VERSION);
402 } else if (!strncmp(argv[i], "--status", 8)) {
404 } else if (!strncmp(argv[i], "--help", 6)) {
405 g_print(_("Usage: %s [OPTION]...\n"),
406 g_basename(argv[0]));
408 puts(_(" --compose [address] open composition window"));
409 puts(_(" --attach file1 [file2]...\n"
410 " open composition window with specified files\n"
412 puts(_(" --receive receive new messages"));
413 puts(_(" --receive-all receive new messages of all accounts"));
414 puts(_(" --send send all queued messages"));
415 puts(_(" --status show the total number of messages"));
416 puts(_(" --debug debug mode"));
417 puts(_(" --help display this help and exit"));
418 puts(_(" --version output version information and exit"));
421 } else if (!strncmp(argv[i], "--crash", 7)) {
423 cmd.crash_params = g_strdup(argv[i + 1]);
429 if (cmd.attach_files && cmd.compose == FALSE) {
431 cmd.compose_mailto = NULL;
435 static gint get_queued_message_num(void)
439 queue = folder_get_default_queue();
440 if (!queue) return -1;
442 folder_item_scan(queue);
446 static void save_all_caches(FolderItem *item, gpointer data)
451 folder_item_write_cache(item);
454 static void initial_processing(FolderItem *item, gpointer data)
456 MainWindow *mainwin = (MainWindow *)data;
459 g_return_if_fail(item);
460 buf = g_strdup_printf(_("Processing (%s)..."),
463 : _("top level folder"));
464 debug_print("%s\n", buf);
467 main_window_cursor_wait(mainwin);
469 folder_item_apply_processing(item);
471 debug_print("done.\n");
472 STATUSBAR_POP(mainwin);
473 main_window_cursor_normal(mainwin);
476 void app_will_exit(GtkWidget *widget, gpointer data)
478 MainWindow *mainwin = data;
481 if (compose_get_compose_list()) {
482 if (alertpanel(_("Notice"),
483 _("Composing message exists. Really quit?"),
484 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
486 manage_window_focus_in(mainwin->window, NULL, NULL);
489 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
490 if (alertpanel(_("Queued messages"),
491 _("Some unsent messages are queued. Exit now?"),
492 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
494 manage_window_focus_in(mainwin->window, NULL, NULL);
497 inc_autocheck_timer_remove();
500 gpgmegtk_free_passphrase();
503 if (prefs_common.clean_on_exit)
504 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
506 /* save prefs for opened folder */
507 if(mainwin->folderview->opened)
511 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
512 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
515 /* save all state before exiting */
517 folder_func_to_all_folders(save_all_caches, NULL);
519 main_window_get_size(mainwin);
520 main_window_get_position(mainwin);
521 prefs_common_save_config();
522 prefs_filter_write_config();
523 account_save_config_all();
524 addressbook_export_to_file();
526 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
527 gtk_item_factory_dump_rc(filename, NULL, TRUE);
530 /* delete temporary files */
531 remove_all_files(get_mime_tmp_dir());
535 /* delete unix domain socket */
536 gdk_input_remove(lock_socket_tag);
537 fd_close(lock_socket);
538 filename = get_socket_name();
549 static void idle_function_for_gpgme(void)
551 while (gtk_events_pending())
552 gtk_main_iteration();
554 #endif /* USE_GPGME */
556 static gchar *get_socket_name(void)
558 static gchar *filename = NULL;
560 if (filename == NULL) {
561 filename = g_strdup_printf("%s%csylpheed-%d",
562 g_get_tmp_dir(), G_DIR_SEPARATOR,
569 static gint prohibit_duplicate_launch(void)
574 path = get_socket_name();
575 uxsock = fd_connect_unix(path);
578 return fd_open_unix(path);
581 /* remote command mode */
583 debug_print("another Sylpheed is already running.\n");
586 fd_write(uxsock, "receive_all\n", 12);
587 else if (cmd.receive)
588 fd_write(uxsock, "receive\n", 8);
589 else if (cmd.compose && cmd.attach_files) {
590 gchar *str, *compose_str;
593 if (cmd.compose_mailto)
594 compose_str = g_strdup_printf("compose_attach %s\n",
597 compose_str = g_strdup("compose_attach\n");
599 fd_write(uxsock, compose_str, strlen(compose_str));
602 for (i = 0; i < cmd.attach_files->len; i++) {
603 str = g_ptr_array_index(cmd.attach_files, i);
604 fd_write(uxsock, str, strlen(str));
605 fd_write(uxsock, "\n", 1);
608 fd_write(uxsock, ".\n", 2);
609 } else if (cmd.compose) {
612 if (cmd.compose_mailto)
613 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
615 compose_str = g_strdup("compose\n");
617 fd_write(uxsock, compose_str, strlen(compose_str));
619 } else if (cmd.send) {
620 fd_write(uxsock, "send\n", 5);
621 } else if (cmd.status) {
624 fd_write(uxsock, "status\n", 7);
625 fd_gets(uxsock, buf, sizeof(buf));
628 fd_write(uxsock, "popup\n", 6);
634 static void lock_socket_input_cb(gpointer data,
636 GdkInputCondition condition)
638 MainWindow *mainwin = (MainWindow *)data;
642 sock = fd_accept(source);
643 fd_gets(sock, buf, sizeof(buf));
645 if (!strncmp(buf, "popup", 5)) {
646 main_window_popup(mainwin);
647 } else if (!strncmp(buf, "receive_all", 11)) {
648 main_window_popup(mainwin);
649 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
650 } else if (!strncmp(buf, "receive", 7)) {
651 main_window_popup(mainwin);
652 inc_mail(mainwin, prefs_common.newmail_notify_manu);
653 } else if (!strncmp(buf, "compose_attach", 14)) {
657 mailto = g_strdup(buf + strlen("compose_attach") + 1);
658 files = g_ptr_array_new();
659 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
660 if (buf[0] == '.' && buf[1] == '\n') break;
662 g_ptr_array_add(files, g_strdup(buf));
664 open_compose_new(mailto, files);
665 ptr_array_free_strings(files);
666 g_ptr_array_free(files, TRUE);
668 } else if (!strncmp(buf, "compose", 7)) {
669 open_compose_new(buf + strlen("compose") + 1, NULL);
670 } else if (!strncmp(buf, "send", 4)) {
672 } else if (!strncmp(buf, "status", 6)) {
673 guint new, unread, total;
675 folder_count_total_msgs(&new, &unread, &total);
676 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
677 fd_write(sock, buf, strlen(buf));
683 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
688 Xstrdup_a(addr, address, return);
692 compose_new(NULL, addr, attach_files);
695 static void send_queue(void)
698 FolderItem *def_outbox;
700 def_outbox = folder_get_default_outbox();
702 for (list = folder_get_list(); list != NULL; list = list->next) {
703 Folder *folder = list->data;
706 if (procmsg_send_queue
707 (folder->queue, prefs_common.savemsg) < 0)
708 alertpanel_error(_("Some errors occurred while sending queued messages."));
710 folder_item_scan(folder->queue);
711 folderview_update_item(folder->queue, TRUE);
712 if (prefs_common.savemsg && folder->outbox) {
713 folderview_update_item(folder->outbox, TRUE);
714 if (folder->outbox == def_outbox)
720 if (prefs_common.savemsg && def_outbox)
721 folderview_update_item(def_outbox, TRUE);