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"
89 gboolean debug_mode = FALSE;
91 static gint lock_socket = -1;
92 static gint lock_socket_tag = 0;
98 const gchar *compose_mailto;
99 GPtrArray *attach_files;
104 static void parse_cmd_opt(int argc, char *argv[]);
107 static void idle_function_for_gpgme(void);
108 #endif /* USE_GPGME */
110 static gint prohibit_duplicate_launch (void);
111 static void lock_socket_input_cb (gpointer data,
113 GdkInputCondition condition);
114 static gchar *get_socket_name (void);
116 static void open_compose_new (const gchar *address,
117 GPtrArray *attach_files);
119 static void send_queue (void);
120 static void initial_processing (FolderItem *item, gpointer data);
124 _("File `%s' already exists.\n"
125 "Can't create folder.")
128 #define MAKE_DIR_IF_NOT_EXIST(dir) \
130 if (!is_dir_exist(dir)) { \
131 if (is_file_exist(dir)) { \
133 (_("File `%s' already exists.\n" \
134 "Can't create folder."), \
138 if (make_dir(dir) < 0) \
143 int main(int argc, char *argv[])
147 FolderView *folderview;
149 setlocale(LC_ALL, "");
150 bindtextdomain(PACKAGE, LOCALEDIR);
153 prog_version = PROG_VERSION;
154 startup_dir = g_get_current_dir();
156 parse_cmd_opt(argc, argv);
159 gtk_init(&argc, &argv);
161 #if USE_THREADS || USE_LDAP
163 if (!g_thread_supported())
164 g_error(_("g_thread is not supported by glib.\n"));
169 gtk_widget_push_visual(gdk_imlib_get_visual());
170 gtk_widget_push_colormap(gdk_imlib_get_colormap());
177 srandom((gint)time(NULL));
179 /* parse gtkrc files */
180 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
182 gtk_rc_parse(userrc);
184 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
185 G_DIR_SEPARATOR_S, "gtkrc", NULL);
186 gtk_rc_parse(userrc);
188 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
189 gtk_rc_parse(userrc);
192 gtk_rc_parse("./gtkrc");
194 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
195 gtk_item_factory_parse_rc(userrc);
198 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
200 /* check and create unix domain socket */
201 lock_socket = prohibit_duplicate_launch();
202 if (lock_socket < 0) return 0;
205 puts("0 Sylpheed not running.");
209 /* backup if old rc file exists */
210 if (is_file_exist(RC_DIR)) {
211 if (rename(RC_DIR, RC_DIR ".bak") < 0)
212 FILE_OP_ERROR(RC_DIR, "rename");
214 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
215 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
216 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
217 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
218 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
219 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
221 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
222 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
223 RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
224 FILE_OP_ERROR("sylpheed.log", "rename");
226 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
228 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
229 !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
230 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
231 RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
232 FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
237 prefs_common_read_config();
240 if (gpgme_check_engine()) { /* Also does some gpgme init */
241 rfc2015_disable_all();
242 debug_print("gpgme_engine_version:\n%s\n",
243 gpgme_get_engine_info());
245 if (prefs_common.gpg_warning) {
248 val = alertpanel_message_with_disable
250 _("GnuPG is not installed properly.\n"
251 "OpenPGP support disabled."));
252 if (val & G_ALERTDISABLE)
253 prefs_common.gpg_warning = FALSE;
256 gpgme_register_idle(idle_function_for_gpgme);
260 gtkpspellcheckers = gtkpspell_checkers_new();
264 prefs_common_save_config();
265 prefs_filter_read_config();
266 prefs_filter_write_config();
267 prefs_actions_read_config();
268 prefs_actions_write_config();
269 prefs_display_header_read_config();
270 prefs_display_header_write_config();
271 /* prefs_filtering_read_config(); */
272 addressbook_read_file();
273 renderer_read_config();
277 mainwin = main_window_create
278 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
279 folderview = mainwin->folderview;
281 /* register the callback of unix domain socket input */
282 lock_socket_tag = gdk_input_add(lock_socket,
283 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
284 lock_socket_input_cb,
287 account_read_config_all();
288 account_save_config_all();
290 if (folder_read_list() < 0) {
294 if (!account_get_list()) {
299 account_set_missing_folder();
300 folder_set_missing_folders();
301 folderview_set(folderview);
303 /* prefs_scoring_read_config(); */
304 prefs_matcher_read_config();
306 /* make one all-folder processing before using sylpheed */
307 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
309 addressbook_read_file();
311 inc_autocheck_timer_init(mainwin);
313 /* ignore SIGPIPE signal for preventing sudden death of program */
314 signal(SIGPIPE, SIG_IGN);
316 if (cmd.receive_all || prefs_common.chk_on_startup)
317 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
318 else if (cmd.receive)
319 inc_mail(mainwin, prefs_common.newmail_notify_manu);
321 gtk_widget_grab_focus(folderview->ctree);
324 open_compose_new(cmd.compose_mailto, cmd.attach_files);
325 if (cmd.attach_files) {
326 ptr_array_free_strings(cmd.attach_files);
327 g_ptr_array_free(cmd.attach_files, TRUE);
328 cmd.attach_files = NULL;
335 addressbook_destroy();
338 gtkpspell_checkers_delete();
344 static void parse_cmd_opt(int argc, char *argv[])
348 for (i = 1; i < argc; i++) {
349 if (!strncmp(argv[i], "--debug", 7))
351 else if (!strncmp(argv[i], "--receive-all", 13))
352 cmd.receive_all = TRUE;
353 else if (!strncmp(argv[i], "--receive", 9))
355 else if (!strncmp(argv[i], "--compose", 9)) {
356 const gchar *p = argv[i + 1];
359 cmd.compose_mailto = NULL;
360 if (p && *p != '\0' && *p != '-') {
361 if (!strncmp(p, "mailto:", 7))
362 cmd.compose_mailto = p + 7;
364 cmd.compose_mailto = p;
367 } else if (!strncmp(argv[i], "--attach", 8)) {
368 const gchar *p = argv[i + 1];
371 while (p && *p != '\0' && *p != '-') {
372 if (!cmd.attach_files)
373 cmd.attach_files = g_ptr_array_new();
374 if (*p != G_DIR_SEPARATOR)
375 file = g_strconcat(startup_dir,
380 g_ptr_array_add(cmd.attach_files, file);
384 } else if (!strncmp(argv[i], "--send", 6)) {
386 } else if (!strncmp(argv[i], "--version", 9)) {
387 puts("Sylpheed version " VERSION);
389 } else if (!strncmp(argv[i], "--status", 8)) {
391 } else if (!strncmp(argv[i], "--help", 6)) {
392 g_print(_("Usage: %s [OPTION]...\n"),
393 g_basename(argv[0]));
395 puts(_(" --compose [address] open composition window"));
396 puts(_(" --attach file1 [file2]...\n"
397 " open composition window with specified files\n"
399 puts(_(" --receive receive new messages"));
400 puts(_(" --receive-all receive new messages of all accounts"));
401 puts(_(" --send send all queued messages"));
402 puts(_(" --status show the total number of messages"));
403 puts(_(" --debug debug mode"));
404 puts(_(" --help display this help and exit"));
405 puts(_(" --version output version information and exit"));
411 if (cmd.attach_files && cmd.compose == FALSE) {
413 cmd.compose_mailto = NULL;
417 static gint get_queued_message_num(void)
421 queue = folder_get_default_queue();
422 if (!queue) return -1;
424 folder_item_scan(queue);
428 static void save_all_caches(FolderItem *item, gpointer data)
433 folder_item_write_cache(item);
436 static void initial_processing(FolderItem *item, gpointer data)
438 MainWindow *mainwin = (MainWindow *)data;
441 g_return_if_fail(item);
442 buf = g_strdup_printf(_("Processing (%s)..."),
445 : _("top level folder"));
446 debug_print("%s\n", buf);
447 STATUSBAR_PUSH(mainwin, buf);
450 main_window_cursor_wait(mainwin);
452 folder_item_apply_processing(item);
454 debug_print(_("done.\n"));
455 STATUSBAR_POP(mainwin);
456 main_window_cursor_normal(mainwin);
459 void app_will_exit(GtkWidget *widget, gpointer data)
461 MainWindow *mainwin = data;
464 if (compose_get_compose_list()) {
465 if (alertpanel(_("Notice"),
466 _("Composing message exists. Really quit?"),
467 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
469 manage_window_focus_in(mainwin->window, NULL, NULL);
472 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
473 if (alertpanel(_("Queued messages"),
474 _("Some unsent messages are queued. Exit now?"),
475 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
477 manage_window_focus_in(mainwin->window, NULL, NULL);
480 inc_autocheck_timer_remove();
483 gpgmegtk_free_passphrase();
486 if (prefs_common.clean_on_exit)
487 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
489 /* save all state before exiting */
491 folder_func_to_all_folders(save_all_caches, NULL);
493 main_window_get_size(mainwin);
494 main_window_get_position(mainwin);
495 prefs_common_save_config();
496 prefs_filter_write_config();
497 account_save_config_all();
498 addressbook_export_to_file();
500 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
501 gtk_item_factory_dump_rc(filename, NULL, TRUE);
504 /* delete temporary files */
505 remove_all_files(get_mime_tmp_dir());
509 /* delete unix domain socket */
510 gdk_input_remove(lock_socket_tag);
511 fd_close(lock_socket);
512 filename = get_socket_name();
523 static void idle_function_for_gpgme(void)
525 while (gtk_events_pending())
526 gtk_main_iteration();
528 #endif /* USE_GPGME */
530 static gchar *get_socket_name(void)
532 static gchar *filename = NULL;
534 if (filename == NULL) {
535 filename = g_strdup_printf("%s%csylpheed-%d",
536 g_get_tmp_dir(), G_DIR_SEPARATOR,
543 static gint prohibit_duplicate_launch(void)
548 path = get_socket_name();
549 uxsock = fd_connect_unix(path);
552 return fd_open_unix(path);
555 /* remote command mode */
557 debug_print(_("another Sylpheed is already running.\n"));
560 fd_write(uxsock, "receive_all\n", 12);
561 else if (cmd.receive)
562 fd_write(uxsock, "receive\n", 8);
563 else if (cmd.compose && cmd.attach_files) {
564 gchar *str, *compose_str;
567 if (cmd.compose_mailto)
568 compose_str = g_strdup_printf("compose_attach %s\n",
571 compose_str = g_strdup("compose_attach\n");
573 fd_write(uxsock, compose_str, strlen(compose_str));
576 for (i = 0; i < cmd.attach_files->len; i++) {
577 str = g_ptr_array_index(cmd.attach_files, i);
578 fd_write(uxsock, str, strlen(str));
579 fd_write(uxsock, "\n", 1);
582 fd_write(uxsock, ".\n", 2);
583 } else if (cmd.compose) {
586 if (cmd.compose_mailto)
587 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
589 compose_str = g_strdup("compose\n");
591 fd_write(uxsock, compose_str, strlen(compose_str));
593 } else if (cmd.send) {
594 fd_write(uxsock, "send\n", 5);
595 } else if (cmd.status) {
598 fd_write(uxsock, "status\n", 7);
599 fd_gets(uxsock, buf, sizeof(buf));
602 fd_write(uxsock, "popup\n", 6);
608 static void lock_socket_input_cb(gpointer data,
610 GdkInputCondition condition)
612 MainWindow *mainwin = (MainWindow *)data;
616 sock = fd_accept(source);
617 fd_gets(sock, buf, sizeof(buf));
619 if (!strncmp(buf, "popup", 5)) {
620 main_window_popup(mainwin);
621 } else if (!strncmp(buf, "receive_all", 11)) {
622 main_window_popup(mainwin);
623 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
624 } else if (!strncmp(buf, "receive", 7)) {
625 main_window_popup(mainwin);
626 inc_mail(mainwin, prefs_common.newmail_notify_manu);
627 } else if (!strncmp(buf, "compose_attach", 14)) {
631 mailto = g_strdup(buf + strlen("compose_attach") + 1);
632 files = g_ptr_array_new();
633 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
634 if (buf[0] == '.' && buf[1] == '\n') break;
636 g_ptr_array_add(files, g_strdup(buf));
638 open_compose_new(mailto, files);
639 ptr_array_free_strings(files);
640 g_ptr_array_free(files, TRUE);
642 } else if (!strncmp(buf, "compose", 7)) {
643 open_compose_new(buf + strlen("compose") + 1, NULL);
644 } else if (!strncmp(buf, "send", 4)) {
646 } else if (!strncmp(buf, "status", 6)) {
647 guint new, unread, total;
649 folder_count_total_msgs(&new, &unread, &total);
650 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
651 fd_write(sock, buf, strlen(buf));
657 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
662 Xstrdup_a(addr, address, return);
666 compose_new(NULL, addr, attach_files);
669 static void send_queue(void)
672 FolderItem *def_outbox;
674 def_outbox = folder_get_default_outbox();
676 for (list = folder_get_list(); list != NULL; list = list->next) {
677 Folder *folder = list->data;
680 if (procmsg_send_queue
681 (folder->queue, prefs_common.savemsg) < 0)
682 alertpanel_error(_("Some errors occurred while sending queued messages."));
684 folder_item_scan(folder->queue);
685 folderview_update_item(folder->queue, TRUE);
686 if (prefs_common.savemsg && folder->outbox) {
687 folderview_update_item(folder->outbox, TRUE);
688 if (folder->outbox == def_outbox)
694 if (prefs_common.savemsg && def_outbox)
695 folderview_update_item(def_outbox, TRUE);