22 #include "qtopiaformat.h"
27 #include <QtCore/QDateTime>
28 #include <QtCore/QString>
29 #include <QtCore/QRegExp>
31 #include <QtCore/QFile>
32 #include <QtCore/QTextStream>
34 #include <QTextDocument>
36 #include <QtXml/QXmlAttributes>
37 #include <QtXml/QXmlDefaultHandler>
38 #include <QtXml/QXmlParseException>
39 #include <QtXml/QXmlInputSource>
40 #include <QtXml/QXmlSimpleReader>
44 #include <kdatetime.h>
49 class QtopiaParser :
public QXmlDefaultHandler
52 QtopiaParser(
Calendar *calendar ) : mCalendar( calendar ) {}
54 bool startElement(
const QString &,
const QString &,
const QString &qName,
55 const QXmlAttributes &attributes )
57 if ( qName ==
"event" ) {
59 QString uid =
"Qtopia" + attributes.value(
"uid" );
62 event->setSummary( attributes.value(
"description" ),
63 Qt::mightBeRichText( attributes.value(
"description" ) ) );
64 event->setLocation( attributes.value(
"location" ),
65 Qt::mightBeRichText( attributes.value(
"location" ) ) );
66 event->setDescription( attributes.value(
"note" ),
67 Qt::mightBeRichText( attributes.value(
"note" ) ) );
68 event->setDtStart( toDateTime( attributes.value(
"start" ) ) );
69 event->setDtEnd( toDateTime( attributes.value(
"end" ) ) );
71 if ( attributes.value(
"type" ) ==
"AllDay" ) {
72 event->setAllDay(
true );
74 event->setAllDay(
false );
77 QString rtype = attributes.value(
"rtype" );
78 if ( !rtype.isEmpty() ) {
79 QDate startDate =
event->dtStart().date();
81 QString freqStr = attributes.value(
"rfreq" );
82 int freq = freqStr.toInt();
84 QString hasEndDateStr = attributes.value(
"rhasenddate" );
85 bool hasEndDate = hasEndDateStr ==
"1";
87 QString endDateStr = attributes.value(
"enddt" );
88 QDate endDate = toDateTime( endDateStr ).date();
90 QString weekDaysStr = attributes.value(
"rweekdays" );
91 int weekDaysNum = weekDaysStr.toInt();
92 QBitArray weekDays( 7 );
94 for ( i = 1; i <= 7; ++i ) {
95 weekDays.setBit( i - 1, ( 2 << i ) & weekDaysNum );
98 QString posStr = attributes.value(
"rposition" );
99 int pos = posStr.toInt();
103 if ( rtype ==
"Daily" ) {
108 }
else if ( rtype ==
"Weekly" ) {
113 }
else if ( rtype ==
"MonthlyDate" ) {
119 }
else if ( rtype ==
"MonthlyDay" ) {
126 days.setBit( startDate.dayOfWeek() - 1 );
128 }
else if ( rtype ==
"Yearly" ) {
136 QString categoryList = attributes.value(
"categories" );
137 event->setCategories( lookupCategories( categoryList ) );
139 QString alarmStr = attributes.value(
"alarm" );
140 if ( !alarmStr.isEmpty() ) {
141 kDebug() <<
"Alarm:" << alarmStr;
145 int alarmOffset = alarmStr.toInt();
147 event->addAlarm( alarm );
150 Event *oldEvent = mCalendar->event( uid );
152 mCalendar->deleteEvent( oldEvent );
155 mCalendar->addEvent( event );
156 }
else if ( qName ==
"Task" ) {
159 QString uid =
"Qtopia" + attributes.value(
"Uid" );
162 QString description = attributes.value(
"Description" );
163 int pos = description.indexOf(
'\n' );
165 QString summary = description.left( pos );
166 todo->
setSummary( summary, Qt::mightBeRichText( summary ) );
167 todo->
setDescription( description, Qt::mightBeRichText( description ) );
169 todo->
setSummary( description, Qt::mightBeRichText( description ) );
172 int priority = attributes.value(
"Priority" ).toInt();
176 QString categoryList = attributes.value(
"Categories" );
179 QString completedStr = attributes.value(
"Completed" );
180 if ( completedStr ==
"1" ) {
184 QString hasDateStr = attributes.value(
"HasDate" );
185 if ( hasDateStr ==
"1" ) {
186 int year = attributes.value(
"DateYear" ).toInt();
187 int month = attributes.value(
"DateMonth" ).toInt();
188 int day = attributes.value(
"DateDay" ).toInt();
190 todo->
setDtDue( KDateTime( QDate( year, month, day ), KDateTime::UTC ) );
194 Todo *oldTodo = mCalendar->todo( uid );
196 mCalendar->deleteTodo( oldTodo );
199 mCalendar->addTodo( todo );
200 }
else if ( qName ==
"Category" ) {
201 QString
id = attributes.value(
"id" );
202 QString name = attributes.value(
"name" );
203 setCategory(
id, name );
209 bool warning (
const QXmlParseException &exception )
211 kDebug() <<
"WARNING";
212 printException( exception );
216 bool error (
const QXmlParseException &exception )
219 printException( exception );
223 bool fatalError (
const QXmlParseException &exception )
225 kDebug() <<
"FATALERROR";
226 printException( exception );
230 QString errorString ()
const
232 return "QtopiaParser: Error!";
236 void printException(
const QXmlParseException &exception )
238 kError() <<
"XML Parse Error (line" << exception.lineNumber()
239 <<
", col" << exception.columnNumber() <<
"):"
240 << exception.message() <<
"(public ID: '"
241 << exception.publicId() <<
"' system ID: '"
242 << exception.systemId() <<
"')";
245 KDateTime toDateTime(
const QString &value )
248 dt.setTime_t( value.toUInt() );
253 QStringList lookupCategories(
const QString &categoryList )
255 const QStringList categoryIds = categoryList.split(
';' );
256 QStringList categories;
257 QStringList::ConstIterator it;
258 for ( it = categoryIds.constBegin(); it != categoryIds.constEnd(); ++it ) {
259 categories.append( category( *it ) );
267 static QString category(
const QString &
id )
269 QMap<QString,QString>::ConstIterator it = mCategoriesMap.constFind(
id );
270 if ( it == mCategoriesMap.constEnd() ) {
277 static void setCategory(
const QString &
id,
const QString &name )
279 mCategoriesMap.insert(
id, name );
282 static QMap<QString,QString> mCategoriesMap;
285 QMap<QString,QString> QtopiaParser::mCategoriesMap;
288 QtopiaFormat::QtopiaFormat() : d( 0 )
292 QtopiaFormat::~QtopiaFormat()
298 kDebug() << fileName;
302 QtopiaParser handler( calendar );
303 QFile xmlFile( fileName );
304 QXmlInputSource source( &xmlFile );
305 QXmlSimpleReader reader;
306 reader.setContentHandler( &handler );
307 return reader.parse( source );
312 kDebug() << fileName;
316 QString text =
toString( calendar );
318 if ( text.isNull() ) {
324 QFile file( fileName );
325 if (!file.open( QIODevice::WriteOnly ) ) {
327 i18n(
"Could not open file '%1'", fileName ) ) );
330 QTextStream ts( &file );
339 kDebug() <<
"not yet implemented.";
345 kDebug() <<
"not yet implemented.";