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 "memorycalendar.h"
22#include "vcalformat.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 {
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";
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 return false;
125 }
126 } else {
127 return false;
128 }
129 } else {
130 qCWarning(KCALCORE_LOG) << "There should be an exception set.";
131 return false;
132 }
133 }
134 }
135
136 calendar()->setProductId(productId);
137 calendar()->setModified(false);
138
139 return true;
140}
141
143{
144 if (d->mFileName.isEmpty()) {
145 return false;
146 }
147
148 CalFormat *format = d->mSaveFormat ? d->mSaveFormat : new ICalFormat;
149
150 bool success = format->save(calendar(), d->mFileName);
151
152 if (success) {
153 calendar()->setModified(false);
154 } else {
155 if (!format->exception()) {
156 qCDebug(KCALCORE_LOG) << "Error. There should be an exception set.";
157 } else {
158 qCDebug(KCALCORE_LOG) << int(format->exception()->code());
159 }
160 }
161
162 if (!d->mSaveFormat) {
163 delete format;
164 }
165
166 return success;
167}
168
170{
171 return true;
172}
173
174#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.
@ 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
vCalendar format implementation.
Definition vcalformat.h:61
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.
This file is part of the API for handling calendar data and defines the MemoryCalendar class.
Namespace for all KCalendarCore types.
Definition alarm.h:37
T qobject_cast(QObject *object)
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 Tue Mar 26 2024 11:13:47 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.