/*************************************************************************** * Copyright (C) 2006 by Michael Kaufmann * * michael@enlighter.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include #include #include #include #include "kovpnwidget.h" #include "openvpnmanager.h" using namespace std; kovpnWidget::kovpnWidget( QWidget* parent, const char* name, WFlags fl ) : kovpnWidgetBase( parent, name, fl ) {} kovpnWidget::~kovpnWidget() {} void kovpnWidget::reg( openVPNManager * manager ) { QTime time( 0, 0, 0 ); mStatusList[ manager->id() ] = new KListViewItem( StatusList, manager->id(), i18n( "Unavailable" ), QString::null, QString::null ); mManagerList[ manager->id() ] = manager; /* Only connect if this window is visible, otherwise don't, because if a showEvent occours it will connect everything, hide Event will disconnect everywhing */ // if ( this->isVisible() ) { QObject::connect( manager, SIGNAL ( signalOvpnStatusChanged( const QString &, openVPNManager::states ) ), this, SLOT( slotOvpnStatusChanged( const QString &, openVPNManager::states ) ) ); QObject::connect( manager, SIGNAL ( signalOvpnServerChanged( const QString &, const QString & ) ), this, SLOT( slotOvpnServerChanged( const QString &, const QString & ) ) ); QObject::connect( manager, SIGNAL ( signalOvpnIPChanged( const QString &, const QString & ) ), this, SLOT( slotOvpnIPChanged( const QString &, const QString & ) ) ); QObject::connect( manager, SIGNAL ( signalOvpnUptimeChanged( const QString &, const int & ) ), this, SLOT( slotOvpnUptimeChanged( const QString &, const int & ) ) ); if ( this->isVisible() ) { ( void ) manager->startUptimeCounter(); } /* This cannot be simply asked openVPNManager, it only sends this. this occours that rare, that it is ok */ QObject::connect( manager, SIGNAL ( signalOvpnReady( const QString & ) ), this, SLOT ( ovpnReady( const QString & ) ) ); QObject::connect( manager, SIGNAL ( signalOvpnNotReady( const QString & ) ), this, SLOT ( ovpnNotReady( const QString & ) ) ); QObject::connect( manager, SIGNAL ( signalOvpnPreReady( const QString & ) ), this, SLOT ( ovpnPreReady( const QString & ) ) ); } void kovpnWidget::dereg( openVPNManager * manager ) { QObject::disconnect( manager, 0, this, 0 ); StatusList->takeItem( mStatusList[ manager->id() ] ); mStatusList.remove( manager->id() ); mManagerList.remove( manager->id() ); } void kovpnWidget::slotOvpnServerChanged( const QString & id, const QString & server ) { debug( "slotOvpnServerChanged" ); } void kovpnWidget::slotOvpnStatusChanged( const QString & id, openVPNManager::states status ) { debug( "slotOvpnStatusChanged" ); switch ( status ) { case openVPNManager::DISCONNECTED: mStatusList[ id ] ->setText( 1, i18n( "Disconnected" ) ); break; case openVPNManager::CONNECTING: mStatusList[ id ] ->setText( 1, i18n( "Connecting" ) ); break; case openVPNManager::WAIT: mStatusList[ id ] ->setText( 1, i18n( "Waiting" ) ); break; case openVPNManager::AUTH: mStatusList[ id ] ->setText( 1, i18n( "Authentication" ) ); break; case openVPNManager::GET_CONFIG: mStatusList[ id ] ->setText( 1, i18n( "Get remote config" ) ); break; case openVPNManager::ASSIGN_IP: mStatusList[ id ] ->setText( 1, i18n( "Assign IP address" ) ); break; case openVPNManager::ADD_ROUTES: mStatusList[ id ] ->setText( 1, i18n( "Add routes" ) ); break; case openVPNManager::CONNECTED: mStatusList[ id ] ->setText( 1, i18n( "Connected" ) ); break; case openVPNManager::RECONNECTING: mStatusList[ id ] ->setText( 1, i18n( "Reconnecting" ) ); break; case openVPNManager::EXITING: default: mStatusList[ id ] ->setText( 1, i18n( "Unavailable" ) ); break; } } void kovpnWidget::slotOvpnIPChanged( const QString & id, const QString & ip ) { debug( "slotOvpnIPChanged" ); mStatusList[ id ] ->setText( 2, ip ); } void kovpnWidget::slotOvpnUptimeChanged( const QString & id, const int & uptime ) { debug( "slotOvpnUptimeChanged" ); QTime time( ( ( uptime / 3600 ) % 60 ), ( ( uptime / 60 ) % 60 ), ( uptime % 60 ) ); mStatusList[ id ] ->setText( 3, KGlobal::locale() ->formatTime( time, true, true ) ); } QSize kovpnWidget::optimalSize() { int currentHeight = this->height(); debug( "optimalSize", "1" ); int heightDiff = this->height() - StatusList->viewport() ->height(); debug( "optimalSize", "2" ); int lineHeight = 0; /* If there is no Child in the List this would otherwise lead to an crash */ if ( StatusList->childCount() > 0 ) { lineHeight = StatusList->firstChild() ->height(); } debug( "optimalSize", "3" ); int lineCount = mStatusList.count(); debug( "optimalSize", "4" ); int borderWidth = StatusList->lineWidth(); debug( "optimalSize", "5" ); debug( "optimalSize", QString( "currentHeight : %1" ).arg( currentHeight ) ); debug( "optimalSize", QString( "heightDiff : %1" ).arg( heightDiff ) ); debug( "optimalSize", QString( "lineHeight : %1" ).arg( lineHeight ) ); debug( "optimalSize", QString( "lineCount : %1" ).arg( lineCount ) ); debug( "optimalSize", QString( "borderWidth : %1" ).arg( borderWidth ) ); return QSize( this->width(), lineHeight * lineCount + heightDiff + borderWidth ); } void kovpnWidget::debug( const QString & method, const QString & message ) { #ifdef DEBUG QString myMethod ( method ); static unsigned int maxLen = 0; maxLen = QMAX( myMethod.length(), maxLen ); cout << "kovpnWidget::" << myMethod.leftJustify( maxLen ) << " => " << message << endl; #endif } void kovpnWidget::ovpnReady( const QString & id ) {} void kovpnWidget::ovpnNotReady( const QString & id ) {} void kovpnWidget::ovpnPreReady( const QString & id ) { /* TODO: Is this correctly called if the window is hidden and showed? */ mStatusList[ id ] ->setText( 1, i18n( "No control access" ) ); } void kovpnWidget::showEvent( QShowEvent * ) { debug( "showEvent" ); /* Optimize CPU usage if window is not shown */ for ( tManagerList::Iterator it = mManagerList.begin(); it != mManagerList.end(); ++it ) { // QObject::connect( it.data(), SIGNAL ( signalOvpnStatusChanged( const QString &, openVPNManager::states ) ), this, SLOT( slotOvpnStatusChanged( const QString &, openVPNManager::states ) ) ); // QObject::connect( it.data(), SIGNAL ( signalOvpnServerChanged( const QString &, const QString & ) ), this, SLOT( slotOvpnServerChanged( const QString &, const QString & ) ) ); // QObject::connect( it.data(), SIGNAL ( signalOvpnIPChanged( const QString &, const QString & ) ), this, SLOT( slotOvpnIPChanged( const QString &, const QString & ) ) ); // QObject::connect( it.data(), SIGNAL ( signalOvpnUptimeChanged( const QString &, const int & ) ), this, SLOT( slotOvpnUptimeChanged( const QString &, const int & ) ) ); /* Update it a first time, because all except uptime are updated rare, so that only using signals/slots would not work properly */ /* slotOvpnStatusChanged( it.data() ->id(), it.data() ->status().state ); slotOvpnServerChanged( it.data() ->id(), it.data() ->status().remoteName ); slotOvpnIPChanged( it.data() ->id(), it.data() ->status().localIP ); slotOvpnUptimeChanged( it.data() ->id(), it.data() ->status().uptime );*/ ( void ) it.data() ->startUptimeCounter(); } } void kovpnWidget::hideEvent( QHideEvent * ) { debug( "hideEvent" ); /* Optimize CPU usage if window is not shown */ for ( tManagerList::Iterator it = mManagerList.begin(); it != mManagerList.end(); ++it ) { // QObject::disconnect( it.data(), 0, this, 0 ); ( void ) it.data() ->stopUptimeCounter(); } } #include "kovpnwidget.moc"