KCal 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
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
00049
00050 class KCal::FileStorage::Private
00051 {
00052 public:
00053 Private( const QString &fileName, CalFormat *format )
00054 : mFileName( fileName ),
00055 mSaveFormat( format )
00056 {}
00057 ~Private() { delete mSaveFormat; }
00058
00059 QString mFileName;
00060 CalFormat *mSaveFormat;
00061 };
00062
00063
00064 FileStorage::FileStorage( Calendar *cal, const QString &fileName,
00065 CalFormat *format )
00066 : CalStorage( cal ),
00067 d( new Private( fileName, format ) )
00068 {
00069 }
00070
00071 FileStorage::~FileStorage()
00072 {
00073 delete d;
00074 }
00075
00076 void FileStorage::setFileName( const QString &fileName )
00077 {
00078 d->mFileName = fileName;
00079 }
00080
00081 QString FileStorage::fileName() const
00082 {
00083 return d->mFileName;
00084 }
00085
00086 void FileStorage::setSaveFormat( CalFormat *format )
00087 {
00088 delete d->mSaveFormat;
00089 d->mSaveFormat = format;
00090 }
00091
00092 CalFormat *FileStorage::saveFormat() const
00093 {
00094 return d->mSaveFormat;
00095 }
00096
00097 bool FileStorage::open()
00098 {
00099 return true;
00100 }
00101
00102 bool FileStorage::load()
00103 {
00104
00105
00106 if ( d->mFileName.isEmpty() ) {
00107 return false;
00108 }
00109
00110
00111
00112 bool success;
00113
00114
00115 success = saveFormat() && saveFormat()->load( calendar(), d->mFileName );
00116 if ( !success ) {
00117 ICalFormat iCal;
00118
00119 success = iCal.load( calendar(), d->mFileName );
00120
00121 if ( !success ) {
00122 if ( iCal.exception() ) {
00123 if ( iCal.exception()->errorCode() == ErrorFormat::CalVersion1 ) {
00124
00125 kDebug() << "Fallback to VCalFormat";
00126 VCalFormat vCal;
00127 success = vCal.load( calendar(), d->mFileName );
00128 calendar()->setProductId( vCal.productId() );
00129 } else {
00130 return false;
00131 }
00132 } else {
00133 kDebug() << "Warning! There should be an exception set.";
00134 return false;
00135 }
00136 } else {
00137 calendar()->setProductId( iCal.loadedProductId() );
00138 }
00139 }
00140
00141 calendar()->setModified( false );
00142
00143 return true;
00144 }
00145
00146 bool FileStorage::save()
00147 {
00148 kDebug();
00149 if ( d->mFileName.isEmpty() ) {
00150 return false;
00151 }
00152
00153 CalFormat *format = d->mSaveFormat ? d->mSaveFormat : new ICalFormat;
00154
00155 bool success = format->save( calendar(), d->mFileName );
00156
00157 if ( success ) {
00158 calendar()->setModified( false );
00159 } else {
00160 if ( !format->exception() ) {
00161 kDebug() << "Error. There should be an expection set.";
00162 } else {
00163 kDebug() << format->exception()->message();
00164 }
00165 }
00166
00167 if ( !d->mSaveFormat ) {
00168 delete format;
00169 }
00170
00171 return success;
00172 }
00173
00174 bool FileStorage::close()
00175 {
00176 return true;
00177 }