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);
122 gchar *get_socket_name (void);
125 static void open_compose_new (const gchar *address,
126 GPtrArray *attach_files);
128 static void send_queue (void);
129 static void initial_processing (FolderItem *item, gpointer data);
133 _("File `%s' already exists.\n"
134 "Can't create folder.")
137 #define MAKE_DIR_IF_NOT_EXIST(dir) \
139 if (!is_dir_exist(dir)) { \
140 if (is_file_exist(dir)) { \
142 (_("File `%s' already exists.\n" \
143 "Can't create folder."), \
147 if (make_dir(dir) < 0) \
152 int main(int argc, char *argv[])
156 FolderView *folderview;
158 setlocale(LC_ALL, "");
159 bindtextdomain(PACKAGE, LOCALEDIR);
162 prog_version = PROG_VERSION;
163 startup_dir = g_get_current_dir();
164 argv0 = g_strdup(argv[0]);
166 parse_cmd_opt(argc, argv);
169 gtk_init(&argc, &argv);
172 crash_main(cmd.crash_params);
176 crash_install_handlers();
178 #if USE_THREADS || USE_LDAP
180 if (!g_thread_supported())
181 g_error(_("g_thread is not supported by glib.\n"));
186 gtk_widget_push_visual(gdk_imlib_get_visual());
187 gtk_widget_push_colormap(gdk_imlib_get_colormap());
194 srandom((gint)time(NULL));
196 /* parse gtkrc files */
197 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
199 gtk_rc_parse(userrc);
201 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
202 G_DIR_SEPARATOR_S, "gtkrc", NULL);
203 gtk_rc_parse(userrc);
205 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
206 gtk_rc_parse(userrc);
209 gtk_rc_parse("./gtkrc");
211 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
212 gtk_item_factory_parse_rc(userrc);
215 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
217 /* check and create unix domain socket */
218 lock_socket = prohibit_duplicate_launch();
219 if (lock_socket < 0) return 0;
222 puts("0 Sylpheed not running.");
226 /* backup if old rc file exists */
227 if (is_file_exist(RC_DIR)) {
228 if (rename(RC_DIR, RC_DIR ".bak") < 0)
229 FILE_OP_ERROR(RC_DIR, "rename");
231 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
232 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
233 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
234 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
235 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
236 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
238 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
239 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
240 RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
241 FILE_OP_ERROR("sylpheed.log", "rename");
243 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
245 if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
246 !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
247 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
248 RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
249 FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
254 prefs_common_read_config();
257 if (gpgme_check_engine()) { /* Also does some gpgme init */
258 rfc2015_disable_all();
259 debug_print("gpgme_engine_version:\n%s\n",
260 gpgme_get_engine_info());
262 if (prefs_common.gpg_warning) {
265 val = alertpanel_message_with_disable
267 _("GnuPG is not installed properly, or needs to be upgraded.\n"
268 "OpenPGP support disabled."));
269 if (val & G_ALERTDISABLE)
270 prefs_common.gpg_warning = FALSE;
273 gpgme_register_idle(idle_function_for_gpgme);
277 gtkaspellcheckers = gtkaspell_checkers_new();
281 prefs_common_save_config();
282 prefs_filter_read_config();
283 prefs_filter_write_config();
284 prefs_actions_read_config();
285 prefs_actions_write_config();
286 prefs_display_header_read_config();
287 prefs_display_header_write_config();
288 /* prefs_filtering_read_config(); */
289 addressbook_read_file();
290 renderer_read_config();
294 mainwin = main_window_create
295 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
296 folderview = mainwin->folderview;
298 /* register the callback of unix domain socket input */
299 lock_socket_tag = gdk_input_add(lock_socket,
300 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
301 lock_socket_input_cb,
304 account_read_config_all();
305 account_save_config_all();
307 if (folder_read_list() < 0) {
311 if (!account_get_list()) {
316 account_set_missing_folder();
317 folder_set_missing_folders();
318 folderview_set(folderview);
320 /* prefs_scoring_read_config(); */
321 prefs_matcher_read_config();
323 /* make one all-folder processing before using sylpheed */
324 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
326 addressbook_read_file();
328 inc_autocheck_timer_init(mainwin);
330 /* ignore SIGPIPE signal for preventing sudden death of program */
331 signal(SIGPIPE, SIG_IGN);
333 if (cmd.receive_all || prefs_common.chk_on_startup)
334 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
335 else if (cmd.receive)
336 inc_mail(mainwin, prefs_common.newmail_notify_manu);
338 gtk_widget_grab_focus(folderview->ctree);
341 open_compose_new(cmd.compose_mailto, cmd.attach_files);
342 if (cmd.attach_files) {
343 ptr_array_free_strings(cmd.attach_files);
344 g_ptr_array_free(cmd.attach_files, TRUE);
345 cmd.attach_files = NULL;
352 addressbook_destroy();
355 gtkaspell_checkers_delete();
361 static void parse_cmd_opt(int argc, char *argv[])
365 for (i = 1; i < argc; i++) {
366 if (!strncmp(argv[i], "--debug", 7))
368 else if (!strncmp(argv[i], "--receive-all", 13))
369 cmd.receive_all = TRUE;
370 else if (!strncmp(argv[i], "--receive", 9))
372 else if (!strncmp(argv[i], "--compose", 9)) {
373 const gchar *p = argv[i + 1];
376 cmd.compose_mailto = NULL;
377 if (p && *p != '\0' && *p != '-') {
378 if (!strncmp(p, "mailto:", 7))
379 cmd.compose_mailto = p + 7;
381 cmd.compose_mailto = p;
384 } else if (!strncmp(argv[i], "--attach", 8)) {
385 const gchar *p = argv[i + 1];
388 while (p && *p != '\0' && *p != '-') {
389 if (!cmd.attach_files)
390 cmd.attach_files = g_ptr_array_new();
391 if (*p != G_DIR_SEPARATOR)
392 file = g_strconcat(startup_dir,
397 g_ptr_array_add(cmd.attach_files, file);
401 } else if (!strncmp(argv[i], "--send", 6)) {
403 } else if (!strncmp(argv[i], "--version", 9)) {
404 puts("Sylpheed version " VERSION);
406 } else if (!strncmp(argv[i], "--status", 8)) {
408 } else if (!strncmp(argv[i], "--help", 6)) {
409 g_print(_("Usage: %s [OPTION]...\n"),
410 g_basename(argv[0]));
412 puts(_(" --compose [address] open composition window"));
413 puts(_(" --attach file1 [file2]...\n"
414 " open composition window with specified files\n"
416 puts(_(" --receive receive new messages"));
417 puts(_(" --receive-all receive new messages of all accounts"));
418 puts(_(" --send send all queued messages"));
419 puts(_(" --status show the total number of messages"));
420 puts(_(" --debug debug mode"));
421 puts(_(" --help display this help and exit"));
422 puts(_(" --version output version information and exit"));
425 } else if (!strncmp(argv[i], "--crash", 7)) {
427 cmd.crash_params = g_strdup(argv[i + 1]);
433 if (cmd.attach_files && cmd.compose == FALSE) {
435 cmd.compose_mailto = NULL;
439 static gint get_queued_message_num(void)
443 queue = folder_get_default_queue();
444 if (!queue) return -1;
446 folder_item_scan(queue);
450 static void save_all_caches(FolderItem *item, gpointer data)
454 folder_item_write_cache(item);
457 static void initial_processing(FolderItem *item, gpointer data)
459 MainWindow *mainwin = (MainWindow *)data;
462 g_return_if_fail(item);
463 buf = g_strdup_printf(_("Processing (%s)..."),
466 : _("top level folder"));
467 debug_print("%s\n", buf);
470 main_window_cursor_wait(mainwin);
472 folder_item_apply_processing(item);
474 debug_print("done.\n");
475 STATUSBAR_POP(mainwin);
476 main_window_cursor_normal(mainwin);
479 void app_will_exit(GtkWidget *widget, gpointer data)
481 MainWindow *mainwin = data;
484 if (compose_get_compose_list()) {
485 if (alertpanel(_("Notice"),
486 _("Composing message exists. Really quit?"),
487 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
489 manage_window_focus_in(mainwin->window, NULL, NULL);
492 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
493 if (alertpanel(_("Queued messages"),
494 _("Some unsent messages are queued. Exit now?"),
495 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
497 manage_window_focus_in(mainwin->window, NULL, NULL);
500 inc_autocheck_timer_remove();
503 gpgmegtk_free_passphrase();
506 if (prefs_common.clean_on_exit)
507 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
509 /* save prefs for opened folder */
510 if(mainwin->folderview->opened)
514 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
515 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
518 /* save all state before exiting */
520 folder_func_to_all_folders(save_all_caches, NULL);
522 main_window_get_size(mainwin);
523 main_window_get_position(mainwin);
524 prefs_common_save_config();
525 prefs_filter_write_config();
526 account_save_config_all();
527 addressbook_export_to_file();
529 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
530 gtk_item_factory_dump_rc(filename, NULL, TRUE);
533 /* delete temporary files */
534 remove_all_files(get_mime_tmp_dir());
538 /* delete unix domain socket */
539 gdk_input_remove(lock_socket_tag);
540 fd_close(lock_socket);
541 filename = get_socket_name();
552 static void idle_function_for_gpgme(void)
554 while (gtk_events_pending())
555 gtk_main_iteration();
557 #endif /* USE_GPGME */
560 * CLAWS: want this public so crash dialog can delete the
566 gchar *get_socket_name(void)
568 static gchar *filename = NULL;
570 if (filename == NULL) {
571 filename = g_strdup_printf("%s%csylpheed-%d",
572 g_get_tmp_dir(), G_DIR_SEPARATOR,
579 static gint prohibit_duplicate_launch(void)
584 path = get_socket_name();
585 uxsock = fd_connect_unix(path);
588 return fd_open_unix(path);
591 /* remote command mode */
593 debug_print("another Sylpheed is already running.\n");
596 fd_write(uxsock, "receive_all\n", 12);
597 else if (cmd.receive)
598 fd_write(uxsock, "receive\n", 8);
599 else if (cmd.compose && cmd.attach_files) {
600 gchar *str, *compose_str;
603 if (cmd.compose_mailto)
604 compose_str = g_strdup_printf("compose_attach %s\n",
607 compose_str = g_strdup("compose_attach\n");
609 fd_write(uxsock, compose_str, strlen(compose_str));
612 for (i = 0; i < cmd.attach_files->len; i++) {
613 str = g_ptr_array_index(cmd.attach_files, i);
614 fd_write(uxsock, str, strlen(str));
615 fd_write(uxsock, "\n", 1);
618 fd_write(uxsock, ".\n", 2);
619 } else if (cmd.compose) {
622 if (cmd.compose_mailto)
623 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
625 compose_str = g_strdup("compose\n");
627 fd_write(uxsock, compose_str, strlen(compose_str));
629 } else if (cmd.send) {
630 fd_write(uxsock, "send\n", 5);
631 } else if (cmd.status) {
634 fd_write(uxsock, "status\n", 7);
635 fd_gets(uxsock, buf, sizeof(buf));
638 fd_write(uxsock, "popup\n", 6);
644 static void lock_socket_input_cb(gpointer data,
646 GdkInputCondition condition)
648 MainWindow *mainwin = (MainWindow *)data;
652 sock = fd_accept(source);
653 fd_gets(sock, buf, sizeof(buf));
655 if (!strncmp(buf, "popup", 5)) {
656 main_window_popup(mainwin);
657 } else if (!strncmp(buf, "receive_all", 11)) {
658 main_window_popup(mainwin);
659 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
660 } else if (!strncmp(buf, "receive", 7)) {
661 main_window_popup(mainwin);
662 inc_mail(mainwin, prefs_common.newmail_notify_manu);
663 } else if (!strncmp(buf, "compose_attach", 14)) {
667 mailto = g_strdup(buf + strlen("compose_attach") + 1);
668 files = g_ptr_array_new();
669 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
670 if (buf[0] == '.' && buf[1] == '\n') break;
672 g_ptr_array_add(files, g_strdup(buf));
674 open_compose_new(mailto, files);
675 ptr_array_free_strings(files);
676 g_ptr_array_free(files, TRUE);
678 } else if (!strncmp(buf, "compose", 7)) {
679 open_compose_new(buf + strlen("compose") + 1, NULL);
680 } else if (!strncmp(buf, "send", 4)) {
682 } else if (!strncmp(buf, "status", 6)) {
683 guint new, unread, total;
685 folder_count_total_msgs(&new, &unread, &total);
686 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
687 fd_write(sock, buf, strlen(buf));
693 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
698 Xstrdup_a(addr, address, return);
702 compose_new(NULL, addr, attach_files);
705 static void send_queue(void)
708 FolderItem *def_outbox;
710 def_outbox = folder_get_default_outbox();
712 for (list = folder_get_list(); list != NULL; list = list->next) {
713 Folder *folder = list->data;
716 if (procmsg_send_queue
717 (folder->queue, prefs_common.savemsg) < 0)
718 alertpanel_error(_("Some errors occurred while sending queued messages."));
720 folder_item_scan(folder->queue);
721 folderview_update_item(folder->queue, TRUE);
722 if (prefs_common.savemsg && folder->outbox) {
723 folderview_update_item(folder->outbox, TRUE);
724 if (folder->outbox == def_outbox)
730 if (prefs_common.savemsg && def_outbox)
731 folderview_update_item(def_outbox, TRUE);