vcardconverter.cpp

来自「funambol window mobile客户端源代码」· C++ 代码 · 共 201 行

CPP
201
字号
/*
 * 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 "vocl/vCard/vCardConverter.h"
#include "base/util/WString.h"
#include "base/globalsdef.h"

USE_NAMESPACE

vCardConverter::vCardConverter() {
    vCard = NULL;
    contact = NULL;
}

vCardConverter::~vCardConverter() {
    if (vCard) {
        delete [] vCard; vCard = NULL;
    }
    if (contact) {
        delete contact; contact = NULL;
    }
}

void vCardConverter::setSource(WCHAR* inputCard) {
    if(vCard) {
        delete[] vCard;
        vCard = NULL;
    }
    if(inputCard) {
        vCard = new WCHAR[wcslen(inputCard) + 1];
        wcscpy(vCard, inputCard);
    }
    if(contact) {
        delete contact; contact = NULL;
    }
}

void vCardConverter::setSource(Contact& inputContact) {
    if(contact)
        delete contact;

    contact = inputContact.clone();

    if(vCard) {
        delete [] vCard; vCard = NULL;
    }
}

void vCardConverter::getvCard(WCHAR* outputCard) {
    if(vCard) {
        if(!outputCard)
            return;
        wcscpy(outputCard, vCard);
    }
    else
        outputCard = NULL;
}

void vCardConverter::getContact(Contact** outputContact) {
    if(contact) {
        *outputContact = contact->clone();
    }
    else
        *outputContact = NULL;
}

bool vCardConverter::convert(WString& errorDescription, long* errorCode) {
    errorDescription = TEXT("");
    *errorCode = ERROR_SUCCESS;

    if(!contact && !vCard)
        return false;
    if(contact && vCard)
        return true;
    if(contact) {
        vCard = contact->toString();
        if (!validate((VObject*) contact, errorDescription, errorCode)) {
            delete [] vCard; vCard = NULL;
        }
    }
    if (vCard) {
        WCHAR* vCardCopy = new WCHAR[wcslen(vCard) + 1];
        vCardCopy =  vCard;
        Contact* invalidatedContact = (Contact*)VConverter::parse(vCardCopy);
        if (invalidatedContact && validate((VObject*)invalidatedContact, errorDescription, errorCode))
            contact = invalidatedContact->clone();
        else
            return false;
        delete invalidatedContact; invalidatedContact = NULL;
    }

   return true;
}

bool vCardConverter::validate(VObject* vObject, WString& errorDescription, long* errorCode) {

    //validate begin, end, N and FN properties
    if(wcscmp(vObject->getProperty(0)->getName(), TEXT("BEGIN")) ||
        wcscmp(vObject->getProperty(0)->getValue(), TEXT("VCARD"))) {
        *errorCode = ERROR_KEY_PROPERTY_MISSING;
        errorDescription =  TEXT("'BEGIN' property is missing");
        return false;
        }
    if(wcscmp(vObject->getProperty(vObject->propertiesCount()-1)->getName(), TEXT("END")) ||
        wcscmp(vObject->getProperty(vObject->propertiesCount()-1)->getValue(), TEXT("VCARD"))) {
        *errorCode = ERROR_KEY_PROPERTY_MISSING;
        errorDescription =  TEXT("'END' property is missing");
        return false;
        }
    if(!vObject->containsProperty(TEXT("N"))) {
        *errorCode = ERROR_KEY_PROPERTY_MISSING;
        errorDescription =  TEXT("'N' property is missing");
        return false;
    }
    if(!vObject->containsProperty(TEXT("FN"))) {
        *errorCode = ERROR_KEY_PROPERTY_MISSING;
        errorDescription =  TEXT("'FN' property is missing");
        return false;
    }

    //validate the version and then each property according with version
    if(vObject->containsProperty(TEXT("VERSION"))) {
        if(!vObject->getProperty(TEXT("VERSION"))->getValue()) {
            *errorCode = ERROR_ILLEGAL_VERSION_NUMBER;
            errorDescription =  TEXT("Version number is missing");
            return false;
        }
        if(!wcscmp(vObject->getProperty(TEXT("VERSION"))->getValue(), TEXT("2.1")))
            for(int i = 0; i < vObject->propertiesCount(); i++) {
                if(!validateProperty21(vObject->getProperty(i), errorDescription, errorCode))
                    return false;
            }
        else if(!wcscmp(vObject->getProperty(TEXT("VERSION"))->getValue(), TEXT("3.0"))) {
            for(int i = 0; i < vObject->propertiesCount(); i++) {
                if(!validateProperty30(vObject->getProperty(i), errorDescription, errorCode))
                    return false;
            }
        }
        //VERSION property is neither 2.1 nor 3.0
        else {
            *errorCode = ERROR_ILLEGAL_VERSION_NUMBER;
            errorDescription = TEXT("Illegal version number : %s");
            errorDescription += vObject->getProperty(TEXT("VERSION"))->getValue();
            return false;
        }
    }
    //Version property is missing
    else {
        *errorCode = ERROR_KEY_PROPERTY_MISSING;
        errorDescription =  TEXT("'VERSION' property is missing");
        return false;
    }

    return true;
}

bool vCardConverter::validateProperty21(VProperty* prop, WString& errorDescription, long* errorCode) {

    if(!wcsstr(VCARD21_PROPERTIES_LIST, prop->getName()) &&
        wcsstr(prop->getName(),TEXT("X-")) != prop->getName()) {
        *errorCode = ERROR_ILLEGAL_PROPERTY_NAME;
        errorDescription = TEXT("Illegal property name : %s");
        errorDescription += prop->getName();
        return false;
    }

    //The default encoding for the vCard object is 7-Bit.
    //It can be overridden for an individual property value by using the 揈NCODING

⌨️ 快捷键说明

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