resourcelocaldir.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00033 #include "vcaldrag.h"
00034 #include "vcalformat.h"
00035 #include "icalformat.h"
00036 #include "exceptions.h"
00037 #include "incidence.h"
00038 #include "event.h"
00039 #include "todo.h"
00040 #include "journal.h"
00041 #include "filestorage.h"
00042
00043 #include <kresources/configwidget.h>
00044
00045 #include "resourcelocaldirconfig.h"
00046
00047 #include "resourcelocaldir.h"
00048
00049 using namespace KCal;
00050
00051 ResourceLocalDir::ResourceLocalDir( const KConfig* config )
00052 : ResourceCached( config ), mLock( 0 )
00053 {
00054 if ( config ) {
00055 readConfig( config );
00056 }
00057
00058 init();
00059 }
00060
00061 ResourceLocalDir::ResourceLocalDir( const QString& dirName )
00062 : ResourceCached( 0 )
00063 {
00064 mURL = KURL( dirName );
00065
00066 init();
00067 }
00068
00069
00070 void ResourceLocalDir::readConfig( const KConfig *config )
00071 {
00072 QString url = config->readPathEntry( "CalendarURL" );
00073 mURL = KURL( url );
00074 }
00075
00076 void ResourceLocalDir::writeConfig( KConfig *config )
00077 {
00078 kdDebug() << "ResourceLocalDir::writeConfig()" << endl;
00079
00080 ResourceCalendar::writeConfig( config );
00081
00082 config->writePathEntry( "CalendarURL", mURL.prettyURL() );
00083 }
00084
00085 void ResourceLocalDir::init()
00086 {
00087 setType( "dir" );
00088
00089 mOpen = false;
00090
00091 connect( &mDirWatch, SIGNAL( dirty( const QString & ) ),
00092 SLOT( reload( const QString & ) ) );
00093 connect( &mDirWatch, SIGNAL( created( const QString & ) ),
00094 SLOT( reload( const QString & ) ) );
00095 connect( &mDirWatch, SIGNAL( deleted( const QString & ) ),
00096 SLOT( reload( const QString & ) ) );
00097
00098 mLock = new KABC::Lock( mURL.path() );
00099
00100 mDirWatch.addDir( mURL.path(), true );
00101 mDirWatch.startScan();
00102 }
00103
00104
00105 ResourceLocalDir::~ResourceLocalDir()
00106 {
00107 close();
00108
00109 delete mLock;
00110 }
00111
00112 bool ResourceLocalDir::doOpen()
00113 {
00114 kdDebug(5800) << "Opening resource " << resourceName() << " with URL " << mURL.prettyURL() << endl;
00115
00116 mOpen = true;
00117
00118 return true;
00119 }
00120
00121 bool ResourceLocalDir::load()
00122 {
00123 kdDebug() << "ResourceLocalDir::load()" << endl;
00124
00125 if ( !mOpen ) return true;
00126
00127 mCalendar.close();
00128
00129 QString dirName = mURL.path();
00130
00131 kdDebug() << "ResourceLocalDir::load(): '" << dirName << "'" << endl;
00132
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( "~" ) )
00140 continue;
00141
00142 QString fileName = dirName + "/" + *it;
00143 kdDebug() << " read '" << fileName << "'" << endl;
00144 CalendarLocal cal( mCalendar.timeZoneId() );
00145 cal.load( fileName );
00146 Incidence::List incidences = cal.rawIncidences();
00147 Incidence *i = incidences.first();
00148 if ( i ) mCalendar.addIncidence( i->clone() );
00149 }
00150
00151 return true;
00152 }
00153
00154 bool ResourceLocalDir::save()
00155 {
00156 kdDebug() << "ResourceLocalDir::save()" << endl;
00157
00158 if ( !mOpen ) return true;
00159
00160 Incidence::List incidences = mCalendar.rawIncidences();
00161
00162 Incidence::List::ConstIterator it;
00163 for( it = incidences.begin(); it != incidences.end(); ++it ) {
00164 Incidence *i = *it;
00165 QString fileName = mURL.path() + "/" + i->uid();
00166 kdDebug() << "writing '" << fileName << "'" << endl;
00167
00168 CalendarLocal cal( mCalendar.timeZoneId() );
00169 cal.addIncidence( i->clone() );
00170 cal.save( fileName );
00171 }
00172
00173 return true;
00174 }
00175
00176 KABC::Lock *ResourceLocalDir::lock()
00177 {
00178 return mLock;
00179 }
00180
00181 void ResourceLocalDir::reload( const QString &file )
00182 {
00183 kdDebug() << "ResourceLocalDir::reload()" << endl;
00184
00185 if ( !mOpen ) return;
00186
00187 kdDebug() << " File: '" << file << "'" << endl;
00188
00189 mCalendar.close();
00190 load();
00191
00192 emit resourceChanged( this );
00193 }
00194
00195 void ResourceLocalDir::doClose()
00196 {
00197 if ( !mOpen ) return;
00198
00199 mCalendar.close();
00200 mOpen = false;
00201 }
00202
00203
00204 void ResourceLocalDir::deleteEvent(Event *event)
00205 {
00206 kdDebug(5800) << "ResourceLocalDir::deleteEvent" << endl;
00207 if ( deleteIncidenceFile(event) )
00208 mCalendar.deleteEvent( event );
00209 }
00210
00211
00212 void ResourceLocalDir::deleteTodo(Todo *todo)
00213 {
00214 if ( deleteIncidenceFile(todo) )
00215 mCalendar.deleteTodo( todo );
00216 }
00217
00218
00219 void ResourceLocalDir::update(IncidenceBase *)
00220 {
00221 }
00222
00223 void ResourceLocalDir::dump() const
00224 {
00225 ResourceCalendar::dump();
00226 kdDebug(5800) << " Url: " << mURL.url() << endl;
00227 }
00228
00229 bool ResourceLocalDir::deleteIncidenceFile(Incidence *incidence)
00230 {
00231 QFile file( mURL.path() + "/" + incidence->uid() );
00232 if ( !file.exists() )
00233 return true;
00234
00235 mDirWatch.stopScan();
00236 bool removed = file.remove();
00237 mDirWatch.startScan();
00238 return removed;
00239 }
00240
00241 #include "resourcelocaldir.moc"
This file is part of the documentation for libkcal Library Version 3.2.2.