1 | /* |
---|
2 | * Author: |
---|
3 | * Martin Baulig <baulig@suse.de> |
---|
4 | * |
---|
5 | * Copyright 2000 SuSE GmbH. |
---|
6 | */ |
---|
7 | |
---|
8 | #include <config.h> |
---|
9 | #include <fcntl.h> |
---|
10 | #include "gshell.h" |
---|
11 | #include "controls.h" |
---|
12 | |
---|
13 | #if USING_OAF |
---|
14 | #include <liboaf/liboaf.h> |
---|
15 | #else |
---|
16 | #include <libgnorba/gnorba.h> |
---|
17 | #endif |
---|
18 | |
---|
19 | /* |
---|
20 | * BonoboObject data |
---|
21 | */ |
---|
22 | typedef struct { |
---|
23 | BonoboEmbeddable *bonobo_object; |
---|
24 | gchar *filename; |
---|
25 | gchar *goad_id; |
---|
26 | GList *view_list; |
---|
27 | } bonobo_object_data_t; |
---|
28 | |
---|
29 | /* |
---|
30 | * View data |
---|
31 | */ |
---|
32 | typedef struct { |
---|
33 | bonobo_object_data_t *bod; |
---|
34 | BonoboObjectClient *server; |
---|
35 | BonoboControlFrame *control_frame; |
---|
36 | GtkWidget *root; |
---|
37 | BonoboView *view; |
---|
38 | } view_data_t; |
---|
39 | |
---|
40 | static void |
---|
41 | destroy_view (BonoboView *view, view_data_t *view_data) |
---|
42 | { |
---|
43 | g_return_if_fail (view_data != NULL); |
---|
44 | |
---|
45 | /* this is not strictly necessary, but it gives us a little "protection" |
---|
46 | * if we or our component are leaking a reference somewhere. |
---|
47 | */ |
---|
48 | bonobo_control_frame_control_deactivate (view_data->control_frame); |
---|
49 | |
---|
50 | /* we keep a reference to this since we bonobo_object_activate()d it. */ |
---|
51 | bonobo_object_client_unref (view_data->server, NULL); |
---|
52 | |
---|
53 | /* the ControlFrame holds the second reference. */ |
---|
54 | bonobo_object_unref (BONOBO_OBJECT (view_data->control_frame)); |
---|
55 | gtk_widget_destroy (view_data->root); |
---|
56 | |
---|
57 | view_data->bod->view_list = g_list_remove (view_data->bod->view_list, view_data); |
---|
58 | |
---|
59 | g_free (view_data); |
---|
60 | } |
---|
61 | |
---|
62 | static void |
---|
63 | view_activate_cb (BonoboView *view, gboolean activate, gpointer data) |
---|
64 | { |
---|
65 | /* |
---|
66 | * Notify the ViewFrame that we accept to be activated or |
---|
67 | * deactivated (we are an acquiescent BonoboView, yes we are). |
---|
68 | */ |
---|
69 | bonobo_view_activate_notify (view, activate); |
---|
70 | } |
---|
71 | |
---|
72 | static gint |
---|
73 | do_load_pf (view_data_t *view_data, const gchar *filename) |
---|
74 | { |
---|
75 | Bonobo_PersistFile persist; |
---|
76 | CORBA_Environment ev; |
---|
77 | gint retval = 0; |
---|
78 | |
---|
79 | CORBA_exception_init (&ev); |
---|
80 | |
---|
81 | persist = Bonobo_Unknown_queryInterface ( |
---|
82 | BONOBO_OBJREF (view_data->server), |
---|
83 | "IDL:Bonobo/PersistFile:1.0", |
---|
84 | &ev); |
---|
85 | |
---|
86 | if (ev._major != CORBA_NO_EXCEPTION || persist == CORBA_OBJECT_NIL) { |
---|
87 | g_warning ("component doesn't support PersistFile!"); |
---|
88 | CORBA_exception_free (&ev); |
---|
89 | return -1; |
---|
90 | } |
---|
91 | |
---|
92 | Bonobo_PersistFile_load (persist, (CORBA_char *) filename, &ev); |
---|
93 | |
---|
94 | if (ev._major != CORBA_NO_EXCEPTION) { |
---|
95 | gnome_warning_dialog (_("An exception occured while trying " |
---|
96 | "to load data into the component with " |
---|
97 | "PersistFile")); |
---|
98 | retval = -1; |
---|
99 | } |
---|
100 | if (ev._major != CORBA_SYSTEM_EXCEPTION) |
---|
101 | CORBA_Object_release (persist, &ev); |
---|
102 | |
---|
103 | Bonobo_Unknown_unref (persist, &ev); |
---|
104 | CORBA_exception_free (&ev); |
---|
105 | |
---|
106 | return retval; |
---|
107 | } |
---|
108 | |
---|
109 | static gint |
---|
110 | do_load_ps (view_data_t *view_data, const gchar *filename) |
---|
111 | { |
---|
112 | Bonobo_PersistStream persist; |
---|
113 | CORBA_Environment ev; |
---|
114 | BonoboStream *stream; |
---|
115 | gint retval = 0; |
---|
116 | |
---|
117 | if (!(stream = bonobo_stream_open ("fs", filename, Bonobo_Storage_READ, O_RDONLY))) { |
---|
118 | char *error_msg; |
---|
119 | error_msg = g_strdup_printf (_("Could not open file %s"), |
---|
120 | filename); |
---|
121 | gnome_warning_dialog (error_msg); |
---|
122 | g_free (error_msg); |
---|
123 | return -1; |
---|
124 | } |
---|
125 | |
---|
126 | CORBA_exception_init (&ev); |
---|
127 | |
---|
128 | persist = Bonobo_Unknown_queryInterface ( |
---|
129 | BONOBO_OBJREF (view_data->server), |
---|
130 | "IDL:Bonobo/PersistStream:1.0", |
---|
131 | &ev); |
---|
132 | |
---|
133 | if (ev._major != CORBA_NO_EXCEPTION || persist == CORBA_OBJECT_NIL) { |
---|
134 | g_warning ("component doesn't support PersistStream!"); |
---|
135 | CORBA_exception_free (&ev); |
---|
136 | return -1; |
---|
137 | } |
---|
138 | |
---|
139 | Bonobo_PersistStream_load (persist, |
---|
140 | (Bonobo_Stream) BONOBO_OBJREF (stream), |
---|
141 | "", &ev); |
---|
142 | |
---|
143 | if (ev._major != CORBA_NO_EXCEPTION) { |
---|
144 | gnome_warning_dialog (_("An exception occured while trying " |
---|
145 | "to load data into the component with " |
---|
146 | "PersistStream")); |
---|
147 | retval = -1; |
---|
148 | } |
---|
149 | if (ev._major != CORBA_SYSTEM_EXCEPTION) |
---|
150 | CORBA_Object_release (persist, &ev); |
---|
151 | |
---|
152 | Bonobo_Unknown_unref (persist, &ev); |
---|
153 | CORBA_exception_free (&ev); |
---|
154 | bonobo_object_unref (BONOBO_OBJECT(stream)); |
---|
155 | |
---|
156 | return retval; |
---|
157 | } |
---|
158 | |
---|
159 | static gint |
---|
160 | do_load_file (view_data_t *view_data, const gchar *filename) |
---|
161 | { |
---|
162 | gint retval = 0; |
---|
163 | |
---|
164 | if (bonobo_object_client_has_interface (view_data->server, |
---|
165 | "IDL:Bonobo/PersistFile:1.0", |
---|
166 | NULL)) { |
---|
167 | retval = do_load_pf (view_data, filename); |
---|
168 | } else if (bonobo_object_client_has_interface (view_data->server, |
---|
169 | "IDL:Bonobo/PersistStream:1.0", |
---|
170 | NULL)) { |
---|
171 | retval = do_load_ps (view_data, filename); |
---|
172 | } else { |
---|
173 | g_warning ("no storage interface found"); |
---|
174 | retval = -1; |
---|
175 | } |
---|
176 | |
---|
177 | return retval; |
---|
178 | } |
---|
179 | |
---|
180 | static BonoboView * |
---|
181 | view_factory (BonoboEmbeddable *bonobo_object, |
---|
182 | const Bonobo_ViewFrame view_frame, |
---|
183 | void *data) |
---|
184 | { |
---|
185 | view_data_t *view_data = g_new0 (view_data_t, 1); |
---|
186 | Bonobo_UIContainer corba_uic; |
---|
187 | Bonobo_Control corba_control; |
---|
188 | CORBA_Environment ev; |
---|
189 | |
---|
190 | view_data->bod = (bonobo_object_data_t *) data; |
---|
191 | view_data->server = bonobo_object_activate (view_data->bod->goad_id, 0); |
---|
192 | |
---|
193 | corba_control = BONOBO_OBJREF (view_data->server); |
---|
194 | |
---|
195 | CORBA_exception_init (&ev); |
---|
196 | corba_uic = Bonobo_ViewFrame_getUIHandler (view_frame, &ev); |
---|
197 | bonobo_object_check_env (BONOBO_OBJECT (bonobo_object), view_frame, &ev); |
---|
198 | CORBA_exception_free (&ev); |
---|
199 | |
---|
200 | view_data->control_frame = bonobo_control_frame_new (corba_uic); |
---|
201 | bonobo_control_frame_bind_to_control (view_data->control_frame, corba_control); |
---|
202 | bonobo_object_release_unref (corba_control, NULL); |
---|
203 | |
---|
204 | view_data->root = bonobo_control_frame_get_widget (view_data->control_frame); |
---|
205 | |
---|
206 | bonobo_control_frame_set_autoactivate (view_data->control_frame, TRUE); |
---|
207 | |
---|
208 | gtk_widget_show_all (view_data->root); |
---|
209 | |
---|
210 | view_data->view = bonobo_view_new (view_data->root); |
---|
211 | |
---|
212 | gtk_signal_connect (GTK_OBJECT (view_data->view), "destroy", |
---|
213 | GTK_SIGNAL_FUNC (destroy_view), view_data); |
---|
214 | |
---|
215 | gtk_signal_connect (GTK_OBJECT (view_data->view), "activate", |
---|
216 | GTK_SIGNAL_FUNC (view_activate_cb), view_data); |
---|
217 | |
---|
218 | if (view_data->bod->filename) |
---|
219 | do_load_file (view_data, view_data->bod->filename); |
---|
220 | |
---|
221 | view_data->bod->view_list = g_list_prepend (view_data->bod->view_list, view_data); |
---|
222 | |
---|
223 | return view_data->view; |
---|
224 | } |
---|
225 | |
---|
226 | static int |
---|
227 | load_file (BonoboPersistFile *pf, const CORBA_char *filename, CORBA_Environment *ev, void *closure) |
---|
228 | { |
---|
229 | bonobo_object_data_t *bod = closure; |
---|
230 | GList *c; |
---|
231 | |
---|
232 | g_free (bod->filename); |
---|
233 | bod->filename = g_strdup (filename); |
---|
234 | |
---|
235 | for (c = bod->view_list; c; c = c->next) { |
---|
236 | gint retval; |
---|
237 | |
---|
238 | retval = do_load_file ((view_data_t *) c->data, filename); |
---|
239 | if (retval != 0) |
---|
240 | return retval; |
---|
241 | } |
---|
242 | |
---|
243 | return 0; |
---|
244 | } |
---|
245 | |
---|
246 | BonoboObjectClient * |
---|
247 | gshell_control_wrapper (const gchar *goad_id) |
---|
248 | { |
---|
249 | BonoboObjectClient *object_client; |
---|
250 | Bonobo_Embeddable corba_embeddable; |
---|
251 | BonoboEmbeddable *bonobo_object; |
---|
252 | bonobo_object_data_t *bod; |
---|
253 | BonoboPersistFile *pf; |
---|
254 | |
---|
255 | bod = g_new0 (bonobo_object_data_t, 1); |
---|
256 | bod->goad_id = g_strdup (goad_id); |
---|
257 | |
---|
258 | bonobo_object = bonobo_embeddable_new (view_factory, bod); |
---|
259 | |
---|
260 | pf = bonobo_persist_file_new (load_file, NULL, bod); |
---|
261 | |
---|
262 | bonobo_object_add_interface (BONOBO_OBJECT (bonobo_object), |
---|
263 | BONOBO_OBJECT (pf)); |
---|
264 | |
---|
265 | corba_embeddable = BONOBO_OBJREF (bonobo_object); |
---|
266 | object_client = bonobo_object_client_from_corba (corba_embeddable); |
---|
267 | |
---|
268 | return object_client; |
---|
269 | } |
---|