summaryrefslogtreecommitdiffabout
path: root/libkcal/qtopiaformat.cpp
authorzautrix <zautrix>2004-06-26 19:01:18 (UTC)
committer zautrix <zautrix>2004-06-26 19:01:18 (UTC)
commitb9aad1f15dc600e4dbe4c62d3fcced6363188ba3 (patch) (unidiff)
tree2c3d4004fb21c72cba65793859f9bcd8ffd3a49c /libkcal/qtopiaformat.cpp
downloadkdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip
kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz
kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2
Initial revision
Diffstat (limited to 'libkcal/qtopiaformat.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libkcal/qtopiaformat.cpp333
1 files changed, 333 insertions, 0 deletions
diff --git a/libkcal/qtopiaformat.cpp b/libkcal/qtopiaformat.cpp
new file mode 100644
index 0000000..0a4a031
--- a/dev/null
+++ b/libkcal/qtopiaformat.cpp
@@ -0,0 +1,333 @@
1/*
2 This file is part of libkcal.
3
4 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20*/
21
22#include <qdatetime.h>
23#include <qstring.h>
24#include <qptrlist.h>
25#include <qregexp.h>
26#include <qclipboard.h>
27#include <qfile.h>
28#include <qtextstream.h>
29#include <qxml.h>
30
31#include <kdebug.h>
32#include <klocale.h>
33
34#include "calendar.h"
35#include "calendarlocal.h"
36
37#include "qtopiaformat.h"
38
39using namespace KCal;
40
41class QtopiaParser : public QXmlDefaultHandler
42{
43 public:
44 QtopiaParser( Calendar *calendar ) : mCalendar( calendar ) {
45 oldCategories = 0;
46 }
47
48 bool startElement( const QString &, const QString &, const QString & qName,
49 const QXmlAttributes &attributes )
50 {
51 if ( qName == "event" ) {
52 Event *event = new Event;
53 QString uid = "Qtopia" + attributes.value( "uid" );
54 // event->setUid( uid );
55
56 event->setSummary( attributes.value( "description" ) );
57 event->setLocation( attributes.value( "location" ) );
58 event->setDescription( attributes.value( "note" ) );
59 event->setDtStart( toDateTime( attributes.value( "start" ) ) );
60 event->setDtEnd( toDateTime( attributes.value( "end" ) ) );
61
62 if ( attributes.value( "type" ) == "AllDay" ) {
63 event->setFloats( true );
64 } else {
65 event->setFloats( false );
66 }
67
68 QString rtype = attributes.value( "rtype" );
69 if ( !rtype.isEmpty() ) {
70 QDate startDate = event->dtStart().date();
71
72 QString freqStr = attributes.value( "rfreq" );
73 int freq = freqStr.toInt();
74
75 QString hasEndDateStr = attributes.value( "rhasenddate" );
76 bool hasEndDate = hasEndDateStr == "1";
77
78 QString endDateStr = attributes.value( "enddt" );
79 QDate endDate = toDateTime( endDateStr ).date();
80
81 QString weekDaysStr = attributes.value( "rweekdays" );
82 int weekDaysNum = weekDaysStr.toInt();
83 if ( weekDaysNum == 0 )
84 weekDaysNum = (1 << (event->dtStart().date().dayOfWeek()-1));
85
86 QBitArray weekDays( 7 );
87 weekDays.fill( false );
88 int i;
89 for( i = 0; i < 7; ++i ) {
90 weekDays.setBit( i , ( 1 << i ) & weekDaysNum );
91 qDebug("%d %d %d ",i, weekDaysNum, weekDays.at(i) );
92 }
93
94 QString posStr = attributes.value( "rposition" );
95 int pos = posStr.toInt();
96
97 Recurrence *r = event->recurrence();
98
99 if ( rtype == "Daily" ) {
100 if ( hasEndDate ) r->setDaily( freq, endDate );
101 else r->setDaily( freq, -1 );
102 } else if ( rtype == "Weekly" ) {
103 // fix needed here
104 // rweekdays not set in XML file
105 if ( hasEndDate ) r->setWeekly( freq, weekDays, endDate );
106 else r->setWeekly( freq, weekDays, -1 );
107 } else if ( rtype == "MonthlyDate" ) {
108 if ( hasEndDate )
109 r->setMonthly( Recurrence::rMonthlyDay, freq, endDate );
110 else
111 r->setMonthly( Recurrence::rMonthlyDay, freq, -1 );
112 r->addMonthlyDay( startDate.day() );
113 } else if ( rtype == "MonthlyDay" ) {
114 if ( hasEndDate )
115 r->setMonthly( Recurrence::rMonthlyPos, freq, endDate );
116 else
117 r->setMonthly( Recurrence::rMonthlyPos, freq, -1 );
118 QBitArray days( 7 );
119 days.fill( false );
120 days.setBit( startDate.dayOfWeek() - 1 );
121 r->addMonthlyPos( pos, days );
122 } else if ( rtype == "Yearly" ) {
123 if ( hasEndDate )
124 r->setYearly( Recurrence::rYearlyMonth, freq, endDate );
125 else
126 r->setYearly( Recurrence::rYearlyMonth, freq, -1 );
127 r->addYearlyNum( startDate.month() );
128 }
129 }
130
131 QString categoryList = attributes.value( "categories" );
132 event->setCategories( lookupCategories( categoryList ) );
133
134 QString alarmStr = attributes.value( "alarm" );
135 if ( !alarmStr.isEmpty() ) {
136 Alarm *alarm = new Alarm( event );
137 alarm->setType( Alarm::Display );
138 alarm->setEnabled( true );
139 int alarmOffset = alarmStr.toInt();
140 alarm->setStartOffset( alarmOffset * -60 );
141 event->addAlarm( alarm );
142 }
143 // the following may not be
144 //Event *oldEvent = mCalendar->event( uid );
145 //if ( oldEvent ) mCalendar->deleteEvent( oldEvent );
146
147 mCalendar->addEventNoDup( event );
148 } else if ( qName == "Task" ) {
149 Todo *todo = new Todo;
150
151 QString uid = "Qtopia" + attributes.value( "Uid" );
152 //todo->setUid( uid );
153
154 QString description = attributes.value( "Description" );
155 int pos = description.find( '\n' );
156 if ( pos > 0 ) {
157 QString summary = description.left( pos );
158 todo->setSummary( summary );
159 todo->setDescription( description );
160 } else {
161 todo->setSummary( description );
162 }
163
164 int priority = attributes.value( "Priority" ).toInt();
165 if ( priority == 0 ) priority = 3;
166 todo->setPriority( priority );
167
168 QString categoryList = attributes.value( "Categories" );
169 todo->setCategories( lookupCategories( categoryList ) );
170
171 QString completedStr = attributes.value( "Completed" );
172 if ( completedStr == "1" ) todo->setCompleted( true );
173
174 QString hasDateStr = attributes.value( "HasDate" );
175 if ( hasDateStr == "1" ) {
176 int year = attributes.value( "DateYear" ).toInt();
177 int month = attributes.value( "DateMonth" ).toInt();
178 int day = attributes.value( "DateDay" ).toInt();
179
180 todo->setDtDue( QDateTime( QDate( year, month, day ) ) );
181 todo->setHasDueDate( true );
182 }
183
184 // Todo *oldTodo = mCalendar->todo( uid );
185 //if ( oldTodo ) mCalendar->deleteTodo( oldTodo );
186
187 mCalendar->addTodoNoDup( todo );
188 } else if ( qName == "Category" ) {
189 QString id = attributes.value( "id" );
190 QString name = attributes.value( "name" );
191 setCategory( id, name );
192 }
193
194 return true;
195 }
196
197 bool warning ( const QXmlParseException &exception )
198 {
199 printException( exception );
200 return true;
201 }
202
203 bool error ( const QXmlParseException &exception )
204 {
205 printException( exception );
206 return false;
207 }
208
209 bool fatalError ( const QXmlParseException &exception )
210 {
211 printException( exception );
212 return false;
213 }
214
215 QString errorString ()
216 {
217 return "QtopiaParser: Error!";
218 }
219 void setCategoriesList ( QStringList * c )
220 {
221 oldCategories = c;
222 }
223
224 protected:
225 void printException( const QXmlParseException &exception )
226 {
227 kdError() << "XML Parse Error (line " << exception.lineNumber()
228 << ", col " << exception.columnNumber() << "): "
229 << exception.message() << "(public ID: '"
230 << exception.publicId() << "' system ID: '"
231 << exception.systemId() << "')" << endl;
232 }
233
234 QDateTime toDateTime( const QString &value )
235 {
236 QDateTime dt;
237 dt.setTime_t( value.toUInt() );
238
239 return dt;
240 }
241
242 QStringList lookupCategories( const QString &categoryList )
243 {
244 QStringList categoryIds = QStringList::split( ";", categoryList );
245 QStringList categories;
246 QStringList::ConstIterator it;
247 for( it = categoryIds.begin(); it != categoryIds.end(); ++it ) {
248 QString cate = category( *it );
249 if ( oldCategories ) {
250 if ( ! oldCategories->contains( cate ) )
251 oldCategories->append( cate );
252 }
253 categories.append(cate );
254 }
255 return categories;
256 }
257
258 private:
259 Calendar *mCalendar;
260 QStringList * oldCategories;
261 static QString category( const QString &id )
262 {
263 QMap<QString,QString>::ConstIterator it = mCategoriesMap.find( id );
264 if ( it == mCategoriesMap.end() ) return id;
265 else return *it;
266 }
267
268 static void setCategory( const QString &id, const QString &name )
269 {
270 mCategoriesMap.insert( id, name );
271 }
272
273 static QMap<QString,QString> mCategoriesMap;
274};
275
276QMap<QString,QString> QtopiaParser::mCategoriesMap;
277
278QtopiaFormat::QtopiaFormat()
279{
280 mCategories = 0;
281}
282
283QtopiaFormat::~QtopiaFormat()
284{
285}
286#include <qdom.h>
287bool QtopiaFormat::load( Calendar *calendar, const QString &fileName )
288{
289 clearException();
290 // qDebug("load QtopiaFormat: %s ",fileName.latin1() );
291 QtopiaParser handler( calendar );
292 handler.setCategoriesList( mCategories );
293 QFile xmlFile( fileName );
294 QXmlInputSource source( xmlFile );
295 QXmlSimpleReader reader;
296 reader.setContentHandler( &handler );
297 return reader.parse( source );
298}
299
300bool QtopiaFormat::save( Calendar *calendar, const QString &fileName )
301{
302
303 clearException();
304
305 QString text = toString( calendar );
306
307 if ( text.isNull() ) return false;
308
309 // TODO: write backup file
310
311 QFile file( fileName );
312 if (!file.open( IO_WriteOnly ) ) {
313 setException(new ErrorFormat(ErrorFormat::SaveError,
314 i18n("Could not open file '%1'").arg(fileName)));
315 return false;
316 }
317 QTextStream ts( &file );
318 ts << text;
319 file.close();
320
321 return true;
322}
323
324bool QtopiaFormat::fromString( Calendar *, const QString & )
325{
326
327 return false;
328}
329
330QString QtopiaFormat::toString( Calendar * )
331{
332 return QString::null;
333}