1/*
2 This file is part of the kcal library.
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 @file
23 This file is part of the API for handling calendar data and
24 defines the FileStorage class.
25
26 @brief
27 This class provides a calendar storage as a local file.
28
29 @author Cornelius Schumacher \<schumacher@kde.org\>
30*/
31
32#include "filestorage.h"
33#include "calendar.h"
34#include "vcalformat.h"
35#include "icalformat.h"
36
37#include <kdebug.h>
38
39#include <QtCore/QString>
40
41#include <stdlib.h>
42
43using namespace KCal;
44
45/**
46 Private class that helps to provide binary compatibility between releases.
47*/
48//@cond PRIVATE
49class KCal::FileStorage::Private
50{
51 public:
52 Private( const QString &fileName, CalFormat *format )
53 : mFileName( fileName ),
54 mSaveFormat( format )
55 {}
56 ~Private() { delete mSaveFormat; }
57
58 QString mFileName;
59 CalFormat *mSaveFormat;
60};
61//@endcond
62
63FileStorage::FileStorage( Calendar *cal, const QString &fileName,
64 CalFormat *format )
65 : CalStorage( cal ),
66 d( new Private( fileName, format ) )
67{
68}
69
70FileStorage::~FileStorage()
71{
72 delete d;
73}
74
75void FileStorage::setFileName( const QString &fileName )
76{
77 d->mFileName = fileName;
78}
79
80QString FileStorage::fileName() const
81{
82 return d->mFileName;
83}
84
85void FileStorage::setSaveFormat( CalFormat *format )
86{
87 delete d->mSaveFormat;
88 d->mSaveFormat = format;
89}
90
91CalFormat *FileStorage::saveFormat() const
92{
93 return d->mSaveFormat;
94}
95
96bool FileStorage::open()
97{
98 return true;
99}
100
101bool FileStorage::load()
102{
103 // do we want to silently accept this, or make some noise? Dunno...
104 // it is a semantical thing vs. a practical thing.
105 if ( d->mFileName.isEmpty() ) {
106 return false;
107 }
108
109 // Always try to load with iCalendar. It will detect, if it is actually a
110 // vCalendar file.
111 bool success;
112 QString productId;
113 // First try the supplied format. Otherwise fall through to iCalendar, then
114 // to vCalendar
115 success = saveFormat() && saveFormat()->load( calendar(), d->mFileName );
116 if ( success ) {
117 productId = saveFormat()->loadedProductId();
118 } else {
119 ICalFormat iCal;
120
121 success = iCal.load( calendar(), d->mFileName );
122
123 if ( success ) {
124 productId = iCal.loadedProductId();
125 } else {
126 if ( iCal.exception() ) {
127 if ( iCal.exception()->errorCode() == ErrorFormat::CalVersion1 ) {
128 // Expected non vCalendar file, but detected vCalendar
129 kDebug() << "Fallback to VCalFormat";
130 VCalFormat vCal;
131 success = vCal.load( calendar(), d->mFileName );
132 productId = vCal.loadedProductId();
133 } else {
134 return false;
135 }
136 } else {
137 kDebug() << "Warning! There should be an exception set.";
138 return false;
139 }
140 }
141 }
142
143 calendar()->setProductId( productId );
144 calendar()->setModified( false );
145
146 return true;
147}
148
149bool FileStorage::save()
150{
151 kDebug();
152 if ( d->mFileName.isEmpty() ) {
153 return false;
154 }
155
156 CalFormat *format = d->mSaveFormat ? d->mSaveFormat : new ICalFormat;
157
158 bool success = format->save( calendar(), d->mFileName );
159
160 if ( success ) {
161 calendar()->setModified( false );
162 } else {
163 if ( !format->exception() ) {
164 kDebug() << "Error. There should be an expection set.";
165 } else {
166 kDebug() << format->exception()->message();
167 }
168 }
169
170 if ( !d->mSaveFormat ) {
171 delete format;
172 }
173
174 return success;
175}
176
177bool FileStorage::close()
178{
179 return true;
180}
181