source: trunk/third/libgnome/libgnome/gnome-init.c @ 20811

Revision 20811, 13.1 KB checked in by ghudson, 20 years ago (diff)
Merge with libgnome 2.8.0.
Line 
1/* gnomelib-init.c - Implement libgnome module
2   Copyright (C) 1997, 1998, 1999 Free Software Foundation
3                 1999, 2000 Red Hat, Inc.
4                 2001 SuSE Linux AG.
5   All rights reserved.
6
7   This file is part of GNOME 2.0.
8
9   The Gnome Library is free software; you can redistribute it and/or
10   modify it under the terms of the GNU Library General Public License as
11   published by the Free Software Foundation; either version 2 of the
12   License, or (at your option) any later version.
13
14   The Gnome Library is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   Library General Public License for more details.
18
19   You should have received a copy of the GNU Library General Public
20   License along with the Gnome Library; see the file COPYING.LIB.  If not,
21   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22   Boston, MA 02111-1307, USA.  */
23/*
24  @NOTATION@
25 */
26
27#include <config.h>
28#include <errno.h>
29#include <locale.h>
30#include <stdarg.h>
31#include <string.h>
32#include <stdlib.h>
33#include <sys/types.h>
34#include <fcntl.h>
35#include <unistd.h>
36#include <pwd.h>
37#include <sys/stat.h>
38
39#include <glib.h>
40#include "gnome-i18nP.h"
41
42#include "gnome-init.h"
43#include "gnome-gconfP.h"
44#include "gnome-util.h"
45#include "gnome-sound.h"
46#include "gnome-triggers.h"
47
48#include <errno.h>
49
50#include <bonobo-activation/bonobo-activation.h>
51#include <bonobo-activation/bonobo-activation-version.h>
52#include <libbonobo.h>
53
54#include <libgnomevfs/gnome-vfs-init.h>
55
56/*****************************************************************************
57 * bonobo
58 *****************************************************************************/
59
60static void
61bonobo_post_args_parse (GnomeProgram *program, GnomeModuleInfo *mod_info)
62{
63        int dumb_argc = 1;
64        char *dumb_argv[] = {NULL};
65
66        dumb_argv[0] = g_get_prgname ();
67
68        bonobo_init (&dumb_argc, dumb_argv);
69}
70
71/**
72* gnome_bonobo_module_info_get:
73*
74* Retrieves the bonobo module version and indicate that it requires the current
75* libgnome and its dependencies (although libbonobo does not depend on
76* libgnome, libbonoboui does and this will also be initialised when
77* initialising a GNOME app).
78*
79* Returns: a new #GnomeModuleInfo structure describing the version of the
80* bonobo modules and its dependents.
81*/
82const GnomeModuleInfo *
83gnome_bonobo_module_info_get (void)
84{
85        static GnomeModuleInfo module_info = {
86                "bonobo",
87                /* FIXME: get this from bonobo */"1.101.2",
88                N_("Bonobo Support"),
89                NULL, NULL,
90                NULL, bonobo_post_args_parse,
91                NULL, NULL, NULL, NULL
92        };
93
94        if (module_info.requirements == NULL) {
95                static GnomeModuleRequirement req[2];
96
97                req[0].required_version = VERSION;
98                req[0].module_info = LIBGNOME_MODULE;
99
100                req[1].required_version = NULL;
101                req[1].module_info = NULL;
102
103                module_info.requirements = req;
104        }
105
106        return &module_info;
107}
108
109/*****************************************************************************
110 * bonobo-activation
111 *****************************************************************************/
112
113static void
114bonobo_activation_pre_args_parse (GnomeProgram *program, GnomeModuleInfo *mod_info)
115{
116        if (!g_thread_supported ())
117                g_thread_init (NULL);
118
119        if (!bonobo_activation_is_initialized ())
120                bonobo_activation_preinit (program, mod_info);
121}
122
123static void
124bonobo_activation_post_args_parse (GnomeProgram *program, GnomeModuleInfo *mod_info)
125{
126        if (!bonobo_activation_is_initialized ()) {
127                int dumb_argc = 1;
128                char *dumb_argv[] = {NULL};
129
130                dumb_argv[0] = g_get_prgname ();
131                (void) bonobo_activation_orb_init (&dumb_argc, dumb_argv);
132               
133                bonobo_activation_postinit (program, mod_info);
134        }
135}
136
137/* No need to make this public, always pulled in */
138static const GnomeModuleInfo *
139gnome_bonobo_activation_module_info_get (void)
140{
141        static GnomeModuleInfo module_info = {
142                "bonobo-activation", NULL, N_("Bonobo activation Support"),
143                NULL, NULL,
144                bonobo_activation_pre_args_parse, bonobo_activation_post_args_parse,
145                bonobo_activation_popt_options
146        };
147        if (module_info.version == NULL) {
148                module_info.version = g_strdup_printf
149                        ("%d.%d.%d",
150                         BONOBO_ACTIVATION_MAJOR_VERSION,
151                         BONOBO_ACTIVATION_MINOR_VERSION,
152                         BONOBO_ACTIVATION_MICRO_VERSION);
153        }
154        return &module_info;
155}
156
157/*****************************************************************************
158 * libgnome
159 *****************************************************************************/
160
161enum { ARG_DISABLE_SOUND = 1, ARG_ENABLE_SOUND, ARG_ESPEAKER, ARG_VERSION };
162
163static char *gnome_user_dir = NULL;
164static char *gnome_user_private_dir = NULL;
165static char *gnome_user_accels_dir = NULL;
166
167/**
168* gnome_user_dir_get:
169*
170* Retrieves the user-specific directory for GNOME apps to use ($HOME/.gnome2
171* is the usual GNOME 2 value).
172*
173* Returns: An absolute path to the directory.
174*/
175const char *
176gnome_user_dir_get (void)
177{
178        return gnome_user_dir;
179}
180
181/**
182* gnome_user_private_dir_get:
183*
184* Differs from gnome_user_dir_get() in that the directory returned here will
185* have had permissions of 0700 (rwx------) enforced when it was created.  Of
186* course, the permissions may have been altered since creation, so care still
187* needs to be taken.
188*
189* Returns: An absolute path to the user-specific private directory that GNOME
190* apps can use.
191*/
192const char *
193gnome_user_private_dir_get (void)
194{
195        return gnome_user_private_dir;
196}
197
198/**
199* gnome_user_accels_dir_get:
200*
201* Retrieves the user-specific directory that stores the keyboard shortcut files
202* for each GNOME app. Note that most applications should be using GConf for
203* storing this information, but it may be necessary to use the
204* gnome_user_accels_dir_get() directory for legacy applications.
205*
206* Returns: The absolute path to the directory.
207*/
208const char *
209gnome_user_accels_dir_get (void)
210{
211        return gnome_user_accels_dir;
212}
213
214static void
215libgnome_option_cb (poptContext ctx, enum poptCallbackReason reason,
216                    const struct poptOption *opt, const char *arg,
217                    void *data)
218{
219        GnomeProgram *program;
220        GValue value = { 0 };
221
222        program = gnome_program_get ();
223       
224        switch(reason) {
225        case POPT_CALLBACK_REASON_OPTION:
226                switch(opt->val) {
227                case ARG_ESPEAKER:
228                        g_value_init (&value, G_TYPE_STRING);
229                        g_value_set_string (&value, opt->arg);
230                        g_object_set_property (G_OBJECT (program),
231                                               GNOME_PARAM_ESPEAKER, &value);
232                        g_value_unset (&value);
233                        break;
234
235                case ARG_DISABLE_SOUND:
236                        g_value_init (&value, G_TYPE_BOOLEAN);
237                        g_value_set_boolean (&value, FALSE);
238                        g_object_set_property (G_OBJECT (program),
239                                               GNOME_PARAM_ENABLE_SOUND, &value);
240                        g_value_unset (&value);
241                        break;
242
243                case ARG_ENABLE_SOUND:
244                        g_value_init (&value, G_TYPE_BOOLEAN);
245                        g_value_set_boolean (&value, TRUE);
246                        g_object_set_property (G_OBJECT (program),
247                                               GNOME_PARAM_ENABLE_SOUND, &value);
248                        g_value_unset (&value);
249                        break;
250
251                case ARG_VERSION:
252                        g_print ("Gnome %s %s\n",
253                                 gnome_program_get_app_id (program),
254                                 gnome_program_get_app_version (program));
255                        exit(0);
256                        break;
257                }
258        default:
259                /* do nothing */
260                break;
261        }
262}
263
264static int
265safe_mkdir (const char *pathname, mode_t mode)
266{
267        char *safe_pathname;
268        int len, ret;
269       
270        safe_pathname = g_strdup (pathname);
271        len = strlen (safe_pathname);
272       
273        if (len > 1 && safe_pathname[len - 1] == '/')
274                safe_pathname[len - 1] = '\0';
275
276        ret = mkdir (safe_pathname, mode);
277
278        g_free (safe_pathname);
279
280        return ret;
281}
282
283static void
284libgnome_userdir_setup (gboolean create_dirs)
285{
286        if(!gnome_user_dir) {
287                const char *override;
288
289                /* FIXME this env variable should be changed
290                 * for each major GNOME release, would be easier to
291                 * remember if not hardcoded.
292                 */
293                override = g_getenv ("GNOME22_USER_DIR");
294               
295                if (override != NULL) {
296                        int len;
297
298                        gnome_user_dir = g_strdup (override);
299
300                        /* chop trailing slash */
301                        len = strlen (gnome_user_dir);
302                        if (len > 1 && gnome_user_dir[len-1] == '/')
303                                gnome_user_dir[len - 1] = '\0';
304                       
305                        gnome_user_private_dir = g_strconcat (gnome_user_dir,
306                                                              "_private",
307                                                              NULL);
308                } else {
309                        gnome_user_dir = g_build_filename (g_get_home_dir(), GNOME_DOT_GNOME, NULL);
310                        gnome_user_private_dir = g_build_filename (g_get_home_dir(),
311                                                                   GNOME_DOT_GNOME_PRIVATE, NULL);
312                }
313               
314                gnome_user_accels_dir = g_build_filename (gnome_user_dir,
315                                                          "accels", NULL);
316        }
317
318        if (!create_dirs)
319                return;
320       
321        if (safe_mkdir (gnome_user_dir, 0700) < 0) { /* private permissions, but we
322                                                        don't check that we got them */
323                if (errno != EEXIST) {
324                        g_printerr (_("Could not create per-user gnome configuration directory `%s': %s\n"),
325                                gnome_user_dir, strerror(errno));
326                }
327        }
328   
329        if (safe_mkdir (gnome_user_private_dir, 0700) < 0) { /* This is private
330                                                                per-user info mode
331                                                                700 will be
332                                                                enforced!  maybe
333                                                                even other security
334                                                                meassures will be
335                                                                taken */
336                if (errno != EEXIST) {
337                        g_printerr (_("Could not create per-user gnome configuration directory `%s': %s\n"),
338                                 gnome_user_private_dir, strerror(errno));
339                }
340        }
341
342
343        /* change mode to 0700 on the private directory */
344        if (chmod (gnome_user_private_dir, 0700) < 0) {
345                g_printerr (_("Could not set mode 0700 on private per-user gnome configuration directory `%s': %s\n"),
346                        gnome_user_private_dir, strerror(errno));
347        }
348 
349        if (safe_mkdir (gnome_user_accels_dir, 0700) < 0) {
350                if (errno != EEXIST) {
351                        g_printerr (_("Could not create gnome accelerators directory `%s': %s\n"),
352                                gnome_user_accels_dir, strerror(errno));
353                }
354        }
355}
356
357static void
358libgnome_post_args_parse (GnomeProgram *program,
359                          GnomeModuleInfo *mod_info)
360{
361        GValue value = { 0 };
362        gboolean enable_val = TRUE, create_dirs_val = TRUE;                           
363        char *espeaker_val = NULL;                                                   
364
365        g_value_init (&value, G_TYPE_BOOLEAN);
366        g_object_get_property (G_OBJECT (program),
367                               GNOME_PARAM_CREATE_DIRECTORIES,
368                               &value);
369        create_dirs_val = g_value_get_boolean (&value);
370        g_value_unset (&value);
371
372        g_value_init (&value, G_TYPE_BOOLEAN);
373        g_object_get_property (G_OBJECT (program),
374                               GNOME_PARAM_ENABLE_SOUND, &value);
375        enable_val = g_value_get_boolean (&value);
376        g_value_unset (&value);
377
378        g_value_init (&value, G_TYPE_STRING);
379        g_object_get_property (G_OBJECT (program),
380                               GNOME_PARAM_ESPEAKER, &value);
381        espeaker_val = g_value_dup_string (&value);
382        g_value_unset (&value);
383
384
385        if (enable_val) {
386                gnome_sound_init(espeaker_val);
387        }
388
389        libgnome_userdir_setup (create_dirs_val);
390
391}
392
393static struct poptOption gnomelib_options [] = {
394        { NULL, '\0', POPT_ARG_INTL_DOMAIN, GETTEXT_PACKAGE, 0, NULL, NULL},
395
396        { NULL, '\0', POPT_ARG_CALLBACK, (void *) libgnome_option_cb, 0, NULL, NULL},
397
398        { "disable-sound", '\0', POPT_ARG_NONE,                                 
399          NULL, ARG_DISABLE_SOUND, N_("Disable sound server usage"), NULL},     
400
401        { "enable-sound", '\0', POPT_ARG_NONE,                                 
402          NULL, ARG_ENABLE_SOUND, N_("Enable sound server usage"), NULL},       
403
404        { "espeaker", '\0', POPT_ARG_STRING,                                   
405          NULL, ARG_ESPEAKER, N_("Host:port on which the sound server to use is"
406                                 " running"),
407          N_("HOSTNAME:PORT")},                                                 
408
409        {"version", '\0', POPT_ARG_NONE,
410         NULL, ARG_VERSION, VERSION, NULL},
411
412        { NULL, '\0', 0,
413          NULL, 0 , NULL, NULL}
414};
415
416static void
417gnome_vfs_post_args_parse (GnomeProgram *program, GnomeModuleInfo *mod_info)
418{
419        gnome_vfs_init ();
420}
421
422/* No need for this to be public */
423static const GnomeModuleInfo *
424gnome_vfs_module_info_get (void)
425{
426        static GnomeModuleInfo module_info = {
427                "gnome-vfs", GNOMEVFSVERSION, N_("GNOME Virtual Filesystem"),
428                NULL, NULL,
429                NULL, gnome_vfs_post_args_parse,
430                NULL,
431                NULL,
432                NULL
433        };
434        return &module_info;
435}
436/**
437* libgnome_module_info_get:
438*
439* Retrieves the current libgnome version and the modules it depends on.
440*
441* Returns: a new #GnomeModuleInfo structure describing the version and
442* the versions of the dependents.
443*/
444const GnomeModuleInfo *
445libgnome_module_info_get (void)
446{
447        static GnomeModuleInfo module_info = {
448                "libgnome", VERSION, N_("GNOME Library"),
449                NULL, NULL,
450                NULL, libgnome_post_args_parse,
451                gnomelib_options,
452                NULL, NULL, NULL, NULL
453        };
454
455        if (module_info.requirements == NULL) {
456                static GnomeModuleRequirement req[4];
457
458                bindtextdomain (GETTEXT_PACKAGE, LIBGNOME_LOCALEDIR);
459#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
460                bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
461#endif
462                req[0].required_version = "0.9.1";
463                req[0].module_info = gnome_bonobo_activation_module_info_get ();
464
465                req[1].required_version = "0.3.0";
466                req[1].module_info = gnome_vfs_module_info_get ();
467
468                req[2].required_version = "1.1.1";
469                req[2].module_info = gnome_gconf_module_info_get ();
470
471                req[3].required_version = NULL;
472                req[3].module_info = NULL;
473
474                module_info.requirements = req;
475        }
476
477        return &module_info;
478}
Note: See TracBrowser for help on using the repository browser.