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>
46 # include "passphrase.h"
52 #include "mainwindow.h"
53 #include "folderview.h"
54 #include "summaryview.h"
55 #include "prefs_common.h"
56 #include "prefs_account.h"
57 #include "prefs_actions.h"
59 #include "prefs_display_header.h"
64 #include "manage_window.h"
65 #include "alertpanel.h"
66 #include "statusbar.h"
67 #include "addressbook.h"
92 static gint lock_socket = -1;
93 static gint lock_socket_tag = 0;
97 ONLINE_MODE_DONT_CHANGE,
105 gboolean receive_all;
107 const gchar *compose_mailto;
108 GPtrArray *attach_files;
116 static void parse_cmd_opt(int argc, char *argv[]);
119 static void idle_function_for_gpgme(void);
120 #endif /* USE_GPGME */
122 static gint prohibit_duplicate_launch (void);
123 static gchar * get_crashfile_name (void);
124 static void lock_socket_input_cb (gpointer data,
126 GdkInputCondition condition);
130 gchar *get_socket_name (void);
133 static void open_compose_new (const gchar *address,
134 GPtrArray *attach_files);
136 static void send_queue (void);
137 static void initial_processing (FolderItem *item, gpointer data);
141 _("File `%s' already exists.\n"
142 "Can't create folder.")
145 #define MAKE_DIR_IF_NOT_EXIST(dir) \
147 if (!is_dir_exist(dir)) { \
148 if (is_file_exist(dir)) { \
150 (_("File `%s' already exists.\n" \
151 "Can't create folder."), \
155 if (make_dir(dir) < 0) \
160 static MainWindow *static_mainwindow;
161 int main(int argc, char *argv[])
165 FolderView *folderview;
167 if(!sylpheed_init(&argc, &argv)) {
171 prog_version = PROG_VERSION;
172 startup_dir = g_get_current_dir();
174 argv0 = g_strdup(argv[0]);
177 parse_cmd_opt(argc, argv);
182 gtk_init(&argc, &argv);
183 crash_main(cmd.crash_params);
186 crash_install_handlers();
189 /* check and create unix domain socket */
190 lock_socket = prohibit_duplicate_launch();
191 if (lock_socket < 0) return 0;
194 puts("0 Sylpheed not running.");
199 gtk_init(&argc, &argv);
201 #if USE_THREADS || USE_LDAP
203 if (!g_thread_supported())
204 g_error(_("g_thread is not supported by glib.\n"));
209 gtk_widget_push_visual(gdk_imlib_get_visual());
210 gtk_widget_push_colormap(gdk_imlib_get_colormap());
213 /* parse gtkrc files */
214 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
216 gtk_rc_parse(userrc);
218 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
219 G_DIR_SEPARATOR_S, "gtkrc", NULL);
220 gtk_rc_parse(userrc);
222 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
223 gtk_rc_parse(userrc);
226 gtk_rc_parse("./gtkrc");
228 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
229 gtk_item_factory_parse_rc(userrc);
232 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
234 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
235 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
236 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
237 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
238 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
239 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
241 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
244 prefs_common_read_config();
248 if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) !=
249 GPGME_No_Error) { /* Also does some gpgme init */
250 rfc2015_disable_all();
251 debug_print("gpgme_engine_version:\n%s\n",
252 gpgme_get_engine_info());
254 if (prefs_common.gpg_warning) {
257 val = alertpanel_message_with_disable
259 _("GnuPG is not installed properly, or needs to be upgraded.\n"
260 "OpenPGP support disabled."));
261 if (val & G_ALERTDISABLE)
262 prefs_common.gpg_warning = FALSE;
267 gpgme_register_idle(idle_function_for_gpgme);
271 gtkaspellcheckers = gtkaspell_checkers_new();
275 prefs_common_save_config();
276 prefs_actions_read_config();
277 prefs_actions_write_config();
278 prefs_display_header_read_config();
279 prefs_display_header_write_config();
280 /* prefs_filtering_read_config(); */
281 addressbook_read_file();
282 renderer_read_config();
286 mainwin = main_window_create
287 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
288 folderview = mainwin->folderview;
290 /* register the callback of unix domain socket input */
291 lock_socket_tag = gdk_input_add(lock_socket,
292 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
293 lock_socket_input_cb,
296 account_read_config_all();
297 account_save_config_all();
299 if (folder_read_list() < 0) {
303 if (!account_get_list()) {
308 account_set_missing_folder();
309 folder_set_missing_folders();
310 folderview_set(folderview);
312 /* prefs_scoring_read_config(); */
313 prefs_matcher_read_config();
315 /* make one all-folder processing before using sylpheed */
316 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
318 /* if Sylpheed crashed, rebuild caches */
319 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
320 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
321 folderview_check_new(NULL);
323 /* make the crash-indicator file */
324 str_write_to_file("foo", get_crashfile_name());
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;
350 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
351 main_window_toggle_work_offline(mainwin, TRUE);
352 if (cmd.online_mode == ONLINE_MODE_ONLINE)
353 main_window_toggle_work_offline(mainwin, FALSE);
355 static_mainwindow = mainwin;
358 addressbook_destroy();
361 gtkaspell_checkers_delete();
367 static void parse_cmd_opt(int argc, char *argv[])
371 for (i = 1; i < argc; i++) {
372 if (!strncmp(argv[i], "--debug", 7))
373 debug_set_mode(TRUE);
374 else if (!strncmp(argv[i], "--receive-all", 13))
375 cmd.receive_all = TRUE;
376 else if (!strncmp(argv[i], "--receive", 9))
378 else if (!strncmp(argv[i], "--compose", 9)) {
379 const gchar *p = argv[i + 1];
382 cmd.compose_mailto = NULL;
383 if (p && *p != '\0' && *p != '-') {
384 if (!strncmp(p, "mailto:", 7))
385 cmd.compose_mailto = p + 7;
387 cmd.compose_mailto = p;
390 } else if (!strncmp(argv[i], "--attach", 8)) {
391 const gchar *p = argv[i + 1];
394 while (p && *p != '\0' && *p != '-') {
395 if (!cmd.attach_files)
396 cmd.attach_files = g_ptr_array_new();
397 if (*p != G_DIR_SEPARATOR)
398 file = g_strconcat(startup_dir,
403 g_ptr_array_add(cmd.attach_files, file);
407 } else if (!strncmp(argv[i], "--send", 6)) {
409 } else if (!strncmp(argv[i], "--version", 9)) {
410 puts("Sylpheed version " VERSION);
412 } else if (!strncmp(argv[i], "--status", 8)) {
414 } else if (!strncmp(argv[i], "--online", 8)) {
415 cmd.online_mode = ONLINE_MODE_ONLINE;
416 } else if (!strncmp(argv[i], "--offline", 9)) {
417 cmd.online_mode = ONLINE_MODE_OFFLINE;
418 } else if (!strncmp(argv[i], "--help", 6)) {
419 g_print(_("Usage: %s [OPTION]...\n"),
420 g_basename(argv[0]));
422 puts(_(" --compose [address] open composition window"));
423 puts(_(" --attach file1 [file2]...\n"
424 " open composition window with specified files\n"
426 puts(_(" --receive receive new messages"));
427 puts(_(" --receive-all receive new messages of all accounts"));
428 puts(_(" --send send all queued messages"));
429 puts(_(" --status show the total number of messages"));
430 puts(_(" --online switch to online mode"));
431 puts(_(" --offline switch to offline mode"));
432 puts(_(" --debug debug mode"));
433 puts(_(" --help display this help and exit"));
434 puts(_(" --version output version information and exit"));
437 } else if (!strncmp(argv[i], "--crash", 7)) {
439 cmd.crash_params = g_strdup(argv[i + 1]);
445 if (cmd.attach_files && cmd.compose == FALSE) {
447 cmd.compose_mailto = NULL;
451 static gint get_queued_message_num(void)
455 queue = folder_get_default_queue();
456 if (!queue) return -1;
458 folder_item_scan(queue);
462 static void save_all_caches(FolderItem *item, gpointer data)
466 folder_item_write_cache(item);
469 static void initial_processing(FolderItem *item, gpointer data)
471 MainWindow *mainwin = (MainWindow *)data;
474 g_return_if_fail(item);
475 buf = g_strdup_printf(_("Processing (%s)..."),
478 : _("top level folder"));
479 debug_print("%s\n", buf);
482 main_window_cursor_wait(mainwin);
484 folder_item_apply_processing(item);
486 debug_print("done.\n");
487 STATUSBAR_POP(mainwin);
488 main_window_cursor_normal(mainwin);
491 static void draft_all_messages(void)
493 GList * compose_list = compose_get_compose_list();
496 for (elem = compose_list; elem != NULL && elem->data != NULL; elem = elem->next) {
497 Compose *c = (Compose*)elem->data;
503 void clean_quit(void)
505 draft_all_messages();
507 if (prefs_common.warn_queued_on_exit)
508 { /* disable the popup */
509 prefs_common.warn_queued_on_exit = FALSE;
510 app_will_exit(NULL, static_mainwindow);
511 prefs_common.warn_queued_on_exit = TRUE;
512 prefs_common_save_config();
514 app_will_exit(NULL, static_mainwindow);
519 void app_will_exit(GtkWidget *widget, gpointer data)
521 MainWindow *mainwin = data;
524 if (compose_get_compose_list()) {
525 gint val = alertpanel(_("Notice"),
526 _("Composing message exists."),
527 _("Draft them"), _("Discard them"), _("Don't quit"));
531 case G_ALERTALTERNATE:
534 draft_all_messages();
537 manage_window_focus_in(mainwin->window, NULL, NULL);
540 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
541 if (alertpanel(_("Queued messages"),
542 _("Some unsent messages are queued. Exit now?"),
543 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
545 manage_window_focus_in(mainwin->window, NULL, NULL);
548 inc_autocheck_timer_remove();
551 gpgmegtk_free_passphrase();
554 if (prefs_common.clean_on_exit)
555 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
557 /* save prefs for opened folder */
558 if(mainwin->folderview->opened)
562 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
563 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
566 /* save all state before exiting */
568 folder_func_to_all_folders(save_all_caches, NULL);
570 main_window_get_size(mainwin);
571 main_window_get_position(mainwin);
572 prefs_common_save_config();
573 account_save_config_all();
574 addressbook_export_to_file();
576 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
577 gtk_item_factory_dump_rc(filename, NULL, TRUE);
580 /* delete temporary files */
581 remove_all_files(get_mime_tmp_dir());
585 /* delete unix domain socket */
586 gdk_input_remove(lock_socket_tag);
587 fd_close(lock_socket);
588 filename = get_socket_name();
591 /* delete crashfile */
593 unlink(get_crashfile_name());
603 static void idle_function_for_gpgme(void)
605 while (gtk_events_pending())
606 gtk_main_iteration();
608 #endif /* USE_GPGME */
611 * CLAWS: want this public so crash dialog can delete the
617 gchar *get_socket_name(void)
619 static gchar *filename = NULL;
621 if (filename == NULL) {
622 filename = g_strdup_printf("%s%csylpheed-%d",
623 g_get_tmp_dir(), G_DIR_SEPARATOR,
630 static gchar *get_crashfile_name(void)
632 static gchar *filename = NULL;
634 if (filename == NULL) {
635 filename = g_strdup_printf("%s%csylpheed-crashed",
636 get_tmp_dir(), G_DIR_SEPARATOR);
642 static gint prohibit_duplicate_launch(void)
647 path = get_socket_name();
648 uxsock = fd_connect_unix(path);
651 return fd_open_unix(path);
654 /* remote command mode */
656 debug_print("another Sylpheed is already running.\n");
659 fd_write(uxsock, "receive_all\n", 12);
660 else if (cmd.receive)
661 fd_write(uxsock, "receive\n", 8);
662 else if (cmd.compose && cmd.attach_files) {
663 gchar *str, *compose_str;
666 if (cmd.compose_mailto)
667 compose_str = g_strdup_printf("compose_attach %s\n",
670 compose_str = g_strdup("compose_attach\n");
672 fd_write(uxsock, compose_str, strlen(compose_str));
675 for (i = 0; i < cmd.attach_files->len; i++) {
676 str = g_ptr_array_index(cmd.attach_files, i);
677 fd_write(uxsock, str, strlen(str));
678 fd_write(uxsock, "\n", 1);
681 fd_write(uxsock, ".\n", 2);
682 } else if (cmd.compose) {
685 if (cmd.compose_mailto)
686 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
688 compose_str = g_strdup("compose\n");
690 fd_write(uxsock, compose_str, strlen(compose_str));
692 } else if (cmd.send) {
693 fd_write(uxsock, "send\n", 5);
694 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
695 fd_write(uxsock, "online\n", 6);
696 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
697 fd_write(uxsock, "offline\n", 7);
698 } else if (cmd.status) {
701 fd_write(uxsock, "status\n", 7);
702 fd_gets(uxsock, buf, sizeof(buf));
705 fd_write(uxsock, "popup\n", 6);
711 static void lock_socket_input_cb(gpointer data,
713 GdkInputCondition condition)
715 MainWindow *mainwin = (MainWindow *)data;
719 sock = fd_accept(source);
720 fd_gets(sock, buf, sizeof(buf));
722 if (!strncmp(buf, "popup", 5)) {
723 main_window_popup(mainwin);
724 } else if (!strncmp(buf, "receive_all", 11)) {
725 main_window_popup(mainwin);
726 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
727 } else if (!strncmp(buf, "receive", 7)) {
728 main_window_popup(mainwin);
729 inc_mail(mainwin, prefs_common.newmail_notify_manu);
730 } else if (!strncmp(buf, "compose_attach", 14)) {
734 mailto = g_strdup(buf + strlen("compose_attach") + 1);
735 files = g_ptr_array_new();
736 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
737 if (buf[0] == '.' && buf[1] == '\n') break;
739 g_ptr_array_add(files, g_strdup(buf));
741 open_compose_new(mailto, files);
742 ptr_array_free_strings(files);
743 g_ptr_array_free(files, TRUE);
745 } else if (!strncmp(buf, "compose", 7)) {
746 open_compose_new(buf + strlen("compose") + 1, NULL);
747 } else if (!strncmp(buf, "send", 4)) {
749 } else if (!strncmp(buf, "online", 6)) {
750 main_window_toggle_work_offline(mainwin, FALSE);
751 } else if (!strncmp(buf, "offline", 7)) {
752 main_window_toggle_work_offline(mainwin, TRUE);
753 } else if (!strncmp(buf, "status", 6)) {
754 guint new, unread, unreadmarked, total;
756 folder_count_total_msgs(&new, &unread, &unreadmarked, &total);
757 g_snprintf(buf, sizeof(buf), "%d %d %d %d\n", new, unread, unreadmarked, total);
758 fd_write(sock, buf, strlen(buf));
764 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
769 Xstrdup_a(addr, address, return);
773 compose_new(NULL, addr, attach_files);
776 static void send_queue(void)
779 FolderItem *def_outbox;
781 def_outbox = folder_get_default_outbox();
783 for (list = folder_get_list(); list != NULL; list = list->next) {
784 Folder *folder = list->data;
787 if (procmsg_send_queue
788 (folder->queue, prefs_common.savemsg) < 0)
789 alertpanel_error(_("Some errors occurred while sending queued messages."));
790 folder_item_scan(folder->queue);
791 if (prefs_common.savemsg && folder->outbox) {
792 folder_update_item(folder->outbox, TRUE);
793 if (folder->outbox == def_outbox)
799 if (prefs_common.savemsg && def_outbox)
800 folder_update_item(def_outbox, TRUE);