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()
27 SUIT_ASSERT( !mySession )
31 myAppList.setAutoDelete( true );
34 SUIT_Session::~SUIT_Session()
41 SUIT_Session* SUIT_Session::session()
47 Starts new application using "createApplication" function of loaded DLL.
50 SUIT_Application* SUIT_Session::startApplication( const QString& name, int args, char** argv )
54 QString appName = applicationName( name );
55 if ( myAppLibs.contains( appName ) )
56 libHandle = myAppLibs[appName];
59 libHandle = loadLibrary( name );
63 SUIT_MessageBox::warn1( 0, tr( "Error" ),
64 tr( "Can not find function %1. %2").arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
68 myAppLibs.insert( appName, libHandle );
70 APP_CREATE_FUNC crtInst = 0;
73 crtInst = (APP_CREATE_FUNC)::GetProcAddress( libHandle, APP_CREATE_NAME );
75 crtInst = (APP_CREATE_FUNC)dlsym( libHandle, APP_CREATE_NAME );
80 SUIT_MessageBox::warn1( 0, tr( "Error" ),
81 tr( "Can not find function %1. %2").arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
85 // Prepare Resource Manager for the new application if it doesn't exist yet
88 myResMgr = createResourceMgr( appName );
89 myResMgr->loadLanguage();
92 SUIT_Application* anApp = crtInst( args, argv );
95 SUIT_MessageBox::warn1(0, tr( "Error" ), tr( "Can not find function %1. %2").arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
99 anApp->setName( appName );
101 connect( anApp, SIGNAL( applicationClosed( SUIT_Application* ) ),
102 this, SLOT( onApplicationClosed( SUIT_Application* ) ) );
104 myAppList.append( anApp );
108 APP_GET_HANDLER_FUNC crtHndlr = 0;
110 crtHndlr = (APP_GET_HANDLER_FUNC)::GetProcAddress( libHandle, APP_GET_HANDLER_NAME );
112 crtHndlr = (APP_GET_HANDLER_FUNC)dlsym( libHandle, APP_GET_HANDLER_NAME );
115 myHandler = crtHndlr();
124 Gets the list of all applications
126 QPtrList<SUIT_Application> SUIT_Session::applications() const
128 QPtrList<SUIT_Application> apps;
129 apps.setAutoDelete( false );
131 for ( AppListIterator it( myAppList ); it.current(); ++it )
132 apps.append( it.current() );
138 Returns the active application
140 SUIT_Application* SUIT_Session::activeApplication() const
142 if ( myAppList.count() == 1 )
143 return myAppList.getFirst();
145 SUIT_Desktop* desktop = 0;
146 for ( AppListIterator it( myAppList ); it.current() && !desktop; ++it )
148 SUIT_Desktop* desk = it.current()->desktop();
149 if ( desk && desk->isActiveWindow() )
156 SUIT_ViewWindow* win = desktop->activeWindow();
157 if ( !win || !win->getViewManager() )
160 SUIT_Study* study = win->getViewManager()->study();
164 return study->application();
168 Returns the resource manager for the specified application name.
170 SUIT_ResourceMgr* SUIT_Session::resourceMgr() const
176 Removes the application from the list of launched applications.
177 If it is a last application the session will be closed.
179 void SUIT_Session::onApplicationClosed( SUIT_Application* theApp )
181 emit applicationClosed( theApp );
183 myAppList.remove( theApp );
185 if ( myAppList.isEmpty() )
190 Destroys session by closing all applications.
192 void SUIT_Session::closeSession()
194 for ( AppListIterator it( myAppList ); it.current(); ++it )
196 if ( !it.current()->isPossibleToClose() )
202 SUIT_ExceptionHandler* SUIT_Session::handler() const
207 QString SUIT_Session::lastError() const
212 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
213 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
214 str = QString( (LPTSTR)lpMsgBuf );
215 LocalFree( lpMsgBuf );
217 str = QString( dlerror() );
222 SUIT_Session::AppLib SUIT_Session::loadLibrary( const QString& name )
224 QString libFile = SUIT_Tools::library( name );
226 if ( libFile.isEmpty() )
231 lib = ::LoadLibrary( (char*)libFile.latin1() );
233 lib = dlopen( (char*)libFile.latin1(), RTLD_LAZY );
238 QString SUIT_Session::applicationName( const QString& str ) const
241 return SUIT_Tools::file( str, false );
243 QString fileName = SUIT_Tools::file( str, false );
244 if ( fileName.startsWith( "lib" ) )
245 fileName = fileName.right( fileName.length() - 3 );
251 Virtual method, creates an instance of ResourceManager
253 SUIT_ResourceMgr* SUIT_Session::createResourceMgr( const QString& appName ) const
255 return new SUIT_ResourceMgr( appName );