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 // First try the supplied format. Otherwise fall through to iCalendar, then 00109 // to vCalendar 00110 success = saveFormat() && saveFormat()->load( calendar(), d->mFileName ); 00111 if ( !success ) { 00112 ICalFormat iCal; 00113 00114 success = iCal.load( calendar(), d->mFileName ); 00115 00116 if ( !success ) { 00117 if ( iCal.exception() ) { 00118 if ( iCal.exception()->code() == Exception::CalVersion1 ) { 00119 // Expected non vCalendar file, but detected vCalendar 00120 kDebug() << "Fallback to VCalFormat"; 00121 VCalFormat vCal; 00122 success = vCal.load( calendar(), d->mFileName ); 00123 calendar()->setProductId( vCal.productId() ); 00124 } else { 00125 return false; 00126 } 00127 } else { 00128 kDebug() << "Warning! There should be an exception set."; 00129 return false; 00130 } 00131 } else { 00132 calendar()->setProductId( iCal.loadedProductId() ); 00133 } 00134 } 00135 00136 calendar()->setModified( false ); 00137 00138 return true; 00139 } 00140 00141 bool FileStorage::save() 00142 { 00143 kDebug(); 00144 if ( d->mFileName.isEmpty() ) { 00145 return false; 00146 } 00147 00148 CalFormat *format = d->mSaveFormat ? d->mSaveFormat : new ICalFormat; 00149 00150 bool success = format->save( calendar(), d->mFileName ); 00151 00152 if ( success ) { 00153 calendar()->setModified( false ); 00154 } else { 00155 if ( !format->exception() ) { 00156 kDebug() << "Error. There should be an expection set."; 00157 } else { 00158 kDebug() << int( format->exception()->code() ); 00159 } 00160 } 00161 00162 if ( !d->mSaveFormat ) { 00163 delete format; 00164 } 00165 00166 return success; 00167 } 00168 00169 bool FileStorage::close() 00170 { 00171 return true; 00172 }