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 if (!item->stype == F_QUEUE)
455 folder_item_write_cache(item);
458 static void initial_processing(FolderItem *item, gpointer data)
460 MainWindow *mainwin = (MainWindow *)data;
463 g_return_if_fail(item);
464 buf = g_strdup_printf(_("Processing (%s)..."),
467 : _("top level folder"));
468 debug_print("%s\n", buf);
471 main_window_cursor_wait(mainwin);
473 folder_item_apply_processing(item);
475 if (item->stype == F_QUEUE)
476 folder_item_scan(item);
478 debug_print("done.\n");
479 STATUSBAR_POP(mainwin);
480 main_window_cursor_normal(mainwin);
483 void app_will_exit(GtkWidget *widget, gpointer data)
485 MainWindow *mainwin = data;
488 if (compose_get_compose_list()) {
489 if (alertpanel(_("Notice"),
490 _("Composing message exists. Really quit?"),
491 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
493 manage_window_focus_in(mainwin->window, NULL, NULL);
496 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
497 if (alertpanel(_("Queued messages"),
498 _("Some unsent messages are queued. Exit now?"),
499 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
501 manage_window_focus_in(mainwin->window, NULL, NULL);
504 inc_autocheck_timer_remove();
507 gpgmegtk_free_passphrase();
510 if (prefs_common.clean_on_exit)
511 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
513 /* save prefs for opened folder */
514 if(mainwin->folderview->opened)
518 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
519 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
522 /* save all state before exiting */
524 folder_func_to_all_folders(save_all_caches, NULL);
526 main_window_get_size(mainwin);
527 main_window_get_position(mainwin);
528 prefs_common_save_config();
529 prefs_filter_write_config();
530 account_save_config_all();
531 addressbook_export_to_file();
533 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
534 gtk_item_factory_dump_rc(filename, NULL, TRUE);
537 /* delete temporary files */
538 remove_all_files(get_mime_tmp_dir());
542 /* delete unix domain socket */
543 gdk_input_remove(lock_socket_tag);
544 fd_close(lock_socket);
545 filename = get_socket_name();
556 static void idle_function_for_gpgme(void)
558 while (gtk_events_pending())
559 gtk_main_iteration();
561 #endif /* USE_GPGME */
564 * CLAWS: want this public so crash dialog can delete the
570 gchar *get_socket_name(void)
572 static gchar *filename = NULL;
574 if (filename == NULL) {
575 filename = g_strdup_printf("%s%csylpheed-%d",
576 g_get_tmp_dir(), G_DIR_SEPARATOR,
583 static gint prohibit_duplicate_launch(void)
588 path = get_socket_name();
589 uxsock = fd_connect_unix(path);
592 return fd_open_unix(path);
595 /* remote command mode */
597 debug_print("another Sylpheed is already running.\n");
600 fd_write(uxsock, "receive_all\n", 12);
601 else if (cmd.receive)
602 fd_write(uxsock, "receive\n", 8);
603 else if (cmd.compose && cmd.attach_files) {
604 gchar *str, *compose_str;
607 if (cmd.compose_mailto)
608 compose_str = g_strdup_printf("compose_attach %s\n",
611 compose_str = g_strdup("compose_attach\n");
613 fd_write(uxsock, compose_str, strlen(compose_str));
616 for (i = 0; i < cmd.attach_files->len; i++) {
617 str = g_ptr_array_index(cmd.attach_files, i);
618 fd_write(uxsock, str, strlen(str));
619 fd_write(uxsock, "\n", 1);
622 fd_write(uxsock, ".\n", 2);
623 } else if (cmd.compose) {
626 if (cmd.compose_mailto)
627 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
629 compose_str = g_strdup("compose\n");
631 fd_write(uxsock, compose_str, strlen(compose_str));
633 } else if (cmd.send) {
634 fd_write(uxsock, "send\n", 5);
635 } else if (cmd.status) {
638 fd_write(uxsock, "status\n", 7);
639 fd_gets(uxsock, buf, sizeof(buf));
642 fd_write(uxsock, "popup\n", 6);
648 static void lock_socket_input_cb(gpointer data,
650 GdkInputCondition condition)
652 MainWindow *mainwin = (MainWindow *)data;
656 sock = fd_accept(source);
657 fd_gets(sock, buf, sizeof(buf));
659 if (!strncmp(buf, "popup", 5)) {
660 main_window_popup(mainwin);
661 } else if (!strncmp(buf, "receive_all", 11)) {
662 main_window_popup(mainwin);
663 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
664 } else if (!strncmp(buf, "receive", 7)) {
665 main_window_popup(mainwin);
666 inc_mail(mainwin, prefs_common.newmail_notify_manu);
667 } else if (!strncmp(buf, "compose_attach", 14)) {
671 mailto = g_strdup(buf + strlen("compose_attach") + 1);
672 files = g_ptr_array_new();
673 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
674 if (buf[0] == '.' && buf[1] == '\n') break;
676 g_ptr_array_add(files, g_strdup(buf));
678 open_compose_new(mailto, files);
679 ptr_array_free_strings(files);
680 g_ptr_array_free(files, TRUE);
682 } else if (!strncmp(buf, "compose", 7)) {
683 open_compose_new(buf + strlen("compose") + 1, NULL);
684 } else if (!strncmp(buf, "send", 4)) {
686 } else if (!strncmp(buf, "status", 6)) {
687 guint new, unread, total;
689 folder_count_total_msgs(&new, &unread, &total);
690 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
691 fd_write(sock, buf, strlen(buf));
697 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
702 Xstrdup_a(addr, address, return);
706 compose_new(NULL, addr, attach_files);
709 static void send_queue(void)
712 FolderItem *def_outbox;
714 def_outbox = folder_get_default_outbox();
716 for (list = folder_get_list(); list != NULL; list = list->next) {
717 Folder *folder = list->data;
720 if (procmsg_send_queue
721 (folder->queue, prefs_common.savemsg) < 0)
722 alertpanel_error(_("Some errors occurred while sending queued messages."));
724 folder_item_scan(folder->queue);
725 folderview_update_item(folder->queue, TRUE);
726 if (prefs_common.savemsg && folder->outbox) {
727 folderview_update_item(folder->outbox, TRUE);
728 if (folder->outbox == def_outbox)
734 if (prefs_common.savemsg && def_outbox)
735 folderview_update_item(def_outbox, TRUE);