23 #include "resourcelocaldir.h"
24 #include "resourcelocaldir_p.h"
33 #include <kcal/assignmentvisitor.h>
34 #include <kcal/comparisonvisitor.h>
36 #include <klocalizedstring.h>
38 #include <kstandarddirs.h>
39 #include <kconfiggroup.h>
41 #include <QtCore/QString>
42 #include <QtCore/QDir>
43 #include <QtCore/QFileInfo>
48 #include "moc_resourcelocaldir.cpp"
49 #include "moc_resourcelocaldir_p.cpp"
53 ResourceLocalDir::ResourceLocalDir()
59 ResourceLocalDir::ResourceLocalDir(
const KConfigGroup &group )
66 ResourceLocalDir::ResourceLocalDir(
const QString &dirName )
72 void ResourceLocalDir::readConfig(
const KConfigGroup &group )
74 QString url = group.readPathEntry(
"CalendarURL", QString() );
75 d->mURL = KUrl( url );
78 void ResourceLocalDir::writeConfig( KConfigGroup &group )
82 ResourceCalendar::writeConfig( group );
84 group.writePathEntry(
"CalendarURL", d->mURL.prettyUrl() );
88 void ResourceLocalDir::Private::init()
90 mResource->setType(
"dir" );
92 mResource->setSavePolicy( SaveDelayed );
94 connect( &mDirWatch, SIGNAL(dirty(QString)),
95 this, SLOT(updateIncidenceInCalendar(QString)) );
96 connect( &mDirWatch, SIGNAL(created(QString)),
97 this, SLOT(addIncidenceToCalendar(QString)) );
98 connect( &mDirWatch, SIGNAL(deleted(QString)),
99 this, SLOT(deleteIncidenceFromCalendar(QString)) );
104 mLock =
new KABC::Lock( mURL.path() );
106 mDirWatch.addDir( mURL.path(), KDirWatch::WatchFiles );
107 mDirWatch.startScan();
111 ResourceLocalDir::~ResourceLocalDir()
121 QFileInfo dirInfo( d->mURL.path() );
122 return dirInfo.isDir() && dirInfo.isReadable() &&
123 ( dirInfo.isWritable() ||
readOnly() );
131 QString dirName = d->mURL.path();
133 if ( !( KStandardDirs::exists( dirName ) || KStandardDirs::exists( dirName +
'/' ) ) ) {
134 kDebug() <<
"Directory '" << dirName <<
"' doesn't exist yet. Creating it.";
139 return KStandardDirs::makeDir( dirName, 0775 );
144 QFileInfo dirInfo( dirName );
145 if ( !( dirInfo.isDir() && dirInfo.isReadable() &&
146 ( dirInfo.isWritable() ||
readOnly() ) ) ) {
151 const QStringList entries = dir.entryList( QDir::Files | QDir::Readable );
155 foreach (
const QString &entry, entries ) {
156 if ( d->isTempFile( entry ) ) {
160 const QString fileName = dirName +
'/' + entry;
161 kDebug() <<
" read '" << fileName <<
"'";
163 if ( !doFileLoad( cal, fileName ) ) {
171 bool ResourceLocalDir::doFileLoad(
CalendarLocal &cal,
const QString &fileName )
173 return d->doFileLoad( cal, fileName,
false );
178 Q_UNUSED( syncCache );
182 list = addedIncidences();
183 list += changedIncidences();
185 for ( Incidence::List::iterator it = list.begin(); it != list.end(); ++it ) {
196 if ( d->mDeletedIncidences.contains( incidence ) ) {
197 d->mDeletedIncidences.removeAll( incidence );
201 d->mDirWatch.stopScan();
203 QString fileName = d->mURL.path() +
'/' + incidence->
uid();
204 kDebug() <<
"writing '" << fileName <<
"'";
208 const bool ret = cal.
save( fileName );
210 d->mDirWatch.startScan();
220 void ResourceLocalDir::reload(
const QString &file )
228 if ( d->deleteIncidenceFile( event ) ) {
230 d->mDeletedIncidences.append( event );
247 if ( d->deleteIncidenceFile( todo ) ) {
249 d->mDeletedIncidences.append( todo );
266 if ( d->deleteIncidenceFile( journal ) ) {
268 d->mDeletedIncidences.append( journal );
283 void ResourceLocalDir::dump()
const
286 kDebug() <<
" Url:" << d->mURL.url();
290 bool ResourceLocalDir::Private::deleteIncidenceFile(
Incidence *incidence )
292 QFile file( mURL.path() +
'/' + incidence->
uid() );
293 if ( !file.exists() ) {
297 mDirWatch.stopScan();
298 bool removed = file.remove();
299 mDirWatch.startScan();
303 bool ResourceLocalDir::Private::isTempFile(
const QString &fileName )
const
306 fileName.contains( QRegExp(
"(~|\\.new|\\.tmp)$" ) ) ||
307 QFileInfo( fileName ).fileName().startsWith( QLatin1String(
"qt_temp." ) ) ||
308 fileName == mURL.path();
311 void ResourceLocalDir::Private::addIncidenceToCalendar(
const QString &file )
314 if ( mResource->isOpen() &&
315 !isTempFile( file ) &&
316 !mResource->calendar()->incidence( getUidFromFileName( file ) ) ) {
319 if ( doFileLoad( cal, file,
true ) ) {
320 emit resourceChanged( mResource );
325 void ResourceLocalDir::Private::updateIncidenceInCalendar(
const QString &file )
327 if ( mResource->isOpen() && !isTempFile( file ) ) {
329 if ( doFileLoad( cal, file,
true ) ) {
330 emit resourceChanged( mResource );
335 QString ResourceLocalDir::Private::getUidFromFileName(
const QString &fileName )
337 return QFileInfo( fileName ).fileName();
340 void ResourceLocalDir::Private::deleteIncidenceFromCalendar(
const QString &file )
343 if ( mResource->isOpen() && !isTempFile( file ) ) {
344 Incidence *inc = mResource->calendar()->incidence( getUidFromFileName( file ) );
347 mResource->calendar()->deleteIncidence( inc );
348 emit resourceChanged( mResource );
353 bool ResourceLocalDir::Private::doFileLoad(
CalendarLocal &cal,
354 const QString &fileName,
357 if ( !cal.
load( fileName ) ) {
361 Incidence::List::ConstIterator it;
365 for ( it = incidences.constBegin(); it != incidences.constEnd(); ++it ) {
369 if ( replace && ( inc = mResource->calendar()->incidence( i->
uid() ) ) ) {
370 if ( compVisitor.
compare( i, inc ) ) {
376 bool assignResult = assVisitor.
assign( inc, i );
378 if ( assignResult ) {
381 inc->
setRelatedTo( mResource->calendar()->incidence( uid ) );
387 kWarning() <<
"Incidence (uid=" << inc->
uid()
388 <<
", summary=" << inc->
summary()
389 <<
") changed type. Replacing it.";
391 mResource->calendar()->deleteIncidence( inc );
393 mResource->calendar()->addIncidence( i->
clone() );
397 mResource->calendar()->addIncidence( i->
clone() );
bool save()
Writes the calendar to disk.
This file is part of the API for handling calendar data and defines the CalendarLocal class...
Helper for type correct assignment of incidences via pointers.
KABC::Lock * lock()
Return object for locking the resource.
Provides a To-do in the sense of RFC2445.
QString relatedToUid() const
Returns a UID string for the incidence that is related to this one.
KDateTime::Spec timeSpec() const
Get the viewing time specification (time zone etc.) for the calendar.
void updated()
Call this to notify the observers after the IncidenceBase object has changed.
virtual bool doLoad(bool syncCache)
Do the actual loading of the resource data.
bool deleteJournal(Journal *journal)
bool load(const QString &fileName, CalFormat *format=0)
Loads a calendar on disk in vCalendar or iCalendar format into the current calendar.
This class provides an Event in the sense of RFC2445.
virtual bool doSave(bool syncCache)
Do the actual saving of the resource data.
virtual void dump() const
bool deleteTodo(Todo *todo)
QString uid() const
Returns the unique id (uid) for the incidence.
This class provides a calendar stored as a local file.
This class provides the interfaces for a calendar resource.
bool doOpen()
Opens the resource.
bool compare(IncidenceBase *incidence, const IncidenceBase *reference)
Compares the incidence referenced by incidence to the incidence referenced by reference.
bool deleteEvent(Event *event)
deletes an event from this calendar.
bool assign(IncidenceBase *target, const IncidenceBase *source)
Assigns the incidence referenced by source to the incidence referenced by target, first ensuring that...
This file is part of the API for handling calendar data and defines the Todo class.
Provides the abstract base class common to non-FreeBusy (Events, To-dos, Journals) calendar component...
This class provides a template for lists of pointers.
void close()
Clears out the current calendar, freeing all used memory etc.
Helper for type correct comparison of incidences via pointers.
void deleteAllJournals()
Removes all journals from this calendar.
Provides a Journal in the sense of RFC2445.
bool deleteEvent(Event *event)
virtual bool readOnly() const
void setRelatedTo(Incidence *incidence)
Relates another incidence to this one.
This file is part of the API for handling calendar data and defines the Journal class.
void startUpdates()
Call this when a group of updates is going to be made.
void deleteAllEvents()
Removes all Events from this calendar.
virtual bool addIncidence(Incidence *incidence)
Inserts an Incidence into the calendar.
bool deleteJournal(Journal *journal)
Remove a journal from the journallist.
This file is part of the API for handling calendar data and defines the Event class.
This file is part of the API for handling calendar data and defines the Incidence class...
void deleteAllTodos()
Removes all todos from this calendar.
This class provides a calendar resource using a local CalendarLocal object to cache the calendar data...
virtual Incidence::List rawIncidences()
Returns an unfiltered list of all Incidences for this Calendar.
This class provides a calendar stored as a file per incidence in a directory.
void endUpdates()
Call this when a group of updates is complete, to notify observers that the instance has changed...
virtual Incidence * clone()=0
Returns an exact copy of this incidence.
bool deleteTodo(Todo *todo)
Remove a todo from the todolist.
QString summary() const
Returns the incidence summary.