email.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "email.h"
00032 #include <kdebug.h>
00033
00034
00035 QStringList KPIM::splitEmailAddrList(const QString& aStr, bool allowSemicolonAsSeparator)
00036 {
00037
00038
00039
00040
00041
00042
00043
00044
00045 QStringList list;
00046
00047 if (aStr.isEmpty())
00048 return list;
00049
00050 QString addr;
00051 uint addrstart = 0;
00052 int commentlevel = 0;
00053 bool insidequote = false;
00054
00055 for (uint index=0; index<aStr.length(); index++) {
00056
00057
00058 switch (aStr[index].latin1()) {
00059 case '"' :
00060 if (commentlevel == 0)
00061 insidequote = !insidequote;
00062 break;
00063 case '(' :
00064 if (!insidequote)
00065 commentlevel++;
00066 break;
00067 case ')' :
00068 if (!insidequote) {
00069 if (commentlevel > 0)
00070 commentlevel--;
00071 else {
00072 kdDebug(5300) << "Error in address splitting: Unmatched ')'"
00073 << endl;
00074 return list;
00075 }
00076 }
00077 break;
00078 case '\\' :
00079 index++;
00080 break;
00081 case ';' :
00082 if ( !allowSemicolonAsSeparator )
00083 break;
00084 case ',' :
00085 if (!insidequote && (commentlevel == 0)) {
00086 addr = aStr.mid(addrstart, index-addrstart);
00087 kdDebug() << endl << "Adding: " << addr.simplifyWhiteSpace() << endl;
00088 if (!addr.isEmpty())
00089 list += addr.simplifyWhiteSpace();
00090 addrstart = index+1;
00091 }
00092 break;
00093 }
00094 }
00095
00096 if (!insidequote && (commentlevel == 0)) {
00097 addr = aStr.mid(addrstart, aStr.length()-addrstart);
00098 if (!addr.isEmpty())
00099 list += addr.simplifyWhiteSpace();
00100 }
00101 else
00102 kdDebug(5300) << "Error in address splitting: "
00103 << "Unexpected end of address list"
00104 << endl;
00105
00106 return list;
00107 }
00108
00109
00110 QCString KPIM::getEmailAddr(const QString& aStr)
00111 {
00112 int a, i, j, len, found = 0;
00113 QChar c;
00114
00115 a = aStr.find('@');
00116 if (a<0) return aStr.latin1();
00117
00118 for (i = a - 1; i >= 0; i--) {
00119 c = aStr[i];
00120 if (c == '<' || c == '(' || c == ' ') found = 1;
00121 if (found) break;
00122 }
00123
00124 found = 0;
00125
00126 for (j = a + 1; j < (int)aStr.length(); j++) {
00127 c = aStr[j];
00128 if (c == '>' || c == ')' || c == ' ') found = 1;
00129 if (found) break;
00130 }
00131
00132 len = j - (i + 1);
00133 return aStr.mid(i+1,len).latin1();
00134 }
00135
00136 bool KPIM::getNameAndMail(const QString& aStr, QString& name, QString& mail)
00137 {
00138 name = QString::null;
00139 mail = QString::null;
00140
00141 const int len=aStr.length();
00142 const char cQuotes = '"';
00143
00144 bool bInComment, bInQuotesOutsideOfEmail;
00145 int i=0, iAd=0, iMailStart=0, iMailEnd=0;
00146 QChar c;
00147
00148
00149
00150 bInComment = false;
00151 while( i < len ){
00152 c = aStr[i];
00153 if( !bInComment ){
00154 if( '(' == c ){
00155 bInComment = true;
00156 }else{
00157 if( '@' == c ){
00158 iAd = i;
00159 break;
00160 }
00161 }
00162 }else{
00163 if( ')' == c ){
00164 bInComment = false;
00165 }
00166 }
00167 ++i;
00168 }
00169
00170 if( !iAd ){
00171
00172
00173
00174 for( i = 0; len > i; ++i ) {
00175 c = aStr[i];
00176 if( '<' != c )
00177 name.append( c );
00178 else
00179 break;
00180 }
00181 mail = aStr.mid( i+1 );
00182 if ( mail.endsWith( ">" ) )
00183 mail.truncate( mail.length() - 1 );
00184
00185 }else{
00186
00187
00188
00189
00190 bInComment = false;
00191 bInQuotesOutsideOfEmail = false;
00192 for( i = iAd-1; 0 <= i; --i ) {
00193 c = aStr[i];
00194 if( bInComment ){
00195 if( '(' == c ){
00196 if( !name.isEmpty() )
00197 name.prepend( ' ' );
00198 bInComment = false;
00199 }else{
00200 name.prepend( c );
00201 }
00202 }else if( bInQuotesOutsideOfEmail ){
00203 if( cQuotes == c )
00204 bInQuotesOutsideOfEmail = false;
00205 else
00206 name.prepend( c );
00207 }else{
00208
00209 if( ',' == c )
00210 break;
00211
00212 if( iMailStart ){
00213 if( cQuotes == c )
00214 bInQuotesOutsideOfEmail = true;
00215 else
00216 name.prepend( c );
00217 }else{
00218 switch( c ){
00219 case '<':
00220 iMailStart = i;
00221 break;
00222 case ')':
00223 if( !name.isEmpty() )
00224 name.prepend( ' ' );
00225 bInComment = true;
00226 break;
00227 default:
00228 if( ' ' != c )
00229 mail.prepend( c );
00230 }
00231 }
00232 }
00233 }
00234
00235 name = name.simplifyWhiteSpace();
00236 mail = mail.simplifyWhiteSpace();
00237
00238 if( mail.isEmpty() )
00239 return false;
00240
00241 mail.append('@');
00242
00243
00244
00245
00246 bInComment = false;
00247 bInQuotesOutsideOfEmail = false;
00248 int parenthesesNesting = 0;
00249 for( i = iAd+1; len > i; ++i ) {
00250 c = aStr[i];
00251 if( bInComment ){
00252 if( ')' == c ){
00253 if ( --parenthesesNesting == 0 ) {
00254 bInComment = false;
00255 if( !name.isEmpty() )
00256 name.append( ' ' );
00257 } else {
00258
00259 name.append( ')' );
00260 }
00261 } else {
00262 if( '(' == c ) {
00263
00264 ++parenthesesNesting;
00265 }
00266 name.append( c );
00267 }
00268 }else if( bInQuotesOutsideOfEmail ){
00269 if( cQuotes == c )
00270 bInQuotesOutsideOfEmail = false;
00271 else
00272 name.append( c );
00273 }else{
00274
00275 if( ',' == c )
00276 break;
00277
00278 if( iMailEnd ){
00279 if( cQuotes == c )
00280 bInQuotesOutsideOfEmail = true;
00281 else
00282 name.append( c );
00283 }else{
00284 switch( c ){
00285 case '>':
00286 iMailEnd = i;
00287 break;
00288 case '(':
00289 if( !name.isEmpty() )
00290 name.append( ' ' );
00291 if ( ++parenthesesNesting > 0 )
00292 bInComment = true;
00293 break;
00294 default:
00295 if( ' ' != c )
00296 mail.append( c );
00297 }
00298 }
00299 }
00300 }
00301 }
00302
00303 name = name.simplifyWhiteSpace();
00304 mail = mail.simplifyWhiteSpace();
00305
00306 return ! (name.isEmpty() || mail.isEmpty());
00307 }
00308
00309 bool KPIM::compareEmail( const QString& email1, const QString& email2,
00310 bool matchName )
00311 {
00312 QString e1Name, e1Email, e2Name, e2Email;
00313
00314 getNameAndMail( email1, e1Name, e1Email );
00315 getNameAndMail( email2, e2Name, e2Email );
00316
00317 return e1Email == e2Email &&
00318 ( !matchName || ( e1Name == e2Name ) );
00319 }
00320
00321
00322 QStringList KPIM::splitEmailAddrList(const QString& aStr)
00323 {
00324 return splitEmailAddrList( aStr, false );
00325 }
00326
00327 QString KPIM::quotedName( const QString& fullName )
00328 {
00329 static const QString specials("()<>@,.;:[]");
00330
00331 bool needsQuotes=false;
00332 if ( fullName[0] == '"' && fullName[fullName.length()-1] == '"' ) return fullName;
00333 QString result;
00334 for (unsigned int i=0; i < fullName.length(); i++) {
00335 if ( specials.contains( fullName[i] ) )
00336 needsQuotes = true;
00337 else if ( fullName[i] == '\\' || fullName[i] == '"' ) {
00338 needsQuotes = true;
00339 result += '\\';
00340 }
00341 result += fullName[i];
00342 }
00343
00344 if (needsQuotes) {
00345 result.insert(0,'"');
00346 result += '"';
00347 }
00348 return result;
00349
00350 }
This file is part of the documentation for libkdepim Library Version 3.3.2.