File: bonobo-exception.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 (296 lines) | stat: -rw-r--r-- 8,117 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
/*
 * bonobo-exception.c: a generic exception -> user string converter.
 *
 * Authors:
 *   Michael Meeks (michael@helixcode.com)
 *
 * Copyright 2000 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 <bonobo/bonobo-exception.h>

typedef enum {
	EXCEPTION_STR,
	EXCEPTION_FN
} ExceptionType;

typedef struct {
	ExceptionType     type;
	char             *repo_id;
	char             *str;
	BonoboExceptionFn fn;
	gpointer          user_data;
	GDestroyNotify    destroy_fn;
} ExceptionHandle;

static GHashTable *bonobo_exceptions = NULL;

static gboolean
except_destroy (gpointer dummy, ExceptionHandle *e, gpointer dummy2)
{
	if (e) {
		if (e->type == EXCEPTION_FN &&
		    e->destroy_fn)
			e->destroy_fn (e->user_data);
		e->destroy_fn = NULL;
		g_free (e->repo_id);
		g_free (e->str);
		g_free (e);
	}
	return TRUE;
}

static void
bonobo_exception_shutdown (void)
{
	g_hash_table_foreach_remove (
		bonobo_exceptions, (GHRFunc) except_destroy, NULL);
	g_hash_table_destroy (bonobo_exceptions);
	bonobo_exceptions = NULL;
}

static GHashTable *
get_hash (void)
{
	if (!bonobo_exceptions) {
		bonobo_exceptions = g_hash_table_new (
			g_str_hash, g_str_equal);
		g_atexit (bonobo_exception_shutdown);
	}

	return bonobo_exceptions;
}

/**
 * bonobo_exception_add_handler_str:
 * @repo_id: exception repository id
 * @str: the user readable, translated exception text.
 * 
 * This routine adds a simple string mapping for an exception
 * with repository id @repo_id, such that when we call
 * bonobo_exception_get_text on an exception of id @repo_id we
 * get @str back.
 **/
void
bonobo_exception_add_handler_str (const char *repo_id, const char *str)
{
	ExceptionHandle *e;
	GHashTable *hash;

	g_return_if_fail (str != NULL);
	g_return_if_fail (repo_id != NULL);

	hash = get_hash ();

	e = g_new0 (ExceptionHandle, 1);

	e->type = EXCEPTION_STR;
	e->repo_id = g_strdup (repo_id);
	e->str = g_strdup (str);

	g_hash_table_insert (hash, e->repo_id, e);
}

/**
 * bonobo_exception_add_handler_fn:
 * @repo_id: exception repository id
 * @fn: function to make exception human readable
 * @user_data: the user data
 * @destroy_fn: user data destroy function or NULL.
 * 
 * This routine adds a method mapping for an exception
 * with repository id @repo_id, such that when we call
 * bonobo_exception_get_text on an exception of id @repo_id
 * the @fn is called and passed @user_data.
 * When the handler is removed the @destroy_fn is called
 * on its @user_data.
 **/
void
bonobo_exception_add_handler_fn (const char *repo_id,
				 BonoboExceptionFn fn,
				 gpointer          user_data,
				 GDestroyNotify    destroy_fn)
{
	ExceptionHandle *e;
	GHashTable *hash;

	g_return_if_fail (fn != NULL);
	g_return_if_fail (repo_id != NULL);

	hash = get_hash ();

	e = g_new0 (ExceptionHandle, 1);

	e->type = EXCEPTION_STR;
	e->repo_id = g_strdup (repo_id);
	e->fn = fn;
	e->user_data = user_data;
	e->destroy_fn = destroy_fn;

	g_hash_table_insert (hash, e->repo_id, e);
}

/**
 * bonobo_exception_repoid_to_text:
 * @repo_id: exception repository id
 * 
 *  This maps builtin bonobo exceptions that the system
 * knows about to user readable strings.
 * 
 * Return value: a user string or NULL for an unknown repo_id
 **/
