/* I chose to use libkcal instead of reading the calendar manually. It's easier to maintain this way. */ STRIGI_ENDANALYZER_RETVAL IcsEndAnalyzer::analyze( Strigi::AnalysisResult& idx, Strigi::InputStream* in ) { CalendarLocal cal( QString::fromLatin1( "UTC" ) ); const char* data; //FIXME: large calendars will exhaust memory; incremental loading would be // nice qint32 nread = in->read( data, in->size(), in->size() ); if ( nread <= 0 ) { //kDebug() <<"Reading data from input stream failed"; return Strigi::Error; } ICalFormat ical; if ( !ical.fromRawString( &cal, QByteArray::fromRawData(data, nread) ) ) { VCalFormat vcal; if ( !vcal.fromRawString( &cal, data ) ) { //kDebug() <<"Could not load calendar"; return Strigi::Error; } } idx.addValue( m_factory->field( ProductId ), cal.productId().toUtf8().data() ); idx.addValue( m_factory->field( Events ), (quint32)cal.events().count() ); idx.addValue( m_factory->field( Journals ), (quint32)cal.journals().count() ); Todo::List todos = cal.todos(); // count completed and overdue int completed = 0; int overdue = 0; foreach ( const Todo* todo, todos ) { if ( todo->isCompleted() ) { ++completed; } else if ( todo->hasDueDate() && todo->dtDue().date() < QDate::currentDate() ) { ++overdue; } } idx.addValue( m_factory->field( Todos ), (quint32)todos.count() ); idx.addValue( m_factory->field( TodosCompleted ), (quint32)completed ); idx.addValue( m_factory->field( TodosOverdue ), (quint32)overdue ); cal.close(); return Strigi::Ok; }
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" ); }