Salome HOME
0e08eb3e52222fd1f237681720d766301b469953
[modules/gui.git] / src / Session / Session_Session_i.cxx
1 // Copyright (C) 2007-2014  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
33 #include "SUIT_Session.h"
34 #include "SUIT_Desktop.h"
35 #include "SUIT_Study.h"
36
37 #include "Basics_Utils.hxx"
38
39 #include <QMutex>
40 #include <QWaitCondition>
41
42 // Open CASCADE Includes
43 #include <OSD_SharedLibrary.hxx>
44 #include <OSD_Function.hxx>
45
46 #ifdef WIN32
47 # include <process.h>
48 #else
49 #include <unistd.h>
50 #endif
51
52 /*!
53   constructor
54 */
55 SALOME_Session_i::SALOME_Session_i(int argc,
56                                    char ** argv,
57                                    CORBA::ORB_ptr orb,
58                                    PortableServer::POA_ptr poa,
59                                    QMutex* GUIMutex,
60                                    QWaitCondition* GUILauncher)
61 {
62   _argc = argc ;
63   _argv = argv ;
64   _isGUI = FALSE ;
65   _runningStudies= 0 ;
66   _orb = CORBA::ORB::_duplicate(orb) ;
67   _poa = PortableServer::POA::_duplicate(poa) ;
68   _GUIMutex = GUIMutex;
69   _GUILauncher = GUILauncher;
70   _NS = new SALOME_NamingService(_orb);
71   _isShuttingDown = false;
72   //MESSAGE("constructor end");
73 }
74
75 /*!
76   returns Visu component
77 */
78 Engines::EngineComponent_ptr SALOME_Session_i::GetComponent(const char* theLibraryName)
79 {
80   typedef Engines::EngineComponent_ptr TGetImpl(CORBA::ORB_ptr,
81                                                 PortableServer::POA_ptr,
82                                                 SALOME_NamingService*,QMutex*);
83   OSD_SharedLibrary  aSharedLibrary(const_cast<char*>(theLibraryName));
84   if(aSharedLibrary.DlOpen(OSD_RTLD_LAZY))
85     if(OSD_Function anOSDFun = aSharedLibrary.DlSymb("GetImpl"))
86       return ((TGetImpl (*)) anOSDFun)(_orb,_poa,_NS,_GUIMutex);
87   return Engines::EngineComponent::_nil();
88 }
89
90 /*!
91   destructor
92 */
93 SALOME_Session_i::~SALOME_Session_i()
94 {
95   delete _NS;
96   //MESSAGE("destructor end");
97 }
98
99 /*!
100   tries to find the Corba Naming Service and to register the session,
101   gives naming service interface to _IAPPThread
102 */
103 void SALOME_Session_i::NSregister()
104 {
105   CORBA::Object_var obref=_this();
106   SALOME::Session_var pSession = SALOME::Session::_narrow(obref);
107   try
108     {
109       _NS->Register(pSession, "/Kernel/Session");
110     }
111   catch (ServiceUnreachable&)
112     {
113       INFOS("Caught exception: Naming Service Unreachable");
114       exit(1) ;
115     }
116   catch (...)
117     {
118       INFOS("Caught unknown exception from Naming Service");
119     }
120   //MESSAGE("Session registered in Naming Service");
121 }
122
123 /*!
124   Unregister session server from CORBA Naming Service
125 */
126 void SALOME_Session_i::NSunregister()
127 {
128   try
129     {
130       _NS->Destroy_Name("/Kernel/Session");
131     }
132   catch (ServiceUnreachable&)
133     {
134       INFOS("Caught exception: Naming Service Unreachable");
135     }
136   catch (...)
137     {
138       INFOS("Caught unknown exception from Naming Service");
139     }
140 }
141
142 /*!
143   Launches the GUI if there is none.
144   The Corba method is oneway (corba client does'nt wait for GUI completion)
145 */
146 void SALOME_Session_i::GetInterface()
147 {
148   _GUIMutex->lock();
149   _GUIMutex->unlock();
150   if ( !SUIT_Session::session() )
151   {
152     _GUILauncher->wakeAll();
153     MESSAGE("SALOME_Session_i::GetInterface() called, starting GUI...");
154   }
155 }
156
157 /*!
158   Kills the session if there are no active studies nore GUI
159 */
160 class CloseEvent : public SALOME_Event
161 {
162 public:
163   virtual void Execute() {
164     if ( SUIT_Session::session() )
165       SUIT_Session::session()->closeSession( SUIT_Session::DONT_SAVE );
166   }
167 };
168
169 /*!
170   Stop session (close all GUI windows)
171 */
172 void SALOME_Session_i::StopSession()
173 {
174   _GUIMutex->lock();
175   _GUIMutex->unlock();
176   if ( SUIT_Session::session() ) {
177     ProcessVoidEvent( new CloseEvent() );
178   }
179 }
180
181 //! Shutdown session
182 void SALOME_Session_i::Shutdown()
183 {
184   _GUIMutex->lock();
185   bool isBeingShuttingDown = _isShuttingDown;
186   _isShuttingDown = true;
187   _GUIMutex->unlock();
188   if ( !isBeingShuttingDown ) {
189     if ( SUIT_Session::session() ) {
190       ProcessVoidEvent( new CloseEvent() );
191     }
192     else {
193       _GUILauncher->wakeAll();
194     }
195   }
196 }
197
198 /*!
199   Send a SALOME::StatSession structure (see idl) to the client
200   (number of running studies and presence of GUI)
201 */
202 /*class QtLock
203 {
204 public:
205   QtLock() { if ( qApp ) qApp->lock(); }
206   ~QtLock() { if ( qApp ) qApp->unlock(); }
207 };*/
208
209
210 SALOME::StatSession SALOME_Session_i::GetStatSession()
211 {
212   // update Session state
213   _GUIMutex->lock();
214
215   _runningStudies = 0;
216   {
217     //QtLock lock;
218     _isGUI = SUIT_Session::session();
219     if ( _isGUI && SUIT_Session::session()->activeApplication() )
220       _runningStudies = SUIT_Session::session()->activeApplication()->getNbStudies();
221   }
222
223   // getting stat info
224   SALOME::StatSession_var myStats = new SALOME::StatSession ;
225   if (_runningStudies)
226     myStats->state = SALOME::running ;
227   else if (_isShuttingDown)
228     myStats->state = SALOME::shutdown ;
229   else
230     myStats->state = SALOME::asleep ;
231   myStats->runningStudies = _runningStudies ;
232   myStats->activeGUI = _isGUI ;
233
234   _GUIMutex->unlock();
235
236   return myStats._retn() ;
237 }
238
239 CORBA::Long SALOME_Session_i::GetActiveStudyId()
240 {
241   long aStudyId=-1;
242   if ( SUIT_Session::session() && SUIT_Session::session()->activeApplication() ) {
243     if ( SUIT_Session::session()->activeApplication()->activeStudy() ) // mkr : IPAL12128
244       aStudyId = SUIT_Session::session()->activeApplication()->activeStudy()->id();
245   }
246   return aStudyId;
247 }
248
249 CORBA::Long SALOME_Session_i::getPID() {
250   return (CORBA::Long)
251 #ifndef WIN32
252     getpid();
253 #else
254     _getpid();
255 #endif
256 }
257
258 char* SALOME_Session_i::getHostname()
259 {
260   std::string aHostName = Kernel_Utils::GetHostname();
261   return CORBA::string_dup( aHostName.data() );
262 }
263
264 bool SALOME_Session_i::restoreVisualState(CORBA::Long theSavePoint)
265 {
266   class TEvent: public SALOME_Event {
267     int _savePoint;
268   public:
269     TEvent(int savePoint) { _savePoint = savePoint; }
270     virtual void Execute() {
271       SUIT_Study* study = SUIT_Session::session()->activeApplication()->activeStudy();
272       if ( study ) {
273         study->restoreState(_savePoint);
274       }
275     }
276   };
277   
278   if(SUIT_Session::session() && SUIT_Session::session()->activeApplication() ) {
279     SUIT_Study* study = SUIT_Session::session()->activeApplication()->activeStudy();
280     if(!study) SUIT_Session::session()->activeApplication()->createEmptyStudy();      
281     ProcessVoidEvent( new TEvent(theSavePoint) );
282     return true;
283   }
284  
285   return false;
286 }
287
288 void SALOME_Session_i::emitMessage(const char* theMessage)
289 {
290   class TEvent: public SALOME_Event {
291   public:
292     TEvent(const char * msg) {
293       _msg = msg;
294     }
295     virtual void Execute() {
296       SUIT_Session::session()->activeApplication()->desktop()->emitMessage(_msg);
297     }
298   private:
299     const char* _msg;
300   };
301   if ( SUIT_Session::session() ) {
302     if ( SUIT_Session::session()->activeApplication() ) {
303       if ( SUIT_Session::session()->activeApplication()->desktop() ) {
304         ProcessVoidEvent( new TEvent(theMessage) );
305       }
306       else {
307         MESSAGE("try to emit message '"<<theMessage<<"' but there is no desktop");
308       }
309     }
310     else {
311       MESSAGE("try to emit message '"<<theMessage<<"' but there is no application");
312     }
313   }
314   else {
315     MESSAGE("try to emit message '"<<theMessage<<"' but there is no session");
316   }
317 }
318
319 void SALOME_Session_i::emitMessageOneWay(const char* theMessage)
320 {
321   emitMessage(theMessage);
322 }