• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.1 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() { delete mSaveFormat; }
53 
54  QString mFileName;
55  CalFormat *mSaveFormat;
56 };
57 //@endcond
58 
59 FileStorage::FileStorage( const Calendar::Ptr &cal, const QString &fileName,
60  CalFormat *format )
61  : CalStorage( cal ),
62  d( new Private( fileName, format ) )
63 {
64 }
65 
66 FileStorage::~FileStorage()
67 {
68  delete d;
69 }
70 
71 void FileStorage::setFileName( const QString &fileName )
72 {
73  d->mFileName = fileName;
74 }
75 
76 QString FileStorage::fileName() const
77 {
78  return d->mFileName;
79 }
80 
81 void FileStorage::setSaveFormat( CalFormat *format )
82 {
83  delete d->mSaveFormat;
84  d->mSaveFormat = format;
85 }
86 
87 CalFormat *FileStorage::saveFormat() const
88 {
89  return d->mSaveFormat;
90 }
91 
92 bool FileStorage::open()
93 {
94  return true;
95 }
96 
97 bool FileStorage::load()
98 {
99  // do we want to silently accept this, or make some noise? Dunno...
100  // it is a semantical thing vs. a practical thing.
101  if ( d->mFileName.isEmpty() ) {
102  return false;
103  }
104 
105  // Always try to load with iCalendar. It will detect, if it is actually a
106  // vCalendar file.
107  bool success;
108  QString productId;
109  // First try the supplied format. Otherwise fall through to iCalendar, then
110  // to vCalendar
111  success = saveFormat() && saveFormat()->load( calendar(), d->mFileName );
112  if ( success ) {
113  productId = saveFormat()->loadedProductId();
114  } else {
115  ICalFormat iCal;
116 
117  success = iCal.load( calendar(), d->mFileName );
118 
119  if ( success ) {
120  productId = iCal.loadedProductId();
121  } else {
122  if ( iCal.exception() ) {
123  if ( iCal.exception()->code() == Exception::CalVersion1 ) {
124  // Expected non vCalendar file, but detected vCalendar
125  kDebug() << "Fallback to VCalFormat";
126  VCalFormat vCal;
127  success = vCal.load( calendar(), d->mFileName );
128  productId = vCal.loadedProductId();
129  } else {
130  return false;
131  }
132  } else {
133  kDebug() << "Warning! There should be an exception set.";
134  return false;
135  }
136  }
137  }
138 
139  calendar()->setProductId( productId );
140  calendar()->setModified( false );
141 
142  return true;
143 }
144 
145 bool FileStorage::save()
146 {
147  kDebug();
148  if ( d->mFileName.isEmpty() ) {
149  return false;
150  }
151 
152  CalFormat *format = d->mSaveFormat ? d->mSaveFormat : new ICalFormat;
153 
154  bool success = format->save( calendar(), d->mFileName );
155 
156  if ( success ) {
157  calendar()->setModified( false );
158  } else {
159  if ( !format->exception() ) {
160  kDebug() << "Error. There should be an expection set.";
161  } else {
162  kDebug() << int( format->exception()->code() );
163  }
164  }
165 
166  if ( !d->mSaveFormat ) {
167  delete format;
168  }
169 
170  return success;
171 }
172 
173 bool FileStorage::close()
174 {
175  return true;
176 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Sep 24 2012 09:01:24 by doxygen 1.8.1.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.9.1 API Reference

Skip menu "kdepimlibs-4.9.1 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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