Salome HOME
updated copyright message
[modules/gui.git] / src / SUIT / SUIT_Session.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 7eca732..e8da504
@@ -1,33 +1,34 @@
-// Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-// 
+// Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either 
-// version 2.1 of the License.
-// 
-// This library is distributed in the hope that it will be useful 
-// but WITHOUT ANY WARRANTY; without even the implied warranty of 
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Lesser General Public License for more details.
 //
-// You should have received a copy of the GNU Lesser General Public  
-// License along with this library; if not, write to the Free Software 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 #include "SUIT_Session.h"
 
+#include "SUIT_Study.h"
 #include "SUIT_Tools.h"
-#include "SUIT_Desktop.h"
 #include "SUIT_MessageBox.h"
-#include "SUIT_ViewWindow.h"
-#include "SUIT_ViewManager.h"
 #include "SUIT_ExceptionHandler.h"
+#include "SUIT_ResourceMgr.h"
 
-#include <qtextcodec.h>
-#include <qmessagebox.h>
-#include <qapplication.h>
+#include <QApplication>
 
 #ifdef WIN32
 #include <windows.h>
@@ -41,23 +42,30 @@ SUIT_Session* SUIT_Session::mySession = 0;
 
 SUIT_Session::SUIT_Session()
 : QObject(),
-myResMgr( 0 ),
-myHandler( 0 ),
-myActiveApp( 0 ),
-myExitStatus( FROM_GUI )
+  myResMgr( 0 ),
+  myActiveApp( 0 ),
+  myHandler( 0 ),
+  myExitStatus( NORMAL ),
+  myExitFlags ( 0 )
 {
   SUIT_ASSERT( !mySession )
 
   mySession = this;
-
-  myAppList.setAutoDelete( true );
 }
 
 /*!destructor. Clear applications list and set mySession to zero.*/
 SUIT_Session::~SUIT_Session()
 {
+  for ( AppList::iterator it = myAppList.begin(); it != myAppList.end(); ++it )
+    delete *it;
+
   myAppList.clear();
 
+  if ( myResMgr )
+  {
+    delete myResMgr;
+    myResMgr = 0;
+  }
   mySession = 0;
 }
 
@@ -71,7 +79,7 @@ SUIT_Session* SUIT_Session::session()
   Starts new application using "createApplication" function of loaded DLL.
 */
 
