libkcal Library API Documentation

resourcelocaldir.cpp

00001 /*
00002     This file is part of libkcal.
00003 
00004     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include <typeinfo>
00023 #include <stdlib.h>
00024 
00025 #include <qdatetime.h>
00026 #include <qstring.h>
00027 #include <qptrlist.h>
00028 
00029 #include <kdebug.h>
00030 #include <klocale.h>
00031 #include <kurl.h>
00032 
00033 #include "vcaldrag.h"
00034 #include "vcalformat.h"
00035 #include "icalformat.h"
00036 #include "exceptions.h"
00037 #include "incidence.h"
00038 #include "event.h"
00039 #include "todo.h"
00040 #include "journal.h"
00041 #include "filestorage.h"
00042 
00043 #include <kresources/configwidget.h>
00044 
00045 #include "resourcelocaldirconfig.h"
00046 
00047 #include "resourcelocaldir.h"
00048 
00049 using namespace KCal;
00050 
00051 ResourceLocalDir::ResourceLocalDir( const KConfig* config )
00052   : ResourceCached( config ), mLock( 0 )
00053 {
00054   if ( config ) {
00055     readConfig( config );
00056   }
00057 
00058   init();
00059 }
00060 
00061 ResourceLocalDir::ResourceLocalDir( const QString& dirName )
00062   : ResourceCached( 0 )
00063 {
00064   mURL = KURL( dirName );
00065 
00066   init();
00067 }
00068 
00069 
00070 void ResourceLocalDir::readConfig( const KConfig *config )
00071 {
00072   QString url = config->readPathEntry( "CalendarURL" );
00073   mURL = KURL( url );
00074 }
00075 
00076 void ResourceLocalDir::writeConfig( KConfig *config )
00077 {
00078   kdDebug() << "ResourceLocalDir::writeConfig()" << endl;
00079 
00080   ResourceCalendar::writeConfig( config );
00081 
00082   config->writePathEntry( "CalendarURL", mURL.prettyURL() );
00083 }
00084 
00085 void ResourceLocalDir::init()
00086 {
00087   setType( "dir" );
00088 
00089   mOpen = false;
00090 
00091   connect( &mDirWatch, SIGNAL( dirty( const QString & ) ),
00092            SLOT( reload( const QString & ) ) );
00093   connect( &mDirWatch, SIGNAL( created( const QString & ) ),
00094            SLOT( reload( const QString & ) ) );
00095   connect( &mDirWatch, SIGNAL( deleted( const QString & ) ),
00096            SLOT( reload( const QString & ) ) );
00097 
00098   mLock = new KABC::Lock( mURL.path() );
00099 
00100   mDirWatch.addDir( mURL.path(), true );
00101   mDirWatch.startScan();
00102 }
00103 
00104 
00105 ResourceLocalDir::~ResourceLocalDir()
00106 {
00107   close();
00108 
00109   delete mLock;
00110 }
00111 
00112 bool ResourceLocalDir::doOpen()
00113 {
00114   kdDebug(5800) << "Opening resource " << resourceName() << " with URL " << mURL.prettyURL() << endl;
00115 
00116   mOpen = true;
00117 
00118   return true;
00119 }
00120 
00121 bool ResourceLocalDir::load()
00122 {
00123   kdDebug() << "ResourceLocalDir::load()" << endl;
00124 
00125   if ( !mOpen ) return true;
00126 
00127   mCalendar.close();
00128 
00129   QString dirName = mURL.path();
00130 
00131   kdDebug() << "ResourceLocalDir::load(): '" << dirName << "'" << endl;
00132 
00133   QDir dir( dirName );
00134 
00135   QStringList entries = dir.entryList( QDir::Files | QDir::Readable );
00136 
00137   QStringList::ConstIterator it;
00138   for( it = entries.begin(); it != entries.end(); ++it ) {
00139     if ( (*it).endsWith( "~" ) ) // is backup file, ignore it
00140       continue;
00141 
00142     QString fileName = dirName + "/" + *it;
00143     kdDebug() << " read '" << fileName << "'" << endl;
00144     CalendarLocal cal( mCalendar.timeZoneId() );
00145     cal.load( fileName );
00146     Incidence::List incidences = cal.rawIncidences();
00147     Incidence *i = incidences.first();
00148     if ( i ) mCalendar.addIncidence( i->clone() );
00149   }
00150 
00151   return true;
00152 }
00153 
00154 bool ResourceLocalDir::save()
00155 {
00156   kdDebug() << "ResourceLocalDir::save()" << endl;
00157 
00158   if ( !mOpen ) return true;
00159 
00160   Incidence::List incidences = mCalendar.rawIncidences();
00161 
00162   Incidence::List::ConstIterator it;
00163   for( it = incidences.begin(); it != incidences.end(); ++it ) {
00164     Incidence *i = *it;
00165     QString fileName = mURL.path() + "/" + i->uid();
00166     kdDebug() << "writing '" << fileName << "'" << endl;
00167 
00168     CalendarLocal cal( mCalendar.timeZoneId() );
00169     cal.addIncidence( i->clone() );
00170     cal.save( fileName );
00171   }
00172 
00173   return true;
00174 }
00175 
00176 KABC::Lock *ResourceLocalDir::lock()
00177 {
00178   return mLock;
00179 }
00180 
00181 void ResourceLocalDir::reload( const QString &file )
00182 {
00183   kdDebug() << "ResourceLocalDir::reload()" << endl;
00184 
00185   if ( !mOpen ) return;
00186 
00187   kdDebug() << "  File: '" << file << "'" << endl;
00188 
00189   mCalendar.close();
00190   load();
00191 
00192   emit resourceChanged( this );
00193 }
00194 
00195 void ResourceLocalDir::doClose()
00196 {
00197   if ( !mOpen ) return;
00198 
00199   mCalendar.close();
00200   mOpen = false;
00201 }
00202 
00203 
00204 void ResourceLocalDir::deleteEvent(Event *event)
00205 {
00206   kdDebug(5800) << "ResourceLocalDir::deleteEvent" << endl;
00207   if ( deleteIncidenceFile(event) )
00208     mCalendar.deleteEvent( event );
00209 }
00210 
00211 
00212 void ResourceLocalDir::deleteTodo(Todo *todo)
00213 {
00214   if ( deleteIncidenceFile(todo) )
00215     mCalendar.deleteTodo( todo );
00216 }
00217 
00218 
00219 void ResourceLocalDir::update(IncidenceBase *)
00220 {
00221 }
00222 
00223 void ResourceLocalDir::dump() const
00224 {
00225   ResourceCalendar::dump();
00226   kdDebug(5800) << "  Url: " << mURL.url() << endl;
00227 }
00228 
00229 bool ResourceLocalDir::deleteIncidenceFile(Incidence *incidence)
00230 {
00231   QFile file( mURL.path() + "/" + incidence->uid() );
00232   if ( !file.exists() )
00233     return true;
00234 
00235   mDirWatch.stopScan();
00236   bool removed = file.remove();
00237   mDirWatch.startScan();
00238   return removed;
00239 }
00240 
00241 #include "resourcelocaldir.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