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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 ) );
361 a.append( QString( "<br>" ) + tr( "begins" ) + QString( " " ) );
362 aTime = time_t(c_info.tc_start);
363 char * t1 = (char * )duplicate(ctime(&aTime));
364 t1 [strlen(t1) -1 ] = ' ';
365 a.append( BOLD( t1 ) );
369 if (c_info.tc_hello != 0 )
371 aTime = time_t(c_info.tc_hello);
372 char * t2 = (char * )duplicate(ctime(&aTime));
373 t2 [strlen(t2) -1 ] = ' ';
374 a.append( tr( "last signal" ) + QString(" : ") );
375 a.append( BOLD( t2 ) );
379 if ((c_info.tc_end - c_info.difftime) != 0)
381 aTime = time_t(c_info.tc_end);
382 char * t3 = (char * )duplicate(ctime(&aTime));
383 t3 [strlen(t3) -1 ] = ' ';
384 a.append( tr( "ends" ) + QString( " " ) );
385 a.append( BOLD( t3 ) );
391 a.append( tr( "still running" ) + QString( "<br>" ) );
394 SCRUTE(c_info.difftime);
395 if (c_info.difftime!= 0)
397 a.append( QString( "(" ) + tr( "Time on" ) + QString( " " ) );
398 a.append( BOLD( c_info.machine ) );
399 a.append( QString( " " ) + tr( "differs from server's time. The difference is" ) + QString( " " ));
400 a.append( BOLD( QString::number( int( c_info.difftime ) ) ) );
401 a.append( QString( " " ) + tr( "seconds" ) + QString( ")<br>" ) );
403 END_OF("setlongText");
411 void ToolsGUI_RegWidget::closeEvent( QCloseEvent *e)
413 BEGIN_OF("closeEvent");
415 myInfoWindow->close();
417 myHelpWindow->close();
418 if (myIntervalWindow)
419 myIntervalWindow->close();
421 END_OF("closeEvent");
427 void ToolsGUI_RegWidget::SetListe()
429 BEGIN_OF("SetListe");
430 _clients->installEventFilter( this );
431 _clients->setAllColumnsShowFocus( true );
432 _clients->addColumn( tr( "Component" ), -1);
433 _clients->addColumn( tr( "PID" ), -1 );
434 _clients->addColumn( tr( "User Name" ), -1 );
435 _clients->addColumn( tr( "Machine" ), -1 );
436 _clients->addColumn( tr( "begins" ), -1 );
437 _clients->addColumn( tr( "hello" ) , -1 );
438 _clients->setColumnAlignment( 1, Qt::AlignRight );
445 void ToolsGUI_RegWidget::SetListeHistory()
447 BEGIN_OF("SetListeHistory")
448 _history->installEventFilter( this );
449 _history->setAllColumnsShowFocus( true );
450 _history->addColumn( tr( "Component" ), -1);
451 _history->addColumn( tr( "PID" ), -1 );
452 _history->addColumn( tr( "User Name" ), -1 );
453 _history->addColumn( tr( "Machine" ), -1 );
454 _history->addColumn( tr( "begins" ), -1 );
455 _history->addColumn( tr( "ends" ), -1 );
456 _history->setColumnAlignment( 1, Qt::AlignRight );
457 END_OF("SetListeHistory")
463 void ToolsGUI_RegWidget::InfoHistory()
466 BEGIN_OF("InfoHistory")
471 _serverhistory = _VarComponents->history();
472 for (CORBA::ULong i=0; i<_serverhistory->length(); i++)
474 const Registry::Infos & c_info=(*_serverhistory)[i];
475 ASSERT( c_info.name!=NULL);
477 a.setNum(int(c_info.pid));
478 aTime = time_t(c_info.tc_start);
479 char * t1 = (char * )duplicate(ctime(&aTime));
480 t1 [strlen(t1) -1 ] = ' ';
481 aTime = time_t(c_info.tc_end);
482 char * t2 = (char * )duplicate(ctime(&aTime));
483 t2 [strlen(t2) -1 ] = ' ';
484 QListViewItem * item = new QListViewItem(_history, QString(c_info.name),\
485 a, QString(c_info.pwname), QString(c_info.machine), \
486 QString(t1), QString(t2));
495 _interval->setDisabled( TRUE ) ;
496 _refresh->setDisabled( TRUE ) ;
498 MESSAGE("Sorry, No more Registry Server") ;
499 statusBar()->message( tr( "Sorry, No more Registry Server" ) ) ;
501 END_OF("InfoHistory")
507 void ToolsGUI_RegWidget::InfoReg()
514 _serverclients = _VarComponents->getall();
515 for (CORBA::ULong i=0; i<_serverclients->length(); i++)
517 const Registry::Infos & c_info=(*_serverclients)[i];
518 ASSERT( c_info.name!=NULL);
520 a.setNum(int(c_info.pid));
521 aTime = time_t(c_info.tc_start);
522 char * t1 = (char * )duplicate(ctime(&aTime));
523 t1 [strlen(t1) -1 ] = ' ';
524 aTime = time_t(c_info.tc_hello);
525 char * t2 = (char * )duplicate(ctime(&aTime));
526 t2 [strlen(t2) -1 ] = ' ';
527 QListViewItem * item = new QListViewItem(_clients, QString(c_info.name),\
528 a, QString(c_info.pwname), QString(c_info.machine), \
529 QString(t1), QString(t2));
538 _interval->setDisabled( TRUE ) ;
539 _refresh->setDisabled( TRUE ) ;
541 MESSAGE("Sorry, No more Registry Server") ;
542 statusBar()->message( tr( "Sorry, No more Registry Server" ) ) ;
548 Called when <Refresh> button is clicked
550 void ToolsGUI_RegWidget::slotListeSelect()
554 ASSERT(_tabWidget->currentPage() != NULL);
555 if (_tabWidget->currentPage () == _clients) InfoReg();
556 else if (_tabWidget->currentPage () == _history) InfoHistory();
560 MESSAGE("Sorry, No more Registry Server") ;
561 statusBar()->message( tr( "Sorry, No more Registry Server" ) ) ;
566 Called when <Interval> button is clicked (changing refresh interval)
568 void ToolsGUI_RegWidget::slotSelectRefresh()
570 BEGIN_OF("slotSelectRefresh");
571 myIntervalWindow = new ToolsGUI_IntervalWindow(this);
572 myIntervalWindow->installEventFilter( this );
573 myIntervalWindow->setValue(myRefreshInterval);
574 myIntervalWindow->show();
575 connect( myIntervalWindow->Cancel(), SIGNAL( clicked() ), myIntervalWindow, SLOT( close() ) );
576 connect( myIntervalWindow->Ok(), SIGNAL( clicked() ), this, SLOT( slotIntervalOk() ) );
577 END_OF("slotSelectRefresh");
581 SLOT: called when IntervalWindow's OK button is clicked
583 void ToolsGUI_RegWidget::slotIntervalOk()
585 BEGIN_OF("slotIntervalOk");
586 myRefreshInterval = myIntervalWindow->getValue();
587 _counter->changeInterval( myRefreshInterval * 1000 );
588 SCRUTE(myRefreshInterval);
589 myIntervalWindow->close();
590 END_OF("slotIntervalOk");
593 Called when <Help> button is clicked
595 void ToolsGUI_RegWidget::slotHelp()
597 BEGIN_OF("slotHelp()");
599 if ( !myHelpWindow ) {
600 myHelpWindow = new ToolsGUI_HelpWindow( this );
601 myHelpWindow->installEventFilter( this );
603 myHelpWindow->show();
604 myHelpWindow->raise();
605 myHelpWindow->setActiveWindow();
607 END_OF("slotHelp()") ;
611 Called when user clicks on item in <Running> list
613 void ToolsGUI_RegWidget::slotClientChanged( QListViewItem* item )
615 BEGIN_OF("slotClientChanged()") ;
620 blockSignals( true ); // for sure that item will not be deleted when refreshing
622 int numeroItem = numitem(item->text(0), item->text(1), item->text(3), _serverclients);
624 SCRUTE(item->text(1)) ;
626 ASSERT(numeroItem>=0) ;
627 ASSERT((size_t)numeroItem<_serverclients->length()) ;
628 const Registry::Infos & c_info=(*_serverclients)[numeroItem];
629 ASSERT( c_info.name!=NULL);
631 if ( !myInfoWindow ) {
632 myInfoWindow = new ToolsGUI_InfoWindow( this );
633 myInfoWindow->installEventFilter( this );
635 QString a = tr( "More about" ) + QString( " " ) + QString( c_info.name );
636 myInfoWindow->setCaption(a);
637 myInfoWindow->setText( ToolsGUI_RegWidget::setlongText( c_info) );
638 myInfoWindow->show();
639 myInfoWindow->raise();
640 myInfoWindow->setActiveWindow();
642 blockSignals( false ); // enabling signals again
644 END_OF("slotClientChanged()") ;
649 Called when user clicks on item in <History> list
651 void ToolsGUI_RegWidget::slotHistoryChanged( QListViewItem* item )
654 BEGIN_OF("slotHistoryChanged()") ;
659 blockSignals( true ); // for sure that item will not be deleted when refreshing
661 int numeroItem = numitem(item->text(0), item->text(1), item->text(3), _serverhistory);
664 SCRUTE(item->text(1)) ;
665 ASSERT(numeroItem>=0) ;
666 ASSERT((size_t)numeroItem<_serverhistory->length()) ;
667 const Registry::Infos & c_info=(*_serverhistory)[numeroItem];
668 ASSERT( c_info.name!=NULL);
670 if ( !myInfoWindow ) {
671 myInfoWindow = new ToolsGUI_InfoWindow( this );
672 myInfoWindow->installEventFilter( this );
674 QString a = tr( "More about" ) + QString( " " ) + QString( c_info.name );
675 myInfoWindow->setCaption(a);
676 myInfoWindow->setText( ToolsGUI_RegWidget::setlongText( c_info ) );
677 myInfoWindow->show();
678 myInfoWindow->raise();
679 myInfoWindow->setActiveWindow();
681 blockSignals( false ); // enabling signals again
683 END_OF("slotHistoryChanged()") ;
690 ToolsGUI_InfoWindow::ToolsGUI_InfoWindow( QWidget* parent, const char* name )
691 : QMainWindow( parent, name, WType_TopLevel | WDestructiveClose )
693 BEGIN_OF("InfoWindow");
694 myTextView = new QTextView( this, "myTextView" );
695 setCentralWidget( myTextView );
696 setMinimumSize( 450, 250 );
697 END_OF("InfoWindow");
703 void ToolsGUI_InfoWindow::setText( const QString& text )
705 myTextView->setText( text );
711 void ToolsGUI_InfoWindow::keyPressEvent( QKeyEvent * e )
713 QMainWindow::keyPressEvent( e );
714 if ( e->key() == Key_Escape )
718 static const char* SEPARATOR = ":";
720 QString findFile( QString filename )
725 // Try ${HOME}/.salome/resources directory
726 cenv = getenv( "HOME" );
728 dir.sprintf( "%s", cenv );
729 if ( !dir.isEmpty() ) {
730 dir = addSlash(dir) ;
731 dir = dir + ".salome" ;
732 dir = addSlash(dir) ;
733 dir = dir + "resources" ;
734 dir = addSlash(dir) ;
735 QFileInfo fileInfo( dir + filename );
736 if ( fileInfo.isFile() && fileInfo.exists() )
737 return fileInfo.filePath();
740 // Try ${SALOME_SITE_DIR}/share/salome/resources directory
741 cenv = getenv( "SALOME_SITE_DIR" );
743 dir.sprintf( "%s", cenv );
744 if ( !dir.isEmpty() ) {
745 dir = addSlash(dir) ;
746 dir = dir + "share" ;
747 dir = addSlash(dir) ;
748 cenv = getenv("SALOME_SITE_NAME");
749 if (cenv) dir = dir + cenv;
750 else 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();
759 // Try ${KERNEL_ROOT_DIR}/share/salome/resources directory
760 cenv = getenv( "KERNEL_ROOT_DIR" );
762 dir.sprintf( "%s", cenv );
763 if ( !dir.isEmpty() ) {
764 dir = addSlash(dir) ;
765 dir = dir + "share" ;
766 dir = addSlash(dir) ;
767 dir = dir + "salome" ;
768 dir = addSlash(dir) ;
769 dir = dir + "resources" ;
770 dir = addSlash(dir) ;
771 QFileInfo fileInfo( dir + filename );
772 if ( fileInfo.isFile() && fileInfo.exists() )
773 return fileInfo.filePath();
777 //SRN Added support for SALOMEGUI
778 cenv = getenv( "SALOMEGUI_ROOT_DIR" );
780 dir.sprintf( "%s", cenv );
781 if ( !dir.isEmpty() ) {
782 dir = addSlash(dir) ;
783 dir = dir + "share" ;
784 dir = addSlash(dir) ;
785 dir = dir + "salome" ;
786 dir = addSlash(dir) ;
787 dir = dir + "resources" ;
788 dir = addSlash(dir) ;
789 QFileInfo fileInfo( dir + filename );
790 if ( fileInfo.isFile() && fileInfo.exists() )
791 return fileInfo.filePath();
795 // Try CSF_SaloameResources env.var directory ( or directory list )
796 cenv = getenv( "CSF_SalomeResources" );
798 dir.sprintf( "%s", cenv );
799 if ( !dir.isEmpty() ) {
800 QStringList dirList = QStringList::split( SEPARATOR, dir, false ); // skip empty entries
801 for ( int i = 0; i < dirList.count(); i++ ) {
802 QFileInfo fileInfo( addSlash( dirList[ i ] ) + filename );
803 if ( fileInfo.isFile() && fileInfo.exists() )
804 return fileInfo.filePath();
810 QString addSlash( const QString& path )
812 return Qtx::addSlash( path );
813 // if (!path.isNull()) {
815 // QChar slash ('\\');
817 // QChar slash ('/');
819 // if ( path.at(path.length()-1) != slash )
820 // return path + slash;