libkdepim

kmailcompletion.cpp

00001 /*
00002     This file is part of libkdepim.
00003 
00004     Copyright (c) 2006 Christian Schaarschmidt <schaarsc@gmx.de>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "kmailcompletion.h"
00023 #include <kdebug.h>
00024 
00025 #include <qregexp.h>
00026 
00027 using namespace KPIM;
00028 
00029 KMailCompletion::KMailCompletion()
00030 {
00031     setIgnoreCase( true );
00032 }
00033 
00034 void KMailCompletion::clear()
00035 {
00036   m_keyMap.clear();
00037   KCompletion::clear();
00038 }
00039 
00040 QString KMailCompletion::makeCompletion( const QString &string )
00041 {
00042   QString match = KCompletion::makeCompletion( string );
00043 
00044   // this should be in postProcessMatch, but postProcessMatch is const and will not allow nextMatch
00045   if ( !match.isEmpty() ){
00046     const QString firstMatch( match );
00047     while ( match.find( QRegExp( "(@)|(<.*>)" ) ) == -1 ) {
00048       /* local email do not require @domain part, if match is an address we'll find
00049        * last+first <match> in m_keyMap and we'll know that match is already a valid email.
00050        *
00051        * Distribution list do not have last+first <match> entry, they will be in mailAddr
00052        */
00053       const QStringList &mailAddr = m_keyMap[ match ]; //get all mailAddr for this keyword
00054       bool isEmail = false;
00055       for ( QStringList::ConstIterator sit ( mailAddr.begin() ), sEnd( mailAddr.end() ); sit != sEnd; ++sit )
00056         if ( (*sit).find( "<" + match + ">" ) != -1 || (*sit) == match ) {
00057           isEmail = true;
00058           break;
00059         }
00060 
00061       if ( !isEmail ) {
00062         // match is a keyword, skip it and try to find match <email@domain>
00063         match = nextMatch();
00064         if ( firstMatch == match ){
00065           match = QString::null;
00066           break;
00067         }
00068       } else
00069         break;
00070     }
00071   }
00072   return match;
00073 }
00074 
00075 void KMailCompletion::addItemWithKeys( const QString& email, int weight, const QStringList*  keyWords)
00076 {
00077   Q_ASSERT( keyWords != 0 );
00078   for ( QStringList::ConstIterator it( keyWords->begin() ); it != keyWords->end(); ++it ) {
00079     QStringList &emailList = m_keyMap[ (*it) ];      //lookup email-list for given keyword
00080     if ( emailList.find( email ) == emailList.end() ) //add email if not there
00081       emailList.append( email );
00082     addItem( (*it),weight );                         //inform KCompletion about keyword
00083     }
00084 }
00085 
00086 void KMailCompletion::postProcessMatches( QStringList * pMatches )const
00087 {
00088   Q_ASSERT( pMatches != 0 );
00089   if ( pMatches->isEmpty() )
00090     return;
00091 
00092   //KCompletion has found the keywords for us, we can now map them to mail-addr
00093   QMap< QString, bool > mailAddrDistinct; //TODO replace with QSet in KDE4
00094   for ( QStringList::ConstIterator sit ( pMatches->begin() ), sEnd( pMatches->end() ); sit != sEnd; ++sit ) {
00095     const QStringList &mailAddr = m_keyMap[ (*sit) ]; //get all mailAddr for this keyword
00096     for ( QStringList::ConstIterator sit ( mailAddr.begin() ), sEnd( mailAddr.end() ); sit != sEnd; ++sit ) {
00097       mailAddrDistinct[ (*sit) ] = true;  //store mailAddr, QMap will make them unique
00098     }
00099   }
00100   pMatches->clear();                      //delete keywords
00101   (*pMatches) += mailAddrDistinct.keys(); //add emailAddr
00102 }
00103 #include "kmailcompletion.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys