Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

contact.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 
00021 #define QTOPIA_INTERNAL_CONTACT_MRE
00022 
00023 #include "contact.h"
00024 #include "vobject_p.h"
00025 #include "qfiledirect_p.h"
00026 
00027 #include <qpe/stringutil.h>
00028 #include <qpe/timeconversion.h>
00029 
00030 #include <qobject.h>
00031 #include <qregexp.h>
00032 #include <qstylesheet.h>
00033 #include <qfileinfo.h>
00034 
00035 #include <stdio.h>
00036 
00048 Qtopia::UidGen Contact::sUidGen( Qtopia::UidGen::Qtopia );
00049 
00053 Contact::Contact()
00054     : Record(), mMap(), d( 0 )
00055 {
00056 }
00057 
00063 Contact::Contact( const QMap<int, QString> &fromMap ) :
00064     Record(), mMap( fromMap ), d( 0 )
00065 {
00066     QString cats = mMap[ Qtopia::AddressCategory ];
00067     if ( !cats.isEmpty() )
00068         setCategories( idsFromString( cats ) );
00069     QString uidStr = find( Qtopia::AddressUid );
00070 
00071     if ( uidStr.isEmpty() )
00072         setUid( uidGen().generate() );
00073     else
00074         setUid( uidStr.toInt() );
00075 
00076     if ( !uidStr.isEmpty() )
00077         setUid( uidStr.toInt() );
00078 }
00079 
00083 Contact::~Contact()
00084 {
00085 }
00086 
00437 QMap<int, QString> Contact::toMap() const
00438 {
00439     QMap<int, QString> map = mMap;
00440     QString cats = idsToString( categories() );
00441     if ( !cats.isEmpty() )
00442         map.insert( Qtopia::AddressCategory, cats );
00443     return map;
00444 }
00445 
00449 QString Contact::toRichText() const
00450 {
00451     QString text;
00452     QString value, comp, state;
00453 
00454     // name, jobtitle and company
00455     if ( !(value = fullName()).isEmpty() )
00456         text += "<b>" + Qtopia::escapeString(value) + "</b><br>";
00457     if ( !(value = jobTitle()).isEmpty() )
00458         text += Qtopia::escapeString(value) + "<br>";
00459 
00460     comp = company();
00461     if ( !(value = department()).isEmpty() ) {
00462         text += Qtopia::escapeString(value);
00463         if ( comp )
00464             text += ", ";
00465         else
00466             text += "<br>";
00467     }
00468     if ( !comp.isEmpty() )
00469         text += Qtopia::escapeString(comp) + "<br>";
00470 
00471     // business address
00472     if ( !businessStreet().isEmpty() || !businessCity().isEmpty() ||
00473          !businessZip().isEmpty() || !businessCountry().isEmpty() ) {
00474         text += "<br>";
00475         text += QObject::tr( "<b>Work Address:</b>" );
00476         text +=  "<br>";
00477     }
00478 
00479     if ( !(value = businessStreet()).isEmpty() )
00480         text += Qtopia::escapeString(value) + "<br>";
00481     state =  businessState();
00482     if ( !(value = businessCity()).isEmpty() ) {
00483         text += Qtopia::escapeString(value);
00484         if ( state )
00485             text += ", " + Qtopia::escapeString(state);
00486         text += "<br>";
00487     } else if ( !state.isEmpty() )
00488         text += Qtopia::escapeString(state) + "<br>";
00489     if ( !(value = businessZip()).isEmpty() )
00490         text += Qtopia::escapeString(value) + "<br>";
00491     if ( !(value = businessCountry()).isEmpty() )
00492         text += Qtopia::escapeString(value) + "<br>";
00493 
00494     // home address
00495     if ( !homeStreet().isEmpty() || !homeCity().isEmpty() ||
00496          !homeZip().isEmpty() || !homeCountry().isEmpty() ) {
00497         text += "<br>";
00498         text += QObject::tr( "<b>Home Address:</b>" );
00499         text +=  "<br>";
00500     }
00501 
00502     if ( !(value = homeStreet()).isEmpty() )
00503         text += Qtopia::escapeString(value) + "<br>";
00504     state =  homeState();
00505     if ( !(value = homeCity()).isEmpty() ) {
00506         text += Qtopia::escapeString(value);
00507         if ( !state.isEmpty() )
00508             text += ", " + Qtopia::escapeString(state);
00509         text += "<br>";
00510     } else if (!state.isEmpty())
00511         text += Qtopia::escapeString(state) + "<br>";
00512     if ( !(value = homeZip()).isEmpty() )
00513         text += Qtopia::escapeString(value) + "<br>";
00514     if ( !(value = homeCountry()).isEmpty() )
00515         text += Qtopia::escapeString(value) + "<br>";
00516 
00517     // the others...
00518     QString str;
00519     str = emails();
00520     if ( !str.isEmpty() )
00521         text += "<b>" + QObject::tr("Email Addresses: ") + "</b>"
00522                 + Qtopia::escapeString(str) + "<br>";
00523     str = homePhone();
00524     if ( !str.isEmpty() )
00525         text += "<b>" + QObject::tr("Home Phone: ") + "</b>"
00526                 + Qtopia::escapeString(str) + "<br>";
00527     str = homeFax();
00528     if ( !str.isEmpty() )
00529         text += "<b>" + QObject::tr("Home Fax: ") + "</b>"
00530                 + Qtopia::escapeString(str) + "<br>";
00531     str = homeMobile();
00532     if ( !str.isEmpty() )
00533         text += "<b>" + QObject::tr("Home Mobile: ") + "</b>"
00534                 + Qtopia::escapeString(str) + "<br>";
00535     str = homeWebpage();
00536     if ( !str.isEmpty() )
00537         text += "<b>" + QObject::tr("Home Web Page: ") + "</b>"
00538                 + Qtopia::escapeString(str) + "<br>";
00539     str = businessWebpage();
00540     if ( !str.isEmpty() )
00541         text += "<b>" + QObject::tr("Business Web Page: ") + "</b>"
00542                 + Qtopia::escapeString(str) + "<br>";
00543     str = office();
00544     if ( !str.isEmpty() )
00545         text += "<b>" + QObject::tr("Office: ") + "</b>"
00546                 + Qtopia::escapeString(str) + "<br>";
00547     str = businessPhone();
00548     if ( !str.isEmpty() )
00549         text += "<b>" + QObject::tr("Business Phone: ") + "</b>"
00550                 + Qtopia::escapeString(str) + "<br>";
00551     str = businessFax();
00552     if ( !str.isEmpty() )
00553         text += "<b>" + QObject::tr("Business Fax: ") + "</b>"
00554                 + Qtopia::escapeString(str) + "<br>";
00555     str = businessMobile();
00556     if ( !str.isEmpty() )
00557         text += "<b>" + QObject::tr("Business Mobile: ") + "</b>"
00558                 + Qtopia::escapeString(str) + "<br>";
00559     str = businessPager();
00560     if ( !str.isEmpty() )
00561         text += "<b>" + QObject::tr("Business Pager: ") + "</b>"
00562                 + Qtopia::escapeString(str) + "<br>";
00563     str = profession();
00564     if ( !str.isEmpty() )
00565         text += "<b>" + QObject::tr("Profession: ") + "</b>"
00566                 + Qtopia::escapeString(str) + "<br>";
00567     str = assistant();
00568     if ( !str.isEmpty() )
00569         text += "<b>" + QObject::tr("Assistant: ") + "</b>"
00570                 + Qtopia::escapeString(str) + "<br>";
00571     str = manager();
00572     if ( !str.isEmpty() )
00573         text += "<b>" + QObject::tr("Manager: ") + "</b>"
00574                 + Qtopia::escapeString(str) + "<br>";
00575     str = gender();
00576     if ( !str.isEmpty() && str.toInt() != 0 ) {
00577         if ( str.toInt() == 1 )
00578             str = QObject::tr( "Male" );
00579         else if ( str.toInt() == 2 )
00580             str = QObject::tr( "Female" );
00581         text += "<b>" + QObject::tr("Gender: ") + "</b>" + str + "<br>";
00582     }
00583     str = spouse();
00584     if ( !str.isEmpty() )
00585         text += "<b>" + QObject::tr("Spouse: ") + "</b>"
00586                 + Qtopia::escapeString(str) + "<br>";
00587     str = birthday();
00588     if ( !str.isEmpty() )
00589         text += "<b>" + QObject::tr("Birthday: ") + "</b>"
00590                 + Qtopia::escapeString(str) + "<br>";
00591     str = anniversary();
00592     if ( !str.isEmpty() )
00593         text += "<b>" + QObject::tr("Anniversary: ") + "</b>"
00594                 + Qtopia::escapeString(str) + "<br>";
00595     str = nickname();
00596     if ( !str.isEmpty() )
00597         text += "<b>" + QObject::tr("Nickname: ") + "</b>"
00598                 + Qtopia::escapeString(str) + "<br>";
00599 
00600     // notes last
00601     if ( (value = notes()) ) {
00602         QRegExp reg("\n");
00603 
00604         //QString tmp = Qtopia::escapeString(value);
00605         QString tmp = QStyleSheet::convertFromPlainText(value);
00606         //tmp.replace( reg, "<br>" );
00607         text += "<br>" + tmp + "<br>";
00608     }
00609     return text;
00610 }
00611 
00615 void Contact::insert( int key, const QString &v )
00616 {
00617     QString value = v.stripWhiteSpace();
00618     if ( value.isEmpty() )
00619         mMap.remove( key );
00620     else
00621         mMap.insert( key, value );
00622 }
00623 
00627 void Contact::replace( int key, const QString & v )
00628 {
00629     QString value = v.stripWhiteSpace();
00630     if ( value.isEmpty() )
00631         mMap.remove( key );
00632     else
00633         mMap.replace( key, value );
00634 }
00635 
00639 QString Contact::find( int key ) const
00640 {
00641     return mMap[key];
00642 }
00643 
00647 QString Contact::displayAddress( const QString &street,
00648                                  const QString &city,
00649                                  const QString &state,
00650                                  const QString &zip,
00651                                  const QString &country ) const
00652 {
00653     QString s = street;
00654     if ( !street.isEmpty() )
00655         s+= "\n";
00656     s += city;
00657     if ( !city.isEmpty() && !state.isEmpty() )
00658         s += ", ";
00659     s += state;
00660     if ( !state.isEmpty() && !zip.isEmpty() )
00661         s += "  ";
00662     s += zip;
00663     if ( !country.isEmpty() && !s.isEmpty() )
00664         s += "\n";
00665     s += country;
00666     return s;
00667 }
00668 
00672 QString Contact::displayBusinessAddress() const
00673 {
00674     return displayAddress( businessStreet(), businessCity(),
00675                            businessState(), businessZip(),
00676                            businessCountry() );
00677 }
00678 
00682 QString Contact::displayHomeAddress() const
00683 {
00684     return displayAddress( homeStreet(), homeCity(),
00685                            homeState(), homeZip(),
00686                            homeCountry() );
00687 }
00688 
00692 QString Contact::fullName() const
00693 {
00694     QString title = find( Qtopia::Title );
00695     QString firstName = find( Qtopia::FirstName );
00696     QString middleName = find( Qtopia::MiddleName );
00697     QString lastName = find( Qtopia::LastName );
00698     QString suffix = find( Qtopia::Suffix );
00699 
00700     QString name = title;
00701     if ( !firstName.isEmpty() ) {
00702         if ( !name.isEmpty() )
00703             name += " ";
00704         name += firstName;
00705     }
00706     if ( !middleName.isEmpty() ) {
00707         if ( !name.isEmpty() )
00708             name += " ";
00709         name += middleName;
00710     }
00711     if ( !lastName.isEmpty() ) {
00712         if ( !name.isEmpty() )
00713             name += " ";
00714         name += lastName;
00715     }
00716     if ( !suffix.isEmpty() ) {
00717         if ( !name.isEmpty() )
00718             name += " ";
00719         name += suffix;
00720     }
00721     return name.simplifyWhiteSpace();
00722 }
00723 
00727 QStringList Contact::childrenList() const
00728 {
00729     return QStringList::split( " ", find( Qtopia::Children ) );
00730 }
00731 
00759 QStringList Contact::emailList() const
00760 {
00761     QString emailStr = emails();
00762 
00763     QStringList r;
00764     if ( !emailStr.isEmpty() ) {
00765         qDebug(" emailstr ");
00766         QStringList l = QStringList::split( emailSeparator(), emailStr );
00767         for ( QStringList::ConstIterator it = l.begin();it != l.end();++it )
00768             r += (*it).simplifyWhiteSpace();
00769     }
00770 
00771     return r;
00772 }
00773 
00780 void Contact::setFileAs()
00781 {
00782     QString lastName, firstName, middleName, fileas;
00783 
00784     lastName = find( Qtopia::LastName );
00785     firstName = find( Qtopia::FirstName );
00786     middleName = find( Qtopia::MiddleName );
00787     if ( !lastName.isEmpty() && !firstName.isEmpty()
00788          && !middleName.isEmpty() )
00789         fileas = lastName + ", " + firstName + " " + middleName;
00790     else if ( !lastName.isEmpty() && !firstName.isEmpty() )
00791         fileas = lastName + ", " + firstName;
00792     else if ( !lastName.isEmpty() || !firstName.isEmpty() ||
00793               !middleName.isEmpty() )
00794         fileas = firstName + ( firstName.isEmpty() ? "" : " " )
00795                  + middleName + ( middleName.isEmpty() ? "" : " " )
00796                  + lastName;
00797 
00798     replace( Qtopia::FileAs, fileas );
00799 }
00800 
00805 void Contact::save( QString &buf ) const
00806 {
00807     static const QStringList SLFIELDS = fields();
00808     // I'm expecting "<Contact " in front of this...
00809     for ( QMap<int, QString>::ConstIterator it = mMap.begin();
00810           it != mMap.end(); ++it ) {
00811         const QString &value = it.data();
00812         int key = it.key();
00813         if ( !value.isEmpty() ) {
00814             if ( key == Qtopia::AddressCategory || key == Qtopia::AddressUid)
00815                 continue;
00816 
00817             key -= Qtopia::AddressCategory+1;
00818             buf += SLFIELDS[key];
00819             buf += "=\"" + Qtopia::escapeString(value) + "\" ";
00820         }
00821     }
00822     buf += customToXml();
00823     if ( categories().count() > 0 )
00824         buf += "Categories=\"" + idsToString( categories() ) + "\" ";
00825     buf += "Uid=\"" + QString::number( uid() ) + "\" ";
00826     // You need to close this yourself
00827 }
00828 
00833 QStringList Contact::fields()
00834 {
00835     QStringList list;
00836 
00837     list.append( "Title" );  // Not Used!
00838     list.append( "FirstName" );
00839     list.append( "MiddleName" );
00840     list.append( "LastName" );
00841     list.append( "Suffix" );
00842     list.append( "FileAs" );
00843 
00844     list.append( "JobTitle" );
00845     list.append( "Department" );
00846     list.append( "Company" );
00847     list.append( "BusinessPhone" );
00848     list.append( "BusinessFax" );
00849     list.append( "BusinessMobile" );
00850 
00851     list.append( "DefaultEmail" );
00852     list.append( "Emails" );
00853 
00854     list.append( "HomePhone" );
00855     list.append( "HomeFax" );
00856     list.append( "HomeMobile" );
00857 
00858     list.append( "BusinessStreet" );
00859     list.append( "BusinessCity" );
00860     list.append( "BusinessState" );
00861     list.append( "BusinessZip" );
00862     list.append( "BusinessCountry" );
00863     list.append( "BusinessPager" );
00864     list.append( "BusinessWebPage" );
00865 
00866     list.append( "Office" );
00867     list.append( "Profession" );
00868     list.append( "Assistant" );
00869     list.append( "Manager" );
00870 
00871     list.append( "HomeStreet" );
00872     list.append( "HomeCity" );
00873     list.append( "HomeState" );
00874     list.append( "HomeZip" );
00875     list.append( "HomeCountry" );
00876     list.append( "HomeWebPage" );
00877 
00878     list.append( "Spouse" );
00879     list.append( "Gender" );
00880     list.append( "Birthday" );
00881     list.append( "Anniversary" );
00882     list.append( "Nickname" );
00883     list.append( "Children" );
00884 
00885     list.append( "Notes" );
00886     list.append( "Groups" );
00887 
00888     return list;
00889 }
00890 
00895 QStringList Contact::trfields()
00896 {
00897     QStringList list;
00898 
00899     list.append( QObject::tr( "Name Title") );
00900     list.append( QObject::tr( "First Name" ) );
00901     list.append( QObject::tr( "Middle Name" ) );
00902     list.append( QObject::tr( "Last Name" ) );
00903     list.append( QObject::tr( "Suffix" ) );
00904     list.append( QObject::tr( "File As" ) );
00905 
00906     list.append( QObject::tr( "Job Title" ) );
00907     list.append( QObject::tr( "Department" ) );
00908     list.append( QObject::tr( "Company" ) );
00909     list.append( QObject::tr( "Business Phone" ) );
00910     list.append( QObject::tr( "Business Fax" ) );
00911     list.append( QObject::tr( "Business Mobile" ) );
00912 
00913     list.append( QObject::tr( "Default Email" ) );
00914     list.append( QObject::tr( "Emails" ) );
00915 
00916     list.append( QObject::tr( "Home Phone" ) );
00917     list.append( QObject::tr( "Home Fax" ) );
00918     list.append( QObject::tr( "Home Mobile" ) );
00919 
00920     list.append( QObject::tr( "Business Street" ) );
00921     list.append( QObject::tr( "Business City" ) );
00922     list.append( QObject::tr( "Business State" ) );
00923     list.append( QObject::tr( "Business Zip" ) );
00924     list.append( QObject::tr( "Business Country" ) );
00925     list.append( QObject::tr( "Business Pager" ) );
00926     list.append( QObject::tr( "Business WebPage" ) );
00927 
00928     list.append( QObject::tr( "Office" ) );
00929     list.append( QObject::tr( "Profession" ) );
00930     list.append( QObject::tr( "Assistant" ) );
00931     list.append( QObject::tr( "Manager" ) );
00932 
00933     list.append( QObject::tr( "Home Street" ) );
00934     list.append( QObject::tr( "Home City" ) );
00935     list.append( QObject::tr( "Home State" ) );
00936     list.append( QObject::tr( "Home Zip" ) );
00937     list.append( QObject::tr( "Home Country" ) );
00938     list.append( QObject::tr( "Home Web Page" ) );
00939 
00940     list.append( QObject::tr( "Spouse" ) );
00941     list.append( QObject::tr( "Gender" ) );
00942     list.append( QObject::tr( "Birthday" ) );
00943     list.append( QObject::tr( "Anniversary" ) );
00944     list.append( QObject::tr( "Nickname" ) );
00945     list.append( QObject::tr( "Children" ) );
00946 
00947     list.append( QObject::tr( "Notes" ) );
00948     list.append( QObject::tr( "Groups" ) );
00949 
00950     return list;
00951 }
00952 
00957 void Contact::setEmails( const QString &str )
00958 {
00959     replace( Qtopia::Emails, str );
00960     if ( str.isEmpty() )
00961         setDefaultEmail( QString::null );
00962 }
00963 
00967 void Contact::setChildren( const QString &str )
00968 {
00969     replace( Qtopia::Children, str );
00970 }
00971 
00972 // vcard conversion code
00976 static inline VObject *safeAddPropValue( VObject *o, const char *prop, const QString &value )
00977 {
00978     VObject *ret = 0;
00979     if ( o && !value.isEmpty() )
00980         ret = addPropValue( o, prop, value.latin1() );
00981     return ret;
00982 }
00983 
00987 static inline VObject *safeAddProp( VObject *o, const char *prop)
00988 {
00989     VObject *ret = 0;
00990     if ( o )
00991         ret = addProp( o, prop );
00992     return ret;
00993 }
00994 
00998 static VObject *createVObject( const Contact &c )
00999 {
01000     VObject *vcard = newVObject( VCCardProp );
01001     safeAddPropValue( vcard, VCVersionProp, "2.1" );
01002     safeAddPropValue( vcard, VCLastRevisedProp, TimeConversion::toISO8601( QDateTime::currentDateTime() ) );
01003     safeAddPropValue( vcard, VCUniqueStringProp, QString::number(c.uid()) );
01004 
01005     // full name
01006     safeAddPropValue( vcard, VCFullNameProp, c.fullName() );
01007 
01008     // name properties
01009     VObject *name = safeAddProp( vcard, VCNameProp );
01010     safeAddPropValue( name, VCFamilyNameProp, c.lastName() );
01011     safeAddPropValue( name, VCGivenNameProp, c.firstName() );
01012     safeAddPropValue( name, VCAdditionalNamesProp, c.middleName() );
01013     safeAddPropValue( name, VCNamePrefixesProp, c.title() );
01014     safeAddPropValue( name, VCNameSuffixesProp, c.suffix() );
01015 
01016     // home properties
01017     VObject *home_adr= safeAddProp( vcard, VCAdrProp );
01018     safeAddProp( home_adr, VCHomeProp );
01019     safeAddPropValue( home_adr, VCStreetAddressProp, c.homeStreet() );
01020     safeAddPropValue( home_adr, VCCityProp, c.homeCity() );
01021     safeAddPropValue( home_adr, VCRegionProp, c.homeState() );
01022     safeAddPropValue( home_adr, VCPostalCodeProp, c.homeZip() );
01023     safeAddPropValue( home_adr, VCCountryNameProp, c.homeCountry() );
01024 
01025     VObject *home_phone = safeAddPropValue( vcard, VCTelephoneProp, c.homePhone() );
01026     safeAddProp( home_phone, VCHomeProp );
01027     home_phone = safeAddPropValue( vcard, VCTelephoneProp, c.homeMobile() );
01028     safeAddProp( home_phone, VCHomeProp );
01029     safeAddProp( home_phone, VCCellularProp );
01030     home_phone = safeAddPropValue( vcard, VCTelephoneProp, c.homeFax() );
01031     safeAddProp( home_phone, VCHomeProp );
01032     safeAddProp( home_phone, VCFaxProp );
01033 
01034     VObject *url = safeAddPropValue( vcard, VCURLProp, c.homeWebpage() );
01035     safeAddProp( url, VCHomeProp );
01036 
01037     // work properties
01038     VObject *work_adr= safeAddProp( vcard, VCAdrProp );
01039     safeAddProp( work_adr, VCWorkProp );
01040     safeAddPropValue( work_adr, VCStreetAddressProp, c.businessStreet() );
01041     safeAddPropValue( work_adr, VCCityProp, c.businessCity() );
01042     safeAddPropValue( work_adr, VCRegionProp, c.businessState() );
01043     safeAddPropValue( work_adr, VCPostalCodeProp, c.businessZip() );
01044     safeAddPropValue( work_adr, VCCountryNameProp, c.businessCountry() );
01045 
01046     VObject *work_phone = safeAddPropValue( vcard, VCTelephoneProp, c.businessPhone() );
01047     safeAddProp( work_phone, VCWorkProp );
01048     work_phone = safeAddPropValue( vcard, VCTelephoneProp, c.businessMobile() );
01049     safeAddProp( work_phone, VCWorkProp );
01050     safeAddProp( work_phone, VCCellularProp );
01051     work_phone = safeAddPropValue( vcard, VCTelephoneProp, c.businessFax() );
01052     safeAddProp( work_phone, VCWorkProp );
01053     safeAddProp( work_phone, VCFaxProp );
01054     work_phone = safeAddPropValue( vcard, VCTelephoneProp, c.businessPager() );
01055     safeAddProp( work_phone, VCWorkProp );
01056     safeAddProp( work_phone, VCPagerProp );
01057 
01058     url = safeAddPropValue( vcard, VCURLProp, c.businessWebpage() );
01059     safeAddProp( url, VCWorkProp );
01060 
01061     VObject *title = safeAddPropValue( vcard, VCTitleProp, c.jobTitle() );
01062     safeAddProp( title, VCWorkProp );
01063 
01064 
01065     QStringList emails = c.emailList();
01066     emails.prepend( c.defaultEmail() );
01067     for( QStringList::Iterator it = emails.begin(); it != emails.end(); ++it ) {
01068         VObject *email = safeAddPropValue( vcard, VCEmailAddressProp, *it );
01069         safeAddProp( email, VCInternetProp );
01070     }
01071 
01072     safeAddPropValue( vcard, VCNoteProp, c.notes() );
01073 
01074     safeAddPropValue( vcard, VCBirthDateProp, c.birthday() );
01075 
01076     if ( !c.company().isEmpty() || !c.department().isEmpty() || !c.office().isEmpty() ) {
01077         VObject *org = safeAddProp( vcard, VCOrgProp );
01078         safeAddPropValue( org, VCOrgNameProp, c.company() );
01079         safeAddPropValue( org, VCOrgUnitProp, c.department() );
01080         safeAddPropValue( org, VCOrgUnit2Prop, c.office() );
01081     }
01082 
01083     // some values we have to export as custom fields
01084     safeAddPropValue( vcard, "X-Qtopia-Profession", c.profession() );
01085     safeAddPropValue( vcard, "X-Qtopia-Manager", c.manager() );
01086     safeAddPropValue( vcard, "X-Qtopia-Assistant", c.assistant() );
01087 
01088     safeAddPropValue( vcard, "X-Qtopia-Spouse", c.spouse() );
01089     safeAddPropValue( vcard, "X-Qtopia-Gender", c.gender() );
01090     safeAddPropValue( vcard, "X-Qtopia-Anniversary", c.anniversary() );
01091     safeAddPropValue( vcard, "X-Qtopia-Nickname", c.nickname() );
01092     safeAddPropValue( vcard, "X-Qtopia-Children", c.children() );
01093 
01094     return vcard;
01095 }
01096 
01097 
01101 static Contact parseVObject( VObject *obj )
01102 {
01103     Contact c;
01104 
01105     VObjectIterator it;
01106     initPropIterator( &it, obj );
01107     while( moreIteration( &it ) ) {
01108         VObject *o = nextVObject( &it );
01109         QCString name = vObjectName( o );
01110         QCString value = vObjectStringZValue( o );
01111         if ( name == VCNameProp ) {
01112             VObjectIterator nit;
01113             initPropIterator( &nit, o );
01114             while( moreIteration( &nit ) ) {
01115                 VObject *o = nextVObject( &nit );
01116                 QCString name = vObjectTypeInfo( o );
01117                 QString value = vObjectStringZValue( o );
01118                 if ( name == VCNamePrefixesProp )
01119                     c.setTitle( value );
01120                 else if ( name == VCNameSuffixesProp )
01121                     c.setSuffix( value );
01122                 else if ( name == VCFamilyNameProp )
01123                     c.setLastName( value );
01124                 else if ( name == VCGivenNameProp )
01125                     c.setFirstName( value );
01126                 else if ( name == VCAdditionalNamesProp )
01127                     c.setMiddleName( value );
01128             }
01129         }
01130         else if ( name == VCAdrProp ) {
01131             bool work = TRUE; // default address is work address
01132             QString street;
01133             QString city;
01134             QString region;
01135             QString postal;
01136             QString country;
01137 
01138             VObjectIterator nit;
01139             initPropIterator( &nit, o );
01140             while( moreIteration( &nit ) ) {
01141                 VObject *o = nextVObject( &nit );
01142                 QCString name = vObjectName( o );
01143                 QString value = vObjectStringZValue( o );
01144                 if ( name == VCHomeProp )
01145                     work = FALSE;
01146                 else if ( name == VCWorkProp )
01147                     work = TRUE;
01148                 else if ( name == VCStreetAddressProp )
01149                     street = value;
01150                 else if ( name == VCCityProp )
01151                     city = value;
01152                 else if ( name == VCRegionProp )
01153                     region = value;
01154                 else if ( name == VCPostalCodeProp )
01155                     postal = value;
01156                 else if ( name == VCCountryNameProp )
01157                     country = value;
01158             }
01159             if ( work ) {
01160                 c.setBusinessStreet( street );
01161                 c.setBusinessCity( city );
01162                 c.setBusinessCountry( country );
01163                 c.setBusinessZip( postal );
01164                 c.setBusinessState( region );
01165             } else {
01166                 c.setHomeStreet( street );
01167                 c.setHomeCity( city );
01168                 c.setHomeCountry( country );
01169                 c.setHomeZip( postal );
01170                 c.setHomeState( region );
01171             }
01172         }
01173         else if ( name == VCTelephoneProp ) {
01174             enum {
01175                 HOME = 0x01,
01176                 WORK = 0x02,
01177                 VOICE = 0x04,
01178                 CELL = 0x08,
01179                 FAX = 0x10,
01180                 PAGER = 0x20,
01181                 UNKNOWN = 0x80
01182             };
01183             int type = 0;
01184 
01185             VObjectIterator nit;
01186             initPropIterator( &nit, o );
01187             while( moreIteration( &nit ) ) {
01188                 VObject *o = nextVObject( &nit );
01189                 QCString name = vObjectTypeInfo( o );
01190                 if ( name == VCHomeProp )
01191                     type |= HOME;
01192                 else if ( name == VCWorkProp )
01193                     type |= WORK;
01194                 else if ( name == VCVoiceProp )
01195                     type |= VOICE;
01196                 else if ( name == VCCellularProp )
01197                     type |= CELL;
01198                 else if ( name == VCFaxProp )
01199                     type |= FAX;
01200                 else if ( name == VCPagerProp )
01201                     type |= PAGER;
01202                 else  if ( name == VCPreferredProp )
01203                     ;
01204                 else
01205                     type |= UNKNOWN;
01206             }
01207             if ( (type & UNKNOWN) != UNKNOWN ) {
01208                 if ( ( type & (HOME|WORK) ) == 0 ) // default
01209                     type |= HOME;
01210                 if ( ( type & (VOICE|CELL|FAX|PAGER) ) == 0 ) // default
01211                     type |= VOICE;
01212 
01213                 if ( (type & (VOICE|HOME) ) == (VOICE|HOME) )
01214                     c.setHomePhone( value );
01215                 if ( ( type & (FAX|HOME) ) == (FAX|HOME) )
01216                     c.setHomeFax( value );
01217                 if ( ( type & (CELL|HOME) ) == (CELL|HOME) )
01218                     c.setHomeMobile( value );
01219                 if ( ( type & (VOICE|WORK) ) == (VOICE|WORK) )
01220                     c.setBusinessPhone( value );
01221                 if ( ( type & (FAX|WORK) ) == (FAX|WORK) )
01222                     c.setBusinessFax( value );
01223                 if ( ( type & (CELL|WORK) ) == (CELL|WORK) )
01224                     c.setBusinessMobile( value );
01225                 if ( ( type & (PAGER|WORK) ) == (PAGER|WORK) )
01226                     c.setBusinessPager( value );
01227             }
01228         }
01229         else if ( name == VCEmailAddressProp ) {
01230             QString email = vObjectStringZValue( o );
01231             bool valid = TRUE;
01232             VObjectIterator nit;
01233             initPropIterator( &nit, o );
01234             while( moreIteration( &nit ) ) {
01235                 VObject *o = nextVObject( &nit );
01236                 QCString name = vObjectTypeInfo( o );
01237                 if ( name != VCInternetProp && name != VCHomeProp &&
01238                      name != VCWorkProp &&
01239                      name != VCPreferredProp )
01240                     // ### preffered should map to default email
01241                     valid = FALSE;
01242             }
01243             if ( valid ) {
01244                 c.insertEmail( email );
01245             }
01246         }
01247         else if ( name == VCURLProp ) {
01248             VObjectIterator nit;
01249             initPropIterator( &nit, o );
01250             while( moreIteration( &nit ) ) {
01251                 VObject *o = nextVObject( &nit );
01252                 QCString name = vObjectTypeInfo( o );
01253                 if ( name == VCHomeProp )
01254                     c.setHomeWebpage( value );
01255                 else if ( name == VCWorkProp )
01256                     c.setBusinessWebpage( value );
01257             }
01258         }
01259         else if ( name == VCOrgProp ) {
01260             VObjectIterator nit;
01261             initPropIterator( &nit, o );
01262             while( moreIteration( &nit ) ) {
01263                 VObject *o = nextVObject( &nit );
01264                 QCString name = vObjectName( o );
01265                 QString value = vObjectStringZValue( o );
01266                 if ( name == VCOrgNameProp )
01267                     c.setCompany( value );
01268                 else if ( name == VCOrgUnitProp )
01269                     c.setDepartment( value );
01270                 else if ( name == VCOrgUnit2Prop )
01271                     c.setOffice( value );
01272             }
01273         }
01274         else if ( name == VCTitleProp ) {
01275             c.setJobTitle( value );
01276         }
01277         else if ( name == "X-Qtopia-Profession" ) {
01278             c.setProfession( value );
01279         }
01280         else if ( name == "X-Qtopia-Manager" ) {
01281             c.setManager( value );
01282         }
01283         else if ( name == "X-Qtopia-Assistant" ) {
01284             c.setAssistant( value );
01285         }
01286         else if ( name == "X-Qtopia-Spouse" ) {
01287             c.setSpouse( value );
01288         }
01289         else if ( name == "X-Qtopia-Gender" ) {
01290             c.setGender( value );
01291         }
01292         else if ( name == "X-Qtopia-Anniversary" ) {
01293             c.setAnniversary( value );
01294         }
01295         else if ( name == "X-Qtopia-Nickname" ) {
01296             c.setNickname( value );
01297         }
01298         else if ( name == "X-Qtopia-Children" ) {
01299             c.setChildren( value );
01300         }
01301 
01302 
01303 #if 0
01304         else {
01305             printf("Name: %s, value=%s\n", name.data(), vObjectStringZValue( o ) );
01306             VObjectIterator nit;
01307             initPropIterator( &nit, o );
01308             while( moreIteration( &nit ) ) {
01309                 VObject *o = nextVObject( &nit );
01310                 QCString name = vObjectName( o );
01311                 QString value = vObjectStringZValue( o );
01312                 printf(" subprop: %s = %s\n", name.data(), value.latin1() );
01313             }
01314         }
01315 #endif
01316     }
01317     c.setFileAs();
01318     return c;
01319 }
01320 
01324 void Contact::writeVCard( const QString &filename, const QValueList<Contact> &contacts)
01325 {
01326         QFileDirect f( filename.utf8().data() );
01327         if ( !f.open( IO_WriteOnly ) ) {
01328                 qWarning("Unable to open vcard write");
01329                 return;
01330         }
01331 
01332     QValueList<Contact>::ConstIterator it;
01333     for( it = contacts.begin(); it != contacts.end(); ++it ) {
01334         VObject *obj = createVObject( *it );
01335         writeVObject(f.directHandle() , obj );
01336         cleanVObject( obj );
01337     }
01338     cleanStrTbl();
01339 }
01340 
01344 void Contact::writeVCard( const QString &filename, const Contact &contact)
01345 {
01346         QFileDirect f( filename.utf8().data() );
01347         if ( !f.open( IO_WriteOnly ) ) {
01348                 qWarning("Unable to open vcard write");
01349                 return;
01350         }
01351 
01352     VObject *obj = createVObject( contact );
01353         writeVObject( f.directHandle() , obj );
01354         cleanVObject( obj );
01355 
01356         cleanStrTbl();
01357 }
01358 
01362 QValueList<Contact> Contact::readVCard( const QString &filename )
01363 {
01364     qDebug("trying to open %s, exists=%d", filename.utf8().data(), QFileInfo( filename.utf8().data() ).size() );
01365     VObject *obj = Parse_MIME_FromFileName( (char *)filename.utf8().data() );
01366 
01367     qDebug("vobject = %p", obj );
01368 
01369     QValueList<Contact> contacts;
01370 
01371     while ( obj ) {
01372         contacts.append( parseVObject( obj ) );
01373 
01374         VObject *t = obj;
01375         obj = nextVObjectInList(obj);
01376         cleanVObject( t );
01377     }
01378 
01379     return contacts;
01380 }
01381 
01386 bool Contact::match( const QString &regexp ) const
01387 {
01388     return match(QRegExp(regexp));
01389 }
01390 
01396 bool Contact::match( const QRegExp &r ) const
01397 {
01398     bool match;
01399     match = false;
01400     QMap<int, QString>::ConstIterator it;
01401     for ( it = mMap.begin(); it != mMap.end(); ++it ) {
01402         if ( (*it).find( r ) > -1 ) {
01403             match = true;
01404             break;
01405         }
01406     }
01407     return match;
01408 }

Generated on Sat Nov 5 16:16:32 2005 for OPIE by  doxygen 1.4.2