libkcal

filestorage.cpp
1/*
2 This file is part of libkcal.
3
4 Copyright (c) 2002 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., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22#include <stdlib.h>
23
24#include <tqdatetime.h>
25#include <tqstring.h>
26#include <tqptrlist.h>
27
28#include <kdebug.h>
29
30#include "calendar.h"
31#include "vcaldrag.h"
32#include "vcalformat.h"
33#include "icalformat.h"
34
35#include "filestorage.h"
36
37using namespace KCal;
38
39FileStorage::FileStorage( Calendar *cal, const TQString &fileName,
40 CalFormat *format )
41 : CalStorage( cal ),
42 mFileName( fileName ),
43 mSaveFormat( format )
44{
45}
46
47FileStorage::~FileStorage()
48{
49 delete mSaveFormat;
50}
51
52void FileStorage::setFileName( const TQString &fileName )
53{
54 mFileName = fileName;
55}
56
57TQString FileStorage::fileName()const
58{
59 return mFileName;
60}
61
62
64{
65 delete mSaveFormat;
66 mSaveFormat = format;
67}
68
69CalFormat *FileStorage::saveFormat()const
70{
71 return mSaveFormat;
72}
73
74
75bool FileStorage::open()
76{
77 return true;
78}
79
80bool FileStorage::load()
81{
82// kdDebug(5800) << "FileStorage::load(): '" << mFileName << "'" << endl;
83
84 // do we want to silently accept this, or make some noise? Dunno...
85 // it is a semantical thing vs. a practical thing.
86 if (mFileName.isEmpty()) return false;
87
88 // Always try to load with iCalendar. It will detect, if it is actually a
89 // vCalendar file.
90 bool success;
91 // First try the supplied format. Otherwise fall through to iCalendar, then
92 // to vCalendar
93 success = saveFormat() && saveFormat()->load( calendar(), mFileName );
94 if ( !success ) {
95 ICalFormat iCal;
96
97 success = iCal.load( calendar(), mFileName);
98 if ( !success ) {
99 if ( iCal.exception() ) {
100// kdDebug(5800) << "---Error: " << mFormat->exception()->errorCode() << endl;
101 if ( iCal.exception()->errorCode() == ErrorFormat::CalVersion1 ) {
102 // Expected non vCalendar file, but detected vCalendar
103 kdDebug(5800) << "FileStorage::load() Fallback to VCalFormat" << endl;
104 VCalFormat vCal;
105 success = vCal.load( calendar(), mFileName );
106 calendar()->setProductId( vCal.productId() );
107 } else {
108 return false;
109 }
110 } else {
111 kdDebug(5800) << "Warning! There should be an exception set." << endl;
112 return false;
113 }
114 } else {
115// kdDebug(5800) << "---Success" << endl;
116 calendar()->setProductId( iCal.loadedProductId() );
117 }
118 }
119
120 calendar()->setModified( false );
121
122 return true;
123}
124
125bool FileStorage::save()
126{
127 if ( mFileName.isEmpty() ) return false;
128
129 CalFormat *format = 0;
130 if ( mSaveFormat ) format = mSaveFormat;
131 else format = new ICalFormat;
132
133 bool success = format->save( calendar(), mFileName );
134
135 if ( success ) {
136 calendar()->setModified( false );
137 } else {
138 if ( !format->exception() ) {
139 kdDebug(5800) << "FileStorage::save(): Error. There should be an exception set."
140 << endl;
141 } else {
142 kdDebug(5800) << "FileStorage::save(): " << format->exception()->message()
143 << endl;
144 }
145 }
146
147 if ( !mSaveFormat ) delete format;
148
149 return success;
150}
151
152bool FileStorage::close()
153{
154 return true;
155}
Provides the main "calendar" object class.
This is the base class for calendar formats.
Definition: calformat.h:44
const TQString & loadedProductId()
Return the PRODID string loaded from calendar file.
Definition: calformat.h:89
virtual bool load(Calendar *, const TQString &fileName)=0
loads a calendar on disk into the calendar associated with this format.
virtual bool save(Calendar *, const TQString &fileName)=0
writes out the calendar to disk.
ErrorFormat * exception()
Return exception, if there is any, containing information about the last error that occurred.
Definition: calformat.cpp:56
static const TQString & productId()
Return the PRODID string to write into calendar files.
Definition: calformat.h:87
This class provides the interface to the storage of a calendar.
Definition: calstorage.h:34
This is the main "calendar" object class.
Definition: calendar.h:171
void setModified(bool modified)
Set if the Calendar had been modified.
Definition: calendar.cpp:950
void setProductId(const TQString &productId)
Set the Calendar Product ID.
Definition: calendar.cpp:1018
@ CalVersion1
vCalendar v1.0 detected
Definition: exceptions.h:76
TQString message()
Return format error message.
Definition: exceptions.cpp:54
ErrorCodeFormat errorCode()
Return format error code.
Definition: exceptions.cpp:101
void setSaveFormat(CalFormat *)
FileStorage takes ownership of format object.
Definition: filestorage.cpp:63
This class implements the iCalendar format.
Definition: icalformat.h:44
bool load(Calendar *calendar, const TQString &fileName)
Loads a calendar on disk in iCalendar format into calendar.
Definition: icalformat.cpp:78
This class implements the vCalendar format.
Definition: vcalformat.h:45
bool load(Calendar *calendar, const TQString &fileName)
Loads a calendar on disk in vCalendar format into the given calendar.
Definition: vcalformat.cpp:58
Namespace KCal is for global classes, objects and/or functions in libkcal.
Definition: alarm.h:38