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 myExitStatus( FROM_GUI )
29 SUIT_ASSERT( !mySession )
33 myAppList.setAutoDelete( true );
36 SUIT_Session::~SUIT_Session()
43 SUIT_Session* SUIT_Session::session()
49 Starts new application using "createApplication" function of loaded DLL.
52 SUIT_Application* SUIT_Session::startApplication( const QString& name, int args, char** argv )
56 QString appName = applicationName( name );
57 if ( myAppLibs.contains( appName ) )
58 libHandle = myAppLibs[appName];
61 libHandle = loadLibrary( name );
65 SUIT_MessageBox::warn1( 0, tr( "Error" ),
66 tr( "Can not find function %1. %2").arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
70 if (!myAppLibs.contains(appName) || !myAppLibs[appName]) // jfa 22.06.2005
71 myAppLibs.insert( appName, libHandle );
73 APP_CREATE_FUNC crtInst = 0;
76 crtInst = (APP_CREATE_FUNC)::GetProcAddress( libHandle, APP_CREATE_NAME );
78 crtInst = (APP_CREATE_FUNC)dlsym( libHandle, APP_CREATE_NAME );
83 SUIT_MessageBox::warn1( 0, tr( "Error" ),
84 tr( "Can not find function %1. %2").arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
88 // Prepare Resource Manager for the new application if it doesn't exist yet
91 myResMgr = createResourceMgr( appName );
92 myResMgr->loadLanguage();
95 //jfa 22.06.2005:SUIT_Application* anApp = crtInst( args, argv );
96 SUIT_Application* anApp = crtInst();
99 SUIT_MessageBox::warn1(0, tr( "Error" ), tr( "Can not find function %1. %2").arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
103 anApp->setName( appName );
105 connect( anApp, SIGNAL( applicationClosed( SUIT_Application* ) ),
106 this, SLOT( onApplicationClosed( SUIT_Application* ) ) );
107 connect( anApp, SIGNAL( activated( SUIT_Application* ) ),
108 this, SLOT( onApplicationActivated( SUIT_Application* ) ) );
110 myAppList.append( anApp );
114 APP_GET_HANDLER_FUNC crtHndlr = 0;
116 crtHndlr = (APP_GET_HANDLER_FUNC)::GetProcAddress( libHandle, APP_GET_HANDLER_NAME );
118 crtHndlr = (APP_GET_HANDLER_FUNC)dlsym( libHandle, APP_GET_HANDLER_NAME );
121 myHandler = crtHndlr();
130 Gets the list of all applications
132 QPtrList<SUIT_Application> SUIT_Session::applications() const
134 QPtrList<SUIT_Application> apps;
135 apps.setAutoDelete( false );
137 for ( AppListIterator it( myAppList ); it.current(); ++it )
138 apps.append( it.current() );
144 Returns the active application
146 SUIT_Application* SUIT_Session::activeApplication() const
149 if ( myAppList.count() == 1 )
150 return myAppList.getFirst();
152 SUIT_Desktop* desktop = 0;
153 for ( AppListIterator it( myAppList ); it.current() && !desktop; ++it )
155 SUIT_Desktop* desk = it.current()->desktop();
156 if ( desk && desk->isActiveWindow() )
163 SUIT_ViewWindow* win = desktop->activeWindow();
164 if ( !win || !win->getViewManager() )
167 SUIT_Study* study = win->getViewManager()->study();
171 return study->application();
177 Returns the resource manager for the specified application name.
179 SUIT_ResourceMgr* SUIT_Session::resourceMgr() const
185 Removes the application from the list of launched applications.
186 If it is a last application the session will be closed.
188 void SUIT_Session::onApplicationClosed( SUIT_Application* theApp )
190 emit applicationClosed( theApp );
192 myAppList.remove( theApp );
193 if ( theApp == myActiveApp )
196 if ( myAppList.isEmpty() )
198 printf( "Calling QApplication::exit() with exit code = %d\n", myExitStatus );
199 qApp->exit( myExitStatus );
204 Destroys session by closing all applications.
206 void SUIT_Session::closeSession( int mode )
208 while ( !myAppList.isEmpty() )
210 SUIT_Application* app = myAppList.getFirst();
211 if ( mode == ASK && !app->isPossibleToClose() )
213 else if ( mode == SAVE )
215 SUIT_Study* study = app->activeStudy();
216 if ( study->isModified() && study->isSaved() )
217 study->saveDocument();
219 else if ( mode == DONT_SAVE )
221 myExitStatus = FROM_CORBA_SESSION;
225 app->closeApplication();
229 SUIT_ExceptionHandler* SUIT_Session::handler() const
234 QString SUIT_Session::lastError() const
239 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
240 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
241 str = QString( (LPTSTR)lpMsgBuf );
242 LocalFree( lpMsgBuf );
244 str = QString( dlerror() );
249 SUIT_Session::AppLib SUIT_Session::loadLibrary( const QString& name )
251 QString libFile = SUIT_Tools::library( name );
253 if ( libFile.isEmpty() )
258 lib = ::LoadLibrary( (char*)libFile.latin1() );
260 lib = dlopen( (char*)libFile.latin1(), RTLD_LAZY );
265 QString SUIT_Session::applicationName( const QString& str ) const
268 return SUIT_Tools::file( str, false );
270 QString fileName = SUIT_Tools::file( str, false );
271 if ( fileName.startsWith( "lib" ) )
272 fileName = fileName.right( fileName.length() - 3 );
278 Virtual method, creates an instance of ResourceManager
280 SUIT_ResourceMgr* SUIT_Session::createResourceMgr( const QString& appName ) const
282 return new SUIT_ResourceMgr( appName );
286 Slot, called on activation of some application's desktop
288 void SUIT_Session::onApplicationActivated( SUIT_Application* app )