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.h"
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>" ) )
60 static const char* const time_data[] = {
85 static const char* const close_data[] = {
110 static const char* const refresh_data[] = {
135 Creates components list
138 Registry::Components_var MakeRegistry( CORBA::ORB_var &orb )
141 const char *registryName="Registry" ;
143 SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
144 naming.init_orb( orb ) ;
146 // Recuperation de la reference de l'objet
147 CORBA::Object_var object = 0 ;
150 SCRUTE(registryName) ;
151 object = naming.Resolve( registryName ) ;
152 if(CORBA::is_nil(object)) throw CommException( "unable to find the RegistryService" ) ;
154 catch( const ServiceUnreachable &ex )
157 exit( EXIT_FAILURE ) ;
159 catch( const CORBA::Exception &exx )
161 exit( EXIT_FAILURE ) ;
164 // Specialisation de l'objet generique
166 return Registry::Components::_narrow( object ) ;
170 Only one global registry window should exist at the moment
171 This method creates registry window if necessary and returns it
174 RegWidget* RegWidget::GetRegWidget( CORBA::ORB_var &orb , QWidget *parent, const char *name )
176 if ( !myRegWidgetPtr )
177 myRegWidgetPtr = new RegWidget( orb, parent, name );
178 return myRegWidgetPtr;
184 RegWidget::RegWidget(CORBA::ORB_var &orb, QWidget *parent, const char *name )
185 : QMainWindow( parent, name, WType_TopLevel | WDestructiveClose ),
186 _VarComponents( MakeRegistry(orb) ),
187 _clients(0), _history(0), _parent( parent ),
188 _tabWidget(0), _refresh(0), _interval(0),
189 myInfoWindow(0), myHelpWindow(0), myIntervalWindow(0)
191 QString aFile = findFile("default.png");
192 /* char* dir = getenv( "CSF_ResourcesDefaults" );
196 path = qDir.filePath( "default.png" );
198 QPixmap pm ( aFile );
202 // pixmap for buttons
203 QPixmap image_refresh ( ( const char** ) refresh_data );
204 QPixmap image_interval( ( const char** ) time_data );
205 QPixmap image_close ( ( const char** ) close_data );
207 // Buttons definition
208 QToolBar* topbar = new QToolBar( tr("Toolbar"), this );
209 setDockEnabled( topbar, DockTornOff, false );
210 setDockMenuEnabled( false );
212 _refresh = new QPushButton( tr( "Refresh" ), topbar );
213 _refresh->setIconSet( image_refresh );
214 _refresh->setFocusPolicy( NoFocus );
215 connect( _refresh, SIGNAL( clicked() ), this, SLOT( slotListeSelect() ) );
216 QToolTip::add( _refresh, "", toolTipGroup(), tr("Immediately updates list of components") );
218 /* PAL5540 - this button is needless
219 QPushButton* help = new QPushButton( tr( "Help" ), topbar );
220 connect( help, SIGNAL( clicked() ), this, SLOT( slotHelp() ) );
221 QToolTip::add( help, "", toolTipGroup(), tr("Opens Help window") );
224 _interval = new QPushButton( tr( "Interval" ), topbar );
225 _interval->setIconSet( image_interval );
226 _interval->setFocusPolicy( NoFocus );
227 connect( _interval, SIGNAL( clicked() ), this, SLOT( slotSelectRefresh() ) );
228 QToolTip::add( _interval, "", toolTipGroup(), tr("Changes refresh interval") );
230 topbar->addSeparator();
231 _close = new QPushButton( tr("Close"), topbar );
232 _close->setIconSet( image_close );
233 _close->setFocusPolicy( NoFocus );
234 connect( _close, SIGNAL( clicked() ), this, SLOT( close() ) );
235 QToolTip::add( _close, "", toolTipGroup(), tr("Closes Registry window") );
237 // Display area and associated slots definition
238 _tabWidget = new QTabWidget( this );
239 _clients = new QListView( _tabWidget );
241 _history = new QListView( _tabWidget );
244 _tabWidget->addTab( _clients, tr( "Running" ) );
245 _tabWidget->addTab( _history, tr( "History" ) );
246 connect( _tabWidget, SIGNAL( currentChanged( QWidget* )), this, SLOT( slotListeSelect() ) );
247 connect( _clients, SIGNAL( clicked( QListViewItem* ) ), this, SLOT( slotClientChanged( QListViewItem* ) ) );
248 connect( _history, SIGNAL( clicked( QListViewItem* ) ), this, SLOT( slotHistoryChanged( QListViewItem* ) ) );
249 setCentralWidget( _tabWidget );
251 // Timer definition (used to automaticaly refresh the display area)
252 _counter = new QTimer( this );
253 connect( _counter, SIGNAL( timeout() ), this, SLOT( slotListeSelect() ) );
254 myRefreshInterval = 10;
255 _counter->start( myRefreshInterval * 1000 );
259 PIXELS largeur = 800 ;
260 PIXELS hauteur = 350 ;
261 setGeometry( xpos, ypos, largeur, hauteur ) ;
263 statusBar()->message(" ");
269 RegWidget::~RegWidget()
271 MESSAGE("Debut du Destructeur");
279 bool RegWidget::eventFilter( QObject* object, QEvent* event )
282 if ( object == myHelpWindow && event->type() == QEvent::Close ) {
285 else if ( object == myInfoWindow && event->type() == QEvent::Close ) {
288 else if ( object == myIntervalWindow && event->type() == QEvent::Close ) {
289 myIntervalWindow = 0;
291 else if ( object == _clients && event->type() == QEvent::KeyPress ) {
292 QKeyEvent* ke = (QKeyEvent*)event;
293 if ( ke->key() == Key_Enter || ke->key() == Key_Return ) {
294 slotClientChanged( _clients->currentItem() );
297 else if ( object == _history && event->type() == QEvent::KeyPress ) {
298 QKeyEvent* ke = (QKeyEvent*)event;
299 if ( ke->key() == Key_Enter || ke->key() == Key_Return ) {
300 slotHistoryChanged( _history->currentItem() );
304 return QMainWindow::eventFilter( object, event );
308 Searches item in the list and returns it's index or -1 if not found
310 int RegWidget::numitem(const QString &name, const QString &pid, const QString &machine,const Registry::AllInfos *listclient)
313 for (CORBA::ULong i=0; i<listclient->length(); i++)
315 const Registry::Infos & c_info=(*listclient)[i];
316 ASSERT( c_info.name!=NULL);
318 b.setNum(int(c_info.pid));
319 if ( (name.compare(QString(c_info.name)) == 0) &&
320 (machine.compare(QString(c_info.machine)) == 0) &&
321 (pid.compare(b) == 0) )
332 Returns text, containing information about client [ static ]
334 QString RegWidget::setlongText( const Registry::Infos &c_info)
336 BEGIN_OF("setlongText");
337 ASSERT( c_info.name != NULL );
338 QString a = QString( "<hr><h2>" ) + tr( "Code" ) + QString( " : " );
339 a.append( QString( c_info.name ) );
340 a.append( "</h1><hr><br>" );
341 a.append( tr( "Process Id" ) + QString( " : " ) );
342 a.append( BOLD( QString::number( int( c_info.pid ) ) ) );
343 a.append( QString( " " ) + tr( "on machine" ) + QString( " " ) );
344 a.append( BOLD( c_info.machine ) );
345 a.append( QString( " " ) + tr( "ip address" ) + QString( " : " ) );
346 a.append( BOLD( c_info.adip ) );
349 a.append( tr( "launched by user" ) + QString( " " ) );
350 a.append( BOLD( c_info.pwname ) );
351 a.append( QString( " ( " ) + tr( "pid" ) + QString( " : " ) );
352 a.append( BOLD( QString::number( int( c_info.uid ) ) ) );
353 a.append( QString( " )<br> " ) + tr( "in directory" ) + QString( " " ));
354 a.append( BOLD( c_info.cdir ) );
356 a.append( QString( "<br>" ) + tr( "begins" ) + QString( " " ) );
357 char * t1 = (char * )duplicate(ctime(&c_info.tc_start));
358 t1 [strlen(t1) -1 ] = ' ';
359 a.append( BOLD( t1 ) );
363 if (c_info.tc_hello != 0 )
365 char * t2 = (char * )duplicate(ctime(&c_info.tc_hello));
366 t2 [strlen(t2) -1 ] = ' ';
367 a.append( tr( "last signal" ) + QString(" : ") );
368 a.append( BOLD( t2 ) );
372 if ((c_info.tc_end - c_info.difftime) != 0)
374 char * t3 = (char * )duplicate(ctime(&c_info.tc_end));
375 t3 [strlen(t3) -1 ] = ' ';
376 a.append( tr( "ends" ) + QString( " " ) );
377 a.append( BOLD( t3 ) );
383 a.append( tr( "still running" ) + QString( "<br>" ) );
386 SCRUTE(c_info.difftime);
387 if (c_info.difftime!= 0)
389 a.append( QString( "(" ) + tr( "Time on" ) + QString( " " ) );
390 a.append( BOLD( c_info.machine ) );
391 a.append( QString( " " ) + tr( "differs from server's time. The difference is" ) + QString( " " ));
392 a.append( BOLD( QString::number( int( c_info.difftime ) ) ) );
393 a.append( QString( " " ) + tr( "seconds" ) + QString( ")<br>" ) );
395 END_OF("setlongText");
403 void RegWidget::closeEvent( QCloseEvent *e)
405 BEGIN_OF("closeEvent");
407 myInfoWindow->close();
409 myHelpWindow->close();
410 if (myIntervalWindow)
411 myIntervalWindow->close();
413 END_OF("closeEvent");
419 void RegWidget::SetListe()
421 BEGIN_OF("SetListe");
422 _clients->installEventFilter( this );
423 _clients->setAllColumnsShowFocus( true );
424 _clients->addColumn( tr( "Component" ), -1);
425 _clients->addColumn( tr( "PID" ), -1 );
426 _clients->addColumn( tr( "User Name" ), -1 );
427 _clients->addColumn( tr( "Machine" ), -1 );
428 _clients->addColumn( tr( "begins" ), -1 );
429 _clients->addColumn( tr( "hello" ) , -1 );
430 _clients->setColumnAlignment( 1, Qt::AlignRight );
437 void RegWidget::SetListeHistory()
439 BEGIN_OF("SetListeHistory")
440 _history->installEventFilter( this );
441 _history->setAllColumnsShowFocus( true );
442 _history->addColumn( tr( "Component" ), -1);
443 _history->addColumn( tr( "PID" ), -1 );
444 _history->addColumn( tr( "User Name" ), -1 );
445 _history->addColumn( tr( "Machine" ), -1 );
446 _history->addColumn( tr( "begins" ), -1 );
447 _history->addColumn( tr( "ends" ), -1 );
448 _history->setColumnAlignment( 1, Qt::AlignRight );
449 END_OF("SetListeHistory")
455 void RegWidget::InfoHistory()
458 BEGIN_OF("InfoHistory")
462 _serverhistory = _VarComponents->history();
463 for (CORBA::ULong i=0; i<_serverhistory->length(); i++)
465 const Registry::Infos & c_info=(*_serverhistory)[i];
466 ASSERT( c_info.name!=NULL);
468 a.setNum(int(c_info.pid));
469 char * t1 = (char * )duplicate(ctime(&c_info.tc_start));
470 t1 [strlen(t1) -1 ] = ' ';
471 char * t2 = (char * )duplicate(ctime(&c_info.tc_end));
472 t2 [strlen(t2) -1 ] = ' ';
473 QListViewItem * item = new QListViewItem(_history, QString(c_info.name),\
474 a, QString(c_info.pwname), QString(c_info.machine), \
475 QString(t1), QString(t2));
484 _interval->setDisabled( TRUE ) ;
485 _refresh->setDisabled( TRUE ) ;
487 MESSAGE("Sorry, No more Registry Server") ;
488 statusBar()->message( tr( "Sorry, No more Registry Server" ) ) ;
490 END_OF("InfoHistory")
496 void RegWidget::InfoReg()
502 _serverclients = _VarComponents->getall();
503 for (CORBA::ULong i=0; i<_serverclients->length(); i++)
505 const Registry::Infos & c_info=(*_serverclients)[i];
506 ASSERT( c_info.name!=NULL);
508 a.setNum(int(c_info.pid));
509 char * t1 = (char * )duplicate(ctime(&c_info.tc_start));
510 t1 [strlen(t1) -1 ] = ' ';
511 char * t2 = (char * )duplicate(ctime(&c_info.tc_hello));
512 t2 [strlen(t2) -1 ] = ' ';
513 QListViewItem * item = new QListViewItem(_clients, QString(c_info.name),\
514 a, QString(c_info.pwname), QString(c_info.machine), \
515 QString(t1), QString(t2));
524 _interval->setDisabled( TRUE ) ;
525 _refresh->setDisabled( TRUE ) ;
527 MESSAGE("Sorry, No more Registry Server") ;
528 statusBar()->message( tr( "Sorry, No more Registry Server" ) ) ;
534 Called when <Refresh> button is clicked
536 void RegWidget::slotListeSelect()
540 ASSERT(_tabWidget->currentPage() != NULL);
541 if (_tabWidget->currentPage () == _clients) InfoReg();
542 else if (_tabWidget->currentPage () == _history) InfoHistory();
546 MESSAGE("Sorry, No more Registry Server") ;
547 statusBar()->message( tr( "Sorry, No more Registry Server" ) ) ;
552 Called when <Interval> button is clicked (changing refresh interval)
554 void RegWidget::slotSelectRefresh()
556 BEGIN_OF("slotSelectRefresh");
557 myIntervalWindow = new IntervalWindow(this);
558 myIntervalWindow->installEventFilter( this );
559 myIntervalWindow->setValue(myRefreshInterval);
560 myIntervalWindow->show();
561 connect( myIntervalWindow->Cancel(), SIGNAL( clicked() ), myIntervalWindow, SLOT( close() ) );
562 connect( myIntervalWindow->Ok(), SIGNAL( clicked() ), this, SLOT( slotIntervalOk() ) );
563 END_OF("slotSelectRefresh");
566 void RegWidget::slotIntervalOk()
568 BEGIN_OF("slotIntervalOk");
569 myRefreshInterval = myIntervalWindow->getValue();
570 _counter->changeInterval( myRefreshInterval * 1000 );
571 SCRUTE(myRefreshInterval);
572 myIntervalWindow->close();
573 END_OF("slotIntervalOk");
576 Called when <Help> button is clicked
578 void RegWidget::slotHelp()
580 BEGIN_OF("slotHelp()");
582 if ( !myHelpWindow ) {
583 myHelpWindow = new HelpWindow( this );
584 myHelpWindow->installEventFilter( this );
586 myHelpWindow->show();
587 myHelpWindow->raise();
588 myHelpWindow->setActiveWindow();
590 END_OF("slotHelp()") ;
594 Called when user clicks on item in <Running> list
596 void RegWidget::slotClientChanged( QListViewItem* item )
598 BEGIN_OF("slotClientChanged()") ;
603 blockSignals( true ); // for sure that item will not be deleted when refreshing
605 int numeroItem = numitem(item->text(0), item->text(1), item->text(3), _serverclients);
607 SCRUTE(item->text(1)) ;
609 ASSERT(numeroItem>=0) ;
610 ASSERT((size_t)numeroItem<_serverclients->length()) ;
611 const Registry::Infos & c_info=(*_serverclients)[numeroItem];
612 ASSERT( c_info.name!=NULL);
614 if ( !myInfoWindow ) {
615 myInfoWindow = new InfoWindow( this );
616 myInfoWindow->installEventFilter( this );
618 QString a = tr( "More about" ) + QString( " " ) + QString( c_info.name );
619 myInfoWindow->setCaption(a);
620 myInfoWindow->setText( RegWidget::setlongText( c_info) );
621 myInfoWindow->show();
622 myInfoWindow->raise();
623 myInfoWindow->setActiveWindow();
625 blockSignals( false ); // enabling signals again
627 END_OF("slotClientChanged()") ;
632 Called when user clicks on item in <History> list
634 void RegWidget::slotHistoryChanged( QListViewItem* item )
637 BEGIN_OF("slotHistoryChanged()") ;
642 blockSignals( true ); // for sure that item will not be deleted when refreshing
644 int numeroItem = numitem(item->text(0), item->text(1), item->text(3), _serverhistory);
647 SCRUTE(item->text(1)) ;
648 ASSERT(numeroItem>=0) ;
649 ASSERT((size_t)numeroItem<_serverhistory->length()) ;
650 const Registry::Infos & c_info=(*_serverhistory)[numeroItem];
651 ASSERT( c_info.name!=NULL);
653 if ( !myInfoWindow ) {
654 myInfoWindow = new InfoWindow( this );
655 myInfoWindow->installEventFilter( this );
657 QString a = tr( "More about" ) + QString( " " ) + QString( c_info.name );
658 myInfoWindow->setCaption(a);
659 myInfoWindow->setText( RegWidget::setlongText( c_info ) );
660 myInfoWindow->show();
661 myInfoWindow->raise();
662 myInfoWindow->setActiveWindow();
664 blockSignals( false ); // enabling signals again
666 END_OF("slotHistoryChanged()") ;
673 InfoWindow::InfoWindow( QWidget* parent, const char* name )
674 : QMainWindow( parent, name, WType_TopLevel | WDestructiveClose )
676 BEGIN_OF("InfoWindow");
677 myTextView = new QTextView( this, "myTextView" );
678 setCentralWidget( myTextView );
679 setMinimumSize( 450, 250 );
680 END_OF("InfoWindow");
686 void InfoWindow::setText( const QString& text )
688 myTextView->setText( text );
694 void InfoWindow::keyPressEvent( QKeyEvent * e )
696 QMainWindow::keyPressEvent( e );
697 if ( e->key() == Key_Escape )
701 static const char* SEPARATOR = ":";
703 QString findFile( QString filename )
708 // Try ${HOME}/.salome/resources directory
709 cenv = getenv( "HOME" );
711 dir.sprintf( "%s", cenv );
712 if ( !dir.isEmpty() ) {
713 dir = addSlash(dir) ;
714 dir = dir + ".salome" ;
715 dir = addSlash(dir) ;
716 dir = dir + "resources" ;
717 dir = addSlash(dir) ;
718 QFileInfo fileInfo( dir + filename );
719 if ( fileInfo.isFile() && fileInfo.exists() )
720 return fileInfo.filePath();
723 // Try ${SALOME_SITE_DIR}/share/salome/resources directory
724 cenv = getenv( "SALOME_SITE_DIR" );
726 dir.sprintf( "%s", cenv );
727 if ( !dir.isEmpty() ) {
728 dir = addSlash(dir) ;
729 dir = dir + "share" ;
730 dir = addSlash(dir) ;
731 cenv = getenv("SALOME_SITE_NAME");
732 if (cenv) dir = dir + cenv;
733 else dir = dir + "salome" ;
734 dir = addSlash(dir) ;
735 dir = dir + "resources" ;
736 dir = addSlash(dir) ;
737 QFileInfo fileInfo( dir + filename );
738 if ( fileInfo.isFile() && fileInfo.exists() )
739 return fileInfo.filePath();
742 // Try ${KERNEL_ROOT_DIR}/share/salome/resources directory
743 cenv = getenv( "KERNEL_ROOT_DIR" );
745 dir.sprintf( "%s", cenv );
746 if ( !dir.isEmpty() ) {
747 dir = addSlash(dir) ;
748 dir = dir + "share" ;
749 dir = addSlash(dir) ;
750 dir = dir + "salome" ;
751 dir = addSlash(dir) ;
752 dir = dir + "resources" ;
753 dir = addSlash(dir) ;
754 QFileInfo fileInfo( dir + filename );
755 if ( fileInfo.isFile() && fileInfo.exists() )
756 return fileInfo.filePath();
760 //SRN Added support for SALOMEGUI
761 cenv = getenv( "SALOMEGUI_ROOT_DIR" );
763 dir.sprintf( "%s", cenv );
764 if ( !dir.isEmpty() ) {
765 dir = addSlash(dir) ;
766 dir = dir + "share" ;
767 dir = addSlash(dir) ;
768 dir = dir + "salome" ;
769 dir = addSlash(dir) ;
770 dir = dir + "resources" ;
771 dir = addSlash(dir) ;
772 QFileInfo fileInfo( dir + filename );
773 if ( fileInfo.isFile() && fileInfo.exists() )
774 return fileInfo.filePath();
778 // Try CSF_SaloameResources env.var directory ( or directory list )
779 cenv = getenv( "CSF_SalomeResources" );
781 dir.sprintf( "%s", cenv );
782 if ( !dir.isEmpty() ) {
783 QStringList dirList = QStringList::split( SEPARATOR, dir, false ); // skip empty entries
784 for ( int i = 0; i < dirList.count(); i++ ) {
785 QFileInfo fileInfo( addSlash( dirList[ i ] ) + filename );
786 if ( fileInfo.isFile() && fileInfo.exists() )
787 return fileInfo.filePath();
793 QString addSlash( const QString& path )
795 if (!path.isNull()) {
801 if ( path.at(path.length()-1) != slash )