KCal Library
resourcelocal.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00031 #include "resourcelocal.h"
00032 #include "resourcelocal_p.h"
00033 #include "resourcelocalconfig.h"
00034 #include "vcalformat.h"
00035 #include "icalformat.h"
00036 #include "calendarlocal.h"
00037 #include "exceptions.h"
00038 #include "incidence.h"
00039 #include "event.h"
00040 #include "todo.h"
00041 #include "journal.h"
00042
00043 #include <kresources/configwidget.h>
00044
00045 #include <typeinfo>
00046 #include <stdlib.h>
00047
00048 #include <QtCore/QString>
00049
00050 #include <kdebug.h>
00051 #include <klocale.h>
00052 #include <kurl.h>
00053 #include <kdirwatch.h>
00054 #include <kstandarddirs.h>
00055 #include <kconfiggroup.h>
00056
00057 #include "resourcelocal.moc"
00058
00059 using namespace KCal;
00060
00061 ResourceLocal::ResourceLocal()
00062 : ResourceCached(), d( new ResourceLocal::Private() )
00063 {
00064 d->mLock = 0;
00065 d->mURL = KUrl();
00066 d->mFormat = new ICalFormat();
00067 init();
00068 }
00069
00070 ResourceLocal::ResourceLocal( const KConfigGroup &group )
00071 : ResourceCached( group ), d( new ResourceLocal::Private() )
00072 {
00073 d->mLock = 0;
00074 QString url = group.readPathEntry( "CalendarURL", QString() );
00075 d->mURL = KUrl( url );
00076
00077 QString format = group.readEntry( "Format" );
00078 if ( format == "ical" ) {
00079 d->mFormat = new ICalFormat();
00080 } else if ( format == "vcal" ) {
00081 d->mFormat = new VCalFormat();
00082 } else {
00083 d->mFormat = new ICalFormat();
00084 }
00085 init();
00086 }
00087
00088 ResourceLocal::ResourceLocal( const QString &fileName )
00089 : ResourceCached(), d( new ResourceLocal::Private )
00090 {
00091 d->mURL = KUrl::fromPath( fileName );
00092 d->mFormat = new ICalFormat();
00093 init();
00094 }
00095
00096 void ResourceLocal::writeConfig( KConfigGroup &group )
00097 {
00098 kDebug(5800);
00099
00100 ResourceCalendar::writeConfig( group );
00101 group.writePathEntry( "CalendarURL", d->mURL.prettyUrl() );
00102
00103 if ( typeid( *d->mFormat ) == typeid( ICalFormat ) ) {
00104 group.writeEntry( "Format", "ical" );
00105 } else if ( typeid( *d->mFormat ) == typeid( VCalFormat ) ) {
00106 group.writeEntry( "Format", "vcal" );
00107 } else {
00108 kDebug(5800) << "ERROR: Unknown format type";
00109 }
00110 }
00111
00112 void ResourceLocal::init()
00113 {
00114
00115 setType( "file" );
00116
00117 setSavePolicy( SaveDelayed );
00118
00119 connect( &d->mDirWatch, SIGNAL( dirty( const QString & ) ),
00120 SLOT( reload() ) );
00121 connect( &d->mDirWatch, SIGNAL( created( const QString & ) ),
00122 SLOT( reload() ) );
00123 connect( &d->mDirWatch, SIGNAL( deleted( const QString & ) ),
00124 SLOT( reload() ) );
00125
00126 d->mLock = new KABC::Lock( d->mURL.path() );
00127
00128 d->mDirWatch.addFile( d->mURL.path() );
00129 d->mDirWatch.startScan();
00130 }
00131
00132 ResourceLocal::~ResourceLocal()
00133 {
00134 d->mDirWatch.stopScan();
00135 close();
00136
00137 delete d->mLock;
00138 delete d;
00139 }
00140
00141 KDateTime ResourceLocal::readLastModified()
00142 {
00143 QFileInfo fi( d->mURL.path() );
00144 return KDateTime( fi.lastModified() );
00145 }
00146
00147 bool ResourceLocal::doLoad( bool syncCache )
00148 {
00149 Q_UNUSED( syncCache );
00150
00151 bool success;
00152 if ( !KStandardDirs::exists( d->mURL.path() ) ) {
00153 kDebug(5800) << "File doesn't exist yet.";
00154
00155 success = doSave( true );
00156 } else {
00157 success = calendar()->load( d->mURL.path() );
00158 if ( success ) {
00159 d->mLastModified = readLastModified();
00160 }
00161 }
00162
00163 return success;
00164 }
00165
00166 bool ResourceLocal::doSave( bool syncCache )
00167 {
00168 Q_UNUSED( syncCache );
00169 bool success = calendar()->save( d->mURL.path() );
00170 kDebug(5800) << "Save of " << d->mURL.path() << "was " << success;
00171 d->mLastModified = readLastModified();
00172
00173 return success;
00174 }
00175
00176 bool ResourceLocal::doSave( bool syncCache, Incidence *incidence )
00177 {
00178 return ResourceCached::doSave( syncCache, incidence );
00179 }
00180
00181 KABC::Lock *ResourceLocal::lock()
00182 {
00183 return d->mLock;
00184 }
00185
00186 bool ResourceLocal::doReload()
00187 {
00188 kDebug(5800);
00189
00190 if ( !isOpen() ) {
00191 kDebug(5800) << "trying to reload from a closed file";
00192 return false;
00193 }
00194
00195 if ( d->mLastModified == readLastModified() ) {
00196 kDebug(5800) << "file not modified since last read.";
00197 return false;
00198 }
00199
00200 calendar()->close();
00201 calendar()->load( d->mURL.path() );
00202 return true;
00203 }
00204
00205 void ResourceLocal::reload()
00206 {
00207 if ( doReload() ) {
00208 emit resourceChanged( this );
00209 }
00210 }
00211
00212 void ResourceLocal::dump() const
00213 {
00214 ResourceCalendar::dump();
00215 kDebug(5800) << " Url:" << d->mURL.url();
00216 }
00217
00218 QString ResourceLocal::fileName() const
00219 {
00220 return d->mURL.path();
00221 }
00222
00223 bool ResourceLocal::setFileName( const QString &fileName )
00224 {
00225 bool open = isOpen();
00226 if ( open ) {
00227 close();
00228 }
00229 delete d->mLock;
00230 d->mDirWatch.stopScan();
00231 d->mDirWatch.removeFile( d->mURL.path() );
00232 d->mURL = KUrl::fromPath( fileName );
00233 d->mLock = new KABC::Lock( d->mURL.path() );
00234 d->mDirWatch.addFile( d->mURL.path() );
00235 d->mDirWatch.startScan();
00236 return true;
00237 }
00238
00239 bool ResourceLocal::setValue( const QString &key, const QString &value )
00240 {
00241 if ( key == "File" ) {
00242 return setFileName( value );
00243 } else {
00244 return false;
00245 }
00246 }
00247
00248 bool ResourceLocal::operator==( const ResourceLocal &other )
00249 {
00250 return
00251 d->mURL == other.d->mURL &&
00252 d->mLastModified == other.d->mLastModified;
00253 }
00254
00255 ResourceLocal &ResourceLocal::operator=( const ResourceLocal &other )
00256 {
00257 d->mURL = other.d->mURL;
00258 d->mLastModified = other.d->mLastModified;
00259 return *this;
00260 }