kmail

kmaddrbook.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-file-style: "gnu" -*-
00002  * kmail: KDE mail client
00003  * Copyright (c) 1996-1998 Stefan Taferner <taferner@kde.org>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  */
00020 #include <config.h>
00021 #include <unistd.h>
00022 
00023 #include "kmaddrbook.h"
00024 #include "kcursorsaver.h"
00025 
00026 #include <kapplication.h>
00027 #include <kdebug.h>
00028 #include <klocale.h>
00029 #include <kmessagebox.h>
00030 #include <kabc/stdaddressbook.h>
00031 #include <kabc/distributionlist.h>
00032 #include <kabc/vcardconverter.h>
00033 #include <dcopref.h>
00034 
00035 #include <qregexp.h>
00036 
00037 void KabcBridge::addresses(QStringList& result) // includes lists
00038 {
00039   KCursorSaver busy(KBusyPtr::busy()); // loading might take a while
00040 
00041   KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00042   KABC::AddressBook::ConstIterator it;
00043   for( it = addressBook->begin(); it != addressBook->end(); ++it ) {
00044     const QStringList emails = (*it).emails();
00045     QString n = (*it).prefix() + " " +
00046         (*it).givenName() + " " +
00047         (*it).additionalName() + " " +
00048             (*it).familyName() + " " +
00049         (*it).suffix();
00050     n = n.simplifyWhiteSpace();
00051 
00052     QRegExp needQuotes("[^ 0-9A-Za-z\\x0080-\\xFFFF]");
00053     QString endQuote = "\" ";
00054     QStringList::ConstIterator mit;
00055     QString addr, email;
00056 
00057     for ( mit = emails.begin(); mit != emails.end(); ++mit ) {
00058       email = *mit;
00059       if (!email.isEmpty()) {
00060     if (n.isEmpty() || (email.find( '<' ) != -1))
00061       addr = QString::null;
00062     else { // do we really need quotes around this name ?
00063           if (n.find(needQuotes) != -1)
00064         addr = '"' + n + endQuote;
00065       else
00066         addr = n + ' ';
00067     }
00068 
00069     if (!addr.isEmpty() && (email.find( '<' ) == -1)
00070         && (email.find( '>' ) == -1)
00071         && (email.find( ',' ) == -1))
00072       addr += '<' + email + '>';
00073     else
00074       addr += email;
00075     addr = addr.stripWhiteSpace();
00076     result.append( addr );
00077       }
00078     }
00079   }
00080   KABC::DistributionListManager manager( addressBook );
00081   manager.load();
00082   result += manager.listNames();
00083 
00084   result.sort();
00085 }
00086 
00087 QStringList KabcBridge::addresses()
00088 {
00089     QStringList entries;
00090     KABC::AddressBook::ConstIterator it;
00091 
00092     const KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00093     for( it = addressBook->begin(); it != addressBook->end(); ++it ) {
00094         entries += (*it).fullEmail();
00095     }
00096     return entries;
00097 }
00098 
00099 //-----------------------------------------------------------------------------
00100 QString KabcBridge::expandNickName( const QString& nickName )
00101 {
00102   if ( nickName.isEmpty() )
00103     return QString::null;
00104 
00105   const QString lowerNickName = nickName.lower();
00106   const KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00107   for( KABC::AddressBook::ConstIterator it = addressBook->begin();
00108        it != addressBook->end(); ++it ) {
00109     if ( (*it).nickName().lower() == lowerNickName )
00110       return (*it).fullEmail();
00111   }
00112   return QString::null;
00113 }
00114 
00115 
00116 //-----------------------------------------------------------------------------
00117 
00118 QStringList KabcBridge::categories()
00119 {
00120   KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00121   KABC::Addressee::List addresses = addressBook->allAddressees();
00122   QStringList allcategories, aux;
00123 
00124   for ( KABC::Addressee::List::Iterator it = addresses.begin();
00125         it != addresses.end(); ++it ) {
00126     aux = ( *it ).categories();
00127     for ( QStringList::ConstIterator itAux = aux.begin();
00128           itAux != aux.end(); ++itAux ) {
00129       // don't have duplicates in allcategories
00130       if ( allcategories.find( *itAux ) == allcategories.end() )
00131         allcategories += *itAux;
00132     }
00133   }
00134   return allcategories;
00135 }