• Skip to content
  • Skip to link menu
KDE 4.2 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 
00049 //@cond PRIVATE
00050 class KCal::FileStorage::Private
00051 {
00052   public:
00053     Private( const QString &fileName, CalFormat *format )
00054       : mFileName( fileName ),
00055         mSaveFormat( format )
00056     {}
00057     ~Private() { delete mSaveFormat; }
00058 
00059     QString mFileName;
00060     CalFormat *mSaveFormat;
00061 };
00062 //@endcond
00063 
00064 FileStorage::FileStorage( Calendar *cal, const QString &fileName,
00065                           CalFormat *format )
00066   : CalStorage( cal ),
00067     d( new Private( fileName, format ) )
00068 {
00069 }
00070 
00071 FileStorage::~FileStorage()
00072 {
00073   delete d;
00074 }
00075 
00076 void FileStorage::setFileName( const QString &fileName )
00077 {
00078   d->mFileName = fileName;
00079 }
00080 
00081 QString FileStorage::fileName() const
00082 {
00083   return d->mFileName;
00084 }
00085 
00086 void FileStorage::setSaveFormat( CalFormat *format )
00087 {
00088   delete d->mSaveFormat;
00089   d->mSaveFormat = format;
00090 }
00091 
00092 CalFormat *FileStorage::saveFormat() const
00093 {
00094   return d->mSaveFormat;
00095 }
00096 
00097 bool FileStorage::open()
00098 {
00099   return true;
00100 }
00101 
00102 bool FileStorage::load()
00103 {
00104   // do we want to silently accept this, or make some noise?  Dunno...
00105   // it is a semantical thing vs. a practical thing.
00106   if ( d->mFileName.isEmpty() ) {
00107     return false;
00108   }
00109 
00110   // Always try to load with iCalendar. It will detect, if it is actually a
00111   // vCalendar file.
00112   bool success;
00113   // First try the supplied format. Otherwise fall through to iCalendar, then
00114   // to vCalendar
00115   success = saveFormat() && saveFormat()->load( calendar(), d->mFileName );
00116   if ( !success ) {
00117     ICalFormat iCal;
00118 
00119     success = iCal.load( calendar(), d->mFileName );
00120 
00121     if ( !success ) {
00122       if ( iCal.exception() ) {
00123         if ( iCal.exception()->errorCode() == ErrorFormat::CalVersion1 ) {
00124           // Expected non vCalendar file, but detected vCalendar
00125           kDebug() << "Fallback to VCalFormat";
00126           VCalFormat vCal;
00127           success = vCal.load( calendar(), d->mFileName );
00128           calendar()->setProductId( vCal.productId() );
00129         } else {
00130           return false;
00131         }
00132       } else {
00133         kDebug() << "Warning! There should be an exception set.";
00134         return false;
00135       }
00136     } else {
00137       calendar()->setProductId( iCal.loadedProductId() );
00138     }
00139   }
00140 
00141   calendar()->setModified( false );
00142 
00143   return true;
00144 }
00145 
00146 bool FileStorage::save()
00147 {
00148   kDebug();
00149   if ( d->mFileName.isEmpty() ) {
00150     return false;
00151   }
00152 
00153   CalFormat *format = d->mSaveFormat ? d->mSaveFormat : new ICalFormat;
00154 
00155   bool success = format->save( calendar(), d->mFileName );
00156 
00157   if ( success ) {
00158     calendar()->setModified( false );
00159   } else {
00160     if ( !format->exception() ) {
00161       kDebug() << "Error. There should be an expection set.";
00162     } else {
00163       kDebug() << format->exception()->message();
00164     }
00165   }
00166 
00167   if ( !d->mSaveFormat ) {
00168     delete format;
00169   }
00170 
00171   return success;
00172 }
00173 
00174 bool FileStorage::close()
00175 {
00176   return true;
00177 }

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
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.7
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