Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/gui.git] / src / Session / Session_Session_i.cxx
1 //  SALOME Session : implementation of Session.idl
2 //
3 //  Copyright (C) 2003  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.
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : SALOME_Session_i.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 #include "utilities.h"
30
31 #include "Session_Session_i.hxx"
32
33 #include "SALOME_NamingService.hxx"
34 #include "SALOME_Event.hxx"
35
36 #include "SUIT_Session.h"
37 #include "SUIT_Application.h"
38
39 #include <qapplication.h>
40
41 // Open CASCADE Includes
42 #include <OSD_SharedLibrary.hxx>
43 #include <OSD_LoadMode.hxx>
44 #include <OSD_Function.hxx>
45
46 using namespace std;
47
48 //=============================================================================
49 /*! SALOME_Session_i
50  *  constructor
51  */
52 //=============================================================================
53
54 SALOME_Session_i::SALOME_Session_i(int argc,
55                                    char ** argv,
56                                    CORBA::ORB_ptr orb,
57                                    PortableServer::POA_ptr poa,
58                                    QMutex* GUIMutex,
59                                    QWaitCondition* GUILauncher)
60 {
61   _argc = argc ;
62   _argv = argv ;
63   _isGUI = FALSE ;
64   _runningStudies= 0 ;
65   _orb = CORBA::ORB::_duplicate(orb) ;
66   _poa = PortableServer::POA::_duplicate(poa) ;
67   _GUIMutex = GUIMutex;
68   _GUILauncher = GUILauncher;
69   //MESSAGE("constructor end");
70 }
71
72 //=============================================================================
73 /*! GetVisuComponent
74  *  returns Visu component
75  */
76 //=============================================================================
77
78 Engines::Component_ptr SALOME_Session_i::GetComponent(const char* theLibraryName)
79 {
80   typedef Engines::Component_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::Component::_nil();
88 }
89
90 //=============================================================================
91 /*! ~SALOME_Session_i
92  *  destructor
93  */
94 //=============================================================================
95
96 SALOME_Session_i::~SALOME_Session_i()
97 {
98   //MESSAGE("destructor end");
99 }
100
101 //=============================================================================
102 /*! NSregister
103  *  tries to find the Corba Naming Service and to register the session,
104  *  gives naming service interface to _IAPPThread
105  */
106 //=============================================================================
107
108 void SALOME_Session_i::NSregister()
109 {
110   SALOME::Session_ptr pSession = SALOME::Session::_narrow(_this());
111   try
112     {
113       _NS = new SALOME_NamingService(_orb);
114       _NS->Register(pSession, "/Kernel/Session");
115     }
116   catch (ServiceUnreachable&)
117     {
118       INFOS("Caught exception: Naming Service Unreachable");
119       exit(1) ;
120     }
121   catch (...)
122     {
123       INFOS("Caught unknown exception from Naming Service");
124     }
125   //MESSAGE("Session registered in Naming Service");
126 }
127
128 //=============================================================================
129 /*! GetInterface
130  *  Launches the GUI if there is none.
131  *  The Corba method is oneway (corba client does'nt wait for GUI completion)
132  */
133 //=============================================================================
134
135 void SALOME_Session_i::GetInterface()
136 {
137   _GUIMutex->lock();
138   _GUIMutex->unlock();
139   if ( !SUIT_Session::session() )
140   {
141     _GUILauncher->wakeAll();
142     MESSAGE("SALOME_Session_i::GetInterface() called, starting GUI...")
143       }
144 }
145
146 //=============================================================================
147 /*! StopSession
148  *  Kills the session if there are no active studies nore GUI
149  */
150 //=============================================================================
151 class CloseEvent : public SALOME_Event
152 {
153 public:
154   virtual void Execute() {
155     SUIT_Session* session = SUIT_Session::session();
156     session->closeSession( SUIT_Session::DONT_SAVE );
157     //if ( SUIT_Application::getDesktop() )
158     //  QAD_Application::getDesktop()->closeDesktop( true );
159   }
160 };
161
162 void SALOME_Session_i::StopSession()
163 {
164   ProcessVoidEvent( new CloseEvent() );
165 }
166
167 //=============================================================================
168 /*! StatSession
169  *  Send a SALOME::StatSession structure (see idl) to the client
170  *  (number of running studies and presence of GUI)
171  */
172 //=============================================================================
173
174 class QtLock
175 {
176 public:
177   QtLock() { if ( qApp ) qApp->lock(); }
178   ~QtLock() { if ( qApp ) qApp->unlock(); }
179 };
180
181
182 SALOME::StatSession SALOME_Session_i::GetStatSession()
183 {
184   // update Session state
185   _GUIMutex->lock();
186
187   _runningStudies = 0;
188   {
189     QtLock lock;
190     _isGUI = SUIT_Session::session();
191     if ( _isGUI && SUIT_Session::session()->activeApplication() )
192       _runningStudies = SUIT_Session::session()->activeApplication()->getNbStudies();
193   }
194
195   _GUIMutex->unlock();
196
197   // getting stat info
198   SALOME::StatSession_var myStats = new SALOME::StatSession ;
199   if (_runningStudies)
200     myStats->state = SALOME::running ;
201   else
202     myStats->state = SALOME::asleep ;
203   myStats->runningStudies = _runningStudies ;
204   myStats->activeGUI = _isGUI ;
205   return myStats._retn() ;
206 }
207
208 CORBA::Long SALOME_Session_i::GetActiveStudyId()
209 {
210   long aStudyId=-1;
211   if ( SUIT_Session::session() && SUIT_Session::session()->activeApplication() ) {
212     aStudyId = SUIT_Session::session()->activeApplication()->activeStudy()->id();
213   }
214   return aStudyId;
215 }
216
217 bool SALOME_Session_i::restoreVisualState(CORBA::Long theSavePoint)
218 {
219   class TEvent: public SALOME_Event {
220     int _savePoint;
221   public:
222     TEvent(int savePoint) { _savePoint = savePoint; }
223     virtual void Execute() {
224       SUIT_Study* study = SUIT_Session::session()->activeApplication()->activeStudy();
225       if ( study ) {
226         study->restoreState(_savePoint);
227       }
228     }
229   };
230   
231   if(SUIT_Session::session() && SUIT_Session::session()->activeApplication() ) {
232     SUIT_Study* study = SUIT_Session::session()->activeApplication()->activeStudy();
233     if(!study) SUIT_Session::session()->activeApplication()->createEmptyStudy();      
234     ProcessVoidEvent( new TEvent(theSavePoint) );
235     return true;
236   }
237  
238   return false;
239 }