ftp://ftp.redhat.com/pub/redhat/linux/rawhide/SRPMS/SRPMS/gnome-vfs2-2.3.8-1.src.rpm
[gnome-vfs-httpcaptive.git] / libgnomevfs / gnome-vfs-init.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* gnome-vfs-init.c - Initialization for the GNOME Virtual File System.
3
4    Copyright (C) 1999 Free Software Foundation
5
6    The Gnome Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The Gnome Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the Gnome Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.
20
21    Author: Ettore Perazzoli <ettore@gnu.org>
22 */
23
24 #include <config.h>
25 #include "gnome-vfs-init.h"
26
27 #include "gnome-vfs-ssl-private.h"
28 #include "gnome-vfs-mime.h"
29
30 #include "gnome-vfs-configuration.h"
31 #include "gnome-vfs-i18n.h"
32 #include "gnome-vfs-method.h"
33 #include "gnome-vfs-process.h"
34 #include "gnome-vfs-utils.h"
35
36 #include "gnome-vfs-async-job-map.h"
37 #include "gnome-vfs-thread-pool.h"
38 #include "gnome-vfs-job-queue.h"
39
40 #include <errno.h>
41 #include <bonobo-activation/bonobo-activation.h>
42 #include <glib/gmessages.h>
43 #include <glib/gfileutils.h>
44 #include <libgnomevfs/gnome-vfs-job-slave.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47
48 static gboolean vfs_already_initialized = FALSE;
49 G_LOCK_DEFINE_STATIC (vfs_already_initialized);
50
51 static GPrivate * private_is_primary_thread;
52
53 static gboolean
54 ensure_dot_gnome_exists (void)
55 {
56         gboolean retval = TRUE;
57         gchar *dirname;
58
59         dirname = g_build_filename (g_get_home_dir (), ".gnome", NULL);
60
61         if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
62                 if (mkdir (dirname, S_IRWXU) != 0) {
63                         g_warning ("Unable to create ~/.gnome directory: %s",
64                                    g_strerror (errno));
65                         retval = FALSE;
66                 }
67         } else if (!g_file_test (dirname, G_FILE_TEST_IS_DIR)) {
68                 g_warning ("Error: ~/.gnome must be a directory.");
69                 retval = FALSE;
70         }
71
72         g_free (dirname);
73         return retval;
74 }
75
76 static void
77 gnome_vfs_pthread_init (void)
78 {
79         private_is_primary_thread = g_private_new (NULL);
80         g_private_set (private_is_primary_thread, GUINT_TO_POINTER (1));
81         
82         _gnome_vfs_module_callback_private_init ();
83         
84         _gnome_vfs_async_job_map_init ();
85         _gnome_vfs_thread_pool_init ();
86         _gnome_vfs_job_queue_init ();
87 }
88
89 /**
90  * gnome_vfs_init:
91  *
92  * If GnomeVFS is not already initialized, initialize it. This must be
93  * called prior to performing any other GnomeVFS operations, and may
94  * be called multiple times without error.
95  * 
96  * Return value: %TRUE if GnomeVFS is successfully initialized (or was
97  * already initialized)
98  **/
99 gboolean 
100 gnome_vfs_init (void)
101 {
102         gboolean retval;
103         char *bogus_argv[2] = { "dummy", NULL };
104
105         if (!ensure_dot_gnome_exists ()) {
106                 return FALSE;
107         }
108
109         if (!g_thread_supported ())
110                 g_thread_init (NULL);
111
112         G_LOCK (vfs_already_initialized);
113
114         if (!vfs_already_initialized) {
115 #ifdef ENABLE_NLS
116                 bindtextdomain (GETTEXT_PACKAGE, GNOMEVFS_LOCALEDIR);
117                 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
118 #endif   
119                 gnome_vfs_pthread_init ();
120
121                 if (bonobo_activation_orb_get() == NULL) {
122                         bonobo_activation_init (0, bogus_argv);
123                 }
124
125                 _gnome_vfs_ssl_init ();
126
127                 retval = gnome_vfs_method_init ();
128
129                 if (retval) {
130                         retval = _gnome_vfs_process_init ();
131                 }
132                 if (retval) {
133                         retval = _gnome_vfs_configuration_init ();
134                 }
135                 if (retval) {
136                         signal (SIGPIPE, SIG_IGN);
137                 }
138         } else {
139                 retval = TRUE;  /* Who cares after all.  */
140         }
141
142         vfs_already_initialized = TRUE;
143         G_UNLOCK (vfs_already_initialized);
144
145         return retval;
146 }
147
148 /**
149  * gnome_vfs_initialized:
150  *
151  * Detects if GnomeVFS has already been initialized (GnomeVFS must be
152  * initialized prior to using any methods or operations).
153  * 
154  * Return value: %TRUE if GnomeVFS has already been initialized
155  **/
156 gboolean
157 gnome_vfs_initialized (void)
158 {
159         gboolean out;
160
161         G_LOCK (vfs_already_initialized);
162         out = vfs_already_initialized;
163         G_UNLOCK (vfs_already_initialized);
164         return out;
165 }
166
167 /**
168  * gnome_vfs_shutdown:
169  *
170  * Cease all active GnomeVFS operations and unload the MIME
171  * database from memory.
172  * 
173  **/
174 void
175 gnome_vfs_shutdown (void)
176 {
177         _gnome_vfs_thread_backend_shutdown ();
178         gnome_vfs_mime_shutdown ();
179 }
180
181 void
182 gnome_vfs_loadinit (gpointer app, gpointer modinfo)
183 {
184 }
185
186 void
187 gnome_vfs_preinit (gpointer app, gpointer modinfo)
188 {
189 }
190
191 void
192 gnome_vfs_postinit (gpointer app, gpointer modinfo)
193 {
194         G_LOCK (vfs_already_initialized);
195
196         gnome_vfs_pthread_init ();
197
198         gnome_vfs_method_init ();
199         _gnome_vfs_process_init ();
200         _gnome_vfs_configuration_init ();
201
202         signal (SIGPIPE, SIG_IGN);
203
204         vfs_already_initialized = TRUE;
205         G_UNLOCK (vfs_already_initialized);
206 }
207
208 /**
209  * gnome_vfs_is_primary_thread:
210  *
211  * Check if the current thread is the thread with the main glib event loop.
212  *
213  * Return value: %TRUE if the current thread is the thread with the 
214  * main glib event loop
215  **/
216 gboolean
217 gnome_vfs_is_primary_thread (void)
218 {
219         if (g_thread_supported()) {
220                 return GPOINTER_TO_UINT(g_private_get (private_is_primary_thread)) == 1;
221         } else {
222                 return TRUE;
223         }
224 }