Revert, revert, try another thing
[gnumeric.git] / src / corba-workbook.c
blobbd1e5ef595d009ef51099101b0199ac9e95749c0
1 /*
2 * corba-workbook.c: CORBA Workbook exporting.
4 * Author:
5 * Miguel de Icaza (miguel@gnu.org)
6 */
7 #include <config.h>
8 #include <libgnorba/gnome-factory.h>
9 #include <gnome.h>
10 #include <bonobo/bonobo-object.h>
11 #include "sheet.h"
12 #include "gnumeric.h"
13 #include "workbook.h"
14 #include "idl/Gnumeric.h"
15 #include "xml-io.h"
16 #include "corba.h"
17 #include "commands.h"
18 #include "command-context-corba.h"
19 #include "workbook-private.h"
20 #include "sheet-private.h"
22 typedef struct {
23 POA_GNOME_Gnumeric_Workbook servant;
24 Workbook *workbook;
25 } WorkbookServant;
27 static POA_GNOME_Gnumeric_Workbook__vepv gnome_gnumeric_workbook_vepv;
28 static POA_GNOME_Gnumeric_Workbook__epv gnome_gnumeric_workbook_epv;
30 static Workbook *
31 workbook_from_servant (PortableServer_Servant servant)
33 WorkbookServant *ws = (WorkbookServant *) servant;
35 return ws->workbook;
38 static inline GNOME_Gnumeric_Sheet
39 corba_sheet (Sheet *sheet, CORBA_Environment *ev)
41 return CORBA_Object_duplicate (sheet->private->corba_server, ev);
44 static GNOME_Gnumeric_Sheet
45 Workbook_sheet_new (PortableServer_Servant servant, const CORBA_char * name, CORBA_Environment * ev)
47 Workbook *workbook = workbook_from_servant (servant);
48 Sheet *sheet;
50 if (workbook_sheet_lookup (workbook, name)){
51 CORBA_exception_set (ev, CORBA_USER_EXCEPTION, ex_GNOME_Gnumeric_Workbook_NameExists, NULL);
52 return CORBA_OBJECT_NIL;
55 sheet = sheet_new (workbook, name);
57 workbook_attach_sheet (workbook, sheet);
59 return corba_sheet (sheet, ev);
62 static GNOME_Gnumeric_Sheet
63 Workbook_sheet_lookup (PortableServer_Servant servant, const CORBA_char * name, CORBA_Environment * ev)
65 Workbook *workbook = workbook_from_servant (servant);
66 Sheet *sheet;
68 sheet = workbook_sheet_lookup (workbook, name);
69 if (sheet == NULL)
70 return CORBA_OBJECT_NIL;
72 return corba_sheet (sheet, ev);
76 * FIXME: This breaks the link between name, saver and level.
78 static void
79 Workbook_set_filename (PortableServer_Servant servant, const CORBA_char * name, CORBA_Environment * ev)
81 Workbook *workbook = workbook_from_servant (servant);
83 workbook_set_filename (workbook, name);
86 static void
87 Workbook_save_to (PortableServer_Servant servant, const CORBA_char * filename, CORBA_Environment * ev)
89 Workbook *wb = workbook_from_servant (servant);
91 /* <FIXME> */
93 g_warning ("Failure to save will not be noticed");
95 if (gnumeric_xml_write_workbook (command_context_corba (wb),
96 wb, filename) == 0)
97 workbook_mark_clean (wb);
98 else
99 g_error ("Could not save to file %s", wb->filename);
101 /* </FIXME> */
104 static GNOME_Gnumeric_Sheet
105 Workbook_sheet_current (PortableServer_Servant servant, CORBA_Environment * ev)
107 Workbook *wb = workbook_from_servant (servant);
108 Sheet *sheet = wb->current_sheet;
110 return corba_sheet (sheet, ev);
113 static GNOME_Gnumeric_Sheet
114 Workbook_sheet_nth (PortableServer_Servant servant, const CORBA_long n, CORBA_Environment * ev)
116 g_error ("Same stuff!");
118 return CORBA_OBJECT_NIL;
121 static CORBA_long
122 Workbook_sheet_count (PortableServer_Servant servant, CORBA_Environment * ev)
124 Workbook *workbook = workbook_from_servant (servant);
126 return workbook_sheet_count (workbook);
129 static void
130 Workbook_set_dirty (PortableServer_Servant servant, const CORBA_boolean is_dirty, CORBA_Environment * ev)
132 Workbook *workbook = workbook_from_servant (servant);
134 workbook_set_dirty (workbook, is_dirty);
137 static CORBA_boolean
138 Workbook_sheet_rename (PortableServer_Servant servant,
139 const CORBA_char * old_name,
140 const CORBA_char * new_name, CORBA_Environment * ev)
142 Workbook *workbook = workbook_from_servant (servant);
144 return cmd_rename_sheet (command_context_corba (workbook),
145 workbook, old_name, new_name);
148 static void
149 Workbook_recalc (PortableServer_Servant servant, CORBA_Environment * ev)
151 Workbook *workbook = workbook_from_servant (servant);
153 workbook_recalc (workbook);
156 static void
157 Workbook_recalc_all (PortableServer_Servant servant, CORBA_Environment * ev)
159 Workbook *workbook = workbook_from_servant (servant);
161 workbook_recalc_all (workbook);
164 static void
165 Workbook_parse (PortableServer_Servant servant,
166 const CORBA_char * cellref,
167 GNOME_Gnumeric_Sheet * sheet,
168 CORBA_long * col,
169 CORBA_long * row, CORBA_Environment * ev)
173 static void
174 Workbook_show (PortableServer_Servant servant, CORBA_boolean show_toplevel, CORBA_Environment *ev)
176 Workbook *workbook = workbook_from_servant (servant);
178 if (show_toplevel)
179 gtk_widget_show (workbook->toplevel);
180 else
181 gtk_widget_hide (workbook->toplevel);
184 static void
185 Workbook_corba_class_init ()
187 static int inited;
189 if (inited)
190 return;
191 inited = TRUE;
193 gnome_gnumeric_workbook_vepv.GNOME_Gnumeric_Workbook_epv =
194 &gnome_gnumeric_workbook_epv;
195 gnome_gnumeric_workbook_vepv.Bonobo_Unknown_epv =
196 bonobo_object_get_epv ();
198 gnome_gnumeric_workbook_epv.sheet_new = Workbook_sheet_new;
199 gnome_gnumeric_workbook_epv.sheet_lookup = Workbook_sheet_lookup;
200 gnome_gnumeric_workbook_epv.set_filename = Workbook_set_filename;
201 gnome_gnumeric_workbook_epv.save_to = Workbook_save_to;
202 gnome_gnumeric_workbook_epv.sheet_current = Workbook_sheet_current;
203 gnome_gnumeric_workbook_epv.sheet_nth = Workbook_sheet_nth;
204 gnome_gnumeric_workbook_epv.sheet_count = Workbook_sheet_count;
205 gnome_gnumeric_workbook_epv.set_dirty = Workbook_set_dirty;
206 gnome_gnumeric_workbook_epv.sheet_rename = Workbook_sheet_rename;
207 gnome_gnumeric_workbook_epv.recalc = Workbook_recalc;
208 gnome_gnumeric_workbook_epv.recalc_all = Workbook_recalc_all;
209 gnome_gnumeric_workbook_epv.parse = Workbook_parse;
210 gnome_gnumeric_workbook_epv.show = Workbook_show;
214 * Initializes the CORBA side of a Workbook structure.
216 * Creates the POA servant to handle requests for this Workbook and makes
217 * the Workbook a GNOME object server.
219 void
220 workbook_corba_setup (Workbook *workbook)
222 WorkbookServant *ws;
223 CORBA_Environment ev;
224 PortableServer_ObjectId *objid;
226 Workbook_corba_class_init ();
228 ws = g_new0 (WorkbookServant, 1);
229 ws->servant.vepv = &gnome_gnumeric_workbook_vepv;
230 ws->workbook = workbook;
232 CORBA_exception_init (&ev);
233 POA_GNOME_Gnumeric_Workbook__init ((PortableServer_Servant) ws, &ev);
234 objid = PortableServer_POA_activate_object (gnumeric_poa, ws, &ev);
235 CORBA_free (objid);
236 workbook->corba_server = PortableServer_POA_servant_to_reference (gnumeric_poa, ws, &ev);
238 workbook->priv->corba_context = command_context_corba_new ();
239 CORBA_exception_free (&ev);
242 void
243 workbook_corba_shutdown (Workbook *wb)
245 CORBA_Environment ev;
247 g_return_if_fail (wb != NULL);
248 g_return_if_fail (wb->corba_server != NULL);
250 g_warning ("Should release all the corba resources here");
252 gtk_object_destroy (GTK_OBJECT (wb->priv->corba_context));
254 CORBA_exception_init (&ev);
255 PortableServer_POA_deactivate_object (gnumeric_poa, wb->corba_server, &ev);
256 CORBA_exception_free (&ev);
259 CommandContext *
260 command_context_corba (Workbook *wb)
262 /* When we are operating before a workbook is created
263 * wb can be NULL
265 if (!wb)
266 return command_context_corba_new ();
268 g_return_val_if_fail (wb->priv && wb->priv->corba_context, NULL);
270 return wb->priv->corba_context;