📄 icalconverter.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/iCalendar/iCalConverter.h"
#include "base/util/WString.h"
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;
--endTask;
}
extractAlarm((VObject*) ret);
return ret;
}
void iCalConverter::extractAlarm(VObject* vo){
int beginAlarm = -1;
int endAlarm = -1;
for(int i = 0, m = vo->propertiesCount(); i < m ; i++) {
if(beginAlarm== -1 && !wcscmp(vo->getProperty(i)->getName(), TEXT("BEGIN")) &&
vo->getProperty(i)->getValue() &&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -