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 : ToolsGUI_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 "ToolsGUI_RegWidget.h"
49 # include "ToolsGUI_HelpWindow.h"
50 # include "ToolsGUI_IntervalWindow.h"
57 ToolsGUI_RegWidget* ToolsGUI_RegWidget::myRegWidgetPtr = 0;
59 QString addSlash( const QString& );
60 QString findFile( QString filename );
62 #define BOLD( text ) ( QString( "<b>" ) + QString( text ) + QString( "</b>" ) )
64 static const char* const time_data[] = {
89 static const char* const close_data[] = {
114 static const char* const refresh_data[] = {
139 Creates components list
142 Registry::Components_var MakeRegistry( CORBA::ORB_var &orb )
145 const char *registryName="Registry" ;
147 SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
148 naming.init_orb( orb ) ;
150 // Recuperation de la reference de l'objet
151 CORBA::Object_var object = 0 ;
154 SCRUTE(registryName) ;
155 object = naming.Resolve( registryName ) ;
156 if(CORBA::is_nil(object)) throw CommException( "unable to find the RegistryService" ) ;
158 catch( const ServiceUnreachable &ex )
161 exit( EXIT_FAILURE ) ;
163 catch( const CORBA::Exception &exx )
165 exit( EXIT_FAILURE ) ;
168 // Specialisation de l'objet generique
170 return Registry::Components::_narrow( object ) ;
174 Only one global registry window should exist at the moment
175 This method creates registry window if necessary and returns it
178 ToolsGUI_RegWidget* ToolsGUI_RegWidget::GetRegWidget( CORBA::ORB_var &orb , QWidget *parent, const char *name )
180 if ( !myRegWidgetPtr )
181 myRegWidgetPtr = new ToolsGUI_RegWidget( orb, parent, name );
182 return myRegWidgetPtr;
188 ToolsGUI_RegWidget::ToolsGUI_RegWidget(CORBA::ORB_var &orb, QWidget *parent, const char *name )
189 : QMainWindow( parent, name, WType_TopLevel | WDestructiveClose ),
190 _VarComponents( MakeRegistry(orb) ),
191 _clients(0), _history(0), _parent( parent ),
192 _tabWidget(0), _refresh(0), _interval(0),
193 myInfoWindow(0), myHelpWindow(0), myIntervalWindow(0)
195 QString aFile = findFile("default.png");
196 /* char* dir = getenv( "CSF_ResourcesDefaults" );
200 path = qDir.filePath( "default.png" );
202 QPixmap pm ( aFile );
206 // pixmap for buttons
207 QPixmap image_refresh ( ( const char** ) refresh_data );
208 QPixmap image_interval( ( const char** ) time_data );
209 QPixmap image_close ( ( const char** ) close_data );
211 // Buttons definition
212 QToolBar* topbar = new QToolBar( tr("Toolbar"), this );
213 setDockEnabled( topbar, DockTornOff, false );
214 setDockMenuEnabled( false );
216 _refresh = new QPushButton( tr( "Refresh" ), topbar );
217 _refresh->setIconSet( image_refresh );
218 _refresh->setFocusPolicy( NoFocus );
219 connect( _refresh, SIGNAL( clicked() ), this, SLOT( slotListeSelect() ) );
220 QToolTip::add( _refresh, "", toolTipGroup(), tr("Immediately updates list of components") );
222 /* PAL5540 - this button is needless
223 QPushButton* help = new QPushButton( tr( "Help" ), topbar );
224 connect( help, SIGNAL( clicked() ), this, SLOT( slotHelp() ) );
225 QToolTip::add( help, "", toolTipGroup(), tr("Opens Help window") );
228 _interval = new QPushButton( tr( "Interval" ), topbar );
229 _interval->setIconSet( image_interval );
230 _interval->setFocusPolicy( NoFocus );
231 connect( _interval, SIGNAL( clicked() ), this, SLOT( slotSelectRefresh() ) );
232 QToolTip::add( _interval, "", toolTipGroup(), tr("Changes refresh interval") );
234 topbar->addSeparator();
235 _close = new QPushButton( tr("Close"), topbar );
236 _close->setIconSet( image_close );
237 _close->setFocusPolicy( NoFocus );
238 connect( _close, SIGNAL( clicked() ), this, SLOT( close() ) );
239 QToolTip::add( _close, "", toolTipGroup(), tr("Closes Registry window") );
241 // Display area and associated slots definition
242 _tabWidget = new QTabWidget( this );
243 _clients = new QListView( _tabWidget );
245 _history = new QListView( _tabWidget );
248 _tabWidget->addTab( _clients, tr( "Running" ) );
249 _tabWidget->addTab( _history, tr( "History" ) );
250 connect( _tabWidget, SIGNAL( currentChanged( QWidget* )), this, SLOT( slotListeSelect() ) );
251 connect( _clients, SIGNAL( clicked( QListViewItem* ) ), this, SLOT( slotClientChanged( QListViewItem* ) ) );
252 connect( _history, SIGNAL( clicked( QListViewItem* ) ), this, SLOT( slotHistoryChanged( QListViewItem* ) ) );
253 setCentralWidget( _tabWidget );
255 // Timer definition (used to automaticaly refresh the display area)
256 _counter = new QTimer( this );
257 connect( _counter, SIGNAL( timeout() ), this, SLOT( slotListeSelect() ) );
258 myRefreshInterval = 10;
259 _counter->start( myRefreshInterval * 1000 );
263 PIXELS largeur = 800 ;
264 PIXELS hauteur = 350 ;
265 setGeometry( xpos, ypos, largeur, hauteur ) ;
267 statusBar()->message(" ");
273 ToolsGUI_RegWidget::~ToolsGUI_RegWidget()
275 MESSAGE("Debut du Destructeur");
283 bool ToolsGUI_RegWidget::eventFilter( QObject* object, QEvent* event )
286 if ( object == myHelpWindow && event->type() == QEvent::Close ) {
289 else if ( object == myInfoWindow && event->type() == QEvent::Close ) {
292 else if ( object == myIntervalWindow && event->type() == QEvent::Close ) {
293 myIntervalWindow = 0;
295 else if ( object == _clients && event->type() == QEvent::KeyPress ) {
296 QKeyEvent* ke = (QKeyEvent*)event;
297 if ( ke->key() == Key_Enter || ke->key() == Key_Return ) {
298 slotClientChanged( _clients->currentItem() );
301 else if ( object == _history && event->type() == QEvent::KeyPress ) {
302 QKeyEvent* ke = (QKeyEvent*)event;
303 if ( ke->key() == Key_Enter || ke->key() == Key_Return ) {
304 slotHistoryChanged( _history->currentItem() );
308 return QMainWindow::eventFilter( object, event );
312 Searches item in the list and returns it's index or -1 if not found
314 int ToolsGUI_RegWidget::numitem(const QString &name, const QString &pid, const QString &machine,const Registry::AllInfos *listclient)
317 for (CORBA::ULong i=0; i<listclient->length(); i++)
319 const Registry::Infos & c_info=(*listclient)[i];
320 ASSERT( c_info.name!=NULL);
322 b.setNum(int(c_info.pid));
323 if ( (name.compare(QString(c_info.name)) == 0) &&
324 (machine.compare(QString(c_info.machine)) == 0) &&
325 (pid.compare(b) == 0) )
336 Returns text, containing information about client [ static ]
338 QString ToolsGUI_RegWidget::setlongText( const Registry::Infos &c_info)
340 BEGIN_OF("setlongText");
341 ASSERT( c_info.name != NULL );
342 QString a = QString( "<hr><h2>" ) + tr( "Code" ) + QString( " : " );
343 a.append( QString( c_info.name ) );
344 a.append( "</h1><hr><br>" );
345 a.append( tr( "Process Id" ) + QString( " : " ) );
346 a.append( BOLD( QString::number( int( c_info.pid ) ) ) );
347 a.append( QString( " " ) + tr( "on machine" ) + QString( " " ) );
348 a.append( BOLD( c_info.machine ) );
349 a.append( QString( " " ) + tr( "ip address" ) + QString( " : " ) );
350 a.append( BOLD( c_info.adip ) );
353 a.append( tr( "launched by user" ) + QString( " " ) );
354 a.append( BOLD( c_info.pwname ) );
355 a.append( QString( " ( " ) + tr( "pid" ) + QString( " : " ) );
356 a.append( BOLD( QString::number( int( c_info.uid ) ) ) );
357 a.append( QString( " )<br> " ) + tr( "in directory" ) + QString( " " ));
358 a.append( BOLD( c_info.cdir ) );
360 a.append( QString( "<br>" ) + tr( "begins" ) + QString( " " ) );
361 char * t1 = (char * )duplicate(ctime(&c_info.tc_start));
362 t1 [strlen(t1) -1 ] = ' ';
363 a.append( BOLD( t1 ) );
367 if (c_info.tc_hello != 0 )
369 char * t2 = (char * )duplicate(ctime(&c_info.tc_hello));
370 t2 [strlen(t2) -1 ] = ' ';
371 a.append( tr( "last signal" ) + QString(" : ") );
372 a.append( BOLD( t2 ) );
376 if ((c_info.tc_end - c_info.difftime) != 0)
378 char * t3 = (char * )duplicate(ctime(&c_info.tc_end));
379 t3 [strlen(t3) -1 ] = ' ';
380 a.append( tr( "ends" ) + QString( " " ) );
381 a.append( BOLD( t3 ) );
387 a.append( tr( "still running" ) + QString( "<br>" ) );
390 SCRUTE(c_info.difftime);
391 if (c_info.difftime!= 0)
393 a.append( QString( "(" ) + tr( "Time on" ) + QString( " " ) );
394 a.append( BOLD( c_info.machine ) );
395 a.append( QString( " " ) + tr( "differs from server's time. The difference is" ) + QString( " " ));
396 a.append( BOLD( QString::number( int( c_info.difftime ) ) ) );
397 a.append( QString( " " ) + tr( "seconds" ) + QString( ")<br>" ) );
399 END_OF("setlongText");
407 void ToolsGUI_RegWidget::closeEvent( QCloseEvent *e)
409 BEGIN_OF("closeEvent");
411 myInfoWindow->close();
413 myHelpWindow->close();
414 if (myIntervalWindow)
415 myIntervalWindow->close();
417 END_OF("closeEvent");
423 void ToolsGUI_RegWidget::SetListe()
425 BEGIN_OF("SetListe");
426 _clients->installEventFilter( this );
427 _clients->setAllColumnsShowFocus( true );
428 _clients->addColumn( tr( "Component" ), -1);
429 _clients->addColumn( tr( "PID" ), -1 );
430 _clients->addColumn( tr( "User Name" ), -1 );
431 _clients->addColumn( tr( "Machine" ), -1 );
432 _clients->addColumn( tr( "begins" ), -1 );
433 _clients->addColumn( tr( "hello" ) , -1 );
434 _clients->setColumnAlignment( 1, Qt::AlignRight );
441 void ToolsGUI_RegWidget::SetListeHistory()
443 BEGIN_OF("SetListeHistory")
444 _history->installEventFilter( this );
445 _history->setAllColumnsShowFocus( true );
446 _history->addColumn( tr( "Component" ), -1);
447 _history->addColumn( tr( "PID" ), -1 );
448 _history->addColumn( tr( "User Name" ), -1 );
449 _history->addColumn( tr( "Machine" ), -1 );
450 _history->addColumn( tr( "begins" ), -1 );
451 _history->addColumn( tr( "ends" ), -1 );
452 _history->setColumnAlignment( 1, Qt::AlignRight );
453 END_OF("SetListeHistory")
459 void ToolsGUI_RegWidget::InfoHistory()
462 BEGIN_OF("InfoHistory")
466 _serverhistory = _VarComponents->history();
467 for (CORBA::ULong i=0; i<_serverhistory->length(); i++)
469 const Registry::Infos & c_info=(*_serverhistory)[i];
470 ASSERT( c_info.name!=NULL);
472 a.setNum(int(c_info.pid));
473 char * t1 = (char * )duplicate(ctime(&c_info.tc_start));
474 t1 [strlen(t1) -1 ] = ' ';
475 char * t2 = (char * )duplicate(ctime(&c_info.tc_end));
476 t2 [strlen(t2) -1 ] = ' ';
477 QListViewItem * item = new QListViewItem(_history, QString(c_info.name),\
478 a, QString(c_info.pwname), QString(c_info.machine), \
479 QString(t1), QString(t2));
488 _interval->setDisabled( TRUE ) ;
489 _refresh->setDisabled( TRUE ) ;
491 MESSAGE("Sorry, No more Registry Server") ;
492 statusBar()->message( tr( "Sorry, No more Registry Server" ) ) ;
494 END_OF("InfoHistory")
500 void ToolsGUI_RegWidget::InfoReg()
506 _serverclients = _VarComponents->getall();
507 for (CORBA::ULong i=0; i<_serverclients->length(); i++)
509 const Registry::Infos & c_info=(*_serverclients)[i];
510 ASSERT( c_info.name!=NULL);
512 a.setNum(int(c_info.pid));
513 char * t1 = (char * )duplicate(ctime(&c_info.tc_start));
514 t1 [strlen(t1) -1 ] = ' ';
515 char * t2 = (char * )duplicate(ctime(&c_info.tc_hello));
516 t2 [strlen(t2) -1 ] = ' ';
517 QListViewItem * item = new QListViewItem(_clients, QString(c_info.name),\
518 a, QString(c_info.pwname), QString(c_info.machine), \
519 QString(t1), QString(t2));
528 _interval->setDisabled( TRUE ) ;
529 _refresh->setDisabled( TRUE ) ;
531 MESSAGE("Sorry, No more Registry Server") ;
532 statusBar()->message( tr( "Sorry, No more Registry Server" ) ) ;
538 Called when <Refresh> button is clicked
540 void ToolsGUI_RegWidget::slotListeSelect()
544 ASSERT(_tabWidget->currentPage() != NULL);
545 if (_tabWidget->currentPage () == _clients) InfoReg();
546 else if (_tabWidget->currentPage () == _history) InfoHistory();
550 MESSAGE("Sorry, No more Registry Server") ;
551 statusBar()->message( tr( "Sorry, No more Registry Server" ) ) ;
556 Called when <Interval> button is clicked (changing refresh interval)
558 void ToolsGUI_RegWidget::slotSelectRefresh()
560 BEGIN_OF("slotSelectRefresh");
561 myIntervalWindow = new ToolsGUI_IntervalWindow(this);
562 myIntervalWindow->installEventFilter( this );
563 myIntervalWindow->setValue(myRefreshInterval);
564 myIntervalWindow->show();
565 connect( myIntervalWindow->Cancel(), SIGNAL( clicked() ), myIntervalWindow, SLOT( close() ) );
566 connect( myIntervalWindow->Ok(), SIGNAL( clicked() ), this, SLOT( slotIntervalOk() ) );
567 END_OF("slotSelectRefresh");
570 void ToolsGUI_RegWidget::slotIntervalOk()
572 BEGIN_OF("slotIntervalOk");
573 myRefreshInterval = myIntervalWindow->getValue();
574 _counter->changeInterval( myRefreshInterval * 1000 );
575 SCRUTE(myRefreshInterval);
576 myIntervalWindow->close();
577 END_OF("slotIntervalOk");
580 Called when <Help> button is clicked
582 void ToolsGUI_RegWidget::slotHelp()
584 BEGIN_OF("slotHelp()");
586 if ( !myHelpWindow ) {
587 myHelpWindow = new ToolsGUI_HelpWindow( this );
588 myHelpWindow->installEventFilter( this );
590 myHelpWindow->show();
591 myHelpWindow->raise();
592 myHelpWindow->setActiveWindow();
594 END_OF("slotHelp()") ;
598 Called when user clicks on item in <Running> list
600 void ToolsGUI_RegWidget::slotClientChanged( QListViewItem* item )
602 BEGIN_OF("slotClientChanged()") ;
607 blockSignals( true ); // for sure that item will not be deleted when refreshing
609 int numeroItem = numitem(item->text(0), item->text(1), item->text(3), _serverclients);
611 SCRUTE(item->text(1)) ;
613 ASSERT(numeroItem>=0) ;
614 ASSERT((size_t)numeroItem<_serverclients->length()) ;
615 const Registry::Infos & c_info=(*_serverclients)[numeroItem];
616 ASSERT( c_info.name!=NULL);
618 if ( !myInfoWindow ) {
619 myInfoWindow = new ToolsGUI_InfoWindow( this );
620 myInfoWindow->installEventFilter( this );
622 QString a = tr( "More about" ) + QString( " " ) + QString( c_info.name );
623 myInfoWindow->setCaption(a);
624 myInfoWindow->setText( ToolsGUI_RegWidget::setlongText( c_info) );
625 myInfoWindow->show();
626 myInfoWindow->raise();
627 myInfoWindow->setActiveWindow();
629 blockSignals( false ); // enabling signals again
631 END_OF("slotClientChanged()") ;
636 Called when user clicks on item in <History> list
638 void ToolsGUI_RegWidget::slotHistoryChanged( QListViewItem* item )
641 BEGIN_OF("slotHistoryChanged()") ;
646 blockSignals( true ); // for sure that item will not be deleted when refreshing
648 int numeroItem = numitem(item->text(0), item->text(1), item->text(3), _serverhistory);
651 SCRUTE(item->text(1)) ;
652 ASSERT(numeroItem>=0) ;
653 ASSERT((size_t)numeroItem<_serverhistory->length()) ;
654 const Registry::Infos & c_info=(*_serverhistory)[numeroItem];
655 ASSERT( c_info.name!=NULL);
657 if ( !myInfoWindow ) {
658 myInfoWindow = new ToolsGUI_InfoWindow( this );
659 myInfoWindow->installEventFilter( this );
661 QString a = tr( "More about" ) + QString( " " ) + QString( c_info.name );
662 myInfoWindow->setCaption(a);
663 myInfoWindow->setText( ToolsGUI_RegWidget::setlongText( c_info ) );
664 myInfoWindow->show();
665 myInfoWindow->raise();
666 myInfoWindow->setActiveWindow();
668 blockSignals( false ); // enabling signals again
670 END_OF("slotHistoryChanged()") ;
677 ToolsGUI_InfoWindow::ToolsGUI_InfoWindow( QWidget* parent, const char* name )
678 : QMainWindow( parent, name, WType_TopLevel | WDestructiveClose )
680 BEGIN_OF("InfoWindow");
681 myTextView = new QTextView( this, "myTextView" );
682 setCentralWidget( myTextView );
683 setMinimumSize( 450, 250 );
684 END_OF("InfoWindow");
690 void ToolsGUI_InfoWindow::setText( const QString& text )
692 myTextView->setText( text );
698 void ToolsGUI_InfoWindow::keyPressEvent( QKeyEvent * e )
700 QMainWindow::keyPressEvent( e );
701 if ( e->key() == Key_Escape )
705 static const char* SEPARATOR = ":";
707 QString findFile( QString filename )
712 // Try ${HOME}/.salome/resources directory
713 cenv = getenv( "HOME" );
715 dir.sprintf( "%s", cenv );
716 if ( !dir.isEmpty() ) {
717 dir = addSlash(dir) ;
718 dir = dir + ".salome" ;
719 dir = addSlash(dir) ;
720 dir = dir + "resources" ;
721 dir = addSlash(dir) ;
722 QFileInfo fileInfo( dir + filename );
723 if ( fileInfo.isFile() && fileInfo.exists() )
724 return fileInfo.filePath();
727 // Try ${SALOME_SITE_DIR}/share/salome/resources directory
728 cenv = getenv( "SALOME_SITE_DIR" );
730 dir.sprintf( "%s", cenv );
731 if ( !dir.isEmpty() ) {
732 dir = addSlash(dir) ;
733 dir = dir + "share" ;
734 dir = addSlash(dir) ;
735 cenv = getenv("SALOME_SITE_NAME");
736 if (cenv) dir = dir + cenv;
737 else dir = dir + "salome" ;
738 dir = addSlash(dir) ;
739 dir = dir + "resources" ;
740 dir = addSlash(dir) ;
741 QFileInfo fileInfo( dir + filename );
742 if ( fileInfo.isFile() && fileInfo.exists() )
743 return fileInfo.filePath();
746 // Try ${KERNEL_ROOT_DIR}/share/salome/resources directory
747 cenv = getenv( "KERNEL_ROOT_DIR" );
749 dir.sprintf( "%s", cenv );
750 if ( !dir.isEmpty() ) {
751 dir = addSlash(dir) ;
752 dir = dir + "share" ;
753 dir = addSlash(dir) ;
754 dir = dir + "salome" ;
755 dir = addSlash(dir) ;
756 dir = dir + "resources" ;
757 dir = addSlash(dir) ;
758 QFileInfo fileInfo( dir + filename );
759 if ( fileInfo.isFile() && fileInfo.exists() )
760 return fileInfo.filePath();
764 //SRN Added support for SALOMEGUI
765 cenv = getenv( "SALOMEGUI_ROOT_DIR" );
767 dir.sprintf( "%s", cenv );
768 if ( !dir.isEmpty() ) {
769 dir = addSlash(dir) ;
770 dir = dir + "share" ;
771 dir = addSlash(dir) ;
772 dir = dir + "salome" ;
773 dir = addSlash(dir) ;
774 dir = dir + "resources" ;
775 dir = addSlash(dir) ;
776 QFileInfo fileInfo( dir + filename );
777 if ( fileInfo.isFile() && fileInfo.exists() )
778 return fileInfo.filePath();
782 // Try CSF_SaloameResources env.var directory ( or directory list )
783 cenv = getenv( "CSF_SalomeResources" );
785 dir.sprintf( "%s", cenv );
786 if ( !dir.isEmpty() ) {
787 QStringList dirList = QStringList::split( SEPARATOR, dir, false ); // skip empty entries
788 for ( int i = 0; i < dirList.count(); i++ ) {
789 QFileInfo fileInfo( addSlash( dirList[ i ] ) + filename );
790 if ( fileInfo.isFile() && fileInfo.exists() )
791 return fileInfo.filePath();
797 QString addSlash( const QString& path )
799 return Qtx::addSlash( path );
800 // if (!path.isNull()) {
802 // QChar slash ('\\');
804 // QChar slash ('/');
806 // if ( path.at(path.length()-1) != slash )
807 // return path + slash;