vproperty.cpp

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

CPP
761
字号
/*
 * 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/util/utils.h"
#include "base/util/WString.h"
#include "vocl/VProperty.h"
#include "base/quoted-printable.h"
#include "base/globalsdef.h"

USE_NAMESPACE

VProperty::VProperty(const WCHAR* propname, const WCHAR* propvalue) {

    name = (propname) ? wstrdup(propname) : NULL;

    parameters = new ArrayList();
    values     = new ArrayList();
    valueBuf   = NULL;

    if (propvalue) {
        WString wsVal = propvalue;
        values->add((ArrayElement&)wsVal);
    }
}

VProperty::~VProperty() {

    if (name) {
        delete [] name; name = NULL;
    }
    if (parameters) {
        delete parameters; parameters = NULL;
    }
    if (values) {
        delete values; values = NULL;
    }
    if (valueBuf) {
        delete [] valueBuf; valueBuf = NULL;
    }
}

void VProperty::setName (const WCHAR* s) {

    set(&name, s);
}


WCHAR* VProperty::getName(WCHAR* buf, int size) {

    if (buf == NULL) {
        return name;
    }

    if (size >= 0) {
        wcsncpy(buf, name, size);
    }
    else {
        wcscpy(buf, name);
    }

    return buf;
}

void VProperty::addValue(const WCHAR* value) {

    // Empty strings are accepted
    if(value) {
        WString wsVal = value;
        values->add((ArrayElement &)wsVal);
    }
}

bool VProperty::removeValue(const int index) {

    bool ret = false;
    if (values != NULL) {
        values->removeElementAt(index);
                ret = true;
    }
    return ret;
}

// Returned value is a pointer to internal buffer,
// copy it if must have a different life cycle.
WCHAR* VProperty::getValue(int index) {

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

    WString* wsValue = (WString*)values->get(index);
    if (wsValue) {
        valueBuf = new WCHAR[wsValue->length() + 1];
        wcscpy(valueBuf, wsValue->c_str());
    }

    return valueBuf;
}

int VProperty::valueCount() {

    return values->size();
}


////////// Only for compatibility
WCHAR* VProperty::getValue(WCHAR* buf) {

    if (!buf) {
        return getValue(0);
    }

    WString* wsValue = (WString*)values->get(0);
    if (wsValue) {
        wcscpy(buf, wsValue->c_str());
    }
    return buf;
}


void VProperty::setValue(const WCHAR* value) {

    addValue(value);

}

WCHAR* VProperty::getPropComponent(int i) {

    return getValue(i-1);
}
//////////////////////////////////


void VProperty::addParameter (const WCHAR* paramName, const WCHAR* paramValue) {

    if(paramName) {
    WKeyValuePair *parameter = new WKeyValuePair(paramName, paramValue);
    parameters->add((ArrayElement &)*parameter);

    delete parameter; parameter = NULL;
    }
}

void VProperty::removeParameter(WCHAR* paramName) {

    if (parameters != NULL) {
        for (int i=0; i<parameters->size(); i++){
            WKeyValuePair *parameter;
            parameter = (WKeyValuePair* )parameters->get(i);
            if(!wcscmp(parameter->getKey(), paramName)) {
                parameters->removeElementAt(i);
                break;
            }
        }
    }
}
bool VProperty::containsParameter(WCHAR* paramName) {

    if (parameters != NULL) {
        for (int i=0; i<parameters->size(); i++){
            WKeyValuePair *parameter;
            parameter = (WKeyValuePair* )parameters->get(i);
            if(!wcscmp(parameter->getKey(), paramName)){
                return true;
            }
        }
    }

    return false;
}
WCHAR* VProperty::getParameterValue(WCHAR* paramName) {

    if (parameters != NULL) {
        for (int i=0; i<parameters->size(); i++) {
            WKeyValuePair *parameter;
            parameter = (WKeyValuePair* )parameters->get(i);
            if(!wcscmp(parameter->getKey(), paramName))
                return ((WCHAR *)parameter->getValue());
        }
    }

    return NULL;
}
WCHAR* VProperty::getParameterValue(int index) {

    if (parameters != NULL) {
        WKeyValuePair *parameter;
        parameter = (WKeyValuePair*)parameters->get(index);
        return parameter ? (WCHAR *)parameter->getValue() : NULL;
    }

    return NULL;
}

void VProperty::set(WCHAR** p, const WCHAR* v) {

    if (*p) {
        delete [] *p;
    }
    *p = (v) ? wstrdup(v) : NULL;
}

ArrayElement* VProperty::clone() {

    if(name) {

        VProperty *cloneProperty = new VProperty(name);

        if(values != NULL) {
            for (int i=0; i<valueCount(); i++) {
                WString* valueCopy = (WString*)values->get(i)->clone();
                cloneProperty->addValue(valueCopy->c_str());
            }
        }

        if (parameters != NULL) {
            for (int i=0; i<parameters->size(); i++) {
                WKeyValuePair* parameterCopy;
                parameterCopy = (WKeyValuePair*)parameters->get(i)->clone();
                cloneProperty->addParameter(parameterCopy->getKey(), parameterCopy->getValue());
                delete parameterCopy;
            }
        }
        return cloneProperty;
    }
    return NULL;
}

int VProperty::parameterCount() {

    return parameters->size();
}


/*
 * Returns a WCHAR* string of this VProperty, based on vCard-vCal specifications.
 * Here values of the property are encoded / special chars are escaped according to
 * vCard-vCal 2.1/3.0 specifications.
 * @param version: vCard version "2.1" or "3.0" - we have different specs
 *                 (if not defined, default will be 2.1)
 *
 * Note:
 * The returned WCHAR* is new allocated, must be freed by the caller.
 */
