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 kdDebug(5800) << "KSaveFile: close: status was " << file.status() << ". See errno.h." << endl;
00130 setException(new ErrorFormat(ErrorFormat::SaveError,
00131 i18n("Could not save '%1'").arg(fileName)));
00132 return false;
00133 }
00134
00135 return true;
00136 }
00137
00138 bool ICalFormat::fromString( Calendar *cal, const QString &text )
00139 {
00140 return fromRawString( cal, text.utf8() );
00141 }
00142
00143 bool ICalFormat::fromRawString( Calendar *cal, const QCString &text )
00144 {
00145 setTimeZone( cal->timeZoneId(), !cal->isLocalTime() );
00146
00147
00148
00149 icalcomponent *calendar;
00150
00151
00152 calendar = icalcomponent_new_from_string( const_cast<char*>( (const char*)text ) );
00153
00154 if (!calendar) {
00155 kdDebug(5800) << "ICalFormat::load() parse error" << endl;
00156 setException(new ErrorFormat(ErrorFormat::ParseErrorIcal));
00157 return false;
00158 }
00159
00160 bool success = true;
00161
00162 if (icalcomponent_isa(calendar) == ICAL_XROOT_COMPONENT) {
00163 icalcomponent *comp;
00164 for ( comp = icalcomponent_get_first_component(calendar, ICAL_VCALENDAR_COMPONENT);
00165 comp != 0; comp = icalcomponent_get_next_component(calendar, ICAL_VCALENDAR_COMPONENT) ) {
00166
00167 if ( !mImpl->populate( cal, comp ) ) {
00168 kdDebug(5800) << "ICalFormat::load(): Could not populate calendar" << endl;
00169 if ( !exception() ) {
00170 setException(new ErrorFormat(ErrorFormat::ParseErrorKcal));
00171 }
00172 success = false;
00173 } else {
00174 mLoadedProductId = mImpl->loadedProductId();
00175 }
00176 icalcomponent_free( comp );
00177 }
00178 } else if (icalcomponent_isa(calendar) != ICAL_VCALENDAR_COMPONENT) {
00179 kdDebug(5800) << "ICalFormat::load(): No VCALENDAR component found" << endl;
00180 setException(new ErrorFormat(ErrorFormat::NoCalendar));
00181 success = false;
00182 } else {
00183
00184 if ( !mImpl->populate( cal, calendar ) ) {
00185 kdDebug(5800) << "ICalFormat::load(): Could not populate calendar" << endl;
00186 if ( !exception() ) {
00187 setException(new ErrorFormat(ErrorFormat::ParseErrorKcal));
00188 }
00189 success = false;
00190 } else
00191 mLoadedProductId = mImpl->loadedProductId();
00192 }
00193
00194 icalcomponent_free( calendar );
00195 icalmemory_free_ring();
00196
00197 return success;
00198 }
00199
00200 Incidence *ICalFormat::fromString( const QString &text )
00201 {
00202 CalendarLocal cal( mTimeZoneId );
00203 fromString(&cal, text);
00204
00205 Incidence *ical = 0;
00206 Event::List elist = cal.events();
00207 if ( elist.count() > 0 ) {
00208 ical = elist.first();
00209 } else {
00210 Todo::List tlist = cal.todos();
00211 if ( tlist.count() > 0 ) {
00212 ical = tlist.first();
00213 } else {
00214 Journal::List jlist = cal.journals();
00215 if ( jlist.count() > 0 ) {
00216 ical = jlist.first();
00217 }
00218 }
00219 }
00220
00221 return ical ? ical->clone() : 0;
00222 }
00223
00224 QString ICalFormat::toString( Calendar *cal )
00225 {
00226 setTimeZone( cal->timeZoneId(), !cal->isLocalTime() );
00227
00228 icalcomponent *calendar = mImpl->createCalendarComponent(cal);
00229
00230 icalcomponent *component;
00231
00232
00233 Todo::List todoList = cal->rawTodos();
00234 Todo::List::ConstIterator it;
00235 for( it = todoList.begin(); it != todoList.end(); ++it ) {
00236
00237
00238 component = mImpl->writeTodo( *it );
00239 icalcomponent_add_component( calendar, component );
00240 }
00241
00242
00243 Event::List events = cal->rawEvents();
00244 Event::List::ConstIterator it2;
00245 for( it2 = events.begin(); it2 != events.end(); ++it2 ) {
00246
00247
00248 component = mImpl->writeEvent( *it2 );
00249 icalcomponent_add_component( calendar, component );
00250 }
00251
00252
00253 Journal::List journals = cal->journals();
00254 Journal::List::ConstIterator it3;
00255 for( it3 = journals.begin(); it3 != journals.end(); ++it3 ) {
00256 kdDebug(5800) << "ICalFormat::toString() write journal "
00257 << (*it3)->uid() << endl;
00258 component = mImpl->writeJournal( *it3 );
00259 icalcomponent_add_component( calendar, component );
00260 }
00261
00262 QString text = QString::fromUtf8( icalcomponent_as_ical_string( calendar ) );
00263
00264 icalcomponent_free( calendar );
00265 icalmemory_free_ring();
00266
00267 if (!text) {
00268 setException(new ErrorFormat(ErrorFormat::SaveError,
00269 i18n("libical error")));
00270 return QString::null;
00271 }
00272
00273 return text;
00274 }
00275
00276 QString ICalFormat::toICalString( Incidence *incidence )
00277 {
00278 CalendarLocal cal( mTimeZoneId );
00279 cal.addIncidence( incidence->clone() );
00280 return toString( &cal );
00281 }
00282
00283 QString ICalFormat::toString( Incidence *incidence )
00284 {
00285 icalcomponent *component;
00286
00287 component = mImpl->writeIncidence( incidence );
00288
00289 QString text = QString::fromUtf8( icalcomponent_as_ical_string( component ) );
00290
00291 icalcomponent_free( component );
00292
00293 return text;
00294 }
00295
00296 QString ICalFormat::toString( RecurrenceRule *recurrence )
00297 {
00298 icalproperty *property;
00299 property = icalproperty_new_rrule( mImpl->writeRecurrenceRule( recurrence ) );
00300 QString text = QString::fromUtf8( icalproperty_as_ical_string( property ) );
00301 icalproperty_free( property );
00302 return text;
00303 }
00304
00305 bool ICalFormat::fromString( RecurrenceRule * recurrence, const QString& rrule )
00306 {
00307 if ( !recurrence ) return false;
00308 bool success = true;
00309 icalerror_clear_errno();
00310 struct icalrecurrencetype recur = icalrecurrencetype_from_string( rrule.latin1() );
00311 if ( icalerrno != ICAL_NO_ERROR ) {
00312 kdDebug(5800) << "Recurrence parsing error: " << icalerror_strerror( icalerrno ) << endl;
00313 success = false;
00314 }
00315
00316 if ( success ) {
00317 mImpl->readRecurrence( recur, recurrence );
00318 }
00319
00320 return success;
00321 }
00322
00323
00324 QString ICalFormat::createScheduleMessage(IncidenceBase *incidence,
00325 Scheduler::Method method)
00326 {
00327 icalcomponent *message = 0;
00328
00329
00330 if ( incidence->type() == "Event" || incidence->type() == "Todo" ) {
00331 Incidence* i = static_cast<Incidence*>( incidence );
00332 if ( i->schedulingID() != i->uid() ) {
00333
00334 i = i->clone();
00335 i->setUid( i->schedulingID() );
00336 i->setSchedulingID( QString::null );
00337
00338
00339 message = mImpl->createScheduleComponent( i, method );
00340
00341
00342 delete i;
00343 }
00344 }
00345
00346 if ( message == 0 )
00347 message = mImpl->createScheduleComponent(incidence,method);
00348
00349
00350 QString messageText = QString::fromUtf8( icalcomponent_as_ical_string(message) );
00351
00352 #if 0
00353 kdDebug(5800) << "ICalFormat::createScheduleMessage: message START\n"
00354 << messageText
00355 << "ICalFormat::createScheduleMessage: message END" << endl;
00356 #endif
00357
00358 return messageText;
00359 }
00360
00361 FreeBusy *ICalFormat::parseFreeBusy( const QString &str )
00362 {
00363 clearException();
00364
00365 icalcomponent *message;
00366 message = icalparser_parse_string( str.utf8() );
00367
00368 if ( !message ) return 0;
00369
00370 FreeBusy *freeBusy = 0;
00371
00372 icalcomponent *c;
00373 for ( c = icalcomponent_get_first_component( message, ICAL_VFREEBUSY_COMPONENT );
00374 c != 0; c = icalcomponent_get_next_component( message, ICAL_VFREEBUSY_COMPONENT ) ) {
00375 FreeBusy *fb = mImpl->readFreeBusy( c );
00376
00377 if ( freeBusy ) {
00378 freeBusy->merge( fb );
00379 delete fb;
00380 } else {
00381 freeBusy = fb;
00382 }
00383 }
00384
00385 if ( !freeBusy )
00386 kdDebug(5800) << "ICalFormat:parseFreeBusy: object is not a freebusy."
00387 << endl;
00388 return freeBusy;
00389 }
00390
00391 ScheduleMessage *ICalFormat::parseScheduleMessage( Calendar *cal,
00392 const QString &messageText )
00393 {
00394 setTimeZone( cal->timeZoneId(), !cal->isLocalTime() );
00395 clearException();
00396
00397 if (messageText.isEmpty())
00398 {
00399 setException( new ErrorFormat( ErrorFormat::ParseErrorKcal, QString::fromLatin1( "messageText was empty, unable to parse into a ScheduleMessage" ) ) );
00400 return 0;
00401 }
00402
00403 icalcomponent *message;
00404 message = icalparser_parse_string(messageText.utf8());
00405
00406 if (!message)
00407 {
00408 setException( new ErrorFormat( ErrorFormat::ParseErrorKcal, QString::fromLatin1( "icalparser was unable to parse messageText into a ScheduleMessage" ) ) );
00409 return 0;
00410 }
00411
00412 icalproperty *m = icalcomponent_get_first_property(message,
00413 ICAL_METHOD_PROPERTY);
00414 if (!m)
00415 {
00416 setException( new ErrorFormat( ErrorFormat::ParseErrorKcal, QString::fromLatin1( "message didn't contain an ICAL_METHOD_PROPERTY" ) ) );
00417 return 0;
00418 }
00419
00420 icalcomponent *c;
00421
00422 IncidenceBase *incidence = 0;
00423 c = icalcomponent_get_first_component(message,ICAL_VEVENT_COMPONENT);
00424 if (c) {
00425 icalcomponent *ctz = icalcomponent_get_first_component(message,ICAL_VTIMEZONE_COMPONENT);
00426 incidence = mImpl->readEvent(c, ctz);
00427 }
00428
00429 if (!incidence) {
00430 c = icalcomponent_get_first_component(message,ICAL_VTODO_COMPONENT);
00431 if (c) {
00432 incidence = mImpl->readTodo(c);
00433 }
00434 }
00435
00436 if (!incidence) {
00437 c = icalcomponent_get_first_component(message,ICAL_VJOURNAL_COMPONENT);
00438 if (c) {
00439 incidence = mImpl->readJournal(c);
00440 }
00441 }
00442
00443 if (!incidence) {
00444 c = icalcomponent_get_first_component(message,ICAL_VFREEBUSY_COMPONENT);
00445 if (c) {
00446 incidence = mImpl->readFreeBusy(c);
00447 }
00448 }
00449
00450
00451
00452 if (!incidence) {
00453 kdDebug(5800) << "ICalFormat:parseScheduleMessage: object is not a freebusy, event, todo or journal" << endl;
00454 setException( new ErrorFormat( ErrorFormat::ParseErrorKcal, QString::fromLatin1( "object is not a freebusy, event, todo or journal" ) ) );
00455 return 0;
00456 }
00457
00458 kdDebug(5800) << "ICalFormat::parseScheduleMessage() getting method..." << endl;
00459
00460 icalproperty_method icalmethod = icalproperty_get_method(m);
00461 Scheduler::Method method;
00462
00463 switch (icalmethod) {
00464 case ICAL_METHOD_PUBLISH:
00465 method = Scheduler::Publish;
00466 break;
00467 case ICAL_METHOD_REQUEST:
00468 method = Scheduler::Request;
00469 break;
00470 case ICAL_METHOD_REFRESH:
00471 method = Scheduler::Refresh;
00472 break;
00473 case ICAL_METHOD_CANCEL:
00474 method = Scheduler::Cancel;
00475 break;
00476 case ICAL_METHOD_ADD:
00477 method = Scheduler::Add;
00478 break;
00479 case ICAL_METHOD_REPLY:
00480 method = Scheduler::Reply;
00481 break;
00482 case ICAL_METHOD_COUNTER:
00483 method = Scheduler::Counter;
00484 break;
00485 case ICAL_METHOD_DECLINECOUNTER:
00486 method = Scheduler::Declinecounter;
00487 break;
00488 default:
00489 method = Scheduler::NoMethod;
00490 kdDebug(5800) << "ICalFormat::parseScheduleMessage(): Unknow method" << endl;
00491 break;
00492 }
00493
00494 kdDebug(5800) << "ICalFormat::parseScheduleMessage() restriction..." << endl;
00495
00496 if (!icalrestriction_check(message)) {
00497 kdWarning(5800) << k_funcinfo << endl << "libkcal reported a problem while parsing:" << endl;
00498 kdWarning(5800) << Scheduler::translatedMethodName(method) + ": " + mImpl->extractErrorProperty(c)<< endl;
00499
00500
00501
00502
00503
00504
00505
00506 }
00507 icalcomponent *calendarComponent = mImpl->createCalendarComponent(cal);
00508
00509 Incidence *existingIncidence =
00510 cal->incidenceFromSchedulingID(incidence->uid());
00511 if (existingIncidence) {
00512
00513
00514 if (existingIncidence->type() == "Todo") {
00515 Todo *todo = static_cast<Todo *>(existingIncidence);
00516 icalcomponent_add_component(calendarComponent,
00517 mImpl->writeTodo(todo));
00518 }
00519 if (existingIncidence->type() == "Event") {
00520 Event *event = static_cast<Event *>(existingIncidence);
00521 icalcomponent_add_component(calendarComponent,
00522 mImpl->writeEvent(event));
00523 }
00524 } else {
00525 calendarComponent = 0;
00526 }
00527
00528 kdDebug(5800) << "ICalFormat::parseScheduleMessage() classify..." << endl;
00529
00530 icalproperty_xlicclass result = icalclassify( message, calendarComponent,
00531 (char *)"" );
00532
00533 kdDebug(5800) << "ICalFormat::parseScheduleMessage() returning..." << endl;
00534 kdDebug(5800) << "ICalFormat::parseScheduleMessage(), result = " << result << endl;
00535
00536 ScheduleMessage::Status status;
00537
00538 switch (result) {
00539 case ICAL_XLICCLASS_PUBLISHNEW:
00540 status = ScheduleMessage::PublishNew;
00541 break;
00542 case ICAL_XLICCLASS_PUBLISHUPDATE:
00543 status = ScheduleMessage::PublishUpdate;
00544 break;
00545 case ICAL_XLICCLASS_OBSOLETE:
00546 status = ScheduleMessage::Obsolete;
00547 break;
00548 case ICAL_XLICCLASS_REQUESTNEW:
00549 status = ScheduleMessage::RequestNew;
00550 break;
00551 case ICAL_XLICCLASS_REQUESTUPDATE:
00552 status = ScheduleMessage::RequestUpdate;
00553 break;
00554 case ICAL_XLICCLASS_UNKNOWN:
00555 default:
00556 status = ScheduleMessage::Unknown;
00557 break;
00558 }
00559
00560 kdDebug(5800) << "ICalFormat::parseScheduleMessage(), status = " << status << endl;
00561
00562
00563 return new ScheduleMessage(incidence,method,status);
00564 }
00565
00566 void ICalFormat::setTimeZone( const QString &id, bool utc )
00567 {
00568 mTimeZoneId = id;
00569 mUtc = utc;
00570 }
00571
00572 QString ICalFormat::timeZoneId() const
00573 {
00574 return mTimeZoneId;
00575 }
00576
00577 bool ICalFormat::utc() const
00578 {
00579 return mUtc;
00580 }