-SUIT_Application* SUIT_Session::startApplication( const QString& name, int args, char** argv )
+SUIT_Application* SUIT_Session::startApplication( const QString& name, int /*args*/, char** /*argv*/ )
 {
   AppLib libHandle = 0;
 
@@ -85,8 +93,8 @@ SUIT_Application* SUIT_Session::startApplication( const QString& name, int args,
 
   if ( !libHandle )
   {
-    SUIT_MessageBox::warn1( 0, tr( "Error" ),
-                            tr( "Can not load application library \"%1\": %2").arg( lib ).arg( lastError() ), tr( "Ok" ) );
+    SUIT_MessageBox::warning( 0, tr( "Error" ),
+                              tr( "Can not load application library \"%1\": %2").arg( lib ).arg( lastError() ) );
     return 0;
   }
 
@@ -96,15 +104,15 @@ SUIT_Application* SUIT_Session::startApplication( const QString& name, int args,
   APP_CREATE_FUNC crtInst = 0;
 
 #ifdef WIN32
-  crtInst = (APP_CREATE_FUNC)::GetProcAddress( libHandle, APP_CREATE_NAME );
+  crtInst = (APP_CREATE_FUNC)::GetProcAddress( (HINSTANCE)libHandle, APP_CREATE_NAME );
 #else
   crtInst = (APP_CREATE_FUNC)dlsym( libHandle, APP_CREATE_NAME );
 #endif
 
   if ( !crtInst )
   {
-    SUIT_MessageBox::warn1( 0, tr( "Error" ),
-                            tr( "Can not find function \"%1\": %2" ).arg( APP_CREATE_NAME ).arg( lastError() ), tr( "Ok" ) );
+    SUIT_MessageBox::warning( 0, tr( "Error" ),
+                              tr( "Can not find function \"%1\": %2" ).arg( APP_CREATE_NAME ).arg( lastError() ) );
     return 0;
   }
 
@@ -119,24 +127,19 @@ SUIT_Application* SUIT_Session::startApplication( const QString& name, int args,
   SUIT_Application* anApp = crtInst();
   if ( !anApp )
   {
-    SUIT_MessageBox::warn1( 0, tr( "Error" ), tr( "Can not create application \"%1\": %2").arg( appName ).arg( lastError() ), tr( "Ok" ) );
+    SUIT_MessageBox::warning( 0, tr( "Error" ), tr( "Can not create application \"%1\": %2").arg( appName ).arg( lastError() ) );
     return 0;
   }
 
-  anApp->setName( appName );
+  anApp->setObjectName( appName );
 
-  connect( anApp, SIGNAL( applicationClosed( SUIT_Application* ) ),
-           this, SLOT( onApplicationClosed( SUIT_Application* ) ) );
-  connect( anApp, SIGNAL( activated( SUIT_Application* ) ), 
-                this, SLOT( onApplicationActivated( SUIT_Application* ) ) );
-
-  myAppList.append( anApp );
+  insertApplication( anApp );
 
   if ( !myHandler )
   {
     APP_GET_HANDLER_FUNC crtHndlr = 0;
 #ifdef WIN32
-    crtHndlr = (APP_GET_HANDLER_FUNC)::GetProcAddress( libHandle, APP_GET_HANDLER_NAME );
+    crtHndlr = (APP_GET_HANDLER_FUNC)::GetProcAddress( (HINSTANCE)libHandle, APP_GET_HANDLER_NAME );
 #else
     crtHndlr = (APP_GET_HANDLER_FUNC)dlsym( libHandle, APP_GET_HANDLER_NAME );
 #endif
@@ -156,15 +159,22 @@ SUIT_Application* SUIT_Session::startApplication( const QString& name, int args,
 /*!
   Gets the list of all applications
 */
-QPtrList<SUIT_Application> SUIT_Session::applications() const
+QList<SUIT_Application*> SUIT_Session::applications() const
 {
-  QPtrList<SUIT_Application> apps;
-  apps.setAutoDelete( false );
+  return myAppList;
+}
 
-  for ( AppListIterator it( myAppList ); it.current(); ++it )
-    apps.append( it.current() );
+void SUIT_Session::insertApplication( SUIT_Application* app )
+{
+  if ( !app || myAppList.contains( app ) )
+    return;
 
-  return apps;
+  myAppList.append( app );
+
+  connect( app, SIGNAL( applicationClosed( SUIT_Application* ) ),
+           this, SLOT( onApplicationClosed( SUIT_Application* ) ) );
+  connect( app, SIGNAL( activated( SUIT_Application* ) ), 
+                 this, SLOT( onApplicationActivated( SUIT_Application* ) ) );
 }
 
 /*!
@@ -216,41 +226,60 @@ void SUIT_Session::onApplicationClosed( SUIT_Application* theApp )
 {
   emit applicationClosed( theApp );
 
-  myAppList.remove( theApp );
+  myAppList.removeAll( theApp );
+  delete theApp;
+
   if ( theApp == myActiveApp )
     myActiveApp = 0;
 
   if ( myAppList.isEmpty() )
   {
-    printf( "Calling QApplication::exit() with exit code = %d\n", myExitStatus );
-    qApp->exit( myExitStatus );
+    //printf( "Calling QApplication::exit() with exit code = %d\n", myExitStatus );
+    QApplication::instance()->exit( myExitStatus );
   }
 }
 
 /*!
   Destroys session by closing all applications.
 */
-void SUIT_Session::closeSession( int mode )
+void SUIT_Session::closeSession( int mode, int flags )
 {
-  while ( !myAppList.isEmpty() )
+  AppList apps = myAppList;
+  for ( AppList::const_iterator it = apps.begin(); it != apps.end(); ++it )
   {
-    SUIT_Application* app = myAppList.getFirst();
-    if ( mode == ASK && !app->isPossibleToClose() )
+    SUIT_Application* app = *it;
+    bool closePermanently;
+    if ( mode == ASK && !app->isPossibleToClose( closePermanently ) )
       return;
     else if ( mode == SAVE )
     {
       SUIT_Study* study = app->activeStudy();
       if ( study->isModified() && study->isSaved() )
-       study->saveDocument();
+              study->saveDocument();
     }
     else if ( mode == DONT_SAVE )
     {
-      myExitStatus = FROM_CORBA_SESSION;
-      //....
+      myExitStatus = FORCED;
     }
 
     app->closeApplication();
   }
+
+  myExitFlags = flags;
+}
+
+/*!
+  Get session exit flags.
+
+  By default, exit flags are set to 0. You can use pass any flags to the
+  closeSession() method if you need to process them later on application
+  quiting.
+
+  \return exit flags
+*/
+int SUIT_Session::exitFlags() const
+{
+  return myExitFlags;
 }
 
 /*! \retval return myHandler*/
@@ -263,11 +292,16 @@ SUIT_ExceptionHandler* SUIT_Session::handler() const
 QString SUIT_Session::lastError() const
 {
   QString str;
-#ifdef WNT
+#ifdef WIN32
   LPVOID lpMsgBuf;
   ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
                    FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
-  str = QString( (LPTSTR)lpMsgBuf );
+  LPTSTR msg = (LPTSTR)lpMsgBuf;
+#ifdef UNICODE
+  str = QString::fromWCharArray(msg);
+#else
+  str = QString( SUIT_Tools::toQString( msg ) );
+#endif
   LocalFree( lpMsgBuf );
 #else
   str = QString( dlerror() );
@@ -287,10 +321,16 @@ SUIT_Session::AppLib SUIT_Session::loadLibrary( const QString& name, QString& li
     return 0;
 
   AppLib lib = 0;
+  QByteArray bid = libFile.toUtf8();
 #ifdef WIN32
-  lib = ::LoadLibrary( (char*)libFile.latin1() );
+#ifdef UNICODE
+  LPTSTR str = (LPTSTR)libFile.utf16();
 #else
-  lib = dlopen( (char*)libFile.latin1(), RTLD_LAZY /*| RTLD_GLOBAL */ );
+  LPTSTR str = (LPTSTR)(const char*)bid;
+#endif
+  lib = ::LoadLibrary( str );
+#else
+  lib = dlopen( (const char*)libFile.toUtf8(), RTLD_LAZY | RTLD_GLOBAL  );
 #endif
   return lib;
 }
@@ -313,7 +353,7 @@ QString SUIT_Session::applicationName( const QString& str ) const
 */
 SUIT_ResourceMgr* SUIT_Session::createResourceMgr( const QString& appName ) const
 {
-  return new SUIT_ResourceMgr( appName );
+  return new SUIT_ResourceMgr( applicationName( appName ) );
 }
 
 /*!
@@ -322,4 +362,4 @@ SUIT_ResourceMgr* SUIT_Session::createResourceMgr( const QString& appName ) cons
 void SUIT_Session::onApplicationActivated( SUIT_Application* app ) 
 {
   myActiveApp = app;
-}
+}
\ No newline at end of file