kabc Library API Documentation

addressee.src.cpp

00001 /*
00002     This file is part of libkabc.
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00004     Copyright (c) 2003 Carsten Pfeiffer <pfeiffer@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include <qregexp.h>
00023 
00024 #include <ksharedptr.h>
00025 #include <kdebug.h>
00026 #include <kapplication.h>
00027 #include <klocale.h>
00028 
00029 #include "addresseehelper.h"
00030 #include "field.h"
00031 #include "resource.h"
00032 
00033 #include "addressee.h"
00034 
00035 using namespace KABC;
00036 
00037 static bool matchBinaryPattern( int value, int pattern );
00038 KABC::Field *Addressee::mSortField = 0;
00039 
00040 struct Addressee::AddresseeData : public KShared
00041 {
00042   QString uid;
00043   --VARIABLES--
00044 
00045   PhoneNumber::List phoneNumbers;
00046   Address::List addresses;
00047   Key::List keys;
00048   QStringList emails;
00049   QStringList categories;
00050   QStringList custom;
00051 
00052   Resource *resource;
00053 
00054   bool empty    :1;
00055   bool changed  :1;
00056 };
00057 
00058 Addressee::AddresseeData* Addressee::shared_null = 0;
00059 
00060 Addressee::AddresseeData* Addressee::makeSharedNull()
00061 {
00062   Addressee::shared_null = new AddresseeData;
00063   shared_null->_KShared_ref(); //just in case (we should add KSD)
00064   shared_null->empty = true;
00065   shared_null->changed = false;
00066   shared_null->resource = 0;
00067   return shared_null;
00068 }
00069 
00070 Addressee::Addressee()
00071 {
00072   mData = shared_null ? shared_null : makeSharedNull();
00073 }
00074 
00075 Addressee::~Addressee()
00076 {
00077 }
00078 
00079 Addressee::Addressee( const Addressee &a )
00080 {
00081   mData = a.mData;
00082 }
00083 
00084 Addressee &Addressee::operator=( const Addressee &a )
00085 {
00086   mData = a.mData;
00087   return (*this);
00088 }
00089 
00090 void Addressee::detach()
00091 {
00092   if ( mData.data() == shared_null ) {
00093     mData = new AddresseeData;
00094     mData->empty = true;
00095     mData->changed = false;
00096     mData->resource = 0;
00097     return;
00098   } else if ( mData.count() == 1 ) return;
00099 
00100   AddresseeData data = *mData;
00101   mData = new AddresseeData( data );
00102 }
00103 
00104 bool Addressee::operator==( const Addressee &a ) const
00105 {
00106   if ( uid() != a.uid() ) return false;
00107   --EQUALSTEST--
00108   if ( ( mData->url.isValid() || a.mData->url.isValid() ) &&
00109        ( mData->url != a.mData->url ) ) return false;
00110   if ( mData->phoneNumbers != a.mData->phoneNumbers ) return false;
00111   if ( mData->addresses != a.mData->addresses ) return false;
00112   if ( mData->keys != a.mData->keys ) return false;
00113   if ( mData->emails != a.mData->emails ) return false;
00114   if ( mData->categories != a.mData->categories ) return false;
00115   if ( mData->custom != a.mData->custom ) return false;
00116 
00117   return true;
00118 }
00119 
00120 bool Addressee::operator!=( const Addressee &a ) const
00121 {
00122   return !( a == *this );
00123 }
00124 
00125 bool Addressee::isEmpty() const
00126 {
00127   return mData->empty;
00128 }
00129 
00130 void Addressee::setUid( const QString &id )
00131 {
00132   if ( id == mData->uid ) return;
00133   detach();
00134   mData->empty = false;
00135   mData->uid = id;
00136 }
00137 
00138 QString Addressee::uid() const
00139 {
00140   if ( mData->uid.isEmpty() )
00141     mData->uid = KApplication::randomString( 10 );
00142 
00143   return mData->uid;
00144 }
00145 
00146 QString Addressee::uidLabel()
00147 {
00148   return i18n("Unique Identifier");
00149 }
00150 
00151 --DEFINITIONS--
00152 
00153 void Addressee::setNameFromString( const QString &str )
00154 {
00155   setFormattedName( str );
00156   setName( str );
00157 
00158   // clear all name parts
00159   setPrefix( QString::null );
00160   setGivenName( QString::null );
00161   setAdditionalName( QString::null );
00162   setFamilyName( QString::null );
00163   setSuffix( QString::null );
00164 
00165   if ( str.isEmpty() )
00166     return;
00167 
00168   QString spaceStr = " ";
00169   QString emptyStr = "";
00170   AddresseeHelper *helper = AddresseeHelper::self();
00171       
00172   int i = str.find( ',' );
00173   if( i < 0 ) {
00174     QStringList parts = QStringList::split( spaceStr, str );
00175     int leftOffset = 0;
00176     int rightOffset = parts.count() - 1;
00177 
00178     QString suffix;
00179     while ( rightOffset >= 0 ) {
00180       if ( helper->containsSuffix( parts[ rightOffset ] ) ) {
00181         suffix.prepend(parts[ rightOffset ] + (suffix.isEmpty() ? emptyStr : spaceStr));
00182         rightOffset--;
00183       } else
00184         break;
00185     }
00186     setSuffix( suffix );
00187 
00188     if ( rightOffset < 0 )
00189       return;
00190 
00191     if ( rightOffset - 1 >= 0 && helper->containsPrefix( parts[ rightOffset - 1 ].lower() ) ) {
00192       setFamilyName( parts[ rightOffset - 1 ] + spaceStr + parts[ rightOffset ] );
00193       rightOffset--;
00194     } else
00195       setFamilyName( parts[ rightOffset ] );
00196 
00197     QString prefix;
00198     while ( leftOffset < rightOffset ) {
00199       if ( helper->containsTitle( parts[ leftOffset ] ) ) {
00200         prefix.append( ( prefix.isEmpty() ? emptyStr : spaceStr) + parts[ leftOffset ] );
00201         leftOffset++;
00202       } else
00203         break;
00204     }
00205     setPrefix( prefix );
00206 
00207     if ( leftOffset < rightOffset ) {
00208       setGivenName( parts[ leftOffset ] );
00209       leftOffset++;
00210     }
00211 
00212     QString additionalName;
00213     while ( leftOffset < rightOffset ) {
00214       additionalName.append( ( additionalName.isEmpty() ? emptyStr : spaceStr) + parts[ leftOffset ] );
00215       leftOffset++;
00216     }
00217     setAdditionalName( additionalName );
00218   } else {
00219     QString part1 = str.left( i );
00220     QString part2 = str.mid( i + 1 );
00221 
00222     QStringList parts = QStringList::split( spaceStr, part1 );
00223     int leftOffset = 0;
00224     int rightOffset = parts.count() - 1;
00225 
00226     if ( parts.count() > 0 ) {
00227 
00228       QString suffix;
00229       while ( rightOffset >= 0 ) {
00230         if ( helper->containsSuffix( parts[ rightOffset ] ) ) {
00231           suffix.prepend(parts[ rightOffset ] + (suffix.isEmpty() ? emptyStr : spaceStr));
00232           rightOffset--;
00233         } else
00234           break;
00235       }
00236       setSuffix( suffix );
00237 
00238       if ( rightOffset - 1 >= 0 && helper->containsPrefix( parts[ rightOffset - 1 ].lower() ) ) {
00239         setFamilyName( parts[ rightOffset - 1 ] + spaceStr + parts[ rightOffset ] );
00240         rightOffset--;
00241       } else
00242         setFamilyName( parts[ rightOffset ] );
00243 
00244       QString prefix;
00245       while ( leftOffset < rightOffset ) {
00246         if ( helper->containsTitle( parts[ leftOffset ] ) ) {
00247           prefix.append( ( prefix.isEmpty() ? emptyStr : spaceStr) + parts[ leftOffset ] );
00248           leftOffset++;
00249         } else
00250           break;
00251       }
00252     } else {
00253       setPrefix( "" );
00254       setFamilyName( "" );
00255       setSuffix( "" );
00256     }
00257 
00258     parts = QStringList::split( spaceStr, part2 );
00259 
00260     leftOffset = 0;
00261     rightOffset = parts.count();
00262 
00263     if ( parts.count() > 0 ) {
00264 
00265       QString prefix;
00266       while ( leftOffset < rightOffset ) {
00267         if ( helper->containsTitle( parts[ leftOffset ] ) ) {
00268           prefix.append( ( prefix.isEmpty() ? emptyStr : spaceStr) + parts[ leftOffset ] );
00269           leftOffset++;
00270         } else
00271           break;
00272       }
00273       setPrefix( prefix );
00274 
00275       if ( leftOffset < rightOffset ) {
00276         setGivenName( parts[ leftOffset ] );
00277         leftOffset++;
00278       }
00279 
00280       QString additionalName;
00281       while ( leftOffset < rightOffset ) {
00282         additionalName.append( ( additionalName.isEmpty() ? emptyStr : spaceStr) + parts[ leftOffset ] );
00283         leftOffset++;
00284       }
00285       setAdditionalName( additionalName );
00286     } else {
00287       setGivenName( "" );
00288       setAdditionalName( "" );
00289     }
00290   }
00291 }
00292 
00293 QString Addressee::realName() const
00294 {
00295   if ( !formattedName().isEmpty() )
00296     return formattedName();
00297 
00298   QString n = assembledName();
00299 
00300   if ( n.isEmpty() )
00301     n = name();
00302 
00303   return n;
00304 }
00305 
00306 QString Addressee::assembledName() const
00307 {
00308   QString name = prefix() + " " + givenName() + " " + additionalName() + " " +
00309               familyName() + " " + suffix();
00310 
00311   return name.simplifyWhiteSpace();
00312 }
00313 
00314 QString Addressee::fullEmail( const QString &email ) const
00315 {
00316   QString e;
00317   if ( email.isNull() ) {
00318     e = preferredEmail();
00319   } else {
00320     e = email;
00321   }
00322   if ( e.isEmpty() ) return QString::null;
00323 
00324   QString text;
00325   if ( realName().isEmpty() )
00326     text = e;
00327   else {
00328     QRegExp needQuotes( "[^ 0-9A-Za-z\\x0080-\\xFFFF]" );
00329     if ( realName().find( needQuotes ) != -1 )
00330       text = "\"" + realName() + "\" <" + e + ">";
00331     else
00332       text = realName() + " <" + e + ">";
00333   }
00334 
00335   return text;
00336 }
00337 
00338 void Addressee::insertEmail( const QString &email, bool preferred )
00339 {
00340   if (email.simplifyWhiteSpace().isEmpty())
00341     return;
00342   detach();
00343 
00344   QStringList::Iterator it = mData->emails.find( email );
00345 
00346   if ( it != mData->emails.end() ) {
00347     if ( !preferred || it == mData->emails.begin() ) return;
00348     mData->emails.remove( it );
00349     mData->emails.prepend( email );
00350   } else {
00351     if ( preferred ) {
00352       mData->emails.prepend( email );
00353     } else {
00354       mData->emails.append( email );
00355     }
00356   }
00357 }
00358 
00359 void Addressee::removeEmail( const QString &email )
00360 {
00361   detach();
00362 
00363   QStringList::Iterator it = mData->emails.find( email );
00364   if ( it == mData->emails.end() ) return;
00365 
00366   mData->emails.remove( it );
00367 }
00368 
00369 QString Addressee::preferredEmail() const
00370 {
00371   if ( mData->emails.count() == 0 ) return QString::null;
00372   else return mData->emails.first();
00373 }
00374 
00375 QStringList Addressee::emails() const
00376 {
00377   return mData->emails;
00378 }
00379 void Addressee::setEmails( const QStringList& emails ) {
00380     detach();
00381     mData->emails = emails;
00382 }
00383 void Addressee::insertPhoneNumber( const PhoneNumber &phoneNumber )
00384 {
00385   detach();
00386   mData->empty = false;
00387 
00388   PhoneNumber::List::Iterator it;
00389   for( it = mData->phoneNumbers.begin(); it != mData->phoneNumbers.end(); ++it ) {
00390     if ( (*it).id() == phoneNumber.id() ) {
00391       *it = phoneNumber;
00392       return;
00393     }
00394   }
00395   if ( !phoneNumber.number().simplifyWhiteSpace().isEmpty() )
00396     mData->phoneNumbers.append( phoneNumber );
00397 }
00398 
00399 void Addressee::removePhoneNumber( const PhoneNumber &phoneNumber )
00400 {
00401   detach();
00402 
00403   PhoneNumber::List::Iterator it;
00404   for( it = mData->phoneNumbers.begin(); it != mData->phoneNumbers.end(); ++it ) {
00405     if ( (*it).id() == phoneNumber.id() ) {
00406       mData->phoneNumbers.remove( it );
00407       return;
00408     }
00409   }
00410 }
00411 
00412 PhoneNumber Addressee::phoneNumber( int type ) const
00413 {
00414   PhoneNumber phoneNumber( "", type );
00415   PhoneNumber::List::ConstIterator it;
00416   for( it = mData->phoneNumbers.begin(); it != mData->phoneNumbers.end(); ++it ) {
00417     if ( matchBinaryPattern( (*it).type(), type ) ) {
00418       if ( (*it).type() & PhoneNumber::Pref )
00419         return (*it);
00420       else if ( phoneNumber.number().isEmpty() )
00421         phoneNumber = (*it);
00422     }
00423   }
00424 
00425   return phoneNumber;
00426 }
00427 
00428 PhoneNumber::List Addressee::phoneNumbers() const
00429 {
00430   return mData->phoneNumbers;
00431 }
00432 
00433 PhoneNumber::List Addressee::phoneNumbers( int type ) const
00434 {
00435   PhoneNumber::List list;
00436 
00437   PhoneNumber::List::ConstIterator it;
00438   for( it = mData->phoneNumbers.begin(); it != mData->phoneNumbers.end(); ++it ) {
00439     if ( matchBinaryPattern( (*it).type(), type ) ) {
00440       list.append( *it );
00441     }
00442   }
00443   return list;
00444 }
00445 
00446 PhoneNumber Addressee::findPhoneNumber( const QString &id ) const
00447 {
00448   PhoneNumber::List::ConstIterator it;
00449   for( it = mData->phoneNumbers.begin(); it != mData->phoneNumbers.end(); ++it ) {
00450     if ( (*it).id() == id ) {
00451       return *it;
00452     }
00453   }
00454   return PhoneNumber();
00455 }
00456 
00457 void Addressee::insertKey( const Key &key )
00458 {
00459   detach();
00460   mData->empty = false;
00461 
00462   Key::List::Iterator it;
00463   for( it = mData->keys.begin(); it != mData->keys.end(); ++it ) {
00464     if ( (*it).id() == key.id() ) {
00465       *it = key;
00466       return;
00467     }
00468   }
00469   mData->keys.append( key );
00470 }
00471 
00472 void Addressee::removeKey( const Key &key )
00473 {
00474   detach();
00475 
00476   Key::List::Iterator it;
00477   for( it = mData->keys.begin(); it != mData->keys.end(); ++it ) {
00478     if ( (*it).id() == key.id() ) {
00479       mData->keys.remove( key );
00480       return;
00481     }
00482   }
00483 }
00484 
00485 Key Addressee::key( int type, QString customTypeString ) const
00486 {
00487   Key::List::ConstIterator it;
00488   for( it = mData->keys.begin(); it != mData->keys.end(); ++it ) {
00489     if ( (*it).type() == type ) {
00490       if ( type == Key::Custom ) {
00491         if ( customTypeString.isEmpty() ) {
00492           return *it;
00493         } else {
00494           if ( (*it).customTypeString() == customTypeString )
00495             return (*it);
00496         }
00497       } else {
00498         return *it;
00499       }
00500     }
00501   }
00502   return Key( QString(), type );
00503 }
00504 
00505 void Addressee::setKeys( const Key::List& list )
00506 {
00507   detach();
00508   mData->keys = list;
00509 }
00510 
00511 Key::List Addressee::keys() const
00512 {
00513   return mData->keys;
00514 }
00515 
00516 Key::List Addressee::keys( int type, QString customTypeString ) const
00517 {
00518   Key::List list;
00519 
00520   Key::List::ConstIterator it;
00521   for( it = mData->keys.begin(); it != mData->keys.end(); ++it ) {
00522     if ( (*it).type() == type ) {
00523       if ( type == Key::Custom ) {
00524         if ( customTypeString.isEmpty() ) {
00525           list.append( *it );
00526         } else {
00527           if ( (*it).customTypeString() == customTypeString )
00528             list.append( *it );
00529         }
00530       } else {
00531         list.append( *it );
00532       }
00533     }
00534   }
00535   return list;
00536 }
00537 
00538 Key Addressee::findKey( const QString &id ) const
00539 {
00540   Key::List::ConstIterator it;
00541   for( it = mData->keys.begin(); it != mData->keys.end(); ++it ) {
00542     if ( (*it).id() == id ) {
00543       return *it;
00544     }
00545   }
00546   return Key();
00547 }
00548 
00549 QString Addressee::asString() const
00550 {
00551   return "Smith, agent Smith...";
00552 }
00553 
00554 void Addressee::dump() const
00555 {
00556   kdDebug(5700) << "Addressee {" << endl;
00557 
00558   kdDebug(5700) << "  Uid: '" << uid() << "'" << endl;
00559 
00560   --DEBUG--
00561 
00562   kdDebug(5700) << "  Emails {" << endl;
00563   QStringList e = emails();
00564   QStringList::ConstIterator it;
00565   for( it = e.begin(); it != e.end(); ++it ) {
00566     kdDebug(5700) << "    " << (*it) << endl;
00567   }
00568   kdDebug(5700) << "  }" << endl;
00569 
00570   kdDebug(5700) << "  PhoneNumbers {" << endl;
00571   PhoneNumber::List p = phoneNumbers();
00572   PhoneNumber::List::ConstIterator it2;
00573   for( it2 = p.begin(); it2 != p.end(); ++it2 ) {
00574     kdDebug(5700) << "    Type: " << int((*it2).type()) << " Number: " << (*it2).number() << endl;
00575   }
00576   kdDebug(5700) << "  }" << endl;
00577 
00578   Address::List a = addresses();
00579   Address::List::ConstIterator it3;
00580   for( it3 = a.begin(); it3 != a.end(); ++it3 ) {
00581     (*it3).dump();
00582   }
00583 
00584   kdDebug(5700) << "  Keys {" << endl;
00585   Key::List k = keys();
00586   Key::List::ConstIterator it4;
00587   for( it4 = k.begin(); it4 != k.end(); ++it4 ) {
00588     kdDebug(5700) << "    Type: " << int((*it4).type()) <<
00589                      " Key: " << (*it4).textData() <<
00590                      " CustomString: " << (*it4).customTypeString() << endl;
00591   }
00592   kdDebug(5700) << "  }" << endl;
00593 
00594   kdDebug(5700) << "}" << endl;
00595 }
00596 
00597 
00598 void Addressee::insertAddress( const Address &address )
00599 {
00600   detach();
00601   mData->empty = false;
00602 
00603   Address::List::Iterator it;
00604   for( it = mData->addresses.begin(); it != mData->addresses.end(); ++it ) {
00605     if ( (*it).id() == address.id() ) {
00606       *it = address;
00607       return;
00608     }
00609   }
00610   mData->addresses.append( address );
00611 }
00612 
00613 void Addressee::removeAddress( const Address &address )
00614 {
00615   detach();
00616 
00617   Address::List::Iterator it;
00618   for( it = mData->addresses.begin(); it != mData->addresses.end(); ++it ) {
00619     if ( (*it).id() == address.id() ) {
00620       mData->addresses.remove( it );
00621       return;
00622     }
00623   }
00624 }
00625 
00626 Address Addressee::address( int type ) const
00627 {
00628   Address address( type );
00629   Address::List::ConstIterator it;
00630   for( it = mData->addresses.begin(); it != mData->addresses.end(); ++it ) {
00631     if ( matchBinaryPattern( (*it).type(), type ) ) {
00632       if ( (*it).type() & Address::Pref )
00633         return (*it);
00634       else if ( address.isEmpty() )
00635         address = (*it);
00636     }
00637   }
00638 
00639   return address;
00640 }
00641 
00642 Address::List Addressee::addresses() const
00643 {
00644   return mData->addresses;
00645 }
00646 
00647 Address::List Addressee::addresses( int type ) const
00648 {
00649   Address::List list;
00650 
00651   Address::List::ConstIterator it;
00652   for( it = mData->addresses.begin(); it != mData->addresses.end(); ++it ) {
00653     if ( matchBinaryPattern( (*it).type(), type ) ) {
00654       list.append( *it );
00655     }
00656   }
00657 
00658   return list;
00659 }
00660 
00661 Address Addressee::findAddress( const QString &id ) const
00662 {
00663   Address::List::ConstIterator it;
00664   for( it = mData->addresses.begin(); it != mData->addresses.end(); ++it ) {
00665     if ( (*it).id() == id ) {
00666       return *it;
00667     }
00668   }
00669   return Address();
00670 }
00671 
00672 void Addressee::insertCategory( const QString &c )
00673 {
00674   detach();
00675   mData->empty = false;
00676 
00677   if ( mData->categories.findIndex( c ) != -1 ) return;
00678 
00679   mData->categories.append( c );
00680 }
00681 
00682 void Addressee::removeCategory( const QString &c )
00683 {
00684   detach();
00685 
00686   QStringList::Iterator it = mData->categories.find( c );
00687   if ( it == mData->categories.end() ) return;
00688 
00689   mData->categories.remove( it );
00690 }
00691 
00692 bool Addressee::hasCategory( const QString &c ) const
00693 {
00694   return ( mData->categories.findIndex( c ) != -1 );
00695 }
00696 
00697 void Addressee::setCategories( const QStringList &c )
00698 {
00699   detach();
00700   mData->empty = false;
00701 
00702   mData->categories = c;
00703 }
00704 
00705 QStringList Addressee::categories() const
00706 {
00707   return mData->categories;
00708 }
00709 
00710 void Addressee::insertCustom( const QString &app, const QString &name,
00711                               const QString &value )
00712 {
00713   if ( value.isNull() || name.isEmpty() || app.isEmpty() ) return;
00714 
00715   detach();
00716   mData->empty = false;
00717 
00718   QString qualifiedName = app + "-" + name + ":";
00719 
00720   QStringList::Iterator it;
00721   for( it = mData->custom.begin(); it != mData->custom.end(); ++it ) {
00722     if ( (*it).startsWith( qualifiedName ) ) {
00723       (*it) = qualifiedName + value;
00724       return;
00725     }
00726   }
00727 
00728   mData->custom.append( qualifiedName + value );
00729 }
00730 
00731 void Addressee::removeCustom( const QString &app, const QString &name)
00732 {
00733   detach();
00734 
00735   QString qualifiedName = app + "-" + name + ":";
00736 
00737   QStringList::Iterator it;
00738   for( it = mData->custom.begin(); it != mData->custom.end(); ++it ) {
00739     if ( (*it).startsWith( qualifiedName ) ) {
00740       mData->custom.remove( it );
00741       return;
00742     }
00743   }
00744 }
00745 
00746 QString Addressee::custom( const QString &app, const QString &name ) const
00747 {
00748   QString qualifiedName = app + "-" + name + ":";
00749   QString value;
00750 
00751   QStringList::ConstIterator it;
00752   for( it = mData->custom.begin(); it != mData->custom.end(); ++it ) {
00753     if ( (*it).startsWith( qualifiedName ) ) {
00754       value = (*it).mid( (*it).find( ":" ) + 1 );
00755       break;
00756     }
00757   }
00758 
00759   return value;
00760 }
00761 
00762 void Addressee::setCustoms( const QStringList &l )
00763 {
00764   detach();
00765   mData->empty = false;
00766 
00767   mData->custom = l;
00768 }
00769 
00770 QStringList Addressee::customs() const
00771 {
00772   return mData->custom;
00773 }
00774 
00775 void Addressee::parseEmailAddress( const QString &rawEmail, QString &fullName,
00776                                    QString &email)
00777 {
00778   int startPos, endPos, len;
00779   QString partA, partB, result;
00780   char endCh = '>';
00781 
00782   startPos = rawEmail.find( '<' );
00783   if ( startPos < 0 ) {
00784     startPos = rawEmail.find( '(' );
00785     endCh = ')';
00786   }
00787 
00788   if ( startPos < 0 ) {
00789     // We couldn't find any separators, so we assume the whole string
00790     // is the email address
00791     email = rawEmail;
00792     fullName = "";
00793   } else {
00794     // We have a start position, try to find an end
00795     endPos = rawEmail.find( endCh, startPos + 1 );
00796 
00797     if ( endPos < 0 ) {
00798       // We couldn't find the end of the email address. We can only
00799       // assume the entire string is the email address.
00800       email = rawEmail;
00801       fullName = "";
00802     } else {
00803       // We have a start and end to the email address
00804 
00805       // Grab the name part
00806       QString left = rawEmail.left( startPos ).stripWhiteSpace();
00807       // grab the email part
00808       QString right = rawEmail.mid( startPos + 1, endPos - startPos - 1 )
00809                               .stripWhiteSpace();
00810 
00811       // Either "Name <email>" or "email (Name)"
00812       if ( endCh == '>' ) {
00813         fullName = left;
00814         email = right;
00815       } else { // endCh == ')'
00816         fullName = right;
00817         email = left;
00818       }
00819 
00820       // Check that we do not have any extra characters on the end of the
00821       // strings
00822       len = fullName.length();
00823       if ( fullName[ 0 ] == '"' && fullName[ len - 1 ] == '"' )
00824         fullName = fullName.mid( 1, len - 2 );
00825       else if ( fullName[ 0 ] == '(' && fullName[ len - 1 ] == ')' )
00826         fullName = fullName.mid( 1, len - 2 );
00827 
00828       len = email.length();
00829       if ( email[ 0 ] == '<' && email[ len - 1 ] == '>' )
00830         email = email.mid( 1, len - 2 );
00831     }
00832   }
00833 }
00834 
00835 void Addressee::setResource( Resource *resource )
00836 {
00837   detach();
00838   mData->resource = resource;
00839 }
00840 
00841 Resource *Addressee::resource() const
00842 {
00843   return mData->resource;
00844 }
00845 
00846 void Addressee::setChanged( bool value )
00847 {
00848   detach();
00849   mData->changed = value;
00850 }
00851 
00852 bool Addressee::changed() const
00853 {
00854   return mData->changed;
00855 }
00856 
00857 void Addressee::setSortKey( KABC::Field *field )
00858 {
00859   mSortField = field;
00860 }
00861 
00862 bool Addressee::operator< ( const Addressee &addr )
00863 {
00864   if ( !mSortField )
00865     return false;
00866   else
00867     return ( QString::localeAwareCompare( mSortField->value( *this ).lower(),
00868                                           mSortField->value( addr ).lower() ) < 0 );
00869 }
00870 
00871 QDataStream &KABC::operator<<( QDataStream &s, const Addressee &a )
00872 {
00873   if (!a.mData) return s;
00874 
00875   s << a.uid();
00876 
00877   --STREAMOUT--
00878   s << a.mData->phoneNumbers;
00879   s << a.mData->addresses;
00880   s << a.mData->emails;
00881   s << a.mData->categories;
00882   s << a.mData->custom;
00883   s << a.mData->keys;
00884   return s;
00885 }
00886 
00887 QDataStream &KABC::operator>>( QDataStream &s, Addressee &a )
00888 {
00889   if (!a.mData) return s;
00890 
00891   s >> a.mData->uid;
00892 
00893   --STREAMIN--
00894   s >> a.mData->phoneNumbers;
00895   s >> a.mData->addresses;
00896   s >> a.mData->emails;
00897   s >> a.mData->categories;
00898   s >> a.mData->custom;
00899   s >> a.mData->keys;
00900 
00901   a.mData->empty = false;
00902 
00903   return s;
00904 }
00905 
00906 bool matchBinaryPattern( int value, int pattern )
00907 {
00914   if ( pattern == 0 )
00915     return ( value == 0 );
00916   else
00917     return ( pattern == ( pattern & value ) );
00918 }
KDE Logo
This file is part of the documentation for kabc Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Sep 30 05:21:03 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2003