summaryrefslogtreecommitdiffabout
path: root/libkcal/qtopiaformat.cpp
blob: 2dfe1a45009b5190b7a452b1d6a2eb6624e0822f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
    This file is part of libkcal.

    Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/

#include <qdatetime.h>
#include <qstring.h>
#include <q3ptrlist.h>
#include <qregexp.h>
#include <qclipboard.h>
#include <qfile.h>
#include <q3textstream.h>
#include <qxml.h>

#include <kdebug.h>
#include <klocale.h>

#include "calendar.h"
#include "calendarlocal.h"

#include "qtopiaformat.h"

using namespace KCal;

class QtopiaParser : public QXmlDefaultHandler
{
  public:
    QtopiaParser( Calendar *calendar ) : mCalendar( calendar ) {
        oldCategories = 0;
    }
  
    bool startElement( const QString &, const QString &, const QString & qName, 
                       const QXmlAttributes &attributes )
    {
        if ( qName == "event" ) {
            Event *event = new Event;
            QString uid = "Qtopia" + attributes.value( "uid" );
            // event->setUid( uid );

            event->setSummary( attributes.value( "description" ) );
            event->setLocation( attributes.value( "location" ) );
            event->setDescription( attributes.value( "note" ) );
            event->setDtStart( toDateTime( attributes.value( "start" ) ) );
            event->setDtEnd( toDateTime( attributes.value( "end" ) ) );

            if ( attributes.value( "type" ) == "AllDay" ) {
                event->setFloats( true );
            } else {
                event->setFloats( false );
            }

            QString rtype = attributes.value( "rtype" );
            if ( !rtype.isEmpty() ) {
                QDate startDate = event->dtStart().date();
        
                QString freqStr = attributes.value( "rfreq" );
                int freq = freqStr.toInt();

                QString hasEndDateStr = attributes.value( "rhasenddate" );
                bool hasEndDate = hasEndDateStr == "1";

                QString endDateStr = attributes.value( "enddt" );
                QDate endDate = toDateTime( endDateStr ).date();

                QString weekDaysStr = attributes.value( "rweekdays" );
                int weekDaysNum = weekDaysStr.toInt();
                if ( weekDaysNum == 0 )
                    weekDaysNum = (1 << (event->dtStart().date().dayOfWeek()-1));
               
                QBitArray weekDays( 7 );
                weekDays.fill( false );
                int i;
                for( i = 0; i < 7; ++i ) {
                    weekDays.setBit( i , ( 1 << i ) & weekDaysNum ); 
                    qDebug("%d %d %d ",i, weekDaysNum, weekDays.at(i) );
                }

                QString posStr = attributes.value( "rposition" );
                int pos = posStr.toInt();

                Recurrence *r = event->recurrence();

                if ( rtype == "Daily" ) {
                    if ( hasEndDate ) r->setDaily( freq, endDate );
                    else r->setDaily( freq, -1 );
                } else if ( rtype == "Weekly" ) {
                    // fix needed here
                    // rweekdays not set in XML file
                    if ( hasEndDate ) r->setWeekly( freq, weekDays, endDate );
                    else r->setWeekly( freq, weekDays, -1 );
                } else if ( rtype == "MonthlyDate" ) {
                    if ( hasEndDate )
                        r->setMonthly( Recurrence::rMonthlyDay, freq, endDate );
                    else
                        r->setMonthly( Recurrence::rMonthlyDay, freq, -1 );
                    r->addMonthlyDay( startDate.day() );
                } else if ( rtype == "MonthlyDay" ) {
                    if ( hasEndDate )
                        r->setMonthly( Recurrence::rMonthlyPos, freq, endDate );
                    else
                        r->setMonthly( Recurrence::rMonthlyPos, freq, -1 );
                    QBitArray days( 7 );
                    days.fill( false );
                    days.setBit( startDate.dayOfWeek() - 1 );
                    r->addMonthlyPos( pos, days );
                } else if ( rtype == "Yearly" ) {
                    if ( hasEndDate )
                        r->setYearly( Recurrence::rYearlyMonth, freq, endDate );
                    else
                        r->setYearly( Recurrence::rYearlyMonth, freq, -1 );
                    r->addYearlyNum( startDate.month() );
                }
            }
        
            QString categoryList = attributes.value( "categories" );
            event->setCategories( lookupCategories( categoryList ) );

            QString alarmStr = attributes.value( "alarm" );
            if ( !alarmStr.isEmpty() ) {
                Alarm *alarm = new Alarm( event );
                alarm->setType( Alarm::Display );
                alarm->setEnabled( true );
                int alarmOffset = alarmStr.toInt();
                alarm->setStartOffset( alarmOffset * -60 );
                event->addAlarm( alarm );
            }
            // the following may not be
            //Event *oldEvent = mCalendar->event( uid );
            //if ( oldEvent ) mCalendar->deleteEvent( oldEvent );

            mCalendar->addEventNoDup( event );
        } else if ( qName == "Task" ) {
            Todo *todo = new Todo;

            QString uid = "Qtopia" + attributes.value( "Uid" );
            //todo->setUid( uid );
        
            QString description = attributes.value( "Description" );
            int pos = description.find( '\n' );
            if ( pos > 0 ) {
                QString summary = description.left( pos );
                todo->setSummary( summary );
                todo->setDescription( description );
            } else {
                todo->setSummary( description );
            }
        
            int priority = attributes.value( "Priority" ).toInt();
            if ( priority == 0 ) priority = 3;
            todo->setPriority( priority );
        
            QString categoryList = attributes.value( "Categories" );
            todo->setCategories( lookupCategories( categoryList ) );
        
            QString completedStr = attributes.value( "Completed" );
            if ( completedStr == "1" ) todo->setCompleted( true );
        
            QString hasDateStr = attributes.value( "HasDate" );
            if ( hasDateStr == "1" ) {
                int year = attributes.value( "DateYear" ).toInt();
                int month = attributes.value( "DateMonth" ).toInt();
                int day = attributes.value( "DateDay" ).toInt();
          
                todo->setDtDue( QDateTime( QDate( year, month, day ) ) );
                todo->setHasDueDate( true );
            }
        
            // Todo *oldTodo = mCalendar->todo( uid );
            //if ( oldTodo ) mCalendar->deleteTodo( oldTodo );

            mCalendar->addTodoNoDup( todo );
        } else if ( qName == "Category" ) {
            QString id = attributes.value( "id" );
            QString name = attributes.value( "name" );
            setCategory( id, name );
        }
      
        return true;
    }