WCHAR* VProperty::toString(WCHAR* version) {

    bool is_30 = false;
    if (version) {
        is_30 = !wcscmp(version, TEXT("3.0"));
    }

    WString propertyString = TEXT("");
    bool isToFormatValue = true;

    if (!name){
        goto finally;
    }
    
    if (parameterCount()>0 && containsParameter(TEXT("CONTENT-VALUE"))) {
        WCHAR* parVal = getParameterValue(TEXT("CONTENT-VALUE"));
        if (parVal != NULL && wcscmp(parVal, TEXT("UNCHANGED")) == 0) 
            isToFormatValue = false;
        
    }

    // Set encoding (QP/B64) parameter if necessary
    // QP encoding not allowed for vCard 3.0 (RFC 2426)
    if (is_30) {
        if (isToFormatValue) {
            if(!equalsEncoding(TEXT("BASE64")) &&
               !equalsEncoding(TEXT("B")) &&
               !equalsEncoding(TEXT("b")) ) {
                for (int i=0; i<valueCount(); i++) {
                    char* charValue = toMultibyte(getValue(i));
                    if (encodingIsNeed(charValue)) {
                        addParameter(TEXT("ENCODING"), TEXT("b"));
                        delete [] charValue;
                        break;
                    }
                }
            }
        }
    }
    else {               
        if (isToFormatValue) {
            if (!equalsEncoding(TEXT("QUOTED-PRINTABLE")) ) {
                for (int i=0; i<valueCount(); i++) {
                    char* charValue = toMultibyte(getValue(i));
                    if (encodingIsNeed(charValue)) {
                        addParameter(TEXT("ENCODING"), TEXT("QUOTED-PRINTABLE"));
                        addParameter(TEXT("CHARSET"), TEXT("UTF-8"));
                        delete [] charValue;
                        break;
                    }
                    delete [] charValue;
                }
            }
        }
    }


    //
    // Write Group:
    //
    if (parameterCount()>0 && containsParameter(TEXT("GROUP"))) {
        propertyString.append(getParameterValue(TEXT("GROUP")));
        propertyString.append(TEXT("."));
    }

    //
    // Write name:
    //
    propertyString.append(name);

    //
    // Write parameters:
    //
    if(parameterCount()>0) {
        for (int i=0; i<parameterCount(); i++) {
            WKeyValuePair *parameter;
            parameter = (WKeyValuePair*)parameters->get(i);
            if (parameter->getKey()) {
                if (!wcscmp(parameter->getKey(), TEXT("GROUP"))) {
                    continue;
                }
                // for the custom value 
                if (!wcscmp(parameter->getKey(), TEXT("CONTENT-VALUE"))) {
                    continue;
                }
                propertyString.append(TEXT(";"));
                propertyString.append(parameter->getKey());
            }
            if (parameter->getValue()) {
                propertyString.append(TEXT("="));
                propertyString.append(parameter->getValue());
            }
        }
    }

    //
    // Write values:
    //
    propertyString.append(TEXT(":"));
    if(valueCount()>0) {
        WString valueString = TEXT("");
        
        if (isToFormatValue) {
            // Get all values in one single string

⌨️ 快捷键说明

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