person.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00031
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
00045
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;
00056 }
00057 }
00058 }else{
00059 if( ')' == c ){
00060 bInComment = false;
00061 }
00062 }
00063 ++i;
00064 }
00065
00066 if( !iAd ){
00067
00068
00069
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
00082
00083
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 );
00095 }
00096 }else if( bInQuotesOutsideOfEmail ){
00097 if( cQuotes == c )
00098 bInQuotesOutsideOfEmail = false;
00099 name.prepend( c );
00100 }else{
00101
00102 if( ',' == c )
00103 break;
00104
00105 if( iMailStart ){
00106 if( cQuotes == c )
00107 bInQuotesOutsideOfEmail = true;
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
00136
00137
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 );
00149 }
00150 }else if( bInQuotesOutsideOfEmail ){
00151 if( cQuotes == c )
00152 bInQuotesOutsideOfEmail = false;
00153 name.append( c );
00154 }else{
00155
00156 if( ',' == c )
00157 break;
00158
00159 if( iMailEnd ){
00160 if( cQuotes == c )
00161 bInQuotesOutsideOfEmail = true;
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
00210 QString Person::fullName() const
00211 {
00212 if( mName.isEmpty() ) {
00213 return mEmail;
00214 } else {
00215 if( mEmail.isEmpty() )
00216 return mName;
00217 else {
00218
00219 QString name = mName;
00220 QRegExp needQuotes( "[^ 0-9A-Za-z\\x0080-\\xFFFF]" );
00221 if ( name.find( needQuotes ) != -1 )
00222 return "\"" + name + "\" <" + mEmail + ">";
00223 else
00224 return name + " <" + mEmail + ">";
00225 }
00226 }
00227 }
00228
00229 bool Person::isEmpty() const
00230 {
00231 return mEmail.isEmpty() && mName.isEmpty();
00232 }
00233
00234 void Person::setName(const QString &name)
00235 {
00236 mName = name;
00237 }
00238
00239 void Person::setEmail(const QString &email)
00240 {
00241 if ( email.startsWith( "mailto:", false ) ) {
00242 mEmail = email.mid(7);
00243 } else {
00244 mEmail = email;
00245 }
00246 }
This file is part of the documentation for libkcal Library Version 3.3.2.