    bool warning ( const QXmlParseException &exception )
    {
      printException( exception );
      return true;
    }
 
    bool error ( const QXmlParseException &exception )
    {
      printException( exception );
      return false;
    }
 
    bool fatalError ( const QXmlParseException &exception )
    {
      printException( exception );
      return false;
    }
 
    QString errorString ()
    {
      return "QtopiaParser: Error!";
    }
    void  setCategoriesList ( QStringList * c )
    {
        oldCategories = c;
    }

  protected:
    void printException( const QXmlParseException &exception )
    {
      kdError() << "XML Parse Error (line " << exception.lineNumber()
                << ", col " << exception.columnNumber() << "): "
                << exception.message() << "(public ID: '"
                << exception.publicId() << "' system ID: '"
                << exception.systemId() << "')" << endl;
    }

    QDateTime toDateTime( const QString &value )
    {
      QDateTime dt;
      dt.setTime_t( value.toUInt() );
      
      return dt;
    }

    QStringList lookupCategories( const QString &categoryList )
    {
      QStringList categoryIds = QStringList::split( ";", categoryList );
      QStringList categories;
      QStringList::ConstIterator it;
      for( it = categoryIds.begin(); it != categoryIds.end(); ++it ) {
          QString cate = category( *it );
          if ( oldCategories ) {
              if ( ! oldCategories->contains( cate ) )
                  oldCategories->append( cate );
              }
          categories.append(cate );
      }
      return categories;
    }

  private:
    Calendar *mCalendar;
    QStringList * oldCategories;
    static QString category( const QString &id )
    {
      QMap<QString,QString>::ConstIterator it = mCategoriesMap.find( id );
      if ( it == mCategoriesMap.end() ) return id;
      else return *it;
    }

    static void setCategory( const QString &id, const QString &name )
    {
      mCategoriesMap.insert( id, name );
    }

    static QMap<QString,QString> mCategoriesMap;
};

QMap<QString,QString> QtopiaParser::mCategoriesMap;

QtopiaFormat::QtopiaFormat()
{
    mCategories = 0;
}

QtopiaFormat::~QtopiaFormat()
{
}
#include <qdom.h> 
bool QtopiaFormat::load( Calendar *calendar, const QString &fileName )
{
  clearException();
  // qDebug("load QtopiaFormat: %s ",fileName.latin1() );
  QtopiaParser handler( calendar );
  handler.setCategoriesList( mCategories );
  QFile xmlFile( fileName );
  QXmlInputSource source( xmlFile );
  QXmlSimpleReader reader;
  reader.setContentHandler( &handler );
  return reader.parse( source );
}

bool QtopiaFormat::save( Calendar *calendar, const QString &fileName )
{

  clearException();

  QString text = toString( calendar );

  if ( text.isNull() ) return false;

  // TODO: write backup file

  QFile file( fileName );
  if (!file.open( QIODevice::WriteOnly ) ) {
    setException(new ErrorFormat(ErrorFormat::SaveError,
                 i18n("Could not open file '%1'").arg(fileName)));
    return false;
  }
  Q3TextStream ts( &file );
  ts << text;
  file.close();

  return true;
}

bool QtopiaFormat::fromString( Calendar *, const QString & )
{
 
  return false;
}

QString QtopiaFormat::toString( Calendar * )
{
  return QString::null;
}