From 460c52653ab0dcca6f19a4f492ed2c5e4e963ab0 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- libkdepim/kmailcompletion.cpp | 103 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 libkdepim/kmailcompletion.cpp (limited to 'libkdepim/kmailcompletion.cpp') diff --git a/libkdepim/kmailcompletion.cpp b/libkdepim/kmailcompletion.cpp new file mode 100644 index 000000000..41d2f2ce8 --- /dev/null +++ b/libkdepim/kmailcompletion.cpp @@ -0,0 +1,103 @@ +/* + This file is part of libkdepim. + + Copyright (c) 2006 Christian Schaarschmidt + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "kmailcompletion.h" +#include + +#include + +using namespace KPIM; + +KMailCompletion::KMailCompletion() +{ + setIgnoreCase( true ); +} + +void KMailCompletion::clear() +{ + m_keyMap.clear(); + KCompletion::clear(); +} + +QString KMailCompletion::makeCompletion( const QString &string ) +{ + QString match = KCompletion::makeCompletion( string ); + + // this should be in postProcessMatch, but postProcessMatch is const and will not allow nextMatch + if ( !match.isEmpty() ){ + const QString firstMatch( match ); + while ( match.find( QRegExp( "(@)|(<.*>)" ) ) == -1 ) { + /* local email do not require @domain part, if match is an address we'll find + * last+first in m_keyMap and we'll know that match is already a valid email. + * + * Distribution list do not have last+first entry, they will be in mailAddr + */ + const QStringList &mailAddr = m_keyMap[ match ]; //get all mailAddr for this keyword + bool isEmail = false; + for ( QStringList::ConstIterator sit ( mailAddr.begin() ), sEnd( mailAddr.end() ); sit != sEnd; ++sit ) + if ( (*sit).find( "<" + match + ">" ) != -1 || (*sit) == match ) { + isEmail = true; + break; + } + + if ( !isEmail ) { + // match is a keyword, skip it and try to find match + match = nextMatch(); + if ( firstMatch == match ){ + match = QString::null; + break; + } + } else + break; + } + } + return match; +} + +void KMailCompletion::addItemWithKeys( const QString& email, int weight, const QStringList* keyWords) +{ + Q_ASSERT( keyWords != 0 ); + for ( QStringList::ConstIterator it( keyWords->begin() ); it != keyWords->end(); ++it ) { + QStringList &emailList = m_keyMap[ (*it) ]; //lookup email-list for given keyword + if ( emailList.find( email ) == emailList.end() ) //add email if not there + emailList.append( email ); + addItem( (*it),weight ); //inform KCompletion about keyword + } +} + +void KMailCompletion::postProcessMatches( QStringList * pMatches )const +{ + Q_ASSERT( pMatches != 0 ); + if ( pMatches->isEmpty() ) + return; + + //KCompletion has found the keywords for us, we can now map them to mail-addr + QMap< QString, bool > mailAddrDistinct; //TODO replace with QSet in KDE4 + for ( QStringList::ConstIterator sit ( pMatches->begin() ), sEnd( pMatches->end() ); sit != sEnd; ++sit ) { + const QStringList &mailAddr = m_keyMap[ (*sit) ]; //get all mailAddr for this keyword + for ( QStringList::ConstIterator sit ( mailAddr.begin() ), sEnd( mailAddr.end() ); sit != sEnd; ++sit ) { + mailAddrDistinct[ (*sit) ] = true; //store mailAddr, QMap will make them unique + } + } + pMatches->clear(); //delete keywords + (*pMatches) += mailAddrDistinct.keys(); //add emailAddr +} +#include "kmailcompletion.moc" -- cgit v1.2.1