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

KCal Library

  • 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 <klocale.h>
52 #include <kurl.h>
53 #include <kdirwatch.h>
54 #include <kstandarddirs.h>
55 #include <kconfiggroup.h>
56 
57 #include "resourcelocal.moc"
58 
59 using namespace KCal;
60 
61 ResourceLocal::ResourceLocal()
62  : ResourceCached(), d( new ResourceLocal::Private() )
63 {
64  d->mLock = 0;
65  d->mURL = KUrl();
66  d->mFormat = new ICalFormat();
67  init();
68 }
69 
70 ResourceLocal::ResourceLocal( const KConfigGroup &group )
71  : ResourceCached( group ), d( new ResourceLocal::Private() )
72 {
73  d->mLock = 0;
74  QString url = group.readPathEntry( "CalendarURL", QString() );
75  d->mURL = KUrl( url );
76 
77  QString format = group.readEntry( "Format" );
78  if ( format == "ical" ) {
79  d->mFormat = new ICalFormat();
80  } else if ( format == "vcal" ) {
81  d->mFormat = new VCalFormat();
82  } else {
83  d->mFormat = new ICalFormat();
84  }
85  init();
86 }
87 
88 ResourceLocal::ResourceLocal( const QString &fileName )
89  : ResourceCached(), d( new ResourceLocal::Private )
90 {
91  d->mURL = KUrl::fromPath( fileName );
92  d->mFormat = new ICalFormat();
93  init();
94 }
95 
96 void ResourceLocal::writeConfig( KConfigGroup &group )
97 {
98  kDebug();
99 
100  ResourceCalendar::writeConfig( group );
101  group.writePathEntry( "CalendarURL", d->mURL.prettyUrl() );
102 
103  if ( typeid( *d->mFormat ) == typeid( ICalFormat ) ) {
104  group.writeEntry( "Format", "ical" );
105  } else if ( typeid( *d->mFormat ) == typeid( VCalFormat ) ) {
106  group.writeEntry( "Format", "vcal" );
107  } else {
108  kDebug() << "ERROR: Unknown format type";
109  }
110 }
111 
112 void ResourceLocal::init()
113 {
114 
115  setType( "file" );
116 
117  setSavePolicy( SaveDelayed );
118 
119  connect( &d->mDirWatch, SIGNAL(dirty(QString)),
120  SLOT(reload()) );
121  connect( &d->mDirWatch, SIGNAL(created(QString)),
122  SLOT(reload()) );
123  connect( &d->mDirWatch, SIGNAL(deleted(QString)),
124  SLOT(reload()) );
125 
126  d->mLock = new KABC::Lock( d->mURL.path() );
127 
128  d->mDirWatch.addFile( d->mURL.path() );
129  d->mDirWatch.startScan();
130 }
131 
132 ResourceLocal::~ResourceLocal()
133 {
134  d->mDirWatch.stopScan();
135  close();
136 
137  delete d->mLock;
138  delete d;
139 }
140 
141 KDateTime ResourceLocal::readLastModified()
142 {
143  QFileInfo fi( d->mURL.path() );
144  return KDateTime( fi.lastModified() ); // use local time zone
145 }
146 
147 bool ResourceLocal::doLoad( bool syncCache )
148 {
149  Q_UNUSED( syncCache );
150 
151  bool success;
152  if ( !KStandardDirs::exists( d->mURL.path() ) ) {
153  kDebug() << "File doesn't exist yet.";
154  // Save the empty calendar, so the calendar file will be created.
155  success = doSave( true );
156  } else {
157  success = calendar()->load( d->mURL.path() );
158  if ( success ) {
159  d->mLastModified = readLastModified();
160  }
161  }
162 
163  return success;
164 }
165 
166 bool ResourceLocal::doSave( bool syncCache )
167 {
168  Q_UNUSED( syncCache );
169  bool success = calendar()->save( d->mURL.path() );
170  kDebug() << "Save of " << d->mURL.path() << "was " << success;
171  d->mLastModified = readLastModified();
172 
173  return success;
174 }
175 
176 bool ResourceLocal::doSave( bool syncCache, Incidence *incidence )
177 {
178  return ResourceCached::doSave( syncCache, incidence );
179 }
180 
181 KABC::Lock *ResourceLocal::lock()
182 {
183  return d->mLock;
184 }
185 
186 bool ResourceLocal::doReload()
187 {
188  kDebug();
189 
190  if ( !isOpen() ) {
191  kDebug() << "trying to reload from a closed file";
192  return false;
193  }
194 
195  if ( d->mLastModified == readLastModified() ) {
196  kDebug() << "file not modified since last read.";
197  return false;
198  }
199 
200  calendar()->close();
201  calendar()->load( d->mURL.path() );
202  return true;
203 }
204 
205 void ResourceLocal::reload()
206 {
207  if ( doReload() ) {
208  emit resourceChanged( this );
209  }
210 }
211 
212 void ResourceLocal::dump() const
213 {
214  ResourceCalendar::dump();
215  kDebug() << " Url:" << d->mURL.url();
216 }
217 
218 QString ResourceLocal::fileName() const
219 {
220  return d->mURL.path();
221 }
222 
223 bool ResourceLocal::setFileName( const QString &fileName )
224 {
225  bool open = isOpen();
226  if ( open ) {
227  close();
228  }
229  delete d->mLock;
230  d->mDirWatch.stopScan();
231  d->mDirWatch.removeFile( d->mURL.path() );
232  d->mURL = KUrl::fromPath( fileName );
233  d->mLock = new KABC::Lock( d->mURL.path() );
234  d->mDirWatch.addFile( d->mURL.path() );
235  d->mDirWatch.startScan();
236  return true;
237 }
238 
239 bool ResourceLocal::setValue( const QString &key, const QString &value )
240 {
241  if ( key == "File" ) {
242  return setFileName( value );
243  } else {
244  return false;
245  }
246 }
247 
248 bool ResourceLocal::operator==( const ResourceLocal &other )
249 {
250  return
251  d->mURL == other.d->mURL &&
252  d->mLastModified == other.d->mLastModified;
253 }
254 
255 ResourceLocal &ResourceLocal::operator=( const ResourceLocal &other )
256 {
257  // check for self assignment
258  if ( &other == this ) {
259  return *this;
260  }
261 
262  d->mURL = other.d->mURL;
263  d->mLastModified = other.d->mLastModified;
264  return *this;
265 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed Nov 28 2012 21:56:55 by doxygen 1.8.1.2 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
  • Related Pages

kdepimlibs-4.9.3 API Reference

Skip menu "kdepimlibs-4.9.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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