libkcal Library API Documentation

resourcelocal.cpp

00001 /*
00002     This file is part of libkcal.
00003 
00004     Copyright (c) 1998 Preston Brown
00005     Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020     Boston, MA 02111-1307, USA.
00021 */
00022 
00023 #include <typeinfo>
00024 #include <stdlib.h>
00025 
00026 #include <qdatetime.h>
00027 #include <qstring.h>
00028 #include <qptrlist.h>
00029 
00030 #include <kdebug.h>
00031 #include <klocale.h>
00032 #include <kurl.h>
00033 
00034 #include "vcaldrag.h"
00035 #include "vcalformat.h"
00036 #include "icalformat.h"
00037 #include "exceptions.h"
00038 #include "incidence.h"
00039 #include "event.h"
00040 #include "todo.h"
00041 #include "journal.h"
00042 #include "filestorage.h"
00043 
00044 #include <kresources/configwidget.h>
00045 
00046 #include "resourcelocalconfig.h"
00047 
00048 #include "resourcelocal.h"
00049 
00050 using namespace KCal;
00051 
00052 class ResourceLocal::Private
00053 {
00054   public:
00055     QDateTime mLastModified;
00056 };
00057 
00058 ResourceLocal::ResourceLocal( const KConfig* config )
00059   : ResourceCached( config ), mLock( 0 )
00060 {
00061   if ( config ) {
00062     QString url = config->readPathEntry( "CalendarURL" );
00063     mURL = KURL( url );
00064 
00065     QString format = config->readEntry( "Format" );
00066     if ( format == "ical" )
00067       mFormat = new ICalFormat();
00068     else if ( format == "vcal" )
00069       mFormat = new VCalFormat();
00070     else {
00071       mFormat = new ICalFormat();
00072     }
00073   } else {
00074     mURL = KURL();
00075     mFormat = new ICalFormat();
00076   }
00077   init();
00078 }
00079 
00080 ResourceLocal::ResourceLocal( const QString& fileName ) 
00081   : ResourceCached( 0 )
00082 {
00083   mURL = KURL( fileName );
00084   mFormat = new ICalFormat();
00085   init();
00086 }
00087 
00088 
00089 void ResourceLocal::writeConfig( KConfig* config )
00090 {
00091   kdDebug() << "ResourceLocal::writeConfig()" << endl;
00092 
00093   ResourceCalendar::writeConfig( config );
00094   config->writePathEntry( "CalendarURL", mURL.prettyURL() );
00095   QString typeID = typeid( *mFormat ).name();
00096   
00097   if ( typeid( *mFormat ) == typeid( ICalFormat ) )
00098     config->writeEntry( "Format", "ical" );
00099   else if ( typeid( *mFormat ) == typeid( VCalFormat ) ) // if ( typeID == "ICalFormat" )
00100     config->writeEntry( "Format", "vcal" );
00101   else
00102     kdDebug() << "ERROR: Unknown format type" << endl;
00103 }
00104 
00105 void ResourceLocal::init()
00106 {
00107   d = new ResourceLocal::Private;
00108 
00109   setType( "file" );
00110 
00111   mOpen = false;
00112 
00113   connect( &mDirWatch, SIGNAL( dirty( const QString & ) ),
00114            SLOT( reload() ) );
00115   connect( &mDirWatch, SIGNAL( created( const QString & ) ),
00116            SLOT( reload() ) );
00117   connect( &mDirWatch, SIGNAL( deleted( const QString & ) ),
00118            SLOT( reload() ) );
00119 
00120   mLock = new KABC::Lock( mURL.path() );
00121 
00122   mDirWatch.addFile( mURL.path() );
00123   mDirWatch.startScan();
00124 }
00125 
00126 
00127 ResourceLocal::~ResourceLocal()
00128 {
00129   close();
00130 
00131   delete mLock;
00132 
00133   delete d;
00134 }
00135 
00136 bool ResourceLocal::doOpen()
00137 {
00138   kdDebug(5800) << "Opening resource " << resourceName() << " with URL " << mURL.prettyURL() << endl;
00139 
00140   mOpen = true;
00141 
00142   return true;
00143 }
00144 
00145 QDateTime ResourceLocal::readLastModified()
00146 {
00147   QFileInfo fi( mURL.path() );
00148   return fi.lastModified();
00149 }
00150 
00151 bool ResourceLocal::load()
00152 {
00153   if ( !mOpen ) return true;
00154 
00155   bool success = mCalendar.load( mURL.path() );
00156 
00157   d->mLastModified = readLastModified();
00158 
00159   return success;
00160 }
00161 
00162 bool ResourceLocal::save()
00163 {
00164   if ( !mOpen ) return true;
00165 
00166   bool success = mCalendar.save( mURL.path() );
00167 
00168   d->mLastModified = readLastModified();
00169 
00170   return success;
00171 }
00172 
00173 KABC::Lock *ResourceLocal::lock()
00174 {
00175   return mLock;
00176 }
00177 
00178 void ResourceLocal::reload()
00179 {
00180   kdDebug(5800) << "ResourceLocal::reload()" << endl;
00181 
00182   if ( !mOpen ) return;
00183 
00184   if ( d->mLastModified == readLastModified() ) {
00185     kdDebug(5800) << "ResourceLocal::reload(): file not modified since last read."
00186               << endl;
00187     return;
00188   }
00189 
00190   mCalendar.close();
00191   mCalendar.load( mURL.path() );
00192 
00193   emit resourceChanged( this );
00194 }
00195 
00196 void ResourceLocal::doClose()
00197 {
00198   if ( !mOpen ) return;
00199 
00200   mCalendar.close();
00201   mOpen = false;
00202 }
00203 
00204 
00205 void ResourceLocal::update(IncidenceBase *)
00206 {
00207 }
00208 
00209 void ResourceLocal::dump() const
00210 {
00211   ResourceCalendar::dump();
00212   kdDebug(5800) << "  Url: " << mURL.url() << endl;
00213 }
00214 
00215 #include "resourcelocal.moc"
KDE Logo
This file is part of the documentation for libkcal Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:36:23 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003