diff options
author | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
---|---|---|
committer | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
commit | 460c52653ab0dcca6f19a4f492ed2c5e4e963ab0 (patch) | |
tree | 67208f7c145782a7e90b123b982ca78d88cc2c87 /libkcal/resourcelocal.cpp | |
download | tdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.tar.gz tdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.zip |
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
Diffstat (limited to 'libkcal/resourcelocal.cpp')
-rw-r--r-- | libkcal/resourcelocal.cpp | 231 |
1 files changed, 231 insertions, 0 deletions
diff --git a/libkcal/resourcelocal.cpp b/libkcal/resourcelocal.cpp new file mode 100644 index 000000000..cb61cd1a6 --- /dev/null +++ b/libkcal/resourcelocal.cpp @@ -0,0 +1,231 @@ +/* + This file is part of libkcal. + + Copyright (c) 1998 Preston Brown <pbrown@kde.org> + Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org> + + 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 <typeinfo> +#include <stdlib.h> + +#include <qdatetime.h> +#include <qstring.h> +#include <qptrlist.h> + +#include <kdebug.h> +#include <klocale.h> +#include <kurl.h> +#include <kstandarddirs.h> + +#include "vcaldrag.h" +#include "vcalformat.h" +#include "icalformat.h" +#include "exceptions.h" +#include "incidence.h" +#include "event.h" +#include "todo.h" +#include "journal.h" +#include "filestorage.h" + +#include <kresources/configwidget.h> + +#include "resourcelocalconfig.h" + +#include "resourcelocal.h" + +using namespace KCal; + +class ResourceLocal::Private +{ + public: + QDateTime mLastModified; +}; + +ResourceLocal::ResourceLocal( const KConfig* config ) + : ResourceCached( config ), mLock( 0 ) +{ + if ( config ) { + QString url = config->readPathEntry( "CalendarURL" ); + mURL = KURL( url ); + + QString format = config->readEntry( "Format" ); + if ( format == "ical" ) + mFormat = new ICalFormat(); + else if ( format == "vcal" ) + mFormat = new VCalFormat(); + else { + mFormat = new ICalFormat(); + } + } else { + mURL = KURL(); + mFormat = new ICalFormat(); + } + init(); +} + +ResourceLocal::ResourceLocal( const QString& fileName ) + : ResourceCached( 0 ) +{ + mURL = KURL( fileName ); + mFormat = new ICalFormat(); + init(); +} + + +void ResourceLocal::writeConfig( KConfig* config ) +{ + kdDebug(5800) << "ResourceLocal::writeConfig()" << endl; + + ResourceCalendar::writeConfig( config ); + config->writePathEntry( "CalendarURL", mURL.prettyURL() ); + QString typeID = typeid( *mFormat ).name(); + + if ( typeid( *mFormat ) == typeid( ICalFormat ) ) + config->writeEntry( "Format", "ical" ); + else if ( typeid( *mFormat ) == typeid( VCalFormat ) ) // if ( typeID == "ICalFormat" ) + config->writeEntry( "Format", "vcal" ); + else + kdDebug(5800) << "ERROR: Unknown format type" << endl; +} + +void ResourceLocal::init() +{ + d = new ResourceLocal::Private; + + setType( "file" ); + + setSavePolicy( SaveDelayed ); + + connect( &mDirWatch, SIGNAL( dirty( const QString & ) ), + SLOT( reload() ) ); + connect( &mDirWatch, SIGNAL( created( const QString & ) ), + SLOT( reload() ) ); + connect( &mDirWatch, SIGNAL( deleted( const QString & ) ), + SLOT( reload() ) ); + + mLock = new KABC::Lock( mURL.path() ); + + mDirWatch.addFile( mURL.path() ); + mDirWatch.startScan(); +} + + +ResourceLocal::~ResourceLocal() +{ + mDirWatch.stopScan(); + + close(); + + delete mLock; + + delete d; +} + +QDateTime ResourceLocal::readLastModified() +{ + QFileInfo fi( mURL.path() ); + return fi.lastModified(); +} + +bool ResourceLocal::doLoad() +{ + bool success; + + if ( !KStandardDirs::exists( mURL.path() ) ) { + kdDebug(5800) << "ResourceLocal::load(): File doesn't exist yet." << endl; + // Save the empty calendar, so the calendar file will be created. + success = doSave(); + } else { + success = mCalendar.load( mURL.path() ); + if ( success ) d->mLastModified = readLastModified(); + } + + return success; +} + +bool ResourceLocal::doSave() +{ + bool success = mCalendar.save( mURL.path() ); + d->mLastModified = readLastModified(); + + return success; +} + +KABC::Lock *ResourceLocal::lock() +{ + return mLock; +} + +bool ResourceLocal::doReload() +{ + kdDebug(5800) << "ResourceLocal::doReload()" << endl; + + if ( !isOpen() ) return false; + + if ( d->mLastModified == readLastModified() ) { + kdDebug(5800) << "ResourceLocal::reload(): file not modified since last read." + << endl; + return false; + } + + mCalendar.close(); + mCalendar.load( mURL.path() ); + return true; +} + +void ResourceLocal::reload() +{ + if ( doReload() ) + emit resourceChanged( this ); +} + +void ResourceLocal::dump() const +{ + ResourceCalendar::dump(); + kdDebug(5800) << " Url: " << mURL.url() << endl; +} + +QString ResourceLocal::fileName() const +{ + return mURL.path(); +} + +bool ResourceLocal::setFileName( const QString &fileName ) +{ + bool open = isOpen(); + if ( open ) close(); + delete mLock; + mDirWatch.stopScan(); + mDirWatch.removeFile( mURL.path() ); + mURL = KURL( fileName ); + mLock = new KABC::Lock( mURL.path() ); + mDirWatch.addFile( mURL.path() ); + mDirWatch.startScan(); + return true; +} + +bool ResourceLocal::setValue( const QString &key, const QString &value ) +{ + if ( key == "File" ) { + return setFileName( value ); + } else return false; +} + + + +#include "resourcelocal.moc" |