1 // SALOME RegistryDisplay : GUI for Registry server implementation
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : RegWidget.cxx
25 // Author : Pascale NOYRET, EDF
29 # include <qpushbutton.h>
30 # include <qlistview.h>
31 # include <qtabwidget.h>
32 # include <qstatusbar.h>
33 # include <qtextview.h>
35 # include <qinputdialog.h>
36 # include <qtoolbar.h>
38 # include <qtooltip.h>
40 # include "SALOME_NamingService.hxx"
41 # include "ServiceUnreachable.hxx"
42 # include "Utils_SINGLETON.hxx"
43 # include "Utils_CommException.hxx"
45 # include "utilities.h"
46 # include "OpUtil.hxx"
48 # include "RegWidget.hxx"
49 # include "HelpWindow.hxx"
50 # include "IntervalWindow.hxx"
54 RegWidget* RegWidget::myRegWidgetPtr = 0;
55 QString addSlash( const QString& );
56 QString findFile( QString filename );
58 #define BOLD( text ) ( QString( "<b>" ) + QString( text ) + QString( "</b>" ) )
61 Creates components list
64 Registry::Components_var MakeRegistry( CORBA::ORB_var &orb )
67 const char *registryName="Registry" ;
69 SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
70 naming.init_orb( orb ) ;
72 // Recuperation de la reference de l'objet
73 CORBA::Object_var object = 0 ;
76 SCRUTE(registryName) ;
77 object = naming.Resolve( registryName ) ;
78 if(CORBA::is_nil(object)) throw CommException( "unable to find the RegistryService" ) ;
80 catch( const ServiceUnreachable &ex )
83 exit( EXIT_FAILURE ) ;
85 catch( const CORBA::Exception &exx )
87 exit( EXIT_FAILURE ) ;
90 // Specialisation de l'objet generique
92 return Registry::Components::_narrow( object ) ;
96 Only one global registry window should exist at the moment
97 This method creates registry window if necessary and returns it
100 RegWidget* RegWidget::GetRegWidget( CORBA::ORB_var &orb , QWidget *parent, const char *name )
102 if ( !myRegWidgetPtr )
103 myRegWidgetPtr = new RegWidget( orb, parent, name );
104 return myRegWidgetPtr;
110 RegWidget::RegWidget(CORBA::ORB_var &orb, QWidget *parent, const char *name )
111 : QMainWindow( parent, name, WType_TopLevel | WDestructiveClose ),
112 _VarComponents( MakeRegistry(orb) ),
113 _clients(0), _history(0), _parent( parent ),
114 _tabWidget(0), _refresh(0), _interval(0),
115 myInfoWindow(0), myHelpWindow(0), myIntervalWindow(0)
117 QString aFile = findFile("default.png");
118 /* char* dir = getenv( "CSF_ResourcesDefaults" );
122 path = qDir.filePath( "default.png" );
124 QPixmap pm ( aFile );
128 // Buttons definition
129 QToolBar* topbar = new QToolBar( tr("Toolbar"), this );
130 setDockEnabled( topbar, DockTornOff, false );
131 setDockMenuEnabled( false );
133 _refresh = new QPushButton( tr( "Refresh" ), topbar );
134 connect( _refresh, SIGNAL( clicked() ), this, SLOT( slotListeSelect() ) );
135 QToolTip::add( _refresh, "", toolTipGroup(), tr("Immediately updates list of components") );
137 QPushButton* help = new QPushButton( tr( "Help" ), topbar );
138 connect( help, SIGNAL( clicked() ), this, SLOT( slotHelp() ) );
139 QToolTip::add( help, "", toolTipGroup(), tr("Opens Help window") );
141 _interval = new QPushButton( tr( "Interval" ), topbar );
142 connect( _interval, SIGNAL( clicked() ), this, SLOT( slotSelectRefresh() ) );
143 QToolTip::add( _interval, "", toolTipGroup(), tr("Changes refresh interval") );
145 // Display area and associated slots definition
146 _tabWidget = new QTabWidget( this );
147 _clients = new QListView( _tabWidget );
149 _history = new QListView( _tabWidget );
152 _tabWidget->addTab( _clients, tr( "Running" ) );
153 _tabWidget->addTab( _history, tr( "History" ) );
154 connect( _tabWidget, SIGNAL( currentChanged( QWidget* )), this, SLOT( slotListeSelect() ) );
155 connect( _clients, SIGNAL( clicked( QListViewItem* ) ), this, SLOT( slotClientChanged( QListViewItem* ) ) );
156 connect( _history, SIGNAL( clicked( QListViewItem* ) ), this, SLOT( slotHistoryChanged( QListViewItem* ) ) );
157 setCentralWidget( _tabWidget );
159 // Timer definition (used to automaticaly refresh the display area)
160 _counter = new QTimer( this );
161 connect( _counter, SIGNAL( timeout() ), this, SLOT( slotListeSelect() ) );
162 myRefreshInterval = 10;
163 _counter->start( myRefreshInterval * 1000 );
167 PIXELS largeur = 800 ;
168 PIXELS hauteur = 350 ;
169 setGeometry( xpos, ypos, largeur, hauteur ) ;
171 statusBar()->message(" ");
177 RegWidget::~RegWidget()
179 MESSAGE("Debut du Destructeur");
187 bool RegWidget::eventFilter( QObject* object, QEvent* event )
190 if ( object == myHelpWindow && event->type() == QEvent::Close ) {
193 else if ( object == myInfoWindow && event->type() == QEvent::Close ) {
196 else if ( object == myIntervalWindow && event->type() == QEvent::Close ) {
197 myIntervalWindow = 0;
200 return QMainWindow::eventFilter( object, event );
204 Searches item in the list and returns it's index or -1 if not found
206 int RegWidget::numitem(const QString &name, const QString &pid, const QString &machine,const Registry::AllInfos *listclient)
209 for (CORBA::ULong i=0; i<listclient->length(); i++)
211 const Registry::Infos & c_info=(*listclient)[i];
212 ASSERT( c_info.name!=NULL);
214 b.setNum(int(c_info.pid));
215 if ( (name.compare(QString(c_info.name)) == 0) &&
216 (machine.compare(QString(c_info.machine)) == 0) &&
217 (pid.compare(b) == 0) )
228 Returns text, containing information about client [ static ]
230 QString RegWidget::setlongText( const Registry::Infos &c_info)
232 BEGIN_OF("setlongText");
233 ASSERT( c_info.name != NULL );
234 QString a = QString( "<hr><h2>" ) + tr( "Code" ) + QString( " : " );
235 a.append( QString( c_info.name ) );
236 a.append( "</h1><hr><br>" );
237 a.append( tr( "Process Id" ) + QString( " : " ) );
238 a.append( BOLD( QString::number( int( c_info.pid ) ) ) );
239 a.append( QString( " " ) + tr( "on machine" ) + QString( " " ) );
240 a.append( BOLD( c_info.machine ) );
241 a.append( QString( " " ) + tr( "ip address" ) + QString( " : " ) );
242 a.append( BOLD( c_info.adip ) );
245 a.append( tr( "launched by user" ) + QString( " " ) );
246 a.append( BOLD( c_info.pwname ) );
247 a.append( QString( " ( " ) + tr( "pid" ) + QString( " : " ) );
248 a.append( BOLD( QString::number( int( c_info.uid ) ) ) );
249 a.append( QString( " )<br> " ) + tr( "in directory" ) + QString( " " ));
250 a.append( BOLD( c_info.cdir ) );
252 a.append( QString( "<br>" ) + tr( "begins" ) + QString( " " ) );
253 char * t1 = (char * )duplicate(ctime(&c_info.tc_start));
254 t1 [strlen(t1) -1 ] = ' ';
255 a.append( BOLD( t1 ) );
259 if (c_info.tc_hello != 0 )
261 char * t2 = (char * )duplicate(ctime(&c_info.tc_hello));
262 t2 [strlen(t2) -1 ] = ' ';
263 a.append( tr( "last signal" ) + QString(" : ") );
264 a.append( BOLD( t2 ) );
268 if ((c_info.tc_end - c_info.difftime) != 0)
270 char * t3 = (char * )duplicate(ctime(&c_info.tc_end));
271 t3 [strlen(t3) -1 ] = ' ';
272 a.append( tr( "ends" ) + QString( " " ) );
273 a.append( BOLD( t3 ) );
279 a.append( tr( "still running" ) + QString( "<br>" ) );
282 SCRUTE(c_info.difftime);
283 if (c_info.difftime!= 0)
285 a.append( QString( "(" ) + tr( "Time on" ) + QString( " " ) );
286 a.append( BOLD( c_info.machine ) );
287 a.append( QString( " " ) + tr( "differs from server's time. The difference is" ) + QString( " " ));
288 a.append( BOLD( QString::number( int( c_info.difftime ) ) ) );
289 a.append( QString( " " ) + tr( "seconds" ) + QString( ")<br>" ) );
291 END_OF("setlongText");
299 void RegWidget::closeEvent( QCloseEvent *e)
301 BEGIN_OF("closeEvent");
303 myInfoWindow->close();
305 myHelpWindow->close();
306 if (myIntervalWindow)
307 myIntervalWindow->close();
309 END_OF("closeEvent");
315 void RegWidget::SetListe()
317 BEGIN_OF("SetListe");
318 _clients->addColumn( tr( "Component" ), -1);
319 _clients->addColumn( tr( "PID" ), -1 );
320 _clients->addColumn( tr( "User Name" ), -1 );
321 _clients->addColumn( tr( "Machine" ), -1 );
322 _clients->addColumn( tr( "begins" ), -1 );
323 _clients->addColumn( tr( "hello" ) , -1 );
324 _clients->setColumnAlignment( 1, Qt::AlignRight );
331 void RegWidget::SetListeHistory()
333 BEGIN_OF("SetListeHistory")
334 _history->addColumn( tr( "Component" ), -1);
335 _history->addColumn( tr( "PID" ), -1 );
336 _history->addColumn( tr( "User Name" ), -1 );
337 _history->addColumn( tr( "Machine" ), -1 );
338 _history->addColumn( tr( "begins" ), -1 );
339 _history->addColumn( tr( "ends" ), -1 );
340 _history->setColumnAlignment( 1, Qt::AlignRight );
341 END_OF("SetListeHistory")
347 void RegWidget::InfoHistory()
350 BEGIN_OF("InfoHistory")
354 _serverhistory = _VarComponents->history();
355 for (CORBA::ULong i=0; i<_serverhistory->length(); i++)
357 const Registry::Infos & c_info=(*_serverhistory)[i];
358 ASSERT( c_info.name!=NULL);
360 a.setNum(int(c_info.pid));
361 char * t1 = (char * )duplicate(ctime(&c_info.tc_start));
362 t1 [strlen(t1) -1 ] = ' ';
363 char * t2 = (char * )duplicate(ctime(&c_info.tc_end));
364 t2 [strlen(t2) -1 ] = ' ';
365 QListViewItem * item = new QListViewItem(_history, QString(c_info.name),\
366 a, QString(c_info.pwname), QString(c_info.machine), \
367 QString(t1), QString(t2));
376 _interval->setDisabled( TRUE ) ;
377 _refresh->setDisabled( TRUE ) ;
379 MESSAGE("Sorry, No more Registry Server") ;
380 statusBar()->message( tr( "Sorry, No more Registry Server" ) ) ;
382 END_OF("InfoHistory")
388 void RegWidget::InfoReg()
394 _serverclients = _VarComponents->getall();
395 for (CORBA::ULong i=0; i<_serverclients->length(); i++)
397 const Registry::Infos & c_info=(*_serverclients)[i];
398 ASSERT( c_info.name!=NULL);
400 a.setNum(int(c_info.pid));
401 char * t1 = (char * )duplicate(ctime(&c_info.tc_start));
402 t1 [strlen(t1) -1 ] = ' ';
403 char * t2 = (char * )duplicate(ctime(&c_info.tc_hello));
404 t2 [strlen(t2) -1 ] = ' ';
405 QListViewItem * item = new QListViewItem(_clients, QString(c_info.name),\
406 a, QString(c_info.pwname), QString(c_info.machine), \
407 QString(t1), QString(t2));
416 _interval->setDisabled( TRUE ) ;
417 _refresh->setDisabled( TRUE ) ;
419 MESSAGE("Sorry, No more Registry Server") ;
420 statusBar()->message( tr( "Sorry, No more Registry Server" ) ) ;
426 Called when <Refresh> button is clicked
428 void RegWidget::slotListeSelect()
432 ASSERT(_tabWidget->currentPage() != NULL);
433 if (_tabWidget->currentPage () == _clients) InfoReg();
434 else if (_tabWidget->currentPage () == _history) InfoHistory();
438 MESSAGE("Sorry, No more Registry Server") ;
439 statusBar()->message( tr( "Sorry, No more Registry Server" ) ) ;
444 Called when <Interval> button is clicked (changing refresh interval)
446 void RegWidget::slotSelectRefresh()
448 BEGIN_OF("slotSelectRefresh");
449 myIntervalWindow = new IntervalWindow(this);
450 myIntervalWindow->installEventFilter( this );
451 myIntervalWindow->setValue(myRefreshInterval);
452 myIntervalWindow->show();
453 connect( myIntervalWindow->Cancel(), SIGNAL( clicked() ), myIntervalWindow, SLOT( close() ) );
454 connect( myIntervalWindow->Ok(), SIGNAL( clicked() ), this, SLOT( slotIntervalOk() ) );
455 END_OF("slotSelectRefresh");
458 void RegWidget::slotIntervalOk()
460 BEGIN_OF("slotIntervalOk");
461 myRefreshInterval = myIntervalWindow->getValue();
462 _counter->changeInterval( myRefreshInterval * 1000 );
463 SCRUTE(myRefreshInterval);
464 myIntervalWindow->close();
465 END_OF("slotIntervalOk");
468 Called when <Help> button is clicked
470 void RegWidget::slotHelp()
472 BEGIN_OF("slotHelp()");
474 if ( !myHelpWindow ) {
475 myHelpWindow = new HelpWindow( this );
476 myHelpWindow->installEventFilter( this );
478 myHelpWindow->show();
479 myHelpWindow->raise();
480 myHelpWindow->setActiveWindow();
482 END_OF("slotHelp()") ;
486 Called when user clicks on item in <Running> list
488 void RegWidget::slotClientChanged( QListViewItem* item )
490 BEGIN_OF("slotClientChanged()") ;
495 blockSignals( true ); // for sure that item will not be deleted when refreshing
497 int numeroItem = numitem(item->text(0), item->text(1), item->text(3), _serverclients);
499 SCRUTE(item->text(1)) ;
501 ASSERT(numeroItem>=0) ;
502 ASSERT((size_t)numeroItem<_serverclients->length()) ;
503 const Registry::Infos & c_info=(*_serverclients)[numeroItem];
504 ASSERT( c_info.name!=NULL);
506 if ( !myInfoWindow ) {
507 myInfoWindow = new InfoWindow( this );
508 myInfoWindow->installEventFilter( this );
510 QString a = tr( "More about" ) + QString( " " ) + QString( c_info.name );
511 myInfoWindow->setCaption(a);
512 myInfoWindow->setText( RegWidget::setlongText( c_info) );
513 myInfoWindow->show();
514 myInfoWindow->raise();
515 myInfoWindow->setActiveWindow();
517 blockSignals( false ); // enabling signals again
519 END_OF("slotClientChanged()") ;
524 Called when user clicks on item in <History> list
526 void RegWidget::slotHistoryChanged( QListViewItem* item )
529 BEGIN_OF("slotHistoryChanged()") ;
534 blockSignals( true ); // for sure that item will not be deleted when refreshing
536 int numeroItem = numitem(item->text(0), item->text(1), item->text(3), _serverhistory);
539 SCRUTE(item->text(1)) ;
540 ASSERT(numeroItem>=0) ;
541 ASSERT((size_t)numeroItem<_serverhistory->length()) ;
542 const Registry::Infos & c_info=(*_serverhistory)[numeroItem];
543 ASSERT( c_info.name!=NULL);
545 if ( !myInfoWindow ) {
546 myInfoWindow = new InfoWindow( this );
547 myInfoWindow->installEventFilter( this );
549 QString a = tr( "More about" ) + QString( " " ) + QString( c_info.name );
550 myInfoWindow->setCaption(a);
551 myInfoWindow->setText( RegWidget::setlongText( c_info ) );
552 myInfoWindow->show();
553 myInfoWindow->raise();
554 myInfoWindow->setActiveWindow();
556 blockSignals( false ); // enabling signals again
558 END_OF("slotHistoryChanged()") ;
565 InfoWindow::InfoWindow( QWidget* parent, const char* name )
566 : QMainWindow( parent, name, WType_TopLevel | WDestructiveClose )
568 BEGIN_OF("InfoWindow");
569 myTextView = new QTextView( this, "myTextView" );
570 setCentralWidget( myTextView );
571 setMinimumSize( 450, 250 );
572 END_OF("InfoWindow");
578 void InfoWindow::setText( const QString& text )
580 myTextView->setText( text );
583 static const char* SEPARATOR = ":";
585 QString findFile( QString filename )
590 // Try ${HOME}/.salome/resources directory
591 cenv = getenv( "HOME" );
593 dir.sprintf( "%s", cenv );
594 if ( !dir.isEmpty() ) {
595 dir = addSlash(dir) ;
596 dir = dir + ".salome" ;
597 dir = addSlash(dir) ;
598 dir = dir + "resources" ;
599 dir = addSlash(dir) ;
600 QFileInfo fileInfo( dir + filename );
601 if ( fileInfo.isFile() && fileInfo.exists() )
602 return fileInfo.filePath();
605 // Try ${SALOME_SITE_DIR}/share/salome/resources directory
606 cenv = getenv( "SALOME_SITE_DIR" );
608 dir.sprintf( "%s", cenv );
609 if ( !dir.isEmpty() ) {
610 dir = addSlash(dir) ;
611 dir = dir + "share" ;
612 dir = addSlash(dir) ;
613 cenv = getenv("SALOME_SITE_NAME");
614 if (cenv) dir = dir + cenv;
615 else dir = dir + "salome" ;
616 dir = addSlash(dir) ;
617 dir = dir + "resources" ;
618 dir = addSlash(dir) ;
619 QFileInfo fileInfo( dir + filename );
620 if ( fileInfo.isFile() && fileInfo.exists() )
621 return fileInfo.filePath();
624 // Try ${SALOME_ROOT_DIR}/share/salome/resources directory
625 cenv = getenv( "SALOME_ROOT_DIR" );
627 dir.sprintf( "%s", cenv );
628 if ( !dir.isEmpty() ) {
629 dir = addSlash(dir) ;
630 dir = dir + "share" ;
631 dir = addSlash(dir) ;
632 dir = dir + "salome" ;
633 dir = addSlash(dir) ;
634 dir = dir + "resources" ;
635 dir = addSlash(dir) ;
636 QFileInfo fileInfo( dir + filename );
637 if ( fileInfo.isFile() && fileInfo.exists() )
638 return fileInfo.filePath();
641 // Try CSF_SaloameResources env.var directory ( or directory list )
642 cenv = getenv( "CSF_SalomeResources" );
644 dir.sprintf( "%s", cenv );
645 if ( !dir.isEmpty() ) {
646 QStringList dirList = QStringList::split( SEPARATOR, dir, false ); // skip empty entries
647 for ( int i = 0; i < dirList.count(); i++ ) {
648 QFileInfo fileInfo( addSlash( dirList[ i ] ) + filename );
649 if ( fileInfo.isFile() && fileInfo.exists() )
650 return fileInfo.filePath();
656 QString addSlash( const QString& path )
658 if (!path.isNull()) {
664 if ( path.at(path.length()-1) != slash )