KDECore
kservicetypefactory.cpp
Go to the documentation of this file.00001 /* This file is part of the KDE libraries 00002 * Copyright (C) 1999 Waldo Bastian <bastian@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., 51 Franklin Street, Fifth Floor, 00016 * Boston, MA 02110-1301, USA. 00017 **/ 00018 00019 #include "kservicetypefactory.h" 00020 #include "ksycoca.h" 00021 #include "ksycocatype.h" 00022 #include "ksycocadict.h" 00023 #include "kservicetypeprofile.h" 00024 00025 #include <kdebug.h> 00026 #include <assert.h> 00027 00028 K_GLOBAL_STATIC(KSycocaFactorySingleton<KServiceTypeFactory>, kServiceTypeFactoryInstance) 00029 00030 KServiceTypeFactory::KServiceTypeFactory() 00031 : KSycocaFactory( KST_KServiceTypeFactory ) 00032 { 00033 kServiceTypeFactoryInstance->instanceCreated(this); 00034 if (!KSycoca::self()->isBuilding()) { 00035 QDataStream* str = stream(); 00036 Q_ASSERT(str); 00037 if (str) { 00038 // Read Header 00039 qint32 n; 00040 (*str) >> n; 00041 if (n > 1024) { 00042 KSycoca::flagError(); 00043 } else { 00044 QString string; 00045 qint32 i; 00046 for(;n;--n) { 00047 KSycocaEntry::read(*str, string); 00048 (*str) >> i; 00049 m_propertyTypeDict.insert(string, i); 00050 } 00051 } 00052 } 00053 } 00054 } 00055 00056 KServiceTypeFactory::~KServiceTypeFactory() 00057 { 00058 KServiceTypeProfile::clearCache(); 00059 if (kServiceTypeFactoryInstance.exists()) 00060 kServiceTypeFactoryInstance->instanceDestroyed(this); 00061 } 00062 00063 KServiceTypeFactory * KServiceTypeFactory::self() 00064 { 00065 return kServiceTypeFactoryInstance->self(); 00066 } 00067 00068 KServiceType::Ptr KServiceTypeFactory::findServiceTypeByName(const QString &_name) 00069 { 00070 if (!sycocaDict()) return KServiceType::Ptr(); // Error! 00071 assert (!KSycoca::self()->isBuilding()); 00072 int offset = sycocaDict()->find_string( _name ); 00073 if (!offset) return KServiceType::Ptr(); // Not found 00074 KServiceType::Ptr newServiceType(createEntry(offset)); 00075 00076 // Check whether the dictionary was right. 00077 if (newServiceType && (newServiceType->name() != _name)) 00078 { 00079 // No it wasn't... 00080 newServiceType = 0; // Not found 00081 } 00082 return newServiceType; 00083 } 00084 00085 QVariant::Type KServiceTypeFactory::findPropertyTypeByName(const QString &_name) 00086 { 00087 if (!sycocaDict()) 00088 return QVariant::Invalid; // Error! 00089 00090 assert (!KSycoca::self()->isBuilding()); 00091 00092 return static_cast<QVariant::Type>( m_propertyTypeDict.value( _name, QVariant::Invalid ) ); 00093 } 00094 00095 KServiceType::List KServiceTypeFactory::allServiceTypes() 00096 { 00097 KServiceType::List result; 00098 const KSycocaEntry::List list = allEntries(); 00099 for( KSycocaEntry::List::ConstIterator it = list.begin(); 00100 it != list.end(); 00101 ++it) 00102 { 00103 if ( (*it)->isType( KST_KServiceType ) ) { 00104 KServiceType::Ptr newServiceType = KServiceType::Ptr::staticCast( *it ); 00105 result.append( newServiceType ); 00106 } 00107 } 00108 return result; 00109 } 00110 00111 KServiceType * KServiceTypeFactory::createEntry(int offset) const 00112 { 00113 KServiceType *newEntry = 0; 00114 KSycocaType type; 00115 QDataStream *str = KSycoca::self()->findEntry(offset, type); 00116 if (!str) return 0; 00117 00118 switch(type) 00119 { 00120 case KST_KServiceType: 00121 newEntry = new KServiceType(*str, offset); 00122 break; 00123 default: 00124 kError(7011) << QString("KServiceTypeFactory: unexpected object entry in KSycoca database (type = %1)").arg((int)type) << endl; 00125 break; 00126 } 00127 if (newEntry && !newEntry->isValid()) 00128 { 00129 kError(7011) << "KServiceTypeFactory: corrupt object in KSycoca database!\n" << endl; 00130 delete newEntry; 00131 newEntry = 0; 00132 } 00133 return newEntry; 00134 } 00135 00136 void KServiceTypeFactory::virtual_hook( int id, void* data ) 00137 { KSycocaFactory::virtual_hook( id, data ); }