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

📄 vproperty.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * 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 "base/util/utils.h"
#include "base/util/WString.h"
#include "vocl/VProperty.h"
#include "base/quoted-printable.h"

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
            WCHAR *value, *valueConv;
            for (int i=0; i<valueCount(); i++) {
                if (i>0) {
                    valueString.append(TEXT(";"));
                }
                value = getValue(i);

                // Escape special chars - based on version (";"  "\", ",")
                valueConv = escapeSpecialChars(value, version);

⌨️ 快捷键说明

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