1 | /* |
---|
2 | * bonobo-exception.c: a generic exception -> user string converter. |
---|
3 | * |
---|
4 | * Authors: |
---|
5 | * Michael Meeks (michael@helixcode.com) |
---|
6 | * |
---|
7 | * Copyright 2000 Helix Code, Inc. |
---|
8 | */ |
---|
9 | #include <config.h> |
---|
10 | #include <glib.h> |
---|
11 | #include <libgnome/gnome-defs.h> |
---|
12 | #define GNOME_EXPLICIT_TRANSLATION_DOMAIN PACKAGE |
---|
13 | #include <libgnome/gnome-i18n.h> |
---|
14 | #include <bonobo/bonobo-exception.h> |
---|
15 | |
---|
16 | typedef enum { |
---|
17 | EXCEPTION_STR, |
---|
18 | EXCEPTION_FN |
---|
19 | } ExceptionType; |
---|
20 | |
---|
21 | typedef struct { |
---|
22 | ExceptionType type; |
---|
23 | char *repo_id; |
---|
24 | char *str; |
---|
25 | BonoboExceptionFn fn; |
---|
26 | gpointer user_data; |
---|
27 | GDestroyNotify destroy_fn; |
---|
28 | } ExceptionHandle; |
---|
29 | |
---|
30 | static GHashTable *bonobo_exceptions = NULL; |
---|
31 | |
---|
32 | static gboolean |
---|
33 | except_destroy (gpointer dummy, ExceptionHandle *e, gpointer dummy2) |
---|
34 | { |
---|
35 | if (e) { |
---|
36 | if (e->type == EXCEPTION_FN && |
---|
37 | e->destroy_fn) |
---|
38 | e->destroy_fn (e->user_data); |
---|
39 | e->destroy_fn = NULL; |
---|
40 | g_free (e->repo_id); |
---|
41 | g_free (e->str); |
---|
42 | g_free (e); |
---|
43 | } |
---|
44 | return TRUE; |
---|
45 | } |
---|
46 | |
---|
47 | static void |
---|
48 | bonobo_exception_shutdown (void) |
---|
49 | { |
---|
50 | g_hash_table_foreach_remove ( |
---|
51 | bonobo_exceptions, (GHRFunc) except_destroy, NULL); |
---|
52 | g_hash_table_destroy (bonobo_exceptions); |
---|
53 | bonobo_exceptions = NULL; |
---|
54 | } |
---|
55 | |
---|
56 | static GHashTable * |
---|
57 | get_hash (void) |
---|
58 | { |
---|
59 | if (!bonobo_exceptions) { |
---|
60 | bonobo_exceptions = g_hash_table_new ( |
---|
61 | g_str_hash, g_str_equal); |
---|
62 | g_atexit (bonobo_exception_shutdown); |
---|
63 | } |
---|
64 | |
---|
65 | return bonobo_exceptions; |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * bonobo_exception_add_handler_str: |
---|
70 | * @repo_id: exception repository id |
---|
71 | * @str: the user readable, translated exception text. |
---|
72 | * |
---|
73 | * This routine adds a simple string mapping for an exception |
---|
74 | * with repository id @repo_id, such that when we call |
---|
75 | * bonobo_exception_get_text on an exception of id @repo_id we |
---|
76 | * get @str back. |
---|
77 | **/ |
---|
78 | void |
---|
79 | bonobo_exception_add_handler_str (const char *repo_id, const char *str) |
---|
80 | { |
---|
81 | ExceptionHandle *e; |
---|
82 | GHashTable *hash; |
---|
83 | |
---|
84 | g_return_if_fail (str != NULL); |
---|
85 | g_return_if_fail (repo_id != NULL); |
---|
86 | |
---|
87 | hash = get_hash (); |
---|
88 | |
---|
89 | e = g_new0 (ExceptionHandle, 1); |
---|
90 | |
---|
91 | e->type = EXCEPTION_STR; |
---|
92 | e->repo_id = g_strdup (repo_id); |
---|
93 | e->str = g_strdup (str); |
---|
94 | |
---|
95 | g_hash_table_insert (hash, e->repo_id, e); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * bonobo_exception_add_handler_fn: |
---|
100 | * @repo_id: exception repository id |
---|
101 | * @fn: function to make exception human readable |
---|
102 | * @user_data: the user data |
---|
103 | * @destroy_fn: user data destroy function or NULL. |
---|
104 | * |
---|
105 | * This routine adds a method mapping for an exception |
---|
106 | * with repository id @repo_id, such that when we call |
---|
107 | * bonobo_exception_get_text on an exception of id @repo_id |
---|
108 | * the @fn is called and passed @user_data. |
---|
109 | * When the handler is removed the @destroy_fn is called |
---|
110 | * on its @user_data. |
---|
111 | **/ |
---|
112 | void |
---|
113 | bonobo_exception_add_handler_fn (const char *repo_id, |
---|
114 | BonoboExceptionFn fn, |
---|
115 | gpointer user_data, |
---|
116 | GDestroyNotify destroy_fn) |
---|
117 | { |
---|
118 | ExceptionHandle *e; |
---|
119 | GHashTable *hash; |
---|
120 | |
---|
121 | g_return_if_fail (fn != NULL); |
---|
122 | g_return_if_fail (repo_id != NULL); |
---|
123 | |
---|
124 | hash = get_hash (); |
---|
125 | |
---|
126 | e = g_new0 (ExceptionHandle, 1); |
---|
127 | |
---|
128 | e->type = EXCEPTION_STR; |
---|
129 | e->repo_id = g_strdup (repo_id); |
---|
130 | e->fn = fn; |
---|
131 | e->user_data = user_data; |
---|
132 | e->destroy_fn = destroy_fn; |
---|
133 | |
---|
134 | g_hash_table_insert (hash, e->repo_id, e); |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * bonobo_exception_repoid_to_text: |
---|
139 | * @repo_id: exception repository id |
---|
140 | * |
---|
141 | * This maps builtin bonobo exceptions that the system |
---|
142 | * knows about to user readable strings. |
---|
143 | * |
---|
144 | * Return value: a user string or NULL for an unknown repo_id |
---|
145 | **/ |
---|
146 | char * |
---|
147 | bonobo_exception_repoid_to_text (const char *repo_id) |
---|
148 | { |
---|
149 | /* Oaf */ |
---|
150 | /* if (!strcmp (repo_id, "IDL:OAF/GeneralError:1.0")) { |
---|
151 | OAF_GeneralError *err = ev->_params; |
---|
152 | |
---|
153 | if (!err || !err->description) |
---|
154 | return g_strdup (_("General oaf error with no description")); |
---|
155 | else |
---|
156 | return g_strdup (err->description); |
---|
157 | |
---|
158 | }*/ |
---|
159 | |
---|
160 | /* Bonobo */ |
---|
161 | if (!strcmp (repo_id, ex_Bonobo_NotSupported)) |
---|
162 | return g_strdup (_("An unsupported action was attempted")); |
---|
163 | |
---|
164 | else if (!strcmp (repo_id, ex_Bonobo_IOError)) |
---|
165 | return g_strdup (_("IO Error")); |
---|
166 | |
---|
167 | else if (!strcmp (repo_id, ex_Bonobo_BadArg)) |
---|
168 | return g_strdup (_("Invalid argument value")); |
---|
169 | |
---|
170 | /* Bonobo::ItemContainer */ |
---|
171 | else if (!strcmp (repo_id, ex_Bonobo_ItemContainer_NotFound)) |
---|
172 | return g_strdup (_("Object not found")); |
---|
173 | |
---|
174 | else if (!strcmp (repo_id, ex_Bonobo_ItemContainer_SyntaxError)) |
---|
175 | return g_strdup (_("Syntax error in object description")); |
---|
176 | |
---|
177 | /* Bonobo::Embeddable */ |
---|
178 | else if (!strcmp (repo_id, ex_Bonobo_Embeddable_UserCancelledSave)) |
---|
179 | return g_strdup (_("The User canceled the save")); |
---|
180 | |
---|
181 | #if 0 |
---|
182 | /* Bonobo::GenericFactory */ |
---|
183 | else if (!strcmp (repo_id, ex_GNOME_ObjectFactory_CannotActivate)) |
---|
184 | return g_strdup (_("Cannot activate object from factory")); |
---|
185 | #endif |
---|
186 | |
---|
187 | /* Bonobo::Stream */ |
---|
188 | else if (!strcmp (repo_id, ex_Bonobo_Stream_NoPermission)) |
---|
189 | return g_strdup (_("No permission to access stream")); |
---|
190 | |
---|
191 | else if (!strcmp (repo_id, ex_Bonobo_Stream_NotSupported)) |
---|
192 | return g_strdup (_("An unsupported stream action was attempted")); |
---|
193 | |
---|
194 | else if (!strcmp (repo_id, ex_Bonobo_Stream_IOError)) |
---|
195 | return g_strdup (_("IO Error on stream")); |
---|
196 | |
---|
197 | /* Bonobo::Storage */ |
---|
198 | else if (!strcmp (repo_id, ex_Bonobo_Storage_IOError)) |
---|
199 | return g_strdup (_("IO Error on storage")); |
---|
200 | |
---|
201 | else if (!strcmp (repo_id, ex_Bonobo_Storage_NameExists)) |
---|
202 | return g_strdup (_("Name already exists in storage")); |
---|
203 | |
---|
204 | else if (!strcmp (repo_id, ex_Bonobo_Storage_NotFound)) |
---|
205 | return g_strdup (_("Object not found in storage")); |
---|
206 | |
---|
207 | else if (!strcmp (repo_id, ex_Bonobo_Storage_NoPermission)) |
---|
208 | return g_strdup (_("No permission to do operation on storage")); |
---|
209 | else if (!strcmp (repo_id, ex_Bonobo_Storage_NotSupported)) |
---|
210 | return g_strdup (_("An unsupported storage action was attempted")); |
---|
211 | else if (!strcmp (repo_id, ex_Bonobo_Storage_NotStream)) |
---|
212 | return g_strdup (_("Object is not a stream")); |
---|
213 | |
---|
214 | else if (!strcmp (repo_id, ex_Bonobo_Storage_NotStorage)) |
---|
215 | return g_strdup (_("Object is not a storage")); |
---|
216 | |
---|
217 | else if (!strcmp (repo_id, ex_Bonobo_Storage_NotEmpty)) |
---|
218 | return g_strdup (_("Storage is not empty")); |
---|
219 | |
---|
220 | /* Bonobo::UIContainer */ |
---|
221 | else if (!strcmp (repo_id, ex_Bonobo_UIContainer_MalFormedXML)) |
---|
222 | return g_strdup (_("malformed user interface XML description")); |
---|
223 | |
---|
224 | else if (!strcmp (repo_id, ex_Bonobo_UIContainer_InvalidPath)) |
---|
225 | return g_strdup (_("invalid path to XML user interface element")); |
---|
226 | |
---|
227 | /* Bonobo::Persist */ |
---|
228 | else if (!strcmp (repo_id, ex_Bonobo_Persist_WrongDataType)) |
---|
229 | return g_strdup (_("incorrect data type")); |
---|
230 | |
---|
231 | else if (!strcmp (repo_id, ex_Bonobo_Persist_FileNotFound)) |
---|
232 | return g_strdup (_("stream not found")); |
---|
233 | |
---|
234 | /* Bonobo::PropertyBag */ |
---|
235 | else if (!strcmp (repo_id, ex_Bonobo_PropertyBag_NotFound)) |
---|
236 | return g_strdup (_("property not found")); |
---|
237 | |
---|
238 | /* Bonobo::Moniker */ |
---|
239 | else if (!strcmp (repo_id, ex_Bonobo_Moniker_InterfaceNotFound)) |
---|
240 | return g_strdup (_("Moniker interface cannot be found")); |
---|
241 | |
---|
242 | else if (!strcmp (repo_id, ex_Bonobo_Moniker_TimeOut)) |
---|
243 | return g_strdup (_("Moniker activation timed out")); |
---|
244 | |
---|
245 | else if (!strcmp (repo_id, ex_Bonobo_Moniker_InvalidSyntax)) |
---|
246 | return g_strdup (_("Syntax error within moniker")); |
---|
247 | |
---|
248 | else if (!strcmp (repo_id, ex_Bonobo_Moniker_UnknownPrefix)) |
---|
249 | return g_strdup (_("Moniker has an unknown moniker prefix")); |
---|
250 | |
---|
251 | else |
---|
252 | return NULL; |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * bonobo_exception_get_text: |
---|
257 | * @ev: the corba environment. |
---|
258 | * |
---|
259 | * Returns a user readable description of the exception. First |
---|
260 | * checks @ev against builtin Bonobo exceptions, then falls back to |
---|
261 | * exception names added through bonobo_exception_add_handler_str |
---|
262 | * or bonobo_exception_add_handler_fn. |
---|
263 | * |
---|
264 | * Return value: A g_malloc'd description, which the caller must free. |
---|
265 | * NULL is never returned. |
---|
266 | **/ |
---|
267 | char * |
---|
268 | bonobo_exception_get_text (CORBA_Environment *ev) |
---|
269 | { |
---|
270 | char *rval; |
---|
271 | |
---|
272 | if (!ev || !BONOBO_EX (ev)) |
---|
273 | return g_strdup (_("Error checking error; no exception")); |
---|
274 | |
---|
275 | if ((rval = bonobo_exception_repoid_to_text (ev->_repo_id))) |
---|
276 | return rval; |
---|
277 | else { |
---|
278 | ExceptionHandle *e; |
---|
279 | GHashTable *hash = get_hash (); |
---|
280 | char *str = NULL; |
---|
281 | |
---|
282 | if ((e = g_hash_table_lookup (hash, ev->_repo_id))) { |
---|
283 | if (e->type == EXCEPTION_STR) |
---|
284 | str = g_strdup (e->str); |
---|
285 | else |
---|
286 | str = e->fn (ev, e->user_data); |
---|
287 | } |
---|
288 | |
---|
289 | if (str) |
---|
290 | return str; |
---|
291 | else |
---|
292 | return g_strdup_printf ( |
---|
293 | "Unknown CORBA exception id: '%s'", |
---|
294 | ev->_repo_id); |
---|
295 | } |
---|
296 | } |
---|