• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.3 API Reference
  • KDE Home
  • Contact Us
 

kresources

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

KDE's Doxygen guidelines are available online.

kresources

Skip menu "kresources"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal