File: bonobo-object-directory.c

package info (click to toggle)
bonobo 1.0.22-2.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,412 kB
  • ctags: 5,445
  • sloc: ansic: 51,714; sh: 7,733; makefile: 1,425; yacc: 318; xml: 266; sed: 16
file content (339 lines) | stat: -rw-r--r-- 8,011 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 c-set-style: linux -*- */
/**
 * bonobo-object-directory.c: abstract the object directory
 *
 * Authors:
 *    Michael Meeks     (michael@helixcode.com)
 *    Havoc Pennington  (hp@redhat.com)
 *    Anders Carlsson   (andersca@gnu.org)
 *    Maciej Stachowiak (mjs@eazel.com)
 *
 * Copyright 1999, 2000 Havoc Pennington, Anders Carlsson,
 *                      Eazel, Inc. Helix Code, Inc.
 */

#include "config.h"
#include <glib.h>
#include <libgnome/gnome-defs.h>
#define GNOME_EXPLICIT_TRANSLATION_DOMAIN PACKAGE
#include <libgnome/gnome-i18n.h>
#include <libgnome/gnome-mime.h>
#include "bonobo-object-directory.h"
#include <liboaf/liboaf.h>

struct _ODServerInfo {
        guint refcount;
        gchar* iid;
	gchar* name;
        gchar* desc;
};

ODServerInfo*
bonobo_directory_new_server_info (const gchar *iid,
				  const gchar *name,
				  const gchar *desc)
{
        ODServerInfo *info;

        info = g_new (ODServerInfo, 1);

        info->refcount = 1;
        info->iid = iid ? g_strdup (iid) : NULL;
	info->name = name ? g_strdup (name) : NULL;
        info->desc = desc ? g_strdup (desc) : NULL;

        return info;
}

const gchar*
bonobo_directory_get_server_info_id (ODServerInfo *info)
{
        return info->iid;
}

const gchar*
bonobo_directory_get_server_info_name (ODServerInfo *info)
{
	return info->name;
}
			 
const gchar*
bonobo_directory_get_server_info_description (ODServerInfo *info)
{
        return info->desc;
}

void
bonobo_directory_server_info_ref (ODServerInfo *info)
{
        info->refcount += 1;
}

void
bonobo_directory_server_info_unref (ODServerInfo *info)
{
        g_return_if_fail(info != NULL);
        g_return_if_fail(info->refcount > 0);

        info->refcount -= 1;

        if (info->refcount == 0) {
                g_free (info->iid);
		g_free (info->name);
                g_free (info->desc);
                g_free (info);
        }
}

void
bonobo_directory_free_server_list (GList *list)
{
        GList *l;

	for (l = list; l; l = l->next)
                bonobo_directory_server_info_unref (l->data);

        g_list_free (list);
}


CORBA_ORB
bonobo_directory_get_orb (void)
{
	return oaf_orb_get ();
}

static char *
build_id_query_fragment (const char **required_ids)
{
        const char **required_ids_iter;
	const char **query_components_iter;
        char       **query_components;
	char        *query;
        guint        n_required = 0;

        /* We need to build a query up from the required_ids */
        required_ids_iter = required_ids;

        while (required_ids && *required_ids_iter) {
                ++n_required;
                ++required_ids_iter;
        }

        query_components = g_new0 (gchar*, n_required + 1);

        query_components_iter = (const gchar **) query_components;
        required_ids_iter = required_ids;

        while (*required_ids_iter) {
                *query_components_iter = g_strconcat ("repo_ids.has('",
                                                      *required_ids_iter,
                                                      "')",
                                                      NULL);
                ++query_components_iter;
                ++required_ids_iter;
        }

        query = g_strjoinv (" AND ", query_components);

        g_strfreev (query_components);

	return query;
}

