summaryrefslogtreecommitdiffabout
path: root/microkde/kresources/managerimpl.cpp
Unidiff
Diffstat (limited to 'microkde/kresources/managerimpl.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--microkde/kresources/managerimpl.cpp353
1 files changed, 353 insertions, 0 deletions
diff --git a/microkde/kresources/managerimpl.cpp b/microkde/kresources/managerimpl.cpp
new file mode 100644
index 0000000..1baa6be
--- a/dev/null
+++ b/microkde/kresources/managerimpl.cpp
@@ -0,0 +1,353 @@
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#include <kglobal.h>
25
26#include <kapplication.h>
27#include <kdebug.h>
28#include <kconfig.h>
29#include <kstandarddirs.h>
30
31#include "resource.h"
32#include "factory.h"
33#include "managerimpl.h"
34
35using namespace KRES;
36
37ManagerImpl::ManagerImpl( const QString &family )
38 : mFamily( family ), mConfig( 0 ), mStdConfig( 0 ), mStandard( 0 ),
39 mFactory( 0 )
40
41{
42 kdDebug(5650) << "ManagerImpl::ManagerImpl()" << endl;
43
44
45}
46
47ManagerImpl::~ManagerImpl()
48{
49 kdDebug(5650) << "ManagerImpl::~ManagerImpl()" << endl;
50
51 Resource::List::ConstIterator it;
52 for ( it = mResources.begin(); it != mResources.end(); ++it ) {
53 delete *it;
54 }
55
56 delete mStdConfig;
57}
58
59void ManagerImpl::createStandardConfig()
60{
61 if ( !mStdConfig ) {
62 QString file = locateLocal( "data", KGlobal::getAppName()
63 + "/kresources/" + mFamily + "rc" );
64 mStdConfig = new KConfig( file );
65 }
66
67 mConfig = mStdConfig;
68}
69
70void ManagerImpl::readConfig( KConfig *cfg )
71{
72 kdDebug(5650) << "ManagerImpl::readConfig()" << endl;
73
74 delete mFactory;
75 mFactory = Factory::self( mFamily );
76
77 if ( !cfg ) {
78 createStandardConfig();
79 } else {
80 mConfig = cfg;
81 }
82
83 mStandard = 0;
84
85 mConfig->setGroup( "General" );
86
87 QStringList keys = mConfig->readListEntry( "ResourceKeys" );
88 keys += mConfig->readListEntry( "PassiveResourceKeys" );
89
90 QString standardKey = mConfig->readEntry( "Standard" );
91
92 for ( QStringList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
93 readResourceConfig( *it, false );
94 }
95
96}
97
98void ManagerImpl::writeConfig( KConfig *cfg )
99{
100//USqDebug("ManagerImpl::writeConfig begin this= %ul cfg=%ul", this, cfg);
101
102
103 kdDebug(5650) << "ManagerImpl::writeConfig()" << endl;
104
105 if ( !cfg ) {
106 createStandardConfig();
107 } else {
108 mConfig = cfg;
109 }
110
111 QStringList activeKeys;
112 QStringList passiveKeys;
113
114 // First write all keys, collect active and passive keys on the way
115 Resource::List::Iterator it;
116 for ( it = mResources.begin(); it != mResources.end(); ++it ) {
117 writeResourceConfig( *it, false );
118
119 QString key = (*it)->identifier();
120 if( (*it)->isActive() )
121 activeKeys.append( key );
122 else
123 passiveKeys.append( key );
124 }
125
126 // And then the general group
127
128 kdDebug(5650) << "Saving general info" << endl;
129 mConfig->setGroup( "General" );
130 mConfig->writeEntry( "ResourceKeys", activeKeys );
131 mConfig->writeEntry( "PassiveResourceKeys", passiveKeys );
132 if ( mStandard )
133 mConfig->writeEntry( "Standard", mStandard->identifier() );
134 else
135 mConfig->writeEntry( "Standard", "" );
136
137 mConfig->sync();
138 kdDebug(5650) << "ManagerImpl::save() finished" << endl;
139
140//US qDebug("ManagerImpl::writeConfig end this= %ul cfg=%ul", this, cfg);
141
142}
143
144void ManagerImpl::add( Resource *resource, bool useDCOP )
145{
146qDebug("ManagerImpl::add begin this= %ul resource=%ul", this, resource);
147
148 resource->setActive( true );
149
150 if ( mResources.isEmpty() ) {
151 mStandard = resource;
152 }
153
154 mResources.append( resource );
155
156 writeResourceConfig( resource, true );
157
158 qDebug("ManagerImpl::add end this= %ul resource=%ul", this, resource);
159
160}
161
162void ManagerImpl::remove( Resource *resource, bool useDCOP )
163{
164 if ( mStandard == resource ) mStandard = 0;
165 removeResource( resource );
166
167 mResources.remove( resource );
168
169 delete resource;
170
171 kdDebug(5650) << "Finished ManagerImpl::remove()" << endl;
172}
173
174void ManagerImpl::setActive( Resource *resource, bool active )
175{
176 if ( resource && resource->isActive() != active ) {
177 resource->setActive( active );
178 }
179}
180
181Resource *ManagerImpl::standardResource()
182{
183 return mStandard;
184}
185
186void ManagerImpl::setStandardResource( Resource *resource )
187{
188 mStandard = resource;
189}
190
191void ManagerImpl::resourceChanged( Resource *resource )
192{
193 writeResourceConfig( resource, true );
194
195
196// ManagerIface_stub allManagers( "*", "ManagerIface_" + mFamily.utf8() );
197// allManagers.dcopResourceModified( resource->identifier() );
198}
199
200// DCOP asynchronous functions
201//US since we work from inside the application, we call the methods directly.
202
203QStringList ManagerImpl::resourceNames()
204{
205 QStringList result;
206
207 Resource::List::ConstIterator it;
208 for ( it = mResources.begin(); it != mResources.end(); ++it ) {
209 result.append( (*it)->resourceName() );
210 }
211 return result;
212}
213
214Resource::List *ManagerImpl::resourceList()
215{
216 return &mResources;
217}
218
219QPtrList<Resource> ManagerImpl::resources()
220{
221 QPtrList<Resource> result;
222
223 Resource::List::ConstIterator it;
224 for ( it = mResources.begin(); it != mResources.end(); ++it ) {
225 result.append( *it );
226 }
227 return result;
228}
229
230QPtrList<Resource> ManagerImpl::resources( bool active )
231{
232 QPtrList<Resource> result;
233
234 Resource::List::ConstIterator it;
235 for ( it = mResources.begin(); it != mResources.end(); ++it ) {
236 if ( (*it)->isActive() == active ) {
237 result.append( *it );
238 }
239 }
240 return result;
241}
242
243void ManagerImpl::setListener( ManagerImplListener *listener )
244{
245 mListener = listener;
246}
247
248Resource* ManagerImpl::readResourceConfig( const QString& identifier,
249 bool checkActive )
250{
251 kdDebug() << "ManagerImpl::readResourceConfig() " << identifier << endl;
252
253// qDebug("ManagerImpl::readResourceConfig() %s", identifier.latin1());
254
255 mConfig->setGroup( "Resource_" + identifier );
256
257 QString type = mConfig->readEntry( "ResourceType" );
258 QString name = mConfig->readEntry( "ResourceName" );
259 Resource *resource = mFactory->resource( type, mConfig );
260 if ( !resource ) {
261 kdDebug(5650) << "Failed to create resource with id " << identifier << endl;
262 return 0;
263 }
264
265 if ( resource->identifier().isEmpty() )
266 resource->setIdentifier( identifier );
267
268 mConfig->setGroup( "General" );
269
270 QString standardKey = mConfig->readEntry( "Standard" );
271 if ( standardKey == identifier ) {
272 mStandard = resource;
273 }
274
275 if ( checkActive ) {
276 QStringList activeKeys = mConfig->readListEntry( "ResourceKeys" );
277 resource->setActive( activeKeys.contains( identifier ) );
278 }
279 mResources.append( resource );
280
281 return resource;
282}
283
284void ManagerImpl::writeResourceConfig( Resource *resource,
285 bool checkActive )
286{
287 QString key = resource->identifier();
288
289 kdDebug(5650) << "Saving resource " << key << endl;
290
291 if ( !mConfig ) createStandardConfig();
292
293 mConfig->setGroup( "Resource_" + key );
294 resource->writeConfig( mConfig );
295
296 mConfig->setGroup( "General" );
297 QString standardKey = mConfig->readEntry( "Standard" );
298
299 if ( resource == mStandard && standardKey != key )
300 mConfig->writeEntry( "Standard", resource->identifier() );
301 else if ( resource != mStandard && standardKey == key )
302 mConfig->writeEntry( "Standard", "" );
303
304 if ( checkActive ) {
305 QStringList activeKeys = mConfig->readListEntry( "ResourceKeys" );
306 if ( resource->isActive() && !activeKeys.contains( key ) ) {
307 activeKeys.append( resource->identifier() );
308 mConfig->writeEntry( "ResourceKeys", activeKeys );
309 } else if ( !resource->isActive() && activeKeys.contains( key ) ) {
310 activeKeys.remove( key );
311 mConfig->writeEntry( "ResourceKeys", activeKeys );
312 }
313 }
314
315 mConfig->sync();
316}
317
318void ManagerImpl::removeResource( Resource *resource )
319{
320 QString key = resource->identifier();
321
322 if ( !mConfig ) createStandardConfig();
323
324 mConfig->setGroup( "General" );
325 QStringList activeKeys = mConfig->readListEntry( "ResourceKeys" );
326 if ( activeKeys.contains( key ) ) {
327 activeKeys.remove( key );
328 mConfig->writeEntry( "ResourceKeys", activeKeys );
329 } else {
330 QStringList passiveKeys = mConfig->readListEntry( "PassiveResourceKeys" );
331 passiveKeys.remove( key );
332 mConfig->writeEntry( "PassiveResourceKeys", passiveKeys );
333 }
334
335 QString standardKey = mConfig->readEntry( "Standard" );
336 if ( standardKey == key ) {
337 mConfig->writeEntry( "Standard", "" );
338 }
339
340 mConfig->deleteGroup( "Resource_" + resource->identifier() );
341
342 mConfig->sync();
343}
344
345Resource* ManagerImpl::getResource( const QString& identifier )
346{
347 Resource::List::ConstIterator it;
348 for ( it = mResources.begin(); it != mResources.end(); ++it ) {
349 if ( (*it)->identifier() == identifier )
350 return *it;
351 }
352 return 0;
353}