~ubuntu-branches/ubuntu/precise/libglade2/precise-proposed

« back to all changes in this revision

Viewing changes to glade/glade-init.c

  • Committer: Bazaar Package Importer
  • Author(s): Christian Marillat
  • Date: 2002-03-30 22:55:37 UTC
  • Revision ID: james.westby@ubuntu.com-20020330225537-jx47zcil1ybgy9qx
Tags: upstream-1.99.10
ImportĀ upstreamĀ versionĀ 1.99.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; c-basic-offset: 4 -*-
 
2
 * libglade - a library for building interfaces from XML files at runtime
 
3
 * Copyright (C) 1998-2002  James Henstridge <james@daa.com.au>
 
4
 *
 
5
 * glade-init.c: initialisation functions for libglade
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Library General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Library General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Library General Public
 
18
 * License along with this library; if not, write to the 
 
19
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
20
 * Boston, MA  02111-1307, USA.
 
21
 */
 
22
#ifdef HAVE_CONFIG_H
 
23
#  include <config.h>
 
24
#endif
 
25
 
 
26
#include <string.h>
 
27
#include <glib.h>
 
28
#include <gmodule.h>
 
29
 
 
30
#include <pango/pango-utils.h>
 
31
 
 
32
#include "glade-init.h"
 
33
#include "glade-build.h"
 
34
#include "glade-private.h"
 
35
 
 
36
#ifdef DEBUG
 
37
guint _glade_debug_flags = 0;
 
38
static const  GDebugKey libglade_debug_keys[] = {
 
39
    { "parser", GLADE_DEBUG_PARSER },
 
40
    { "build",  GLADE_DEBUG_BUILD },
 
41
};
 
42
static const guint libglade_ndebug_keys = G_N_ELEMENTS(libglade_debug_keys);
 
43
#endif
 
44
 
 
45
 
 
46
void _glade_init_gtk_widgets (void);
 
47
 
 
48
/**
 
49
 * glade_init:
 
50
 * 
 
51
 * It used to be necessary to call glade_init() before creating
 
52
 * GladeXML objects.  This is now no longer the case, as libglade will
 
53
 * be initialised on demand now.  Calling glade_init() manually will
 
54
 * not cause any problems though.
 
55
 */
 
56
void
 
57
glade_init(void)
 
58
{
 
59
    static gboolean initialised = FALSE;
 
60
#ifdef DEBUG
 
61
    const gchar *env_string;
 
62
#endif
 
63
 
 
64
    if (initialised) return;
 
65
    initialised = TRUE;
 
66
    _glade_init_gtk_widgets();
 
67
 
 
68
#ifdef DEBUG
 
69
    env_string = g_getenv("LIBGLADE_DEBUG");
 
70
    if (env_string != NULL) {
 
71
        _glade_debug_flags = g_parse_debug_string (env_string,
 
72
                                                   libglade_debug_keys,
 
73
                                                   libglade_ndebug_keys);
 
74
        env_string = NULL;
 
75
    }
 
76
#endif
 
77
 
 
78
}
 
79
 
 
80
gchar *
 
81
glade_module_check_version(gint version)
 
82
{
 
83
  if (version != GLADE_MODULE_API_VERSION)
 
84
    return "Wrong plugin API version";
 
85
  else
 
86
    return NULL;
 
87
}
 
88
 
 
89
static GPtrArray *loaded_packages = NULL;
 
90
 
 
91
static gchar **
 
92
get_module_path (void)
 
93
{
 
94
    const gchar *module_path_env = g_getenv ("LIBGLADE_MODULE_PATH");
 
95
    const gchar *exe_prefix = g_getenv("LIBGLADE_EXE_PREFIX");
 
96
    gchar **result;
 
97
    gchar *module_path;
 
98
    gchar *default_dir;
 
99
    
 
100
    if (exe_prefix)
 
101
        default_dir = g_build_filename (exe_prefix, "lib", NULL);
 
102
    else
 
103
        default_dir = g_strdup (GLADE_LIBDIR);
 
104
    
 
105
    module_path = g_strconcat (module_path_env ? module_path_env : "",
 
106
                               module_path_env ? G_SEARCHPATH_SEPARATOR_S : "",
 
107
                               default_dir, NULL);
 
108
    
 
109
    result = pango_split_file_list (module_path);
 
110
    
 
111
    g_free (default_dir);
 
112
    g_free (module_path);
 
113
    
 
114
    return result;
 
115
}
 
116
 
 
117
static GModule *
 
118
find_module (gchar      **module_path,
 
119
             const gchar *subdir,
 
120
             const gchar *name)
 
121
{
 
122
    GModule *module;
 
123
    gchar *module_name;
 
124
    gint i;
 
125
    
 
126
    if (g_path_is_absolute (name))
 
127
        return g_module_open (name, G_MODULE_BIND_LAZY);
 
128
    
 
129
    for (i = 0; module_path[i]; i++) {
 
130
        gchar *version_directory;
 
131
        
 
132
#ifndef G_OS_WIN32 /* ignoring GTK_BINARY_VERSION elsewhere too */
 
133
        version_directory = g_build_filename (module_path[i], subdir, NULL);
 
134
        module_name = g_module_build_path (version_directory, name);
 
135
        g_free (version_directory);
 
136
        
 
137
        /*g_print ("trying: %s\n", module_name);*/
 
138
 
 
139
        if (g_file_test (module_name, G_FILE_TEST_EXISTS)) {
 
140
            module = g_module_open (module_name, G_MODULE_BIND_LAZY);
 
141
            g_free (module_name);
 
142
            return module;
 
143
        }
 
144
      
 
145
        g_free (module_name);
 
146
#endif
 
147
        
 
148
        module_name = g_module_build_path (module_path[i], name);
 
149
        
 
150
        if (g_file_test (module_name, G_FILE_TEST_EXISTS)) {
 
151
            module = g_module_open (module_name, G_MODULE_BIND_LAZY);
 
152
            g_free (module_name);
 
153
            return module;
 
154
        }
 
155
        
 
156
        g_free (module_name);
 
157
    }
 
158
    
 
159
    /* As last resort, try loading without an absolute path (using system
 
160
     * library path)
 
161
     */
 
162
    module_name = g_module_build_path (NULL, name);
 
163
    module = g_module_open (module_name, G_MODULE_BIND_LAZY);
 
164
    g_free(module_name);
 
165
    
 
166
    return module;
 
167
}
 
168
 
 
169
/**
 
170
 * glade_require:
 
171
 * @library: the required library
 
172
 *
 
173
 * Ensure that a required library is available.  If it is not already
 
174
 * available, libglade will attempt to dynamically load a module that
 
175
 * contains the handlers for that library.
 
176
 */
 
177
 
 
178
void
 
179
glade_require(const gchar *library)
 
180
{
 
181
    gboolean already_loaded = FALSE;
 
182
    GModule *module;
 
183
    void (* init_func)(void);
 
184
    static char **module_path = NULL;
 
185
 
 
186
    /* a call to glade_init here to make sure libglade is initialised */
 
187
    glade_init();
 
188
 
 
189
    if (loaded_packages) {
 
190
        gint i;
 
191
 
 
192
        for (i = 0; i < loaded_packages->len; i++)
 
193
            if (!strcmp(library, g_ptr_array_index(loaded_packages, i))) {
 
194
                already_loaded = TRUE;
 
195
                break;
 
196
            }
 
197
    }
 
198
 
 
199
    if (already_loaded)
 
200
        return;
 
201
 
 
202
    if (!module_path)
 
203
        module_path = get_module_path ();
 
204
 
 
205
    module = find_module (module_path, "libglade/2.0", library);
 
206
 
 
207
    if (!module) {
 
208
        g_warning("Could not load support for `%s': %s", library,
 
209
                  g_module_error());
 
210
        return;
 
211
    }
 
212
 
 
213
    if (!g_module_symbol(module, "glade_module_register_widgets",
 
214
                         (gpointer)&init_func)) {
 
215
        g_warning("could not find `%s' init function: %s", library,
 
216
                  g_module_error());
 
217
        g_module_close(module);
 
218
        return;
 
219
    }
 
220
 
 
221
    init_func();
 
222
    g_module_make_resident(module);
 
223
}
 
224
 
 
225
/**
 
226
 * glade_provide:
 
227
 * @library: the provided library
 
228
 *
 
229
 * This function should be called by a module to assert that it
 
230
 * provides wrappers for a particular library.  This should be called
 
231
 * by the register_widgets() function of a libglade module so that it
 
232
 * isn't loaded twice, for instance.
 
233
 */
 
234
 
 
235
void
 
236
glade_provide(const gchar *library)
 
237
{
 
238
    gboolean already_loaded = FALSE;
 
239
    gint i;
 
240
 
 
241
    if (!loaded_packages)
 
242
        loaded_packages = g_ptr_array_new();
 
243
 
 
244
    for (i = 0; i < loaded_packages->len; i++)
 
245
        if (!strcmp(library, g_ptr_array_index(loaded_packages, i))) {
 
246
            already_loaded = TRUE;
 
247
            break;
 
248
        }
 
249
 
 
250
    if (!already_loaded)
 
251
        g_ptr_array_add(loaded_packages, g_strdup(library));
 
252
}
 
253
 
 
254
/**
 
255
 * GLADE_MODULE_CHECK_INIT:
 
256
 *
 
257
 * This macro will insert a suitable version check function into a
 
258
 * libglade loadable module.
 
259
 */