libkcal

resourcelocal.cpp
1 /*
2  This file is part of libkcal.
3 
4  Copyright (c) 1998 Preston Brown <pbrown@kde.org>
5  Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include <typeinfo>
24 #include <stdlib.h>
25 
26 #include <tqdatetime.h>
27 #include <tqstring.h>
28 #include <tqptrlist.h>
29 
30 #include <kdebug.h>
31 #include <tdelocale.h>
32 #include <kurl.h>
33 #include <kstandarddirs.h>
34 
35 #include "vcaldrag.h"
36 #include "vcalformat.h"
37 #include "icalformat.h"
38 #include "exceptions.h"
39 #include "incidence.h"
40 #include "event.h"
41 #include "todo.h"
42 #include "journal.h"
43 #include "filestorage.h"
44 
45 #include <tderesources/configwidget.h>
46 
47 #include "resourcelocalconfig.h"
48 
49 #include "resourcelocal.h"
50 
51 using namespace KCal;
52 
53 class ResourceLocal::Private
54 {
55  public:
56  TQDateTime mLastModified;
57 };
58 
59 ResourceLocal::ResourceLocal( const TDEConfig* config )
60  : ResourceCached( config ), mLock( 0 )
61 {
62  if ( config ) {
63  TQString url = config->readPathEntry( "CalendarURL" );
64  mURL = KURL( url );
65 
66  TQString format = config->readEntry( "Format" );
67  if ( format == "ical" )
68  mFormat = new ICalFormat();
69  else if ( format == "vcal" )
70  mFormat = new VCalFormat();
71  else {
72  mFormat = new ICalFormat();
73  }
74  } else {
75  mURL = KURL();
76  mFormat = new ICalFormat();
77  }
78  init();
79 }
80 
81 ResourceLocal::ResourceLocal( const TQString& fileName )
82  : ResourceCached( 0 )
83 {
84  mURL = KURL( fileName );
85  mFormat = new ICalFormat();
86  init();
87 }
88 
89 
90 void ResourceLocal::writeConfig( TDEConfig* config )
91 {
92  kdDebug(5800) << "ResourceLocal::writeConfig()" << endl;
93 
94  ResourceCalendar::writeConfig( config );
95  config->writePathEntry( "CalendarURL", mURL.prettyURL() );
96  TQString typeID = typeid( *mFormat ).name();
97 
98  if ( typeid( *mFormat ) == typeid( ICalFormat ) )
99  config->writeEntry( "Format", "ical" );
100  else if ( typeid( *mFormat ) == typeid( VCalFormat ) ) // if ( typeID == "ICalFormat" )
101  config->writeEntry( "Format", "vcal" );
102  else
103  kdDebug(5800) << "ERROR: Unknown format type" << endl;
104 }
105 
106 void ResourceLocal::init()
107 {
108  d = new ResourceLocal::Private;
109 
110  setType( "file" );
111 
112  setSavePolicy( SaveDelayed );
113 
114  connect( &mDirWatch, TQ_SIGNAL( dirty( const TQString & ) ),
115  TQ_SLOT( reload() ) );
116  connect( &mDirWatch, TQ_SIGNAL( created( const TQString & ) ),
117  TQ_SLOT( reload() ) );
118  connect( &mDirWatch, TQ_SIGNAL( deleted( const TQString & ) ),
119  TQ_SLOT( reload() ) );
120 
121  mLock = new TDEABC::Lock( mURL.path() );
122 
123  mDirWatch.addFile( mURL.path() );
124  mDirWatch.startScan();
125 }
126 
127 
128 ResourceLocal::~ResourceLocal()
129 {
130  mDirWatch.stopScan();
131 
132  close();
133 
134  delete mLock;
135 
136  delete d;
137 }
138 
139 TQDateTime ResourceLocal::readLastModified()
140 {
141  TQFileInfo fi( mURL.path() );
142  return fi.lastModified();
143 }
144 
146 {
147  bool success;
148 
149  if ( !TDEStandardDirs::exists( mURL.path() ) ) {
150  kdDebug(5800) << "ResourceLocal::load(): File doesn't exist yet." << endl;
151  // Save the empty calendar, so the calendar file will be created.
152  success = doSave();
153  } else {
154  success = mCalendar.load( mURL.path() );
155  if ( success ) d->mLastModified = readLastModified();
156  }
157 
158  return success;
159 }
160 
162 {
163  bool success = mCalendar.save( mURL.path() );
164  d->mLastModified = readLastModified();
165 
166  return success;
167 }
168 
169 TDEABC::Lock *ResourceLocal::lock()
170 {
171  return mLock;
172 }
173 
175 {
176  kdDebug(5800) << "ResourceLocal::doReload()" << endl;
177 
178  if ( !isOpen() ) return false;
179 
180  if ( d->mLastModified == readLastModified() ) {
181  kdDebug(5800) << "ResourceLocal::reload(): file not modified since last read."
182  << endl;
183  return false;
184  }
185 
186  mCalendar.close();
187  mCalendar.load( mURL.path() );
188  return true;
189 }
190 
191 void ResourceLocal::reload()
192 {
193  if ( doReload() )
194  emit resourceChanged( this );
195 }
196 
197 void ResourceLocal::dump() const
198 {
199  ResourceCalendar::dump();
200  kdDebug(5800) << " Url: " << mURL.url() << endl;
201 }
202 
203 TQString ResourceLocal::fileName() const
204 {
205  return mURL.path();
206 }
207 
208 bool ResourceLocal::setFileName( const TQString &fileName )
209 {
210  bool open = isOpen();
211  if ( open ) close();
212  delete mLock;
213  mDirWatch.stopScan();
214  mDirWatch.removeFile( mURL.path() );
215  mURL = KURL( fileName );
216  mLock = new TDEABC::Lock( mURL.path() );
217  mDirWatch.addFile( mURL.path() );
218  mDirWatch.startScan();
219  return true;
220 }
221 
222 bool ResourceLocal::setValue( const TQString &key, const TQString &value )
223 {
224  if ( key == "File" ) {
225  return setFileName( value );
226  } else return false;
227 }
228 
229 
230 
231 #include "resourcelocal.moc"
bool load(const TQString &fileName, CalFormat *format=0)
Loads a calendar on disk in vCalendar or iCalendar format into the current calendar.
bool save(const TQString &fileName, CalFormat *format=0)
Writes out the calendar to disk in the specified format.
void resourceChanged(ResourceCalendar *)
This signal is emitted when the data in the resource has changed.
virtual bool doSave()
Do the actual saving of the resource data.
Definition: alarm.h:38
This class provides a calendar resource using a local CalendarLocal object to cache the calendar data...
This class implements the vCalendar format.
Definition: vcalformat.h:44
bool setValue(const TQString &key, const TQString &value)
Sets a particular value of the resource's configuration.
virtual bool doLoad()
Do the actual loading of the resource data.
ResourceLocal(const TDEConfig *)
Create resource from configuration information stored in a TDEConfig object.
virtual bool doReload()
Called by reload() to reload the resource, if it is already open.
This class implements the iCalendar format.
Definition: icalformat.h:43
void setSavePolicy(int policy)
Set save policy.
TDEABC::Lock * lock()
Return object for locking the resource.
void close()
Clears out the current calendar, freeing all used memory etc.