• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.14.10 API Reference
  • KDE Home
  • Contact Us
 

KCalCore Library

  • kcalcore
filestorage.cpp
Go to the documentation of this file.
1/*
2 This file is part of the kcalcore 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*/
31#include "filestorage.h"
32#include "exceptions.h"
33#include "icalformat.h"
34#include "memorycalendar.h"
35#include "vcalformat.h"
36
37#include <KDebug>
38
39using namespace KCalCore;
40
41/*
42 Private class that helps to provide binary compatibility between releases.
43*/
44//@cond PRIVATE
45class KCalCore::FileStorage::Private
46{
47public:
48 Private(const QString &fileName, CalFormat *format)
49 : mFileName(fileName),
50 mSaveFormat(format)
51 {}
52 ~Private() {
53 delete mSaveFormat;
54 }
55
56 QString mFileName;
57 CalFormat *mSaveFormat;
58};
59//@endcond
60
61FileStorage::FileStorage(const Calendar::Ptr &cal, const QString &fileName,
62 CalFormat *format)
63 : CalStorage(cal),
64 d(new Private(fileName, format))
65{
66}
67
68FileStorage::~FileStorage()
69{
70 delete d;
71}
72
73void FileStorage::setFileName(const QString &fileName)
74{
75 d->mFileName = fileName;
76}
77
78QString FileStorage::fileName() const
79{
80 return d->mFileName;
81}
82
83void FileStorage::setSaveFormat(CalFormat *format)
84{
85 delete d->mSaveFormat;
86 d->mSaveFormat = format;
87}
88
89CalFormat *FileStorage::saveFormat() const
90{
91 return d->mSaveFormat;
92}
93
94bool FileStorage::open()
95{
96 return true;
97}
98
99bool FileStorage::load()
100{
101 if (d->mFileName.isEmpty()) {
102 kWarning() << "Empty filename while trying to load";
103 return false;
104 }
105
106 // Always try to load with iCalendar. It will detect, if it is actually a
107 // vCalendar file.
108 bool success;
109 QString productId;
110 // First try the supplied format. Otherwise fall through to iCalendar, then
111 // to vCalendar
112 success = saveFormat() && saveFormat()->load(calendar(), d->mFileName);
113 if (success) {
114 productId = saveFormat()->loadedProductId();
115 } else {
116 ICalFormat iCal;
117
118 success = iCal.load(calendar(), d->mFileName);
119
120 if (success) {
121 productId = iCal.loadedProductId();
122 } else {
123 if (iCal.exception()) {
124 if (iCal.exception()->code() == Exception::CalVersion1) {
125 // Expected non vCalendar file, but detected vCalendar
126 kDebug() << "Fallback to VCalFormat";
127 VCalFormat vCal;
128 success = vCal.load(calendar(), d->mFileName);
129 productId = vCal.loadedProductId();
130 if (!success) {
131 if (vCal.exception()) {
132 kWarning() << "Exception while importing:" << vCal.exception()->code();
133 }
134 return false;
135 }
136 } else {
137 return false;
138 }
139 } else {
140 kWarning() << "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
152bool FileStorage::save()
153{
154 kDebug();
155 if (d->mFileName.isEmpty()) {
156 return false;
157 }
158
159 CalFormat *format = d->mSaveFormat ? d->mSaveFormat : new ICalFormat;
160
161 bool success = format->save(calendar(), d->mFileName);
162
163 if (success) {
164 calendar()->setModified(false);
165 } else {
166 if (!format->exception()) {
167 kDebug() << "Error. There should be an expection set.";
168 } else {
169 kDebug() << int(format->exception()->code());
170 }
171 }
172
173 if (!d->mSaveFormat) {
174 delete format;
175 }
176
177 return success;
178}
179
180bool FileStorage::close()
181{
182 return true;
183}
KCalCore::CalFormat
An abstract base class that provides an interface to various calendar formats.
Definition calformat.h:49
KCalCore::CalFormat::loadedProductId
QString loadedProductId()
Returns the PRODID string loaded from calendar file.
Definition calformat.cpp:113
KCalCore::CalFormat::load
virtual bool load(const Calendar::Ptr &calendar, const QString &fileName)=0
Loads a calendar on disk into the calendar associated with this format.
KCalCore::CalStorage
An abstract base class that provides a calendar storage interface.
Definition calstorage.h:47
KCalCore::CalStorage::calendar
Calendar::Ptr calendar() const
Returns the calendar for this storage object.
Definition calstorage.cpp:61
KCalCore::Calendar::Ptr
QSharedPointer< Calendar > Ptr
A shared pointer to a Calendar.
Definition calendar.h:138
KCalCore::Exception::CalVersion1
@ CalVersion1
vCalendar v1.0 detected
Definition exceptions.h:64
KCalCore::FileStorage::open
bool open()
Opens the calendar for storage.
Definition filestorage.cpp:94
KCalCore::FileStorage::~FileStorage
virtual ~FileStorage()
Destructor.
Definition filestorage.cpp:68
KCalCore::FileStorage::setSaveFormat
void setSaveFormat(KCalCore::CalFormat *format)
Sets the CalFormat object to use for this storage.
Definition filestorage.cpp:83
KCalCore::FileStorage::FileStorage
FileStorage(const Calendar::Ptr &calendar, const QString &fileName=QString(), KCalCore::CalFormat *format=0)
Constructs a new FileStorage object for Calendar calendar with format format, and storage to file fil...
Definition filestorage.cpp:61
KCalCore::FileStorage::fileName
QString fileName() const
Returns the calendar file name.
Definition filestorage.cpp:78
KCalCore::FileStorage::saveFormat
CalFormat * saveFormat() const
Returns the CalFormat object used by this storage.
Definition filestorage.cpp:89
KCalCore::FileStorage::save
bool save()
Saves the calendar.
Definition filestorage.cpp:152
KCalCore::FileStorage::load
bool load()
Loads the calendar into memory.
Definition filestorage.cpp:99
KCalCore::FileStorage::setFileName
void setFileName(const QString &fileName)
Sets the name of the file that contains the calendar data.
Definition filestorage.cpp:73
KCalCore::FileStorage::close
bool close()
Closes the calendar storage.
Definition filestorage.cpp:180
KCalCore::ICalFormat
iCalendar format implementation.
Definition icalformat.h:59
KCalCore::ICalFormat::save
bool save(const Calendar::Ptr &calendar, const QString &fileName)
Definition icalformat.cpp:104
KCalCore::ICalFormat::load
bool load(const Calendar::Ptr &calendar, const QString &fileName)
Definition icalformat.cpp:79
KCalCore::VCalFormat
vCalendar format implementation.
Definition vcalformat.h:70
KCalCore::VCalFormat::load
bool load(const Calendar::Ptr &calendar, const QString &fileName)
Definition vcalformat.cpp:103
exceptions.h
This file is part of the API for handling calendar data and defines the Exception class.
filestorage.h
This file is part of the API for handling calendar data and defines the FileStorage class.
icalformat.h
This file is part of the API for handling calendar data and defines the ICalFormat class.
memorycalendar.h
This file is part of the API for handling calendar data and defines the MemoryCalendar class.
KCalCore
TODO: KDE5:
Definition alarm.h:47
qHash
static uint qHash(const KDateTime &dt)
Private class that helps to provide binary compatibility between releases.
Definition occurrenceiterator.cpp:157
vcalformat.h
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 Wed Jan 24 2024 00:00:00 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KCalCore Library

Skip menu "KCalCore Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepimlibs-4.14.10 API Reference

Skip menu "kdepimlibs-4.14.10 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal