1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SALOME_Session_i.cxx
23 // Author : Paul RASCLE, EDF
26 #include "utilities.h"
28 #include "Session_Session_i.hxx"
30 #include "SALOME_NamingService.hxx"
31 #include "SALOME_Event.h"
32 #include "SalomeApp_Engine_i.h"
33 #include "LightApp_Application.h"
34 #include "LightApp_SelectionMgr.h"
35 #include "SALOME_ListIO.hxx"
36 #include "SUIT_Session.h"
37 #include "SUIT_Desktop.h"
38 #include "SUIT_Study.h"
40 #include "Basics_Utils.hxx"
43 #include <QWaitCondition>
45 // Open CASCADE Includes
46 #include <OSD_SharedLibrary.hxx>
47 #include <OSD_Function.hxx>
58 SALOME_Session_i::SALOME_Session_i(int argc,
61 PortableServer::POA_ptr poa,
63 QWaitCondition* GUILauncher)
68 _orb = CORBA::ORB::_duplicate(orb) ;
69 _poa = PortableServer::POA::_duplicate(poa) ;
71 _GUILauncher = GUILauncher;
72 _NS = new SALOME_NamingService(_orb);
73 _isShuttingDown = false;
74 //MESSAGE("constructor end");
78 returns Visu component
80 Engines::EngineComponent_ptr SALOME_Session_i::GetComponent(const char* theLibraryName)
82 typedef Engines::EngineComponent_ptr TGetImpl(CORBA::ORB_ptr,
83 PortableServer::POA_ptr,
84 SALOME_NamingService*,QMutex*);
85 OSD_SharedLibrary aSharedLibrary(const_cast<char*>(theLibraryName));
86 if (aSharedLibrary.DlOpen(OSD_RTLD_LAZY)) {
87 if (OSD_Function anOSDFun = aSharedLibrary.DlSymb("GetImpl"))
88 return ((TGetImpl (*)) anOSDFun)(_orb,_poa,_NS,_GUIMutex);
90 CORBA::Object_var obj = SalomeApp_Engine_i::EngineForComponent(theLibraryName, true);
91 if (!CORBA::is_nil(obj)){
92 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
93 return anEngine._retn();
95 return Engines::EngineComponent::_nil();
101 SALOME_Session_i::~SALOME_Session_i()
104 //MESSAGE("destructor end");
108 tries to find the Corba Naming Service and to register the session,
109 gives naming service interface to _IAPPThread
111 void SALOME_Session_i::NSregister()
113 CORBA::Object_var obref=_this();
114 SALOME::Session_var pSession = SALOME::Session::_narrow(obref);
117 _NS->Register(pSession, "/Kernel/Session");
119 catch (ServiceUnreachable&)
121 INFOS("Caught exception: Naming Service Unreachable");
126 INFOS("Caught unknown exception from Naming Service");
128 //MESSAGE("Session registered in Naming Service");
132 Unregister session server from CORBA Naming Service
134 void SALOME_Session_i::NSunregister()
138 _NS->Destroy_Name("/Kernel/Session");
140 catch (ServiceUnreachable&)
142 INFOS("Caught exception: Naming Service Unreachable");
146 INFOS("Caught unknown exception from Naming Service");
151 Launches the GUI if there is none.
152 The Corba method is oneway (corba client does'nt wait for GUI completion)
154 void SALOME_Session_i::GetInterface()
158 if ( !SUIT_Session::session() )
160 _GUILauncher->wakeAll();
161 MESSAGE("SALOME_Session_i::GetInterface() called, starting GUI...");
166 Kills the session if there are no active studies nore GUI
168 class CloseEvent : public SALOME_Event
171 virtual void Execute() {
172 if ( SUIT_Session::session() )
173 SUIT_Session::session()->closeSession( SUIT_Session::DONT_SAVE );
178 Stop session (close all GUI windows)
180 void SALOME_Session_i::StopSession()
184 if ( SUIT_Session::session() ) {
185 ProcessVoidEvent( new CloseEvent() );
190 void SALOME_Session_i::Shutdown()
193 bool isBeingShuttingDown = _isShuttingDown;
194 _isShuttingDown = true;
196 if ( !isBeingShuttingDown ) {
197 if ( SUIT_Session::session() ) {
198 ProcessVoidEvent( new CloseEvent() );
201 _GUILauncher->wakeAll();
207 Send a SALOME::StatSession structure (see idl) to the client
213 QtLock() { if ( qApp ) qApp->lock(); }
214 ~QtLock() { if ( qApp ) qApp->unlock(); }
218 SALOME::StatSession SALOME_Session_i::GetStatSession()
220 // update Session state
226 _isGUI = SUIT_Session::session();
227 if ( _isGUI && SUIT_Session::session()->activeApplication() )
228 activeStudy = SUIT_Session::session()->activeApplication()->getNbStudies();
232 SALOME::StatSession_var myStats = new SALOME::StatSession;
234 myStats->state = SALOME::running ;
235 else if (_isShuttingDown)
236 myStats->state = SALOME::shutdown ;
238 myStats->state = SALOME::asleep ;
239 myStats->activeGUI = _isGUI ;
243 return myStats._retn() ;
246 CORBA::Long SALOME_Session_i::getPID() {
255 char* SALOME_Session_i::getHostname()
257 std::string aHostName = Kernel_Utils::GetHostname();
258 return CORBA::string_dup( aHostName.data() );
261 bool SALOME_Session_i::restoreVisualState(CORBA::Long theSavePoint)
263 class TEvent: public SALOME_Event {
266 TEvent(int savePoint) { _savePoint = savePoint; }
267 virtual void Execute() {
268 SUIT_Study* study = SUIT_Session::session()->activeApplication()->activeStudy();
270 study->restoreState(_savePoint);
275 if(SUIT_Session::session() && SUIT_Session::session()->activeApplication() ) {
276 SUIT_Study* study = SUIT_Session::session()->activeApplication()->activeStudy();
277 if(!study) SUIT_Session::session()->activeApplication()->createEmptyStudy();
278 ProcessVoidEvent( new TEvent(theSavePoint) );
285 void SALOME_Session_i::emitMessage(const char* theMessage)
287 class TEvent: public SALOME_Event {
289 TEvent(const char * msg) {
292 virtual void Execute() {
293 SUIT_Session::session()->activeApplication()->desktop()->emitMessage(_msg);
298 if ( SUIT_Session::session() ) {
299 if ( SUIT_Session::session()->activeApplication() ) {
300 if ( SUIT_Session::session()->activeApplication()->desktop() ) {
301 ProcessVoidEvent( new TEvent(theMessage) );
304 MESSAGE("try to emit message '"<<theMessage<<"' but there is no desktop");
308 MESSAGE("try to emit message '"<<theMessage<<"' but there is no application");
312 MESSAGE("try to emit message '"<<theMessage<<"' but there is no session");
316 void SALOME_Session_i::emitMessageOneWay(const char* theMessage)
318 emitMessage(theMessage);
321 SALOME::StringSeq* SALOME_Session_i::getSelection()
323 SALOME::StringSeq_var selection = new SALOME::StringSeq;
325 if ( SUIT_Session::session() ) {
326 LightApp_Application* app = dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() );
328 LightApp_SelectionMgr* selMgr = dynamic_cast<LightApp_SelectionMgr*>( app->selectionMgr() );
329 SALOME_ListIO selected;
330 selMgr->selectedObjects( selected );
331 selection->length( selected.Extent() );
333 for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
334 Handle( SALOME_InteractiveObject ) io = it.Value();
335 if ( io->hasEntry() )
336 selection[nbSel++] = CORBA::string_dup( io->getEntry() );
338 selection->length( nbSel );
342 return selection._retn();