1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 #include "SUIT_Session.h"
21 #include "SUIT_Tools.h"
22 #include "SUIT_Desktop.h"
23 #include "SUIT_MessageBox.h"
24 #include "SUIT_ViewWindow.h"
25 #include "SUIT_ViewManager.h"
26 #include "SUIT_ExceptionHandler.h"
28 #include <qtextcodec.h>
29 #include <qmessagebox.h>
30 #include <qapplication.h>
38 SUIT_Session* SUIT_Session::mySession = 0;
42 SUIT_Session::SUIT_Session()
47 myExitStatus( FROM_GUI )
49 SUIT_ASSERT( !mySession )
53 myAppList.setAutoDelete( true );
56 /*!destructor. Clear applications list and set mySession to zero.*/
57 SUIT_Session::~SUIT_Session()
64 /*! \retval return mySession */
65 SUIT_Session* SUIT_Session::session()
71 Starts new application using "createApplication" function of loaded DLL.
74 SUIT_Application* SUIT_Session::startApplication( const QString& name, int args, char** argv )
78 QString appName = applicationName( name );
79 if ( myAppLibs.contains( appName ) )
80 libHandle = myAppLibs[appName];
84 libHandle = loadLibrary( name, lib );
88 SUIT_MessageBox::warn1( 0, tr( "Error" ),
89 tr( "Can not load application library \"%1\": %2").arg( lib ).arg( lastError() ), tr( "Ok" ) );
93 if ( !myAppLibs.contains( appName ) || !myAppLibs[appName] ) // jfa 22.06.2005
94 myAppLibs.insert( appName, libHandle );
96 APP_CREATE_FUNC crtInst = 0;
99 crtInst = (APP_CREATE_FUNC)::GetProcAddress( libHandle, APP_CREATE_NAME );
101 crtInst = (APP_CREATE_FUNC)dlsym( libHandle, APP_CREATE_NAME );
106 SUIT_MessageBox::warn1( 0, tr( "Error" ),
107 tr( "Can not find function \"%1\": %2" ).arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
111 // Prepare Resource Manager for the new application if it doesn't exist yet
114 myResMgr = createResourceMgr( appName );
115 myResMgr->loadLanguage();
118 //jfa 22.06.2005:SUIT_Application* anApp = crtInst( args, argv );
119 SUIT_Application* anApp = crtInst();
122 SUIT_MessageBox::warn1( 0, tr( "Error" ), tr( "Can not create application \"%1\": %2").arg( appName ).arg( lastError() ), tr( "Ok" ) );
126 anApp->setName( appName );
128 connect( anApp, SIGNAL( applicationClosed( SUIT_Application* ) ),
129 this, SLOT( onApplicationClosed( SUIT_Application* ) ) );
130 connect( anApp, SIGNAL( activated( SUIT_Application* ) ),
131 this, SLOT( onApplicationActivated( SUIT_Application* ) ) );
133 myAppList.append( anApp );
137 APP_GET_HANDLER_FUNC crtHndlr = 0;
139 crtHndlr = (APP_GET_HANDLER_FUNC)::GetProcAddress( libHandle, APP_GET_HANDLER_NAME );
141 crtHndlr = (APP_GET_HANDLER_FUNC)dlsym( libHandle, APP_GET_HANDLER_NAME );
144 myHandler = crtHndlr();
149 // Application can be closed during starting (not started).
150 if ( !myAppList.contains( anApp ) )
157 Gets the list of all applications
159 QPtrList<SUIT_Application> SUIT_Session::applications() const
161 QPtrList<SUIT_Application> apps;
162 apps.setAutoDelete( false );
164 for ( AppListIterator it( myAppList ); it.current(); ++it )
165 apps.append( it.current() );
171 Returns the active application
173 SUIT_Application* SUIT_Session::activeApplication() const
176 if ( myAppList.count() == 1 )
177 return myAppList.getFirst();
179 SUIT_Desktop* desktop = 0;
180 for ( AppListIterator it( myAppList ); it.current() && !desktop; ++it )
182 SUIT_Desktop* desk = it.current()->desktop();
183 if ( desk && desk->isActiveWindow() )
190 SUIT_ViewWindow* win = desktop->activeWindow();
191 if ( !win || !win->getViewManager() )
194 SUIT_Study* study = win->getViewManager()->study();
198 return study->application();
204 Returns the resource manager for the specified application name.
206 SUIT_ResourceMgr* SUIT_Session::resourceMgr() const
212 Removes the application from the list of launched applications.
213 If it is a last application the session will be closed.
215 void SUIT_Session::onApplicationClosed( SUIT_Application* theApp )
217 emit applicationClosed( theApp );
219 myAppList.remove( theApp );
220 if ( theApp == myActiveApp )
223 if ( myAppList.isEmpty() )
225 printf( "Calling QApplication::exit() with exit code = %d\n", myExitStatus );
226 qApp->exit( myExitStatus );
231 Destroys session by closing all applications.
233 void SUIT_Session::closeSession( int mode )
235 while ( !myAppList.isEmpty() )
237 SUIT_Application* app = myAppList.getFirst();
238 if ( mode == ASK && !app->isPossibleToClose() )
240 else if ( mode == SAVE )
242 SUIT_Study* study = app->activeStudy();
243 if ( study->isModified() && study->isSaved() )
244 study->saveDocument();
246 else if ( mode == DONT_SAVE )
248 myExitStatus = FROM_CORBA_SESSION;
252 app->closeApplication();
256 /*! \retval return myHandler*/
257 SUIT_ExceptionHandler* SUIT_Session::handler() const
262 /*! \retval return last error string.*/
263 QString SUIT_Session::lastError() const
268 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
269 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
270 str = QString( (LPTSTR)lpMsgBuf );
271 LocalFree( lpMsgBuf );
273 str = QString( dlerror() );
278 /*! Load library to session.
279 * \retval Loaded library.
281 SUIT_Session::AppLib SUIT_Session::loadLibrary( const QString& name, QString& libName )
283 QString libFile = SUIT_Tools::library( name );
286 if ( libFile.isEmpty() )
291 lib = ::LoadLibrary( (char*)libFile.latin1() );
293 lib = dlopen( (char*)libFile.latin1(), RTLD_LAZY /*| RTLD_GLOBAL */ );
298 /*! \retval Return file name by application name.*/
299 QString SUIT_Session::applicationName( const QString& str ) const
302 return SUIT_Tools::file( str, false );
304 QString fileName = SUIT_Tools::file( str, false );
305 if ( fileName.startsWith( "lib" ) )
306 fileName = fileName.right( fileName.length() - 3 );
312 Virtual method, creates an instance of ResourceManager
314 SUIT_ResourceMgr* SUIT_Session::createResourceMgr( const QString& appName ) const
316 return new SUIT_ResourceMgr( appName );
320 Slot, called on activation of some application's desktop
322 void SUIT_Session::onApplicationActivated( SUIT_Application* app )