libkcal
resourcelocaldir.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <typeinfo>
00023 #include <stdlib.h>
00024
00025 #include <qdatetime.h>
00026 #include <qstring.h>
00027 #include <qptrlist.h>
00028
00029 #include <kdebug.h>
00030 #include <klocale.h>
00031 #include <kurl.h>
00032 #include <kconfig.h>
00033 #include <kstandarddirs.h>
00034
00035 #include "vcaldrag.h"
00036 #include "vcalformat.h"
00037 #include "icalformat.h"
00038 #include "exceptions.h"
00039 #include "calendarlocal.h"
00040 #include "incidence.h"
00041 #include "event.h"
00042 #include "todo.h"
00043 #include "journal.h"
00044 #include "filestorage.h"
00045
00046 #include <kresources/configwidget.h>
00047
00048 #include "resourcelocaldirconfig.h"
00049
00050 #include "resourcelocaldir.h"
00051
00052 using namespace KCal;
00053
00054 ResourceLocalDir::ResourceLocalDir( const KConfig* config )
00055 : ResourceCached( config ), mLock( 0 )
00056 {
00057 if ( config ) {
00058 readConfig( config );
00059 }
00060
00061 init();
00062 }
00063
00064 ResourceLocalDir::ResourceLocalDir( const QString& dirName )
00065 : ResourceCached( 0 )
00066 {
00067 mURL = KURL( dirName );
00068
00069 init();
00070 }
00071
00072
00073 void ResourceLocalDir::readConfig( const KConfig *config )
00074 {
00075 QString url = config->readPathEntry( "CalendarURL" );
00076 mURL = KURL( url );
00077 }
00078
00079 void ResourceLocalDir::writeConfig( KConfig *config )
00080 {
00081 kdDebug(5800) << "ResourceLocalDir::writeConfig()" << endl;
00082
00083 ResourceCalendar::writeConfig( config );
00084
00085 config->writePathEntry( "CalendarURL", mURL.prettyURL() );
00086 }
00087
00088 void ResourceLocalDir::init()
00089 {
00090 setType( "dir" );
00091
00092 setSavePolicy( SaveDelayed );
00093
00094 connect( &mDirWatch, SIGNAL( dirty( const QString & ) ),
00095 SLOT( reload( const QString & ) ) );
00096 connect( &mDirWatch, SIGNAL( created( const QString & ) ),
00097 SLOT( reload( const QString & ) ) );
00098 connect( &mDirWatch, SIGNAL( deleted( const QString & ) ),
00099 SLOT( reload( const QString & ) ) );
00100
00101 mLock = new KABC::Lock( mURL.path() );
00102
00103 mDirWatch.addDir( mURL.path(), true );
00104 mDirWatch.startScan();
00105 }
00106
00107
00108 ResourceLocalDir::~ResourceLocalDir()
00109 {
00110 close();
00111
00112 delete mLock;
00113 }
00114
00115 bool ResourceLocalDir::doLoad()
00116 {
00117 kdDebug(5800) << "ResourceLocalDir::load()" << endl;
00118
00119 mCalendar.close();
00120 QString dirName = mURL.path();
00121 bool success = true;
00122
00123 if ( !( KStandardDirs::exists( dirName ) || KStandardDirs::exists( dirName + "/") ) ) {
00124 kdDebug(5800) << "ResourceLocalDir::load(): Directory '" << dirName << "' doesn't exist yet. Creating it..." << endl;
00125
00126
00127
00128
00129 success = KStandardDirs::makeDir( dirName, 0775 );
00130 } else {
00131
00132 kdDebug(5800) << "ResourceLocalDir::load(): '" << dirName << "'" << endl;
00133 QDir dir( dirName );
00134
00135 QStringList entries = dir.entryList( QDir::Files | QDir::Readable );
00136
00137 QStringList::ConstIterator it;
00138 for( it = entries.begin(); it != entries.end(); ++it ) {
00139 if ( (*it).endsWith( "~" ) )
00140 continue;
00141
00142 QString fileName = dirName + "/" + *it;
00143 kdDebug(5800) << " read '" << fileName << "'" << endl;
00144 CalendarLocal cal( mCalendar.timeZoneId() );
00145 if ( !doFileLoad( cal, fileName ) ) {
00146 success = false;
00147 }
00148 }
00149 }
00150
00151 return success;
00152 }
00153
00154 bool ResourceLocalDir::doFileLoad( CalendarLocal &cal, const QString &fileName )
00155 {
00156 if ( !cal.load( fileName ) )
00157 return false;
00158 Incidence::List incidences = cal.rawIncidences();
00159 Incidence::List::ConstIterator it;
00160 for ( it = incidences.constBegin(); it != incidences.constEnd(); ++it ) {
00161 Incidence *i = *it;
00162 if ( i ) mCalendar.addIncidence( i->clone() );
00163 }
00164 return true;
00165 }
00166
00167 bool ResourceLocalDir::doSave()
00168 {
00169 Incidence::List list;
00170
00171 list = addedIncidences();
00172 for (Incidence::List::iterator it = list.begin(); it != list.end(); ++it)
00173 doSave(*it);
00174
00175 list = changedIncidences();
00176 for (Incidence::List::iterator it = list.begin(); it != list.end(); ++it)
00177 doSave(*it);
00178
00179 return true;
00180 }
00181
00182 bool ResourceLocalDir::doSave( Incidence *incidence )
00183 {
00184 mDirWatch.stopScan();
00185
00186 QString fileName = mURL.path() + "/" + incidence->uid();
00187 kdDebug(5800) << "writing '" << fileName << "'" << endl;
00188
00189 CalendarLocal cal( mCalendar.timeZoneId() );
00190 cal.addIncidence( incidence->clone() );
00191 cal.save( fileName );
00192
00193 mDirWatch.startScan();
00194
00195 return true;
00196 }
00197
00198 KABC::Lock *ResourceLocalDir::lock()
00199 {
00200 return mLock;
00201 }
00202
00203 void ResourceLocalDir::reload( const QString &file )
00204 {
00205 kdDebug(5800) << "ResourceLocalDir::reload()" << endl;
00206
00207 if ( !isOpen() ) return;
00208
00209 kdDebug(5800) << " File: '" << file << "'" << endl;
00210
00211 mCalendar.close();
00212 load();
00213
00214 emit resourceChanged( this );
00215 }
00216
00217
00218 bool ResourceLocalDir::deleteEvent(Event *event)
00219 {
00220 kdDebug(5800) << "ResourceLocalDir::deleteEvent" << endl;
00221 if ( deleteIncidenceFile(event) )
00222 return( mCalendar.deleteEvent( event ) );
00223 else
00224 return( false );
00225 }
00226
00227
00228 bool ResourceLocalDir::deleteTodo(Todo *todo)
00229 {
00230 if ( deleteIncidenceFile(todo) )
00231 return( mCalendar.deleteTodo( todo ) );
00232 else
00233 return( false );
00234 }
00235
00236
00237 bool ResourceLocalDir::deleteJournal( Journal *journal )
00238 {
00239 if ( deleteIncidenceFile( journal ) )
00240 return( mCalendar.deleteJournal( journal ) );
00241 else
00242 return( false );
00243 }
00244
00245
00246 void ResourceLocalDir::dump() const
00247 {
00248 ResourceCalendar::dump();
00249 kdDebug(5800) << " Url: " << mURL.url() << endl;
00250 }
00251
00252 bool ResourceLocalDir::deleteIncidenceFile(Incidence *incidence)
00253 {
00254 QFile file( mURL.path() + "/" + incidence->uid() );
00255 if ( !file.exists() )
00256 return true;
00257
00258 mDirWatch.stopScan();
00259 bool removed = file.remove();
00260 mDirWatch.startScan();
00261 return removed;
00262 }
00263
00264 #include "resourcelocaldir.moc"
|