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

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"
  • 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.5
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