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];
65 libHandle = loadLibrary( name );
69 SUIT_MessageBox::warn1( 0, tr( "Error" ),
70 tr( "Can not find function %1. %2").arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
74 if (!myAppLibs.contains(appName) || !myAppLibs[appName]) // jfa 22.06.2005
75 myAppLibs.insert( appName, libHandle );
77 APP_CREATE_FUNC crtInst = 0;
80 crtInst = (APP_CREATE_FUNC)::GetProcAddress( libHandle, APP_CREATE_NAME );
82 crtInst = (APP_CREATE_FUNC)dlsym( libHandle, APP_CREATE_NAME );
87 SUIT_MessageBox::warn1( 0, tr( "Error" ),
88 tr( "Can not find function %1. %2").arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
92 // Prepare Resource Manager for the new application if it doesn't exist yet
95 myResMgr = createResourceMgr( appName );
96 myResMgr->loadLanguage();
99 //jfa 22.06.2005:SUIT_Application* anApp = crtInst( args, argv );
100 SUIT_Application* anApp = crtInst();
103 SUIT_MessageBox::warn1(0, tr( "Error" ), tr( "Can not find function %1. %2").arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
107 anApp->setName( appName );
109 connect( anApp, SIGNAL( applicationClosed( SUIT_Application* ) ),
110 this, SLOT( onApplicationClosed( SUIT_Application* ) ) );
111 connect( anApp, SIGNAL( activated( SUIT_Application* ) ),
112 this, SLOT( onApplicationActivated( SUIT_Application* ) ) );
114 myAppList.append( anApp );
118 APP_GET_HANDLER_FUNC crtHndlr = 0;
120 crtHndlr = (APP_GET_HANDLER_FUNC)::GetProcAddress( libHandle, APP_GET_HANDLER_NAME );
122 crtHndlr = (APP_GET_HANDLER_FUNC)dlsym( libHandle, APP_GET_HANDLER_NAME );
125 myHandler = crtHndlr();
134 Gets the list of all applications
136 QPtrList<SUIT_Application> SUIT_Session::applications() const
138 QPtrList<SUIT_Application> apps;
139 apps.setAutoDelete( false );
141 for ( AppListIterator it( myAppList ); it.current(); ++it )
142 apps.append( it.current() );
148 Returns the active application
150 SUIT_Application* SUIT_Session::activeApplication() const
153 if ( myAppList.count() == 1 )
154 return myAppList.getFirst();
156 SUIT_Desktop* desktop = 0;
157 for ( AppListIterator it( myAppList ); it.current() && !desktop; ++it )
159 SUIT_Desktop* desk = it.current()->desktop();
160 if ( desk && desk->isActiveWindow() )
167 SUIT_ViewWindow* win = desktop->activeWindow();
168 if ( !win || !win->getViewManager() )
171 SUIT_Study* study = win->getViewManager()->study();
175 return study->application();
181 Returns the resource manager for the specified application name.
183 SUIT_ResourceMgr* SUIT_Session::resourceMgr() const
189 Removes the application from the list of launched applications.
190 If it is a last application the session will be closed.
192 void SUIT_Session::onApplicationClosed( SUIT_Application* theApp )
194 emit applicationClosed( theApp );
196 myAppList.remove( theApp );
197 if ( theApp == myActiveApp )
200 if ( myAppList.isEmpty() )
202 printf( "Calling QApplication::exit() with exit code = %d\n", myExitStatus );
203 qApp->exit( myExitStatus );
208 Destroys session by closing all applications.
210 void SUIT_Session::closeSession( int mode )
212 while ( !myAppList.isEmpty() )
214 SUIT_Application* app = myAppList.getFirst();
215 if ( mode == ASK && !app->isPossibleToClose() )
217 else if ( mode == SAVE )
219 SUIT_Study* study = app->activeStudy();
220 if ( study->isModified() && study->isSaved() )
221 study->saveDocument();
223 else if ( mode == DONT_SAVE )
225 myExitStatus = FROM_CORBA_SESSION;
229 app->closeApplication();
233 /*! \retval return myHandler*/
234 SUIT_ExceptionHandler* SUIT_Session::handler() const
239 /*! \retval return last error string.*/
240 QString SUIT_Session::lastError() const
245 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
246 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
247 str = QString( (LPTSTR)lpMsgBuf );
248 LocalFree( lpMsgBuf );
250 str = QString( dlerror() );
255 /*! Load library to session.
256 * \retval Loaded library.
258 SUIT_Session::AppLib SUIT_Session::loadLibrary( const QString& name )
260 QString libFile = SUIT_Tools::library( name );
262 if ( libFile.isEmpty() )
267 lib = ::LoadLibrary( (char*)libFile.latin1() );
269 lib = dlopen( (char*)libFile.latin1(), RTLD_LAZY );
274 /*! \retval Return file name by application name.*/
275 QString SUIT_Session::applicationName( const QString& str ) const
278 return SUIT_Tools::file( str, false );
280 QString fileName = SUIT_Tools::file( str, false );
281 if ( fileName.startsWith( "lib" ) )
282 fileName = fileName.right( fileName.length() - 3 );
288 Virtual method, creates an instance of ResourceManager
290 SUIT_ResourceMgr* SUIT_Session::createResourceMgr( const QString& appName ) const
292 return new SUIT_ResourceMgr( appName );
296 Slot, called on activation of some application's desktop
298 void SUIT_Session::onApplicationActivated( SUIT_Application* app )