00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <qdatetime.h>
00023 #include <qstring.h>
00024 #include <qptrlist.h>
00025 #include <qregexp.h>
00026 #include <qclipboard.h>
00027 #include <qfile.h>
00028 #include <qtextstream.h>
00029
00030 #include <kdebug.h>
00031 #include <klocale.h>
00032
00033 extern "C" {
00034 #include <ical.h>
00035 #include <icalss.h>
00036 #include <icalparser.h>
00037 #include <icalrestriction.h>
00038 #include <icalmemory.h>
00039 }
00040
00041 #include "calendar.h"
00042 #include "calendarlocal.h"
00043 #include "journal.h"
00044
00045 #include "icalformat.h"
00046 #include "icalformatimpl.h"
00047 #include <ksavefile.h>
00048
00049 #include <stdio.h>
00050
00051 #define _ICAL_VERSION "2.0"
00052
00053 using namespace KCal;
00054
00055 ICalFormat::ICalFormat() : mImpl(0)
00056 {
00057 setImplementation( new ICalFormatImpl( this ) );
00058
00059 mTimeZoneId = "UTC";
00060 mUtc = true;
00061 }
00062
00063 ICalFormat::~ICalFormat()
00064 {
00065 delete mImpl;
00066 }
00067
00068 void ICalFormat::setImplementation( ICalFormatImpl *impl )
00069 {
00070 if ( mImpl ) delete mImpl;
00071 mImpl = impl;
00072 }
00073
00074 #if defined(_AIX) && defined(open)
00075 #undef open
00076 #endif
00077
00078 bool ICalFormat::load( Calendar *calendar, const QString &fileName)
00079 {
00080 kdDebug(5800) << "ICalFormat::load() " << fileName << endl;
00081
00082 clearException();
00083
00084 QFile file( fileName );
00085 if (!file.open( IO_ReadOnly ) ) {
00086 kdDebug(5800) << "ICalFormat::load() load error" << endl;
00087 setException(new ErrorFormat(ErrorFormat::LoadError));
00088 return false;
00089 }
00090 QTextStream ts( &file );
00091 ts.setEncoding( QTextStream::Latin1 );
00092 QString text = ts.read();
00093 file.close();
00094
00095 if ( text.stripWhiteSpace().isEmpty() )
00096 return true;
00097 else
00098 return fromRawString( calendar, text.latin1() );
00099 }
00100
00101
00102 bool ICalFormat::save( Calendar *calendar, const QString &fileName )
00103 {
00104 kdDebug(5800) << "ICalFormat::save(): " << fileName << endl;
00105
00106 clearException();
00107
00108 QString text = toString( calendar );
00109
00110 if ( text.isNull() ) return false;
00111
00112
00113 KSaveFile::backupFile( fileName );
00114
00115 KSaveFile file( fileName );
00116 if ( file.status() != 0 ) {
00117 kdDebug(5800) << "ICalFormat::save() errno: " << strerror( file.status() )
00118 << endl;
00119 setException( new ErrorFormat( ErrorFormat::SaveError,
00120 i18n( "Error saving to '%1'." ).arg( fileName ) ) );
00121 return false;
00122 }
00123
00124
00125 QCString textUtf8 = text.utf8();
00126 file.file()->writeBlock( textUtf8.data(), textUtf8.size() - 1 );
00127
00128 if ( !file.close() ) {
00129 setException(new ErrorFormat(ErrorFormat::SaveError,
00130 i18n("Could not save '%1'").arg(fileName)));
00131 return false;
00132 }
00133
00134 return true;
00135 }
00136
00137 bool ICalFormat::fromString( Calendar *cal, const QString &text )
00138 {
00139 return fromRawString( cal, text.utf8() );
00140 }
00141
00142 bool ICalFormat::fromRawString( Calendar *cal, const QCString &text )
00143 {
00144 setTimeZone( cal->timeZoneId(), !cal->isLocalTime() );
00145
00146
00147
00148 icalcomponent *calendar;
00149
00150
00151 calendar = icalcomponent_new_from_string( const_cast<char*>( (const char*)text ) );
00152
00153 if (!calendar) {
00154 kdDebug(5800) << "ICalFormat::load() parse error" << endl;
00155 setException(new ErrorFormat(ErrorFormat::ParseErrorIcal));
00156 return false;
00157 }
00158
00159 bool success = true;
00160
00161 if (icalcomponent_isa(calendar) == ICAL_XROOT_COMPONENT) {
00162 icalcomponent *comp;
00163 for ( comp = icalcomponent_get_first_component(calendar, ICAL_VCALENDAR_COMPONENT);
00164 comp != 0; comp = icalcomponent_get_next_component(calendar, ICAL_VCALENDAR_COMPONENT) ) {
00165
00166 if ( !mImpl->populate( cal, comp ) ) {
00167 kdDebug(5800) << "ICalFormat::load(): Could not populate calendar" << endl;
00168 if ( !exception() ) {
00169 setException(new ErrorFormat(ErrorFormat::ParseErrorKcal));
00170 }
00171 success = false;
00172 } else
00173 mLoadedProductId = mImpl->loadedProductId();
00174 }
00175 } else if (icalcomponent_isa(calendar) != ICAL_VCALENDAR_COMPONENT) {
00176 kdDebug(5800) << "ICalFormat::load(): No VCALENDAR component found" << endl;
00177 setException(new ErrorFormat(ErrorFormat::NoCalendar));
00178 success = false;
00179 } else {
00180
00181 if ( !mImpl->populate( cal, calendar ) ) {
00182 kdDebug(5800) << "ICalFormat::load(): Could not populate calendar" << endl;
00183 if ( !exception() ) {
00184 setException(new ErrorFormat(ErrorFormat::ParseErrorKcal));
00185 }
00186 success = false;
00187 } else
00188 mLoadedProductId = mImpl->loadedProductId();
00189 }
00190
00191 icalcomponent_free( calendar );
00192 icalmemory_free_ring();
00193
00194 return success;
00195 }
00196
00197 Incidence *ICalFormat::fromString( const QString &text )
00198 {
00199 CalendarLocal cal( mTimeZoneId );
00200 fromString(&cal, text);
00201
00202 Incidence *ical = 0;
00203 Event::List elist = cal.events();
00204 if ( elist.count() > 0 ) {
00205 ical = elist.first();
00206 } else {
00207 Todo::List tlist = cal.todos();
00208 if ( tlist.count() > 0 ) {
00209 ical = tlist.first();
00210 } else {
00211 Journal::List jlist = cal.journals();
00212 if ( jlist.count() > 0 ) {
00213 ical = jlist.first();
00214 }
00215 }
00216 }
00217
00218 return ical ? ical->clone() : 0;
00219 }
00220
00221 QString ICalFormat::toString( Calendar *cal )
00222 {
00223 setTimeZone( cal->timeZoneId(), !cal->isLocalTime() );
00224
00225 icalcomponent *calendar = mImpl->createCalendarComponent(cal);
00226
00227 icalcomponent *component;
00228
00229
00230 Todo::List todoList = cal->rawTodos();
00231 Todo::List::ConstIterator it;
00232 for( it = todoList.begin(); it != todoList.end(); ++it ) {
00233
00234
00235 component = mImpl->writeTodo( *it );
00236 icalcomponent_add_component( calendar, component );
00237 }
00238
00239
00240 Event::List events = cal->rawEvents();
00241 Event::List::ConstIterator it2;
00242 for( it2 = events.begin(); it2 != events.end(); ++it2 ) {
00243
00244
00245 component = mImpl->writeEvent( *it2 );
00246 icalcomponent_add_component( calendar, component );
00247 }
00248
00249
00250 Journal::List journals = cal->journals();
00251 Journal::List::ConstIterator it3;
00252 for( it3 = journals.begin(); it3 != journals.end(); ++it3 ) {
00253 kdDebug(5800) << "ICalFormat::toString() write journal "
00254 << (*it3)->uid() << endl;
00255 component = mImpl->writeJournal( *it3 );
00256 icalcomponent_add_component( calendar, component );
00257 }
00258
00259 QString text = QString::fromUtf8( icalcomponent_as_ical_string( calendar ) );
00260
00261 icalcomponent_free( calendar );
00262 icalmemory_free_ring();
00263
00264 if (!text) {
00265 setException(new ErrorFormat(ErrorFormat::SaveError,
00266 i18n("libical error")));
00267 return QString::null;
00268 }
00269
00270 return text;
00271 }
00272
00273 QString ICalFormat::toICalString( Incidence *incidence )
00274 {
00275 CalendarLocal cal( mTimeZoneId );
00276 cal.addIncidence( incidence->clone() );
00277 return toString( &cal );
00278 }
00279
00280 QString ICalFormat::toString( Incidence *incidence )
00281 {
00282 icalcomponent *component;
00283
00284 component = mImpl->writeIncidence( incidence );
00285
00286 QString text = QString::fromUtf8( icalcomponent_as_ical_string( component ) );
00287
00288 icalcomponent_free( component );
00289
00290 return text;
00291 }
00292
00293 QString ICalFormat::toString( RecurrenceRule *recurrence )
00294 {
00295 icalproperty *property;
00296 property = icalproperty_new_rrule( mImpl->writeRecurrenceRule( recurrence ) );
00297 QString text = QString::fromUtf8( icalproperty_as_ical_string( property ) );
00298 icalproperty_free( property );
00299 return text;
00300 }
00301
00302 bool ICalFormat::fromString( RecurrenceRule * recurrence, const QString& rrule )
00303 {
00304 if ( !recurrence ) return false;
00305 bool success = true;
00306 icalerror_clear_errno();
00307 struct icalrecurrencetype recur = icalrecurrencetype_from_string( rrule.latin1() );
00308 if ( icalerrno != ICAL_NO_ERROR ) {
00309 kdDebug(5800) << "Recurrence parsing error: " << icalerror_strerror( icalerrno ) << endl;
00310 success = false;
00311 }
00312
00313 if ( success ) {
00314 mImpl->readRecurrence( recur, recurrence );
00315 }
00316
00317 return success;
00318 }
00319
00320
00321 QString ICalFormat::createScheduleMessage(IncidenceBase *incidence,
00322 Scheduler::Method method)
00323 {
00324 icalcomponent *message = 0;
00325
00326
00327 if ( incidence->type() == "Event" || incidence->type() == "Todo" ) {
00328 Incidence* i = static_cast<Incidence*>( incidence );
00329 if ( i->schedulingID() != i->uid() ) {
00330
00331 i = i->clone();
00332 i->setUid( i->schedulingID() );
00333 i->setSchedulingID( QString::null );
00334
00335
00336 message = mImpl->createScheduleComponent( i, method );
00337
00338
00339 delete i;
00340 }
00341 }
00342
00343 if ( message == 0 )
00344 message = mImpl->createScheduleComponent(incidence,method);
00345
00346
00347 QString messageText = QString::fromUtf8( icalcomponent_as_ical_string(message) );
00348
00349 #if 0
00350 kdDebug(5800) << "ICalFormat::createScheduleMessage: message START\n"
00351 << messageText
00352 << "ICalFormat::createScheduleMessage: message END" << endl;
00353 #endif
00354
00355 return messageText;
00356 }
00357
00358 FreeBusy *ICalFormat::parseFreeBusy( const QString &str )
00359 {
00360 clearException();
00361
00362 icalcomponent *message;
00363 message = icalparser_parse_string( str.utf8() );
00364
00365 if ( !message ) return 0;
00366
00367 FreeBusy *freeBusy = 0;
00368
00369 icalcomponent *c;
00370 for ( c = icalcomponent_get_first_component( message, ICAL_VFREEBUSY_COMPONENT );
00371 c != 0; c = icalcomponent_get_next_component( message, ICAL_VFREEBUSY_COMPONENT ) ) {
00372 FreeBusy *fb = mImpl->readFreeBusy( c );
00373
00374 if ( freeBusy ) {
00375 freeBusy->merge( fb );
00376 delete fb;
00377 } else {
00378 freeBusy = fb;
00379 }
00380 }
00381
00382 if ( !freeBusy )
00383 kdDebug(5800) << "ICalFormat:parseFreeBusy: object is not a freebusy."
00384 << endl;
00385 return freeBusy;
00386 }
00387
00388 ScheduleMessage *ICalFormat::parseScheduleMessage( Calendar *cal,
00389 const QString &messageText )
00390 {
00391 setTimeZone( cal->timeZoneId(), !cal->isLocalTime() );
00392 clearException();
00393
00394 if (messageText.isEmpty())
00395 {
00396 setException( new ErrorFormat( ErrorFormat::ParseErrorKcal, QString::fromLatin1( "messageText was empty, unable to parse into a ScheduleMessage" ) ) );
00397 return 0;
00398 }
00399
00400 icalcomponent *message;
00401 message = icalparser_parse_string(messageText.utf8());
00402
00403 if (!message)
00404 {
00405 setException( new ErrorFormat( ErrorFormat::ParseErrorKcal, QString::fromLatin1( "icalparser was unable to parse messageText into a ScheduleMessage" ) ) );
00406 return 0;
00407 }
00408
00409 icalproperty *m = icalcomponent_get_first_property(message,
00410 ICAL_METHOD_PROPERTY);
00411 if (!m)
00412 {
00413 setException( new ErrorFormat( ErrorFormat::ParseErrorKcal, QString::fromLatin1( "message didn't contain an ICAL_METHOD_PROPERTY" ) ) );
00414 return 0;
00415 }
00416
00417 icalcomponent *c;
00418
00419 IncidenceBase *incidence = 0;
00420 c = icalcomponent_get_first_component(message,ICAL_VEVENT_COMPONENT);
00421 if (c) {
00422 icalcomponent *ctz = icalcomponent_get_first_component(message,ICAL_VTIMEZONE_COMPONENT);
00423 incidence = mImpl->readEvent(c, ctz);
00424 }
00425
00426 if (!incidence) {
00427 c = icalcomponent_get_first_component(message,ICAL_VTODO_COMPONENT);
00428 if (c) {
00429 incidence = mImpl->readTodo(c);
00430 }
00431 }
00432
00433 if (!incidence) {
00434 c = icalcomponent_get_first_component(message,ICAL_VJOURNAL_COMPONENT);
00435 if (c) {
00436 incidence = mImpl->readJournal(c);
00437 }
00438 }
00439
00440 if (!incidence) {
00441 c = icalcomponent_get_first_component(message,ICAL_VFREEBUSY_COMPONENT);
00442 if (c) {
00443 incidence = mImpl->readFreeBusy(c);
00444 }
00445 }
00446
00447
00448
00449 if (!incidence) {
00450 kdDebug(5800) << "ICalFormat:parseScheduleMessage: object is not a freebusy, event, todo or journal" << endl;
00451 setException( new ErrorFormat( ErrorFormat::ParseErrorKcal, QString::fromLatin1( "object is not a freebusy, event, todo or journal" ) ) );
00452 return 0;
00453 }
00454
00455 kdDebug(5800) << "ICalFormat::parseScheduleMessage() getting method..." << endl;
00456
00457 icalproperty_method icalmethod = icalproperty_get_method(m);
00458 Scheduler::Method method;
00459
00460 switch (icalmethod) {
00461 case ICAL_METHOD_PUBLISH:
00462 method = Scheduler::Publish;
00463 break;
00464 case ICAL_METHOD_REQUEST:
00465 method = Scheduler::Request;
00466 break;
00467 case ICAL_METHOD_REFRESH:
00468 method = Scheduler::Refresh;
00469 break;
00470 case ICAL_METHOD_CANCEL:
00471 method = Scheduler::Cancel;
00472 break;
00473 case ICAL_METHOD_ADD:
00474 method = Scheduler::Add;
00475 break;
00476 case ICAL_METHOD_REPLY:
00477 method = Scheduler::Reply;
00478 break;
00479 case ICAL_METHOD_COUNTER:
00480 method = Scheduler::Counter;
00481 break;
00482 case ICAL_METHOD_DECLINECOUNTER:
00483 method = Scheduler::Declinecounter;
00484 break;
00485 default:
00486 method = Scheduler::NoMethod;
00487 kdDebug(5800) << "ICalFormat::parseScheduleMessage(): Unknow method" << endl;
00488 break;
00489 }
00490
00491 kdDebug(5800) << "ICalFormat::parseScheduleMessage() restriction..." << endl;
00492
00493 if (!icalrestriction_check(message)) {
00494 kdWarning(5800) << k_funcinfo << endl << "libkcal reported a problem while parsing:" << endl;
00495 kdWarning(5800) << Scheduler::translatedMethodName(method) + ": " + mImpl->extractErrorProperty(c)<< endl;
00496
00497
00498
00499
00500
00501
00502
00503 }
00504 icalcomponent *calendarComponent = mImpl->createCalendarComponent(cal);
00505
00506 Incidence *existingIncidence =
00507 cal->incidenceFromSchedulingID(incidence->uid());
00508 if (existingIncidence) {
00509
00510
00511 if (existingIncidence->type() == "Todo") {
00512 Todo *todo = static_cast<Todo *>(existingIncidence);
00513 icalcomponent_add_component(calendarComponent,
00514 mImpl->writeTodo(todo));
00515 }
00516 if (existingIncidence->type() == "Event") {
00517 Event *event = static_cast<Event *>(existingIncidence);
00518 icalcomponent_add_component(calendarComponent,
00519 mImpl->writeEvent(event));
00520 }
00521 } else {
00522 calendarComponent = 0;
00523 }
00524
00525 kdDebug(5800) << "ICalFormat::parseScheduleMessage() classify..." << endl;
00526
00527 icalproperty_xlicclass result = icalclassify( message, calendarComponent,
00528 (char *)"" );
00529
00530 kdDebug(5800) << "ICalFormat::parseScheduleMessage() returning..." << endl;
00531 kdDebug(5800) << "ICalFormat::parseScheduleMessage(), result = " << result << endl;
00532
00533 ScheduleMessage::Status status;
00534
00535 switch (result) {
00536 case ICAL_XLICCLASS_PUBLISHNEW:
00537 status = ScheduleMessage::PublishNew;
00538 break;
00539 case ICAL_XLICCLASS_PUBLISHUPDATE:
00540 status = ScheduleMessage::PublishUpdate;
00541 break;
00542 case ICAL_XLICCLASS_OBSOLETE:
00543 status = ScheduleMessage::Obsolete;
00544 break;
00545 case ICAL_XLICCLASS_REQUESTNEW:
00546 status = ScheduleMessage::RequestNew;
00547 break;
00548 case ICAL_XLICCLASS_REQUESTUPDATE:
00549 status = ScheduleMessage::RequestUpdate;
00550 break;
00551 case ICAL_XLICCLASS_UNKNOWN:
00552 default:
00553 status = ScheduleMessage::Unknown;
00554 break;
00555 }
00556
00557 kdDebug(5800) << "ICalFormat::parseScheduleMessage(), status = " << status << endl;
00558
00559
00560 return new ScheduleMessage(incidence,method,status);
00561 }
00562
00563 void ICalFormat::setTimeZone( const QString &id, bool utc )
00564 {
00565 mTimeZoneId = id;
00566 mUtc = utc;
00567 }
00568
00569 QString ICalFormat::timeZoneId() const
00570 {
00571 return mTimeZoneId;
00572 }
00573
00574 bool ICalFormat::utc() const
00575 {
00576 return mUtc;
00577 }