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(RC_DIR G_DIR_SEPARATOR_S "uidl");
220 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
221 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
222 RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
223 FILE_OP_ERROR("sylpheed.log", "rename");
225 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
227 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
228 !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
229 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
230 RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
231 FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
236 prefs_common_read_config();
239 if (gpgme_check_engine()) { /* Also does some gpgme init */
240 rfc2015_disable_all();
241 debug_print("gpgme_engine_version:\n%s\n",
242 gpgme_get_engine_info());
244 if (prefs_common.gpg_warning) {
247 val = alertpanel_message_with_disable
249 _("GnuPG is not installed properly.\n"
250 "OpenPGP support disabled."));
251 if (val & G_ALERTDISABLE)
252 prefs_common.gpg_warning = FALSE;
255 gpgme_register_idle(idle_function_for_gpgme);
259 gtkpspellcheckers = gtkpspell_checkers_new();
263 prefs_common_save_config();
264 prefs_filter_read_config();
265 prefs_filter_write_config();
266 prefs_actions_read_config();
267 prefs_actions_write_config();
268 prefs_display_header_read_config();
269 prefs_display_header_write_config();
270 /* prefs_filtering_read_config(); */
271 addressbook_read_file();
272 renderer_read_config();
276 mainwin = main_window_create
277 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
278 folderview = mainwin->folderview;
280 /* register the callback of unix domain socket input */
281 lock_socket_tag = gdk_input_add(lock_socket,
282 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
283 lock_socket_input_cb,
286 account_read_config_all();
287 account_save_config_all();
289 if (folder_read_list() < 0) {
293 if (!account_get_list()) {
298 account_set_missing_folder();
299 folder_set_missing_folders();
300 folderview_set(folderview);
302 /* prefs_scoring_read_config(); */
303 prefs_matcher_read_config();
305 /* make one all-folder processing before using sylpheed */
306 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
308 addressbook_read_file();
310 inc_autocheck_timer_init(mainwin);
312 /* ignore SIGPIPE signal for preventing sudden death of program */
313 signal(SIGPIPE, SIG_IGN);
315 if (cmd.receive_all || prefs_common.chk_on_startup)
316 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
317 else if (cmd.receive)
318 inc_mail(mainwin, prefs_common.newmail_notify_manu);
320 gtk_widget_grab_focus(folderview->ctree);
323 open_compose_new(cmd.compose_mailto, cmd.attach_files);
324 if (cmd.attach_files) {
325 ptr_array_free_strings(cmd.attach_files);
326 g_ptr_array_free(cmd.attach_files, TRUE);
327 cmd.attach_files = NULL;
334 addressbook_destroy();
337 gtkpspell_checkers_delete();
343 static void parse_cmd_opt(int argc, char *argv[])
347 for (i = 1; i < argc; i++) {
348 if (!strncmp(argv[i], "--debug", 7))
350 else if (!strncmp(argv[i], "--receive-all", 13))
351 cmd.receive_all = TRUE;
352 else if (!strncmp(argv[i], "--receive", 9))
354 else if (!strncmp(argv[i], "--compose", 9)) {
355 const gchar *p = argv[i + 1];
358 cmd.compose_mailto = NULL;
359 if (p && *p != '\0' && *p != '-') {
360 if (!strncmp(p, "mailto:", 7))
361 cmd.compose_mailto = p + 7;
363 cmd.compose_mailto = p;
366 } else if (!strncmp(argv[i], "--attach", 8)) {
367 const gchar *p = argv[i + 1];
370 while (p && *p != '\0' && *p != '-') {
371 if (!cmd.attach_files)
372 cmd.attach_files = g_ptr_array_new();
373 if (*p != G_DIR_SEPARATOR)
374 file = g_strconcat(startup_dir,
379 g_ptr_array_add(cmd.attach_files, file);
383 } else if (!strncmp(argv[i], "--send", 6)) {
385 } else if (!strncmp(argv[i], "--version", 9)) {
386 puts("Sylpheed version " VERSION);
388 } else if (!strncmp(argv[i], "--status", 8)) {
390 } else if (!strncmp(argv[i], "--help", 6)) {
391 g_print(_("Usage: %s [OPTION]...\n"),
392 g_basename(argv[0]));
394 puts(_(" --compose [address] open composition window"));
395 puts(_(" --attach file1 [file2]...\n"
396 " open composition window with specified files\n"
398 puts(_(" --receive receive new messages"));
399 puts(_(" --receive-all receive new messages of all accounts"));
400 puts(_(" --send send all queued messages"));
401 puts(_(" --status show the total number of messages"));
402 puts(_(" --debug debug mode"));
403 puts(_(" --help display this help and exit"));
404 puts(_(" --version output version information and exit"));
410 if (cmd.attach_files && cmd.compose == FALSE) {
412 cmd.compose_mailto = NULL;
416 static gint get_queued_message_num(void)
420 queue = folder_get_default_queue();
421 if (!queue) return -1;
423 folder_item_scan(queue);
427 static void save_all_caches(FolderItem *item, gpointer data)
432 folder_item_write_cache(item);
435 static void initial_processing(FolderItem *item, gpointer data)
437 MainWindow *mainwin = (MainWindow *)data;
440 g_return_if_fail(item);
441 buf = g_strdup_printf(_("Processing (%s)..."),
444 : _("top level folder"));
445 debug_print("%s\n", buf);
446 STATUSBAR_PUSH(mainwin, buf);
449 main_window_cursor_wait(mainwin);
451 folder_item_apply_processing(item);
453 debug_print(_("done.\n"));
454 STATUSBAR_POP(mainwin);
455 main_window_cursor_normal(mainwin);
458 void app_will_exit(GtkWidget *widget, gpointer data)
460 MainWindow *mainwin = data;
463 if (compose_get_compose_list()) {
464 if (alertpanel(_("Notice"),
465 _("Composing message exists. Really quit?"),
466 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
468 manage_window_focus_in(mainwin->window, NULL, NULL);
471 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
472 if (alertpanel(_("Queued messages"),
473 _("Some unsent messages are queued. Exit now?"),
474 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
476 manage_window_focus_in(mainwin->window, NULL, NULL);
479 inc_autocheck_timer_remove();
482 gpgmegtk_free_passphrase();
485 if (prefs_common.clean_on_exit)
486 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
488 /* save all state before exiting */
490 folder_func_to_all_folders(save_all_caches, NULL);
492 main_window_get_size(mainwin);
493 main_window_get_position(mainwin);
494 prefs_common_save_config();
495 prefs_filter_write_config();
496 account_save_config_all();
497 addressbook_export_to_file();
499 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
500 gtk_item_factory_dump_rc(filename, NULL, TRUE);
503 /* delete temporary files */
504 remove_all_files(get_mime_tmp_dir());
508 /* delete unix domain socket */
509 gdk_input_remove(lock_socket_tag);
510 fd_close(lock_socket);
511 filename = get_socket_name();
522 static void idle_function_for_gpgme(void)
524 while (gtk_events_pending())
525 gtk_main_iteration();
527 #endif /* USE_GPGME */
529 static gchar *get_socket_name(void)
531 static gchar *filename = NULL;
533 if (filename == NULL) {
534 filename = g_strdup_printf("%s%csylpheed-%d",
535 g_get_tmp_dir(), G_DIR_SEPARATOR,
542 static gint prohibit_duplicate_launch(void)
547 path = get_socket_name();
548 uxsock = fd_connect_unix(path);
551 return fd_open_unix(path);
554 /* remote command mode */
556 debug_print(_("another Sylpheed is already running.\n"));
559 fd_write(uxsock, "receive_all\n", 12);
560 else if (cmd.receive)
561 fd_write(uxsock, "receive\n", 8);
562 else if (cmd.compose && cmd.attach_files) {
563 gchar *str, *compose_str;
566 if (cmd.compose_mailto)
567 compose_str = g_strdup_printf("compose_attach %s\n",
570 compose_str = g_strdup("compose_attach\n");
572 fd_write(uxsock, compose_str, strlen(compose_str));
575 for (i = 0; i < cmd.attach_files->len; i++) {
576 str = g_ptr_array_index(cmd.attach_files, i);
577 fd_write(uxsock, str, strlen(str));
578 fd_write(uxsock, "\n", 1);
581 fd_write(uxsock, ".\n", 2);
582 } else if (cmd.compose) {
585 if (cmd.compose_mailto)
586 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
588 compose_str = g_strdup("compose\n");
590 fd_write(uxsock, compose_str, strlen(compose_str));
592 } else if (cmd.send) {
593 fd_write(uxsock, "send\n", 5);
594 } else if (cmd.status) {
597 fd_write(uxsock, "status\n", 7);
598 fd_gets(uxsock, buf, sizeof(buf));
601 fd_write(uxsock, "popup\n", 6);
607 static void lock_socket_input_cb(gpointer data,
609 GdkInputCondition condition)
611 MainWindow *mainwin = (MainWindow *)data;
615 sock = fd_accept(source);
616 fd_gets(sock, buf, sizeof(buf));
618 if (!strncmp(buf, "popup", 5)) {
619 main_window_popup(mainwin);
620 } else if (!strncmp(buf, "receive_all", 11)) {
621 main_window_popup(mainwin);
622 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
623 } else if (!strncmp(buf, "receive", 7)) {
624 main_window_popup(mainwin);
625 inc_mail(mainwin, prefs_common.newmail_notify_manu);
626 } else if (!strncmp(buf, "compose_attach", 14)) {
630 mailto = g_strdup(buf + strlen("compose_attach") + 1);
631 files = g_ptr_array_new();
632 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
633 if (buf[0] == '.' && buf[1] == '\n') break;
635 g_ptr_array_add(files, g_strdup(buf));
637 open_compose_new(mailto, files);
638 ptr_array_free_strings(files);
639 g_ptr_array_free(files, TRUE);
641 } else if (!strncmp(buf, "compose", 7)) {
642 open_compose_new(buf + strlen("compose") + 1, NULL);
643 } else if (!strncmp(buf, "send", 4)) {
645 } else if (!strncmp(buf, "status", 6)) {
646 guint new, unread, total;
648 folder_count_total_msgs(&new, &unread, &total);
649 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
650 fd_write(sock, buf, strlen(buf));
656 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
661 Xstrdup_a(addr, address, return);
665 compose_new(NULL, addr, attach_files);
668 static void send_queue(void)
671 FolderItem *def_outbox;
673 def_outbox = folder_get_default_outbox();
675 for (list = folder_get_list(); list != NULL; list = list->next) {
676 Folder *folder = list->data;
679 if (procmsg_send_queue
680 (folder->queue, prefs_common.savemsg) < 0)
681 alertpanel_error(_("Some errors occurred while sending queued messages."));
683 folder_item_scan(folder->queue);
684 folderview_update_item(folder->queue, TRUE);
685 if (prefs_common.savemsg && folder->outbox) {
686 folderview_update_item(folder->outbox, TRUE);
687 if (folder->outbox == def_outbox)
693 if (prefs_common.savemsg && def_outbox)
694 folderview_update_item(def_outbox, TRUE);