krop's commit fixes my problem in a better way, reverting
[kdepim.git] / kmail / kmaddrbook.cpp
blob18bf79ae32566b3855ab2582f9f35f8cdfafd0d1
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2 * kmail: KDE mail client
3 * Copyright (c) 1996-1998 Stefan Taferner <taferner@kde.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "kmaddrbook.h"
21 #include "kcursorsaver.h"
23 #include <kabc/stdaddressbook.h>
24 #include <kabc/vcardconverter.h>
26 #include <kdebug.h>
27 #include <klocale.h>
28 #include <kmessagebox.h>
30 #include <QRegExp>
32 #include <unistd.h>
34 void KabcBridge::addresses(QStringList& result) // includes lists
36 KCursorSaver busy(KBusyPtr::busy()); // loading might take a while
38 KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
39 KABC::AddressBook::ConstIterator it;
40 for( it = addressBook->constBegin(); it != addressBook->constEnd(); ++it ) {
41 const QStringList emails = (*it).emails();
42 QString n = (*it).prefix() + ' ' +
43 (*it).givenName() + ' ' +
44 (*it).additionalName() + ' ' +
45 (*it).familyName() + ' ' +
46 (*it).suffix();
47 n = n.simplified();
49 QRegExp needQuotes("[^ 0-9A-Za-z\\x0080-\\xFFFF]");
50 QString endQuote = "\" ";
51 QStringList::ConstIterator mit;
52 QString addr, email;
54 for ( mit = emails.begin(); mit != emails.end(); ++mit ) {
55 email = *mit;
56 if (!email.isEmpty()) {
57 if (n.isEmpty() || (email.contains( '<' ) ))
58 addr.clear();
59 else { // do we really need quotes around this name ?
60 if (n.contains(needQuotes) )
61 addr = '"' + n + endQuote;
62 else
63 addr = n + ' ';
66 if (!addr.isEmpty() && !(email.contains( '<' ) )
67 && !(email.contains( '>' ) )
68 && !(email.contains( ',' ) ))
69 addr += '<' + email + '>';
70 else
71 addr += email;
72 addr = addr.trimmed();
73 result.append( addr );
78 result += addressBook->allDistributionListNames();
80 result.sort();
83 QStringList KabcBridge::addresses()
85 QStringList entries;
86 KABC::AddressBook::ConstIterator it;
88 const KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
89 for( it = addressBook->begin(); it != addressBook->end(); ++it ) {
90 entries += (*it).fullEmail();
92 return entries;
95 //-----------------------------------------------------------------------------
96 QString KabcBridge::expandNickName( const QString& nickName )
98 if ( nickName.isEmpty() )
99 return QString();
101 const QString lowerNickName = nickName.toLower();
102 const KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
103 for( KABC::AddressBook::ConstIterator it = addressBook->begin();
104 it != addressBook->end(); ++it ) {
105 if ( (*it).nickName().toLower() == lowerNickName )
106 return (*it).fullEmail();
108 return QString();
112 //-----------------------------------------------------------------------------
114 QStringList KabcBridge::categories()
116 KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
117 KABC::Addressee::List addresses = addressBook->allAddressees();
118 QStringList allcategories, aux;
120 for ( KABC::Addressee::List::Iterator it = addresses.begin();
121 it != addresses.end(); ++it ) {
122 aux = ( *it ).categories();
123 for ( QStringList::ConstIterator itAux = aux.constBegin();
124 itAux != aux.constEnd(); ++itAux ) {
125 // don't have duplicates in allcategories
126 if ( !allcategories.contains( *itAux ) )
127 allcategories += *itAux;
130 return allcategories;