/*************************************************************************** * 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 "prefopenvpn.h" #include "openvpnmanager.h" #include "kovpnconfig.h" #include #include #include #include #include #include #include #include #include using namespace std; prefOpenVPN::prefOpenVPN() : prefOpenVPNLayout() { modifyConnection->setEnabled( false ); deleteConnection->setEnabled( false ); QObject::connect( addConnection, SIGNAL ( clicked() ), this, SLOT ( add() ) ); QObject::connect( modifyConnection, SIGNAL ( clicked() ), this, SLOT ( modify() ) ); QObject::connect( deleteConnection, SIGNAL ( clicked() ), this, SLOT ( remove() ) ); QObject::connect( connectionListView, SIGNAL ( clicked( QListViewItem * ) ), this, SLOT ( clicked( QListViewItem * ) ) ); mConfig = kovpnConfig::self(); mChanged = false; readConfig(); } prefOpenVPN::~prefOpenVPN() { debug( "~prefOpenVPN" ); } /* This is used to add and modify a connection since the differences are really small */ void prefOpenVPN::add( bool modify ) { debug( "add", QString( "modify = %1" ).arg( modify ) ); ovpnRessource = new prefOvpnRessource(); bool error = true; if ( modify ) { QString item = connectionListView->selectedItems().first() ->text( 0 ); tConnection * con = mConnectionMap[ QString::fromLatin1( item ) ]; ovpnRessource->kcfg_name->setReadOnly( true ); ovpnRessource->kcfg_name->setText( con->mId ); ovpnRessource->kcfg_host->setText( con->mHost ); ovpnRessource->kcfg_port->setValue( con->mPort.toInt() ); ovpnRessource->kcfg_saveManagementInterfacePassword->setChecked( con->mSaveMIPassword ); ovpnRessource->kcfg_save_account_data->setChecked( con->mSaveAccount ); ovpnRessource->kcfg_save_passphrase->setChecked( con->mSavePassphrase ); ovpnRessource->kcfg_reconnect->setChecked( con->mReconnect ); ovpnRessource->kcfg_autoconnect->setChecked( con->mAutoconnect ); ovpnRessource->kcfg_disconnect_on_exit->setChecked( con->mDisconnectOnExit ); } /* TODO WARNING: Could lead to a double showed ovpnRessource Dialog if compiler doesn't stop if error == false */ while ( error == true && ovpnRessource->exec() ) { /* Check if all options are reasonable */ QString errorMessage = QString::null; error = false; /* Look if the name is unique. */ if ( ! modify && connectionListView->findItem( ovpnRessource->kcfg_name->text(), 0 ) ) { error = true; errorMessage += i18n( "The name has to be unique, but %1 isn't." ).arg( ovpnRessource->kcfg_name->text() ); } /* Look if name isn't empty. TODO: Improve, check reasonability, syntax. */ if ( ovpnRessource->kcfg_name->text().length() == 0 ) { if ( error ) { errorMessage += "
";} error = true; errorMessage += i18n( "You must set a name" ); } /* Look if hostname isn't empty. TODO: Improve, check reasonability, syntax. */ if ( ovpnRessource->kcfg_host->text().length() == 0 ) { if ( error ) { errorMessage += "
";} error = true; errorMessage += i18n( "You must set a hostname" ); } /* Look if port isn't empty. TODO: Improve, check reasonability, syntax. */ if ( ovpnRessource->kcfg_port->text() == "0" ) { if ( error ) { errorMessage += "
";} error = true; errorMessage += i18n( "You must set a port" ); } if ( error ) { KMessageBox::error( this, errorMessage, i18n( "Error" ) ); } else { mChanged = true; tConnection *con = new tConnection; con->mId = ovpnRessource->kcfg_name->text(); con->mHost = ovpnRessource->kcfg_host->text(); con->mPort = ovpnRessource->kcfg_port->text(); con->mSaveAccount = ovpnRessource->kcfg_save_account_data->isOn(); con->mSaveMIPassword = ovpnRessource->kcfg_saveManagementInterfacePassword->isOn(); con->mSavePassphrase = ovpnRessource->kcfg_save_passphrase->isOn(); con->mReconnect = ovpnRessource->kcfg_reconnect->isOn(); con->mAutoconnect = ovpnRessource->kcfg_autoconnect->isOn(); con->mDisconnectOnExit = ovpnRessource->kcfg_disconnect_on_exit->isOn(); mAddConnection[ QString::fromLatin1( con->mId ) ] = con; /* only for updating the configuration */ mRemoveConnection.remove( QString::fromLatin1( con->mId ) ); mConnectionMap[ QString::fromLatin1( con->mId ) ] = con; /* All connections - for displaing, not more */ } } modifyConnection->setEnabled( false ); deleteConnection->setEnabled( false ); delete ovpnRessource; ovpnRessource = NULL; updateWidgets(); } void prefOpenVPN::modify( ) { debug( "modify" ); add( true ); } void prefOpenVPN::remove( ) { debug( "remove" ); QPtrList selectedItems = connectionListView->selectedItems(); for ( QListViewItem * item = selectedItems.first(); item != NULL; item = selectedItems.next() ) { debug( "remove", QString( "Connection %1" ).arg( item->text( 0 ) ) ); mRemoveConnection[ item->text( 0 ) ] = mConnectionMap[ item->text( 0 ) ]; mAddConnection.remove( item->text( 0 ) ); mConnectionMap.remove( item->text( 0 ) ); delete item; mChanged = true; } modifyConnection->setEnabled( false ); deleteConnection->setEnabled( false ); updateWidgets(); } void prefOpenVPN::updateWidgets( ) { debug( "updateWidgets" ); connectionListView->clear(); for ( tConnectionMap::iterator it = mConnectionMap.begin(); it != mConnectionMap.end(); it++ ) { ( void ) new KListViewItem( connectionListView, it.data() ->mId, it.data() ->mHost, it.data() ->mPort ); } emit widgetChanged(); } void prefOpenVPN::updateWidgetsDefault( ) { debug( "updateWidgetsDefault" ); } void prefOpenVPN::updateSettings( ) { debug( "updateSettings" ); /* Add the new connections, modify the old ones. Makes no difference here. */ for ( tConnectionMap::iterator it = mAddConnection.begin(); it != mAddConnection.end(); it++ ) { tConnection *con = new tConnection; tConnection *tmp = *it; mConfig->setCurrentGroup( QString::fromLatin1( "Openvpn-" + tmp->mId ) ); mConfig->addItemString( QString::fromLatin1( "id" ), con->mId, QString::fromLatin1( "null" ) ); mConfig->addItemString( QString::fromLatin1( "host" ), con->mHost, QString::fromLatin1( "localhost" ) ); mConfig->addItemString( QString::fromLatin1( "port" ), con->mPort, QString::fromLatin1( "11194" ) ); mConfig->addItemBool( QString::fromLatin1( "saveManagementInterfacePassword" ), con->mSaveMIPassword, false ); mConfig->addItemBool( QString::fromLatin1( "saveAccount" ), con->mSaveAccount, false ); mConfig->addItemBool( QString::fromLatin1( "savePassphrase" ), con->mSavePassphrase, false ); mConfig->addItemBool( QString::fromLatin1( "reconnect" ), con->mReconnect, false ); mConfig->addItemBool( QString::fromLatin1( "autoconnect" ), con->mAutoconnect, false ); mConfig->addItemBool( QString::fromLatin1( "disconnectOnExit" ), con->mDisconnectOnExit, false ); /* Because addItem* sets all values to the defaults, I have to set them to the right values now. */ con->mId = tmp->mId; con->mHost = tmp->mHost; con->mPort = tmp->mPort; con->mSaveMIPassword = tmp->mSaveMIPassword; con->mSaveAccount = tmp->mSaveAccount; con->mSavePassphrase = tmp->mSavePassphrase; con->mReconnect = tmp->mReconnect; con->mAutoconnect = tmp->mAutoconnect; con->mDisconnectOnExit = tmp->mDisconnectOnExit; } /* Remove connections */ for ( tConnectionMap::iterator it = mRemoveConnection.begin(); it != mRemoveConnection.end(); it++ ) { tConnection *con = *it; mConfig->config() ->deleteGroup( "Openvpn-" + con->mId ); } mChanged = false; // mConfig->setConnections( mConnections ); mConfig->writeConfig(); } bool prefOpenVPN::hasChanged( ) { debug( "hasChanged" ); return mChanged; } bool prefOpenVPN::isDefault( ) { debug( "isDefault" ); return ! mChanged; } void prefOpenVPN::readConfig( ) { debug( "readConfig" ); mChanged = false; mConnections = mConfig->config() ->groupList().grep( "Openvpn-" ).gres( "Openvpn-", "" ); for ( QStringList::iterator it = mConnections.begin(); it != mConnections.end(); it++ ) { debug( "readConfig", QString( "Connection %1" ).arg( *it ) ); tConnection *con = new tConnection; mConfig->setCurrentGroup( QString::fromLatin1( "Openvpn-" + *it ) ); mConfig->addItemString( QString::fromLatin1( "id" ), con->mId, QString::fromLatin1( "null" ) ); mConfig->addItemString( QString::fromLatin1( "host" ), con->mHost, QString::fromLatin1( "localhost" ) ); mConfig->addItemString( QString::fromLatin1( "port" ), con->mPort, QString::fromLatin1( "11194" ) ); mConfig->addItemBool( QString::fromLatin1( "saveManagementInterfacePassword" ), con->mSaveMIPassword, false ); mConfig->addItemBool( QString::fromLatin1( "saveAccount" ), con->mSaveAccount, false ); mConfig->addItemBool( QString::fromLatin1( "savePassphrase" ), con->mSavePassphrase, false ); mConfig->addItemBool( QString::fromLatin1( "reconnect" ), con->mReconnect, false ); mConfig->addItemBool( QString::fromLatin1( "autoconnect" ), con->mAutoconnect, false ); mConfig->addItemBool( QString::fromLatin1( "disconnectOnExit" ), con->mDisconnectOnExit, false ); mConnectionMap[ *it ] = con; } mConfig->readConfig(); /* Read the new groups and group entries */ /* Add the Connections from the config file to the ListView */ connectionListView->clear(); for ( tConnectionMap::iterator it = mConnectionMap.begin(); it != mConnectionMap.end(); it++ ) { ( void ) new KListViewItem( connectionListView, it.data() ->mId, it.data() ->mHost, it.data() ->mPort ); } } void prefOpenVPN::writeConfig( ) { debug( "writeConfig" ); mChanged = false; mConfig->writeConfig(); } void prefOpenVPN::clicked( QListViewItem * item ) { debug( "clicked" ); if ( item != NULL ) { modifyConnection->setEnabled( true ); deleteConnection->setEnabled( true ); } else { modifyConnection->setEnabled( false ); deleteConnection->setEnabled( false ); } } // void prefOpenVPN::show( ) // { // mConfig->readConfig(); // prefOpenVPNLayout::show(); // } void prefOpenVPN::debug( const QString & method, const QString & message ) { #ifdef DEBUG QString myMethod ( method ); static unsigned int maxLen = 0; maxLen = QMAX( myMethod.length(), maxLen ); cout << "prefOpenVPN::" << myMethod.leftJustify( maxLen ) << " => " << message << endl; #endif } #include "prefopenvpn.moc"