]> SALOME platform Git repositories - modules/gui.git/blob - src/Session/Session_Session_i.cxx
Salome HOME
1e337b0352994213cc57309adc8bfe2c71e0283d
[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   constructor
50 */
51 SALOME_Session_i::SALOME_Session_i(int argc,
52                                    char ** argv,
53                                    CORBA::ORB_ptr orb,
54                                    PortableServer::POA_ptr poa,
55                                    QMutex* GUIMutex,
56                                    QWaitCondition* GUILauncher)
57 {
58   _argc = argc ;
59   _argv = argv ;
60   _isGUI = FALSE ;
61   _runningStudies= 0 ;
62   _orb = CORBA::ORB::_duplicate(orb) ;
63   _poa = PortableServer::POA::_duplicate(poa) ;
64   _GUIMutex = GUIMutex;
65   _GUILauncher = GUILauncher;
66   //MESSAGE("constructor end");
67 }
68
69 /*!
70   returns Visu component
71 */
72 Engines::Component_ptr SALOME_Session_i::GetComponent(const char* theLibraryName)
73 {
74   typedef Engines::Component_ptr TGetImpl(CORBA::ORB_ptr,
75                                           PortableServer::POA_ptr,
76                                           SALOME_NamingService*,QMutex*);
77   OSD_SharedLibrary  aSharedLibrary(const_cast<char*>(theLibraryName));
78   if(aSharedLibrary.DlOpen(OSD_RTLD_LAZY))
79     if(OSD_Function anOSDFun = aSharedLibrary.DlSymb("GetImpl"))
80       return ((TGetImpl (*)) anOSDFun)(_orb,_poa,_NS,_GUIMutex);
81   return Engines::Component::_nil();
82 }
83
84 /*!
85   destructor
86 */
87 SALOME_Session_i::~SALOME_Session_i()
88 {
89   //MESSAGE("destructor end");
90 }
91
92 /*!
93   tries to find the Corba Naming Service and to register the session,
94   gives naming service interface to _IAPPThread
95 */
96 void SALOME_Session_i::NSregister()
97 {
98   SALOME::Session_ptr pSession = SALOME::Session::_narrow(_this());
99   try
100     {
101       _NS = new SALOME_NamingService(_orb);
102       _NS->Register(pSession, "/Kernel/Session");
103     }
104   catch (ServiceUnreachable&)
105     {
106       INFOS("Caught exception: Naming Service Unreachable");
107       exit(1) ;
108     }
109   catch (...)
110     {
111       INFOS("Caught unknown exception from Naming Service");
112     }
113   //MESSAGE("Session registered in Naming Service");
114 }
115
116 /*!
117   Launches the GUI if there is none.
118   The Corba method is oneway (corba client does'nt wait for GUI completion)
119 */
120 void SALOME_Session_i::GetInterface()
121 {
122   _GUIMutex->lock();
123   _GUIMutex->unlock();
124   if ( !SUIT_Session::session() )
125   {
126     _GUILauncher->wakeAll();
127     MESSAGE("SALOME_Session_i::GetInterface() called, starting GUI...")
128       }
129 }
130
131 /*!
132   Kills the session if there are no active studies nore GUI
133 */
134 class CloseEvent : public SALOME_Event
135 {
136 public:
137   virtual void Execute() {
138     SUIT_Session* session = SUIT_Session::session();
139     session->closeSession( SUIT_Session::DONT_SAVE );
140     //if ( SUIT_Application::getDesktop() )
141     //  QAD_Application::getDesktop()->closeDesktop( true );
142   }
143 };
144
145 /*!
146   Processes event to close session
147 */
148 void SALOME_Session_i::StopSession()
149 {
150   ProcessVoidEvent( new CloseEvent() );
151 }
152
153 /*!
154   Send a SALOME::StatSession structure (see idl) to the client
155   (number of running studies and presence of GUI)
156 */
157 class QtLock
158 {
159 public:
160   QtLock() { if ( qApp ) qApp->lock(); }
161   ~QtLock() { if ( qApp ) qApp->unlock(); }
162 };
163
164
165 SALOME::StatSession SALOME_Session_i::GetStatSession()
166 {
167   // update Session state
168   _GUIMutex->lock();
169
170   _runningStudies = 0;
171   {
172     QtLock lock;
173     _isGUI = SUIT_Session::session();
174     if ( _isGUI && SUIT_Session::session()->activeApplication() )
175       _runningStudies = SUIT_Session::session()->activeApplication()->getNbStudies();
176   }
177
178   _GUIMutex->unlock();
179
180   // getting stat info
181   SALOME::StatSession_var myStats = new SALOME::StatSession ;
182   if (_runningStudies)
183     myStats->state = SALOME::running ;
184   else
185     myStats->state = SALOME::asleep ;
186   myStats->runningStudies = _runningStudies ;
187   myStats->activeGUI = _isGUI ;
188   return myStats._retn() ;
189 }
190
191 CORBA::Long SALOME_Session_i::GetActiveStudyId()
192 {
193   long aStudyId=-1;
194   if ( SUIT_Session::session() && SUIT_Session::session()->activeApplication() ) {
195     if ( SUIT_Session::session()->activeApplication()->activeStudy() ) // mkr : IPAL12128
196       aStudyId = SUIT_Session::session()->activeApplication()->activeStudy()->id();
197   }
198   return aStudyId;
199 }
200
201 bool SALOME_Session_i::restoreVisualState(CORBA::Long theSavePoint)
202 {
203   class TEvent: public SALOME_Event {
204     int _savePoint;
205   public:
206     TEvent(int savePoint) { _savePoint = savePoint; }
207     virtual void Execute() {
208       SUIT_Study* study = SUIT_Session::session()->activeApplication()->activeStudy();
209       if ( study ) {
210         study->restoreState(_savePoint);
211       }
212     }
213   };
214   
215   if(SUIT_Session::session() && SUIT_Session::session()->activeApplication() ) {
216     SUIT_Study* study = SUIT_Session::session()->activeApplication()->activeStudy();
217     if(!study) SUIT_Session::session()->activeApplication()->createEmptyStudy();      
218     ProcessVoidEvent( new TEvent(theSavePoint) );
219     return true;
220   }
221  
222   return false;
223 }