KCalCore Library
filestorage.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of the kcalcore 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 */ 00031 #include "filestorage.h" 00032 #include "exceptions.h" 00033 #include "icalformat.h" 00034 #include "memorycalendar.h" 00035 #include "vcalformat.h" 00036 00037 #include <KDebug> 00038 00039 using namespace KCalCore; 00040 00041 /* 00042 Private class that helps to provide binary compatibility between releases. 00043 */ 00044 //@cond PRIVATE 00045 class KCalCore::FileStorage::Private 00046 { 00047 public: 00048 Private( const QString &fileName, CalFormat *format ) 00049 : mFileName( fileName ), 00050 mSaveFormat( format ) 00051 {} 00052 ~Private() { delete mSaveFormat; } 00053 00054 QString mFileName; 00055 CalFormat *mSaveFormat; 00056 }; 00057 //@endcond 00058 00059 FileStorage::FileStorage( const Calendar::Ptr &cal, const QString &fileName, 00060 CalFormat *format ) 00061 : CalStorage( cal ), 00062 d( new Private( fileName, format ) ) 00063 { 00064 } 00065 00066 FileStorage::~FileStorage() 00067 { 00068 delete d; 00069 } 00070 00071 void FileStorage::setFileName( const QString &fileName ) 00072 { 00073 d->mFileName = fileName; 00074 } 00075 00076 QString FileStorage::fileName() const 00077 { 00078 return d->mFileName; 00079 } 00080 00081 void FileStorage::setSaveFormat( CalFormat *format ) 00082 { 00083 delete d->mSaveFormat; 00084 d->mSaveFormat = format; 00085 } 00086 00087 CalFormat *FileStorage::saveFormat() const 00088 { 00089 return d->mSaveFormat; 00090 } 00091 00092 bool FileStorage::open() 00093 { 00094 return true; 00095 } 00096 00097 bool FileStorage::load() 00098 { 00099 // do we want to silently accept this, or make some noise? Dunno... 00100 // it is a semantical thing vs. a practical thing. 00101 if ( d->mFileName.isEmpty() ) { 00102 return false; 00103 } 00104 00105 // Always try to load with iCalendar. It will detect, if it is actually a 00106 // vCalendar file. 00107 bool success; 00108 QString productId; 00109 // First try the supplied format. Otherwise fall through to iCalendar, then 00110 // to vCalendar 00111 success = saveFormat() && saveFormat()->load( calendar(), d->mFileName ); 00112 if ( success ) { 00113 productId = saveFormat()->loadedProductId(); 00114 } else { 00115 ICalFormat iCal; 00116 00117 success = iCal.load( calendar(), d->mFileName ); 00118 00119 if ( success ) { 00120 productId = iCal.loadedProductId(); 00121 } else { 00122 if ( iCal.exception() ) { 00123 if ( iCal.exception()->code() == Exception::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 productId = vCal.loadedProductId(); 00129 } else { 00130 return false; 00131 } 00132 } else { 00133 kDebug() << "Warning! There should be an exception set."; 00134 return false; 00135 } 00136 } 00137 } 00138 00139 calendar()->setProductId( productId ); 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() << int( format->exception()->code() ); 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 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 7 2012 23:54:01 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 7 2012 23:54:01 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.