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 <[email protected]>
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 <[email protected]>
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 
26 using namespace KCalendarCore;
27 
28 /*
29  Private class that helps to provide binary compatibility between releases.
30 */
31 //@cond PRIVATE
32 class Q_DECL_HIDDEN KCalendarCore::FileStorage::Private
33 {
34 public:
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 
50 FileStorage::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 
61 void 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  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"
virtual bool save(const Calendar::Ptr &calendar, const QString &fileName)=0
Writes the calendar to disk.
bool load() override
Loads the calendar into memory.
Definition: filestorage.cpp:87
Exception base class.
bool load(const Calendar::Ptr &calendar, const QString &fileName) override
Definition: icalformat.cpp:64
~FileStorage() override
Destructor.
Definition: filestorage.cpp:56
bool close() override
Closes the calendar storage.
Calendar::Ptr calendar() const
Returns the calendar for this storage object.
Definition: calstorage.cpp:49
Namespace for all KCalendarCore types.
Definition: alarm.h:36
virtual bool load(const Calendar::Ptr &calendar, const QString &fileName)=0
Loads a calendar on disk into the calendar associated with this format.
virtual ErrorCode code() const
Returns the error code.
Definition: exceptions.cpp:55
CalFormat * saveFormat() const
Returns the CalFormat object used by this storage.
Definition: filestorage.cpp:77
bool open() override
Opens the calendar for storage.
Definition: filestorage.cpp:82
bool load(const Calendar::Ptr &calendar, const QString &fileName) override
Definition: vcalformat.cpp:89
Exception * exception() const
Returns an exception, if there is any, containing information about the last error that occurred.
Definition: calformat.cpp:64
QString loadedProductId()
Returns the PRODID string loaded from calendar file.
Definition: calformat.cpp:85
vCalendar format implementation.
vCalendar format implementation.
Definition: vcalformat.h:60
iCalendar format implementation.
Definition: icalformat.h:44
void setFileName(const QString &fileName)
Sets the name of the file that contains the calendar data.
Definition: filestorage.cpp:61
bool save() override
Saves the calendar.
An abstract base class that provides an interface to various calendar formats.
Definition: calformat.h:38
@ CalVersion1
vCalendar v1.0 detected
Definition: exceptions.h:53
@ ParseErrorIcal
Parse error in libical.
Definition: exceptions.h:50
QString fileName() const
Returns the calendar file name.
Definition: filestorage.cpp:66
An abstract base class that provides a calendar storage interface.
Definition: calstorage.h:33
void setSaveFormat(KCalendarCore::CalFormat *format)
Sets the CalFormat object to use for this storage.
Definition: filestorage.cpp:71
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...
Definition: filestorage.cpp:50
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 21 2023 04:00:45 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.