KCalendarCore

filestorage.cpp
Go to the documentation of this file.
1/*
2 This file is part of the kcalcore library.
3
4 SPDX-FileCopyrightText: 2002 Cornelius Schumacher <schumacher@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8/**
9 @file
10 This file is part of the API for handling calendar data and
11 defines the FileStorage class.
12
13 @brief
14 This class provides a calendar storage as a local file.
15
16 @author Cornelius Schumacher <schumacher@kde.org>
17*/
18#include "filestorage.h"
19#include "exceptions.h"
20#include "icalformat.h"
21#include "vcalformat.h"
22#include "xcalformat.h"
23
24#include "kcalendarcore_debug.h"
25
26using namespace KCalendarCore;
27
28/*
29 Private class that helps to provide binary compatibility between releases.
30*/
31//@cond PRIVATE
32class Q_DECL_HIDDEN KCalendarCore::FileStorage::Private
33{
34public:
35 Private(const QString &fileName, CalFormat *format)
36 : mFileName(fileName)
37 , mSaveFormat(format)
38 {
39 }
40 ~Private()
41 {
42 delete mSaveFormat;
43 }
44
45 QString mFileName;
46 CalFormat *mSaveFormat = nullptr;
47};
48//@endcond
49
50FileStorage::FileStorage(const Calendar::Ptr &cal, const QString &fileName, CalFormat *format)
51 : CalStorage(cal)
52 , d(new Private(fileName, format))
53{
54}
55
57{
58 delete d;
59}
60
61void FileStorage::setFileName(const QString &fileName)
62{
63 d->mFileName = fileName;
64}
65
67{
68 return d->mFileName;
69}
70
72{
73 delete d->mSaveFormat;
74 d->mSaveFormat = format;
75}
76
78{
79 return d->mSaveFormat;
80}
81
83{
84 return true;
85}
86
88{
89 if (d->mFileName.isEmpty()) {
90 qCWarning(KCALCORE_LOG) << "Empty filename while trying to load";
91 return false;
92 }
93
94 // Always try to load with iCalendar. It will detect, if it is actually a
95 // vCalendar file.
96 bool success;
97 QString productId;
98 // First try the supplied format. Otherwise fall through to iCalendar, then
99 // to vCalendar
100 success = saveFormat() && saveFormat()->load(calendar(), d->mFileName);
101 if (success) {
102 productId = saveFormat()->loadedProductId();
103 } else {
104 ICalFormat iCal;
105
106 success = iCal.load(calendar(), d->mFileName);
107
108 if (success) {
109 productId = iCal.loadedProductId();
110 } else {
111 if (iCal.exception()) {
112 if ((iCal.exception()->code() == Exception::ParseErrorIcal) || (iCal.exception()->code() == Exception::CalVersion1)) {
113 // Possible vCalendar or invalid iCalendar encountered
114 qCDebug(KCALCORE_LOG) << d->mFileName << " is an invalid iCalendar or possibly a vCalendar.";
115 qCDebug(KCALCORE_LOG) << "Try to load it as a vCalendar";
116 VCalFormat vCal;
117 success = vCal.load(calendar(), d->mFileName);
118 productId = vCal.loadedProductId();
119 if (!success) {
120 if (vCal.exception()) {
121 qCWarning(KCALCORE_LOG) << d->mFileName << " is not a valid vCalendar file."
122 << " exception code " << vCal.exception()->code();
123 }
124
125 XCalFormat xCal;
126 success = xCal.load(calendar(), d->mFileName);
127 productId = xCal.loadedProductId();
128 if (!success) {
129 if (xCal.exception()) {
130 qCWarning(KCALCORE_LOG) << d->mFileName << " is not a valid xCalendar file."
131 << " exception code " << xCal.exception()->code() << xCal.exception()->arguments();
132 }
133 return false;
134 }
135 }
136 } else {
137 return false;
138 }
139 } else {
140 qCWarning(KCALCORE_LOG) << "There should be an exception set.";
141 return false;
142 }
143 }
144 }
145
146 calendar()->setProductId(productId);
147 calendar()->setModified(false);
148
149 return true;
150}
151
153{
154 if (d->mFileName.isEmpty()) {
155 return false;
156 }
157
158 CalFormat *format = d->mSaveFormat ? d->mSaveFormat : new ICalFormat;
159
160 bool success = format->save(calendar(), d->mFileName);
161
162 if (success) {
163 calendar()->setModified(false);
164 } else {
165 if (!format->exception()) {
166 qCDebug(KCALCORE_LOG) << "Error. There should be an exception set.";
167 } else {
168 qCDebug(KCALCORE_LOG) << int(format->exception()->code());
169 }
170 }
171
172 if (!d->mSaveFormat) {
173 delete format;
174 }
175
176 return success;
177}
178
180{
181 return true;
182}
183
184#include "moc_filestorage.cpp"
An abstract base class that provides an interface to various calendar formats.
Definition calformat.h:39
QString loadedProductId()
Returns the PRODID string loaded from calendar file.
Definition calformat.cpp:78
Exception * exception() const
Returns an exception, if there is any, containing information about the last error that occurred.
Definition calformat.cpp:57
virtual bool load(const Calendar::Ptr &calendar, const QString &fileName)=0
Loads a calendar on disk into the calendar associated with this format.
virtual bool save(const Calendar::Ptr &calendar, const QString &fileName)=0
Writes the calendar to disk.
An abstract base class that provides a calendar storage interface.
Definition calstorage.h:34
Calendar::Ptr calendar() const
Returns the calendar for this storage object.
virtual QStringList arguments() const
Returns the arguments.
@ ParseErrorIcal
Parse error in libical.
Definition exceptions.h:50
@ CalVersion1
vCalendar v1.0 detected
Definition exceptions.h:53
virtual ErrorCode code() const
Returns the error code.
bool save() override
Saves the calendar.
~FileStorage() override
Destructor.
bool close() override
Closes the calendar storage.
bool load() override
Loads the calendar into memory.
FileStorage(const Calendar::Ptr &calendar, const QString &fileName=QString(), KCalendarCore::CalFormat *format=nullptr)
Constructs a new FileStorage object for Calendar calendar with format format, and storage to file fil...
bool open() override
Opens the calendar for storage.
QString fileName() const
Returns the calendar file name.
CalFormat * saveFormat() const
Returns the CalFormat object used by this storage.
void setSaveFormat(KCalendarCore::CalFormat *format)
Sets the CalFormat object to use for this storage.
void setFileName(const QString &fileName)
Sets the name of the file that contains the calendar data.
iCalendar format implementation.
Definition icalformat.h:45
bool load(const Calendar::Ptr &calendar, const QString &fileName) override
vCalendar format implementation.
Definition vcalformat.h:61
bool load(const Calendar::Ptr &calendar, const QString &fileName) override
Read support for xCal events.
Definition xcalformat.h:27
bool load(const Calendar::Ptr &calendar, const QString &fileName) override
This file is part of the API for handling calendar data and defines the Exception class.
This file is part of the API for handling calendar data and defines the FileStorage class.
This file is part of the API for handling calendar data and defines the ICalFormat class.
Namespace for all KCalendarCore types.
Definition alarm.h:37
This file is part of the API for handling calendar data and defines the VCalFormat base class.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:15:54 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.