libkcal

resourcelocal.cpp

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   connect( &mDirWatch, SIGNAL( dirty( const QString & ) ),
00113            SLOT( reload() ) );
00114   connect( &mDirWatch, SIGNAL( created( const QString & ) ),
00115            SLOT( reload() ) );
00116   connect( &mDirWatch, SIGNAL( deleted( const QString & ) ),
00117            SLOT( reload() ) );
00118 
00119   mLock = new KABC::Lock( mURL.path() );
00120 
00121   mDirWatch.addFile( mURL.path() );
00122   mDirWatch.startScan();
00123 }
00124 
00125 
00126 ResourceLocal::~ResourceLocal()
00127 {
00128   mDirWatch.stopScan();
00129 
00130   close();
00131 
00132   delete mLock;
00133 
00134   delete d;
00135 }
00136 
00137 QDateTime ResourceLocal::readLastModified()
00138 {
00139   QFileInfo fi( mURL.path() );
00140   return fi.lastModified();
00141 }
00142 
00143 bool ResourceLocal::doLoad()
00144 {
00145   bool success;
00146 
00147   if ( !KStandardDirs::exists( mURL.path() ) ) {
00148     kdDebug(5800) << "ResourceLocal::load(): File doesn't exist yet." << endl;
00149     // Save the empty calendar, so the calendar file will be created.
00150     success = doSave();
00151   } else {
00152     success = mCalendar.load( mURL.path() );
00153     if ( success ) d->mLastModified = readLastModified();
00154   }
00155 
00156   return success;
00157 }
00158 
00159 bool ResourceLocal::doSave()
00160 {
00161   bool success = mCalendar.save( mURL.path() );
00162   d->mLastModified = readLastModified();
00163 
00164   return success;
00165 }
00166 
00167 KABC::Lock *ResourceLocal::lock()
00168 {
00169   return mLock;
00170 }
00171 
00172 bool ResourceLocal::doReload()
00173 {
00174   kdDebug(5800) << "ResourceLocal::doReload()" << endl;
00175 
00176   if ( !isOpen() ) return false;
00177 
00178   if ( d->mLastModified == readLastModified() ) {
00179     kdDebug(5800) << "ResourceLocal::reload(): file not modified since last read."
00180               << endl;
00181     return false;
00182   }
00183 
00184   mCalendar.close();
00185   mCalendar.load( mURL.path() );
00186   return true;
00187 }
00188 
00189 void ResourceLocal::reload()
00190 {
00191   if ( doReload() )
00192     emit resourceChanged( this );
00193 }
00194 
00195 void ResourceLocal::dump() const
00196 {
00197   ResourceCalendar::dump();
00198   kdDebug(5800) << "  Url: " << mURL.url() << endl;
00199 }
00200 
00201 QString ResourceLocal::fileName() const
00202 {
00203   return mURL.path();
00204 }
00205 
00206 bool ResourceLocal::setFileName( const QString &fileName )
00207 {
00208   bool open = isOpen();
00209   if ( open ) close();
00210   delete mLock;
00211   mDirWatch.stopScan();
00212   mDirWatch.removeFile( mURL.path() );
00213   mURL = KURL( fileName );
00214   mLock = new KABC::Lock( mURL.path() );
00215   mDirWatch.addFile( mURL.path() );
00216   mDirWatch.startScan();
00217   return true;
00218 }
00219 
00220 bool ResourceLocal::setValue( const QString &key, const QString &value ) 
00221 {
00222   if ( key == "File" ) {
00223     return setFileName( value );
00224   } else return false;
00225 }
00226 
00227 
00228 
00229 #include "resourcelocal.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys