⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vcardconverter.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
字号:
/*
 * 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 General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307  USA
 */


#include "vocl/vCard/vCardConverter.h"
#include "base/util/WString.h"

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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -