Salome HOME
#18963 Minimize compiler warnings
[modules/gui.git] / src / Session / Session_Session_i.cxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SALOME_Session_i.cxx
23 //  Author : Paul RASCLE, EDF
24 //  Module : SALOME
25
26 #include "utilities.h"
27
28 #include "Session_Session_i.hxx"
29
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"
39
40 #include "Basics_Utils.hxx"
41
42 #include <QMutex>
43 #include <QWaitCondition>
44
45 // Open CASCADE Includes
46 #include <OSD_SharedLibrary.hxx>
47 #include <OSD_Function.hxx>
48
49 #ifdef WIN32
50 # include <process.h>
51 #else
52 #include <unistd.h>
53 #endif
54
55 /*!
56   constructor
57 */
58 SALOME_Session_i::SALOME_Session_i(int argc,
59                                    char ** argv,
60                                    CORBA::ORB_ptr orb,
61                                    PortableServer::POA_ptr poa,
62                                    QMutex* GUIMutex,
63                                    QWaitCondition* GUILauncher)
64 {
65   _argc = argc ;
66   _argv = argv ;
67   _isGUI = false ;
68   _orb = CORBA::ORB::_duplicate(orb) ;
69   _poa = PortableServer::POA::_duplicate(poa) ;
70   _GUIMutex = GUIMutex;
71   _GUILauncher = GUILauncher;
72   _NS = new SALOME_NamingService(_orb);
73   _isShuttingDown = false;
74   //MESSAGE("constructor end");
75 }
76
77 /*!
78   returns Visu component
79 */
80 Engines::EngineComponent_ptr SALOME_Session_i::GetComponent(const char* theLibraryName)
81 {
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);
89   }
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();
94   }
95   return Engines::EngineComponent::_nil();
96 }
97
98 /*!
99   destructor
100 */
101 SALOME_Session_i::~SALOME_Session_i()
102 {
103   delete _NS;
104   //MESSAGE("destructor end");
105 }
106
107 /*!
108   tries to find the Corba Naming Service and to register the session,
109   gives naming service interface to _IAPPThread
110 */
111 void SALOME_Session_i::NSregister()
112 {
113   CORBA::Object_var obref=_this();
114   SALOME::Session_var pSession = SALOME::Session::_narrow(obref);
115   try
116     {
117       _NS->Register(pSession, "/Kernel/Session");
118     }
119   catch (ServiceUnreachable&)
120     {
121       INFOS("Caught exception: Naming Service Unreachable");
122       exit(1) ;
123     }
124   catch (...)
125     {
126       INFOS("Caught unknown exception from Naming Service");
127     }
128   //MESSAGE("Session registered in Naming Service");
129 }
130
131 /*!
132   Unregister session server from CORBA Naming Service
133 */
134 void SALOME_Session_i::NSunregister()
135 {
136   try
137     {
138       _NS->Destroy_Name("/Kernel/Session");
139       _NS->Destroy_Directory("/Kernel");
140       deleteContainersinNS();
141     }
142   catch (ServiceUnreachable&)
143     {
144       INFOS("Caught exception: Naming Service Unreachable");
145     }
146   catch (...)
147     {
148       INFOS("Caught unknown exception from Naming Service");
149     }
150 }
151
152 /*!
153   Launches the GUI if there is none.
154   The Corba method is oneway (corba client does'nt wait for GUI completion)
155 */
156 void SALOME_Session_i::GetInterface()
157 {
158   _GUIMutex->lock();
159   _GUIMutex->unlock();
160   if ( !SUIT_Session::session() )
161   {
162     _GUILauncher->wakeAll();
163     MESSAGE("SALOME_Session_i::GetInterface() called, starting GUI...");
164   }
165 }
166
167 /*!
168   Kills the session if there are no active studies nore GUI
169 */
170 class CloseEvent : public SALOME_Event
171 {
172 public:
173   virtual void Execute() {
174     if ( SUIT_Session::session() )
175       SUIT_Session::session()->closeSession( SUIT_Session::DONT_SAVE );
176   }
177 };
178
179 /*!
180   Stop session (close all GUI windows)
181 */
182 void SALOME_Session_i::StopSession()
183 {
184   _GUIMutex->lock();
185   _GUIMutex->unlock();
186   if ( SUIT_Session::session() ) {
187     ProcessVoidEvent( new CloseEvent() );
188   }
189 }
190
191 //! Shutdown session
192 void SALOME_Session_i::Shutdown()
193 {
194   _GUIMutex->lock();
195   bool isBeingShuttingDown = _isShuttingDown;
196   _isShuttingDown = true;
197   _GUIMutex->unlock();
198   if ( !isBeingShuttingDown ) {
199     if ( SUIT_Session::session() ) {
200       ProcessVoidEvent( new CloseEvent() );
201     }
202     else {
203       _GUILauncher->wakeAll();
204     }
205   }
206 }
207
208 /*!
209   Send a SALOME::StatSession structure (see idl) to the client
210   (presence of GUI)
211 */
212 /*class QtLock
213 {
214 public:
215   QtLock() { if ( qApp ) qApp->lock(); }
216   ~QtLock() { if ( qApp ) qApp->unlock(); }
217 };*/
218
219
220 SALOME::StatSession SALOME_Session_i::GetStatSession()
221 {
222   // update Session state
223   _GUIMutex->lock();
224   int activeStudy = 0;
225
226   {
227     //QtLock lock;
228     _isGUI = SUIT_Session::session();
229     if ( _isGUI && SUIT_Session::session()->activeApplication() )
230       activeStudy = SUIT_Session::session()->activeApplication()->getNbStudies();
231   }
232
233   // getting stat info
234   SALOME::StatSession_var myStats = new SALOME::StatSession;
235   if (activeStudy)
236     myStats->state = SALOME::running ;
237   else if (_isShuttingDown)
238     myStats->state = SALOME::shutdown ;
239   else
240     myStats->state = SALOME::asleep ;
241   myStats->activeGUI = _isGUI ;
242
243   _GUIMutex->unlock();
244
245   return myStats._retn() ;
246 }
247
248 CORBA::Long SALOME_Session_i::getPID() {
249   return (CORBA::Long)
250 #ifndef WIN32
251     getpid();
252 #else
253     _getpid();
254 #endif
255 }
256
257 char* SALOME_Session_i::getHostname()
258 {
259   std::string aHostName = Kernel_Utils::GetHostname();
260   return CORBA::string_dup( aHostName.data() );
261 }
262
263 bool SALOME_Session_i::restoreVisualState(CORBA::Long theSavePoint)
264 {
265   class TEvent: public SALOME_Event {
266     int _savePoint;
267   public:
268     TEvent(int savePoint) { _savePoint = savePoint; }
269     virtual void Execute() {
270       SUIT_Study* study = SUIT_Session::session()->activeApplication()->activeStudy();
271       if ( study ) {
272         study->restoreState(_savePoint);
273       }
274     }
275   };
276   
277   if(SUIT_Session::session() && SUIT_Session::session()->activeApplication() ) {
278     SUIT_Study* study = SUIT_Session::session()->activeApplication()->activeStudy();
279     if(!study) SUIT_Session::session()->activeApplication()->createEmptyStudy();      
280     ProcessVoidEvent( new TEvent(theSavePoint) );
281     return true;
282   }
283  
284   return false;
285 }
286
287 void SALOME_Session_i::emitMessage(const char* theMessage)
288 {
289   class TEvent: public SALOME_Event {
290   public:
291     TEvent(const char * msg) {
292       _msg = msg;
293     }
294     virtual void Execute() {
295       SUIT_Session::session()->activeApplication()->desktop()->emitMessage(_msg);
296     }
297   private:
298     const char* _msg;
299   };
300   if ( SUIT_Session::session() ) {
301     if ( SUIT_Session::session()->activeApplication() ) {
302       if ( SUIT_Session::session()->activeApplication()->desktop() ) {
303         ProcessVoidEvent( new TEvent(theMessage) );
304       }
305       else {
306         MESSAGE("try to emit message '"<<theMessage<<"' but there is no desktop");
307       }
308     }
309     else {
310       MESSAGE("try to emit message '"<<theMessage<<"' but there is no application");
311     }
312   }
313   else {
314     MESSAGE("try to emit message '"<<theMessage<<"' but there is no session");
315   }
316 }
317
318 void SALOME_Session_i::emitMessageOneWay(const char* theMessage)
319 {
320   emitMessage(theMessage);
321 }
322
323 SALOME::StringSeq* SALOME_Session_i::getSelection()
324 {
325   SALOME::StringSeq_var selection = new SALOME::StringSeq;
326   _GUIMutex->lock();
327   if ( SUIT_Session::session() ) {
328     LightApp_Application* app = dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() );
329     if ( app ) {
330       LightApp_SelectionMgr* selMgr = dynamic_cast<LightApp_SelectionMgr*>( app->selectionMgr() );
331       SALOME_ListIO selected;
332       selMgr->selectedObjects( selected );
333       selection->length( selected.Extent() );
334       int nbSel = 0;
335       for ( SALOME_ListIteratorOfListIO it( selected ); it.More(); it.Next() ) {
336         Handle( SALOME_InteractiveObject ) io = it.Value();
337         if ( io->hasEntry() )
338           selection[nbSel++] = CORBA::string_dup( io->getEntry() );
339       }
340       selection->length( nbSel );
341     }
342   }
343   _GUIMutex->unlock();
344   return selection._retn();
345 }
346
347 void SALOME_Session_i::deleteContainersinNS()
348 {
349 // destroy of all containers and modules
350   _NS->Change_Directory("/Containers");
351   std::vector<std::string> machines = _NS->list_subdirs();
352   for(int i=0;i<(int)machines.size();i++){
353     _NS->Change_Directory(machines[i].c_str());
354     std::vector<std::string> toto = _NS->list_directory();
355     for(int j=0;j<(int)toto.size();j++)
356       _NS->Destroy_Name(toto[j].c_str());
357     std::vector<std::string> containers = _NS->list_subdirs();
358     for(int j=0;j<(int)containers.size();j++){
359       _NS->Change_Directory(containers[j].c_str());
360       std::vector<std::string> modules = _NS->list_directory();
361       for(int k=0;k<(int)modules.size();k++)
362         _NS->Destroy_Name(modules[k].c_str());
363       _NS->Change_Directory("/Containers");
364       _NS->Change_Directory(machines[i].c_str());
365       _NS->Destroy_Directory(containers[j].c_str());
366     }
367     _NS->Change_Directory("/Containers");
368     _NS->Destroy_Directory(machines[i].c_str());
369   }
370   _NS->Change_Directory("/");
371   _NS->Destroy_Directory("/Containers");
372 }