1 | /* |
2 | This file is part of the kcal library. |
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 "resourcecalendar.h" |
26 | |
27 | #include <kconfig.h> |
28 | #include <kdebug.h> |
29 | #include <klocalizedstring.h> |
30 | |
31 | |
32 | using namespace KCal; |
33 | |
34 | //@cond PRIVATE |
35 | class ResourceCalendar::Private |
36 | { |
37 | public: |
38 | Private() |
39 | : mResolveConflict( false ), |
40 | mNoReadOnlyOnLoad( false ), |
41 | mInhibitSave( false ) |
42 | {} |
43 | bool mResolveConflict; |
44 | bool mNoReadOnlyOnLoad; |
45 | bool mInhibitSave; // true to prevent saves |
46 | bool mReceivedLoadError; |
47 | bool mReceivedSaveError; |
48 | QString mLastError; |
49 | }; |
50 | //@endcond |
51 | |
52 | ResourceCalendar::ResourceCalendar() |
53 | : KRES::Resource(), d( new Private ) |
54 | { |
55 | } |
56 | |
57 | ResourceCalendar::ResourceCalendar( const KConfigGroup &group ) |
58 | : KRES::Resource( group ), |
59 | d( new Private ) |
60 | { |
61 | } |
62 | |
63 | ResourceCalendar::~ResourceCalendar() |
64 | { |
65 | delete d; |
66 | } |
67 | |
68 | bool ResourceCalendar::isResolveConflictSet() const |
69 | { |
70 | return d->mResolveConflict; |
71 | } |
72 | |
73 | void ResourceCalendar::setResolveConflict( bool b ) |
74 | { |
75 | d->mResolveConflict = b; |
76 | } |
77 | |
78 | QString ResourceCalendar::infoText() const |
79 | { |
80 | QString txt; |
81 | |
82 | txt += "<b>" + resourceName() + "</b>" ; |
83 | txt += "<br>" ; |
84 | |
85 | KRES::Factory *factory = KRES::Factory::self( "calendar" ); |
86 | QString t = factory->typeName( type() ); |
87 | txt += i18n( "Type: %1" , t ); |
88 | |
89 | addInfoText( txt ); |
90 | |
91 | return txt; |
92 | } |
93 | |
94 | void ResourceCalendar::writeConfig( KConfigGroup &group ) |
95 | { |
96 | KRES::Resource::writeConfig( group ); |
97 | } |
98 | |
99 | Incidence *ResourceCalendar::incidence( const QString &uid ) |
100 | { |
101 | Incidence *i = event( uid ); |
102 | if ( i ) { |
103 | return i; |
104 | } |
105 | |
106 | i = todo( uid ); |
107 | if ( i ) { |
108 | return i; |
109 | } |
110 | |
111 | i = journal( uid ); |
112 | return i; |
113 | } |
114 | |
115 | bool ResourceCalendar::addIncidence( Incidence *incidence ) |
116 | { |
117 | Incidence::AddVisitor<ResourceCalendar> v( this ); |
118 | return incidence->accept( v ); |
119 | } |
120 | |
121 | bool ResourceCalendar::deleteIncidence( Incidence *incidence ) |
122 | { |
123 | Incidence::DeleteVisitor<ResourceCalendar> v( this ); |
124 | return incidence->accept( v ); |
125 | } |
126 | |
127 | Incidence::List ResourceCalendar::rawIncidences() |
128 | { |
129 | return Calendar::mergeIncidenceList( rawEvents(), rawTodos(), rawJournals() ); |
130 | } |
131 | |
132 | void ResourceCalendar::setSubresourceActive( const QString &, bool ) |
133 | { |
134 | } |
135 | |
136 | bool ResourceCalendar::removeSubresource( const QString &resource ) |
137 | { |
138 | Q_UNUSED( resource ) |
139 | return true; |
140 | } |
141 | |
142 | bool ResourceCalendar::addSubresource( const QString &resource, const QString &parent ) |
143 | { |
144 | Q_UNUSED( resource ) |
145 | Q_UNUSED( parent ) |
146 | return true; |
147 | } |
148 | |
149 | QString ResourceCalendar::subresourceType( const QString &resource ) |
150 | { |
151 | Q_UNUSED( resource ) |
152 | return QString(); |
153 | } |
154 | |
155 | bool ResourceCalendar::load() |
156 | { |
157 | kDebug() << resourceName(); |
158 | |
159 | d->mReceivedLoadError = false; |
160 | |
161 | bool success = true; |
162 | if ( !isOpen() ) { |
163 | success = open(); //krazy:exclude=syscalls open is a class method |
164 | } |
165 | if ( success ) { |
166 | success = doLoad( false ); |
167 | } |
168 | if ( !success && !d->mReceivedLoadError ) { |
169 | loadError(); |
170 | } |
171 | |
172 | // If the resource is read-only, we need to set its incidences to read-only, |
173 | // too. This can't be done at a lower-level, since the read-only setting |
174 | // happens at this level |
175 | if ( !d->mNoReadOnlyOnLoad && readOnly() ) { |
176 | Incidence::List incidences( rawIncidences() ); |
177 | Incidence::List::Iterator it; |
178 | for ( it = incidences.begin(); it != incidences.end(); ++it ) { |
179 | (*it)->setReadOnly( true ); |
180 | } |
181 | } |
182 | |
183 | kDebug() << "Done loading resource" << resourceName(); |
184 | |
185 | return success; |
186 | } |
187 | |
188 | void ResourceCalendar::loadError( const QString &err ) |
189 | { |
190 | kDebug() << "Error loading resource:" << err; |
191 | |
192 | d->mReceivedLoadError = true; |
193 | |
194 | QString msg = i18n( "Error while loading %1.\n" , resourceName() ); |
195 | if ( !err.isEmpty() ) { |
196 | msg += err; |
197 | } |
198 | emit resourceLoadError( this, msg ); |
199 | } |
200 | |
201 | bool ResourceCalendar::receivedLoadError() const |
202 | { |
203 | return d->mReceivedLoadError; |
204 | } |
205 | |
206 | void ResourceCalendar::setReceivedLoadError( bool b ) |
207 | { |
208 | d->mReceivedLoadError = b; |
209 | } |
210 | |
211 | bool ResourceCalendar::save( Incidence *incidence ) |
212 | { |
213 | if ( d->mInhibitSave ) { |
214 | return true; |
215 | } |
216 | |
217 | if ( !readOnly() ) { |
218 | kDebug() << resourceName(); |
219 | |
220 | d->mReceivedSaveError = false; |
221 | |
222 | if ( !isOpen() ) { |
223 | kDebug() << "Trying to save into a closed resource" << resourceName(); |
224 | return true; |
225 | } |
226 | bool success = incidence ? doSave( false, incidence ) : doSave( false ); |
227 | if ( !success && !d->mReceivedSaveError ) { |
228 | saveError(); |
229 | } |
230 | return success; |
231 | } else { |
232 | // Read-only, just don't save... |
233 | kDebug() << "Don't save read-only resource" << resourceName(); |
234 | return true; |
235 | } |
236 | } |
237 | |
238 | bool ResourceCalendar::save( QString &err, Incidence *incidence ) |
239 | { |
240 | d->mLastError.clear(); |
241 | bool ret = save( incidence ); // a new mLastError may be set in here |
242 | err = d->mLastError; |
243 | return ret; |
244 | } |
245 | |
246 | bool ResourceCalendar::isSaving() |
247 | { |
248 | return false; |
249 | } |
250 | |
251 | bool ResourceCalendar::doSave( bool syncCache, Incidence *incidence ) |
252 | { |
253 | Q_UNUSED( incidence ); |
254 | return doSave( syncCache ); |
255 | } |
256 | |
257 | void ResourceCalendar::saveError( const QString &err ) |
258 | { |
259 | kDebug() << "Error saving resource:" << err; |
260 | |
261 | d->mReceivedSaveError = true; |
262 | QString msg = i18n( "Error while saving %1.\n" , resourceName() ); |
263 | if ( !err.isEmpty() ) { |
264 | msg += err; |
265 | } |
266 | d->mLastError = err; |
267 | emit resourceSaveError( this, msg ); |
268 | } |
269 | |
270 | QStringList ResourceCalendar::subresources() const |
271 | { |
272 | return QStringList(); |
273 | } |
274 | |
275 | bool ResourceCalendar::canHaveSubresources() const |
276 | { |
277 | return false; |
278 | } |
279 | |
280 | bool ResourceCalendar::subresourceActive( const QString &resource ) const |
281 | { |
282 | Q_UNUSED( resource ); |
283 | return true; |
284 | } |
285 | |
286 | QString ResourceCalendar::labelForSubresource( const QString &resource ) const |
287 | { |
288 | // the resource identifier is a sane fallback |
289 | return resource; |
290 | } |
291 | |
292 | QString ResourceCalendar::subresourceIdentifier( Incidence *incidence ) |
293 | { |
294 | Q_UNUSED( incidence ); |
295 | return QString(); |
296 | } |
297 | |
298 | bool ResourceCalendar::receivedSaveError() const |
299 | { |
300 | return d->mReceivedSaveError; |
301 | } |
302 | |
303 | void ResourceCalendar::setReceivedSaveError( bool b ) |
304 | { |
305 | d->mReceivedSaveError = b; |
306 | } |
307 | |
308 | void ResourceCalendar::setInhibitSave( bool inhibit ) |
309 | { |
310 | d->mInhibitSave = inhibit; |
311 | } |
312 | |
313 | bool ResourceCalendar::saveInhibited() const |
314 | { |
315 | return d->mInhibitSave; |
316 | } |
317 | |
318 | bool ResourceCalendar::setValue( const QString &key, const QString &value ) |
319 | { |
320 | Q_UNUSED( key ); |
321 | Q_UNUSED( value ); |
322 | return false; |
323 | } |
324 | |
325 | void ResourceCalendar::setNoReadOnlyOnLoad( bool noReadOnly ) |
326 | { |
327 | d->mNoReadOnlyOnLoad = noReadOnly; |
328 | } |
329 | |
330 | bool ResourceCalendar::noReadOnlyOnLoad() const |
331 | { |
332 | return d->mNoReadOnlyOnLoad; |
333 | } |
334 | |