• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

KCal Library

filestorage.cpp

Go to the documentation of this file.
00001 /*
00002   This file is part of the kcal library.
00003 
00004   Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
00005 
00006   This library is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Library General Public
00008   License as published by the Free Software Foundation; either
00009   version 2 of the License, or (at your option) any later version.
00010 
00011   This library is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Library General Public License for more details.
00015 
00016   You should have received a copy of the GNU Library General Public License
00017   along with this library; see the file COPYING.LIB.  If not, write to
00018   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019   Boston, MA 02110-1301, USA.
00020 */
00032 #include "filestorage.h"
00033 #include "calendar.h"
00034 #include "vcalformat.h"
00035 #include "icalformat.h"
00036 
00037 #include <kdebug.h>
00038 
00039 #include <QtCore/QString>
00040 
00041 #include <stdlib.h>
00042 
00043 using namespace KCal;
00044 
00048 //@cond PRIVATE
00049 class KCal::FileStorage::Private
00050 {
00051   public:
00052     Private( const QString &fileName, CalFormat *format )
00053       : mFileName( fileName ),
00054         mSaveFormat( format )
00055     {}
00056     ~Private() { delete mSaveFormat; }
00057 
00058     QString mFileName;
00059     CalFormat *mSaveFormat;
00060 };
00061 //@endcond
00062 
00063 FileStorage::FileStorage( Calendar *cal, const QString &fileName,
00064                           CalFormat *format )
00065   : CalStorage( cal ),
00066     d( new Private( fileName, format ) )
00067 {
00068 }
00069 
00070 FileStorage::~FileStorage()
00071 {
00072   delete d;
00073 }
00074 
00075 void FileStorage::setFileName( const QString &fileName )
00076 {
00077   d->mFileName = fileName;
00078 }
00079 
00080 QString FileStorage::fileName() const
00081 {
00082   return d->mFileName;
00083 }
00084 
00085 void FileStorage::setSaveFormat( CalFormat *format )
00086 {
00087   delete d->mSaveFormat;
00088   d->mSaveFormat = format;
00089 }
00090 
00091 CalFormat *FileStorage::saveFormat() const
00092 {
00093   return d->mSaveFormat;
00094 }
00095 
00096 bool FileStorage::open()
00097 {
00098   return true;
00099 }
00100 
00101 bool FileStorage::load()
00102 {
00103   // do we want to silently accept this, or make some noise?  Dunno...
00104   // it is a semantical thing vs. a practical thing.
00105   if ( d->mFileName.isEmpty() ) {
00106     return false;
00107   }
00108 
00109   // Always try to load with iCalendar. It will detect, if it is actually a
00110   // vCalendar file.
00111   bool success;
00112   // First try the supplied format. Otherwise fall through to iCalendar, then
00113   // to vCalendar
00114   success = saveFormat() && saveFormat()->load( calendar(), d->mFileName );
00115   if ( !success ) {
00116     ICalFormat iCal;
00117 
00118     success = iCal.load( calendar(), d->mFileName );
00119 
00120     if ( !success ) {
00121       if ( iCal.exception() ) {
00122         if ( iCal.exception()->errorCode() == ErrorFormat::CalVersion1 ) {
00123           // Expected non vCalendar file, but detected vCalendar
00124           kDebug() << "Fallback to VCalFormat";
00125           VCalFormat vCal;
00126           success = vCal.load( calendar(), d->mFileName );
00127           calendar()->setProductId( vCal.productId() );
00128         } else {
00129           return false;
00130         }
00131       } else {
00132         kDebug() << "Warning! There should be an exception set.";
00133         return false;
00134       }
00135     } else {
00136       calendar()->setProductId( iCal.loadedProductId() );
00137     }
00138   }
00139 
00140   calendar()->setModified( false );
00141 
00142   return true;
00143 }
00144 
00145 bool FileStorage::save()
00146 {
00147   kDebug();
00148   if ( d->mFileName.isEmpty() ) {
00149     return false;
00150   }
00151 
00152   CalFormat *format = d->mSaveFormat ? d->mSaveFormat : new ICalFormat;
00153 
00154   bool success = format->save( calendar(), d->mFileName );
00155 
00156   if ( success ) {
00157     calendar()->setModified( false );
00158   } else {
00159     if ( !format->exception() ) {
00160       kDebug() << "Error. There should be an expection set.";
00161     } else {
00162       kDebug() << format->exception()->message();
00163     }
00164   }
00165 
00166   if ( !d->mSaveFormat ) {
00167     delete format;
00168   }
00169 
00170   return success;
00171 }
00172 
00173 bool FileStorage::close()
00174 {
00175   return true;
00176 }

KCal Library

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal