Diffstat (limited to 'microkde/kresources/manager.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | microkde/kresources/manager.h | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/microkde/kresources/manager.h b/microkde/kresources/manager.h index b5e97fc..7e9e19a 100644 --- a/microkde/kresources/manager.h +++ b/microkde/kresources/manager.h | |||
@@ -1,108 +1,116 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of libkresources. | 2 | This file is part of libkresources. |
3 | 3 | ||
4 | Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> | 4 | Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> |
5 | Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org> | 5 | Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org> |
6 | Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> | 6 | Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> |
7 | Copyright (c) 2004 Ulf Schenk | ||
7 | 8 | ||
8 | This library is free software; you can redistribute it and/or | 9 | This library is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU Library General Public | 10 | modify it under the terms of the GNU Library General Public |
10 | License as published by the Free Software Foundation; either | 11 | License as published by the Free Software Foundation; either |
11 | version 2 of the License, or (at your option) any later version. | 12 | version 2 of the License, or (at your option) any later version. |
12 | 13 | ||
13 | This library is distributed in the hope that it will be useful, | 14 | This library is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | Library General Public License for more details. | 17 | Library General Public License for more details. |
17 | 18 | ||
18 | You should have received a copy of the GNU Library General Public License | 19 | You should have received a copy of the GNU Library General Public License |
19 | along with this library; see the file COPYING.LIB. If not, write to | 20 | along with this library; see the file COPYING.LIB. If not, write to |
20 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 21 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
21 | Boston, MA 02111-1307, USA. | 22 | Boston, MA 02111-1307, USA. |
22 | */ | 23 | */ |
23 | 24 | ||
25 | /* | ||
26 | Enhanced Version of the file for platform independent KDE tools. | ||
27 | Copyright (c) 2004 Ulf Schenk | ||
28 | |||
29 | $Id$ | ||
30 | */ | ||
31 | |||
24 | #ifndef KRESOURCES_MANAGER_H | 32 | #ifndef KRESOURCES_MANAGER_H |
25 | #define KRESOURCES_MANAGER_H | 33 | #define KRESOURCES_MANAGER_H |
26 | 34 | ||
27 | #include <qdict.h> | 35 | #include <qdict.h> |
28 | #include <qstringlist.h> | 36 | #include <qstringlist.h> |
29 | 37 | ||
30 | #include "factory.h" | 38 | #include "factory.h" |
31 | #include "managerimpl.h" | 39 | #include "managerimpl.h" |
32 | 40 | ||
33 | namespace KRES { | 41 | namespace KRES { |
34 | 42 | ||
35 | class Resource; | 43 | class Resource; |
36 | 44 | ||
37 | template<class T> | 45 | template<class T> |
38 | class ManagerListener | 46 | class ManagerListener |
39 | { | 47 | { |
40 | public: | 48 | public: |
41 | virtual void resourceAdded( T *resource ) = 0; | 49 | virtual void resourceAdded( T *resource ) = 0; |
42 | virtual void resourceModified( T *resource ) = 0; | 50 | virtual void resourceModified( T *resource ) = 0; |
43 | virtual void resourceDeleted( T *resource ) = 0; | 51 | virtual void resourceDeleted( T *resource ) = 0; |
44 | }; | 52 | }; |
45 | 53 | ||
46 | // TODO: | 54 | // TODO: |
47 | // The resource manager should provide some signals | 55 | // The resource manager should provide some signals |
48 | // to warn applications that resources have been added, | 56 | // to warn applications that resources have been added, |
49 | // removed or modified. | 57 | // removed or modified. |
50 | // | 58 | // |
51 | // The manager should also keep track of which (or at least | 59 | // The manager should also keep track of which (or at least |
52 | // how many) applications hve opened a resource, so that it | 60 | // how many) applications hve opened a resource, so that it |
53 | // is only closed if none of them is using it any more | 61 | // is only closed if none of them is using it any more |
54 | 62 | ||
55 | template<class T> | 63 | template<class T> |
56 | class Manager : private ManagerImplListener | 64 | class Manager : private ManagerImplListener |
57 | { | 65 | { |
58 | public: | 66 | public: |
59 | class Iterator | 67 | class Iterator |
60 | { | 68 | { |
61 | friend class Manager; | 69 | friend class Manager; |
62 | public: | 70 | public: |
63 | Iterator() {}; | 71 | Iterator() {}; |
64 | Iterator( const Iterator &it ) { mIt = it.mIt; } | 72 | Iterator( const Iterator &it ) { mIt = it.mIt; } |
65 | 73 | ||
66 | T *operator*() { return static_cast<T *>( *mIt ); } | 74 | T *operator*() { return static_cast<T *>( *mIt ); } |
67 | Iterator &operator++() { mIt++; return *this; } | 75 | Iterator &operator++() { mIt++; return *this; } |
68 | Iterator &operator++(int) { mIt++; return *this; } | 76 | Iterator &operator++(int) { mIt++; return *this; } |
69 | Iterator &operator--() { mIt--; return *this; } | 77 | Iterator &operator--() { mIt--; return *this; } |
70 | Iterator &operator--(int) { mIt--; return *this; } | 78 | Iterator &operator--(int) { mIt--; return *this; } |
71 | bool operator==( const Iterator &it ) { return mIt == it.mIt; } | 79 | bool operator==( const Iterator &it ) { return mIt == it.mIt; } |
72 | bool operator!=( const Iterator &it ) { return mIt != it.mIt; } | 80 | bool operator!=( const Iterator &it ) { return mIt != it.mIt; } |
73 | 81 | ||
74 | private: | 82 | private: |
75 | Resource::List::Iterator mIt; | 83 | Resource::List::Iterator mIt; |
76 | }; | 84 | }; |
77 | 85 | ||
78 | Iterator begin() | 86 | Iterator begin() |
79 | { | 87 | { |
80 | Iterator it; | 88 | Iterator it; |
81 | it.mIt = mImpl->resourceList()->begin(); | 89 | it.mIt = mImpl->resourceList()->begin(); |
82 | return it; | 90 | return it; |
83 | } | 91 | } |
84 | 92 | ||
85 | Iterator end() | 93 | Iterator end() |
86 | { | 94 | { |
87 | Iterator it; | 95 | Iterator it; |
88 | it.mIt = mImpl->resourceList()->end(); | 96 | it.mIt = mImpl->resourceList()->end(); |
89 | return it; | 97 | return it; |
90 | } | 98 | } |
91 | 99 | ||
92 | class ActiveIterator | 100 | class ActiveIterator |
93 | { | 101 | { |
94 | friend class Manager; | 102 | friend class Manager; |
95 | public: | 103 | public: |
96 | ActiveIterator() : mList( 0 ) {}; | 104 | ActiveIterator() : mList( 0 ) {}; |
97 | ActiveIterator( const ActiveIterator &it ) | 105 | ActiveIterator( const ActiveIterator &it ) |
98 | { | 106 | { |
99 | mIt = it.mIt; | 107 | mIt = it.mIt; |
100 | mList = it.mList; | 108 | mList = it.mList; |
101 | } | 109 | } |
102 | 110 | ||
103 | T *operator*() { return static_cast<T *>( *mIt ); } | 111 | T *operator*() { return static_cast<T *>( *mIt ); } |
104 | ActiveIterator &operator++() | 112 | ActiveIterator &operator++() |
105 | { | 113 | { |
106 | do { mIt++; } while ( checkActive() ); | 114 | do { mIt++; } while ( checkActive() ); |
107 | return *this; | 115 | return *this; |
108 | } | 116 | } |
@@ -138,61 +146,66 @@ class Manager : private ManagerImplListener | |||
138 | Resource::List *mList; | 146 | Resource::List *mList; |
139 | }; | 147 | }; |
140 | 148 | ||
141 | ActiveIterator activeBegin() | 149 | ActiveIterator activeBegin() |
142 | { | 150 | { |
143 | ActiveIterator it; | 151 | ActiveIterator it; |
144 | it.mIt = mImpl->resourceList()->begin(); | 152 | it.mIt = mImpl->resourceList()->begin(); |
145 | it.mList = mImpl->resourceList(); | 153 | it.mList = mImpl->resourceList(); |
146 | if ( it.mIt != mImpl->resourceList()->end() ) { | 154 | if ( it.mIt != mImpl->resourceList()->end() ) { |
147 | if ( !(*it)->isActive() ) it++; | 155 | if ( !(*it)->isActive() ) it++; |
148 | } | 156 | } |
149 | return it; | 157 | return it; |
150 | } | 158 | } |
151 | 159 | ||
152 | ActiveIterator activeEnd() | 160 | ActiveIterator activeEnd() |
153 | { | 161 | { |
154 | ActiveIterator it; | 162 | ActiveIterator it; |
155 | it.mIt = mImpl->resourceList()->end(); | 163 | it.mIt = mImpl->resourceList()->end(); |
156 | it.mList = mImpl->resourceList(); | 164 | it.mList = mImpl->resourceList(); |
157 | return it; | 165 | return it; |
158 | } | 166 | } |
159 | 167 | ||
160 | bool isEmpty() const { return mImpl->resourceList()->isEmpty(); } | 168 | bool isEmpty() const { return mImpl->resourceList()->isEmpty(); } |
161 | 169 | ||
162 | Manager( const QString &family ) | 170 | /** |
171 | Return true, if the manager manages syncable resources. | ||
172 | */ | ||
173 | bool manageSyncable() { return mImpl->manageSyncable(); } | ||
174 | |||
175 | Manager( const QString &family, bool syncable ) | ||
163 | { | 176 | { |
164 | mFactory = Factory::self( family ); | 177 | mFactory = Factory::self( family ); |
165 | // The managerimpl will use the same Factory object as the manager | 178 | // The managerimpl will use the same Factory object as the manager |
166 | // because of the Factory::self() pattern | 179 | // because of the Factory::self() pattern |
167 | mImpl = new ManagerImpl( family ); | 180 | mImpl = new ManagerImpl( family, syncable ); |
168 | mImpl->setListener( this ); | 181 | mImpl->setListener( this ); |
169 | 182 | ||
170 | mListeners = new QPtrList<ManagerListener<T> >; | 183 | mListeners = new QPtrList<ManagerListener<T> >; |
171 | } | 184 | } |
172 | 185 | ||
173 | virtual ~Manager() | 186 | virtual ~Manager() |
174 | { | 187 | { |
175 | mImpl->setListener( 0 ); | 188 | mImpl->setListener( 0 ); |
176 | delete mListeners; | 189 | delete mListeners; |
177 | delete mImpl; | 190 | delete mImpl; |
178 | } | 191 | } |
179 | 192 | ||
180 | /** | 193 | /** |
181 | Recreate Resource objects from configuration file. If cfg is 0, read standard | 194 | Recreate Resource objects from configuration file. If cfg is 0, read standard |
182 | configuration file. | 195 | configuration file. |
183 | */ | 196 | */ |
184 | void readConfig( KConfig *cfg = 0 ) | 197 | void readConfig( KConfig *cfg = 0 ) |
185 | { | 198 | { |
186 | mImpl->readConfig( cfg ); | 199 | mImpl->readConfig( cfg ); |
187 | } | 200 | } |
188 | 201 | ||
189 | /** | 202 | /** |
190 | Write configuration of Resource objects to configuration file. If cfg is 0, write | 203 | Write configuration of Resource objects to configuration file. If cfg is 0, write |
191 | to standard configuration file. | 204 | to standard configuration file. |
192 | */ | 205 | */ |
193 | void writeConfig( KConfig *cfg = 0 ) | 206 | void writeConfig( KConfig *cfg = 0 ) |
194 | { | 207 | { |
195 | mImpl->writeConfig( cfg ); | 208 | mImpl->writeConfig( cfg ); |
196 | } | 209 | } |
197 | 210 | ||
198 | /** | 211 | /** |
@@ -218,115 +231,116 @@ class Manager : private ManagerImplListener | |||
218 | { | 231 | { |
219 | if ( resource ) mImpl->setStandardResource( resource ); | 232 | if ( resource ) mImpl->setStandardResource( resource ); |
220 | } | 233 | } |
221 | 234 | ||
222 | void setActive( Resource *resource, bool active ) | 235 | void setActive( Resource *resource, bool active ) |
223 | { | 236 | { |
224 | if ( resource ) mImpl->setActive( resource, active ); | 237 | if ( resource ) mImpl->setActive( resource, active ); |
225 | } | 238 | } |
226 | 239 | ||
227 | /** | 240 | /** |
228 | Returns a list of the names of the reources managed by the | 241 | Returns a list of the names of the reources managed by the |
229 | Manager for this family. | 242 | Manager for this family. |
230 | */ | 243 | */ |
231 | QStringList resourceNames() const | 244 | QStringList resourceNames() const |
232 | { | 245 | { |
233 | return mImpl->resourceNames(); | 246 | return mImpl->resourceNames(); |
234 | } | 247 | } |
235 | 248 | ||
236 | ConfigWidget *configWidget( const QString& type, QWidget *parent = 0 ) | 249 | ConfigWidget *configWidget( const QString& type, QWidget *parent = 0 ) |
237 | { | 250 | { |
238 | return mFactory->resourceConfigWidget( type, parent ); | 251 | return mFactory->resourceConfigWidget( type, parent ); |
239 | } | 252 | } |
240 | 253 | ||
241 | /** | 254 | /** |
242 | Creates a new resource of type @param type, with default | 255 | Creates a new resource of type @param type, with default |
243 | settings. The resource is | 256 | settings. The resource is |
244 | not added to the manager, the application has to do that. | 257 | not added to the manager, the application has to do that. |
245 | Returns a pointer to a resource object or a null pointer | 258 | Returns a pointer to a resource object or a null pointer |
246 | if resource type doesn't exist. | 259 | if resource type doesn't exist. |
247 | 260 | ||
248 | @param type The type of the resource, one of those returned | 261 | @param type The type of the resource, one of those returned |
249 | by @ref resourceTypeNames() | 262 | by @ref resourceTypeNames() |
263 | * @param syncable If the resource should support syncing capabilities. | ||
250 | */ | 264 | */ |
251 | T *createResource( const QString& type ) | 265 | T *createResource( const QString& type ) |
252 | { | 266 | { |
253 | return (T *)( mFactory->resource( type, 0 ) ); | 267 | return (T *)( mFactory->resource( type, 0, mImpl->manageSyncable() ) ); |
254 | } | 268 | } |
255 | 269 | ||
256 | /** | 270 | /** |
257 | Returns a list of the names of all available resource types. | 271 | Returns a list of the names of all available resource types. |
258 | */ | 272 | */ |
259 | QStringList resourceTypeNames() const | 273 | QStringList resourceTypeNames() const |
260 | { | 274 | { |
261 | return mFactory->typeNames(); | 275 | return mFactory->typeNames(); |
262 | } | 276 | } |
263 | 277 | ||
264 | QStringList resourceTypeDescriptions() const | 278 | QStringList resourceTypeDescriptions() const |
265 | { | 279 | { |
266 | QStringList typeDescs; | 280 | QStringList typeDescs; |
267 | QStringList types = mFactory->typeNames(); | 281 | QStringList types = mFactory->typeNames(); |
268 | 282 | ||
269 | for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) { | 283 | for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) { |
270 | QString desc = mFactory->typeName( *it ); | 284 | QString desc = mFactory->typeName( *it ); |
271 | if ( !mFactory->typeDescription( *it ).isEmpty() ) | 285 | if ( !mFactory->typeDescription( *it ).isEmpty() ) |
272 | desc += " (" + mFactory->typeDescription( *it ) + ")"; | 286 | desc += " (" + mFactory->typeDescription( *it ) + ")"; |
273 | 287 | ||
274 | typeDescs.append( desc ); | 288 | typeDescs.append( desc ); |
275 | } | 289 | } |
276 | 290 | ||
277 | return typeDescs; | 291 | return typeDescs; |
278 | } | 292 | } |
279 | 293 | ||
280 | void resourceChanged( T *resource ) | 294 | void resourceChanged( T *resource ) |
281 | { | 295 | { |
282 | mImpl->resourceChanged( resource ); | 296 | mImpl->resourceChanged( resource ); |
283 | } | 297 | } |
284 | 298 | ||
285 | void addListener( ManagerListener<T> *listener ) | 299 | void addListener( ManagerListener<T> *listener ) |
286 | { | 300 | { |
287 | mListeners->append( listener ); | 301 | mListeners->append( listener ); |
288 | } | 302 | } |
289 | 303 | ||
290 | void removeListener( ManagerListener<T> *listener ) | 304 | void removeListener( ManagerListener<T> *listener ) |
291 | { | 305 | { |
292 | mListeners->remove( listener ); | 306 | mListeners->remove( listener ); |
293 | } | 307 | } |
294 | 308 | ||
295 | virtual void resourceAdded( Resource *res ) | 309 | virtual void resourceAdded( Resource *res ) |
296 | { | 310 | { |
297 | kdDebug(5650) << "Manager::resourceAdded " << res->resourceName() << endl; | 311 | kdDebug(5650) << "Manager::resourceAdded " << res->resourceName() << endl; |
298 | T* resource = (T *)( res ); | 312 | T* resource = (T *)( res ); |
299 | ManagerListener<T> *listener; | 313 | ManagerListener<T> *listener; |
300 | for ( listener = mListeners->first(); listener; listener = mListeners->next() ) | 314 | for ( listener = mListeners->first(); listener; listener = mListeners->next() ) |
301 | listener->resourceAdded( resource ); | 315 | listener->resourceAdded( resource ); |
302 | } | 316 | } |
303 | 317 | ||
304 | virtual void resourceModified( Resource *res ) | 318 | virtual void resourceModified( Resource *res ) |
305 | { | 319 | { |
306 | kdDebug(5650) << "Manager::resourceModified " << res->resourceName() << endl; | 320 | kdDebug(5650) << "Manager::resourceModified " << res->resourceName() << endl; |
307 | T* resource = (T *)( res ); | 321 | T* resource = (T *)( res ); |
308 | ManagerListener<T> *listener; | 322 | ManagerListener<T> *listener; |
309 | for ( listener = mListeners->first(); listener; listener = mListeners->next() ) | 323 | for ( listener = mListeners->first(); listener; listener = mListeners->next() ) |
310 | listener->resourceModified( resource ); | 324 | listener->resourceModified( resource ); |
311 | } | 325 | } |
312 | 326 | ||
313 | virtual void resourceDeleted( Resource *res ) | 327 | virtual void resourceDeleted( Resource *res ) |
314 | { | 328 | { |
315 | kdDebug(5650) << "Manager::resourceDeleted " << res->resourceName() << endl; | 329 | kdDebug(5650) << "Manager::resourceDeleted " << res->resourceName() << endl; |
316 | T* resource = (T *)( res ); | 330 | T* resource = (T *)( res ); |
317 | ManagerListener<T> *listener; | 331 | ManagerListener<T> *listener; |
318 | for ( listener = mListeners->first(); listener; listener = mListeners->next() ) { | 332 | for ( listener = mListeners->first(); listener; listener = mListeners->next() ) { |
319 | kdDebug(5650) << "Notifying a listener to Manager..." << endl; | 333 | kdDebug(5650) << "Notifying a listener to Manager..." << endl; |
320 | listener->resourceDeleted( resource ); | 334 | listener->resourceDeleted( resource ); |
321 | } | 335 | } |
322 | } | 336 | } |
323 | 337 | ||
324 | private: | 338 | private: |
325 | ManagerImpl *mImpl; | 339 | ManagerImpl *mImpl; |
326 | Factory *mFactory; | 340 | Factory *mFactory; |
327 | QPtrList<ManagerListener<T> > *mListeners; | 341 | QPtrList<ManagerListener<T> > *mListeners; |
328 | }; | 342 | }; |
329 | 343 | ||
330 | } | 344 | } |
331 | 345 | ||
332 | #endif | 346 | #endif |