libkcal

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., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, 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 <kconfig.h>
00033 #include <kstandarddirs.h>
00034 
00035 #include "vcaldrag.h"
00036 #include "vcalformat.h"
00037 #include "icalformat.h"
00038 #include "exceptions.h"
00039 #include "calendarlocal.h"
00040 #include "incidence.h"
00041 #include "event.h"
00042 #include "todo.h"
00043 #include "journal.h"
00044 #include "filestorage.h"
00045 
00046 #include <kresources/configwidget.h>
00047 
00048 #include "resourcelocaldirconfig.h"
00049 
00050 #include "resourcelocaldir.h"
00051 
00052 using namespace KCal;
00053 
00054 ResourceLocalDir::ResourceLocalDir( const KConfig* config )
00055   : ResourceCached( config ), mLock( 0 )
00056 {
00057   if ( config ) {
00058     readConfig( config );
00059   }
00060 
00061   init();
00062 }
00063 
00064 ResourceLocalDir::ResourceLocalDir( const QString& dirName )
00065   : ResourceCached( 0 )
00066 {
00067   mURL = KURL( dirName );
00068 
00069   init();
00070 }
00071 
00072 
00073 void ResourceLocalDir::readConfig( const KConfig *config )
00074 {
00075   QString url = config->readPathEntry( "CalendarURL" );
00076   mURL = KURL( url );
00077 }
00078 
00079 void ResourceLocalDir::writeConfig( KConfig *config )
00080 {
00081   kdDebug(5800) << "ResourceLocalDir::writeConfig()" << endl;
00082 
00083   ResourceCalendar::writeConfig( config );
00084 
00085   config->writePathEntry( "CalendarURL", mURL.prettyURL() );
00086 }
00087 
00088 void ResourceLocalDir::init()
00089 {
00090   setType( "dir" );
00091 
00092   setSavePolicy( SaveDelayed );
00093 
00094   connect( &mDirWatch, SIGNAL( dirty( const QString & ) ),
00095            SLOT( reload( const QString & ) ) );
00096   connect( &mDirWatch, SIGNAL( created( const QString & ) ),
00097            SLOT( reload( const QString & ) ) );
00098   connect( &mDirWatch, SIGNAL( deleted( const QString & ) ),
00099            SLOT( reload( const QString & ) ) );
00100 
00101   mLock = new KABC::Lock( mURL.path() );
00102 
00103   mDirWatch.addDir( mURL.path(), true );
00104   mDirWatch.startScan();
00105 }
00106 
00107 
00108 ResourceLocalDir::~ResourceLocalDir()
00109 {
00110   close();
00111 
00112   delete mLock;
00113 }
00114 
00115 bool ResourceLocalDir::doLoad()
00116 {
00117   kdDebug(5800) << "ResourceLocalDir::load()" << endl;
00118 
00119   mCalendar.close();
00120   QString dirName = mURL.path();
00121   bool success = true;
00122 
00123   if ( !( KStandardDirs::exists( dirName ) || KStandardDirs::exists( dirName + "/") ) ) {
00124     kdDebug(5800) << "ResourceLocalDir::load(): Directory '" << dirName << "' doesn't exist yet. Creating it..." << endl;
00125 
00126     // Create the directory. Use 0775 to allow group-writable if the umask
00127     // allows it (permissions will be 0775 & ~umask). This is desired e.g. for
00128     // group-shared directories!
00129     success = KStandardDirs::makeDir( dirName, 0775 );
00130   } else {
00131 
00132     kdDebug(5800) << "ResourceLocalDir::load(): '" << dirName << "'" << endl;
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(5800) << " read '" << fileName << "'" << endl;
00144       CalendarLocal cal( mCalendar.timeZoneId() );
00145       if ( !doFileLoad( cal, fileName ) ) {
00146         success = false;
00147       }
00148     }
00149   }
00150 
00151   return success;
00152 }
00153 
00154 bool ResourceLocalDir::doFileLoad( CalendarLocal &cal, const QString &fileName )
00155 {
00156   if ( !cal.load( fileName ) )
00157     return false;
00158   Incidence::List incidences = cal.rawIncidences();
00159   Incidence::List::ConstIterator it;
00160   for ( it = incidences.constBegin(); it != incidences.constEnd(); ++it ) {
00161     Incidence *i = *it;
00162     if ( i ) mCalendar.addIncidence( i->clone() );
00163   }
00164   return true;
00165 }
00166 
00167 bool ResourceLocalDir::doSave()
00168 {
00169   Incidence::List list;
00170   bool success = true;
00171 
00172   list = addedIncidences();
00173   list += changedIncidences();
00174 
00175   for ( Incidence::List::iterator it = list.begin(); it != list.end(); ++it )
00176     if ( !doSave( *it ) )
00177       success = false;
00178 
00179   return success;
00180 }
00181 
00182 bool ResourceLocalDir::doSave( Incidence *incidence )
00183 {
00184   mDirWatch.stopScan();  // do prohibit the dirty() signal and a following reload()
00185 
00186   QString fileName = mURL.path() + "/" + incidence->uid();
00187   kdDebug(5800) << "writing '" << fileName << "'" << endl;
00188 
00189   CalendarLocal cal( mCalendar.timeZoneId() );
00190   cal.addIncidence( incidence->clone() );
00191   const bool ret = cal.save( fileName );
00192 
00193   mDirWatch.startScan();
00194 
00195   return ret;
00196 }
00197 
00198 KABC::Lock *ResourceLocalDir::lock()
00199 {
00200   return mLock;
00201 }
00202 
00203 void ResourceLocalDir::reload( const QString &file )
00204 {
00205   kdDebug(5800) << "ResourceLocalDir::reload()" << endl;
00206 
00207   if ( !isOpen() )
00208     return;
00209 
00210   kdDebug(5800) << "  File: '" << file << "'" << endl;
00211 
00212   mCalendar.close();
00213   load();
00214 
00215   emit resourceChanged( this );
00216 }
00217 
00218 
00219 bool ResourceLocalDir::deleteEvent(Event *event)
00220 {
00221   kdDebug(5800) << "ResourceLocalDir::deleteEvent" << endl;
00222   if ( deleteIncidenceFile(event) )
00223     return( mCalendar.deleteEvent( event ) );
00224   else
00225     return( false );
00226 }
00227 
00228 
00229 bool ResourceLocalDir::deleteTodo(Todo *todo)
00230 {
00231   if ( deleteIncidenceFile(todo) )
00232     return( mCalendar.deleteTodo( todo ) );
00233   else
00234     return( false );
00235 }
00236 
00237 
00238 bool ResourceLocalDir::deleteJournal( Journal *journal )
00239 {
00240   if ( deleteIncidenceFile( journal ) )
00241     return( mCalendar.deleteJournal( journal ) );
00242   else
00243     return( false );
00244 }
00245 
00246 
00247 void ResourceLocalDir::dump() const
00248 {
00249   ResourceCalendar::dump();
00250   kdDebug(5800) << "  Url: " << mURL.url() << endl;
00251 }
00252 
00253 bool ResourceLocalDir::deleteIncidenceFile(Incidence *incidence)
00254 {
00255   QFile file( mURL.path() + "/" + incidence->uid() );
00256   if ( !file.exists() )
00257     return true;
00258 
00259   mDirWatch.stopScan();
00260   bool removed = file.remove();
00261   mDirWatch.startScan();
00262   return removed;
00263 }
00264 
00265 #include "resourcelocaldir.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys