person.cppGo to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00034 #include "person.h"
00035
00036 #include "kpimutils/email.h"
00037
00038 #include <QtCore/QRegExp>
00039
00040 #include <kdebug.h>
00041 #include <klocale.h>
00042
00043 using namespace KCal;
00044
00049
00050 class KCal::Person::Private
00051 {
00052 public:
00053 QString mName;
00054 QString mEmail;
00055 };
00056
00057
00058 Person::Person() : d( new KCal::Person::Private )
00059 {
00060 }
00061
00062 Person::Person( const QString &fullName )
00063 : d( new Private )
00064 {
00065 KPIMUtils::extractEmailAddressAndName( fullName, d->mEmail, d->mName );
00066 }
00067
00068 Person Person::fromFullName( const QString &fullName )
00069 {
00070 QString email, name;
00071 KPIMUtils::extractEmailAddressAndName( fullName, email, name );
00072 return Person( name, email );
00073 }
00074
00075 Person::Person( const QString &name, const QString &email )
00076 : d( new KCal::Person::Private )
00077 {
00078 d->mName = name;
00079 d->mEmail = email;
00080 }
00081
00082 Person::Person( const Person &person )
00083 : d( new KCal::Person::Private( *person.d ) )
00084 {
00085 }
00086
00087 Person::~Person()
00088 {
00089 delete d;
00090 }
00091
00092 #if defined(Q_CC_MSVC)
00093 bool KCal::Person::operator==( const Person &person ) const
00094 #else
00095 bool KCal::Person::operator==( const Person &person )
00096 #endif
00097 {
00098 return
00099 d->mName == person.d->mName &&
00100 d->mEmail == person.d->mEmail;
00101 }
00102
00103 Person &KCal::Person::operator=( const Person &person )
00104 {
00105
00106 if ( &person == this ) {
00107 return *this;
00108 }
00109
00110 *d = *person.d;
00111 return *this;
00112 }
00113
00114 QString Person::fullName() const
00115 {
00116 if ( d->mName.isEmpty() ) {
00117 return d->mEmail;
00118 } else {
00119 if ( d->mEmail.isEmpty() ) {
00120 return d->mName;
00121 } else {
00122
00123 QString name = d->mName;
00124 QRegExp needQuotes( "[^ 0-9A-Za-z\\x0080-\\xFFFF]" );
00125 bool weNeedToQuote = name.indexOf( needQuotes ) != -1;
00126 if ( weNeedToQuote ) {
00127 if ( name[0] != '"' ) {
00128 name.prepend( '"' );
00129 }
00130 if ( name[ name.length()-1 ] != '"' ) {
00131 name.append( '"' );
00132 }
00133 }
00134 return name + " <" + d->mEmail + '>';
00135 }
00136 }
00137 }
00138
00139 QString Person::name() const
00140 {
00141 return d->mName;
00142 }
00143
00144 QString Person::email() const
00145 {
00146 return d->mEmail;
00147 }
00148
00149 bool Person::isEmpty() const
00150 {
00151 return d->mEmail.isEmpty() && d->mName.isEmpty();
00152 }
00153
00154 void Person::setName( const QString &name )
00155 {
00156 d->mName = name;
00157 }
00158
00159 void Person::setEmail( const QString &email )
00160 {
00161 if ( email.startsWith( QLatin1String( "mailto:" ), Qt::CaseInsensitive ) ) {
00162 d->mEmail = email.mid( 7 );
00163 } else {
00164 d->mEmail = email;
00165 }
00166 }
|