You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kopete-otr/src/otrpreferences.cpp

200 lines
8.0 KiB

/***************************************************************************
* Copyright (C) 2007 by Michael Zanetti
*
* *
* 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., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <qlayout.h>
#include <qlabel.h>
#include <qmap.h>
#include <qptrlist.h>
#include <qcombobox.h>
#include <qstringlist.h>
#include <qtable.h>
#include <qpaintdevicemetrics.h>
#include <qvbox.h>
#include <qradiobutton.h>
#include <qtabwidget.h>
#include <kgenericfactory.h>
#include <kurlrequester.h>
#include <kmessagebox.h>
#include <kconfig.h>
#include <kapplication.h>
#include <kanimwidget.h>
#include <kpassivepopup.h>
#include <kiconloader.h>
#include <kstandarddirs.h>
#include <kactivelabel.h>
#include <kopeteaccountmanager.h>
#include <kopeteaccount.h>
#include <kopeteprotocol.h>
#include "otrprefs.h"
#include "otrpreferences.h"
#include "otrplugin.h"
#include "kopete_otr.h"
/**
* @author Michael Zanetti
*/
typedef KGenericFactory<OTRPreferences> OTRPreferencesFactory;
K_EXPORT_COMPONENT_FACTORY( kcm_kopete_otr, OTRPreferencesFactory("kcm_kopete_otr"))
OTRPreferences::OTRPreferences(QWidget *parent, const char* /*name*/, const QStringList &args)
: KCModule(OTRPreferencesFactory::instance(), parent, args)
{
( new QVBoxLayout( this ) )->setAutoAdd( true );
preferencesDialog = new OTRPrefsUI(this);
this->addConfig( KopeteOtrKcfg::self(), preferencesDialog );
KopeteOtrKcfg::self()->readConfig();
load();
otrlConfInterface = new OtrlConfInterface( preferencesDialog );
connect( preferencesDialog->btGenFingerprint, SIGNAL(clicked()), SLOT(generateFingerprint()));
connect( preferencesDialog->cbKeys, SIGNAL(activated(int)), SLOT(showPrivFingerprint(int)));
connect( preferencesDialog->btVerify, SIGNAL(clicked()), SLOT(verifyFingerprint()));
connect( preferencesDialog->twSettings, SIGNAL(currentChanged(QWidget *)), SLOT(fillFingerprints()));
connect( preferencesDialog->tbFingerprints, SIGNAL(currentChanged(int, int)), SLOT(updateButtons(int, int)));
connect( preferencesDialog->btForget, SIGNAL( clicked() ), SLOT( forgetFingerprint() ) );
int index = 0;
int accountnr = 0;
QPtrList<Kopete::Account> accounts = Kopete::AccountManager::self()->accounts();
if( !accounts.isEmpty() ){
for ( QPtrListIterator<Kopete::Account> it( accounts );
Kopete::Account *account = it.current();
++it ){
if ( account->protocol()->pluginId() != "IRCProtocol" ){
preferencesDialog->cbKeys->insertItem(account->accountId() + " (" + account->protocol()->displayName() + ")");
privKeys.insert(index++, accountnr);
}
accountnr++;
}
}
showPrivFingerprint( preferencesDialog->cbKeys->currentItem() );
preferencesDialog->tbFingerprints->setColumnWidth( 0, 200 );
preferencesDialog->tbFingerprints->setColumnWidth( 1, 80 );
preferencesDialog->tbFingerprints->setColumnWidth( 2, 60 );
preferencesDialog->tbFingerprints->setColumnWidth( 3, 400 );
preferencesDialog->tbFingerprints->setColumnWidth( 4, 200 );
}
OTRPreferences::~OTRPreferences(){
}
void OTRPreferences::generateFingerprint()
{
QPtrList<Kopete::Account> accounts = Kopete::AccountManager::self()->accounts();
if( (accounts.isEmpty())){
return;
}
Kopete::Account *account = accounts.at( privKeys[preferencesDialog->cbKeys->currentItem()] );
if ((otrlConfInterface->hasPrivFingerprint( account->accountId(), account->protocol()->displayName() ) ) && (KMessageBox::questionYesNo(this, i18n("Selected account already has a key. Do you want to create a new one?"), i18n("Overwrite key?")) !=3)) return;
otrlConfInterface->generateNewPrivKey( account->accountId(), account->protocol()->displayName() );
showPrivFingerprint( preferencesDialog->cbKeys->currentItem() );
}
void OTRPreferences::showPrivFingerprint( int accountnr )
{
QPtrList<Kopete::Account> accounts = Kopete::AccountManager::self()->accounts();
if( !accounts.isEmpty() ){
Kopete::Account *account = accounts.at(privKeys[accountnr]);
preferencesDialog->tlFingerprint->setText( otrlConfInterface->getPrivFingerprint( account->accountId(), account->protocol()->displayName() ) );
}
}
void OTRPreferences::fillFingerprints(){
QTable *fingerprintsTable = preferencesDialog->tbFingerprints;
preferencesDialog->tbFingerprints->setNumRows(0);
QValueList<QString[5]> list = otrlConfInterface->readAllFingerprints();
QValueList<QString[5]>::iterator it;
int j = 0;
for( it = list.begin(); it != list.end(); ++it ){
preferencesDialog->tbFingerprints->setNumRows( preferencesDialog->tbFingerprints->numRows() +1 );
(*it)[0] = OtrlChatInterface::self()->formatContact((*it)[0]);
for( int i = 0; i < 5; i++ ){
//preferencesDialog->tbFingerprints->setText(j, i, (*it)[i] );
fingerprintsTable->setItem(j,i, new QAlignTableItem(fingerprintsTable, QTableItem::Never,(*it)[i],Qt::AlignLeft));
}
j++;
}
updateButtons( preferencesDialog->tbFingerprints->currentRow(), preferencesDialog->tbFingerprints->currentColumn() );
}
void OTRPreferences::verifyFingerprint(){
int doVerify = KMessageBox::questionYesNo(
this,
i18n("Please contact %1 via another secure way and verify that the following Fingerprint is correct:").arg(preferencesDialog->tbFingerprints->text( preferencesDialog->tbFingerprints->currentRow(), 0 )) + "\n\n" + preferencesDialog->tbFingerprints->text( preferencesDialog->tbFingerprints->currentRow(), 3 ) + "\n\n" + i18n("Are you sure you want to trust this fingerprint?"), i18n("Verify fingerprint") );
if( doVerify == KMessageBox::Yes ){
otrlConfInterface->verifyFingerprint( preferencesDialog->tbFingerprints->text( preferencesDialog->tbFingerprints->currentRow(), 3 ), true );
} else {
otrlConfInterface->verifyFingerprint( preferencesDialog->tbFingerprints->text( preferencesDialog->tbFingerprints->currentRow(), 3 ), false );
}
fillFingerprints();
}
void OTRPreferences::updateButtons( int row, int col ){
if( row != -1 ){
if( !otrlConfInterface->isEncrypted( preferencesDialog->tbFingerprints->text( row, 3 ) ) ){
preferencesDialog->btForget->setEnabled( true );
} else {
preferencesDialog->btForget->setEnabled( false );
}
preferencesDialog->btVerify->setEnabled( true );
} else {
preferencesDialog->btVerify->setEnabled( false );
preferencesDialog->btForget->setEnabled( false );
}
}
void OTRPreferences::forgetFingerprint(){
if( !otrlConfInterface->isEncrypted( preferencesDialog->tbFingerprints->text( preferencesDialog->tbFingerprints->currentRow(), 3 ) ) ){
otrlConfInterface->forgetFingerprint( preferencesDialog->tbFingerprints->text( preferencesDialog->tbFingerprints->currentRow(), 3 ) );
fillFingerprints();
} else {
updateButtons( preferencesDialog->tbFingerprints->currentRow(), preferencesDialog->tbFingerprints->currentColumn() );
}
}
QAlignTableItem :: QAlignTableItem( QTable *table, EditType editType, const QString& text, int alignment )
: QTableItem( table, editType, text ) {
align = alignment;
}
#include "otrpreferences.moc"
// vim: set noet ts=4 sts=4 sw=4: