libkcal Library API Documentation

resourcelocal.cpp

00001 /* 00002 This file is part of libkcal. 00003 00004 Copyright (c) 1998 Preston Brown 00005 Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00020 Boston, MA 02111-1307, USA. 00021 */ 00022 00023 #include <typeinfo> 00024 #include <stdlib.h> 00025 00026 #include <qdatetime.h> 00027 #include <qstring.h> 00028 #include <qptrlist.h> 00029 00030 #include <kdebug.h> 00031 #include <klocale.h> 00032 #include <kurl.h> 00033 #include <kstandarddirs.h> 00034 00035 #include "vcaldrag.h" 00036 #include "vcalformat.h" 00037 #include "icalformat.h" 00038 #include "exceptions.h" 00039 #include "incidence.h" 00040 #include "event.h" 00041 #include "todo.h" 00042 #include "journal.h" 00043 #include "filestorage.h" 00044 00045 #include <kresources/configwidget.h> 00046 00047 #include "resourcelocalconfig.h" 00048 00049 #include "resourcelocal.h" 00050 00051 using namespace KCal; 00052 00053 class ResourceLocal::Private 00054 { 00055 public: 00056 QDateTime mLastModified; 00057 }; 00058 00059 ResourceLocal::ResourceLocal( const KConfig* config ) 00060 : ResourceCached( config ), mLock( 0 ) 00061 { 00062 if ( config ) { 00063 QString url = config->readPathEntry( "CalendarURL" ); 00064 mURL = KURL( url ); 00065 00066 QString format = config->readEntry( "Format" ); 00067 if ( format == "ical" ) 00068 mFormat = new ICalFormat(); 00069 else if ( format == "vcal" ) 00070 mFormat = new VCalFormat(); 00071 else { 00072 mFormat = new ICalFormat(); 00073 } 00074 } else { 00075 mURL = KURL(); 00076 mFormat = new ICalFormat(); 00077 } 00078 init(); 00079 } 00080 00081 ResourceLocal::ResourceLocal( const QString& fileName ) 00082 : ResourceCached( 0 ) 00083 { 00084 mURL = KURL( fileName ); 00085 mFormat = new ICalFormat(); 00086 init(); 00087 } 00088 00089 00090 void ResourceLocal::writeConfig( KConfig* config ) 00091 { 00092 kdDebug(5800) << "ResourceLocal::writeConfig()" << endl; 00093 00094 ResourceCalendar::writeConfig( config ); 00095 config->writePathEntry( "CalendarURL", mURL.prettyURL() ); 00096 QString typeID = typeid( *mFormat ).name(); 00097 00098 if ( typeid( *mFormat ) == typeid( ICalFormat ) ) 00099 config->writeEntry( "Format", "ical" ); 00100 else if ( typeid( *mFormat ) == typeid( VCalFormat ) ) // if ( typeID == "ICalFormat" ) 00101 config->writeEntry( "Format", "vcal" ); 00102 else 00103 kdDebug(5800) << "ERROR: Unknown format type" << endl; 00104 } 00105 00106 void ResourceLocal::init() 00107 { 00108 d = new ResourceLocal::Private; 00109 00110 setType( "file" ); 00111 00112 mOpen = false; 00113 00114 connect( &mDirWatch, SIGNAL( dirty( const QString & ) ), 00115 SLOT( reload() ) ); 00116 connect( &mDirWatch, SIGNAL( created( const QString & ) ), 00117 SLOT( reload() ) ); 00118 connect( &mDirWatch, SIGNAL( deleted( const QString & ) ), 00119 SLOT( reload() ) ); 00120 00121 mLock = new KABC::Lock( mURL.path() ); 00122 00123 mDirWatch.addFile( mURL.path() ); 00124 mDirWatch.startScan(); 00125 } 00126 00127 00128 ResourceLocal::~ResourceLocal() 00129 { 00130 mDirWatch.stopScan(); 00131 00132 close(); 00133 00134 delete mLock; 00135 00136 delete d; 00137 } 00138 00139 bool ResourceLocal::doOpen() 00140 { 00141 kdDebug(5800) << "Opening resource " << resourceName() << " with URL " 00142 << mURL.prettyURL() << endl; 00143 00144 mOpen = true; 00145 00146 return true; 00147 } 00148 00149 QDateTime ResourceLocal::readLastModified() 00150 { 00151 QFileInfo fi( mURL.path() ); 00152 return fi.lastModified(); 00153 } 00154 00155 bool ResourceLocal::doLoad() 00156 { 00157 if ( !mOpen ) return true; 00158 00159 bool success; 00160 00161 if ( !KStandardDirs::exists( mURL.path() ) ) { 00162 kdDebug(5800) << "ResourceLocal::load(): File doesn't exist yet." << endl; 00163 success = true; 00164 } else { 00165 success = mCalendar.load( mURL.path() ); 00166 if ( success ) d->mLastModified = readLastModified(); 00167 } 00168 00169 return success; 00170 } 00171 00172 bool ResourceLocal::doSave() 00173 { 00174 if ( !mOpen ) return true; 00175 00176 bool success = mCalendar.save( mURL.path() ); 00177 00178 d->mLastModified = readLastModified(); 00179 00180 return success; 00181 } 00182 00183 KABC::Lock *ResourceLocal::lock() 00184 { 00185 return mLock; 00186 } 00187 00188 void ResourceLocal::reload() 00189 { 00190 kdDebug(5800) << "ResourceLocal::reload()" << endl; 00191 00192 if ( !mOpen ) return; 00193 00194 if ( d->mLastModified == readLastModified() ) { 00195 kdDebug(5800) << "ResourceLocal::reload(): file not modified since last read." 00196 << endl; 00197 return; 00198 } 00199 00200 mCalendar.close(); 00201 mCalendar.load( mURL.path() ); 00202 00203 emit resourceChanged( this ); 00204 } 00205 00206 void ResourceLocal::doClose() 00207 { 00208 if ( !mOpen ) return; 00209 00210 mCalendar.close(); 00211 mOpen = false; 00212 } 00213 00214 00215 void ResourceLocal::dump() const 00216 { 00217 ResourceCalendar::dump(); 00218 kdDebug(5800) << " Url: " << mURL.url() << endl; 00219 } 00220 00221 QString ResourceLocal::fileName() const 00222 { 00223 return mURL.path(); 00224 } 00225 00226 #include "resourcelocal.moc"
KDE Logo
This file is part of the documentation for libkcal Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Aug 27 12:49:12 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003