libkcal Library API Documentation

person.cpp

00001 /*
00002     This file is part of libkcal.
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library 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 GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include "person.h"
00022 
00023 #include <kdebug.h>
00024 #include <klocale.h>
00025 
00026 #include <qregexp.h>
00027 
00028 using namespace KCal;
00029 
00030 // Copy of KPIM::getNameAndMail from libkdepim/email.cpp, which we can't link to.
00031 // HEAD has a better solution, with a shared lib.
00032 static bool getNameAndMail(const QString& aStr, QString& name, QString& mail)
00033 {
00034   name = QString::null;
00035   mail = QString::null;
00036 
00037   const int len=aStr.length();
00038   const char cQuotes = '"';
00039 
00040   bool bInComment, bInQuotesOutsideOfEmail;
00041   int i=0, iAd=0, iMailStart=0, iMailEnd=0;
00042   QChar c;
00043 
00044   // Find the '@' of the email address
00045   // skipping all '@' inside "(...)" comments:
00046   bInComment = false;
00047   while( i < len ){
00048     c = aStr[i];
00049     if( !bInComment ){
00050       if( '(' == c ){
00051         bInComment = true;
00052       }else{
00053         if( '@' == c ){
00054           iAd = i;
00055           break; // found it
00056         }
00057       }
00058     }else{
00059       if( ')' == c ){
00060         bInComment = false;
00061       }
00062     }
00063     ++i;
00064   }
00065 
00066   if( !iAd ){
00067     // We suppose the user is typing the string manually and just
00068     // has not finished typing the mail address part.
00069     // So we take everything that's left of the '<' as name and the rest as mail
00070     for( i = 0; len > i; ++i ) {
00071       c = aStr[i];
00072       if( '<' != c )
00073         name.append( c );
00074       else
00075         break;
00076     }
00077     mail = aStr.mid( i+1 );
00078 
00079   }else{
00080 
00081     // Loop backwards until we find the start of the string
00082     // or a ',' that is outside of a comment
00083     //          and outside of quoted text before the leading '<'.
00084     bInComment = false;
00085     bInQuotesOutsideOfEmail = false;
00086     for( i = iAd-1; 0 <= i; --i ) {
00087       c = aStr[i];
00088       if( bInComment ){
00089         if( '(' == c ){
00090           if( !name.isEmpty() )
00091             name.prepend( ' ' );
00092           bInComment = false;
00093         }else{
00094           name.prepend( c ); // all comment stuff is part of the name
00095         }
00096       }else if( bInQuotesOutsideOfEmail ){
00097         if( cQuotes == c )
00098           bInQuotesOutsideOfEmail = false;
00099         name.prepend( c );
00100       }else{
00101         // found the start of this addressee ?
00102         if( ',' == c )
00103           break;
00104         // stuff is before the leading '<' ?
00105         if( iMailStart ){
00106           if( cQuotes == c )
00107             bInQuotesOutsideOfEmail = true; // end of quoted text found
00108           name.prepend( c );
00109         }else{
00110           switch( c ){
00111             case '<':
00112               iMailStart = i;
00113               break;
00114             case ')':
00115               if( !name.isEmpty() )
00116                 name.prepend( ' ' );
00117               bInComment = true;
00118               break;
00119             default:
00120               if( ' ' != c )
00121                 mail.prepend( c );
00122           }
00123         }
00124       }
00125     }
00126 
00127     name = name.simplifyWhiteSpace();
00128     mail = mail.simplifyWhiteSpace();
00129 
00130     if( mail.isEmpty() )
00131       return false;
00132 
00133     mail.append('@');
00134 
00135     // Loop forward until we find the end of the string
00136     // or a ',' that is outside of a comment
00137     //          and outside of quoted text behind the trailing '>'.
00138     bInComment = false;
00139     bInQuotesOutsideOfEmail = false;
00140     for( i = iAd+1; len > i; ++i ) {
00141       c = aStr[i];
00142       if( bInComment ){
00143         if( ')' == c ){
00144           if( !name.isEmpty() )
00145             name.append( ' ' );
00146           bInComment = false;
00147         }else{
00148           name.append( c ); // all comment stuff is part of the name
00149         }
00150       }else if( bInQuotesOutsideOfEmail ){
00151         if( cQuotes == c )
00152           bInQuotesOutsideOfEmail = false;
00153         name.append( c );
00154       }else{
00155         // found the end of this addressee ?
00156         if( ',' == c )
00157           break;
00158         // stuff is behind the trailing '>' ?
00159         if( iMailEnd ){
00160           if( cQuotes == c )
00161             bInQuotesOutsideOfEmail = true; // start of quoted text found
00162           name.append( c );
00163         }else{
00164           switch( c ){
00165             case '>':
00166               iMailEnd = i;
00167               break;
00168             case '(':
00169               if( !name.isEmpty() )
00170                 name.append( ' ' );
00171               bInComment = true;
00172               break;
00173             default:
00174               if( ' ' != c )
00175                 mail.append( c );
00176           }
00177         }
00178       }
00179     }
00180   }
00181 
00182   name = name.simplifyWhiteSpace();
00183   mail = mail.simplifyWhiteSpace();
00184 
00185   return ! (name.isEmpty() || mail.isEmpty());
00186 }
00187 
00188 Person::Person( const QString &fullName )
00189 {
00190   QString name, email;
00191   getNameAndMail( fullName, name, email );
00192   setName( name );
00193   setEmail( email );
00194 }
00195 
00196 Person::Person( const QString &name, const QString &email )
00197 {
00198   setName( name );
00199   setEmail( email );
00200 }
00201 
00202 
00203 bool KCal::operator==( const Person& p1, const Person& p2 )
00204 {
00205     return ( p1.name() == p2.name() &&
00206              p1.email() == p2.email() );
00207 }
00208 
00209 // Taken from KABC::Addressee::fullEmail
00210 QString Person::quotedName() const
00211 {
00212   QString name = mName;
00213   QRegExp needQuotes( "[^ 0-9A-Za-z]" );
00214   bool weNeedToQuote = name.find( needQuotes ) != -1;
00215   if ( weNeedToQuote ) {
00216     if ( name[0] != '"' )
00217       name.prepend( '"' );
00218     if ( name[ name.length()-1 ] != '"' )
00219       name.append( '"' );
00220   }
00221   return name;
00222 }
00223 
00224 QString Person::fullName() const
00225 {
00226   if( mName.isEmpty() ) {
00227     return mEmail;
00228   } else {
00229     if( mEmail.isEmpty() )
00230       return mName;
00231     else {
00232       return quotedName() + " <" + mEmail + ">";
00233     }
00234   }
00235 }
00236 
00237 bool Person::isEmpty() const
00238 {
00239   return mEmail.isEmpty() && mName.isEmpty();
00240 }
00241 
00242 void Person::setName(const QString &name)
00243 {
00244   mName = name;
00245 }
00246 
00247 void Person::setEmail(const QString &email)
00248 {
00249   if ( email.startsWith( "mailto:", false ) ) {
00250     mEmail = email.mid(7);
00251   } else {
00252     mEmail = email;
00253   }
00254 }
KDE Logo
This file is part of the documentation for libkcal Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Aug 23 18:18:40 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003