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   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::doLoad()
00114 {
00115   kdDebug(5800) << "ResourceLocalDir::load()" << endl;
00116 
00117   mCalendar.close();
00118   QString dirName = mURL.path();
00119   bool success = true;
00120 
00121   if ( !( KStandardDirs::exists( dirName ) || KStandardDirs::exists( dirName + "/") ) ) {
00122     kdDebug(5800) << "ResourceLocalDir::load(): Directory '" << dirName << "' doesn't exist yet. Creating it..." << endl;
00123 
00124     // Create the directory. Use 0775 to allow group-writable if the umask
00125     // allows it (permissions will be 0775 & ~umask). This is desired e.g. for
00126     // group-shared directories!
00127     success = KStandardDirs::makeDir( dirName, 0775 );
00128   } else {
00129 
00130     kdDebug(5800) << "ResourceLocalDir::load(): '" << dirName << "'" << endl;
00131     QDir dir( dirName );
00132 
00133     QStringList entries = dir.entryList( QDir::Files | QDir::Readable );
00134 
00135     QStringList::ConstIterator it;
00136     for( it = entries.begin(); it != entries.end(); ++it ) {
00137       if ( (*it).endsWith( "~" ) ) // is backup file, ignore it
00138         continue;
00139 
00140       QString fileName = dirName + "/" + *it;
00141       kdDebug(5800) << " read '" << fileName << "'" << endl;
00142       CalendarLocal cal( mCalendar.timeZoneId() );
00143       if ( !doFileLoad( cal, fileName ) ) {
00144         success = false;
00145       }
00146     }
00147   }
00148 
00149   return success;
00150 }
00151 
00152 bool ResourceLocalDir::doFileLoad( CalendarLocal &cal, const QString &fileName )
00153 {
00154   if ( !cal.load( fileName ) )
00155     return false;
00156   Incidence::List incidences = cal.rawIncidences();
00157   Incidence::List::ConstIterator it;
00158   for ( it = incidences.constBegin(); it != incidences.constEnd(); ++it ) {
00159     Incidence *i = *it;
00160     if ( i ) mCalendar.addIncidence( i->clone() );
00161   }
00162   return true;
00163 }
00164 
00165 bool ResourceLocalDir::doSave()
00166 {
00167   Incidence::List list;
00168 
00169   list = addedIncidences();
00170   for (Incidence::List::iterator it = list.begin(); it != list.end(); ++it)
00171     doSave(*it);
00172 
00173   list = changedIncidences();
00174   for (Incidence::List::iterator it = list.begin(); it != list.end(); ++it)
00175     doSave(*it);
00176 
00177   return true;
00178 }
00179 
00180 bool ResourceLocalDir::doSave( Incidence *incidence )
00181 {
00182   mDirWatch.stopScan();  // do prohibit the dirty() signal and a following reload()
00183 
00184   QString fileName = mURL.path() + "/" + incidence->uid();
00185   kdDebug(5800) << "writing '" << fileName << "'" << endl;
00186 
00187   CalendarLocal cal( mCalendar.timeZoneId() );
00188   cal.addIncidence( incidence->clone() );
00189   cal.save( fileName );
00190 
00191   mDirWatch.startScan();
00192 
00193   return true;
00194 }
00195 
00196 KABC::Lock *ResourceLocalDir::lock()
00197 {
00198   return mLock;
00199 }
00200 
00201 void ResourceLocalDir::reload( const QString &file )
00202 {
00203   kdDebug(5800) << "ResourceLocalDir::reload()" << endl;
00204 
00205   if ( !isOpen() ) return;
00206 
00207   kdDebug(5800) << "  File: '" << file << "'" << endl;
00208 
00209   mCalendar.close();
00210   load();
00211 
00212   emit resourceChanged( this );
00213 }
00214 
00215 
00216 bool ResourceLocalDir::deleteEvent(Event *event)
00217 {
00218   kdDebug(5800) << "ResourceLocalDir::deleteEvent" << endl;
00219   if ( deleteIncidenceFile(event) )
00220     return( mCalendar.deleteEvent( event ) );
00221   else
00222     return( false );
00223 }
00224 
00225 
00226 bool ResourceLocalDir::deleteTodo(Todo *todo)
00227 {
00228   if ( deleteIncidenceFile(todo) )
00229     return( mCalendar.deleteTodo( todo ) );
00230   else
00231     return( false );
00232 }
00233 
00234 
00235 bool ResourceLocalDir::deleteJournal( Journal *journal )
00236 {
00237   if ( deleteIncidenceFile( journal ) )
00238     return( mCalendar.deleteJournal( journal ) );
00239   else
00240     return( false );
00241 }
00242 
00243 
00244 void ResourceLocalDir::dump() const
00245 {
00246   ResourceCalendar::dump();
00247   kdDebug(5800) << "  Url: " << mURL.url() << endl;
00248 }
00249 
00250 bool ResourceLocalDir::deleteIncidenceFile(Incidence *incidence)
00251 {
00252   QFile file( mURL.path() + "/" + incidence->uid() );
00253   if ( !file.exists() )
00254     return true;
00255 
00256   mDirWatch.stopScan();
00257   bool removed = file.remove();
00258   mDirWatch.startScan();
00259   return removed;
00260 }
00261 
00262 #include "resourcelocaldir.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys