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 "dispatcher.h"
00021 #include "dispatcher_p.h"
00022
00023 #include <kdebug.h>
00024 #include <kconfig.h>
00025 #include <kcomponentdata.h>
00026 #include <assert.h>
00027 #include <kglobal.h>
00028
00029 namespace KSettings
00030 {
00031
00032 namespace Dispatcher
00033 {
00034
00035 K_GLOBAL_STATIC(DispatcherPrivate, d)
00036
00037 void registerComponent(const KComponentData &componentData, QObject *recv, const char *slot)
00038 {
00039 Q_ASSERT(componentData.isValid());
00040
00041
00042 QString componentName = componentData.componentName();
00043 kDebug(701) << componentName;
00044 d->m_componentName[recv] = componentName;
00045 if (!d->m_componentInfo.contains(componentName)) {
00046 d->m_componentInfo[componentName].componentData = componentData;
00047 }
00048 d->m_componentInfo[componentName].slotList.append(ComponentInfo::Slot(recv, slot));
00049
00050 ++(d->m_componentInfo[componentName].count);
00051 QObject::connect(recv, SIGNAL(destroyed(QObject *)), d, SLOT(unregisterComponent(QObject *)));
00052 }
00053
00054 KSharedConfig::Ptr configForComponentName(const QString &componentName)
00055 {
00056 kDebug(701) ;
00057 if (d->m_componentInfo.contains(componentName)) {
00058 KComponentData componentData = d->m_componentInfo[componentName].componentData;
00059 if (componentData.isValid()) {
00060 return componentData.config();
00061 }
00062 }
00063 kError(701) << "configForComponentName('" << componentName.constData()
00064 << "') could not find the KComponentData object" << endl;
00065 Q_ASSERT(!d->m_componentInfo.isEmpty());
00066 return d->m_componentInfo.constBegin()->componentData.config();
00067 }
00068
00069 QList<QString> componentNames()
00070 {
00071 kDebug(701) ;
00072 QList<QString> names;
00073 for (QMap<QString, ComponentInfo>::ConstIterator it = d->m_componentInfo.begin(); it != d->m_componentInfo.end(); ++it) {
00074 if ((*it).count > 0) {
00075 names.append(it.key());
00076 }
00077 }
00078 return names;
00079 }
00080
00081 void reparseConfiguration(const QString & componentName)
00082 {
00083 kDebug(701) << componentName;
00084
00085 if (! d->m_componentInfo.contains(componentName)) {
00086 return;
00087 }
00088
00089
00090 KSharedConfig::Ptr config = d->m_componentInfo[componentName].componentData.config();
00091 config->reparseConfiguration();
00092 foreach(const ComponentInfo::Slot& slot, d->m_componentInfo[componentName].slotList ) {
00093 QMetaObject::invokeMethod(slot.first, slot.second);
00094 }
00095 }
00096
00097 void syncConfiguration()
00098 {
00099 for (QMap<QString, ComponentInfo>::ConstIterator it = d->m_componentInfo.begin(); it != d->m_componentInfo.end(); ++it) {
00100 KSharedConfig::Ptr config = (*it).componentData.config();
00101 config->sync();
00102 }
00103 }
00104
00105 void DispatcherPrivate::unregisterComponent(QObject *obj)
00106 {
00107 if (!m_componentName.contains(obj)) {
00108 kWarning(701) << k_funcinfo << "Tried to unregister an object which is not already registered.";
00109 return;
00110 }
00111
00112 QString name = m_componentName[obj];
00113 m_componentName.remove(obj);
00114 --(m_componentInfo[name].count);
00115 kDebug(701) << "componentName=" << name << "refcount=" << m_componentInfo[name].count;
00116 Q_ASSERT(m_componentInfo[name].count >= 0);
00117 if (m_componentInfo[name].count == 0) {
00118 m_componentInfo.remove(name);
00119 }
00120 }
00121
00122 }
00123 }
00124
00125 #include "dispatcher_p.moc"