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;
164 int main(int argc, char *argv[])
168 FolderView *folderview;
170 if (!sylpheed_init(&argc, &argv)) {
174 prog_version = PROG_VERSION;
175 startup_dir = g_get_current_dir();
177 argv0 = g_strdup(argv[0]);
180 parse_cmd_opt(argc, argv);
185 gtk_init(&argc, &argv);
186 crash_main(cmd.crash_params);
189 crash_install_handlers();
192 /* check and create unix domain socket */
193 lock_socket = prohibit_duplicate_launch();
194 if (lock_socket < 0) return 0;
197 puts("0 Sylpheed not running.");
198 lock_socket_remove();
203 gtk_init(&argc, &argv);
205 #if USE_THREADS || USE_LDAP
207 if (!g_thread_supported())
208 g_error(_("g_thread is not supported by glib.\n"));
213 gtk_widget_push_visual(gdk_imlib_get_visual());
214 gtk_widget_push_colormap(gdk_imlib_get_colormap());
217 /* parse gtkrc files */
218 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
220 gtk_rc_parse(userrc);
222 userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
223 G_DIR_SEPARATOR_S, "gtkrc", NULL);
224 gtk_rc_parse(userrc);
226 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
227 gtk_rc_parse(userrc);
230 gtk_rc_parse("./gtkrc");
232 userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
233 gtk_item_factory_parse_rc(userrc);
236 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
238 MAKE_DIR_IF_NOT_EXIST(RC_DIR);
239 MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
240 MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
241 MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
242 MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
243 MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
245 set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
247 folder_system_init();
249 prefs_common_read_config();
253 if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) !=
254 GPGME_No_Error) { /* Also does some gpgme init */
255 rfc2015_disable_all();
256 debug_print("gpgme_engine_version:\n%s\n",
257 gpgme_get_engine_info());
259 if (prefs_common.gpg_warning) {
262 val = alertpanel_message_with_disable
264 _("GnuPG is not installed properly, or needs to be upgraded.\n"
265 "OpenPGP support disabled."));
266 if (val & G_ALERTDISABLE)
267 prefs_common.gpg_warning = FALSE;
272 gpgme_register_idle(idle_function_for_gpgme);
276 gtkaspellcheckers = gtkaspell_checkers_new();
280 prefs_common_save_config();
281 prefs_actions_read_config();
282 prefs_actions_write_config();
283 prefs_display_header_read_config();
284 prefs_display_header_write_config();
285 /* prefs_filtering_read_config(); */
286 addressbook_read_file();
287 renderer_read_config();
291 mainwin = main_window_create
292 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
293 folderview = mainwin->folderview;
295 /* register the callback of unix domain socket input */
296 lock_socket_tag = gdk_input_add(lock_socket,
297 GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
298 lock_socket_input_cb,
301 account_read_config_all();
302 account_save_config_all();
304 if (folder_read_list() < 0) {
308 if (!account_get_list()) {
313 account_set_missing_folder();
314 folder_set_missing_folders();
315 folderview_set(folderview);
317 /* prefs_scoring_read_config(); */
318 prefs_matcher_read_config();
320 /* make one all-folder processing before using sylpheed */
321 folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
323 /* if Sylpheed crashed, rebuild caches */
324 if (!cmd.crash && is_file_exist(get_crashfile_name())) {
325 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
326 folderview_check_new(NULL);
328 /* make the crash-indicator file */
329 str_write_to_file("foo", get_crashfile_name());
331 addressbook_read_file();
333 inc_autocheck_timer_init(mainwin);
335 /* ignore SIGPIPE signal for preventing sudden death of program */
336 signal(SIGPIPE, SIG_IGN);
338 if (cmd.receive_all || prefs_common.chk_on_startup)
339 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
340 else if (cmd.receive)
341 inc_mail(mainwin, prefs_common.newmail_notify_manu);
343 gtk_widget_grab_focus(folderview->ctree);
346 open_compose_new(cmd.compose_mailto, cmd.attach_files);
347 if (cmd.attach_files) {
348 ptr_array_free_strings(cmd.attach_files);
349 g_ptr_array_free(cmd.attach_files, TRUE);
350 cmd.attach_files = NULL;
355 if (cmd.online_mode == ONLINE_MODE_OFFLINE)
356 main_window_toggle_work_offline(mainwin, TRUE);
357 if (cmd.online_mode == ONLINE_MODE_ONLINE)
358 main_window_toggle_work_offline(mainwin, FALSE);
360 prefs_toolbar_init();
362 plugin_load_all("GTK");
364 static_mainwindow = mainwin;
367 plugin_unload_all("GTK");
369 prefs_toolbar_done();
371 addressbook_destroy();
374 gtkaspell_checkers_delete();
381 static void parse_cmd_opt(int argc, char *argv[])
385 for (i = 1; i < argc; i++) {
386 if (!strncmp(argv[i], "--receive-all", 13))
387 cmd.receive_all = TRUE;
388 else if (!strncmp(argv[i], "--receive", 9))
390 else if (!strncmp(argv[i], "--compose", 9)) {
391 const gchar *p = argv[i + 1];
394 cmd.compose_mailto = NULL;
395 if (p && *p != '\0' && *p != '-') {
396 if (!strncmp(p, "mailto:", 7))
397 cmd.compose_mailto = p + 7;
399 cmd.compose_mailto = p;
402 } else if (!strncmp(argv[i], "--attach", 8)) {
403 const gchar *p = argv[i + 1];
406 while (p && *p != '\0' && *p != '-') {
407 if (!cmd.attach_files)
408 cmd.attach_files = g_ptr_array_new();
409 if (*p != G_DIR_SEPARATOR)
410 file = g_strconcat(startup_dir,
415 g_ptr_array_add(cmd.attach_files, file);
419 } else if (!strncmp(argv[i], "--send", 6)) {
421 } else if (!strncmp(argv[i], "--version", 9)) {
422 puts("Sylpheed version " VERSION);
424 } else if (!strncmp(argv[i], "--status", 8)) {
426 } else if (!strncmp(argv[i], "--online", 8)) {
427 cmd.online_mode = ONLINE_MODE_ONLINE;
428 } else if (!strncmp(argv[i], "--offline", 9)) {
429 cmd.online_mode = ONLINE_MODE_OFFLINE;
430 } else if (!strncmp(argv[i], "--help", 6)) {
431 g_print(_("Usage: %s [OPTION]...\n"),
432 g_basename(argv[0]));
434 puts(_(" --compose [address] open composition window"));
435 puts(_(" --attach file1 [file2]...\n"
436 " open composition window with specified files\n"
438 puts(_(" --receive receive new messages"));
439 puts(_(" --receive-all receive new messages of all accounts"));
440 puts(_(" --send send all queued messages"));
441 puts(_(" --status show the total number of messages"));
442 puts(_(" --online switch to online mode"));
443 puts(_(" --offline switch to offline mode"));
444 puts(_(" --debug debug mode"));
445 puts(_(" --help display this help and exit"));
446 puts(_(" --version output version information and exit"));
449 } else if (!strncmp(argv[i], "--crash", 7)) {
451 cmd.crash_params = g_strdup(argv[i + 1]);
457 if (cmd.attach_files && cmd.compose == FALSE) {
459 cmd.compose_mailto = NULL;
463 static gint get_queued_message_num(void)
467 queue = folder_get_default_queue();
468 if (!queue) return -1;
470 folder_item_scan(queue);
471 return queue->total_msgs;
474 static void save_all_caches(FolderItem *item, gpointer data)
478 folder_item_write_cache(item);
481 static void initial_processing(FolderItem *item, gpointer data)
483 MainWindow *mainwin = (MainWindow *)data;
486 g_return_if_fail(item);
487 buf = g_strdup_printf(_("Processing (%s)..."),
490 : _("top level folder"));
491 debug_print("%s\n", buf);
494 main_window_cursor_wait(mainwin);
496 folder_item_apply_processing(item);
498 debug_print("done.\n");
499 STATUSBAR_POP(mainwin);
500 main_window_cursor_normal(mainwin);
503 static void draft_all_messages(void)
505 GList *compose_list = compose_get_compose_list();
509 for (elem = compose_list; elem != NULL && elem->data != NULL;
511 Compose *c = (Compose*)elem->data;
517 void clean_quit(void)
519 /*!< Good idea to have the main window stored in a
520 * static variable so we can check that variable
521 * to see if we're really allowed to do things
522 * that actually the spawner is supposed to
523 * do (like: sending mail, composing messages).
524 * Because, really, if we're the spawnee, and
525 * we touch GTK stuff, we're hosed. See the
528 /* FIXME: Use something else to signal that we're
529 * in the original spawner, and not in a spawned
531 if (!static_mainwindow)
534 draft_all_messages();
536 if (prefs_common.warn_queued_on_exit) {
537 /* disable the popup */
538 prefs_common.warn_queued_on_exit = FALSE;
539 app_will_exit(NULL, static_mainwindow);
540 prefs_common.warn_queued_on_exit = TRUE;
541 prefs_common_save_config();
543 app_will_exit(NULL, static_mainwindow);
548 void app_will_exit(GtkWidget *widget, gpointer data)
550 MainWindow *mainwin = data;
553 if (compose_get_compose_list()) {
554 gint val = alertpanel(_("Notice"),
555 _("Composing message exists."),
556 _("Draft them"), _("Discard them"), _("Don't quit"));
560 case G_ALERTALTERNATE:
563 draft_all_messages();
566 manage_window_focus_in(mainwin->window, NULL, NULL);
569 if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
570 if (alertpanel(_("Queued messages"),
571 _("Some unsent messages are queued. Exit now?"),
572 _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
574 manage_window_focus_in(mainwin->window, NULL, NULL);
577 inc_autocheck_timer_remove();
580 gpgmegtk_free_passphrase();
583 if (prefs_common.clean_on_exit)
584 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
586 /* save prefs for opened folder */
587 if(mainwin->folderview->opened)
591 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
592 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
595 /* save all state before exiting */
597 folder_func_to_all_folders(save_all_caches, NULL);
599 main_window_get_size(mainwin);
600 main_window_get_position(mainwin);
601 prefs_common_save_config();
602 account_save_config_all();
603 addressbook_export_to_file();
605 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
606 gtk_item_factory_dump_rc(filename, NULL, TRUE);
609 /* delete temporary files */
610 remove_all_files(get_mime_tmp_dir());
614 /* delete crashfile */
616 unlink(get_crashfile_name());
618 lock_socket_remove();
624 static void idle_function_for_gpgme(void)
626 while (gtk_events_pending())
627 gtk_main_iteration();
629 #endif /* USE_GPGME */
632 * CLAWS: want this public so crash dialog can delete the
638 gchar *get_socket_name(void)
640 static gchar *filename = NULL;
642 if (filename == NULL) {
643 filename = g_strdup_printf("%s%csylpheed-%d",
644 g_get_tmp_dir(), G_DIR_SEPARATOR,
651 static gchar *get_crashfile_name(void)
653 static gchar *filename = NULL;
655 if (filename == NULL) {
656 filename = g_strdup_printf("%s%csylpheed-crashed",
657 get_tmp_dir(), G_DIR_SEPARATOR);
663 static gint prohibit_duplicate_launch(void)
668 path = get_socket_name();
669 uxsock = fd_connect_unix(path);
672 return fd_open_unix(path);
675 /* remote command mode */
677 debug_print("another Sylpheed is already running.\n");
680 fd_write_all(uxsock, "receive_all\n", 12);
681 else if (cmd.receive)
682 fd_write_all(uxsock, "receive\n", 8);
683 else if (cmd.compose && cmd.attach_files) {
684 gchar *str, *compose_str;
687 if (cmd.compose_mailto)
688 compose_str = g_strdup_printf("compose_attach %s\n",
691 compose_str = g_strdup("compose_attach\n");
693 fd_write_all(uxsock, compose_str, strlen(compose_str));
696 for (i = 0; i < cmd.attach_files->len; i++) {
697 str = g_ptr_array_index(cmd.attach_files, i);
698 fd_write_all(uxsock, str, strlen(str));
699 fd_write_all(uxsock, "\n", 1);
702 fd_write_all(uxsock, ".\n", 2);
703 } else if (cmd.compose) {
706 if (cmd.compose_mailto)
707 compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
709 compose_str = g_strdup("compose\n");
711 fd_write_all(uxsock, compose_str, strlen(compose_str));
713 } else if (cmd.send) {
714 fd_write_all(uxsock, "send\n", 5);
715 } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
716 fd_write(uxsock, "online\n", 6);
717 } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
718 fd_write(uxsock, "offline\n", 7);
719 } else if (cmd.status) {
722 fd_write_all(uxsock, "status\n", 7);
723 fd_gets(uxsock, buf, sizeof(buf));
726 fd_write_all(uxsock, "popup\n", 6);
732 static gint lock_socket_remove(void)
736 if (lock_socket < 0) return -1;
738 if (lock_socket_tag > 0)
739 gdk_input_remove(lock_socket_tag);
740 fd_close(lock_socket);
741 filename = get_socket_name();
747 static void lock_socket_input_cb(gpointer data,
749 GdkInputCondition condition)
751 MainWindow *mainwin = (MainWindow *)data;
755 sock = fd_accept(source);
756 fd_gets(sock, buf, sizeof(buf));
758 if (!strncmp(buf, "popup", 5)) {
759 main_window_popup(mainwin);
760 } else if (!strncmp(buf, "receive_all", 11)) {
761 main_window_popup(mainwin);
762 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
763 } else if (!strncmp(buf, "receive", 7)) {
764 main_window_popup(mainwin);
765 inc_mail(mainwin, prefs_common.newmail_notify_manu);
766 } else if (!strncmp(buf, "compose_attach", 14)) {
770 mailto = g_strdup(buf + strlen("compose_attach") + 1);
771 files = g_ptr_array_new();
772 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
773 if (buf[0] == '.' && buf[1] == '\n') break;
775 g_ptr_array_add(files, g_strdup(buf));
777 open_compose_new(mailto, files);
778 ptr_array_free_strings(files);
779 g_ptr_array_free(files, TRUE);
781 } else if (!strncmp(buf, "compose", 7)) {
782 open_compose_new(buf + strlen("compose") + 1, NULL);
783 } else if (!strncmp(buf, "send", 4)) {
785 } else if (!strncmp(buf, "online", 6)) {
786 main_window_toggle_work_offline(mainwin, FALSE);
787 } else if (!strncmp(buf, "offline", 7)) {
788 main_window_toggle_work_offline(mainwin, TRUE);
789 } else if (!strncmp(buf, "status", 6)) {
790 guint new, unread, unreadmarked, total;
792 folder_count_total_msgs(&new, &unread, &unreadmarked, &total);
793 g_snprintf(buf, sizeof(buf), "%d %d %d %d\n", new, unread, unreadmarked, total);
794 fd_write_all(sock, buf, strlen(buf));
800 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
805 Xstrdup_a(addr, address, return);
809 compose_new(NULL, addr, attach_files);
812 static void send_queue(void)
815 FolderItem *def_outbox;
817 def_outbox = folder_get_default_outbox();
819 for (list = folder_get_list(); list != NULL; list = list->next) {
820 Folder *folder = list->data;
823 gint res = procmsg_send_queue
824 (folder->queue, prefs_common.savemsg);
827 alertpanel_error(_("Some errors occurred while sending queued messages."));
829 folder_item_scan(folder->queue);
830 if (prefs_common.savemsg && folder->outbox) {
831 if (folder->outbox == def_outbox)