GList *
bonobo_directory_get_server_list (const gchar **required_ids)
{
        GList *retval = NULL;
        gchar *query;
        OAF_ServerInfoList *servers;
        CORBA_Environment ev;
        guint i, j;
        
        g_return_val_if_fail (required_ids != NULL, NULL);
        g_return_val_if_fail (*required_ids != NULL, NULL);

	query = build_id_query_fragment (required_ids);

        CORBA_exception_init (&ev);
        servers = oaf_query (query, NULL, &ev);
        g_free (query);
        CORBA_exception_free (&ev);

        if (servers == NULL)
                return NULL;

	for (i = 0; i < servers->_length; i++) {
                OAF_ServerInfo *oafinfo = &servers->_buffer[i];
                ODServerInfo *info;
		gchar *name = NULL, *desc = NULL;

		for (j = 0; j < oafinfo->props._length; j++) {
			if (oafinfo->props._buffer[j].v._d != OAF_P_STRING)
				continue;

			if (strcmp (oafinfo->props._buffer[j].name, "name") == 0)
				name = oafinfo->props._buffer[j].v._u.value_string;

			else if (strcmp (oafinfo->props._buffer[j].name, "description") == 0)
				desc = oafinfo->props._buffer[j].v._u.value_string;

			/* FIXME: internationalize here */
		}

		/*
		 * If no name attribute exists, use the description attribute.
		 *  If no description attribute exists, use the name attribute.
		 *  If neither a description attribute nor a name attribute exists, use the oafiid
		 */
		if (!name && !desc)
			name = desc = oafinfo->iid;

		if (!name)
			name = desc;

		if (!desc)
			desc = name;
		
                info = bonobo_directory_new_server_info (oafinfo->iid,
					   name,
					   desc);

                retval = g_list_prepend (retval, info);
        }

        CORBA_free (servers);
        
        return g_list_reverse (retval);
}


char *
bonobo_directory_find_for_file (const char  *fname,
				const char **required_ids,
				char       **error)
{
	char *query, *interfaces;
	const char *mime_type;
	char *iid;
	CORBA_Environment ev;
        OAF_ServerInfoList *servers;
	OAF_ServerInfo *oafinfo;

	if (!fname) {
		if (error)
			*error = g_strdup (_("No filename"));
		return NULL;
	}

	if (!(mime_type = gnome_mime_type ((char *) fname))) {
		if (error)
			*error = g_strdup_printf (_("unknown mime type for '%s'"), fname);
		return CORBA_OBJECT_NIL;
	}

	interfaces = build_id_query_fragment (required_ids);

	if (required_ids && required_ids [0] && interfaces)
		query = g_strdup_printf ("%s AND bonobo:supported_mime_types.has ('%s')",
					 interfaces, mime_type);
	else
		query = g_strdup_printf ("bonobo:supported_mime_types.has ('%s')",
					 mime_type);

	g_free (interfaces);

        CORBA_exception_init (&ev);

        servers = oaf_query (query, NULL, &ev);
        g_free (query);

        CORBA_exception_free (&ev);

        if (servers == CORBA_OBJECT_NIL || !servers->_buffer) {
		if (error)
			*error = g_strdup_printf (
				_("no handlers for mime type '%s'"), mime_type);
                return NULL;
	}

	/* Just return the first one */
	oafinfo = &servers->_buffer [0];
	iid = g_strdup (oafinfo->iid);

        CORBA_free (servers);

	if (error)
		*error = NULL;
        
        return iid;
}

CORBA_Object
od_server_activate_with_id (const gchar       *iid,
			    gint               flags,
			    CORBA_Environment *ev)
{
	CORBA_Environment *real_ev, tmp_ev;
	CORBA_Object retval;
	
	if (ev)
		real_ev = ev;
	else {
		CORBA_exception_init (&tmp_ev);
		real_ev = &tmp_ev;
	}
		
	retval = oaf_activate_from_id ((gchar *) iid, 0, NULL, real_ev);

	if (!ev)
		CORBA_exception_free (&tmp_ev);
	
	return retval;
}

ODRegistrationResult
bonobo_directory_register_server (CORBA_Object objref,
				  const gchar *iid)
{
        OAF_RegistrationResult result;
	ODRegistrationResult retval;

        result = oaf_active_server_register (iid, objref);

        switch (result) {
        case OAF_REG_SUCCESS:
                retval = OD_REG_SUCCESS;
                break;
                
        case OAF_REG_NOT_LISTED:
                retval = OD_REG_NOT_LISTED;
                break;

        case OAF_REG_ALREADY_ACTIVE:
                retval = OD_REG_ALREADY_ACTIVE;
                break;

        case OAF_REG_ERROR:
        default:
                retval = OD_REG_ERROR;
                break;
        }

	return retval;
}

ODRegistrationResult
bonobo_directory_unregister_server (CORBA_Object objref,
				    const gchar *iid)
{
        oaf_active_server_unregister (iid, objref);
        
        return OD_REG_SUCCESS;
}

CORBA_Object
bonobo_directory_get_name_service (CORBA_Environment *ev)
{
	return oaf_name_service_get (ev);
}