libtdepim

kmailcompletion.cpp
1 /*
2  This file is part of libtdepim.
3 
4  Copyright (c) 2006 Christian Schaarschmidt <schaarsc@gmx.de>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "kmailcompletion.h"
23 #include <kdebug.h>
24 
25 #include <tqregexp.h>
26 
27 using namespace KPIM;
28 
29 KMailCompletion::KMailCompletion()
30 {
31  setIgnoreCase( true );
32 }
33 
35 {
36  m_keyMap.clear();
37  TDECompletion::clear();
38 }
39 
40 TQString KMailCompletion::makeCompletion( const TQString &string )
41 {
42  TQString match = TDECompletion::makeCompletion( string );
43 
44  // this should be in postProcessMatch, but postProcessMatch is const and will not allow nextMatch
45  if ( !match.isEmpty() ){
46  const TQString firstMatch( match );
47  while ( match.find( TQRegExp( "(@)|(<.*>)" ) ) == -1 ) {
48  /* local email do not require @domain part, if match is an address we'll find
49  * last+first <match> in m_keyMap and we'll know that match is already a valid email.
50  *
51  * Distribution list do not have last+first <match> entry, they will be in mailAddr
52  */
53  const TQStringList &mailAddr = m_keyMap[ match ]; //get all mailAddr for this keyword
54  bool isEmail = false;
55  for ( TQStringList::ConstIterator sit ( mailAddr.begin() ), sEnd( mailAddr.end() ); sit != sEnd; ++sit )
56  if ( (*sit).find( "<" + match + ">" ) != -1 || (*sit) == match ) {
57  isEmail = true;
58  break;
59  }
60 
61  if ( !isEmail ) {
62  // match is a keyword, skip it and try to find match <email@domain>
63  match = nextMatch();
64  if ( firstMatch == match ){
65  match = TQString();
66  break;
67  }
68  } else
69  break;
70  }
71  }
72  return match;
73 }
74 
75 void KMailCompletion::addItemWithKeys( const TQString& email, int weight, const TQStringList* keyWords)
76 {
77  Q_ASSERT( keyWords != 0 );
78  for ( TQStringList::ConstIterator it( keyWords->begin() ); it != keyWords->end(); ++it ) {
79  TQStringList &emailList = m_keyMap[ (*it) ]; //lookup email-list for given keyword
80  if ( emailList.find( email ) == emailList.end() ) //add email if not there
81  emailList.append( email );
82  addItem( (*it),weight ); //inform TDECompletion about keyword
83  }
84 }
85 
86 void KMailCompletion::postProcessMatches( TQStringList * pMatches )const
87 {
88  Q_ASSERT( pMatches != 0 );
89  if ( pMatches->isEmpty() )
90  return;
91 
92  //TDECompletion has found the keywords for us, we can now map them to mail-addr
93  TQMap< TQString, bool > mailAddrDistinct; //TODO replace with TQSet in KDE4
94  for ( TQStringList::ConstIterator sit ( pMatches->begin() ), sEnd( pMatches->end() ); sit != sEnd; ++sit ) {
95  const TQStringList &mailAddr = m_keyMap[ (*sit) ]; //get all mailAddr for this keyword
96  for ( TQStringList::ConstIterator sit ( mailAddr.begin() ), sEnd( mailAddr.end() ); sit != sEnd; ++sit ) {
97  mailAddrDistinct[ (*sit) ] = true; //store mailAddr, TQMap will make them unique
98  }
99  }
100  pMatches->clear(); //delete keywords
101  (*pMatches) += mailAddrDistinct.keys(); //add emailAddr
102 }
103 #include "kmailcompletion.moc"
void addItemWithKeys(const TQString &email, int weight, const TQStringList *keyWords)
Specify keywords for email.
virtual void clear()
Clears internal keyword map and calls TDECompletion::clear.
virtual void postProcessMatches(TQStringList *pMatches) const
Uses an internal map to replace all keywords in pMatches whith corrsesponding email addresses.
TQString makeCompletion(const TQString &string)
Uses TDECompletion::makeCompletion to find email addresses which starts with string.
TDEPIM classes for drag and drop of mails.