libkcal

resourcelocal.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of libkcal.
00003 
00004     Copyright (c) 1998 Preston Brown <pbrown@kde.org>
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., 51 Franklin Street, Fifth Floor,
00020     Boston, MA 02110-1301, 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 #include <kstandarddirs.h>
00034 
00035 #include "vcaldrag.h"
00036 #include "vcalformat.h"
00037 #include "icalformat.h"
00038 #include "exceptions.h"
00039 #include "incidence.h"
00040 #include "event.h"
00041 #include "todo.h"
00042 #include "journal.h"
00043 #include "filestorage.h"
00044 
00045 #include <kresources/configwidget.h>
00046 
00047 #include "resourcelocalconfig.h"
00048 
00049 #include "resourcelocal.h"
00050 
00051 using namespace KCal;
00052 
00053 class ResourceLocal::Private
00054 {
00055   public:
00056     QDateTime mLastModified;
00057 };
00058 
00059 ResourceLocal::ResourceLocal( const KConfig* config )
00060   : ResourceCached( config ), mLock( 0 )
00061 {
00062   if ( config ) {
00063     QString url = config->readPathEntry( "CalendarURL" );
00064     mURL = KURL( url );
00065 
00066     QString format = config->readEntry( "Format" );
00067     if ( format == "ical" )
00068       mFormat = new ICalFormat();
00069     else if ( format == "vcal" )
00070       mFormat = new VCalFormat();
00071     else {
00072       mFormat = new ICalFormat();
00073     }
00074   } else {
00075     mURL = KURL();
00076     mFormat = new ICalFormat();
00077   }
00078   init();
00079 }
00080 
00081 ResourceLocal::ResourceLocal( const QString& fileName )
00082   : ResourceCached( 0 )
00083 {
00084   mURL = KURL( fileName );
00085   mFormat = new ICalFormat();
00086   init();
00087 }
00088 
00089 
00090 void ResourceLocal::writeConfig( KConfig* config )
00091 {
00092   kdDebug(5800) << "ResourceLocal::writeConfig()" << endl;
00093 
00094   ResourceCalendar::writeConfig( config );
00095   config->writePathEntry( "CalendarURL", mURL.prettyURL() );
00096   QString typeID = typeid( *mFormat ).name();
00097 
00098   if ( typeid( *mFormat ) == typeid( ICalFormat ) )
00099     config->writeEntry( "Format", "ical" );
00100   else if ( typeid( *mFormat ) == typeid( VCalFormat ) ) // if ( typeID == "ICalFormat" )
00101     config->writeEntry( "Format", "vcal" );
00102   else
00103     kdDebug(5800) << "ERROR: Unknown format type" << endl;
00104 }
00105 
00106 void ResourceLocal::init()
00107 {
00108   d = new ResourceLocal::Private;
00109 
00110   setType( "file" );
00111 
00112   setSavePolicy( SaveDelayed );
00113 
00114   connect( &mDirWatch, SIGNAL( dirty( const QString & ) ),
00115            SLOT( reload() ) );
00116   connect( &mDirWatch, SIGNAL( created( const QString & ) ),
00117            SLOT( reload() ) );
00118   connect( &mDirWatch, SIGNAL( deleted( const QString & ) ),
00119            SLOT( reload() ) );
00120 
00121   mLock = new KABC::Lock( mURL.path() );
00122 
00123   mDirWatch.addFile( mURL.path() );
00124   mDirWatch.startScan();
00125 }
00126 
00127 
00128 ResourceLocal::~ResourceLocal()
00129 {
00130   mDirWatch.stopScan();
00131 
00132   close();
00133 
00134   delete mLock;
00135 
00136   delete d;
00137 }
00138 
00139 QDateTime ResourceLocal::readLastModified()
00140 {
00141   QFileInfo fi( mURL.path() );
00142   return fi.lastModified();
00143 }
00144 
00145 bool ResourceLocal::doLoad()
00146 {
00147   bool success;
00148 
00149   if ( !KStandardDirs::exists( mURL.path() ) ) {
00150     kdDebug(5800) << "ResourceLocal::load(): File doesn't exist yet." << endl;
00151     // Save the empty calendar, so the calendar file will be created.
00152     success = doSave();
00153   } else {
00154     success = mCalendar.load( mURL.path() );
00155     if ( success ) d->mLastModified = readLastModified();
00156   }
00157 
00158   return success;
00159 }
00160 
00161 bool ResourceLocal::doSave()
00162 {
00163   bool success = mCalendar.save( mURL.path() );
00164   d->mLastModified = readLastModified();
00165 
00166   return success;
00167 }
00168 
00169 KABC::Lock *ResourceLocal::lock()
00170 {
00171   return mLock;
00172 }
00173 
00174 bool ResourceLocal::doReload()
00175 {
00176   kdDebug(5800) << "ResourceLocal::doReload()" << endl;
00177 
00178   if ( !isOpen() ) return false;
00179 
00180   if ( d->mLastModified == readLastModified() ) {
00181     kdDebug(5800) << "ResourceLocal::reload(): file not modified since last read."
00182               << endl;
00183     return false;
00184   }
00185 
00186   mCalendar.close();
00187   mCalendar.load( mURL.path() );
00188   return true;
00189 }
00190 
00191 void ResourceLocal::reload()
00192 {
00193   if ( doReload() )
00194     emit resourceChanged( this );
00195 }
00196 
00197 void ResourceLocal::dump() const
00198 {
00199   ResourceCalendar::dump();
00200   kdDebug(5800) << "  Url: " << mURL.url() << endl;
00201 }
00202 
00203 QString ResourceLocal::fileName() const
00204 {
00205   return mURL.path();
00206 }
00207 
00208 bool ResourceLocal::setFileName( const QString &fileName )
00209 {
00210   bool open = isOpen();
00211   if ( open ) close();
00212   delete mLock;
00213   mDirWatch.stopScan();
00214   mDirWatch.removeFile( mURL.path() );
00215   mURL = KURL( fileName );
00216   mLock = new KABC::Lock( mURL.path() );
00217   mDirWatch.addFile( mURL.path() );
00218   mDirWatch.startScan();
00219   return true;
00220 }
00221 
00222 bool ResourceLocal::setValue( const QString &key, const QString &value ) 
00223 {
00224   if ( key == "File" ) {
00225     return setFileName( value );
00226   } else return false;
00227 }
00228 
00229 
00230 
00231 #include "resourcelocal.moc"