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 KServiceTypeFactory::KServiceTypeFactory() 00029 : KSycocaFactory( KST_KServiceTypeFactory ) 00030 { 00031 _self = this; 00032 if (m_str) 00033 { 00034 // Read Header 00035 qint32 n; 00036 (*m_str) >> n; 00037 if (n > 1024) 00038 { 00039 KSycoca::flagError(); 00040 } 00041 else 00042 { 00043 QString str; 00044 qint32 i; 00045 for(;n;--n) 00046 { 00047 KSycocaEntry::read(*m_str, str); 00048 (*m_str) >> i; 00049 m_propertyTypeDict.insert(str, i); 00050 } 00051 } 00052 } 00053 } 00054 00055 00056 KServiceTypeFactory::~KServiceTypeFactory() 00057 { 00058 _self = 0; 00059 KServiceTypeProfile::clearCache(); 00060 } 00061 00062 KServiceTypeFactory * KServiceTypeFactory::self() 00063 { 00064 if (!_self) 00065 _self = new KServiceTypeFactory(); 00066 return _self; 00067 } 00068 00069 KServiceType::Ptr KServiceTypeFactory::findServiceTypeByName(const QString &_name) 00070 { 00071 if (!sycocaDict()) return KServiceType::Ptr(); // Error! 00072 assert (!KSycoca::self()->isBuilding()); 00073 int offset = sycocaDict()->find_string( _name ); 00074 if (!offset) return KServiceType::Ptr(); // Not found 00075 KServiceType::Ptr newServiceType(createEntry(offset)); 00076 00077 // Check whether the dictionary was right. 00078 if (newServiceType && (newServiceType->name() != _name)) 00079 { 00080 // No it wasn't... 00081 newServiceType = 0; // Not found 00082 } 00083 return newServiceType; 00084 } 00085 00086 QVariant::Type KServiceTypeFactory::findPropertyTypeByName(const QString &_name) 00087 { 00088 if (!sycocaDict()) 00089 return QVariant::Invalid; // Error! 00090 00091 assert (!KSycoca::self()->isBuilding()); 00092 00093 return static_cast<QVariant::Type>( m_propertyTypeDict.value( _name, QVariant::Invalid ) ); 00094 } 00095 00096 KServiceType::List KServiceTypeFactory::allServiceTypes() 00097 { 00098 KServiceType::List result; 00099 const KSycocaEntry::List list = allEntries(); 00100 for( KSycocaEntry::List::ConstIterator it = list.begin(); 00101 it != list.end(); 00102 ++it) 00103 { 00104 if ( (*it)->isType( KST_KServiceType ) ) { 00105 KServiceType::Ptr newServiceType = KServiceType::Ptr::staticCast( *it ); 00106 result.append( newServiceType ); 00107 } 00108 } 00109 return result; 00110 } 00111 00112 KServiceType * KServiceTypeFactory::createEntry(int offset) const 00113 { 00114 KServiceType *newEntry = 0; 00115 KSycocaType type; 00116 QDataStream *str = KSycoca::self()->findEntry(offset, type); 00117 if (!str) return 0; 00118 00119 switch(type) 00120 { 00121 case KST_KServiceType: 00122 newEntry = new KServiceType(*str, offset); 00123 break; 00124 default: 00125 kError(7011) << QString("KServiceTypeFactory: unexpected object entry in KSycoca database (type = %1)").arg((int)type) << endl; 00126 break; 00127 } 00128 if (newEntry && !newEntry->isValid()) 00129 { 00130 kError(7011) << "KServiceTypeFactory: corrupt object in KSycoca database!\n" << endl; 00131 delete newEntry; 00132 newEntry = 0; 00133 } 00134 return newEntry; 00135 } 00136 00137 KServiceTypeFactory *KServiceTypeFactory::_self = 0; 00138 00139 void KServiceTypeFactory::virtual_hook( int id, void* data ) 00140 { KSycocaFactory::virtual_hook( id, data ); } 00141 00142 // vim: ts=3 sw=3 et