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 connect( &mDirWatch, SIGNAL( dirty( const QString & ) ),
00093 SLOT( reload( const QString & ) ) );
00094 connect( &mDirWatch, SIGNAL( created( const QString & ) ),
00095 SLOT( reload( const QString & ) ) );
00096 connect( &mDirWatch, SIGNAL( deleted( const QString & ) ),
00097 SLOT( reload( const QString & ) ) );
00098
00099 mLock = new KABC::Lock( mURL.path() );
00100
00101 mDirWatch.addDir( mURL.path(), true );
00102 mDirWatch.startScan();
00103 }
00104
00105
00106 ResourceLocalDir::~ResourceLocalDir()
00107 {
00108 close();
00109
00110 delete mLock;
00111 }
00112
00113 bool ResourceLocalDir::doLoad()
00114 {
00115 kdDebug(5800) << "ResourceLocalDir::load()" << endl;
00116
00117 mCalendar.close();
00118 QString dirName = mURL.path();
00119 bool success = true;
00120
00121 if ( !( KStandardDirs::exists( dirName ) || KStandardDirs::exists( dirName + "/") ) ) {
00122 kdDebug(5800) << "ResourceLocalDir::load(): Directory '" << dirName << "' doesn't exist yet. Creating it..." << endl;
00123
00124
00125
00126
00127 success = KStandardDirs::makeDir( dirName, 0775 );
00128 } else {
00129
00130 kdDebug(5800) << "ResourceLocalDir::load(): '" << dirName << "'" << endl;
00131 QDir dir( dirName );
00132
00133 QStringList entries = dir.entryList( QDir::Files | QDir::Readable );
00134
00135 QStringList::ConstIterator it;
00136 for( it = entries.begin(); it != entries.end(); ++it ) {
00137 if ( (*it).endsWith( "~" ) )
00138 continue;
00139
00140 QString fileName = dirName + "/" + *it;
00141 kdDebug(5800) << " read '" << fileName << "'" << endl;
00142 CalendarLocal cal( mCalendar.timeZoneId() );
00143 if ( !doFileLoad( cal, fileName ) ) {
00144 success = false;
00145 }
00146 }
00147 }
00148
00149 return success;
00150 }
00151
00152 bool ResourceLocalDir::doFileLoad( CalendarLocal &cal, const QString &fileName )
00153 {
00154 if ( !cal.load( fileName ) )
00155 return false;
00156 Incidence::List incidences = cal.rawIncidences();
00157 Incidence::List::ConstIterator it;
00158 for ( it = incidences.constBegin(); it != incidences.constEnd(); ++it ) {
00159 Incidence *i = *it;
00160 if ( i ) mCalendar.addIncidence( i->clone() );
00161 }
00162 return true;
00163 }
00164
00165 bool ResourceLocalDir::doSave()
00166 {
00167 Incidence::List list;
00168
00169 list = addedIncidences();
00170 for (Incidence::List::iterator it = list.begin(); it != list.end(); ++it)
00171 doSave(*it);
00172
00173 list = changedIncidences();
00174 for (Incidence::List::iterator it = list.begin(); it != list.end(); ++it)
00175 doSave(*it);
00176
00177 return true;
00178 }
00179
00180 bool ResourceLocalDir::doSave( Incidence *incidence )
00181 {
00182 mDirWatch.stopScan();
00183
00184 QString fileName = mURL.path() + "/" + incidence->uid();
00185 kdDebug(5800) << "writing '" << fileName << "'" << endl;
00186
00187 CalendarLocal cal( mCalendar.timeZoneId() );
00188 cal.addIncidence( incidence->clone() );
00189 cal.save( fileName );
00190
00191 mDirWatch.startScan();
00192
00193 return true;
00194 }
00195
00196 KABC::Lock *ResourceLocalDir::lock()
00197 {
00198 return mLock;
00199 }
00200
00201 void ResourceLocalDir::reload( const QString &file )
00202 {
00203 kdDebug(5800) << "ResourceLocalDir::reload()" << endl;
00204
00205 if ( !isOpen() ) return;
00206
00207 kdDebug(5800) << " File: '" << file << "'" << endl;
00208
00209 mCalendar.close();
00210 load();
00211
00212 emit resourceChanged( this );
00213 }
00214
00215
00216 bool ResourceLocalDir::deleteEvent(Event *event)
00217 {
00218 kdDebug(5800) << "ResourceLocalDir::deleteEvent" << endl;
00219 if ( deleteIncidenceFile(event) )
00220 return( mCalendar.deleteEvent( event ) );
00221 else
00222 return( false );
00223 }
00224
00225
00226 bool ResourceLocalDir::deleteTodo(Todo *todo)
00227 {
00228 if ( deleteIncidenceFile(todo) )
00229 return( mCalendar.deleteTodo( todo ) );
00230 else
00231 return( false );
00232 }
00233
00234
00235 bool ResourceLocalDir::deleteJournal( Journal *journal )
00236 {
00237 if ( deleteIncidenceFile( journal ) )
00238 return( mCalendar.deleteJournal( journal ) );
00239 else
00240 return( false );
00241 }
00242
00243
00244 void ResourceLocalDir::dump() const
00245 {
00246 ResourceCalendar::dump();
00247 kdDebug(5800) << " Url: " << mURL.url() << endl;
00248 }
00249
00250 bool ResourceLocalDir::deleteIncidenceFile(Incidence *incidence)
00251 {
00252 QFile file( mURL.path() + "/" + incidence->uid() );
00253 if ( !file.exists() )
00254 return true;
00255
00256 mDirWatch.stopScan();
00257 bool removed = file.remove();
00258 mDirWatch.startScan();
00259 return removed;
00260 }
00261
00262 #include "resourcelocaldir.moc"
|