From 460c52653ab0dcca6f19a4f492ed2c5e4e963ab0 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- libkcal/resourcelocaldir.cpp | 277 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 277 insertions(+) create mode 100644 libkcal/resourcelocaldir.cpp (limited to 'libkcal/resourcelocaldir.cpp') diff --git a/libkcal/resourcelocaldir.cpp b/libkcal/resourcelocaldir.cpp new file mode 100644 index 00000000..5e6528e8 --- /dev/null +++ b/libkcal/resourcelocaldir.cpp @@ -0,0 +1,277 @@ +/* + This file is part of libkcal. + + Copyright (c) 2003 Cornelius Schumacher + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "vcaldrag.h" +#include "vcalformat.h" +#include "icalformat.h" +#include "exceptions.h" +#include "calendarlocal.h" +#include "incidence.h" +#include "event.h" +#include "todo.h" +#include "journal.h" +#include "filestorage.h" + +#include + +#include "resourcelocaldirconfig.h" + +#include "resourcelocaldir.h" + +using namespace KCal; + +ResourceLocalDir::ResourceLocalDir( const KConfig* config ) + : ResourceCached( config ), mLock( 0 ) +{ + if ( config ) { + readConfig( config ); + } + + init(); +} + +ResourceLocalDir::ResourceLocalDir( const QString& dirName ) + : ResourceCached( 0 ) +{ + mURL = KURL( dirName ); + + init(); +} + + +void ResourceLocalDir::readConfig( const KConfig *config ) +{ + QString url = config->readPathEntry( "CalendarURL" ); + mURL = KURL( url ); +} + +void ResourceLocalDir::writeConfig( KConfig *config ) +{ + kdDebug(5800) << "ResourceLocalDir::writeConfig()" << endl; + + ResourceCalendar::writeConfig( config ); + + config->writePathEntry( "CalendarURL", mURL.prettyURL() ); +} + +void ResourceLocalDir::init() +{ + setType( "dir" ); + + setSavePolicy( SaveDelayed ); + + connect( &mDirWatch, SIGNAL( dirty( const QString & ) ), + SLOT( reload( const QString & ) ) ); + connect( &mDirWatch, SIGNAL( created( const QString & ) ), + SLOT( reload( const QString & ) ) ); + connect( &mDirWatch, SIGNAL( deleted( const QString & ) ), + SLOT( reload( const QString & ) ) ); + + mLock = new KABC::Lock( mURL.path() ); + + mDirWatch.addDir( mURL.path(), true ); + mDirWatch.startScan(); +} + + +ResourceLocalDir::~ResourceLocalDir() +{ + close(); + + delete mLock; +} + +bool ResourceLocalDir::doOpen() +{ + QFileInfo dirInfo( mURL.path() ); + return dirInfo.isDir() && dirInfo.isReadable() && + ( dirInfo.isWritable() || readOnly() ); +} + +bool ResourceLocalDir::doLoad() +{ + kdDebug(5800) << "ResourceLocalDir::load()" << endl; + + mCalendar.close(); + QString dirName = mURL.path(); + + if ( !( KStandardDirs::exists( dirName ) || KStandardDirs::exists( dirName + "/") ) ) { + kdDebug(5800) << "ResourceLocalDir::load(): Directory '" << dirName + << "' doesn't exist yet. Creating it..." << endl; + // Create the directory. Use 0775 to allow group-writable if the umask + // allows it (permissions will be 0775 & ~umask). This is desired e.g. for + // group-shared directories! + return KStandardDirs::makeDir( dirName, 0775 ); + } + + // The directory exists. Now try to open (the files in) it. + kdDebug(5800) << "ResourceLocalDir::load(): '" << dirName << "'" << endl; + QFileInfo dirInfo( dirName ); + if ( !( dirInfo.isDir() && dirInfo.isReadable() && + ( dirInfo.isWritable() || readOnly() ) ) ) + return false; + + QDir dir( dirName ); + QStringList entries = dir.entryList( QDir::Files | QDir::Readable ); + + bool success = true; + QStringList::ConstIterator it; + for( it = entries.constBegin(); it != entries.constEnd(); ++it ) { + if ( (*it).endsWith( "~" ) ) // is backup file, ignore it + continue; + + QString fileName = dirName + "/" + *it; + kdDebug(5800) << " read '" << fileName << "'" << endl; + CalendarLocal cal( mCalendar.timeZoneId() ); + if ( !doFileLoad( cal, fileName ) ) { + success = false; + } + } + + return success; +} + +bool ResourceLocalDir::doFileLoad( CalendarLocal &cal, const QString &fileName ) +{ + if ( !cal.load( fileName ) ) + return false; + Incidence::List incidences = cal.rawIncidences(); + Incidence::List::ConstIterator it; + for ( it = incidences.constBegin(); it != incidences.constEnd(); ++it ) { + Incidence *i = *it; + if ( i ) mCalendar.addIncidence( i->clone() ); + } + return true; +} + +bool ResourceLocalDir::doSave() +{ + Incidence::List list; + bool success = true; + + list = addedIncidences(); + list += changedIncidences(); + + for ( Incidence::List::iterator it = list.begin(); it != list.end(); ++it ) + if ( !doSave( *it ) ) + success = false; + + return success; +} + +bool ResourceLocalDir::doSave( Incidence *incidence ) +{ + mDirWatch.stopScan(); // do prohibit the dirty() signal and a following reload() + + QString fileName = mURL.path() + "/" + incidence->uid(); + kdDebug(5800) << "writing '" << fileName << "'" << endl; + + CalendarLocal cal( mCalendar.timeZoneId() ); + cal.addIncidence( incidence->clone() ); + const bool ret = cal.save( fileName ); + + mDirWatch.startScan(); + + return ret; +} + +KABC::Lock *ResourceLocalDir::lock() +{ + return mLock; +} + +void ResourceLocalDir::reload( const QString &file ) +{ + kdDebug(5800) << "ResourceLocalDir::reload()" << endl; + + if ( !isOpen() ) + return; + + kdDebug(5800) << " File: '" << file << "'" << endl; + + mCalendar.close(); + load(); + + emit resourceChanged( this ); +} + + +bool ResourceLocalDir::deleteEvent(Event *event) +{ + kdDebug(5800) << "ResourceLocalDir::deleteEvent" << endl; + if ( deleteIncidenceFile(event) ) + return( mCalendar.deleteEvent( event ) ); + else + return( false ); +} + + +bool ResourceLocalDir::deleteTodo(Todo *todo) +{ + if ( deleteIncidenceFile(todo) ) + return( mCalendar.deleteTodo( todo ) ); + else + return( false ); +} + + +bool ResourceLocalDir::deleteJournal( Journal *journal ) +{ + if ( deleteIncidenceFile( journal ) ) + return( mCalendar.deleteJournal( journal ) ); + else + return( false ); +} + + +void ResourceLocalDir::dump() const +{ + ResourceCalendar::dump(); + kdDebug(5800) << " Url: " << mURL.url() << endl; +} + +bool ResourceLocalDir::deleteIncidenceFile(Incidence *incidence) +{ + QFile file( mURL.path() + "/" + incidence->uid() ); + if ( !file.exists() ) + return true; + + mDirWatch.stopScan(); + bool removed = file.remove(); + mDirWatch.startScan(); + return removed; +} + +#include "resourcelocaldir.moc" -- cgit v1.2.3