/*************************************************************************** * 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 #include #include #include #include #include "kovpn.h" #include "kovpnwidget.h" #include "kovpnconfig.h" #include "traywindow.h" #include "prefopenvpn.h" #include "prefgeneral.h" #include "authmanager.h" using namespace std; kovpn::kovpn() : KMainWindow( 0, "kovpn" ) { /* Init all Pointers and other Variables */ mConfig = NULL; centralWidget = NULL; mSystemTray = NULL; mLogWidget = NULL; // infoPopup = NULL; mConfigDialog = NULL; mAuthManager = NULL; mIsShuttingDown = false; mStartMinimized = false; mShowMoreInformation = false; mQuietMode = false; /* Load configuration - this is the only module where this is done. All others get their config from here. */ debug( "kovpn", "Loading configuration" ); loadConfig(); debug( "kovpn", "Init icon loader" ); kovpnIcons = new KIconLoader( "kovpn" ); /* Init authManager and connect all neccessary signals and slots. */ debug( "kovpn", "Init authentification manager" ); mAuthManager = authManager::self(); QObject::connect( mAuthManager, SIGNAL ( authRequestGUI( bool &, bool & , const QString & , QString & , QString & , const QString & ) ), this, SLOT ( authRequestGUI( bool &, bool & , const QString & , QString & , QString & , const QString & ) ) ); QObject::connect( mAuthManager, SIGNAL ( passRequestGUI( bool &, bool & , const QString &, QString &, const QString & ) ), this, SLOT ( passRequestGUI( bool &, bool & , const QString & , QString &, const QString & ) ) ); /* Init Systray. It is needed by openVPNManager to register to the menus */ initSystemTray(); debug( "kovpn", "Init central Widget" ); centralWidget = new kovpnWidget( this ); setCentralWidget( centralWidget ); setIcon( kovpnIcons->loadIcon( "kovpn", KIcon::Small ) ); debug( "kovpn", "Init logging Widget" ); mLogWidget = new logWidget( kovpnConfig::self() ->maxHistoryLines() ); mLogWidget->setAdvanced( mShowMoreInformation ); /* Init OpenVPN Manager. Create an instance for each Openvpn-* entry in the config (mConnections). Connect all neccessary signals and slots. */ debug( "kovpn", "Init OpenVPN managers" ); /* TODO: multiple instances. DONE */ for ( QStringList::iterator it = mConnections.begin(); it != mConnections.end(); it++ ) { debug( "kovpn", QString( "Manager for %1" ).arg( kovpnConfig::self() ->id( *it ) ) ); mOvpnManagers[ *it ] = new openVPNManager( kovpnConfig::self() ->id( *it ), kovpnConfig::self() ->host( *it ), kovpnConfig::self() ->port( *it ), kovpnConfig::self() ->saveMIPassword( *it ), kovpnConfig::self() ->saveAccount( *it ), kovpnConfig::self() ->savePassphrase( *it ), kovpnConfig::self() ->reconnect( *it ), kovpnConfig::self() ->autoconnect( *it ), kovpnConfig::self() ->disconnectOnExit( *it ) ); QObject::connect( mOvpnManagers[ *it ], SIGNAL ( error( const QString & ) ), this, SLOT ( error( const QString & ) ) ); QObject::connect( mOvpnManagers[ *it ], SIGNAL ( message( bool &, const QString &, const QString & ) ), this, SLOT ( message( bool &, const QString &, const QString & ) ) ); // QObject::connect( mOvpnManagers[ *it ], SIGNAL ( signalOvpnStatusChanged( const QString &, openVPNManager::states ) ), this, SLOT( slotOvpnStatusChanged( const QString &, openVPNManager::states ) ) ); // QObject::connect( mOvpnManagers[ *it ], SIGNAL ( signalOvpnServerChanged( const QString &, const QString & ) ), this, SLOT( slotOvpnServerChanged( const QString &, const QString & ) ) ); // QObject::connect( mOvpnManagers[ *it ], SIGNAL ( signalOvpnIPChanged( const QString &, const QString & ) ), this, SLOT( slotOvpnIPChanged( const QString &, const QString & ) ) ); // QObject::connect( mOvpnManagers[ *it ], SIGNAL ( signalOvpnUptimeChanged( const QString &, const int & ) ), this, SLOT( slotOvpnUptimeChanged( const QString &, const int & ) ) ); /* Register the Manager to the System Tray (Menu: Connect, Disconnect) */ mSystemTray->reg( mOvpnManagers[ *it ] ); centralWidget->reg( mOvpnManagers[ *it ] ); mLogWidget->reg( mOvpnManagers[ *it ] ); this->reg( mOvpnManagers[ *it ] ); } debug( "kovpn", "Init Info Popup" ); // initInfoPopup(); } kovpn::~kovpn() { debug( "~kovpn" ); for ( QMap::Iterator it = mOvpnManagers.begin(); it != mOvpnManagers.end(); ++it ) { debug( "~kovpn", QString( "Manager for %1 is going to be deleted" ).arg( it.data() ->name() ) ); QObject::disconnect( it.data() ); mSystemTray->dereg( it.data() ); centralWidget->dereg( it.data() ); mLogWidget->dereg( it.data() ); this->dereg( it.data() ); delete it.data(); // it.data() ->disconnect(); mOvpnManagers.remove( it ); } delete kovpnIcons; QObject::disconnect( mAuthManager ); delete mAuthManager; delete centralWidget; delete mLogWidget; delete mSystemTray; } bool kovpn::getAuthData( QString &username, QString &password, QString ressource ) { KIO::PasswordDialog KovpnAuthDialog( i18n( "You need to supply a username and a password." ), username, password ); KovpnAuthDialog.addCommentLine( i18n( "Network" ), ressource ); // bool keep = false; // KovpnAuthDialog.getNameAndPassword(username, password, &keep); if ( KovpnAuthDialog.exec() ) { username = KovpnAuthDialog.username(); password = KovpnAuthDialog.password(); return true; } return false; } /** * */ void kovpn::initSystemTray() { /* Create system tray window */ mSystemTray = TrayWindow::systemTray( this, "KopvnSystemTray" ); mSystemTray->setAdvanced( mShowMoreInformation ); mSystemTray->show(); } /** * */ // void kovpn::initInfoPopup() // { // infoPopup = new KPassivePopup(); // } void kovpn::slotConnect() { QString password; QString username; if ( getAuthData( username, password, "itiVPN" ) ) { emit signalConnect( username, password ); } } void kovpn::slotDisconnect() { emit signalDisconnect(); } void kovpn::slotSettingsChanged() { debug( "slotSettingsChanged", "Init OpenVPN Managers" ); loadConfig(); mSystemTray->setAdvanced( mShowMoreInformation ); /* Delete deconfigured Managers. */ debug( "slotSettingsChanged", "deleting deconfigured managers" ); QValueList managerIDs = mOvpnManagers.keys(); for ( QValueList::Iterator it = managerIDs.begin(); it != managerIDs.end(); ++it ) { debug( "slotSettingsChanged", QString( "Manager for %1 is going to be deleted" ).arg( *it ) ); if ( ! mConnections.contains( *it ) ) { mSystemTray->dereg( mOvpnManagers[ *it ] ); centralWidget->dereg( mOvpnManagers[ *it ] ); mLogWidget->dereg( mOvpnManagers[ *it ] ); this->dereg( mOvpnManagers[ *it ] ); QObject::disconnect( mOvpnManagers[ *it ], 0, 0, 0 ); delete mOvpnManagers[ *it ]; mOvpnManagers.remove( *it ); } } /* Add new Managers or reconfigure the existing ones. */ debug( "slotSettingsChanged", "add new managers or reconfigure the existing ones" ); for ( QStringList::iterator it = mConnections.begin(); it != mConnections.end(); it++ ) { debug( "slotSettingsChanged", QString( "Manager for %1" ).arg( kovpnConfig::self() ->id( *it ) ) ); if ( mOvpnManagers[ *it ] == NULL ) { mOvpnManagers[ *it ] = new openVPNManager( kovpnConfig::self() ->id( *it ), kovpnConfig::self() ->host( *it ), kovpnConfig::self() ->port( *it ), kovpnConfig::self() ->saveMIPassword( *it ), kovpnConfig::self() ->saveAccount( *it ), kovpnConfig::self() ->savePassphrase( *it ), kovpnConfig::self() ->reconnect( *it ), kovpnConfig::self() ->autoconnect( *it ), kovpnConfig::self() ->disconnectOnExit( *it ) ); /* Register the Manager to the System Tray (Menu: Connect, Disconnect) */ mSystemTray->reg( mOvpnManagers[ *it ] ); centralWidget->reg( mOvpnManagers[ *it ] ); mLogWidget->reg( mOvpnManagers[ *it ] ); this->reg( mOvpnManagers[ *it ] ); } else { mSystemTray->dereg( mOvpnManagers[ *it ] ); mSystemTray->reg( mOvpnManagers[ *it ] ); mOvpnManagers[ *it ] ->setHost( kovpnConfig::self() ->host( *it ) ); mOvpnManagers[ *it ] ->setPort( kovpnConfig::self() ->port( *it ) ); mOvpnManagers[ *it ] ->setSaveMIPassword( kovpnConfig::self() ->saveMIPassword( *it ) ); mOvpnManagers[ *it ] ->setSaveAccount( kovpnConfig::self() ->saveAccount( *it ) ); mOvpnManagers[ *it ] ->setSavePassphrase( kovpnConfig::self() ->savePassphrase( *it ) ); mOvpnManagers[ *it ] ->setReconnect( kovpnConfig::self() ->reconnect( *it ) ); mOvpnManagers[ *it ] ->setAutoconnect( kovpnConfig::self() ->autoconnect( *it ) ); } } } void kovpn::slotPreferences() { // delete mConfigDialog; if ( !mConfigDialog ) { debug( "slotPreferences", "Init config dialog" ); mConfigDialog = new kovpnConfigDialog( this, "Preferences", mConfig, KDialogBase::IconList ); QObject::connect( mConfigDialog, SIGNAL ( settingsChanged() ), this, SLOT( slotSettingsChanged() ) ); } mConfigDialog->show(); } void kovpn::closeEvent( QCloseEvent * ev ) { if ( mIsShuttingDown ) { KMainWindow::closeEvent( ev ); } else { this->hide(); ev->ignore(); } } void kovpn::slotQuitKovpn( ) { mIsShuttingDown = true; } void kovpn::authRequestGUI( bool & cancel, bool & failure, const QString & ressource, QString & password, QString & username, const QString & message ) { debug( "authRequestGUI" ); if ( KIO::PasswordDialog::getNameAndPassword ( username, password, NULL, message, false, i18n( "Authentication request" ), ressource, i18n( "Network" ) ) ) { failure = false; } else { debug( "authRequestGUI", "Failure" ); failure = true; username = QString::null; password = QString::null; } } void kovpn::passRequestGUI( bool & cancel, bool & failure, const QString & ressource, QString & passphrase, const QString & message ) { debug( "passRequestGUI" ); QCString tempPassphrase; KPasswordDialog * myPassDlg = new KPasswordDialog( KPasswordDialog::Password, false, 0 ); myPassDlg->setPrompt( message ); myPassDlg->addLine( i18n( "Network" ), ressource ); myPassDlg->disableCoreDumps(); if ( myPassDlg->exec() ) { passphrase = myPassDlg->password(); failure = false; } else { debug( "passRequestGUI", "Failure" ); failure = true; passphrase = QString::null; } myPassDlg->clearPassword(); delete myPassDlg; } void kovpn::error( const QString & message ) { KMessageBox::error ( this, message ); } void kovpn::loadConfig( ) { debug( "loadConfig" ); mConfig = kovpnConfig::self(); mConfig->readConfig(); mConnections = mConfig->config() ->groupList().grep( "Openvpn-" ).gres( "Openvpn-", "" ); mStartMinimized = mConfig->startMinimized(); mShowMoreInformation = mConfig->showMoreInformation(); mQuietMode = mConfig->quietMode(); for ( QStringList::iterator it = mConnections.begin(); it != mConnections.end(); it++ ) { debug( "loadConfig", QString( "Manager for %1" ).arg( *it ) ); } debug( "loadConfig", QString( "mShowMoreInformation : %1" ).arg( mShowMoreInformation ) ); debug( "loadConfig", QString( "mStartMinimized : %1" ).arg( mStartMinimized ) ); } void kovpn::slotShowLogWidget( ) { mLogWidget->show(); } void kovpn::show( ) { KMainWindow::show(); } void kovpn::reg( openVPNManager * manager ) { QObject::connect( manager, SIGNAL ( error( const QString & ) ), this, SLOT ( error( const QString & ) ) ); QObject::connect( manager, SIGNAL ( message( bool &, const QString &, const QString & ) ), this, SLOT ( message( bool &, const QString &, const QString & ) ) ); this->resize( centralWidget->optimalSize() ); kovpnInterface::self() ->reg( manager ); } void kovpn::dereg( openVPNManager * manager ) { kovpnInterface::self() ->dereg( manager ); this->resize( centralWidget->optimalSize() ); QObject::disconnect( manager, 0, this, 0 ); } void kovpn::debug( const QString & method, const QString & message ) { #ifdef DEBUG QString myMethod ( method ); static unsigned int maxLen = 0; maxLen = QMAX( myMethod.length(), maxLen ); cout << "kovpn::" << myMethod.leftJustify( maxLen ) << " => " << message << endl; #endif } void kovpn::message( bool & yesno, const QString & caption, const QString & text ) { debug( "message", "needok" ); int answer = KMessageBox::questionYesNo ( this, text, caption ); if ( answer == KMessageBox::Yes ) { yesno = true; } else { yesno = false; } } #include "kovpn.moc"