icalconverter.cpp

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

CPP
945
字号
/*
 * 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 "vocl/iCalendar/iCalConverter.h"
#include "base/util/WString.h"
#include "base/globalsdef.h"

USE_NAMESPACE


iCalConverter::iCalConverter() {
    iCalendar = NULL;
    calendar = NULL;
}

iCalConverter::~iCalConverter() {
    if (iCalendar) {
        delete [] iCalendar; iCalendar = NULL;
    }
    if (calendar) {
        delete calendar; calendar = NULL;
    }
}

void iCalConverter::setSource(WCHAR* inputICalendar) {
    if(iCalendar) {
        delete[] iCalendar;
        iCalendar = NULL;
    }
    if(inputICalendar) {
        iCalendar = new WCHAR[wcslen(inputICalendar) + 1];
        wcscpy(iCalendar, inputICalendar);
    }
    if(calendar) {
        delete calendar; calendar = NULL;
    }
}

void iCalConverter::setSource(Calendar& inputCalendar) {
    if(calendar)
        delete calendar;
    calendar = (Calendar *)inputCalendar.clone();
    if(iCalendar) {
        delete [] iCalendar; iCalendar = NULL;
    }
}

void iCalConverter::getICalendar(WCHAR* ouputICalendar) {
    if(iCalendar) {
        if(!ouputICalendar)
            return;
        wcscpy(ouputICalendar, iCalendar);
    }
    else
        ouputICalendar = NULL;
}

void iCalConverter::getCalendar(Calendar** outputCalendar) {
    if(calendar) {
        *outputCalendar = (Calendar *)calendar->clone();
    }
    else
        *outputCalendar = NULL;
}

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

    if(!calendar && !iCalendar)
        return false;
    if(calendar && iCalendar)
        return true;
    if(calendar) {

        iCalendar = calendar->toString();

        if(!calendar->getProdID() || !calendar->getProdID()->getValue()) {
            *errorCode = ERROR_KEY_PROPERTY_MISSING;
            errorDescription = TEXT("'PRODID' property is missing");
            delete [] iCalendar; iCalendar = NULL;
            return false;
        }

        if(!calendar->getVersion() || !calendar->getVersion()->getValue()) {
            *errorCode = ERROR_KEY_PROPERTY_MISSING;
            errorDescription = TEXT("'VERSION' property is missing");
            delete [] iCalendar; iCalendar = NULL;
            return false;
        }
        if(calendar->getEvents())
            for(int i=0; i<calendar->getEvents()->size(); i++)
                if(!validateEvent(((Event*)calendar->getEvents()->get(i)), errorDescription, errorCode)) {
                    delete [] iCalendar; iCalendar = NULL;
                    return false;
            }
        if(calendar->getToDos())
            for(int i=0; i<calendar->getToDos()->size(); i++)
                if(!validateTodo(((ToDo*)calendar->getToDos()->get(i)), errorDescription, errorCode)) {
                    delete [] iCalendar; iCalendar = NULL;
                    return false;
                }

        return true;
   }
    if (iCalendar) {

        calendar = new Calendar();
        VObject* vo = VConverter::parse(iCalendar);
        if(!vo) {
            *errorCode = ERROR_PARSING_ERROR;
            errorDescription = TEXT("Invalid VObject returned");
            return false;
        }

        int n = vo->propertiesCount();

        if(wcscmp(vo->getProperty(0)->getName(), TEXT("BEGIN")) ||
            !vo->getProperty(0)->getValue() ||
            wcscmp(vo->getProperty(0)->getValue(), TEXT("VCALENDAR"))) {
                *errorCode = ERROR_KEY_PROPERTY_MISSING;
                errorDescription = TEXT("'BEGIN:VCALENDAR' property is missing");
                return false;
            }

        if(wcscmp(vo->getProperty(n-1)->getName(), TEXT("END")) ||
            !vo->getProperty(n-1)->getValue() ||
            wcscmp(vo->getProperty(n-1)->getValue(), TEXT("VCALENDAR"))) {
                *errorCode = ERROR_KEY_PROPERTY_MISSING;
                errorDescription = TEXT("'END:VCALENDAR' property is missing");
                return false;
            }

        if(!vo->containsProperty(TEXT("VERSION"))) {
            *errorCode = ERROR_KEY_PROPERTY_MISSING;
            errorDescription = TEXT("'VERSION' property is missing");
            return false;
        }

        if(vo->containsProperty(TEXT("VERSION")) &&
            (!vo->getProperty(TEXT("VERSION")) || wcscmp(vo->getProperty(TEXT("VERSION"))->getValue(), TEXT("2.0")))) {
                *errorCode = ERROR_ILLEGAL_VERSION_NUMBER;
                if(vo->getProperty(TEXT("VERSION"))) {
                    errorDescription = TEXT("Illegal version number : ");
                    errorDescription += vo->getProperty(TEXT("VERSION"))->getValue();
                }
                else {
                    errorDescription = TEXT("Illegal version number");
                }
                return false;
            }
        else {
            iCalProperty* prop = new iCalProperty(vo->getProperty(TEXT("VERSION"))->getValue());
            calendar->setVersion(*prop);
            vo->removeProperty(TEXT("VERSION"));
            delete prop;
        }

        if(!vo->containsProperty(TEXT("PRODID")) || !vo->getProperty(TEXT("PRODID"))) {
            *errorCode = ERROR_KEY_PROPERTY_MISSING;
            errorDescription = TEXT("'PRODID' property is missing");
            return false;
        }
        else {
            iCalProperty* prop = new iCalProperty(vo->getProperty(TEXT("PRODID"))->getValue());
            calendar->setProdID(*prop);
            vo->removeProperty(TEXT("PRODID"));
            delete prop;
        }

        if(vo->containsProperty(TEXT("CALSCALE")) ||
            vo->getProperty(TEXT("CALSCALE"))) {
                iCalProperty* prop = new iCalProperty(vo->getProperty(TEXT("CALSCALE"))->getValue());
                calendar->setCalScale(*prop);
                vo->removeProperty(TEXT("CALSCALE"));
                delete prop;
            }

        if(vo->containsProperty(TEXT("METHOD")) ||
            vo->getProperty(TEXT("METHOD"))) {
                iCalProperty* prop = new iCalProperty(vo->getProperty(TEXT("METHOD"))->getValue());
                calendar->setMethod(*prop);
                vo->removeProperty(TEXT("METHOD"));
                delete prop;
            }

        //extract VEVENTs from vo
        Event* ev;
        while((ev = extractEvent(vo, errorDescription, errorCode))) {
            if (!validateEvent(ev, errorDescription, errorCode)) {
                delete ev; ev = NULL;
                return false;
            }
            calendar->addEvent(ev);
            delete ev; ev = NULL;
        }

        //extract VTODOs from vo
        ToDo* task;
        while((task = extractTask(vo, errorDescription, errorCode))) {
            if (!validateTodo(task, errorDescription, errorCode)) {
                delete task; task = NULL;
                return false;
            }
            calendar->addToDo(task);
            delete task; task = NULL;
        }
    }

    return true;
}

Event* iCalConverter::extractEvent(VObject* vo, WString& errorDescription, long* errorCode) {
    int i,m;
	int beginEvent = -1;
    int endEvent = -1;
    for(i = 0, m = vo->propertiesCount(); i < m ; i++) {
        if(beginEvent == -1 && !wcscmp(vo->getProperty(i)->getName(), TEXT("BEGIN")) &&
            vo->getProperty(i)->getValue() &&
            !wcscmp(vo->getProperty(i)->getValue(), TEXT("VEVENT")))
            beginEvent = i;
        if(endEvent == -1 && !wcscmp(vo->getProperty(i)->getName(),TEXT("END")) &&
            vo->getProperty(i)->getValue() &&
            !wcscmp(vo->getProperty(i)->getValue(),TEXT("VEVENT"))) {
                endEvent = i;
                break;
            }
    }

    if(beginEvent == -1)
        return NULL;

    if(beginEvent > endEvent) {
        *errorCode = ERROR_INVALID_EVENT_BLOCK;
        errorDescription = TEXT("BEGIN:VEVENT property found, but END:VEVENT is missing");
        return NULL;
    }

    Event* ret = new Event();
    for(i = beginEvent; i <= endEvent; i++) {
        ret->addProperty(vo->getProperty(i));
        vo->removeProperty(i);
        --i;
        --endEvent;
    }

    extractAlarm((VObject*) ret);

    return ret;
}

ToDo* iCalConverter::extractTask(VObject* vo, WString& errorDescription, long* errorCode) {
    int i,m;
	int beginTask = -1;
    int endTask = -1;
    for(i = 0, m = vo->propertiesCount(); i < m ; i++) {
        if(beginTask == -1 && !wcscmp(vo->getProperty(i)->getName(), TEXT("BEGIN")) &&
            vo->getProperty(i)->getValue() &&
            !wcscmp(vo->getProperty(i)->getValue(), TEXT("VTODO")))
            beginTask = i;
        if(endTask == -1 && !wcscmp(vo->getProperty(i)->getName(),TEXT("END")) &&
            vo->getProperty(i)->getValue() &&
            !wcscmp(vo->getProperty(i)->getValue(),TEXT("VTODO"))) {
                endTask = i;
                break;
            }
    }

    if(beginTask == -1)
        return NULL;

    if(beginTask > endTask) {
        *errorCode = ERROR_INVALID_TODO_BLOCK;
        errorDescription = TEXT("BEGIN:VTODO property found, but END:VTODO is missing");
        return NULL;
    }

    ToDo* ret = new ToDo();
    for(i = beginTask; i <= endTask; i++) {
        ret->addProperty(vo->getProperty(i));
        vo->removeProperty(i);
        --i;

⌨️ 快捷键说明

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