contact.cpp

来自「funambol window mobile客户端源代码」· C++ 代码 · 共 1,263 行 · 第 1/4 页

CPP
1,263
字号
/*
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * Copyright (C) 2003 - 2007 Funambol, Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission 
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License 
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably 
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */


#include "base/fscapi.h"
#include "base/util/utils.h"
#include "vocl/vCard/Contact.h"
#include "base/globalsdef.h"

USE_NAMESPACE

Contact::Contact() {
    name           = NULL;
    notes          = NULL;
    businessDetail = NULL;
    personalDetail = NULL;

    setVersion(TEXT("2.1"));
    setProdID(TEXT("VCARD"));
}

Contact::~Contact() {
    if (name) {
        delete name; name = NULL;
    }
    if (notes) {
        delete notes; notes = NULL;
    }
    if (businessDetail) {
        delete businessDetail; businessDetail = NULL;
    }
    if (personalDetail) {
        delete personalDetail; personalDetail = NULL;
    }
}

/**
 * Returns the UID of this contact
 *
 * @return the uid of this contact or NULL if not specified
 */
WCHAR* Contact::getUID(WCHAR* buf, int size) {

    if(!containsProperty(TEXT("UID")))
        return NULL;

    if (buf == NULL) {
        return (getProperty(TEXT("UID"))->getValue());
    }

    if (size >= 0) {
        wcsncpy(buf, getProperty(TEXT("UID"))->getValue(), size);
    } else {
        wcscpy(buf, getProperty(TEXT("UID"))->getValue());
    }

    return buf;
}

WCHAR* Contact::getTimezone (WCHAR* buf, int size) {

    if(!containsProperty(TEXT("TZ")))
        return NULL;

    if (buf == NULL) {
        return (getProperty(TEXT("TZ"))->getValue());
    }

    if (size >= 0) {
        wcsncpy(buf, getProperty(TEXT("TZ"))->getValue(), size);
    } else {
        wcscpy(buf, getProperty(TEXT("TZ"))->getValue());
    }

    return buf;
}

ArrayList* Contact::getNotes() {
    if(!notes) {
        for(int i = 0; i<propertiesCount();i++) {
            if(!wcscmp(getProperty(i)->getName(), TEXT("NOTE")))
                if(getProperty(i)->getValue()) {
                    vCardProperty *property = getPropertyFromVProperty(getProperty(i));
                    Note* note = new Note();
                    note->setProperty(*property);
                    if(getProperty(i)->containsParameter(TEXT("TYPE")))
                        note->setType(getProperty(i)->getParameterValue(TEXT("TYPE")));
                    if(!notes)
                        notes = new ArrayList();
                    notes->add((ArrayElement&)*note);
                }
        }
    }

    return notes;
}

void Contact::setNotes(ArrayList& list) {
	int i,m;
    if (notes) {
        notes->clear();
    } else {
        notes = new ArrayList();
    }

    Note *note = NULL;
    VProperty *vp;

    for(i = 0, m = propertiesCount();i < m;i++) {
        if(!wcscmp(getProperty(i)->getName(), TEXT("NOTE"))) {
            removeProperty(i);
            --i;
            --m;
        }
    }

    int s = list.size();
    for (i=0; i<s; ++i) {
        notes->add(*list[i]);

        note = (Note*)list[i];
        if(note->getProperty()) {
            vp = getVPropertyFromProperty(TEXT("NOTE"), note->getProperty());
            if(note->getType())
                vp->addParameter(TEXT("TYPE"), note->getType());
            insertProperty(vp);
        }
    }
}

WCHAR* Contact::getRevision (WCHAR* buf, int size) {
    if(!containsProperty(TEXT("REV")))
        return NULL;

    if (buf == NULL) {
        return (getProperty(TEXT("REV"))->getValue());
    }

    if (size >= 0) {
        wcsncpy(buf, getProperty(TEXT("REV"))->getValue(), size);
    } else {
        wcscpy(buf, getProperty(TEXT("REV"))->getValue());
    }

    return buf;
}

