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"
91 gboolean debug_mode = FALSE;
93 static gint lock_socket = -1;
94 static gint lock_socket_tag = 0;
100 const gchar *compose_mailto;
101 GPtrArray *attach_files;
108 static void parse_cmd_opt(int argc, char *argv[]);
111 static void idle_function_for_gpgme(void);
112 #endif /* USE_GPGME */
114 static gint prohibit_duplicate_launch (void);
115 static void lock_socket_input_cb (gpointer data,
117 GdkInputCondition condition);
118 static gchar *get_socket_name (void);
120 static void open_compose_new (const gchar *address,
121 GPtrArray *attach_files);
123 static void send_queue (void);
124 static void initial_processing (FolderItem *item, gpointer data);
128 _("File `%s' already exists.\n"
129 "Can't create folder.")
132 #define MAKE_DIR_IF_NOT_EXIST(dir) \
134 if (!is_dir_exist(dir)) { \
135 if (is_file_exist(dir)) { \
137 (_("File `%s' already exists.\n" \
138 "Can't create folder."), \
142 if (make_dir(dir) < 0) \
147 int main(int argc, char *argv[])
151 FolderView *folderview;
153 setlocale(LC_ALL, "");
154 bindtextdomain(PACKAGE, LOCALEDIR);
157 prog_version = PROG_VERSION;
158 startup_dir = g_get_current_dir();
160 parse_cmd_opt(argc, argv);
163 gtk_init(&argc, &argv);
166 crash_main(cmd.crash_params);
170 crash_install_handlers();
172 #if USE_THREADS || USE_LDAP
174 if (!g_thread_supported())
175 g_error(_("g_thread is not supported by glib.\n"));
180 gtk_widget_push_visual(gdk_imlib_get_visual());
181 gtk_widget_push_colormap(gdk_imlib_get_colormap());
188 srandom((gint)time(NULL));
190 /* parse gtkrc files */
191 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
193 gtk_rc_parse(userrc);
195 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
196 G_DIR_SEPARATOR_S, "gtkrc", NULL);
197 gtk_rc_parse(userrc);
199 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
200 gtk_rc_parse(userrc);
203 gtk_rc_parse("./gtkrc");
205 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
206 gtk_item_factory_parse_rc(userrc);
209 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
211 /* check and create unix domain socket */
212 lock_socket = prohibit_duplicate_launch();
213 if (lock_socket < 0) return 0;
216 puts("0 Sylpheed not running.");
220 /* backup if old rc file exists */
221 if (is_file_exist(RC_DIR)) {
222 if (rename(RC_DIR, RC_DIR ".bak") < 0)
223 FILE_OP_ERROR(RC_DIR, "rename");
225 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
226 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
227 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
228 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
229 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
230 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
232 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
233 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
234 RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
235 FILE_OP_ERROR("sylpheed.log", "rename");
237 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
239 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
240 !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
241 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
242 RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
243 FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
248 prefs_common_read_config();
251 if (gpgme_check_engine()) { /* Also does some gpgme init */
252 rfc2015_disable_all();
253 debug_print("gpgme_engine_version:\n%s\n",
254 gpgme_get_engine_info());
256 if (prefs_common.gpg_warning) {
259 val = alertpanel_message_with_disable
261 _("GnuPG is not installed properly.\n"
262 "OpenPGP support disabled."));
263 if (val & G_ALERTDISABLE)
264 prefs_common.gpg_warning = FALSE;
267 gpgme_register_idle(idle_function_for_gpgme);
271 gtkpspellcheckers = gtkpspell_checkers_new();
275 prefs_common_save_config();
276 prefs_filter_read_config();
277 prefs_filter_write_config();
278 prefs_actions_read_config();
279 prefs_actions_write_config();
280 prefs_display_header_read_config();
281 prefs_display_header_write_config();
282 /* prefs_filtering_read_config(); */
283 addressbook_read_file();
284 renderer_read_config();
288 mainwin = main_window_create
289 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
290 folderview = mainwin->folderview;
292 /* register the callback of unix domain socket input */
293 lock_socket_tag = gdk_input_add(lock_socket,
294 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
295 lock_socket_input_cb,
298 account_read_config_all();
299 account_save_config_all();
301 if (folder_read_list() < 0) {
305 if (!account_get_list()) {
310 account_set_missing_folder();
311 folder_set_missing_folders();
312 folderview_set(folderview);
314 /* prefs_scoring_read_config(); */
315 prefs_matcher_read_config();
317 /* make one all-folder processing before using sylpheed */
318 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
320 addressbook_read_file();
322 inc_autocheck_timer_init(mainwin);
324 /* ignore SIGPIPE signal for preventing sudden death of program */
325 signal(SIGPIPE, SIG_IGN);
327 if (cmd.receive_all || prefs_common.chk_on_startup)
328 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
329 else if (cmd.receive)
330 inc_mail(mainwin, prefs_common.newmail_notify_manu);
332 gtk_widget_grab_focus(folderview->ctree);
335 open_compose_new(cmd.compose_mailto, cmd.attach_files);
336 if (cmd.attach_files) {
337 ptr_array_free_strings(cmd.attach_files);
338 g_ptr_array_free(cmd.attach_files, TRUE);
339 cmd.attach_files = NULL;
346 addressbook_destroy();
349 gtkpspell_checkers_delete();
355 static void parse_cmd_opt(int argc, char *argv[])
359 for (i = 1; i < argc; i++) {
360 if (!strncmp(argv[i], "--debug", 7))
362 else if (!strncmp(argv[i], "--receive-all", 13))
363 cmd.receive_all = TRUE;
364 else if (!strncmp(argv[i], "--receive", 9))
366 else if (!strncmp(argv[i], "--compose", 9)) {
367 const gchar *p = argv[i + 1];
370 cmd.compose_mailto = NULL;
371 if (p && *p != '\0' && *p != '-') {
372 if (!strncmp(p, "mailto:", 7))
373 cmd.compose_mailto = p + 7;
375 cmd.compose_mailto = p;
378 } else if (!strncmp(argv[i], "--attach", 8)) {
379 const gchar *p = argv[i + 1];
382 while (p && *p != '\0' && *p != '-') {
383 if (!cmd.attach_files)
384 cmd.attach_files = g_ptr_array_new();
385 if (*p != G_DIR_SEPARATOR)
386 file = g_strconcat(startup_dir,
391 g_ptr_array_add(cmd.attach_files, file);
395 } else if (!strncmp(argv[i], "--send", 6)) {
397 } else if (!strncmp(argv[i], "--version", 9)) {
398 puts("Sylpheed version " VERSION);
400 } else if (!strncmp(argv[i], "--status", 8)) {
402 } else if (!strncmp(argv[i], "--help", 6)) {
403 g_print(_("Usage: %s [OPTION]...\n"),
404 g_basename(argv[0]));
406 puts(_(" --compose [address] open composition window"));
407 puts(_(" --attach file1 [file2]...\n"
408 " open composition window with specified files\n"
410 puts(_(" --receive receive new messages"));
411 puts(_(" --receive-all receive new messages of all accounts"));
412 puts(_(" --send send all queued messages"));
413 puts(_(" --status show the total number of messages"));
414 puts(_(" --debug debug mode"));
415 puts(_(" --help display this help and exit"));
416 puts(_(" --version output version information and exit"));
419 } else if (!strncmp(argv[i], "--crash", 7)) {
421 cmd.crash_params = g_strdup(argv[i + 1]);
427 if (cmd.attach_files && cmd.compose == FALSE) {
429 cmd.compose_mailto = NULL;
433 static gint get_queued_message_num(void)
437 queue = folder_get_default_queue();
438 if (!queue) return -1;
440 folder_item_scan(queue);
444 static void save_all_caches(FolderItem *item, gpointer data)
449 folder_item_write_cache(item);
452 static void initial_processing(FolderItem *item, gpointer data)
454 MainWindow *mainwin = (MainWindow *)data;
457 g_return_if_fail(item);
458 buf = g_strdup_printf(_("Processing (%s)..."),
461 : _("top level folder"));
462 debug_print("%s\n", buf);
465 main_window_cursor_wait(mainwin);
467 folder_item_apply_processing(item);
469 debug_print(_("done.\n"));
470 STATUSBAR_POP(mainwin);
471 main_window_cursor_normal(mainwin);
474 void app_will_exit(GtkWidget *widget, gpointer data)
476 MainWindow *mainwin = data;
479 if (compose_get_compose_list()) {
480 if (alertpanel(_("Notice"),
481 _("Composing message exists. Really quit?"),
482 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
484 manage_window_focus_in(mainwin->window, NULL, NULL);
487 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
488 if (alertpanel(_("Queued messages"),
489 _("Some unsent messages are queued. Exit now?"),
490 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
492 manage_window_focus_in(mainwin->window, NULL, NULL);
495 inc_autocheck_timer_remove();
498 gpgmegtk_free_passphrase();
501 if (prefs_common.clean_on_exit)
502 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
504 /* save all state before exiting */
506 folder_func_to_all_folders(save_all_caches, NULL);
508 main_window_get_size(mainwin);
509 main_window_get_position(mainwin);
510 prefs_common_save_config();
511 prefs_filter_write_config();
512 account_save_config_all();
513 addressbook_export_to_file();
515 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
516 gtk_item_factory_dump_rc(filename, NULL, TRUE);
519 /* delete temporary files */
520 remove_all_files(get_mime_tmp_dir());
524 /* delete unix domain socket */
525 gdk_input_remove(lock_socket_tag);
526 fd_close(lock_socket);
527 filename = get_socket_name();
538 static void idle_function_for_gpgme(void)
540 while (gtk_events_pending())
541 gtk_main_iteration();
543 #endif /* USE_GPGME */
545 static gchar *get_socket_name(void)
547 static gchar *filename = NULL;
549 if (filename == NULL) {
550 filename = g_strdup_printf("%s%csylpheed-%d",
551 g_get_tmp_dir(), G_DIR_SEPARATOR,
558 static gint prohibit_duplicate_launch(void)
563 path = get_socket_name();
564 uxsock = fd_connect_unix(path);
567 return fd_open_unix(path);
570 /* remote command mode */
572 debug_print(_("another Sylpheed is already running.\n"));
575 fd_write(uxsock, "receive_all\n", 12);
576 else if (cmd.receive)
577 fd_write(uxsock, "receive\n", 8);
578 else if (cmd.compose && cmd.attach_files) {
579 gchar *str, *compose_str;
582 if (cmd.compose_mailto)
583 compose_str = g_strdup_printf("compose_attach %s\n",
586 compose_str = g_strdup("compose_attach\n");
588 fd_write(uxsock, compose_str, strlen(compose_str));
591 for (i = 0; i < cmd.attach_files->len; i++) {
592 str = g_ptr_array_index(cmd.attach_files, i);
593 fd_write(uxsock, str, strlen(str));
594 fd_write(uxsock, "\n", 1);
597 fd_write(uxsock, ".\n", 2);
598 } else if (cmd.compose) {
601 if (cmd.compose_mailto)
602 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
604 compose_str = g_strdup("compose\n");
606 fd_write(uxsock, compose_str, strlen(compose_str));
608 } else if (cmd.send) {
609 fd_write(uxsock, "send\n", 5);
610 } else if (cmd.status) {
613 fd_write(uxsock, "status\n", 7);
614 fd_gets(uxsock, buf, sizeof(buf));
617 fd_write(uxsock, "popup\n", 6);
623 static void lock_socket_input_cb(gpointer data,
625 GdkInputCondition condition)
627 MainWindow *mainwin = (MainWindow *)data;
631 sock = fd_accept(source);
632 fd_gets(sock, buf, sizeof(buf));
634 if (!strncmp(buf, "popup", 5)) {
635 main_window_popup(mainwin);
636 } else if (!strncmp(buf, "receive_all", 11)) {
637 main_window_popup(mainwin);
638 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
639 } else if (!strncmp(buf, "receive", 7)) {
640 main_window_popup(mainwin);
641 inc_mail(mainwin, prefs_common.newmail_notify_manu);
642 } else if (!strncmp(buf, "compose_attach", 14)) {
646 mailto = g_strdup(buf + strlen("compose_attach") + 1);
647 files = g_ptr_array_new();
648 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
649 if (buf[0] == '.' && buf[1] == '\n') break;
651 g_ptr_array_add(files, g_strdup(buf));
653 open_compose_new(mailto, files);
654 ptr_array_free_strings(files);
655 g_ptr_array_free(files, TRUE);
657 } else if (!strncmp(buf, "compose", 7)) {
658 open_compose_new(buf + strlen("compose") + 1, NULL);
659 } else if (!strncmp(buf, "send", 4)) {
661 } else if (!strncmp(buf, "status", 6)) {
662 guint new, unread, total;
664 folder_count_total_msgs(&new, &unread, &total);
665 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
666 fd_write(sock, buf, strlen(buf));
672 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
677 Xstrdup_a(addr, address, return);
681 compose_new(NULL, addr, attach_files);
684 static void send_queue(void)
687 FolderItem *def_outbox;
689 def_outbox = folder_get_default_outbox();
691 for (list = folder_get_list(); list != NULL; list = list->next) {
692 Folder *folder = list->data;
695 if (procmsg_send_queue
696 (folder->queue, prefs_common.savemsg) < 0)
697 alertpanel_error(_("Some errors occurred while sending queued messages."));
699 folder_item_scan(folder->queue);
700 folderview_update_item(folder->queue, TRUE);
701 if (prefs_common.savemsg && folder->outbox) {
702 folderview_update_item(folder->outbox, TRUE);
703 if (folder->outbox == def_outbox)
709 if (prefs_common.savemsg && def_outbox)
710 folderview_update_item(def_outbox, TRUE);