KUtils
dispatcher.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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
00051 {
00052 kdDebug( 701 ) << k_funcinfo << endl;
00053 }
00054
00055 Dispatcher::~Dispatcher()
00056 {
00057 kdDebug( 701 ) << k_funcinfo << endl;
00058
00059 }
00060
00061 void Dispatcher::registerInstance( KInstance * instance, QObject * recv, const char * slot )
00062 {
00063 assert( instance != 0 );
00064
00065
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
00096
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
00114 if( ! m_instanceInfo.contains( instanceName ) )
00115 return;
00116
00117
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 );
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
00149
00150
00151
00152
00153 }
00154
00155 #include "dispatcher.moc"
00156
00157