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