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);
181 crash_main(cmd.crash_params);
184 crash_install_handlers();
187 /* check and create unix domain socket */
188 lock_socket = prohibit_duplicate_launch();
189 if (lock_socket < 0) return 0;
192 puts("0 Sylpheed not running.");
197 gtk_init(&argc, &argv);
199 #if USE_THREADS || USE_LDAP
201 if (!g_thread_supported())
202 g_error(_("g_thread is not supported by glib.\n"));
207 gtk_widget_push_visual(gdk_imlib_get_visual());
208 gtk_widget_push_colormap(gdk_imlib_get_colormap());
211 /* parse gtkrc files */
212 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
214 gtk_rc_parse(userrc);
216 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
217 G_DIR_SEPARATOR_S, "gtkrc", NULL);
218 gtk_rc_parse(userrc);
220 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
221 gtk_rc_parse(userrc);
224 gtk_rc_parse("./gtkrc");
226 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
227 gtk_item_factory_parse_rc(userrc);
230 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
232 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
233 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
234 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
235 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
236 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
237 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
239 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
242 prefs_common_read_config();
246 if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) !=
247 GPGME_No_Error) { /* Also does some gpgme init */
248 rfc2015_disable_all();
249 debug_print("gpgme_engine_version:\n%s\n",
250 gpgme_get_engine_info());
252 if (prefs_common.gpg_warning) {
255 val = alertpanel_message_with_disable
257 _("GnuPG is not installed properly, or needs to be upgraded.\n"
258 "OpenPGP support disabled."));
259 if (val & G_ALERTDISABLE)
260 prefs_common.gpg_warning = FALSE;
265 gpgme_register_idle(idle_function_for_gpgme);
269 gtkaspellcheckers = gtkaspell_checkers_new();
273 prefs_common_save_config();
274 prefs_actions_read_config();
275 prefs_actions_write_config();
276 prefs_display_header_read_config();
277 prefs_display_header_write_config();
278 /* prefs_filtering_read_config(); */
279 addressbook_read_file();
280 renderer_read_config();
284 mainwin = main_window_create
285 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
286 folderview = mainwin->folderview;
288 /* register the callback of unix domain socket input */
289 lock_socket_tag = gdk_input_add(lock_socket,
290 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
291 lock_socket_input_cb,
294 account_read_config_all();
295 account_save_config_all();
297 if (folder_read_list() < 0) {
301 if (!account_get_list()) {
306 account_set_missing_folder();
307 folder_set_missing_folders();
308 folderview_set(folderview);
310 /* prefs_scoring_read_config(); */
311 prefs_matcher_read_config();
313 /* make one all-folder processing before using sylpheed */
314 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
316 /* if Sylpheed crashed, rebuild caches */
317 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
318 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
319 folderview_check_new(NULL);
321 /* make the crash-indicator file */
322 str_write_to_file("foo", get_crashfile_name());
324 addressbook_read_file();
326 inc_autocheck_timer_init(mainwin);
328 /* ignore SIGPIPE signal for preventing sudden death of program */
329 signal(SIGPIPE, SIG_IGN);
331 if (cmd.receive_all || prefs_common.chk_on_startup)
332 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
333 else if (cmd.receive)
334 inc_mail(mainwin, prefs_common.newmail_notify_manu);
336 gtk_widget_grab_focus(folderview->ctree);
339 open_compose_new(cmd.compose_mailto, cmd.attach_files);
340 if (cmd.attach_files) {
341 ptr_array_free_strings(cmd.attach_files);
342 g_ptr_array_free(cmd.attach_files, TRUE);
343 cmd.attach_files = NULL;
348 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
349 main_window_toggle_work_offline(mainwin, TRUE);
350 if (cmd.online_mode == ONLINE_MODE_ONLINE)
351 main_window_toggle_work_offline(mainwin, FALSE);
353 static_mainwindow = mainwin;
356 addressbook_destroy();
359 gtkaspell_checkers_delete();
365 static void parse_cmd_opt(int argc, char *argv[])
369 for (i = 1; i < argc; i++) {
370 if (!strncmp(argv[i], "--debug", 7))
371 debug_set_mode(TRUE);
372 else if (!strncmp(argv[i], "--receive-all", 13))
373 cmd.receive_all = TRUE;
374 else if (!strncmp(argv[i], "--receive", 9))
376 else if (!strncmp(argv[i], "--compose", 9)) {
377 const gchar *p = argv[i + 1];
380 cmd.compose_mailto = NULL;
381 if (p && *p != '\0' && *p != '-') {
382 if (!strncmp(p, "mailto:", 7))
383 cmd.compose_mailto = p + 7;
385 cmd.compose_mailto = p;
388 } else if (!strncmp(argv[i], "--attach", 8)) {
389 const gchar *p = argv[i + 1];
392 while (p && *p != '\0' && *p != '-') {
393 if (!cmd.attach_files)
394 cmd.attach_files = g_ptr_array_new();
395 if (*p != G_DIR_SEPARATOR)
396 file = g_strconcat(startup_dir,
401 g_ptr_array_add(cmd.attach_files, file);
405 } else if (!strncmp(argv[i], "--send", 6)) {
407 } else if (!strncmp(argv[i], "--version", 9)) {
408 puts("Sylpheed version " VERSION);
410 } else if (!strncmp(argv[i], "--status", 8)) {
412 } else if (!strncmp(argv[i], "--online", 8)) {
413 cmd.online_mode = ONLINE_MODE_ONLINE;
414 } else if (!strncmp(argv[i], "--offline", 9)) {
415 cmd.online_mode = ONLINE_MODE_OFFLINE;
416 } else if (!strncmp(argv[i], "--help", 6)) {
417 g_print(_("Usage: %s [OPTION]...\n"),
418 g_basename(argv[0]));
420 puts(_(" --compose [address] open composition window"));
421 puts(_(" --attach file1 [file2]...\n"
422 " open composition window with specified files\n"
424 puts(_(" --receive receive new messages"));
425 puts(_(" --receive-all receive new messages of all accounts"));
426 puts(_(" --send send all queued messages"));
427 puts(_(" --status show the total number of messages"));
428 puts(_(" --online switch to online mode"));
429 puts(_(" --offline switch to offline mode"));
430 puts(_(" --debug debug mode"));
431 puts(_(" --help display this help and exit"));
432 puts(_(" --version output version information and exit"));
435 } else if (!strncmp(argv[i], "--crash", 7)) {
437 cmd.crash_params = g_strdup(argv[i + 1]);
443 if (cmd.attach_files && cmd.compose == FALSE) {
445 cmd.compose_mailto = NULL;
449 static gint get_queued_message_num(void)
453 queue = folder_get_default_queue();
454 if (!queue) return -1;
456 folder_item_scan(queue);
460 static void save_all_caches(FolderItem *item, gpointer data)
464 folder_item_write_cache(item);
467 static void initial_processing(FolderItem *item, gpointer data)
469 MainWindow *mainwin = (MainWindow *)data;
472 g_return_if_fail(item);
473 buf = g_strdup_printf(_("Processing (%s)..."),
476 : _("top level folder"));
477 debug_print("%s\n", buf);
480 main_window_cursor_wait(mainwin);
482 folder_item_apply_processing(item);
484 debug_print("done.\n");
485 STATUSBAR_POP(mainwin);
486 main_window_cursor_normal(mainwin);
489 static void draft_all_messages(void)
491 GList * compose_list = compose_get_compose_list();
494 for (elem = compose_list; elem != NULL && elem->data != NULL; elem = elem->next) {
495 Compose *c = (Compose*)elem->data;
501 void clean_quit(void)
503 draft_all_messages();
505 if (prefs_common.warn_queued_on_exit)
506 { /* disable the popup */
507 prefs_common.warn_queued_on_exit = FALSE;
508 app_will_exit(NULL, static_mainwindow);
509 prefs_common.warn_queued_on_exit = TRUE;
510 prefs_common_save_config();
512 app_will_exit(NULL, static_mainwindow);
517 void app_will_exit(GtkWidget *widget, gpointer data)
519 MainWindow *mainwin = data;
522 if (compose_get_compose_list()) {
523 gint val = alertpanel(_("Notice"),
524 _("Composing message exists."),
525 _("Draft them"), _("Discard them"), _("Don't quit"));
529 case G_ALERTALTERNATE:
532 draft_all_messages();
535 manage_window_focus_in(mainwin->window, NULL, NULL);
538 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
539 if (alertpanel(_("Queued messages"),
540 _("Some unsent messages are queued. Exit now?"),
541 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
543 manage_window_focus_in(mainwin->window, NULL, NULL);
546 inc_autocheck_timer_remove();
549 gpgmegtk_free_passphrase();
552 if (prefs_common.clean_on_exit)
553 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
555 /* save prefs for opened folder */
556 if(mainwin->folderview->opened)
560 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
561 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
564 /* save all state before exiting */
566 folder_func_to_all_folders(save_all_caches, NULL);
568 main_window_get_size(mainwin);
569 main_window_get_position(mainwin);
570 prefs_common_save_config();
571 account_save_config_all();
572 addressbook_export_to_file();
574 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
575 gtk_item_factory_dump_rc(filename, NULL, TRUE);
578 /* delete temporary files */
579 remove_all_files(get_mime_tmp_dir());
583 /* delete unix domain socket */
584 gdk_input_remove(lock_socket_tag);
585 fd_close(lock_socket);
586 filename = get_socket_name();
589 /* delete crashfile */
591 unlink(get_crashfile_name());
601 static void idle_function_for_gpgme(void)
603 while (gtk_events_pending())
604 gtk_main_iteration();
606 #endif /* USE_GPGME */
609 * CLAWS: want this public so crash dialog can delete the
615 gchar *get_socket_name(void)
617 static gchar *filename = NULL;
619 if (filename == NULL) {
620 filename = g_strdup_printf("%s%csylpheed-%d",
621 g_get_tmp_dir(), G_DIR_SEPARATOR,
628 static gchar *get_crashfile_name(void)
630 static gchar *filename = NULL;
632 if (filename == NULL) {
633 filename = g_strdup_printf("%s%csylpheed-crashed",
634 get_tmp_dir(), G_DIR_SEPARATOR);
640 static gint prohibit_duplicate_launch(void)
645 path = get_socket_name();
646 uxsock = fd_connect_unix(path);
649 return fd_open_unix(path);
652 /* remote command mode */
654 debug_print("another Sylpheed is already running.\n");
657 fd_write(uxsock, "receive_all\n", 12);
658 else if (cmd.receive)
659 fd_write(uxsock, "receive\n", 8);
660 else if (cmd.compose && cmd.attach_files) {
661 gchar *str, *compose_str;
664 if (cmd.compose_mailto)
665 compose_str = g_strdup_printf("compose_attach %s\n",
668 compose_str = g_strdup("compose_attach\n");
670 fd_write(uxsock, compose_str, strlen(compose_str));
673 for (i = 0; i < cmd.attach_files->len; i++) {
674 str = g_ptr_array_index(cmd.attach_files, i);
675 fd_write(uxsock, str, strlen(str));
676 fd_write(uxsock, "\n", 1);
679 fd_write(uxsock, ".\n", 2);
680 } else if (cmd.compose) {
683 if (cmd.compose_mailto)
684 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
686 compose_str = g_strdup("compose\n");
688 fd_write(uxsock, compose_str, strlen(compose_str));
690 } else if (cmd.send) {
691 fd_write(uxsock, "send\n", 5);
692 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
693 fd_write(uxsock, "online\n", 6);
694 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
695 fd_write(uxsock, "offline\n", 7);
696 } else if (cmd.status) {
699 fd_write(uxsock, "status\n", 7);
700 fd_gets(uxsock, buf, sizeof(buf));
703 fd_write(uxsock, "popup\n", 6);
709 static void lock_socket_input_cb(gpointer data,
711 GdkInputCondition condition)
713 MainWindow *mainwin = (MainWindow *)data;
717 sock = fd_accept(source);
718 fd_gets(sock, buf, sizeof(buf));
720 if (!strncmp(buf, "popup", 5)) {
721 main_window_popup(mainwin);
722 } else if (!strncmp(buf, "receive_all", 11)) {
723 main_window_popup(mainwin);
724 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
725 } else if (!strncmp(buf, "receive", 7)) {
726 main_window_popup(mainwin);
727 inc_mail(mainwin, prefs_common.newmail_notify_manu);
728 } else if (!strncmp(buf, "compose_attach", 14)) {
732 mailto = g_strdup(buf + strlen("compose_attach") + 1);
733 files = g_ptr_array_new();
734 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
735 if (buf[0] == '.' && buf[1] == '\n') break;
737 g_ptr_array_add(files, g_strdup(buf));
739 open_compose_new(mailto, files);
740 ptr_array_free_strings(files);
741 g_ptr_array_free(files, TRUE);
743 } else if (!strncmp(buf, "compose", 7)) {
744 open_compose_new(buf + strlen("compose") + 1, NULL);
745 } else if (!strncmp(buf, "send", 4)) {
747 } else if (!strncmp(buf, "online", 6)) {
748 main_window_toggle_work_offline(mainwin, FALSE);
749 } else if (!strncmp(buf, "offline", 7)) {
750 main_window_toggle_work_offline(mainwin, TRUE);
751 } else if (!strncmp(buf, "status", 6)) {
752 guint new, unread, unreadmarked, total;
754 folder_count_total_msgs(&new, &unread, &unreadmarked, &total);
755 g_snprintf(buf, sizeof(buf), "%d %d %d %d\n", new, unread, unreadmarked, total);
756 fd_write(sock, buf, strlen(buf));
762 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
767 Xstrdup_a(addr, address, return);
771 compose_new(NULL, addr, attach_files);
774 static void send_queue(void)
777 FolderItem *def_outbox;
779 def_outbox = folder_get_default_outbox();
781 for (list = folder_get_list(); list != NULL; list = list->next) {
782 Folder *folder = list->data;
785 if (procmsg_send_queue
786 (folder->queue, prefs_common.savemsg) < 0)
787 alertpanel_error(_("Some errors occurred while sending queued messages."));
789 folder_item_scan(folder->queue);
790 if (prefs_common.savemsg && folder->outbox) {
791 folder_update_item(folder->outbox, TRUE);
792 if (folder->outbox == def_outbox)
798 if (prefs_common.savemsg && def_outbox)
799 folder_update_item(def_outbox, TRUE);