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>
43#include <QtCore/QFileInfo>
48#include "moc_resourcelocaldir.cpp"
49#include "moc_resourcelocaldir_p.cpp"
53ResourceLocalDir::ResourceLocalDir()
59ResourceLocalDir::ResourceLocalDir(
const KConfigGroup &group )
66ResourceLocalDir::ResourceLocalDir(
const QString &dirName )
72void ResourceLocalDir::readConfig(
const KConfigGroup &group )
74 QString url = group.readPathEntry(
"CalendarURL", QString() );
75 d->mURL = KUrl( url );
78void ResourceLocalDir::writeConfig( KConfigGroup &group )
82 ResourceCalendar::writeConfig( group );
84 group.writePathEntry(
"CalendarURL", d->mURL.prettyUrl() );
88void 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();
111ResourceLocalDir::~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 ) ) {
171bool 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();
220void 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 );
283void ResourceLocalDir::dump()
const
286 kDebug() <<
" Url:" << d->mURL.url();
290bool 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();
303bool 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();
311void 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 );
325void ResourceLocalDir::Private::updateIncidenceInCalendar(
const QString &file )
327 if ( mResource->isOpen() && !isTempFile( file ) ) {
329 if ( doFileLoad( cal, file,
true ) ) {
330 emit resourceChanged( mResource );
335QString ResourceLocalDir::Private::getUidFromFileName(
const QString &fileName )
337 return QFileInfo( fileName ).fileName();
340void 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 );
353bool 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() );
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.
bool assign(IncidenceBase *target, const IncidenceBase *source)
Assigns the incidence referenced by source to the incidence referenced by target, first ensuring that...
This class provides a calendar stored as a local file.
bool load(const QString &fileName, CalFormat *format=0)
Loads a calendar on disk in vCalendar or iCalendar format into the current calendar.
bool save()
Writes the calendar to disk.
void close()
Clears out the current calendar, freeing all used memory etc.
virtual bool addIncidence(Incidence *incidence)
Inserts an Incidence into the calendar.
virtual Incidence::List rawIncidences()
Returns an unfiltered list of all Incidences for this Calendar.
Helper for type correct comparison of incidences via pointers.
bool compare(IncidenceBase *incidence, const IncidenceBase *reference)
Compares the incidence referenced by incidence to the incidence referenced by reference.
This class provides an Event in the sense of RFC2445.
void updated()
Call this to notify the observers after the IncidenceBase object has changed.
void startUpdates()
Call this when a group of updates is going to be made.
QString uid() const
Returns the unique id (uid) for the incidence.
void endUpdates()
Call this when a group of updates is complete, to notify observers that the instance has changed.
Provides the abstract base class common to non-FreeBusy (Events, To-dos, Journals) calendar component...
QString relatedToUid() const
Returns a UID string for the incidence that is related to this one.
void setRelatedTo(Incidence *incidence)
Relates another incidence to this one.
virtual Incidence * clone()=0
Returns an exact copy of this incidence.
QString summary() const
Returns the incidence summary.
Provides a Journal in the sense of RFC2445.
This class provides a template for lists of pointers.
This class provides a calendar resource using a local CalendarLocal object to cache the calendar data...
virtual Journal * journal(const QString &uid)
Return Journal with given unique id.
KDateTime::Spec timeSpec() const
Get the viewing time specification (time zone etc.) for the calendar.
Event * event(const QString &UniqueStr)
Retrieves an event on the basis of the unique string ID.
Todo * todo(const QString &uid)
Searches todolist for an event with this unique string identifier, returns a pointer or null.
This class provides the interfaces for a calendar resource.
Incidence * incidence(const QString &uid)
Return incidence with given unique id.
This class provides a calendar stored as a file per incidence in a directory.
bool deleteJournal(Journal *journal)
Remove a journal from the journallist.
KABC::Lock * lock()
Return object for locking the resource.
virtual bool doSave(bool syncCache)
Do the actual saving of the resource data.
bool deleteTodo(Todo *todo)
Remove a todo from the todolist.
void deleteAllJournals()
Removes all journals from this calendar.
void deleteAllTodos()
Removes all todos from this calendar.
virtual bool doLoad(bool syncCache)
Do the actual loading of the resource data.
bool doOpen()
Opens the resource.
bool deleteEvent(Event *event)
deletes an event from this calendar.
void deleteAllEvents()
Removes all Events from this calendar.
Provides a To-do in the sense of RFC2445.
virtual void dump() const
virtual bool readOnly() const
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.
This file is part of the API for handling calendar data and defines the Journal class.
This file is part of the API for handling calendar data and defines the Todo class.