libkcal

person.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of libkcal.
00003 
00004     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020     Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include "person.h"
00024 
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027 
00028 #include <libemailfunctions/email.h>
00029 
00030 #include <qregexp.h>
00031 
00032 using namespace KCal;
00033 
00034 Person::Person( const QString &fullName )
00035 {
00036   QString name, email;
00037   KPIM::getNameAndMail( fullName, name, email );
00038   setName( name );
00039   setEmail( email );
00040 }
00041 
00042 Person::Person( const QString &name, const QString &email )
00043 {
00044   setName( name );
00045   setEmail( email );
00046 }
00047 
00048 
00049 bool KCal::operator==( const Person& p1, const Person& p2 )
00050 {
00051     return ( p1.name() == p2.name() &&
00052              p1.email() == p2.email() );
00053 }
00054 
00055 
00056 QString Person::fullName() const
00057 {
00058   if( mName.isEmpty() ) {
00059     return mEmail;
00060   } else {
00061     if( mEmail.isEmpty() )
00062       return mName;
00063     else {
00064       // Taken from KABC::Addressee::fullEmail
00065       QString name = mName;
00066       QRegExp needQuotes( "[^ 0-9A-Za-z\\x0080-\\xFFFF]" );
00067       bool weNeedToQuote = name.find( needQuotes ) != -1;
00068       if ( weNeedToQuote ) {
00069           if ( name[0] != '"' )
00070               name.prepend( '"' );
00071           if ( name[ name.length()-1 ] != '"' )
00072               name.append( '"' );
00073       }
00074       return name + " <" + mEmail + ">";
00075     }
00076   }
00077 }
00078 
00079 bool Person::isEmpty() const
00080 {
00081   return mEmail.isEmpty() && mName.isEmpty();
00082 }
00083 
00084 void Person::setName(const QString &name)
00085 {
00086   mName = name;
00087 }
00088 
00089 void Person::setEmail(const QString &email)
00090 {
00091   if ( email.startsWith( "mailto:", false ) ) {
00092     mEmail = email.mid(7);
00093   } else {
00094     mEmail = email;
00095   }
00096 }