libkcal
filestorage.cpp
Go to the documentation of this file.00001 /* 00002 This file is part of libkcal. 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 */ 00021 00022 #include <stdlib.h> 00023 00024 #include <qdatetime.h> 00025 #include <qstring.h> 00026 #include <qptrlist.h> 00027 00028 #include <kdebug.h> 00029 00030 #include "calendar.h" 00031 #include "vcaldrag.h" 00032 #include "vcalformat.h" 00033 #include "icalformat.h" 00034 00035 #include "filestorage.h" 00036 00037 using namespace KCal; 00038 00039 FileStorage::FileStorage( Calendar *cal, const QString &fileName, 00040 CalFormat *format ) 00041 : CalStorage( cal ), 00042 mFileName( fileName ), 00043 mSaveFormat( format ) 00044 { 00045 } 00046 00047 FileStorage::~FileStorage() 00048 { 00049 delete mSaveFormat; 00050 } 00051 00052 void FileStorage::setFileName( const QString &fileName ) 00053 { 00054 mFileName = fileName; 00055 } 00056 00057 QString FileStorage::fileName()const 00058 { 00059 return mFileName; 00060 } 00061 00062 00063 void FileStorage::setSaveFormat( CalFormat *format ) 00064 { 00065 delete mSaveFormat; 00066 mSaveFormat = format; 00067 } 00068 00069 CalFormat *FileStorage::saveFormat()const 00070 { 00071 return mSaveFormat; 00072 } 00073 00074 00075 bool FileStorage::open() 00076 { 00077 return true; 00078 } 00079 00080 bool FileStorage::load() 00081 { 00082 // kdDebug(5800) << "FileStorage::load(): '" << mFileName << "'" << endl; 00083 00084 // do we want to silently accept this, or make some noise? Dunno... 00085 // it is a semantical thing vs. a practical thing. 00086 if (mFileName.isEmpty()) return false; 00087 00088 // Always try to load with iCalendar. It will detect, if it is actually a 00089 // vCalendar file. 00090 bool success; 00091 // First try the supplied format. Otherwise fall through to iCalendar, then 00092 // to vCalendar 00093 success = saveFormat() && saveFormat()->load( calendar(), mFileName ); 00094 if ( !success ) { 00095 ICalFormat iCal; 00096 00097 success = iCal.load( calendar(), mFileName); 00098 if ( !success ) { 00099 if ( iCal.exception() ) { 00100 // kdDebug(5800) << "---Error: " << mFormat->exception()->errorCode() << endl; 00101 if ( iCal.exception()->errorCode() == ErrorFormat::CalVersion1 ) { 00102 // Expected non vCalendar file, but detected vCalendar 00103 kdDebug(5800) << "FileStorage::load() Fallback to VCalFormat" << endl; 00104 VCalFormat vCal; 00105 success = vCal.load( calendar(), mFileName ); 00106 calendar()->setProductId( vCal.productId() ); 00107 } else { 00108 return false; 00109 } 00110 } else { 00111 kdDebug(5800) << "Warning! There should be an exception set." << endl; 00112 return false; 00113 } 00114 } else { 00115 // kdDebug(5800) << "---Success" << endl; 00116 calendar()->setProductId( iCal.loadedProductId() ); 00117 } 00118 } 00119 00120 calendar()->setModified( false ); 00121 00122 return true; 00123 } 00124 00125 bool FileStorage::save() 00126 { 00127 if ( mFileName.isEmpty() ) return false; 00128 00129 CalFormat *format = 0; 00130 if ( mSaveFormat ) format = mSaveFormat; 00131 else format = new ICalFormat; 00132 00133 bool success = format->save( calendar(), mFileName ); 00134 00135 if ( success ) { 00136 calendar()->setModified( false ); 00137 } else { 00138 if ( !format->exception() ) { 00139 kdDebug(5800) << "FileStorage::save(): Error. There should be an exception set." 00140 << endl; 00141 } else { 00142 kdDebug(5800) << "FileStorage::save(): " << format->exception()->message() 00143 << endl; 00144 } 00145 } 00146 00147 if ( !mSaveFormat ) delete format; 00148 00149 return success; 00150 } 00151 00152 bool FileStorage::close() 00153 { 00154 return true; 00155 }