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