bool FileStorage::load()
{
  // do we want to silently accept this, or make some noise?  Dunno...
  // it is a semantical thing vs. a practical thing.
  if ( d->mFileName.isEmpty() ) {
    return false;
  }

  // Always try to load with iCalendar. It will detect, if it is actually a
  // vCalendar file.
  bool success;
  // First try the supplied format. Otherwise fall through to iCalendar, then
  // to vCalendar
  success = saveFormat() && saveFormat()->load( calendar(), d->mFileName );
  if ( !success ) {
    ICalFormat iCal;

    success = iCal.load( calendar(), d->mFileName );

    if ( !success ) {
      if ( iCal.exception() ) {
        if ( iCal.exception()->code() == Exception::CalVersion1 ) {
          // Expected non vCalendar file, but detected vCalendar
          kDebug() << "Fallback to VCalFormat";
          VCalFormat vCal;
          success = vCal.load( calendar(), d->mFileName );
          calendar()->setProductId( vCal.productId() );
        } else {
          return false;
        }
      } else {
        kDebug() << "Warning! There should be an exception set.";
        return false;
      }
    } else {
      calendar()->setProductId( iCal.loadedProductId() );
    }
  }

  calendar()->setModified( false );

  return true;
}
Beispiel #2
0
void KOIncidenceEditor::slotLoadTemplate( const QString& templateName )
{
  CalendarLocal cal( KOPrefs::instance()->mTimeZoneId );
  QString fileName = locateLocal( "data", "korganizer/templates/" + type() + "/" +
      templateName );

  if ( fileName.isEmpty() ) {
    KMessageBox::error( this, i18n("Unable to find template '%1'.")
        .arg( fileName ) );
  } else {
    ICalFormat format;
    if ( !format.load( &cal, fileName ) ) {
      KMessageBox::error( this, i18n("Error loading template file '%1'.")
          .arg( fileName ) );
      return;
    }
  }
  loadTemplate( cal );
}
Beispiel #3
0
void ICalFormatTest::testCharsets()
{
  ICalFormat format;
  const QDate currentDate = QDate::currentDate();
  Event::Ptr event = Event::Ptr( new Event() );
  event->setUid( "12345" );
  event->setDtStart( KDateTime( currentDate ) );
  event->setDtEnd( KDateTime( currentDate.addDays( 1 ) ) );

  // ü
  const QChar latin1_umlaut[] = { 0xFC, '\0' };
  event->setSummary( QString( latin1_umlaut ) );

  // Test if toString( Incidence ) didn't mess charsets
  const QString serialized = format.toString( event.staticCast<Incidence>() );
  const QChar utf_umlaut[] = { 0xC3, 0XBC, '\0' };
  QVERIFY( serialized.toUtf8().contains( QString( utf_umlaut ).toLatin1().constData() ) );
  QVERIFY( !serialized.toUtf8().contains( QString( latin1_umlaut ).toLatin1().constData() ) );
  QVERIFY( serialized.toLatin1().contains( QString( latin1_umlaut ).toLatin1().constData() ) );
  QVERIFY( !serialized.toLatin1().contains( QString( utf_umlaut ).toLatin1().constData() ) );

  // test fromString( QString )
  const QString serializedCalendar =
    "BEGIN:VCALENDAR\nPRODID:-//K Desktop Environment//NONSGML libkcal 3.2//EN\nVERSION:2.0\n" +
    serialized +
    "\nEND:VCALENDAR";

  Incidence::Ptr event2 = format.fromString( serializedCalendar );
  QVERIFY( event->summary() == event2->summary() );
  QVERIFY( event2->summary().toUtf8() ==
           QByteArray( QString( utf_umlaut ).toLatin1().constData() ) );

  // test save()
  MemoryCalendar::Ptr calendar( new MemoryCalendar( "UTC" ) );
  calendar->addIncidence( event );
  QVERIFY( format.save( calendar, "hommer.ics" ) );

  // Make sure hommer.ics is in UTF-8
  QFile file( "hommer.ics" );
  QVERIFY( file.open( QIODevice::ReadOnly | QIODevice::Text ) );

  const QByteArray bytesFromFile = file.readAll();
  QVERIFY( bytesFromFile.contains( QString( utf_umlaut ).toLatin1().constData() ) );
  QVERIFY( !bytesFromFile.contains( QString( latin1_umlaut ).toLatin1().constData() ) );
  file.close();

  // Test load:
  MemoryCalendar::Ptr calendar2( new MemoryCalendar( "UTC" ) );
  QVERIFY( format.load( calendar2, "hommer.ics" ) );
  QVERIFY( calendar2->incidences().count() == 1 );

  // kDebug() << format.toString( event.staticCast<Incidence>() );
  // kDebug() << format.toString( calendar2->incidences().first() );

  Event::Ptr loadedEvent = calendar2->incidences().first().staticCast<Event>();
  QVERIFY( loadedEvent->summary().toUtf8() ==
           QByteArray( QString( utf_umlaut ).toLatin1().constData() ) );
  QVERIFY( *loadedEvent == *event );

  // Test fromRawString()
  MemoryCalendar::Ptr calendar3( new MemoryCalendar( "UTC" ) );
  format.fromRawString( calendar3, bytesFromFile );
  QVERIFY( calendar3->incidences().count() == 1 );
  QVERIFY( *calendar3->incidences().first() == *event );

  unlink( "hommer.ics" );
}