ru.po: Corrections from Evgeny Bulgakov <bgav@netvision.net.il>
[midnight-commander.git] / gnome / gcorba.c
blob983dd0c9511a78603269b52aacc51889ecf84c2f
1 /* CORBA support for the Midnight Commander
3 * Copyright (C) 1999 The Free Sofware Foundation
5 * Authors: Miguel de Icaza <miguel@nuclecu.unam.mx>
6 * Federico Mena <federico@nuclecu.unam.mx>
7 * Elliot Lee <sopwith@cuc.edu>
8 */
10 #include <config.h>
11 #include "panel.h"
12 #include "../vfs/vfs.h"
13 #include <libgnorba/gnorba.h>
14 #include "FileManager.h"
15 #include "gcmd.h"
16 #include "gcorba.h"
17 #include "gdesktop.h"
18 #include "global.h"
19 #include "gmain.h"
20 #include "gscreen.h"
21 #include "main.h"
24 /* The ORB for the whole program */
25 CORBA_ORB orb = CORBA_OBJECT_NIL;
27 /* The POA */
28 PortableServer_POA poa = CORBA_OBJECT_NIL;
32 /* Desktop servant */
33 typedef struct {
34 POA_GNOME_FileManager_Desktop servant;
35 } DesktopServant;
37 static PortableServer_ServantBase__epv desktop_base_epv;
38 static POA_GNOME_FileManager_Desktop__epv desktop_epv;
39 static POA_GNOME_FileManager_Desktop__vepv desktop_vepv;
41 /* Window servant */
42 typedef struct {
43 POA_GNOME_FileManager_Window servant;
45 WPanel *panel;
46 } WindowServant;
48 static PortableServer_ServantBase__epv window_base_epv;
49 static POA_GNOME_FileManager_Window__epv window_epv;
50 static POA_GNOME_FileManager_Window__vepv window_vepv;
52 /* WindowFactory servant */
54 typedef struct {
55 POA_GNOME_FileManager_WindowFactory servant;
56 } WindowFactoryServant;
58 static PortableServer_ServantBase__epv window_factory_base_epv;
59 static POA_GNOME_GenericFactory__epv window_factory_generic_factory_epv;
60 static POA_GNOME_FileManager_WindowFactory__epv window_factory_epv;
61 static POA_GNOME_FileManager_WindowFactory__vepv window_factory_vepv;
65 /* References to the window factory and desktop server objects */
67 static CORBA_Object window_factory_server = CORBA_OBJECT_NIL;
68 static CORBA_Object desktop_server = CORBA_OBJECT_NIL;
72 /* Desktop implementation */
74 /* Desktop::rescan method */
75 static void
76 Desktop_rescan (PortableServer_Servant servant, CORBA_Environment *ev)
78 desktop_reload_icons (FALSE, 0, 0);
81 /* Desktop::rescan_devices method */
82 static void
83 Desktop_rescan_devices (PortableServer_Servant servant, CORBA_Environment *ev)
85 desktop_rescan_devices ();
88 /* Fills the vepv structure for the desktop object */
89 static void
90 Desktop_class_init (void)
92 static int inited = FALSE;
94 if (inited)
95 return;
97 inited = TRUE;
99 desktop_epv.rescan = Desktop_rescan;
100 desktop_epv.rescan_devices = Desktop_rescan_devices;
102 desktop_vepv._base_epv = &desktop_base_epv;
103 desktop_vepv.GNOME_FileManager_Desktop_epv = &desktop_epv;
106 /* Creates a reference for the desktop object */
107 static GNOME_FileManager_Desktop
108 Desktop_create (PortableServer_POA poa, CORBA_Environment *ev)
110 DesktopServant *ds;
111 PortableServer_ObjectId *objid;
113 Desktop_class_init ();
115 ds = g_new0 (DesktopServant, 1);
116 ds->servant.vepv = &desktop_vepv;
118 POA_GNOME_FileManager_Desktop__init ((PortableServer_Servant) ds, ev);
119 objid = PortableServer_POA_activate_object (poa, ds, ev);
120 CORBA_free (objid);
122 return PortableServer_POA_servant_to_reference (poa, ds, ev);
127 /* Window implementation */
129 /* Window::close method */
130 static void
131 Window_close (PortableServer_Servant servant, CORBA_Environment *ev)
133 WindowServant *ws;
135 ws = (WindowServant *) servant;
136 gnome_close_panel (GTK_WIDGET (ws->panel->xwindow), ws->panel);
139 /* Destroys the servant for an image window */
140 static void
141 window_destroy (WindowServant *ws, CORBA_Environment *ev)
143 PortableServer_ObjectId *objid;
145 objid = PortableServer_POA_servant_to_id (poa, ws, ev);
146 PortableServer_POA_deactivate_object (poa, objid, ev);
147 CORBA_free (objid);
149 POA_GNOME_FileManager_Window__fini (ws, ev);
150 g_free (ws);
153 /* Fills the vepv structure for the window object */
154 static void
155 Window_class_init (void)
157 static int inited = FALSE;
159 if (inited)
160 return;
162 inited = TRUE;
164 window_epv.close = Window_close;
166 window_vepv._base_epv = &window_base_epv;
167 window_vepv.GNOME_FileManager_Window_epv = &window_epv;
172 /* WindowFactory implementation */
174 /* WindowFactory::the_desktop attribute getter */
175 static GNOME_FileManager_Desktop
176 WindowFactory_get_the_desktop (PortableServer_Servant servant,
177 CORBA_Environment *ev)
179 g_assert (desktop_server != CORBA_OBJECT_NIL);
181 return CORBA_Object_duplicate (desktop_server, ev);
184 /* Called when a panel created through CORBA is destroyed */
185 static void
186 panel_destroyed (GtkObject *object, gpointer data)
188 WindowServant *ws;
189 CORBA_Environment ev;
191 ws = data;
193 CORBA_exception_init (&ev);
194 window_destroy (ws, &ev);
195 CORBA_exception_free (&ev);
198 /* Returns a servant for a panel, creating one if necessary */
199 static WindowServant *
200 window_servant_from_panel (WPanel *panel, CORBA_Environment *ev)
202 WindowServant *ws;
203 PortableServer_ObjectId *objid;
205 if (panel->servant)
206 return panel->servant;
208 Window_class_init ();
210 ws = g_new0 (WindowServant, 1);
211 ws->servant.vepv = &window_vepv;
213 POA_GNOME_FileManager_Window__init ((PortableServer_Servant) ws, ev);
214 objid = PortableServer_POA_activate_object (poa, ws, ev);
215 CORBA_free (objid);
217 ws->panel = panel;
218 panel->servant = ws;
220 gtk_signal_connect (GTK_OBJECT (panel->xwindow), "destroy",
221 (GtkSignalFunc) panel_destroyed,
222 ws);
224 return ws;
227 /* WindowFactory::create_window method */
228 static GNOME_FileManager_Window
229 WindowFactory_create_window (PortableServer_Servant servant,
230 CORBA_char *dir,
231 CORBA_Environment *ev)
233 WPanel *panel;
234 WindowServant *ws;
236 panel = new_panel_at (dir);
237 ws = window_servant_from_panel (panel, ev);
239 return PortableServer_POA_servant_to_reference (poa, ws, ev);
242 /* WindowFactory::rescan_directory method */
243 static void
244 WindowFactory_rescan_directory (PortableServer_Servant servant,
245 CORBA_char *dir,
246 CORBA_Environment *ev)
248 PanelContainer *pc;
249 GList *l;
250 int len;
252 /* We do a blind compare against the panel's cwd */
254 len = strlen (dir);
255 if (dir[len - 1] == PATH_SEP)
256 len--;
258 for (l = containers; l; l = l->next) {
259 pc = l->data;
261 if (strncmp (dir, pc->panel->cwd, len) == 0
262 && (pc->panel->cwd[len] == 0 || pc->panel->cwd[len] == PATH_SEP))
263 update_one_panel_widget (pc->panel, UP_RELOAD, UP_KEEPSEL);
267 /* WindowFactory::close_invalid_windows method */
268 static void
269 WindowFactory_close_invalid_windows (PortableServer_Servant servant,
270 CORBA_Environment *ev)
272 PanelContainer *pc;
273 GList *l;
275 /* To see if a panel is valid or not, we try to cd to its cwd. If this
276 * fails, then we destroy the panel's window.
279 l = containers;
280 while (l) {
281 pc = l->data;
282 l = l->next;
284 if (mc_chdir (pc->panel->cwd) != 0)
285 gnome_close_panel (GTK_WIDGET (pc->panel->xwindow), pc->panel);
289 /* Creates an object reference for a panel */
290 static GNOME_FileManager_Window
291 window_reference_from_panel (WPanel *panel, CORBA_Environment *ev)
293 WindowServant *ws;
295 ws = window_servant_from_panel (panel, ev);
296 return PortableServer_POA_servant_to_reference (poa, ws, ev);
299 /* WindowFactory::get_window_by_directory method */
300 static GNOME_FileManager_WindowFactory_WindowSeq *
301 WindowFactory_get_windows_by_directory (PortableServer_Servant servant,
302 CORBA_char *dir,
303 CORBA_Environment *ev)
305 GNOME_FileManager_WindowFactory_WindowSeq *seq;
306 PanelContainer *pc;
307 GList *l;
308 int n, i;
310 /* We return a sequence of the windows that match the specified
311 * directory.
314 /* Count 'em */
315 n = 0;
316 for (l = containers; l; l = l->next) {
317 pc = l->data;
319 if (strcmp (pc->panel->cwd, dir) == 0)
320 n++;
323 seq = GNOME_FileManager_WindowFactory_WindowSeq__alloc ();
324 seq->_length = n;
325 seq->_buffer = CORBA_sequence_GNOME_FileManager_Window_allocbuf (n);
327 i = 0;
329 for (l = containers; l; l = l->next) {
330 pc = l->data;
332 if (strcmp (pc->panel->cwd, dir) == 0)
333 seq->_buffer[i++] = window_reference_from_panel (pc->panel, ev);
336 return seq;
339 /* WindowFactory GenericFactory::supports method */
340 static CORBA_boolean
341 WindowFactory_supports (PortableServer_Servant servant,
342 CORBA_char *obj_goad_id,
343 CORBA_Environment *ev)
345 if (strcmp (obj_goad_id, "IDL:GNOME:FileManager:Window:1.0") == 0
346 || strcmp (obj_goad_id, "IDL:GNOME:FileManager:Desktop:1.0") == 0)
347 return CORBA_TRUE;
348 else
349 return CORBA_FALSE;
352 /* WindowFactory GenericFactory::create_object method */
353 static CORBA_Object
354 WindowFactory_create_object (PortableServer_Servant servant,
355 CORBA_char *goad_id,
356 GNOME_stringlist *params,
357 CORBA_Environment *ev)
359 if (strcmp (goad_id, "IDL:GNOME:FileManager:Window:1.0") != 0)
360 return WindowFactory_create_window (
361 servant,
362 params->_length != 0 ? params->_buffer[0] : home_dir,
363 ev);
364 else if (strcmp (goad_id, "IDL:GNOME:FileManager:Desktop:1.0") == 0)
365 return WindowFactory_get_the_desktop (servant, ev);
366 else {
367 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
368 ex_GNOME_GenericFactory_CannotActivate,
369 NULL);
370 return CORBA_OBJECT_NIL;
374 /* Fills the vepv structure for the window factory object */
375 static void
376 WindowFactory_class_init (void)
378 static int inited = FALSE;
380 if (inited)
381 return;
383 inited = TRUE;
385 window_factory_generic_factory_epv.supports = WindowFactory_supports;
386 window_factory_generic_factory_epv.create_object = WindowFactory_create_object;
388 window_factory_epv._get_the_desktop = WindowFactory_get_the_desktop;
389 window_factory_epv.create_window = WindowFactory_create_window;
390 window_factory_epv.rescan_directory = WindowFactory_rescan_directory;
391 window_factory_epv.close_invalid_windows = WindowFactory_close_invalid_windows;
392 window_factory_epv.get_windows_by_directory = WindowFactory_get_windows_by_directory;
394 window_factory_vepv._base_epv = &window_factory_base_epv;
395 window_factory_vepv.GNOME_GenericFactory_epv = &window_factory_generic_factory_epv;
396 window_factory_vepv.GNOME_FileManager_WindowFactory_epv = &window_factory_epv;
399 /* Creates a reference for the window factory object */
400 static GNOME_FileManager_WindowFactory
401 WindowFactory_create (PortableServer_POA poa, CORBA_Environment *ev)
403 WindowFactoryServant *wfs;
404 PortableServer_ObjectId *objid;
406 WindowFactory_class_init ();
408 wfs = g_new0 (WindowFactoryServant, 1);
409 wfs->servant.vepv = &window_factory_vepv;
411 POA_GNOME_FileManager_WindowFactory__init ((PortableServer_Servant) wfs, ev);
412 objid = PortableServer_POA_activate_object (poa, wfs, ev);
413 CORBA_free (objid);
415 return PortableServer_POA_servant_to_reference (poa, wfs, ev);
420 /* Creates and registers the CORBA servers. Returns TRUE on success, FALSE
421 * otherwise.
423 static int
424 register_servers (void)
426 CORBA_Environment ev;
427 int retval;
428 int v;
430 retval = FALSE;
431 CORBA_exception_init (&ev);
433 /* Register the window factory and see if it was already there */
435 window_factory_server = WindowFactory_create (poa, &ev);
436 if (ev._major != CORBA_NO_EXCEPTION)
437 goto out;
439 v = goad_server_register (CORBA_OBJECT_NIL, window_factory_server,
440 "IDL:GNOME:FileManager:WindowFactory:1.0", "object", &ev);
441 switch (v) {
442 case 0:
443 corba_have_server = FALSE;
444 break;
446 case -2:
447 corba_have_server = FALSE;
448 break;
450 default:
451 goto out;
454 /* Register the desktop server */
456 desktop_server = Desktop_create (poa, &ev);
457 if (ev._major != CORBA_NO_EXCEPTION)
458 goto out;
460 goad_server_register (CORBA_OBJECT_NIL, desktop_server,
461 "IDL:GNOME:FileManager:Desktop:1.0", "object", &ev);
463 retval = TRUE;
465 /* Done */
466 out:
467 CORBA_exception_free (&ev);
469 return retval;
473 * corba_init_server:
474 * @void:
476 * Initializes the CORBA server for GMC. Returns whether initialization was
477 * successful or not, and sets the global corba_have_server variable.
479 * Return value: TRUE if successful, FALSE otherwise.
482 corba_init_server (void)
484 int retval;
485 CORBA_Environment ev;
487 retval = FALSE;
488 CORBA_exception_init (&ev);
490 /* Get the POA and create the server */
492 poa = (PortableServer_POA) CORBA_ORB_resolve_initial_references (orb, "RootPOA", &ev);
493 if (ev._major != CORBA_NO_EXCEPTION)
494 goto out;
496 CORBA_exception_free (&ev);
498 /* See if the servers are there */
500 window_factory_server = goad_server_activate_with_id (
501 NULL,
502 "IDL:GNOME:FileManager:WindowFactory:1.0",
503 GOAD_ACTIVATE_EXISTING_ONLY,
504 NULL);
506 desktop_server = goad_server_activate_with_id (
507 NULL,
508 "IDL:GNOME:FileManager:Desktop:1.0",
509 GOAD_ACTIVATE_EXISTING_ONLY,
510 NULL);
512 if (window_factory_server != CORBA_OBJECT_NIL) {
513 corba_have_server = TRUE;
514 retval = TRUE;
515 } else
516 retval = register_servers ();
518 out:
519 return retval;
523 * corba_activate_server:
524 * @void:
526 * Activates the POA manager and thus makes the services available to the
527 * outside world.
529 void
530 corba_activate_server (void)
532 CORBA_Environment ev;
533 PortableServer_POAManager poa_manager;
535 /* Do nothing if the server is already running */
536 if (corba_have_server)
537 return;
539 CORBA_exception_init (&ev);
541 poa_manager = PortableServer_POA__get_the_POAManager (poa, &ev);
542 if (ev._major != CORBA_NO_EXCEPTION)
543 goto out;
545 PortableServer_POAManager_activate (poa_manager, &ev);
546 if (ev._major != CORBA_NO_EXCEPTION)
547 goto out;
549 out:
551 CORBA_exception_free (&ev);
555 * corba_create_window:
556 * @dir: The directory in which to create the window, or NULL for the cwd.
558 * Creates a GMC window using a CORBA call to the server.
560 void
561 corba_create_window (char *dir)
563 CORBA_Environment ev;
564 char cwd[MC_MAXPATHLEN];
566 if (dir == NULL) {
567 mc_get_current_wd (cwd, MC_MAXPATHLEN);
568 dir = cwd;
571 CORBA_exception_init (&ev);
572 GNOME_FileManager_WindowFactory_create_window (window_factory_server, dir, &ev);
573 CORBA_exception_free (&ev);