char *
bonobo_exception_repoid_to_text  (const char *repo_id)
{
	/* Oaf */
/*	if (!strcmp (repo_id, "IDL:OAF/GeneralError:1.0")) {
		OAF_GeneralError *err = ev->_params;
		
		if (!err || !err->description)
			return g_strdup (_("General oaf error with no description"));
		else
			return g_strdup (err->description);

			}*/

	/* Bonobo */ 
	if (!strcmp (repo_id, ex_Bonobo_NotSupported))
		return g_strdup (_("An unsupported action was attempted"));
	
	else if (!strcmp (repo_id, ex_Bonobo_IOError))
		return g_strdup (_("IO Error"));
	
	else if (!strcmp (repo_id, ex_Bonobo_BadArg))
		return g_strdup (_("Invalid argument value"));
	
	/* Bonobo::ItemContainer */
	else if (!strcmp (repo_id, ex_Bonobo_ItemContainer_NotFound))
		return g_strdup (_("Object not found"));

	else if (!strcmp (repo_id, ex_Bonobo_ItemContainer_SyntaxError))
		return g_strdup (_("Syntax error in object description"));

	/* Bonobo::Embeddable */
	else if (!strcmp (repo_id, ex_Bonobo_Embeddable_UserCancelledSave))
		return g_strdup (_("The User canceled the save"));

#if 0
	/* Bonobo::GenericFactory */
	else if (!strcmp (repo_id, ex_GNOME_ObjectFactory_CannotActivate))
		return g_strdup (_("Cannot activate object from factory"));
#endif

	/* Bonobo::Stream */
	else if (!strcmp (repo_id, ex_Bonobo_Stream_NoPermission))
		return g_strdup (_("No permission to access stream"));

	else if (!strcmp (repo_id, ex_Bonobo_Stream_NotSupported))
		return g_strdup (_("An unsupported stream action was attempted"));
	
	else if (!strcmp (repo_id, ex_Bonobo_Stream_IOError))
		return g_strdup (_("IO Error on stream"));

	/* Bonobo::Storage */
	else if (!strcmp (repo_id, ex_Bonobo_Storage_IOError))
		return g_strdup (_("IO Error on storage"));

	else if (!strcmp (repo_id, ex_Bonobo_Storage_NameExists))
		return g_strdup (_("Name already exists in storage"));

	else if (!strcmp (repo_id, ex_Bonobo_Storage_NotFound))
		return g_strdup (_("Object not found in storage"));

	else if (!strcmp (repo_id, ex_Bonobo_Storage_NoPermission))
		return g_strdup (_("No permission to do operation on storage"));
	else if (!strcmp (repo_id, ex_Bonobo_Storage_NotSupported))
		return g_strdup (_("An unsupported storage action was attempted"));
	else if (!strcmp (repo_id, ex_Bonobo_Storage_NotStream))
		return g_strdup (_("Object is not a stream"));

	else if (!strcmp (repo_id, ex_Bonobo_Storage_NotStorage))
		return g_strdup (_("Object is not a storage"));

	else if (!strcmp (repo_id, ex_Bonobo_Storage_NotEmpty))
		return g_strdup (_("Storage is not empty"));

	/* Bonobo::UIContainer */
	else if (!strcmp (repo_id, ex_Bonobo_UIContainer_MalFormedXML))
		return g_strdup (_("malformed user interface XML description"));

	else if (!strcmp (repo_id, ex_Bonobo_UIContainer_InvalidPath))
		return g_strdup (_("invalid path to XML user interface element"));
		
	/* Bonobo::Persist */
	else if (!strcmp (repo_id, ex_Bonobo_Persist_WrongDataType))
		return g_strdup (_("incorrect data type"));

	else if (!strcmp (repo_id, ex_Bonobo_Persist_FileNotFound))
		return g_strdup (_("stream not found"));

	/* Bonobo::PropertyBag */
	else if (!strcmp (repo_id, ex_Bonobo_PropertyBag_NotFound))
		return g_strdup (_("property not found"));

	/* Bonobo::Moniker */
	else if (!strcmp (repo_id, ex_Bonobo_Moniker_InterfaceNotFound))
		return g_strdup (_("Moniker interface cannot be found"));

	else if (!strcmp (repo_id, ex_Bonobo_Moniker_TimeOut))
		return g_strdup (_("Moniker activation timed out"));
		
	else if (!strcmp (repo_id, ex_Bonobo_Moniker_InvalidSyntax))
		return g_strdup (_("Syntax error within moniker"));

	else if (!strcmp (repo_id, ex_Bonobo_Moniker_UnknownPrefix))
		return g_strdup (_("Moniker has an unknown moniker prefix"));

	else
		return NULL;
}

/**
 * bonobo_exception_get_text:
 * @ev: the corba environment.
 * 
 * Returns a user readable description of the exception.  First
 * checks @ev against builtin Bonobo exceptions, then falls back to
 * exception names added through bonobo_exception_add_handler_str
 * or bonobo_exception_add_handler_fn.
 * 
 * Return value: A g_malloc'd description, which the caller must free.
 * NULL is never returned.
 **/
char *
bonobo_exception_get_text (CORBA_Environment *ev)
{
	char *rval;

	if (!ev || !BONOBO_EX (ev))
		return g_strdup (_("Error checking error; no exception"));

	if ((rval = bonobo_exception_repoid_to_text (ev->_repo_id)))
		return rval;
	else {
		ExceptionHandle *e;
		GHashTable *hash = get_hash ();
		char *str = NULL;
		
		if ((e = g_hash_table_lookup (hash, ev->_repo_id))) {
			if (e->type == EXCEPTION_STR)
				str = g_strdup (e->str);
			else
				str = e->fn (ev, e->user_data);
		}

		if (str)
			return str;
		else
			return g_strdup_printf (
				"Unknown CORBA exception id: '%s'", 
				ev->_repo_id);
	}
}