2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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"
74 #include "prefs_toolbar.h"
94 static gint lock_socket = -1;
95 static gint lock_socket_tag = 0;
99 ONLINE_MODE_DONT_CHANGE,
104 static struct RemoteCmd {
106 gboolean receive_all;
108 const gchar *compose_mailto;
109 GPtrArray *attach_files;
117 static void parse_cmd_opt(int argc, char *argv[]);
120 static void idle_function_for_gpgme(void);
121 #endif /* USE_GPGME */
123 static gint prohibit_duplicate_launch (void);
124 static gchar * get_crashfile_name (void);
125 static gint lock_socket_remove (void);
126 static void lock_socket_input_cb (gpointer data,
128 GdkInputCondition condition);
132 gchar *get_socket_name (void);
135 static void open_compose_new (const gchar *address,
136 GPtrArray *attach_files);
138 static void send_queue (void);
139 static void initial_processing (FolderItem *item, gpointer data);
143 _("File `%s' already exists.\n"
144 "Can't create folder.")
147 #define MAKE_DIR_IF_NOT_EXIST(dir) \
149 if (!is_dir_exist(dir)) { \
150 if (is_file_exist(dir)) { \
152 (_("File `%s' already exists.\n" \
153 "Can't create folder."), \
157 if (make_dir(dir) < 0) \
162 static MainWindow *static_mainwindow;
163 int main(int argc, char *argv[])
167 FolderView *folderview;
169 if(!sylpheed_init(&argc, &argv)) {
173 prog_version = PROG_VERSION;
174 startup_dir = g_get_current_dir();
176 argv0 = g_strdup(argv[0]);
179 parse_cmd_opt(argc, argv);
184 gtk_init(&argc, &argv);
185 crash_main(cmd.crash_params);
188 crash_install_handlers();
191 /* check and create unix domain socket */
192 lock_socket = prohibit_duplicate_launch();
193 if (lock_socket < 0) return 0;
196 puts("0 Sylpheed not running.");
197 lock_socket_remove();
202 gtk_init(&argc, &argv);
204 #if USE_THREADS || USE_LDAP
206 if (!g_thread_supported())
207 g_error(_("g_thread is not supported by glib.\n"));
212 gtk_widget_push_visual(gdk_imlib_get_visual());
213 gtk_widget_push_colormap(gdk_imlib_get_colormap());
216 /* parse gtkrc files */
217 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
219 gtk_rc_parse(userrc);
221 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
222 G_DIR_SEPARATOR_S, "gtkrc", NULL);
223 gtk_rc_parse(userrc);
225 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
226 gtk_rc_parse(userrc);
229 gtk_rc_parse("./gtkrc");
231 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
232 gtk_item_factory_parse_rc(userrc);
235 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
237 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
238 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
239 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
240 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
241 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
242 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
244 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
246 folder_system_init();
248 prefs_common_read_config();
252 if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) !=
253 GPGME_No_Error) { /* Also does some gpgme init */
254 rfc2015_disable_all();
255 debug_print("gpgme_engine_version:\n%s\n",
256 gpgme_get_engine_info());
258 if (prefs_common.gpg_warning) {
261 val = alertpanel_message_with_disable
263 _("GnuPG is not installed properly, or needs to be upgraded.\n"
264 "OpenPGP support disabled."));
265 if (val & G_ALERTDISABLE)
266 prefs_common.gpg_warning = FALSE;
271 gpgme_register_idle(idle_function_for_gpgme);
275 gtkaspellcheckers = gtkaspell_checkers_new();
279 prefs_common_save_config();
280 prefs_actions_read_config();
281 prefs_actions_write_config();
282 prefs_display_header_read_config();
283 prefs_display_header_write_config();
284 /* prefs_filtering_read_config(); */
285 addressbook_read_file();
286 renderer_read_config();
290 mainwin = main_window_create
291 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
292 folderview = mainwin->folderview;
294 /* register the callback of unix domain socket input */
295 lock_socket_tag = gdk_input_add(lock_socket,
296 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
297 lock_socket_input_cb,
300 account_read_config_all();
301 account_save_config_all();
303 if (folder_read_list() < 0) {
307 if (!account_get_list()) {
312 account_set_missing_folder();
313 folder_set_missing_folders();
314 folderview_set(folderview);
316 /* prefs_scoring_read_config(); */
317 prefs_matcher_read_config();
319 /* make one all-folder processing before using sylpheed */
320 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
322 /* if Sylpheed crashed, rebuild caches */
323 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
324 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
325 folderview_check_new(NULL);
327 /* make the crash-indicator file */
328 str_write_to_file("foo", get_crashfile_name());
330 addressbook_read_file();
332 inc_autocheck_timer_init(mainwin);
334 /* ignore SIGPIPE signal for preventing sudden death of program */
335 signal(SIGPIPE, SIG_IGN);
337 if (cmd.receive_all || prefs_common.chk_on_startup)
338 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
339 else if (cmd.receive)
340 inc_mail(mainwin, prefs_common.newmail_notify_manu);
342 gtk_widget_grab_focus(folderview->ctree);
345 open_compose_new(cmd.compose_mailto, cmd.attach_files);
346 if (cmd.attach_files) {
347 ptr_array_free_strings(cmd.attach_files);
348 g_ptr_array_free(cmd.attach_files, TRUE);
349 cmd.attach_files = NULL;
354 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
355 main_window_toggle_work_offline(mainwin, TRUE);
356 if (cmd.online_mode == ONLINE_MODE_ONLINE)
357 main_window_toggle_work_offline(mainwin, FALSE);
359 prefs_toolbar_init();
361 plugin_load_all("GTK");
363 static_mainwindow = mainwin;
366 plugin_unload_all("GTK");
368 prefs_toolbar_done();
370 addressbook_destroy();
373 gtkaspell_checkers_delete();
380 static void parse_cmd_opt(int argc, char *argv[])
384 for (i = 1; i < argc; i++) {
385 if (!strncmp(argv[i], "--receive-all", 13))
386 cmd.receive_all = TRUE;
387 else if (!strncmp(argv[i], "--receive", 9))
389 else if (!strncmp(argv[i], "--compose", 9)) {
390 const gchar *p = argv[i + 1];
393 cmd.compose_mailto = NULL;
394 if (p && *p != '\0' && *p != '-') {
395 if (!strncmp(p, "mailto:", 7))
396 cmd.compose_mailto = p + 7;
398 cmd.compose_mailto = p;
401 } else if (!strncmp(argv[i], "--attach", 8)) {
402 const gchar *p = argv[i + 1];
405 while (p && *p != '\0' && *p != '-') {
406 if (!cmd.attach_files)
407 cmd.attach_files = g_ptr_array_new();
408 if (*p != G_DIR_SEPARATOR)
409 file = g_strconcat(startup_dir,
414 g_ptr_array_add(cmd.attach_files, file);
418 } else if (!strncmp(argv[i], "--send", 6)) {
420 } else if (!strncmp(argv[i], "--version", 9)) {
421 puts("Sylpheed version " VERSION);
423 } else if (!strncmp(argv[i], "--status", 8)) {
425 } else if (!strncmp(argv[i], "--online", 8)) {
426 cmd.online_mode = ONLINE_MODE_ONLINE;
427 } else if (!strncmp(argv[i], "--offline", 9)) {
428 cmd.online_mode = ONLINE_MODE_OFFLINE;
429 } else if (!strncmp(argv[i], "--help", 6)) {
430 g_print(_("Usage: %s [OPTION]...\n"),
431 g_basename(argv[0]));
433 puts(_(" --compose [address] open composition window"));
434 puts(_(" --attach file1 [file2]...\n"
435 " open composition window with specified files\n"
437 puts(_(" --receive receive new messages"));
438 puts(_(" --receive-all receive new messages of all accounts"));
439 puts(_(" --send send all queued messages"));
440 puts(_(" --status show the total number of messages"));
441 puts(_(" --online switch to online mode"));
442 puts(_(" --offline switch to offline mode"));
443 puts(_(" --debug debug mode"));
444 puts(_(" --help display this help and exit"));
445 puts(_(" --version output version information and exit"));
448 } else if (!strncmp(argv[i], "--crash", 7)) {
450 cmd.crash_params = g_strdup(argv[i + 1]);
456 if (cmd.attach_files && cmd.compose == FALSE) {
458 cmd.compose_mailto = NULL;
462 static gint get_queued_message_num(void)
466 queue = folder_get_default_queue();
467 if (!queue) return -1;
469 folder_item_scan(queue);
473 static void save_all_caches(FolderItem *item, gpointer data)
477 folder_item_write_cache(item);
480 static void initial_processing(FolderItem *item, gpointer data)
482 MainWindow *mainwin = (MainWindow *)data;
485 g_return_if_fail(item);
486 buf = g_strdup_printf(_("Processing (%s)..."),
489 : _("top level folder"));
490 debug_print("%s\n", buf);
493 main_window_cursor_wait(mainwin);
495 folder_item_apply_processing(item);
497 debug_print("done.\n");
498 STATUSBAR_POP(mainwin);
499 main_window_cursor_normal(mainwin);
502 static void draft_all_messages(void)
504 GList * compose_list = compose_get_compose_list();
507 for (elem = compose_list; elem != NULL && elem->data != NULL; elem = elem->next) {
508 Compose *c = (Compose*)elem->data;
514 void clean_quit(void)
516 draft_all_messages();
518 if (prefs_common.warn_queued_on_exit)
519 { /* disable the popup */
520 prefs_common.warn_queued_on_exit = FALSE;
521 app_will_exit(NULL, static_mainwindow);
522 prefs_common.warn_queued_on_exit = TRUE;
523 prefs_common_save_config();
525 app_will_exit(NULL, static_mainwindow);
530 void app_will_exit(GtkWidget *widget, gpointer data)
532 MainWindow *mainwin = data;
535 if (compose_get_compose_list()) {
536 gint val = alertpanel(_("Notice"),
537 _("Composing message exists."),
538 _("Draft them"), _("Discard them"), _("Don't quit"));
542 case G_ALERTALTERNATE:
545 draft_all_messages();
548 manage_window_focus_in(mainwin->window, NULL, NULL);
551 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
552 if (alertpanel(_("Queued messages"),
553 _("Some unsent messages are queued. Exit now?"),
554 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
556 manage_window_focus_in(mainwin->window, NULL, NULL);
559 inc_autocheck_timer_remove();
562 gpgmegtk_free_passphrase();
565 if (prefs_common.clean_on_exit)
566 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
568 /* save prefs for opened folder */
569 if(mainwin->folderview->opened)
573 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
574 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
577 /* save all state before exiting */
579 folder_func_to_all_folders(save_all_caches, NULL);
581 main_window_get_size(mainwin);
582 main_window_get_position(mainwin);
583 prefs_common_save_config();
584 account_save_config_all();
585 addressbook_export_to_file();
587 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
588 gtk_item_factory_dump_rc(filename, NULL, TRUE);
591 /* delete temporary files */
592 remove_all_files(get_mime_tmp_dir());
596 /* delete crashfile */
598 unlink(get_crashfile_name());
600 lock_socket_remove();
606 static void idle_function_for_gpgme(void)
608 while (gtk_events_pending())
609 gtk_main_iteration();
611 #endif /* USE_GPGME */
614 * CLAWS: want this public so crash dialog can delete the
620 gchar *get_socket_name(void)
622 static gchar *filename = NULL;
624 if (filename == NULL) {
625 filename = g_strdup_printf("%s%csylpheed-%d",
626 g_get_tmp_dir(), G_DIR_SEPARATOR,
633 static gchar *get_crashfile_name(void)
635 static gchar *filename = NULL;
637 if (filename == NULL) {
638 filename = g_strdup_printf("%s%csylpheed-crashed",
639 get_tmp_dir(), G_DIR_SEPARATOR);
645 static gint prohibit_duplicate_launch(void)
650 path = get_socket_name();
651 uxsock = fd_connect_unix(path);
654 return fd_open_unix(path);
657 /* remote command mode */
659 debug_print("another Sylpheed is already running.\n");
662 fd_write_all(uxsock, "receive_all\n", 12);
663 else if (cmd.receive)
664 fd_write_all(uxsock, "receive\n", 8);
665 else if (cmd.compose && cmd.attach_files) {
666 gchar *str, *compose_str;
669 if (cmd.compose_mailto)
670 compose_str = g_strdup_printf("compose_attach %s\n",
673 compose_str = g_strdup("compose_attach\n");
675 fd_write_all(uxsock, compose_str, strlen(compose_str));
678 for (i = 0; i < cmd.attach_files->len; i++) {
679 str = g_ptr_array_index(cmd.attach_files, i);
680 fd_write_all(uxsock, str, strlen(str));
681 fd_write_all(uxsock, "\n", 1);
684 fd_write_all(uxsock, ".\n", 2);
685 } else if (cmd.compose) {
688 if (cmd.compose_mailto)
689 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
691 compose_str = g_strdup("compose\n");
693 fd_write_all(uxsock, compose_str, strlen(compose_str));
695 } else if (cmd.send) {
696 fd_write_all(uxsock, "send\n", 5);
697 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
698 fd_write(uxsock, "online\n", 6);
699 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
700 fd_write(uxsock, "offline\n", 7);
701 } else if (cmd.status) {
704 fd_write_all(uxsock, "status\n", 7);
705 fd_gets(uxsock, buf, sizeof(buf));
708 fd_write_all(uxsock, "popup\n", 6);
714 static gint lock_socket_remove(void)
718 if (lock_socket < 0) return -1;
720 if (lock_socket_tag > 0)
721 gdk_input_remove(lock_socket_tag);
722 fd_close(lock_socket);
723 filename = get_socket_name();
729 static void lock_socket_input_cb(gpointer data,
731 GdkInputCondition condition)
733 MainWindow *mainwin = (MainWindow *)data;
737 sock = fd_accept(source);
738 fd_gets(sock, buf, sizeof(buf));
740 if (!strncmp(buf, "popup", 5)) {
741 main_window_popup(mainwin);
742 } else if (!strncmp(buf, "receive_all", 11)) {
743 main_window_popup(mainwin);
744 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
745 } else if (!strncmp(buf, "receive", 7)) {
746 main_window_popup(mainwin);
747 inc_mail(mainwin, prefs_common.newmail_notify_manu);
748 } else if (!strncmp(buf, "compose_attach", 14)) {
752 mailto = g_strdup(buf + strlen("compose_attach") + 1);
753 files = g_ptr_array_new();
754 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
755 if (buf[0] == '.' && buf[1] == '\n') break;
757 g_ptr_array_add(files, g_strdup(buf));
759 open_compose_new(mailto, files);
760 ptr_array_free_strings(files);
761 g_ptr_array_free(files, TRUE);
763 } else if (!strncmp(buf, "compose", 7)) {
764 open_compose_new(buf + strlen("compose") + 1, NULL);
765 } else if (!strncmp(buf, "send", 4)) {
767 } else if (!strncmp(buf, "online", 6)) {
768 main_window_toggle_work_offline(mainwin, FALSE);
769 } else if (!strncmp(buf, "offline", 7)) {
770 main_window_toggle_work_offline(mainwin, TRUE);
771 } else if (!strncmp(buf, "status", 6)) {
772 guint new, unread, unreadmarked, total;
774 folder_count_total_msgs(&new, &unread, &unreadmarked, &total);
775 g_snprintf(buf, sizeof(buf), "%d %d %d %d\n", new, unread, unreadmarked, total);
776 fd_write_all(sock, buf, strlen(buf));
782 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
787 Xstrdup_a(addr, address, return);
791 compose_new(NULL, addr, attach_files);
794 static void send_queue(void)
797 FolderItem *def_outbox;
799 def_outbox = folder_get_default_outbox();
801 for (list = folder_get_list(); list != NULL; list = list->next) {
802 Folder *folder = list->data;
805 gint res = procmsg_send_queue
806 (folder->queue, prefs_common.savemsg);
809 alertpanel_error(_("Some errors occurred while sending queued messages."));
811 folder_item_scan(folder->queue);
812 if (prefs_common.savemsg && folder->outbox) {
813 if (folder->outbox == def_outbox)