1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /** |
---|
3 | * bonobo-item-handler.c: a generic Item Container resolver (implements ItemContainer) |
---|
4 | * |
---|
5 | * Author: |
---|
6 | * Miguel de Icaza (miguel@kernel.org) |
---|
7 | * |
---|
8 | * Copyright 2000 Miguel de Icaza. |
---|
9 | */ |
---|
10 | #include <config.h> |
---|
11 | #include <gtk/gtksignal.h> |
---|
12 | #include <gtk/gtkmarshal.h> |
---|
13 | #include <gtk/gtkwidget.h> |
---|
14 | #include <bonobo/bonobo-main.h> |
---|
15 | #include <bonobo/bonobo-object.h> |
---|
16 | #include <bonobo/bonobo-exception.h> |
---|
17 | #include "bonobo-item-handler.h" |
---|
18 | |
---|
19 | #define PARENT_TYPE BONOBO_X_OBJECT_TYPE |
---|
20 | |
---|
21 | /* |
---|
22 | * Returns a list of the objects in this container |
---|
23 | */ |
---|
24 | static Bonobo_ItemContainer_ObjectNames * |
---|
25 | impl_enum_objects (PortableServer_Servant servant, CORBA_Environment *ev) |
---|
26 | { |
---|
27 | BonoboObject *object = bonobo_object_from_servant (servant); |
---|
28 | BonoboItemHandler *handler = BONOBO_ITEM_HANDLER (object); |
---|
29 | |
---|
30 | if (handler->enum_objects) |
---|
31 | return handler->enum_objects (handler, handler->user_data, ev); |
---|
32 | else |
---|
33 | return Bonobo_ItemContainer_ObjectNames__alloc (); |
---|
34 | } |
---|
35 | |
---|
36 | static Bonobo_Unknown |
---|
37 | impl_get_object (PortableServer_Servant servant, |
---|
38 | const CORBA_char *item_name, |
---|
39 | CORBA_boolean only_if_exists, |
---|
40 | CORBA_Environment *ev) |
---|
41 | { |
---|
42 | BonoboObject *object = bonobo_object_from_servant (servant); |
---|
43 | BonoboItemHandler *handler = BONOBO_ITEM_HANDLER (object); |
---|
44 | |
---|
45 | if (handler->get_object) |
---|
46 | return handler->get_object (handler, item_name, |
---|
47 | only_if_exists, |
---|
48 | handler->user_data, ev); |
---|
49 | else |
---|
50 | return CORBA_OBJECT_NIL; |
---|
51 | } |
---|
52 | |
---|
53 | static void |
---|
54 | bonobo_item_handler_class_init (BonoboItemHandlerClass *klass) |
---|
55 | { |
---|
56 | POA_Bonobo_ItemContainer__epv *epv = &klass->epv; |
---|
57 | |
---|
58 | epv->enumObjects = impl_enum_objects; |
---|
59 | epv->getObjectByName = impl_get_object; |
---|
60 | } |
---|
61 | |
---|
62 | static void |
---|
63 | bonobo_item_handler_init (GtkObject *object) |
---|
64 | { |
---|
65 | /* nothing to do */ |
---|
66 | } |
---|
67 | |
---|
68 | BONOBO_X_TYPE_FUNC_FULL (BonoboItemHandler, |
---|
69 | Bonobo_ItemContainer, |
---|
70 | PARENT_TYPE, |
---|
71 | bonobo_item_handler); |
---|
72 | |
---|
73 | /** |
---|
74 | * bonobo_item_handler_construct: |
---|
75 | * @container: The handler object to construct |
---|
76 | * @corba_container: The CORBA object that implements Bonobo::ItemContainer |
---|
77 | * |
---|
78 | * Constructs the @container Gtk object using the provided CORBA |
---|
79 | * object. |
---|
80 | * |
---|
81 | * Returns: The constructed BonoboItemContainer object. |
---|
82 | */ |
---|
83 | BonoboItemHandler * |
---|
84 | bonobo_item_handler_construct (BonoboItemHandler *handler, |
---|
85 | BonoboItemHandlerEnumObjectsFn enum_objects, |
---|
86 | BonoboItemHandlerGetObjectFn get_object, |
---|
87 | gpointer user_data) |
---|
88 | { |
---|
89 | g_return_val_if_fail (handler != NULL, NULL); |
---|
90 | g_return_val_if_fail (BONOBO_IS_ITEM_HANDLER (handler), NULL); |
---|
91 | |
---|
92 | handler->get_object = get_object; |
---|
93 | handler->enum_objects = enum_objects; |
---|
94 | handler->user_data = user_data; |
---|
95 | |
---|
96 | return handler; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * bonobo_item_handler_new: |
---|
101 | * |
---|
102 | * Creates a new BonoboItemHandler object. These are used to hold |
---|
103 | * client sites. |
---|
104 | * |
---|
105 | * Returns: The newly created BonoboItemHandler object |
---|
106 | */ |
---|
107 | BonoboItemHandler * |
---|
108 | bonobo_item_handler_new (BonoboItemHandlerEnumObjectsFn enum_objects, |
---|
109 | BonoboItemHandlerGetObjectFn get_object, |
---|
110 | gpointer user_data) |
---|
111 | |
---|
112 | { |
---|
113 | BonoboItemHandler *handler; |
---|
114 | |
---|
115 | handler = gtk_type_new (bonobo_item_handler_get_type ()); |
---|
116 | |
---|
117 | return bonobo_item_handler_construct ( |
---|
118 | handler, enum_objects, get_object, user_data); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * bonobo_parse_item_options: |
---|
123 | * @option_string: a string with a list of options |
---|
124 | * |
---|
125 | * The bonobo_parse_item_options() routine parses the |
---|
126 | * @option_string which is a semi-colon separated list |
---|
127 | * of arguments. |
---|
128 | * |
---|
129 | * Each argument is of the form value[=key]. The entire |
---|
130 | * option string is defined by: |
---|
131 | * |
---|
132 | * option_string := keydef |
---|
133 | * | keydef ; option_string |
---|
134 | * |
---|
135 | * keydef := value [=key] |
---|
136 | * |
---|
137 | * The key can be literal values, values with spaces, and the |
---|
138 | * \ character is used as an escape sequence. To include a |
---|
139 | * literal ";" in a value you can use \;. |
---|
140 | * |
---|
141 | * Returns: A GSList that contains structures of type BonoboItemOption |
---|
142 | * each BonoboItemOption |
---|
143 | */ |
---|
144 | GSList * |
---|
145 | bonobo_item_option_parse (const char *option_string) |
---|
146 | { |
---|
147 | GSList *list = NULL; |
---|
148 | GString *key = NULL; |
---|
149 | BonoboItemOption *option = NULL; |
---|
150 | const char *p; |
---|
151 | |
---|
152 | for (p = option_string; *p; p++) { |
---|
153 | if (*p == '=' ) { |
---|
154 | GString *value = NULL; |
---|
155 | if (!key) |
---|
156 | return list; |
---|
157 | |
---|
158 | option = g_new0 (BonoboItemOption, 1); |
---|
159 | option->key = key->str; |
---|
160 | g_string_free (key, FALSE); |
---|
161 | key = NULL; |
---|
162 | |
---|
163 | for (p++; *p; p++) { |
---|
164 | if (*p == ';') |
---|
165 | goto next; |
---|
166 | if (!value) |
---|
167 | value = g_string_new (""); |
---|
168 | |
---|
169 | if (*p == '\\') { |
---|
170 | p++; |
---|
171 | if (*p == 0) |
---|
172 | break; |
---|
173 | g_string_append_c (value, *p); |
---|
174 | continue; |
---|
175 | } |
---|
176 | g_string_append_c (value, *p); |
---|
177 | } |
---|
178 | next: |
---|
179 | if (value) { |
---|
180 | option->value = value->str; |
---|
181 | g_string_free (value, FALSE); |
---|
182 | } |
---|
183 | list = g_slist_append (list, option); |
---|
184 | if (*p == 0) |
---|
185 | break; |
---|
186 | } else { |
---|
187 | if (key == NULL) |
---|
188 | key = g_string_new (""); |
---|
189 | g_string_append_c (key, *p); |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | if (key) { |
---|
194 | BonoboItemOption *option = g_new (BonoboItemOption, 1); |
---|
195 | |
---|
196 | option->key = key->str; |
---|
197 | g_string_free (key, FALSE); |
---|
198 | |
---|
199 | list = g_slist_append (list, option); |
---|
200 | } |
---|
201 | |
---|
202 | return list; |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * bonobo_item_options_free: |
---|
207 | * @options: a GSList of BonoboItemOption structures that was returned by bonobo_item_option_parse() |
---|
208 | * |
---|
209 | * Use this to release a list returned by bonobo_item_option_parse() |
---|
210 | */ |
---|
211 | void |
---|
212 | bonobo_item_options_free (GSList *options) |
---|
213 | { |
---|
214 | GSList *l; |
---|
215 | |
---|
216 | for (l = options; l; l = l->next) { |
---|
217 | BonoboItemOption *option = l->data; |
---|
218 | |
---|
219 | g_free (option->key); |
---|
220 | if (option->value) |
---|
221 | g_free (option->value); |
---|
222 | } |
---|
223 | |
---|
224 | g_slist_free (options); |
---|
225 | } |
---|