KDECore
kprotocolinfofactory.cpp
Go to the documentation of this file.00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1999 Torben Weis <weis@kde.org> 00003 Copyright (C) 2003 Waldo Bastian <bastian@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kprotocolinfofactory.h" 00021 00022 #include <kstandarddirs.h> 00023 #include <kdebug.h> 00024 #include <ksycoca.h> 00025 #include <ksycocadict.h> 00026 00027 K_GLOBAL_STATIC(KSycocaFactorySingleton<KProtocolInfoFactory>, kProtocolInfoFactoryInstance) 00028 00029 KProtocolInfoFactory::KProtocolInfoFactory() : KSycocaFactory( KST_KProtocolInfoFactory ) 00030 { 00031 kProtocolInfoFactoryInstance->instanceCreated(this); 00032 } 00033 00034 KProtocolInfoFactory::~KProtocolInfoFactory() 00035 { 00036 if (kProtocolInfoFactoryInstance.exists()) 00037 kProtocolInfoFactoryInstance->instanceDestroyed(this); 00038 } 00039 00040 KProtocolInfo* 00041 KProtocolInfoFactory::createEntry(int offset) const 00042 { 00043 KProtocolInfo *info = 0; 00044 KSycocaType type; 00045 QDataStream *str = KSycoca::self()->findEntry(offset, type); 00046 switch (type) 00047 { 00048 case KST_KProtocolInfo: 00049 info = new KProtocolInfo(*str, offset); 00050 break; 00051 default: 00052 return 0; 00053 } 00054 if (!info->isValid()) 00055 { 00056 delete info; 00057 info = 0; 00058 } 00059 return info; 00060 } 00061 00062 00063 QStringList KProtocolInfoFactory::protocols() const 00064 { 00065 QStringList res; 00066 const KSycocaEntry::List list = allEntries(); 00067 for( KSycocaEntry::List::const_iterator it = list.begin(); 00068 it != list.end(); 00069 ++it) { 00070 const KSycocaEntry *entry = (*it).data(); 00071 const KProtocolInfo *info = static_cast<const KProtocolInfo *>(entry); 00072 res.append( info->name() ); 00073 } 00074 return res; 00075 } 00076 00077 KProtocolInfo::List KProtocolInfoFactory::allProtocols() const 00078 { 00079 KProtocolInfo::List result; 00080 const KSycocaEntry::List list = allEntries(); 00081 for( KSycocaEntry::List::ConstIterator it = list.begin(); 00082 it != list.end(); 00083 ++it) { 00084 if ((*it)->isType(KST_KProtocolInfo)) { 00085 result.append(KProtocolInfo::Ptr::staticCast(*it)); 00086 } 00087 } 00088 return result; 00089 } 00090 00091 KProtocolInfo::Ptr KProtocolInfoFactory::findProtocol(const QString &protocol) 00092 { 00093 if (!sycocaDict()) return KProtocolInfo::Ptr(); // Error! 00094 00095 QMap<QString,KProtocolInfo::Ptr>::iterator it = m_cache.find(protocol); 00096 if (it != m_cache.end()) 00097 return *it; 00098 00099 int offset; 00100 00101 offset = sycocaDict()->find_string( protocol ); 00102 00103 if (!offset) return KProtocolInfo::Ptr(); // Not found; 00104 00105 KProtocolInfo::Ptr info(createEntry(offset)); 00106 00107 if (info && (info->name() != protocol)) 00108 { 00109 // No it wasn't... 00110 return KProtocolInfo::Ptr(); // Not found 00111 } 00112 m_cache.insert(protocol,info); 00113 return info; 00114 } 00115 00116 void KProtocolInfoFactory::virtual_hook( int id, void* data ) 00117 { KSycocaFactory::virtual_hook( id, data ); } 00118 00119 KProtocolInfoFactory* KProtocolInfoFactory::self() 00120 { 00121 return kProtocolInfoFactoryInstance->self(); 00122 }