1 #include "SUIT_Session.h"
3 #include "SUIT_Tools.h"
4 #include "SUIT_Desktop.h"
5 #include "SUIT_MessageBox.h"
6 #include "SUIT_ViewWindow.h"
7 #include "SUIT_ViewManager.h"
8 #include "SUIT_ExceptionHandler.h"
10 #include <qtextcodec.h>
11 #include <qmessagebox.h>
12 #include <qapplication.h>
20 SUIT_Session* SUIT_Session::mySession = 0;
24 SUIT_Session::SUIT_Session()
29 myExitStatus( FROM_GUI )
31 SUIT_ASSERT( !mySession )
35 myAppList.setAutoDelete( true );
38 /*!destructor. Clear applications list and set mySession to zero.*/
39 SUIT_Session::~SUIT_Session()
46 /*! \retval return mySession */
47 SUIT_Session* SUIT_Session::session()
53 Starts new application using "createApplication" function of loaded DLL.
56 SUIT_Application* SUIT_Session::startApplication( const QString& name, int args, char** argv )
60 QString appName = applicationName( name );
61 if ( myAppLibs.contains( appName ) )
62 libHandle = myAppLibs[appName];
66 libHandle = loadLibrary( name, lib );
70 SUIT_MessageBox::warn1( 0, tr( "Error" ),
71 tr( "Can not load application library \"%1\": %2").arg( lib ).arg( lastError() ), tr( "Ok" ) );
75 if ( !myAppLibs.contains( appName ) || !myAppLibs[appName] ) // jfa 22.06.2005
76 myAppLibs.insert( appName, libHandle );
78 APP_CREATE_FUNC crtInst = 0;
81 crtInst = (APP_CREATE_FUNC)::GetProcAddress( libHandle, APP_CREATE_NAME );
83 crtInst = (APP_CREATE_FUNC)dlsym( libHandle, APP_CREATE_NAME );
88 SUIT_MessageBox::warn1( 0, tr( "Error" ),
89 tr( "Can not find function \"%1\": %2" ).arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
93 // Prepare Resource Manager for the new application if it doesn't exist yet
96 myResMgr = createResourceMgr( appName );
97 myResMgr->loadLanguage();
100 //jfa 22.06.2005:SUIT_Application* anApp = crtInst( args, argv );
101 SUIT_Application* anApp = crtInst();
104 SUIT_MessageBox::warn1( 0, tr( "Error" ), tr( "Can not create application \"%1\": %2").arg( appName ).arg( lastError() ), tr( "Ok" ) );
108 anApp->setName( appName );
110 connect( anApp, SIGNAL( applicationClosed( SUIT_Application* ) ),
111 this, SLOT( onApplicationClosed( SUIT_Application* ) ) );
112 connect( anApp, SIGNAL( activated( SUIT_Application* ) ),
113 this, SLOT( onApplicationActivated( SUIT_Application* ) ) );
115 myAppList.append( anApp );
119 APP_GET_HANDLER_FUNC crtHndlr = 0;
121 crtHndlr = (APP_GET_HANDLER_FUNC)::GetProcAddress( libHandle, APP_GET_HANDLER_NAME );
123 crtHndlr = (APP_GET_HANDLER_FUNC)dlsym( libHandle, APP_GET_HANDLER_NAME );
126 myHandler = crtHndlr();
131 // Application can be closed during starting (not started).
132 if ( !myAppList.contains( anApp ) )
139 Gets the list of all applications
141 QPtrList<SUIT_Application> SUIT_Session::applications() const
143 QPtrList<SUIT_Application> apps;
144 apps.setAutoDelete( false );
146 for ( AppListIterator it( myAppList ); it.current(); ++it )
147 apps.append( it.current() );
153 Returns the active application
155 SUIT_Application* SUIT_Session::activeApplication() const
158 if ( myAppList.count() == 1 )
159 return myAppList.getFirst();
161 SUIT_Desktop* desktop = 0;
162 for ( AppListIterator it( myAppList ); it.current() && !desktop; ++it )
164 SUIT_Desktop* desk = it.current()->desktop();
165 if ( desk && desk->isActiveWindow() )
172 SUIT_ViewWindow* win = desktop->activeWindow();
173 if ( !win || !win->getViewManager() )
176 SUIT_Study* study = win->getViewManager()->study();
180 return study->application();
186 Returns the resource manager for the specified application name.
188 SUIT_ResourceMgr* SUIT_Session::resourceMgr() const
194 Removes the application from the list of launched applications.
195 If it is a last application the session will be closed.
197 void SUIT_Session::onApplicationClosed( SUIT_Application* theApp )
199 emit applicationClosed( theApp );
201 myAppList.remove( theApp );
202 if ( theApp == myActiveApp )
205 if ( myAppList.isEmpty() )
207 printf( "Calling QApplication::exit() with exit code = %d\n", myExitStatus );
208 qApp->exit( myExitStatus );
213 Destroys session by closing all applications.
215 void SUIT_Session::closeSession( int mode )
217 while ( !myAppList.isEmpty() )
219 SUIT_Application* app = myAppList.getFirst();
220 if ( mode == ASK && !app->isPossibleToClose() )
222 else if ( mode == SAVE )
224 SUIT_Study* study = app->activeStudy();
225 if ( study->isModified() && study->isSaved() )
226 study->saveDocument();
228 else if ( mode == DONT_SAVE )
230 myExitStatus = FROM_CORBA_SESSION;
234 app->closeApplication();
238 /*! \retval return myHandler*/
239 SUIT_ExceptionHandler* SUIT_Session::handler() const
244 /*! \retval return last error string.*/
245 QString SUIT_Session::lastError() const
250 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
251 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
252 str = QString( (LPTSTR)lpMsgBuf );
253 LocalFree( lpMsgBuf );
255 str = QString( dlerror() );
260 /*! Load library to session.
261 * \retval Loaded library.
263 SUIT_Session::AppLib SUIT_Session::loadLibrary( const QString& name, QString& libName )
265 QString libFile = SUIT_Tools::library( name );
268 if ( libFile.isEmpty() )
273 lib = ::LoadLibrary( (char*)libFile.latin1() );
275 lib = dlopen( (char*)libFile.latin1(), RTLD_LAZY );
280 /*! \retval Return file name by application name.*/
281 QString SUIT_Session::applicationName( const QString& str ) const
284 return SUIT_Tools::file( str, false );
286 QString fileName = SUIT_Tools::file( str, false );
287 if ( fileName.startsWith( "lib" ) )
288 fileName = fileName.right( fileName.length() - 3 );
294 Virtual method, creates an instance of ResourceManager
296 SUIT_ResourceMgr* SUIT_Session::createResourceMgr( const QString& appName ) const
298 return new SUIT_ResourceMgr( appName );
302 Slot, called on activation of some application's desktop
304 void SUIT_Session::onApplicationActivated( SUIT_Application* app )