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 #include <kstandarddirs.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 "resourcelocaldirconfig.h"
00047 
00048 #include "resourcelocaldir.h"
00049 
00050 using namespace KCal;
00051 
00052 ResourceLocalDir::ResourceLocalDir( const KConfig* config )
00053   : ResourceCached( config ), mLock( 0 )
00054 {
00055   if ( config ) {
00056     readConfig( config );
00057   }
00058 
00059   init();
00060 }
00061 
00062 ResourceLocalDir::ResourceLocalDir( const QString& dirName )
00063   : ResourceCached( 0 )
00064 {
00065   mURL = KURL( dirName );
00066 
00067   init();
00068 }
00069 
00070 
00071 void ResourceLocalDir::readConfig( const KConfig *config )
00072 {
00073   QString url = config->readPathEntry( "CalendarURL" );
00074   mURL = KURL( url );
00075 }
00076 
00077 void ResourceLocalDir::writeConfig( KConfig *config )
00078 {
00079   kdDebug(5800) << "ResourceLocalDir::writeConfig()" << endl;
00080 
00081   ResourceCalendar::writeConfig( config );
00082 
00083   config->writePathEntry( "CalendarURL", mURL.prettyURL() );
00084 }
00085 
00086 void ResourceLocalDir::init()
00087 {
00088   setType( "dir" );
00089 
00090   mOpen = false;
00091 
00092   connect( &mDirWatch, SIGNAL( dirty( const QString & ) ),
00093            SLOT( reload( const QString & ) ) );
00094   connect( &mDirWatch, SIGNAL( created( const QString & ) ),
00095            SLOT( reload( const QString & ) ) );
00096   connect( &mDirWatch, SIGNAL( deleted( const QString & ) ),
00097            SLOT( reload( const QString & ) ) );
00098 
00099   mLock = new KABC::Lock( mURL.path() );
00100 
00101   mDirWatch.addDir( mURL.path(), true );
00102   mDirWatch.startScan();
00103 }
00104 
00105 
00106 ResourceLocalDir::~ResourceLocalDir()
00107 {
00108   close();
00109 
00110   delete mLock;
00111 }
00112 
00113 bool ResourceLocalDir::doOpen()
00114 {
00115   kdDebug(5800) << "Opening resource " << resourceName() << " with URL " << mURL.prettyURL() << endl;
00116 
00117   mOpen = true;
00118 
00119   return true;
00120 }
00121 
00122 bool ResourceLocalDir::doLoad()
00123 {
00124   kdDebug(5800) << "ResourceLocalDir::load()" << endl;
00125 
00126   if ( !mOpen ) return true;
00127 
00128   mCalendar.close();
00129   bool success = true;
00130 
00131   QString dirName = mURL.path();
00132   if ( !KStandardDirs::exists( dirName ) ) {
00133     kdDebug(5800) << "ResourceLocalDir::load(): Directory doesn't exist yet. Creating it..." << endl;
00134     
00135     // Create the directory. Use 0775 to allow group-writable if the umask 
00136     // allows it (permissions will be 0775 & ~umask). This is desired e.g. for
00137     // group-shared directories!
00138     success = KStandardDirs::makeDir( dirName, 0775 );
00139   } else {
00140 
00141     kdDebug(5800) << "ResourceLocalDir::load(): '" << dirName << "'" << endl;
00142 
00143     QDir dir( dirName );
00144 
00145     QStringList entries = dir.entryList( QDir::Files | QDir::Readable );
00146 
00147     QStringList::ConstIterator it;
00148     for( it = entries.begin(); it != entries.end(); ++it ) {
00149       if ( (*it).endsWith( "~" ) ) // is backup file, ignore it
00150         continue;
00151 
00152       QString fileName = dirName + "/" + *it;
00153       kdDebug(5800) << " read '" << fileName << "'" << endl;
00154       CalendarLocal cal( mCalendar.timeZoneId() );
00155       cal.load( fileName );
00156       Incidence::List incidences = cal.rawIncidences();
00157       Incidence *i = incidences.first();
00158       if ( i ) mCalendar.addIncidence( i->clone() );
00159     }
00160   }
00161 
00162   return success;
00163 }
00164 
00165 bool ResourceLocalDir::doSave()
00166 {
00167   kdDebug(5800) << "ResourceLocalDir::save()" << endl;
00168 
00169   if ( !mOpen ) return true;
00170 
00171   Incidence::List incidences = mCalendar.rawIncidences();
00172 
00173   Incidence::List::ConstIterator it;
00174   for( it = incidences.begin(); it != incidences.end(); ++it ) {
00175     Incidence *i = *it;
00176     QString fileName = mURL.path() + "/" + i->uid();
00177     kdDebug(5800) << "writing '" << fileName << "'" << endl;
00178 
00179     CalendarLocal cal( mCalendar.timeZoneId() );
00180     cal.addIncidence( i->clone() );
00181     cal.save( fileName );
00182   }
00183 
00184   return true;
00185 }
00186 
00187 KABC::Lock *ResourceLocalDir::lock()
00188 {
00189   return mLock;
00190 }
00191 
00192 void ResourceLocalDir::reload( const QString &file )
00193 {
00194   kdDebug(5800) << "ResourceLocalDir::reload()" << endl;
00195 
00196   if ( !mOpen ) return;
00197 
00198   kdDebug(5800) << "  File: '" << file << "'" << endl;
00199 
00200   mCalendar.close();
00201   load();
00202 
00203   emit resourceChanged( this );
00204 }
00205 
00206 void ResourceLocalDir::doClose()
00207 {
00208   if ( !mOpen ) return;
00209 
00210   mCalendar.close();
00211   mOpen = false;
00212 }
00213 
00214 
00215 void ResourceLocalDir::deleteEvent(Event *event)
00216 {
00217   kdDebug(5800) << "ResourceLocalDir::deleteEvent" << endl;
00218   if ( deleteIncidenceFile(event) )
00219     mCalendar.deleteEvent( event );
00220 }
00221 
00222 
00223 void ResourceLocalDir::deleteTodo(Todo *todo)
00224 {
00225   if ( deleteIncidenceFile(todo) )
00226     mCalendar.deleteTodo( todo );
00227 }
00228 
00229 
00230 void ResourceLocalDir::dump() const
00231 {
00232   ResourceCalendar::dump();
00233   kdDebug(5800) << "  Url: " << mURL.url() << endl;
00234 }
00235 
00236 bool ResourceLocalDir::deleteIncidenceFile(Incidence *incidence)
00237 {
00238   QFile file( mURL.path() + "/" + incidence->uid() );
00239   if ( !file.exists() )
00240     return true;
00241 
00242   mDirWatch.stopScan();
00243   bool removed = file.remove();
00244   mDirWatch.startScan();
00245   return removed;
00246 }
00247 
00248 #include "resourcelocaldir.moc"
KDE Logo
This file is part of the documentation for libkcal Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 17 09:52:51 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003