Codepage messages related translated & other stuff...
[midnight-commander.git] / gnome / gcorba.c
blobb01cbe851187e74ba4e077b50eec13cce58fb828
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 "gcorba.h"
16 #include "gdesktop.h"
17 #include "global.h"
18 #include "gmain.h"
19 #include "gscreen.h"
20 #include "main.h"
23 /* The ORB for the whole program */
24 CORBA_ORB orb = CORBA_OBJECT_NIL;
26 /* The POA */
27 PortableServer_POA poa = CORBA_OBJECT_NIL;
31 /* Desktop servant */
32 typedef struct {
33 POA_GNOME_FileManager_Desktop servant;
34 } DesktopServant;
36 static PortableServer_ServantBase__epv desktop_base_epv;
37 static POA_GNOME_FileManager_Desktop__epv desktop_epv;
38 static POA_GNOME_FileManager_Desktop__vepv desktop_vepv;
40 /* Window servant */
41 typedef struct {
42 POA_GNOME_FileManager_Window servant;
44 WPanel *panel;
45 } WindowServant;
47 static PortableServer_ServantBase__epv window_base_epv;
48 static POA_GNOME_FileManager_Window__epv window_epv;
49 static POA_GNOME_FileManager_Window__vepv window_vepv;
51 /* WindowFactory servant */
53 typedef struct {
54 POA_GNOME_FileManager_WindowFactory servant;
55 } WindowFactoryServant;
57 static PortableServer_ServantBase__epv window_factory_base_epv;
58 static POA_GNOME_GenericFactory__epv window_factory_generic_factory_epv;
59 static POA_GNOME_FileManager_WindowFactory__epv window_factory_epv;
60 static POA_GNOME_FileManager_WindowFactory__vepv window_factory_vepv;
64 /* References to the window factory and desktop server objects */
66 static CORBA_Object window_factory_server = CORBA_OBJECT_NIL;
67 static CORBA_Object desktop_server = CORBA_OBJECT_NIL;
71 /* Desktop implementation */
73 /* Desktop::rescan method */
74 static void
75 Desktop_rescan (PortableServer_Servant servant, CORBA_Environment *ev)
77 desktop_reload_icons (FALSE, 0, 0);
80 /* Desktop::rescan_devices method */
81 static void
82 Desktop_rescan_devices (PortableServer_Servant servant, CORBA_Environment *ev)
84 desktop_rescan_devices ();
87 /* Desktop::arrange_icons method */
88 static void
89 Desktop_arrange_icons (PortableServer_Servant servant,
90 GNOME_FileManager_Desktop_ArrangeType type,
91 CORBA_Environment *ev)
93 SortType sort_type;
95 switch (type) {
96 case GNOME_FileManager_Desktop_BY_NAME:
97 sort_type = SORT_NAME;
98 break;
100 case GNOME_FileManager_Desktop_BY_TYPE:
101 sort_type = SORT_EXTENSION;
102 break;
104 case GNOME_FileManager_Desktop_BY_SIZE:
105 sort_type = SORT_SIZE;
106 break;
108 case GNOME_FileManager_Desktop_BY_ATIME:
109 sort_type = SORT_ACCESS;
110 break;
112 case GNOME_FileManager_Desktop_BY_MTIME:
113 sort_type = SORT_MODIFY;
114 break;
116 case GNOME_FileManager_Desktop_BY_CTIME:
117 sort_type = SORT_CHANGE;
118 break;
120 default:
121 return; /* Should we raise an exception instead? */
124 desktop_arrange_icons (sort_type);
127 /* Fills the vepv structure for the desktop object */
128 static void
129 Desktop_class_init (void)
131 static int inited = FALSE;
133 if (inited)
134 return;
136 inited = TRUE;
138 desktop_epv.rescan = Desktop_rescan;
139 desktop_epv.rescan_devices = Desktop_rescan_devices;
140 desktop_epv.arrange_icons = Desktop_arrange_icons;
142 desktop_vepv._base_epv = &desktop_base_epv;
143 desktop_vepv.GNOME_FileManager_Desktop_epv = &desktop_epv;
146 /* Creates a reference for the desktop object */
147 static GNOME_FileManager_Desktop
148 Desktop_create (PortableServer_POA poa, CORBA_Environment *ev)
150 DesktopServant *ds;
151 PortableServer_ObjectId *objid;
153 Desktop_class_init ();
155 ds = g_new0 (DesktopServant, 1);
156 ds->servant.vepv = &desktop_vepv;
158 POA_GNOME_FileManager_Desktop__init ((PortableServer_Servant) ds, ev);
159 objid = PortableServer_POA_activate_object (poa, ds, ev);
160 CORBA_free (objid);
162 return PortableServer_POA_servant_to_reference (poa, ds, ev);
167 /* Window implementation */
169 /* Window::close method */
170 static void
171 Window_close (PortableServer_Servant servant, CORBA_Environment *ev)
173 WindowServant *ws;
175 ws = (WindowServant *) servant;
176 gnome_close_panel (GTK_WIDGET (ws->panel->xwindow), ws->panel);
179 /* Destroys the servant for an image window */
180 static void
181 window_destroy (WindowServant *ws, CORBA_Environment *ev)
183 PortableServer_ObjectId *objid;
185 objid = PortableServer_POA_servant_to_id (poa, ws, ev);
186 PortableServer_POA_deactivate_object (poa, objid, ev);
187 CORBA_free (objid);
189 POA_GNOME_FileManager_Window__fini (ws, ev);
190 g_free (ws);
193 /* Fills the vepv structure for the window object */
194 static void
195 Window_class_init (void)
197 static int inited = FALSE;
199 if (inited)
200 return;
202 inited = TRUE;
204 window_epv.close = Window_close;
206 window_vepv._base_epv = &window_base_epv;
207 window_vepv.GNOME_FileManager_Window_epv = &window_epv;
212 /* WindowFactory implementation */
214 /* WindowFactory::the_desktop attribute getter */
215 static GNOME_FileManager_Desktop
216 WindowFactory_get_the_desktop (PortableServer_Servant servant,
217 CORBA_Environment *ev)
219 g_assert (desktop_server != CORBA_OBJECT_NIL);
221 return CORBA_Object_duplicate (desktop_server, ev);
224 /* Called when a panel created through CORBA is destroyed */
225 static void
226 panel_destroyed (GtkObject *object, gpointer data)
228 WindowServant *ws;
229 CORBA_Environment ev;
231 ws = data;
233 CORBA_exception_init (&ev);
234 window_destroy (ws, &ev);
235 CORBA_exception_free (&ev);
238 /* Returns a servant for a panel, creating one if necessary */
239 static WindowServant *
240 window_servant_from_panel (WPanel *panel, CORBA_Environment *ev)
242 WindowServant *ws;
243 PortableServer_ObjectId *objid;
245 if (panel->servant)
246 return panel->servant;
248 Window_class_init ();
250 ws = g_new0 (WindowServant, 1);
251 ws->servant.vepv = &window_vepv;
253 POA_GNOME_FileManager_Window__init ((PortableServer_Servant) ws, ev);
254 objid = PortableServer_POA_activate_object (poa, ws, ev);
255 CORBA_free (objid);
257 ws->panel = panel;
258 panel->servant = ws;
260 gtk_signal_connect (GTK_OBJECT (panel->xwindow), "destroy",
261 (GtkSignalFunc) panel_destroyed,
262 ws);
264 return ws;
267 /* WindowFactory::create_window method */
268 static GNOME_FileManager_Window
269 WindowFactory_create_window (PortableServer_Servant servant,
270 const CORBA_char *dir,
271 CORBA_Environment *ev)
273 WPanel *panel;
274 WindowServant *ws;
276 panel = new_panel_at (dir);
277 ws = window_servant_from_panel (panel, ev);
279 return PortableServer_POA_servant_to_reference (poa, ws, ev);
282 /* WindowFactory::rescan_directory method */
283 static void
284 WindowFactory_rescan_directory (PortableServer_Servant servant,
285 const CORBA_char *dir,
286 CORBA_Environment *ev)
288 PanelContainer *pc;
289 GList *l;
290 int len;
292 /* We do a blind compare against the panel's cwd */
294 len = strlen (dir);
295 if (dir[len - 1] == PATH_SEP)
296 len--;
298 for (l = containers; l; l = l->next) {
299 pc = l->data;
301 if (strncmp (dir, pc->panel->cwd, len) == 0
302 && (pc->panel->cwd[len] == 0 || pc->panel->cwd[len] == PATH_SEP))
303 update_one_panel_widget (pc->panel, UP_RELOAD, UP_KEEPSEL);
307 /* WindowFactory::close_invalid_windows method */
308 static void
309 WindowFactory_close_invalid_windows (PortableServer_Servant servant,
310 CORBA_Environment *ev)
312 PanelContainer *pc;
313 GList *l;
315 /* To see if a panel is valid or not, we try to cd to its cwd. If this
316 * fails, then we destroy the panel's window.
319 l = containers;
320 while (l) {
321 pc = l->data;
322 l = l->next;
324 if (mc_chdir (pc->panel->cwd) != 0)
325 gnome_close_panel (GTK_WIDGET (pc->panel->xwindow), pc->panel);
329 /* Creates an object reference for a panel */
330 static GNOME_FileManager_Window
331 window_reference_from_panel (WPanel *panel, CORBA_Environment *ev)
333 WindowServant *ws;
335 ws = window_servant_from_panel (panel, ev);
336 return PortableServer_POA_servant_to_reference (poa, ws, ev);
339 /* WindowFactory::get_window_by_directory method */
340 static GNOME_FileManager_WindowFactory_WindowSeq *
341 WindowFactory_get_windows_by_directory (PortableServer_Servant servant,
342 const CORBA_char *dir,
343 CORBA_Environment *ev)
345 GNOME_FileManager_WindowFactory_WindowSeq *seq;
346 PanelContainer *pc;
347 GList *l;
348 int n, i;
350 /* We return a sequence of the windows that match the specified
351 * directory.
354 /* Count 'em */
355 n = 0;
356 for (l = containers; l; l = l->next) {
357 pc = l->data;
359 if (strcmp (pc->panel->cwd, dir) == 0)
360 n++;
363 seq = GNOME_FileManager_WindowFactory_WindowSeq__alloc ();
364 seq->_length = n;
365 seq->_buffer = CORBA_sequence_GNOME_FileManager_Window_allocbuf (n);
367 i = 0;
369 for (l = containers; l; l = l->next) {
370 pc = l->data;
372 if (strcmp (pc->panel->cwd, dir) == 0)
373 seq->_buffer[i++] = window_reference_from_panel (pc->panel, ev);
376 return seq;
379 /* WindowFactory GenericFactory::supports method */
380 static CORBA_boolean
381 WindowFactory_supports (PortableServer_Servant servant,
382 const CORBA_char *obj_goad_id,
383 CORBA_Environment *ev)
385 if (strcmp (obj_goad_id, "IDL:GNOME:FileManager:Window:1.0") == 0
386 || strcmp (obj_goad_id, "IDL:GNOME:FileManager:Desktop:1.0") == 0)
387 return CORBA_TRUE;
388 else
389 return CORBA_FALSE;
392 /* WindowFactory GenericFactory::create_object method */
393 static CORBA_Object
394 WindowFactory_create_object (PortableServer_Servant servant,
395 const CORBA_char *goad_id,
396 const GNOME_stringlist *params,
397 CORBA_Environment *ev)
399 if (strcmp (goad_id, "IDL:GNOME:FileManager:Window:1.0") != 0)
400 return WindowFactory_create_window (
401 servant,
402 params->_length != 0 ? params->_buffer[0] : home_dir,
403 ev);
404 else if (strcmp (goad_id, "IDL:GNOME:FileManager:Desktop:1.0") == 0)
405 return WindowFactory_get_the_desktop (servant, ev);
406 else {
407 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
408 ex_GNOME_GenericFactory_CannotActivate,
409 NULL);
410 return CORBA_OBJECT_NIL;
414 /* Fills the vepv structure for the window factory object */
415 static void
416 WindowFactory_class_init (void)
418 static int inited = FALSE;
420 if (inited)
421 return;
423 inited = TRUE;
425 window_factory_generic_factory_epv.supports = WindowFactory_supports;
426 window_factory_generic_factory_epv.create_object = WindowFactory_create_object;
428 window_factory_epv._get_the_desktop = WindowFactory_get_the_desktop;
429 window_factory_epv.create_window = WindowFactory_create_window;
430 window_factory_epv.rescan_directory = WindowFactory_rescan_directory;
431 window_factory_epv.close_invalid_windows = WindowFactory_close_invalid_windows;
432 window_factory_epv.get_windows_by_directory = WindowFactory_get_windows_by_directory;
434 window_factory_vepv._base_epv = &window_factory_base_epv;
435 window_factory_vepv.GNOME_GenericFactory_epv = &window_factory_generic_factory_epv;
436 window_factory_vepv.GNOME_FileManager_WindowFactory_epv = &window_factory_epv;
439 /* Creates a reference for the window factory object */
440 static GNOME_FileManager_WindowFactory
441 WindowFactory_create (PortableServer_POA poa, CORBA_Environment *ev)
443 WindowFactoryServant *wfs;
444 PortableServer_ObjectId *objid;
446 WindowFactory_class_init ();
448 wfs = g_new0 (WindowFactoryServant, 1);
449 wfs->servant.vepv = &window_factory_vepv;
451 POA_GNOME_FileManager_WindowFactory__init ((PortableServer_Servant) wfs, ev);
452 objid = PortableServer_POA_activate_object (poa, wfs, ev);
453 CORBA_free (objid);
455 return PortableServer_POA_servant_to_reference (poa, wfs, ev);
460 /* Creates and registers the CORBA servers. Returns TRUE on success, FALSE
461 * otherwise.
463 static int
464 register_servers (void)
466 CORBA_Environment ev;
467 int retval;
468 int v;
470 retval = FALSE;
471 CORBA_exception_init (&ev);
473 /* Register the window factory and see if it was already there */
475 window_factory_server = WindowFactory_create (poa, &ev);
476 if (ev._major != CORBA_NO_EXCEPTION)
477 goto out;
479 v = goad_server_register (CORBA_OBJECT_NIL, window_factory_server,
480 "IDL:GNOME:FileManager:WindowFactory:1.0", "object", &ev);
481 switch (v) {
482 case 0:
483 corba_have_server = FALSE;
484 break;
486 case -2:
487 corba_have_server = FALSE;
488 break;
490 default:
491 goto out;
494 /* Register the desktop server */
496 desktop_server = Desktop_create (poa, &ev);
497 if (ev._major != CORBA_NO_EXCEPTION)
498 goto out;
500 goad_server_register (CORBA_OBJECT_NIL, desktop_server,
501 "IDL:GNOME:FileManager:Desktop:1.0", "object", &ev);
503 retval = TRUE;
505 /* Done */
506 out:
507 CORBA_exception_free (&ev);
509 return retval;
513 * corba_init_server:
514 * @void:
516 * Initializes the CORBA server for GMC. Returns whether initialization was
517 * successful or not, and sets the global corba_have_server variable.
519 * Return value: TRUE if successful, FALSE otherwise.
522 corba_init_server (void)
524 int retval;
525 CORBA_Environment ev;
527 retval = FALSE;
528 CORBA_exception_init (&ev);
530 /* Get the POA and create the server */
532 poa = (PortableServer_POA) CORBA_ORB_resolve_initial_references (orb, "RootPOA", &ev);
533 if (ev._major != CORBA_NO_EXCEPTION)
534 goto out;
536 CORBA_exception_free (&ev);
538 /* See if the servers are there */
540 window_factory_server = goad_server_activate_with_id (
541 NULL,
542 "IDL:GNOME:FileManager:WindowFactory:1.0",
543 GOAD_ACTIVATE_EXISTING_ONLY,
544 NULL);
546 desktop_server = goad_server_activate_with_id (
547 NULL,
548 "IDL:GNOME:FileManager:Desktop:1.0",
549 GOAD_ACTIVATE_EXISTING_ONLY,
550 NULL);
552 if (window_factory_server != CORBA_OBJECT_NIL) {
553 corba_have_server = TRUE;
554 retval = TRUE;
555 } else
556 retval = register_servers ();
558 out:
559 return retval;
563 * corba_activate_server:
564 * @void:
566 * Activates the POA manager and thus makes the services available to the
567 * outside world.
569 void
570 corba_activate_server (void)
572 CORBA_Environment ev;
573 PortableServer_POAManager poa_manager;
575 /* Do nothing if the server is already running */
576 if (corba_have_server)
577 return;
579 CORBA_exception_init (&ev);
581 poa_manager = PortableServer_POA__get_the_POAManager (poa, &ev);
582 if (ev._major != CORBA_NO_EXCEPTION)
583 goto out;
585 PortableServer_POAManager_activate (poa_manager, &ev);
586 if (ev._major != CORBA_NO_EXCEPTION)
587 goto out;
589 out:
591 CORBA_exception_free (&ev);
595 * corba_create_window:
596 * @dir: The directory in which to create the window, or NULL for the cwd.
598 * Creates a GMC window using a CORBA call to the server.
600 void
601 corba_create_window (char *dir)
603 CORBA_Environment ev;
604 char cwd[MC_MAXPATHLEN];
606 if (dir == NULL) {
607 mc_get_current_wd (cwd, MC_MAXPATHLEN);
608 dir = cwd;
611 CORBA_exception_init (&ev);
612 GNOME_FileManager_WindowFactory_create_window (window_factory_server, dir, &ev);
613 CORBA_exception_free (&ev);