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;
22 SUIT_Session::SUIT_Session()
28 SUIT_ASSERT( !mySession )
32 myAppList.setAutoDelete( true );
35 SUIT_Session::~SUIT_Session()
42 SUIT_Session* SUIT_Session::session()
48 Starts new application using "createApplication" function of loaded DLL.
51 SUIT_Application* SUIT_Session::startApplication( const QString& name, int args, char** argv )
55 QString appName = applicationName( name );
56 if ( myAppLibs.contains( appName ) )
57 libHandle = myAppLibs[appName];
60 libHandle = loadLibrary( name );
64 SUIT_MessageBox::warn1( 0, tr( "Error" ),
65 tr( "Can not find function %1. %2").arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
69 myAppLibs.insert( appName, libHandle );
71 APP_CREATE_FUNC crtInst = 0;
74 crtInst = (APP_CREATE_FUNC)::GetProcAddress( libHandle, APP_CREATE_NAME );
76 crtInst = (APP_CREATE_FUNC)dlsym( libHandle, APP_CREATE_NAME );
81 SUIT_MessageBox::warn1( 0, tr( "Error" ),
82 tr( "Can not find function %1. %2").arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
86 // Prepare Resource Manager for the new application if it doesn't exist yet
89 myResMgr = createResourceMgr( appName );
90 myResMgr->loadLanguage();
93 SUIT_Application* anApp = crtInst( args, argv );
96 SUIT_MessageBox::warn1(0, tr( "Error" ), tr( "Can not find function %1. %2").arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
100 anApp->setName( appName );
102 connect( anApp, SIGNAL( applicationClosed( SUIT_Application* ) ),
103 this, SLOT( onApplicationClosed( SUIT_Application* ) ) );
104 connect( anApp, SIGNAL( activated( SUIT_Application* ) ),
105 this, SLOT( onApplicationActivated( SUIT_Application* ) ) );
107 myAppList.append( anApp );
111 APP_GET_HANDLER_FUNC crtHndlr = 0;
113 crtHndlr = (APP_GET_HANDLER_FUNC)::GetProcAddress( libHandle, APP_GET_HANDLER_NAME );
115 crtHndlr = (APP_GET_HANDLER_FUNC)dlsym( libHandle, APP_GET_HANDLER_NAME );
118 myHandler = crtHndlr();
127 Gets the list of all applications
129 QPtrList<SUIT_Application> SUIT_Session::applications() const
131 QPtrList<SUIT_Application> apps;
132 apps.setAutoDelete( false );
134 for ( AppListIterator it( myAppList ); it.current(); ++it )
135 apps.append( it.current() );
141 Returns the active application
143 SUIT_Application* SUIT_Session::activeApplication() const
146 if ( myAppList.count() == 1 )
147 return myAppList.getFirst();
149 SUIT_Desktop* desktop = 0;
150 for ( AppListIterator it( myAppList ); it.current() && !desktop; ++it )
152 SUIT_Desktop* desk = it.current()->desktop();
153 if ( desk && desk->isActiveWindow() )
160 SUIT_ViewWindow* win = desktop->activeWindow();
161 if ( !win || !win->getViewManager() )
164 SUIT_Study* study = win->getViewManager()->study();
168 return study->application();
174 Returns the resource manager for the specified application name.
176 SUIT_ResourceMgr* SUIT_Session::resourceMgr() const
182 Removes the application from the list of launched applications.
183 If it is a last application the session will be closed.
185 void SUIT_Session::onApplicationClosed( SUIT_Application* theApp )
187 emit applicationClosed( theApp );
189 myAppList.remove( theApp );
190 if ( theApp == myActiveApp )
193 if ( myAppList.isEmpty() )
198 Destroys session by closing all applications.
200 void SUIT_Session::closeSession()
202 while ( !myAppList.isEmpty() )
204 SUIT_Application* app = myAppList.getFirst();
205 if ( !app->isPossibleToClose() )
208 app->closeApplication();
212 SUIT_ExceptionHandler* SUIT_Session::handler() const
217 QString SUIT_Session::lastError() const
222 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
223 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
224 str = QString( (LPTSTR)lpMsgBuf );
225 LocalFree( lpMsgBuf );
227 str = QString( dlerror() );
232 SUIT_Session::AppLib SUIT_Session::loadLibrary( const QString& name )
234 QString libFile = SUIT_Tools::library( name );
236 if ( libFile.isEmpty() )
241 lib = ::LoadLibrary( (char*)libFile.latin1() );
243 lib = dlopen( (char*)libFile.latin1(), RTLD_LAZY );
248 QString SUIT_Session::applicationName( const QString& str ) const
251 return SUIT_Tools::file( str, false );
253 QString fileName = SUIT_Tools::file( str, false );
254 if ( fileName.startsWith( "lib" ) )
255 fileName = fileName.right( fileName.length() - 3 );
261 Virtual method, creates an instance of ResourceManager
263 SUIT_ResourceMgr* SUIT_Session::createResourceMgr( const QString& appName ) const
265 return new SUIT_ResourceMgr( appName );
269 Slot, called on activation of some application's desktop
271 void SUIT_Session::onApplicationActivated( SUIT_Application* app )