• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.5 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 
39 using namespace KCalCore;
40 
41 /*
42  Private class that helps to provide binary compatibility between releases.
43 */
44 //@cond PRIVATE
45 class KCalCore::FileStorage::Private
46 {
47 public:
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 
61 FileStorage::FileStorage(const Calendar::Ptr &cal, const QString &fileName,
62  CalFormat *format)
63  : CalStorage(cal),
64  d(new Private(fileName, format))
65 {
66 }
67 
68 FileStorage::~FileStorage()
69 {
70  delete d;
71 }
72 
73 void FileStorage::setFileName(const QString &fileName)
74 {
75  d->mFileName = fileName;
76 }
77 
78 QString FileStorage::fileName() const
79 {
80  return d->mFileName;
81 }
82 
83 void FileStorage::setSaveFormat(CalFormat *format)
84 {
85  delete d->mSaveFormat;
86  d->mSaveFormat = format;
87 }
88 
89 CalFormat *FileStorage::saveFormat() const
90 {
91  return d->mSaveFormat;
92 }
93 
94 bool FileStorage::open()
95 {
96  return true;
97 }
98 
99 bool 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  } else {
131  return false;
132  }
133  } else {
134  kWarning() << "There should be an exception set.";
135  return false;
136  }
137  }
138  }
139 
140  calendar()->setProductId(productId);
141  calendar()->setModified(false);
142 
143  return true;
144 }
145 
146 bool FileStorage::save()
147 {
148  kDebug();
149  if (d->mFileName.isEmpty()) {
150  return false;
151  }
152 
153  CalFormat *format = d->mSaveFormat ? d->mSaveFormat : new ICalFormat;
154 
155  bool success = format->save(calendar(), d->mFileName);
156 
157  if (success) {
158  calendar()->setModified(false);
159  } else {
160  if (!format->exception()) {
161  kDebug() << "Error. There should be an expection set.";
162  } else {
163  kDebug() << int(format->exception()->code());
164  }
165  }
166 
167  if (!d->mSaveFormat) {
168  delete format;
169  }
170 
171  return success;
172 }
173 
174 bool FileStorage::close()
175 {
176  return true;
177 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Thu Jan 9 2014 17:48:39 by doxygen 1.8.3.1 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.11.5 API Reference

Skip menu "kdepimlibs-4.11.5 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