libkcal

resourcecalendar.cpp
1 /*
2  This file is part of libkcal.
3 
4  Copyright (c) 1998 Preston Brown <pbrown@kde.org>
5  Copyright (c) 2001-2004 Cornelius Schumacher <schumacher@kde.org>
6  Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
7  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Library General Public
11  License as published by the Free Software Foundation; either
12  version 2 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Library General Public License for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with this library; see the file COPYING.LIB. If not, write to
21  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  Boston, MA 02110-1301, USA.
23 */
24 
25 #include <tdeconfig.h>
26 #include <kdebug.h>
27 #include <tdelocale.h>
28 
29 #include "calendar.h"
30 
31 #include "resourcecalendar.h"
32 
33 using namespace KCal;
34 
35 ResourceCalendar::ResourceCalendar( const TDEConfig *config )
36  : KRES::Resource( config ), mResolveConflict( false )
37 {
38  mException = 0;
39 }
40 
41 ResourceCalendar::~ResourceCalendar()
42 {
43  delete mException;
44 }
45 
47 {
48  delete mException;
49  mException = 0;
50 }
51 
53 {
54  delete mException;
55  mException = exception;
56 }
57 
59 {
60  return mException;
61 }
62 
63 void ResourceCalendar::setResolveConflict( bool b)
64 {
65  mResolveConflict = b;
66 }
67 
69 {
70  TQString txt;
71 
72  txt += "<b>" + resourceName() + "</b>";
73  txt += "<br>";
74 
75  KRES::Factory *factory = KRES::Factory::self( "calendar" );
76  TQString t = factory->typeName( type() );
77  txt += i18n("Type: %1").arg( t );
78 
79  addInfoText( txt );
80 
81  return txt;
82 }
83 
84 void ResourceCalendar::writeConfig( TDEConfig* config )
85 {
86 // kdDebug(5800) << "ResourceCalendar::writeConfig()" << endl;
87 
88  KRES::Resource::writeConfig( config );
89 }
90 
91 Incidence *ResourceCalendar::incidence( const TQString &uid )
92 {
93  Incidence *i = event( uid );
94  if ( i ) return i;
95  i = todo( uid );
96  if ( i ) return i;
97  i = journal( uid );
98  return i;
99 }
100 
102 {
104  return incidence->accept( v );
105 }
106 
107 bool ResourceCalendar::addIncidence( Incidence *incidence, const TQString &subresource )
108 {
110  return incidence->accept( v );
111 }
112 
114 {
116  return incidence->accept( v );
117 }
118 
120 {
122 }
123 
124 void ResourceCalendar::setSubresourceActive( const TQString &, bool )
125 {
126 }
127 
128 bool ResourceCalendar::addSubresource( const TQString &, const TQString & )
129 {
130  return true;
131 }
132 
133 bool ResourceCalendar::removeSubresource( const TQString & )
134 {
135  return true;
136 }
137 
139 {
140  kdDebug(5800) << "Loading resource " + resourceName() << endl;
141 
142  mReceivedLoadError = false;
143 
144  bool success = true;
145  if ( !isOpen() )
146  success = open();
147  if ( success )
148  success = doLoad();
149 
150  if ( !success && !mReceivedLoadError )
151  loadError();
152 
153  // If the resource is read-only, we need to set its incidences to read-only,
154  // too. This can't be done at a lower-level, since the read-only setting
155  // happens at this level
156  if ( readOnly() ) {
157  Incidence::List incidences( rawIncidences() );
158  Incidence::List::Iterator it;
159  for ( it = incidences.begin(); it != incidences.end(); ++it ) {
160  (*it)->setReadOnly( true );
161  }
162  }
163 
164  kdDebug(5800) << "Done loading resource " + resourceName() << endl;
165 
166  return success;
167 }
168 
169 void ResourceCalendar::loadError( const TQString &err )
170 {
171  kdDebug(5800) << "Error loading resource: " << err << endl;
172 
173  mReceivedLoadError = true;
174 
175  TQString msg = i18n("Error while loading %1.\n") .arg( resourceName() );
176  if ( !err.isEmpty() ) {
177  msg += err;
178  }
179  emit resourceLoadError( this, msg );
180 }
181 
183 {
184  if ( !readOnly() ) {
185  kdDebug(5800) << "Save resource " + resourceName() << endl;
186 
187  mReceivedSaveError = false;
188 
189  if ( !isOpen() ) return true;
190  bool success = incidence ? doSave(incidence) : doSave();
191  if ( !success && !mReceivedSaveError ) saveError();
192 
193  return success;
194  } else {
195  // Read-only, just don't save...
196  kdDebug(5800) << "Don't save read-only resource " + resourceName() << endl;
197  return true;
198  }
199 }
200 
202 {
203  return doSave();
204 }
205 
206 void ResourceCalendar::saveError( const TQString &err )
207 {
208  kdDebug(5800) << "Error saving resource: " << err << endl;
209 
210  mReceivedSaveError = true;
211 
212  TQString msg = i18n("Error while saving %1.\n") .arg( resourceName() );
213  if ( !err.isEmpty() ) {
214  msg += err;
215  }
216  emit resourceSaveError( this, msg );
217 }
218 
219 bool ResourceCalendar::setValue( const TQString &key, const TQString &value )
220 {
221  Q_UNUSED( key );
222  Q_UNUSED( value );
223  return false;
224 }
225 
226 TQString ResourceCalendar::subresourceType( const TQString &resource )
227 {
228  Q_UNUSED( resource );
229  return TQString();
230 }
231 
232 bool ResourceCalendar::subresourceWritable( const TQString &resource ) const
233 {
234  if ( resource.isEmpty() ) {
235  return !readOnly();
236  } else {
237  return false;
238  }
239 }
240 
242 {
243 }
244 
246 {
247 }
248 
249 #include "resourcecalendar.moc"
Provides the main "calendar" object class.
static Incidence::List mergeIncidenceList(const Event::List &events, const Todo::List &todos, const Journal::List &journals)
Create a merged list of Events, Todos, and Journals.
Definition: calendar.cpp:1028
Calendar format related error class.
Definition: exceptions.h:65
virtual bool accept(Visitor &)
Accept IncidenceVisitor.
This class implements a visitor for adding an Incidence to a resource plus subresource supporting add...
Definition: incidence.h:74
This class implements a visitor for adding an Incidence to a resource supporting addEvent(),...
Definition: incidence.h:56
This class implements a visitor for deleting an Incidence from a resource supporting deleteEvent(),...
Definition: incidence.h:104
This class provides the base class common to all calendar components.
Definition: incidence.h:48
virtual Todo * todo(const TQString &uid)=0
Searches todolist for an event with this unique id.
void clearException()
Clears the exception status.
void setException(ErrorFormat *error)
Set exception for this object.
virtual Event * event(const TQString &uid)=0
Retrieves an event on the basis of the unique string ID.
virtual bool doLoad()=0
Do the actual loading of the resource data.
virtual bool doSave()=0
Do the actual saving of the resource data.
virtual bool setValue(const TQString &key, const TQString &value)
Sets a particular value of the resource's configuration.
virtual void setSubresourceActive(const TQString &, bool active)
(De-)activate a subresource.
virtual bool subresourceWritable(const TQString &) const
Is this subresource writable or not?
bool save(Incidence *incidence=0)
Save resource data.
virtual TQString subresourceType(const TQString &resource)
Returns the type of the subresource: "event", "todo" or "journal", TQString() if unknown/mixed.
virtual Event::List rawEvents(EventSortField sortField=EventSortUnsorted, SortDirection sortDirection=SortDirectionAscending)=0
Return unfiltered list of all events in calendar.
bool load()
Load resource data.
virtual TDE_DEPRECATED bool addIncidence(Incidence *)
Add incidence to resource.
virtual bool removeSubresource(const TQString &resource)
Remove a subresource with the id.
ErrorFormat * exception()
Returns an exception, if there is any, containing information about the last error that occurred.
virtual Journal * journal(const TQString &uid)=0
Return Journal with given unique id.
virtual Todo::List rawTodos(TodoSortField sortField=TodoSortUnsorted, SortDirection sortDirection=SortDirectionAscending)=0
Return list of all todos.
virtual TQString infoText() const
Return rich text with info about the resource.
void resourceSaveError(ResourceCalendar *, const TQString &error)
This signal is emitted when an error occurs during saving.
virtual Journal::List rawJournals(JournalSortField sortField=JournalSortUnsorted, SortDirection sortDirection=SortDirectionAscending)=0
Return list of all journals.
void saveError(const TQString &errorMessage=TQString())
A resource should call this function if a save error happens.
virtual bool deleteIncidence(Incidence *)
Delete incidence from resource.
virtual bool addSubresource(const TQString &resource, const TQString &parent)
Add a subresource with the name.
Incidence * incidence(const TQString &uid)
Return incidence with given unique id.
void resourceLoadError(ResourceCalendar *, const TQString &error)
This signal is emitted when an error occurs during loading.
virtual void addInfoText(TQString &) const
Add info text for concrete resources.
void loadError(const TQString &errorMessage=TQString())
A resource should call this function if a load error happens.
virtual void endAddingIncidences()
Called when we finish adding a batch of incidences.
Incidence::List rawIncidences()
Returns a list of all incideces.
virtual void beginAddingIncidences()
Called when we starting adding a batch of incidences.
Namespace KCal is for global classes, objects and/or functions in libkcal.
Definition: alarm.h:38