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