• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.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() { 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  if ( d->mFileName.isEmpty() ) {
100  kWarning() << "Empty filename while trying to load";
101  return false;
102  }
103 
104  // Always try to load with iCalendar. It will detect, if it is actually a
105  // vCalendar file.
106  bool success;
107  QString productId;
108  // First try the supplied format. Otherwise fall through to iCalendar, then
109  // to vCalendar
110  success = saveFormat() && saveFormat()->load( calendar(), d->mFileName );
111  if ( success ) {
112  productId = saveFormat()->loadedProductId();
113  } else {
114  ICalFormat iCal;
115 
116  success = iCal.load( calendar(), d->mFileName );
117 
118  if ( success ) {
119  productId = iCal.loadedProductId();
120  } else {
121  if ( iCal.exception() ) {
122  if ( iCal.exception()->code() == Exception::CalVersion1 ) {
123  // Expected non vCalendar file, but detected vCalendar
124  kDebug() << "Fallback to VCalFormat";
125  VCalFormat vCal;
126  success = vCal.load( calendar(), d->mFileName );
127  productId = vCal.loadedProductId();
128  } else {
129  return false;
130  }
131  } else {
132  kWarning() << "There should be an exception set.";
133  return false;
134  }
135  }
136  }
137 
138  calendar()->setProductId( productId );
139  calendar()->setModified( false );
140 
141  return true;
142 }
143 
144 bool FileStorage::save()
145 {
146  kDebug();
147  if ( d->mFileName.isEmpty() ) {
148  return false;
149  }
150 
151  CalFormat *format = d->mSaveFormat ? d->mSaveFormat : new ICalFormat;
152 
153  bool success = format->save( calendar(), d->mFileName );
154 
155  if ( success ) {
156  calendar()->setModified( false );
157  } else {
158  if ( !format->exception() ) {
159  kDebug() << "Error. There should be an expection set.";
160  } else {
161  kDebug() << int( format->exception()->code() );
162  }
163  }
164 
165  if ( !d->mSaveFormat ) {
166  delete format;
167  }
168 
169  return success;
170 }
171 
172 bool FileStorage::close()
173 {
174  return true;
175 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:24:51 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.10.5 API Reference

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