libkcal
filestorage.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00083
00084
00085
00086 if (mFileName.isEmpty()) return false;
00087
00088
00089
00090 bool success;
00091
00092
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
00101 if ( iCal.exception()->errorCode() == ErrorFormat::CalVersion1 ) {
00102
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
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 }
|