• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

KCal Library

  • sources
  • kde-4.12
  • kdepimlibs
  • kcal
resourcelocal.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the kcal library.
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 */
31 #include "resourcelocal.h"
32 #include "resourcelocal_p.h"
33 #include "resourcelocalconfig.h"
34 #include "vcalformat.h"
35 #include "icalformat.h"
36 #include "calendarlocal.h"
37 #include "exceptions.h"
38 #include "incidence.h"
39 #include "event.h"
40 #include "todo.h"
41 #include "journal.h"
42 
43 #include <kresources/configwidget.h>
44 
45 #include <typeinfo>
46 #include <stdlib.h>
47 
48 #include <QtCore/QString>
49 
50 #include <kdebug.h>
51 #include <klocalizedstring.h>
52 #include <kurl.h>
53 #include <kdirwatch.h>
54 #include <kstandarddirs.h>
55 #include <kconfiggroup.h>
56 
57 
58 using namespace KCal;
59 
60 ResourceLocal::ResourceLocal()
61  : ResourceCached(), d( new ResourceLocal::Private() )
62 {
63  d->mLock = 0;
64  d->mURL = KUrl();
65  d->mFormat = new ICalFormat();
66  init();
67 }
68 
69 ResourceLocal::ResourceLocal( const KConfigGroup &group )
70  : ResourceCached( group ), d( new ResourceLocal::Private() )
71 {
72  d->mLock = 0;
73  QString url = group.readPathEntry( "CalendarURL", QString() );
74  d->mURL = KUrl( url );
75 
76  QString format = group.readEntry( "Format" );
77  if ( format == "ical" ) {
78  d->mFormat = new ICalFormat();
79  } else if ( format == "vcal" ) {
80  d->mFormat = new VCalFormat();
81  } else {
82  d->mFormat = new ICalFormat();
83  }
84  init();
85 }
86 
87 ResourceLocal::ResourceLocal( const QString &fileName )
88  : ResourceCached(), d( new ResourceLocal::Private )
89 {
90  d->mURL = KUrl::fromPath( fileName );
91  d->mFormat = new ICalFormat();
92  init();
93 }
94 
95 void ResourceLocal::writeConfig( KConfigGroup &group )
96 {
97  kDebug();
98 
99  ResourceCalendar::writeConfig( group );
100  group.writePathEntry( "CalendarURL", d->mURL.prettyUrl() );
101 
102  if ( typeid( *d->mFormat ) == typeid( ICalFormat ) ) {
103  group.writeEntry( "Format", "ical" );
104  } else if ( typeid( *d->mFormat ) == typeid( VCalFormat ) ) {
105  group.writeEntry( "Format", "vcal" );
106  } else {
107  kDebug() << "ERROR: Unknown format type";
108  }
109 }
110 
111 void ResourceLocal::init()
112 {
113 
114  setType( "file" );
115 
116  setSavePolicy( SaveDelayed );
117 
118  connect( &d->mDirWatch, SIGNAL(dirty(QString)),
119  SLOT(reload()) );
120  connect( &d->mDirWatch, SIGNAL(created(QString)),
121  SLOT(reload()) );
122  connect( &d->mDirWatch, SIGNAL(deleted(QString)),
123  SLOT(reload()) );
124 
125  d->mLock = new KABC::Lock( d->mURL.path() );
126 
127  d->mDirWatch.addFile( d->mURL.path() );
128  d->mDirWatch.startScan();
129 }
130 
131 ResourceLocal::~ResourceLocal()
132 {
133  d->mDirWatch.stopScan();
134  close();
135 
136  delete d->mLock;
137  delete d;
138 }
139 
140 KDateTime ResourceLocal::readLastModified()
141 {
142  QFileInfo fi( d->mURL.path() );
143  return KDateTime( fi.lastModified() ); // use local time zone
144 }
145 
146 bool ResourceLocal::doLoad( bool syncCache )
147 {
148  Q_UNUSED( syncCache );
149 
150  bool success;
151  if ( !KStandardDirs::exists( d->mURL.path() ) ) {
152  kDebug() << "File doesn't exist yet.";
153  // Save the empty calendar, so the calendar file will be created.
154  success = doSave( true );
155  } else {
156  success = calendar()->load( d->mURL.path() );
157  if ( success ) {
158  d->mLastModified = readLastModified();
159  }
160  }
161 
162  return success;
163 }
164 
165 bool ResourceLocal::doSave( bool syncCache )
166 {
167  Q_UNUSED( syncCache );
168  bool success = calendar()->save( d->mURL.path() );
169  kDebug() << "Save of " << d->mURL.path() << "was " << success;
170  d->mLastModified = readLastModified();
171 
172  return success;
173 }
174 
175 bool ResourceLocal::doSave( bool syncCache, Incidence *incidence )
176 {
177  return ResourceCached::doSave( syncCache, incidence );
178 }
179 
180 KABC::Lock *ResourceLocal::lock()
181 {
182  return d->mLock;
183 }
184 
185 bool ResourceLocal::doReload()
186 {
187  kDebug();
188 
189  if ( !isOpen() ) {
190  kDebug() << "trying to reload from a closed file";
191  return false;
192  }
193 
194  if ( d->mLastModified == readLastModified() ) {
195  kDebug() << "file not modified since last read.";
196  return false;
197  }
198 
199  calendar()->close();
200  calendar()->load( d->mURL.path() );
201  return true;
202 }
203 
204 void ResourceLocal::reload()
205 {
206  if ( doReload() ) {
207  emit resourceChanged( this );
208  }
209 }
210 
211 void ResourceLocal::dump() const
212 {
213  ResourceCalendar::dump();
214  kDebug() << " Url:" << d->mURL.url();
215 }
216 
217 QString ResourceLocal::fileName() const
218 {
219  return d->mURL.path();
220 }
221 
222 bool ResourceLocal::setFileName( const QString &fileName )
223 {
224  bool open = isOpen();
225  if ( open ) {
226  close();
227  }
228  delete d->mLock;
229  d->mDirWatch.stopScan();
230  d->mDirWatch.removeFile( d->mURL.path() );
231  d->mURL = KUrl::fromPath( fileName );
232  d->mLock = new KABC::Lock( d->mURL.path() );
233  d->mDirWatch.addFile( d->mURL.path() );
234  d->mDirWatch.startScan();
235  return true;
236 }
237 
238 bool ResourceLocal::setValue( const QString &key, const QString &value )
239 {
240  if ( key == "File" ) {
241  return setFileName( value );
242  } else {
243  return false;
244  }
245 }
246 
247 bool ResourceLocal::operator==( const ResourceLocal &other )
248 {
249  return
250  d->mURL == other.d->mURL &&
251  d->mLastModified == other.d->mLastModified;
252 }
253 
254 ResourceLocal &ResourceLocal::operator=( const ResourceLocal &other )
255 {
256  // check for self assignment
257  if ( &other == this ) {
258  return *this;
259  }
260 
261  d->mURL = other.d->mURL;
262  d->mLastModified = other.d->mLastModified;
263  return *this;
264 }
KCal::ResourceLocal::doReload
virtual bool doReload()
Called by reload() to reload the resource, if it is already open.
Definition: resourcelocal.cpp:185
KCal::ResourceLocal::setFileName
bool setFileName(const QString &fileName)
Sets the fileName for this resource.
Definition: resourcelocal.cpp:222
KCal::CalendarLocal::save
bool save()
Writes the calendar to disk.
Definition: calendarlocal.cpp:132
calendarlocal.h
This file is part of the API for handling calendar data and defines the CalendarLocal class...
KCal::ResourceLocal::~ResourceLocal
virtual ~ResourceLocal()
Destroys the resource.
Definition: resourcelocal.cpp:131
KCal::ResourceCached::setSavePolicy
void setSavePolicy(int policy)
Set save policy.
Definition: resourcecached.cpp:160
KCal::ResourceLocal::lock
KABC::Lock * lock()
Returns the lock.
Definition: resourcelocal.cpp:180
KCal::ResourceLocal::doSave
virtual bool doSave(bool syncCache)
Actually saves the data to the local file.
Definition: resourcelocal.cpp:165
KCal::CalendarLocal::load
bool load(const QString &fileName, CalFormat *format=0)
Loads a calendar on disk in vCalendar or iCalendar format into the current calendar.
Definition: calendarlocal.cpp:115
KRES::Resource::open
bool open()
KCal::ResourceLocal::readLastModified
KDateTime readLastModified()
Returns the date/time the local file was last modified.
Definition: resourcelocal.cpp:140
KRES::Resource::dump
virtual void dump() const
KRES::Resource::setType
void setType(const QString &type)
KCal::ResourceLocal::operator=
ResourceLocal & operator=(const ResourceLocal &other)
Sets this ResourceLocal equal to other.
Definition: resourcelocal.cpp:254
configwidget.h
resourcelocal.h
This file is part of the API for handling calendar data and defines the ResourceLocal class...
KCal::ResourceLocal::dump
void dump() const
Dumps the resource.
Definition: resourcelocal.cpp:211
KCal::ResourceCached::SaveDelayed
save after every change, after a 15 second delay
Definition: resourcecached.h:66
KCal::ICalFormat
iCalendar format implementation.
Definition: icalformat.h:52
todo.h
This file is part of the API for handling calendar data and defines the Todo class.
KCal::Incidence
Provides the abstract base class common to non-FreeBusy (Events, To-dos, Journals) calendar component...
Definition: incidence.h:68
exceptions.h
This file is part of the API for handling calendar data and defines the Exception and ErrorFormat cla...
KCal::CalendarLocal::close
void close()
Clears out the current calendar, freeing all used memory etc.
Definition: calendarlocal.cpp:158
KCal::ResourceLocal::fileName
QString fileName() const
Returns the fileName for this resource.
Definition: resourcelocal.cpp:217
KCal::ResourceLocal::reload
void reload()
Reload the resource data from the local file.
Definition: resourcelocal.cpp:204
KCal::ResourceCached::doSave
virtual bool doSave(bool syncCache)=0
Do the actual saving of the resource data.
KCal::ResourceLocal::ResourceLocal
ResourceLocal()
Constructs a resource using default configuration information.
Definition: resourcelocal.cpp:60
journal.h
This file is part of the API for handling calendar data and defines the Journal class.
KCal::ResourceLocal::doLoad
virtual bool doLoad(bool syncCache)
Actually loads the data from the local file.
Definition: resourcelocal.cpp:146
KCal::ResourceLocal
Provides a calendar resource stored as a local file.
Definition: resourcelocal.h:46
event.h
This file is part of the API for handling calendar data and defines the Event class.
incidence.h
This file is part of the API for handling calendar data and defines the Incidence class...
KRES::Resource::isOpen
bool isOpen() const
KCal::ResourceCached
This class provides a calendar resource using a local CalendarLocal object to cache the calendar data...
Definition: resourcecached.h:43
KCal::ResourceLocal::setValue
bool setValue(const QString &key, const QString &value)
Sets a value for this resource.
Definition: resourcelocal.cpp:238
vcalformat.h
This file is part of the API for handling calendar data and defines the VCalFormat base class...
KCal::ResourceLocal::writeConfig
virtual void writeConfig(KConfigGroup &group)
Writes KConfig config to a local file.
Definition: resourcelocal.cpp:95
KCal::ResourceLocal::operator==
bool operator==(const ResourceLocal &other)
Compares this ResourceLocal and other for equality.
Definition: resourcelocal.cpp:247
KCal::ResourceCalendar::resourceChanged
void resourceChanged(ResourceCalendar *)
This signal is emitted when the data in the resource has changed.
icalformat.h
This file is part of the API for handling calendar data and defines the ICalFormat class...
KRES::Resource::close
void close()
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KCal Library

Skip menu "KCal Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal