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 QString productId; 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 productId = saveFormat()->loadedProductId(); 00118 } else { 00119 ICalFormat iCal; 00120 00121 success = iCal.load( calendar(), d->mFileName ); 00122 00123 if ( success ) { 00124 productId = iCal.loadedProductId(); 00125 } else { 00126 if ( iCal.exception() ) { 00127 if ( iCal.exception()->errorCode() == ErrorFormat::CalVersion1 ) { 00128 // Expected non vCalendar file, but detected vCalendar 00129 kDebug() << "Fallback to VCalFormat"; 00130 VCalFormat vCal; 00131 success = vCal.load( calendar(), d->mFileName ); 00132 productId = vCal.loadedProductId(); 00133 } else { 00134 return false; 00135 } 00136 } else { 00137 kDebug() << "Warning! There should be an exception set."; 00138 return false; 00139 } 00140 } 00141 } 00142 00143 calendar()->setProductId( productId ); 00144 calendar()->setModified( false ); 00145 00146 return true; 00147 } 00148 00149 bool FileStorage::save() 00150 { 00151 kDebug(); 00152 if ( d->mFileName.isEmpty() ) { 00153 return false; 00154 } 00155 00156 CalFormat *format = d->mSaveFormat ? d->mSaveFormat : new ICalFormat; 00157 00158 bool success = format->save( calendar(), d->mFileName ); 00159 00160 if ( success ) { 00161 calendar()->setModified( false ); 00162 } else { 00163 if ( !format->exception() ) { 00164 kDebug() << "Error. There should be an expection set."; 00165 } else { 00166 kDebug() << format->exception()->message(); 00167 } 00168 } 00169 00170 if ( !d->mSaveFormat ) { 00171 delete format; 00172 } 00173 00174 return success; 00175 } 00176 00177 bool FileStorage::close() 00178 { 00179 return true; 00180 }