• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tderesources
 

tderesources

  • tderesources
managerimpl.cpp
1 /*
2  This file is part of libtderesources.
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., 51 Franklin Street, Fifth Floor,
21  Boston, MA 02110-1301, USA.
22 */
23 
24 #include <dcopclient.h>
25 
26 #include <tdeaboutdata.h>
27 #include <tdeapplication.h>
28 #include <kdebug.h>
29 #include <tdeconfig.h>
30 #include <kstandarddirs.h>
31 
32 #include "resource.h"
33 #include "factory.h"
34 #include "manager.h"
35 #include "managerimpl.h"
36 #include "manageriface_stub.h"
37 
38 using namespace KRES;
39 
40 ManagerImpl::ManagerImpl( ManagerNotifier *notifier, const TQString &family )
41  : DCOPObject( "ManagerIface_" + family.utf8() ),
42  mNotifier( notifier ),
43  mFamily( family ), mConfig( 0 ), mStdConfig( 0 ), mStandard( 0 ),
44  mFactory( 0 ), mConfigRead( false )
45 {
46  kdDebug(5650) << "ManagerImpl::ManagerImpl()" << endl;
47 
48  mId = TDEApplication::randomString( 8 );
49 
50  // Register with DCOP
51  if ( !kapp->dcopClient()->isRegistered() ) {
52  kapp->dcopClient()->registerAs( "TDEResourcesManager" );
53  kapp->dcopClient()->setDefaultObject( objId() );
54  }
55 
56  kdDebug(5650) << "Connecting DCOP signals..." << endl;
57  if ( !connectDCOPSignal( 0, "ManagerIface_" + family.utf8(),
58  "signalKResourceAdded( TQString, TQString )",
59  "dcopKResourceAdded( TQString, TQString )", false ) )
60  kdWarning(5650) << "Could not connect ResourceAdded signal!" << endl;
61 
62  if ( !connectDCOPSignal( 0, "ManagerIface_" + family.utf8(),
63  "signalKResourceModified( TQString, TQString )",
64  "dcopKResourceModified( TQString, TQString )", false ) )
65  kdWarning(5650) << "Could not connect ResourceModified signal!" << endl;
66 
67  if ( !connectDCOPSignal( 0, "ManagerIface_" + family.utf8(),
68  "signalKResourceDeleted( TQString, TQString )",
69  "dcopKResourceDeleted( TQString, TQString )", false ) )
70  kdWarning(5650) << "Could not connect ResourceDeleted signal!" << endl;
71 
72  kapp->dcopClient()->setNotifications( true );
73 }
74 
75 ManagerImpl::~ManagerImpl()
76 {
77  kdDebug(5650) << "ManagerImpl::~ManagerImpl()" << endl;
78 
79  Resource::List::ConstIterator it;
80  for ( it = mResources.begin(); it != mResources.end(); ++it ) {
81  delete *it;
82  }
83 
84  delete mStdConfig;
85 }
86 
87 void ManagerImpl::createStandardConfig()
88 {
89  if ( !mStdConfig ) {
90  TQString file = defaultConfigFile( mFamily );
91  mStdConfig = new TDEConfig( file );
92  }
93 
94  mConfig = mStdConfig;
95 }
96 
97 void ManagerImpl::readConfig( TDEConfig *cfg )
98 {
99  kdDebug(5650) << "ManagerImpl::readConfig()" << endl;
100 
101  delete mFactory;
102  mFactory = Factory::self( mFamily );
103 
104  if ( !cfg ) {
105  createStandardConfig();
106  } else {
107  mConfig = cfg;
108  }
109 
110  mStandard = 0;
111 
112  mConfig->setGroup( "General" );
113 
114  TQStringList keys = mConfig->readListEntry( "ResourceKeys" );
115  keys += mConfig->readListEntry( "PassiveResourceKeys" );
116 
117  TQString standardKey = mConfig->readEntry( "Standard" );
118 
119  for ( TQStringList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
120  readResourceConfig( *it, false );
121  }
122 
123  mConfigRead = true;
124 }
125 
126 void ManagerImpl::writeConfig( TDEConfig *cfg )
127 {
128  kdDebug(5650) << "ManagerImpl::writeConfig()" << endl;
129 
130  if ( !cfg ) {
131  createStandardConfig();
132  } else {
133  mConfig = cfg;
134  }
135 
136  TQStringList activeKeys;
137  TQStringList passiveKeys;
138 
139  // First write all keys, collect active and passive keys on the way
140  Resource::List::Iterator it;
141  for ( it = mResources.begin(); it != mResources.end(); ++it ) {
142  writeResourceConfig( *it, false );
143 
144  TQString key = (*it)->identifier();
145  if( (*it)->isActive() )
146  activeKeys.append( key );
147  else
148  passiveKeys.append( key );
149  }
150 
151  // And then the general group
152 
153  kdDebug(5650) << "Saving general info" << endl;
154  mConfig->setGroup( "General" );
155  mConfig->writeEntry( "ResourceKeys", activeKeys );
156  mConfig->writeEntry( "PassiveResourceKeys", passiveKeys );
157  if ( mStandard )
158  mConfig->writeEntry( "Standard", mStandard->identifier() );
159  else
160  mConfig->writeEntry( "Standard", "" );
161 
162  mConfig->sync();
163  kdDebug(5650) << "ManagerImpl::save() finished" << endl;
164 }
165 
166 void ManagerImpl::add( Resource *resource )
167 {
168  resource->setActive( true );
169 
170  if ( mResources.isEmpty() ) {
171  mStandard = resource;
172  }
173 
174  mResources.append( resource );
175 
176  if ( mConfigRead )
177  writeResourceConfig( resource, true );
178 
179  signalKResourceAdded( mId, resource->identifier() );
180 }
181 
182 void ManagerImpl::remove( Resource *resource )
183 {
184  if ( mStandard == resource ) mStandard = 0;
185  removeResource( resource );
186 
187  mResources.remove( resource );
188 
189  signalKResourceDeleted( mId, resource->identifier() );
190 
191  delete resource;
192 
193  kdDebug(5650) << "Finished ManagerImpl::remove()" << endl;
194 }
195 
196 void ManagerImpl::change( Resource *resource )
197 {
198  writeResourceConfig( resource, true );
199 
200  signalKResourceModified( mId, resource->identifier() );
201 }
202 
203 void ManagerImpl::setActive( Resource *resource, bool active )
204 {
205  if ( resource && resource->isActive() != active ) {
206  resource->setActive( active );
207  }
208 }
209 
210 Resource *ManagerImpl::standardResource()
211 {
212  return mStandard;
213 }
214 
215 void ManagerImpl::setStandardResource( Resource *resource )
216 {
217  mStandard = resource;
218 }
219 
220 // DCOP asynchronous functions
221 
222 void ManagerImpl::dcopKResourceAdded( TQString managerId, TQString resourceId )
223 {
224  if ( managerId == mId ) {
225  kdDebug(5650) << "Ignore DCOP notification to myself" << endl;
226  return;
227  }
228  kdDebug(5650) << "Receive DCOP call: added resource " << resourceId << endl;
229 
230  if ( getResource( resourceId ) ) {
231  kdDebug(5650) << "This resource is already known to me." << endl;
232  }
233 
234  if ( !mConfig ) createStandardConfig();
235 
236  mConfig->reparseConfiguration();
237  Resource *resource = readResourceConfig( resourceId, true );
238 
239  if ( resource ) {
240  mNotifier->notifyResourceAdded( resource );
241  } else
242  kdError() << "Received DCOP: resource added for unknown resource "
243  << resourceId << endl;
244 }
245 
246 void ManagerImpl::dcopKResourceModified( TQString managerId, TQString resourceId )
247 {
248  if ( managerId == mId ) {
249  kdDebug(5650) << "Ignore DCOP notification to myself" << endl;
250  return;
251  }
252  kdDebug(5650) << "Receive DCOP call: modified resource " << resourceId << endl;
253 
254  Resource *resource = getResource( resourceId );
255  if ( resource ) {
256  mNotifier->notifyResourceModified( resource );
257  } else
258  kdError() << "Received DCOP: resource modified for unknown resource "
259  << resourceId << endl;
260 }
261 
262 void ManagerImpl::dcopKResourceDeleted( TQString managerId, TQString resourceId )
263 {
264  if ( managerId == mId ) {
265  kdDebug(5650) << "Ignore DCOP notification to myself" << endl;
266  return;
267  }
268  kdDebug(5650) << "Receive DCOP call: deleted resource " << resourceId << endl;
269 
270  Resource *resource = getResource( resourceId );
271  if ( resource ) {
272  mNotifier->notifyResourceDeleted( resource );
273 
274  kdDebug(5650) << "Removing item from mResources" << endl;
275  // Now delete item
276  if ( mStandard == resource )
277  mStandard = 0;
278  mResources.remove( resource );
279  } else
280  kdError() << "Received DCOP: resource deleted for unknown resource "
281  << resourceId << endl;
282 }
283 
284 TQStringList ManagerImpl::resourceNames()
285 {
286  TQStringList result;
287 
288  Resource::List::ConstIterator it;
289  for ( it = mResources.begin(); it != mResources.end(); ++it ) {
290  result.append( (*it)->resourceName() );
291  }
292  return result;
293 }
294 
295 Resource::List *ManagerImpl::resourceList()
296 {
297  return &mResources;
298 }
299 
300 TQPtrList<Resource> ManagerImpl::resources()
301 {
302  TQPtrList<Resource> result;
303 
304  Resource::List::ConstIterator it;
305  for ( it = mResources.begin(); it != mResources.end(); ++it ) {
306  result.append( *it );
307  }
308  return result;
309 }
310 
311 TQPtrList<Resource> ManagerImpl::resources( bool active )
312 {
313  TQPtrList<Resource> result;
314 
315  Resource::List::ConstIterator it;
316  for ( it = mResources.begin(); it != mResources.end(); ++it ) {
317  if ( (*it)->isActive() == active ) {
318  result.append( *it );
319  }
320  }
321  return result;
322 }
323 
324 Resource *ManagerImpl::readResourceConfig( const TQString &identifier,
325  bool checkActive )
326 {
327  kdDebug(5650) << "ManagerImpl::readResourceConfig() " << identifier << endl;
328 
329  if ( !mFactory ) {
330  kdError(5650) << "ManagerImpl::readResourceConfig: mFactory is 0. Did the app forget to call readConfig?" << endl;
331  return 0;
332  }
333 
334  mConfig->setGroup( "Resource_" + identifier );
335 
336  TQString type = mConfig->readEntry( "ResourceType" );
337  TQString name = mConfig->readEntry( "ResourceName" );
338  Resource *resource = mFactory->resource( type, mConfig );
339  if ( !resource ) {
340  kdDebug(5650) << "Failed to create resource with id " << identifier << endl;
341  return 0;
342  }
343 
344  if ( resource->identifier().isEmpty() )
345  resource->setIdentifier( identifier );
346 
347  mConfig->setGroup( "General" );
348 
349  TQString standardKey = mConfig->readEntry( "Standard" );
350  if ( standardKey == identifier ) {
351  mStandard = resource;
352  }
353 
354  if ( checkActive ) {
355  TQStringList activeKeys = mConfig->readListEntry( "ResourceKeys" );
356  resource->setActive( activeKeys.contains( identifier ) );
357  }
358  mResources.append( resource );
359 
360  return resource;
361 }
362 
363 void ManagerImpl::writeResourceConfig( Resource *resource, bool checkActive )
364 {
365  TQString key = resource->identifier();
366 
367  kdDebug(5650) << "Saving resource " << key << endl;
368 
369  if ( !mConfig ) createStandardConfig();
370 
371  mConfig->setGroup( "Resource_" + key );
372  resource->writeConfig( mConfig );
373 
374  mConfig->setGroup( "General" );
375  TQString standardKey = mConfig->readEntry( "Standard" );
376 
377  if ( resource == mStandard && standardKey != key )
378  mConfig->writeEntry( "Standard", resource->identifier() );
379  else if ( resource != mStandard && standardKey == key )
380  mConfig->writeEntry( "Standard", "" );
381 
382  if ( checkActive ) {
383  TQStringList activeKeys = mConfig->readListEntry( "ResourceKeys" );
384  TQStringList passiveKeys = mConfig->readListEntry( "PassiveResourceKeys" );
385  if ( resource->isActive() ) {
386  if ( passiveKeys.contains( key ) ) { // remove it from passive list
387  passiveKeys.remove( key );
388  mConfig->writeEntry( "PassiveResourceKeys", passiveKeys );
389  }
390  if ( !activeKeys.contains( key ) ) { // add it to active list
391  activeKeys.append( key );
392  mConfig->writeEntry( "ResourceKeys", activeKeys );
393  }
394  } else if ( !resource->isActive() ) {
395  if ( activeKeys.contains( key ) ) { // remove it from active list
396  activeKeys.remove( key );
397  mConfig->writeEntry( "ResourceKeys", activeKeys );
398  }
399  if ( !passiveKeys.contains( key ) ) { // add it to passive list
400  passiveKeys.append( key );
401  mConfig->writeEntry( "PassiveResourceKeys", passiveKeys );
402  }
403  }
404  }
405 
406  mConfig->sync();
407 }
408 
409 void ManagerImpl::removeResource( Resource *resource )
410 {
411  TQString key = resource->identifier();
412 
413  if ( !mConfig ) createStandardConfig();
414 
415  mConfig->setGroup( "General" );
416  TQStringList activeKeys = mConfig->readListEntry( "ResourceKeys" );
417  if ( activeKeys.contains( key ) ) {
418  activeKeys.remove( key );
419  mConfig->writeEntry( "ResourceKeys", activeKeys );
420  } else {
421  TQStringList passiveKeys = mConfig->readListEntry( "PassiveResourceKeys" );
422  passiveKeys.remove( key );
423  mConfig->writeEntry( "PassiveResourceKeys", passiveKeys );
424  }
425 
426  TQString standardKey = mConfig->readEntry( "Standard" );
427  if ( standardKey == key ) {
428  mConfig->writeEntry( "Standard", "" );
429  }
430 
431  mConfig->deleteGroup( "Resource_" + resource->identifier() );
432  mConfig->sync();
433 }
434 
435 Resource *ManagerImpl::getResource( const TQString &identifier )
436 {
437  Resource::List::ConstIterator it;
438  for ( it = mResources.begin(); it != mResources.end(); ++it ) {
439  if ( (*it)->identifier() == identifier )
440  return *it;
441  }
442  return 0;
443 }
444 
445 TQString ManagerImpl::defaultConfigFile( const TQString &family )
446 {
447  return TQString( "tderesources/%1/stdrc" ).arg( family );
448 }
KRES::Resource
This class provides a resource which is managed in a general way.
Definition: resource.h:256
KRES::Resource::isActive
bool isActive() const
Return true, if the resource is active.
Definition: resource.cpp:168
KRES::Resource::writeConfig
virtual void writeConfig(TDEConfig *config)
Write configuration information for this resource to a configuration file.
Definition: resource.cpp:74
KRES::Resource::setActive
void setActive(bool active)
Sets, if the resource is active.
Definition: resource.cpp:163
KRES::Resource::identifier
TQString identifier() const
Returns a unique identifier.
Definition: resource.cpp:128

tderesources

Skip menu "tderesources"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

tderesources

Skip menu "tderesources"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tderesources by doxygen 1.9.1
This website is maintained by Timothy Pearson.