KCalCore Library
filestorage.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00043
00044
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
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
00100
00101 if ( d->mFileName.isEmpty() ) {
00102 return false;
00103 }
00104
00105
00106
00107 bool success;
00108
00109
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
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 }