Name* Contact::getName () {
    if (!name) {
        if(containsProperty(TEXT("N")) && getProperty(TEXT("N"))->getValue()) {
            VProperty* vp = getProperty(TEXT("N"));

            name = new Name();
            if(vp->getPropComponent(4)) {
                vCardProperty *salutation = getPropertyFromVProperty(vp);
                salutation->setValue(vp->getPropComponent(4));
                name->setSalutation(*salutation);

                delete salutation;
            }
            if(vp->getPropComponent(2)) {
                vCardProperty *firstName = getPropertyFromVProperty(vp);
                firstName->setValue(vp->getPropComponent(2));
                name->setFirstName(*firstName);

                delete firstName;
            }
            if(vp->getPropComponent(3)) {
                vCardProperty *middleName = getPropertyFromVProperty(vp);
                middleName->setValue(vp->getPropComponent(3));
                name->setMiddleName(*middleName);

                delete middleName;
            }
            if(vp->getPropComponent(1)) {
                vCardProperty *lastName = getPropertyFromVProperty(vp);
                lastName->setValue(vp->getPropComponent(1));
                name->setLastName(*lastName);

                delete lastName;
            }
            if(vp->getPropComponent(5)) {
                vCardProperty *suffix = getPropertyFromVProperty(vp);
                suffix->setValue(vp->getPropComponent(5));
                name->setSuffix(*suffix);

                delete suffix;
            }
        }

        if(containsProperty(TEXT("FN")) && getProperty(TEXT("FN"))->getValue()) {
            if (!name)
                name = new Name();

            VProperty* vp = getProperty(TEXT("FN"));
            vCardProperty *displayName = getPropertyFromVProperty(vp);
            name->setDisplayName(*displayName);

            delete displayName;
        }

        if(containsProperty(TEXT("NICKNAME")) && getProperty(TEXT("NICKNAME"))->getValue()) {
            if (!name)
                name = new Name();

            VProperty* vp = getProperty(TEXT("NICKNAME"));
            vCardProperty *nickName = getPropertyFromVProperty(vp);
            name->setNickname(*nickName);

            delete nickName;
        }
    }
    return name;
}

void Contact::setName(Name& n) {
    if (name) delete name;

    name = n.clone();

    if (containsProperty(TEXT("N")))
        removeProperty(TEXT("N"));
    if (containsProperty(TEXT("FN")))
        removeProperty(TEXT("FN"));
    if (containsProperty(TEXT("NICKNAME")))
        removeProperty(TEXT("NICKNAME"));

    if(name) {
        WCHAR *nameProp = new WCHAR[MAX_VPROPERTY_VALUE + 1];
        wcscpy(nameProp,TEXT(""));
        if(name->getLastName())
            wcscat(nameProp,name->getLastName()->getValue());
        wcscat(nameProp,TEXT(";"));

        if(name->getFirstName())
            wcscat(nameProp,name->getFirstName()->getValue());
            wcscat(nameProp,TEXT(";"));

        if(name->getMiddleName())
            wcscat(nameProp,name->getMiddleName()->getValue());
            wcscat(nameProp,TEXT(";"));

        if(name->getSalutation())
            wcscat(nameProp,name->getSalutation()->getValue());
            wcscat(nameProp,TEXT(";"));

        if(name->getSuffix())
            wcscat(nameProp,name->getSuffix()->getValue());

        VProperty* vpName = new VProperty(TEXT("N"), nameProp);
        if(name->getFirstName()) {
            if(name->getFirstName()->getCharset())
                vpName->addParameter(TEXT("CHARSET"), name->getFirstName()->getCharset());
            if(name->getFirstName()->getEncoding())
                vpName->addParameter(TEXT("ENCODING"), name->getFirstName()->getEncoding());
            if(name->getFirstName()->getLanguage())
                vpName->addParameter(TEXT("LANGUAGE"), name->getFirstName()->getLanguage());
        }
        insertProperty(vpName);

        if(name->getDisplayName()) {
            VProperty* vpDisplayName = getVPropertyFromProperty(TEXT("FN"), name->getDisplayName());
            insertProperty(vpDisplayName);
        }
        if(name->getNickname()) {
            VProperty* vpNickName = getVPropertyFromProperty(TEXT("NICKNAME"), name->getNickname());
            insertProperty(vpNickName);
        }
    }
}

BusinessDetail* Contact::getBusinessDetail () {
    if(!businessDetail) {
        WCHAR* titles[MAX_TITLES];
        int titlesIndex = 0;
        ArrayList* phones = NULL;
        ArrayList* emails = NULL;
        ArrayList* webPages = NULL;
        ContactDetail* contactDetail = NULL;
        Address* adr = NULL;

        for(int i = 0; i<propertiesCount();i++) {
            if(!wcscmp(getProperty(i)->getName(), TEXT("ADR")) && getProperty(i)->isType(TEXT("WORK")))
                if(getProperty(i)->getValue()) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?