• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

KCal Library

resourcelocaldir.cpp

00001 /*
00002   This file is part of the kcal library.
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 "resourcelocaldir.h"
00023 #include "resourcelocaldir_p.h"
00024 #include "calendarlocal.h"
00025 #include "incidence.h"
00026 #include "event.h"
00027 #include "todo.h"
00028 #include "journal.h"
00029 
00030 #include "kresources/configwidget.h"
00031 
00032 #include <kdebug.h>
00033 #include <klocale.h>
00034 #include <kconfig.h>
00035 #include <kstandarddirs.h>
00036 #include <kconfiggroup.h>
00037 
00038 #include <QtCore/QString>
00039 #include <QtCore/QDir>
00040 
00041 #include <typeinfo>
00042 #include <stdlib.h>
00043 
00044 #include "resourcelocaldir.moc"
00045 
00046 using namespace KCal;
00047 
00048 ResourceLocalDir::ResourceLocalDir()
00049   : ResourceCached(), d( new KCal::ResourceLocalDir::Private )
00050 {
00051   d->init( this );
00052 }
00053 
00054 ResourceLocalDir::ResourceLocalDir( const KConfigGroup &group )
00055   : ResourceCached( group ), d( new KCal::ResourceLocalDir::Private )
00056 {
00057   readConfig( group );
00058   d->init( this );
00059 }
00060 
00061 ResourceLocalDir::ResourceLocalDir( const QString &dirName )
00062   : ResourceCached(), d( new KCal::ResourceLocalDir::Private( dirName ) )
00063 {
00064   d->init( this );
00065 }
00066 
00067 void ResourceLocalDir::readConfig( const KConfigGroup &group )
00068 {
00069   QString url = group.readPathEntry( "CalendarURL", QString() );
00070   d->mURL = KUrl( url );
00071 }
00072 
00073 void ResourceLocalDir::writeConfig( KConfigGroup &group )
00074 {
00075   kDebug();
00076 
00077   ResourceCalendar::writeConfig( group );
00078 
00079   group.writePathEntry( "CalendarURL", d->mURL.prettyUrl() );
00080 }
00081 
00082 void ResourceLocalDir::Private::init( ResourceLocalDir *rdir )
00083 {
00084   rdir->setType( "dir" );
00085 
00086   rdir->setSavePolicy( SaveDelayed );
00087 
00088   rdir->connect( &mDirWatch, SIGNAL( dirty( const QString & ) ),
00089                  SLOT( reload( const QString & ) ) );
00090   rdir->connect( &mDirWatch, SIGNAL( created( const QString & ) ),
00091                  SLOT( reload( const QString & ) ) );
00092   rdir->connect( &mDirWatch, SIGNAL( deleted( const QString & ) ),
00093                  SLOT( reload( const QString & ) ) );
00094 
00095   mLock = new KABC::Lock( mURL.path() );
00096 
00097   mDirWatch.addDir( mURL.path(), KDirWatch::WatchFiles );
00098   mDirWatch.startScan();
00099 }
00100 
00101 ResourceLocalDir::~ResourceLocalDir()
00102 {
00103   close();
00104 
00105   delete d->mLock;
00106   delete d;
00107 }
00108 
00109 bool ResourceLocalDir::doLoad( bool )
00110 {
00111   kDebug();
00112 
00113   calendar()->close();
00114   QString dirName = d->mURL.path();
00115   bool success = true;
00116 
00117   if ( !( KStandardDirs::exists( dirName ) || KStandardDirs::exists( dirName + '/' ) ) ) {
00118     kDebug() << "Directory '" << dirName << "' doesn't exist yet. Creating it.";
00119 
00120     // Create the directory. Use 0775 to allow group-writable if the umask
00121     // allows it (permissions will be 0775 & ~umask). This is desired e.g. for
00122     // group-shared directories!
00123     success = KStandardDirs::makeDir( dirName, 0775 );
00124   } else {
00125 
00126     kDebug() << dirName;
00127     QDir dir( dirName );
00128 
00129     QStringList entries = dir.entryList( QDir::Files | QDir::Readable );
00130 
00131     QStringList::ConstIterator it;
00132     for ( it = entries.begin(); it != entries.end(); ++it ) {
00133       if ( (*it).endsWith( '~' ) ) { // is backup file, ignore it
00134         continue;
00135       }
00136 
00137       QString fileName = dirName + '/' + *it;
00138       kDebug() << " read '" << fileName << "'";
00139       CalendarLocal cal( calendar()->timeSpec() );
00140       if ( !doFileLoad( cal, fileName ) ) {
00141         success = false;
00142       }
00143     }
00144   }
00145 
00146   return success;
00147 }
00148 
00149 bool ResourceLocalDir::doFileLoad( CalendarLocal &cal, const QString &fileName )
00150 {
00151   if ( !cal.load( fileName ) ) {
00152     return false;
00153   }
00154   Incidence::List incidences = cal.rawIncidences();
00155   Incidence::List::ConstIterator it;
00156   for ( it = incidences.constBegin(); it != incidences.constEnd(); ++it ) {
00157     Incidence *i = *it;
00158     if ( i ) {
00159       calendar()->addIncidence( i->clone() );
00160     }
00161   }
00162   return true;
00163 }
00164 
00165 bool ResourceLocalDir::doSave( bool syncCache )
00166 {
00167   Q_UNUSED( syncCache );
00168   Incidence::List list;
00169   bool success = true;
00170 
00171   list = addedIncidences();
00172   list += changedIncidences();
00173 
00174   for ( Incidence::List::iterator it = list.begin(); it != list.end(); ++it ) {
00175     if ( !doSave( *it ) ) {
00176       success = false;
00177     }
00178   }
00179 
00180   return success;
00181 }
00182 
00183 bool ResourceLocalDir::doSave( bool, Incidence *incidence )
00184 {
00185   d->mDirWatch.stopScan();  // do prohibit the dirty() signal and a following reload()
00186 
00187   QString fileName = d->mURL.path() + '/' + incidence->uid();
00188   kDebug() << "writing '" << fileName << "'";
00189 
00190   CalendarLocal cal( calendar()->timeSpec() );
00191   cal.addIncidence( incidence->clone() );
00192   const bool ret = cal.save( fileName );
00193 
00194   d->mDirWatch.startScan();
00195 
00196   return ret;
00197 }
00198 
00199 KABC::Lock *ResourceLocalDir::lock()
00200 {
00201   return d->mLock;
00202 }
00203 
00204 void ResourceLocalDir::reload( const QString &file )
00205 {
00206   kDebug();
00207 
00208   if ( !isOpen() ) {
00209     return;
00210   }
00211 
00212   kDebug() << "  File: '" << file << "'";
00213 
00214   calendar()->close();
00215   load();
00216 
00217   emit resourceChanged( this );
00218 }
00219 
00220 bool ResourceLocalDir::deleteEvent( Event *event )
00221 {
00222   kDebug();
00223   if ( d->deleteIncidenceFile( event ) ) {
00224     return calendar()->deleteEvent( event );
00225   } else {
00226     return false;
00227   }
00228 }
00229 
00230 void ResourceLocalDir::deleteAllEvents()
00231 {
00232   calendar()->deleteAllEvents();
00233 }
00234 
00235 bool ResourceLocalDir::deleteTodo( Todo *todo )
00236 {
00237   if ( d->deleteIncidenceFile( todo ) ) {
00238     return calendar()->deleteTodo( todo );
00239   } else {
00240     return false;
00241   }
00242 }
00243 
00244 void ResourceLocalDir::deleteAllTodos()
00245 {
00246   calendar()->deleteAllTodos();
00247 }
00248 
00249 bool ResourceLocalDir::deleteJournal( Journal *journal )
00250 {
00251   if ( d->deleteIncidenceFile( journal ) ) {
00252     return calendar()->deleteJournal( journal );
00253   } else {
00254     return false;
00255   }
00256 }
00257 
00258 void ResourceLocalDir::deleteAllJournals()
00259 {
00260   calendar()->deleteAllJournals();
00261 }
00262 
00263 void ResourceLocalDir::dump() const
00264 {
00265   ResourceCalendar::dump();
00266   kDebug() << "  Url:" << d->mURL.url();
00267 }
00268 
00269 bool ResourceLocalDir::Private::deleteIncidenceFile( Incidence *incidence )
00270 {
00271   QFile file( mURL.path() + '/' + incidence->uid() );
00272   if ( !file.exists() ) {
00273     return true;
00274   }
00275 
00276   mDirWatch.stopScan();
00277   bool removed = file.remove();
00278   mDirWatch.startScan();
00279   return removed;
00280 }

KCal Library

Skip menu "KCal Library"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal