Salome HOME
PR: new python function getShortHostName() in Utils_Identity.py, based on socket...
[modules/yacs.git] / src / Session / SALOME_Session_QThread.cxx
1 //  SALOME Session : implementation of Session.idl
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOME_Session_QThread.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 #include <qapplication.h>
30 #include <qlabel.h>
31
32 #include "utilities.h"
33 #include "SALOME_Session_QThread.hxx"
34 #include "SALOME_NamingService.hxx"
35 #include "SALOMEGUI_Application.h"
36 #include "QAD.h"
37 #include "QAD_MessageBox.h"
38 #include "QAD_Application.h"
39 #include "QAD_Settings.h"
40 #include "QAD_Config.h"
41 #include "QAD_Tools.h"
42 // only to use findFile method : perhaps put it in QAD_Tools ???
43 #include "QAD_ResourceMgr.h"
44 #include <qlabel.h>
45
46 #include "Utils_CatchSignals.h"
47 #include "Utils_CorbaException.hxx"
48 #include <SALOMEconfig.h>
49 #include CORBA_SERVER_HEADER(SALOMEDS)
50 using namespace std;
51
52 //=============================================================================
53 /*! SALOME_Session_QThread
54  *  constructor
55  */
56 //=============================================================================
57
58 SALOME_Session_QThread::SALOME_Session_QThread(int argc, char ** argv) : QThread()
59 {
60   _qappl = 0 ;
61   _mw = 0 ;
62   _argc = argc ;
63   _argv = argv ;
64   _NS = 0 ;
65 }
66
67
68 //=============================================================================
69 /*! run
70  *  Creates one QApplication for the all the session life.
71  *  Executes the Qt main window of the GUI on a separate thread
72  */
73 //=============================================================================
74 void SALOME_Session_QThread::run()
75 {
76   if (_qappl == 0)
77     {
78       _qappl = new QApplication(_argc, _argv );
79       MESSAGE("creation QApplication");
80     }
81
82   QAD_ASSERT ( QObject::connect(_qappl, SIGNAL(lastWindowClosed()), _qappl, SLOT(quit()) ) );
83   _mw = new SALOMEGUI_Application ( "MDTV-Standard", "HDF", "hdf" );
84   MESSAGE("creation SALOMEGUI_Application");
85   
86   if ( !SALOMEGUI_Application::addToDesktop ( _mw, _NS ) )
87     {
88       QAD_MessageBox::error1 ( 0,
89                                QObject::tr("ERR_ERROR"), 
90                                QObject::tr("ERR_APP_INITFAILED"),
91                                QObject::tr("BUT_OK") );
92     }
93   else
94     {
95       
96       QFileInfo prgInfo(_argv[0]);
97       QDir prgDir(prgInfo.dirPath(true));
98       QAD_CONFIG->setPrgDir(prgDir);        // CWD is program directory
99       QAD_CONFIG->createConfigFile(false);  // Create config file ~/.tr(MEN_APPNAME)/tr(MEN_APPNAME).conf if there's none
100       QAD_CONFIG->readConfigFile();         // Read config file
101
102       _qappl->setPalette( QAD_Application::getPalette() ); 
103
104       Utils_CatchSignals aCatch;
105       aCatch.Activate();
106       
107       /* Run 'SALOMEGUI' application */
108       QAD_Application::run();
109      
110       // T2.12 - catch exceptions thrown on attempts to modified a locked study
111       while (1) {
112         try {
113           MESSAGE("run(): starting the main event loop");
114           _ret = _qappl->exec();
115           break;
116         }
117         catch (SALOME::SALOME_Exception& e) {
118           INFOS("run(): SALOME::SALOME_Exception is caught");
119           QApplication::restoreOverrideCursor();
120           QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
121                                   QObject::tr("ERR_ERROR"), 
122                                   QObject::tr("ERR_APP_EXCEPTION")+ QObject::tr(" SALOME::SALOME_Exception is caught") ,
123                                   QObject::tr("BUT_OK") );
124           
125         }
126         catch(SALOMEDS::StudyBuilder::LockProtection&) {
127           INFOS("run(): An attempt to modify a locked study has not been handled by QAD_Operation");
128           QApplication::restoreOverrideCursor();
129           QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
130                                  QObject::tr("WRN_WARNING"), 
131                                  QObject::tr("WRN_STUDY_LOCKED"),
132                                  QObject::tr("BUT_OK") );
133         }
134         catch (const CORBA::Exception& e) {
135           CORBA::Any tmp;
136           tmp<<= e;
137           CORBA::TypeCode_var tc = tmp.type();
138           const char *p = tc->name();
139           INFOS ("run(): CORBA exception of the kind : "<<p<< " is caught");
140
141           QApplication::restoreOverrideCursor();
142           QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
143                                QObject::tr("ERR_ERROR"), 
144                                QObject::tr("ERR_APP_EXCEPTION")+ QObject::tr(" CORBA exception ") + QObject::tr(p),
145                                QObject::tr("BUT_OK") );
146         }
147         catch(std::exception& e) {
148           INFOS("run(): An exception has been caught");
149           QApplication::restoreOverrideCursor();
150           QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
151                                QObject::tr("ERR_ERROR"), 
152                                QObject::tr("ERR_APP_EXCEPTION")+ " " +QObject::tr(e.what()),
153                                QObject::tr("BUT_OK") );
154         }
155         catch(...) {
156           INFOS("run(): An exception has been caught")
157           QApplication::restoreOverrideCursor();
158           QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
159                                QObject::tr("ERR_ERROR"), 
160                                QObject::tr("ERR_APP_EXCEPTION"),
161                                QObject::tr("BUT_OK") );
162         }
163       }
164       
165       aCatch.Deactivate();
166       QString confMsg = "Settings create "
167         + QAD_CONFIG->getConfigDir().absPath() + "/" + QObject::tr("MEN_APPNAME") + ".conf";
168       MESSAGE (confMsg);
169       QAD_CONFIG->createConfigFile(true);
170     }
171 }
172
173 //=============================================================================
174 /*! setNamingService
175  *  gives the naming service interface 
176  */
177 //=============================================================================
178 void SALOME_Session_QThread::setNamingService(SALOME_NamingService *ns)
179 {
180   _NS = ns;
181 }