kutils Library API Documentation

dispatcher.cpp

00001 /* This file is part of the KDE project 00002 Copyright (C) 2003 Matthias Kretz <kretz@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00016 Boston, MA 02111-1307, USA. 00017 00018 */ 00019 00020 #include "ksettings/dispatcher.h" 00021 00022 #include <qsignal.h> 00023 00024 #include <kstaticdeleter.h> 00025 #include <kdebug.h> 00026 #include <kconfig.h> 00027 #include <assert.h> 00028 00029 namespace KSettings 00030 { 00031 00032 //class Dispatcher::DispatcherPrivate 00033 //{ 00034 //}; 00035 00036 static KStaticDeleter<Dispatcher> ksd_kpd; 00037 00038 Dispatcher * Dispatcher::m_self = 0; 00039 00040 Dispatcher * Dispatcher::self() 00041 { 00042 kdDebug( 701 ) << k_funcinfo << endl; 00043 if( m_self == 0 ) 00044 ksd_kpd.setObject( m_self, new Dispatcher() ); 00045 return m_self; 00046 } 00047 00048 Dispatcher::Dispatcher( QObject * parent, const char * name ) 00049 : QObject( parent, name ) 00050 //, d( 0 ) 00051 { 00052 kdDebug( 701 ) << k_funcinfo << endl; 00053 } 00054 00055 Dispatcher::~Dispatcher() 00056 { 00057 kdDebug( 701 ) << k_funcinfo << endl; 00058 //delete d; 00059 } 00060 00061 void Dispatcher::registerInstance( KInstance * instance, QObject * recv, const char * slot ) 00062 { 00063 assert( instance != 0 ); 00064 // keep the KInstance around and call 00065 // instance->config()->reparseConfiguration when the app should reparse 00066 QCString instanceName = instance->instanceName(); 00067 kdDebug( 701 ) << k_funcinfo << instanceName << endl; 00068 m_instanceName[ recv ] = instanceName; 00069 QSignal * sig; 00070 if( m_instanceInfo.contains( instanceName ) ) 00071 { 00072 sig = m_instanceInfo[ instanceName ].signal; 00073 } 00074 else 00075 { 00076 sig = new QSignal( this, "signal dispatcher" ); 00077 m_instanceInfo[ instanceName ].signal = sig; 00078 m_instanceInfo[ instanceName ].instance = instance; 00079 } 00080 sig->connect( recv, slot ); 00081 00082 ++m_instanceInfo[ instanceName ].count; 00083 connect( recv, SIGNAL( destroyed( QObject * ) ), this, SLOT( unregisterInstance( QObject * ) ) ); 00084 } 00085 00086 KConfig * Dispatcher::configForInstanceName( const QCString & instanceName ) 00087 { 00088 kdDebug( 701 ) << k_funcinfo << endl; 00089 if( m_instanceInfo.contains( instanceName ) ) 00090 { 00091 KInstance * inst = m_instanceInfo[ instanceName ].instance; 00092 if( inst ) 00093 return inst->config(); 00094 } 00095 //if( fallback ) 00096 //return new KSimpleConfig( instanceName ); 00097 return 0; 00098 } 00099 00100 QStrList Dispatcher::instanceNames() const 00101 { 00102 kdDebug( 701 ) << k_funcinfo << endl; 00103 QStrList names; 00104 for( QMap<QCString, InstanceInfo>::ConstIterator it = m_instanceInfo.begin(); it != m_instanceInfo.end(); ++it ) 00105 if( ( *it ).count > 0 ) 00106 names.append( it.key() ); 00107 return names; 00108 } 00109 00110 void Dispatcher::reparseConfiguration( const QCString & instanceName ) 00111 { 00112 kdDebug( 701 ) << k_funcinfo << instanceName << endl; 00113 // check if the instanceName is valid: 00114 if( ! m_instanceInfo.contains( instanceName ) ) 00115 return; 00116 // first we reparse the config of the instance so that the KConfig object 00117 // will be up to date 00118 m_instanceInfo[ instanceName ].instance->config()->reparseConfiguration(); 00119 QSignal * sig = m_instanceInfo[ instanceName ].signal; 00120 if( sig ) 00121 { 00122 kdDebug( 701 ) << "emit signal to instance" << endl; 00123 sig->activate(); 00124 } 00125 } 00126 00127 void Dispatcher::syncConfiguration() 00128 { 00129 for( QMap<QCString, InstanceInfo>::ConstIterator it = m_instanceInfo.begin(); it != m_instanceInfo.end(); ++it ) 00130 { 00131 ( *it ).instance->config()->sync(); 00132 } 00133 } 00134 00135 void Dispatcher::unregisterInstance( QObject * obj ) 00136 { 00137 kdDebug( 701 ) << k_funcinfo << endl; 00138 QCString name = m_instanceName[ obj ]; 00139 m_instanceName.remove( obj ); //obj will be destroyed when we return, so we better remove this entry 00140 --m_instanceInfo[ name ].count; 00141 if( m_instanceInfo[ name ].count == 0 ) 00142 { 00143 delete m_instanceInfo[ name ].signal; 00144 m_instanceInfo.remove( name ); 00145 } 00146 } 00147 00148 //X KInstance * Dispatcher::instanceForName( const QCString & instanceName ) 00149 //X { 00150 //X return m_instanceInfo[ instanceName ].instance; 00151 //X } 00152 00153 } //namespace 00154 00155 #include "dispatcher.moc" 00156 00157 // vim: sw=4 sts=4 et
KDE Logo
This file is part of the documentation for kutils Library Version 3.3.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 17 11:31:54 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003