KDECore
kmimetypefactory.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 * Copyright (C) 2006-2009 David Faure <faure@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 "kmimetypefactory.h" 00021 #include <ksycoca.h> 00022 #include <ksycocadict_p.h> 00023 #include <kdebug.h> 00024 00025 extern int servicesDebugArea(); 00026 00027 K_GLOBAL_STATIC(KSycocaFactorySingleton<KMimeTypeFactory>, kMimeTypeFactoryInstance) 00028 00029 KMimeTypeFactory::KMimeTypeFactory() 00030 : KSycocaFactory( KST_KMimeTypeFactory ) 00031 { 00032 kMimeTypeFactoryInstance->instanceCreated(this); 00033 } 00034 00035 KMimeTypeFactory::~KMimeTypeFactory() 00036 { 00037 if (kMimeTypeFactoryInstance.exists()) 00038 kMimeTypeFactoryInstance->instanceDestroyed(this); 00039 } 00040 00041 KMimeTypeFactory * KMimeTypeFactory::self() 00042 { 00043 return kMimeTypeFactoryInstance->self(); 00044 } 00045 00046 int KMimeTypeFactory::entryOffset(const QString& mimeTypeName) 00047 { 00048 if (!sycocaDict()) 00049 return -1; // Error! 00050 assert (!KSycoca::self()->isBuilding()); 00051 const int offset = sycocaDict()->find_string(mimeTypeName); 00052 return offset; 00053 } 00054 00055 int KMimeTypeFactory::serviceOffersOffset(const QString& mimeTypeName) 00056 { 00057 const int offset = entryOffset(mimeTypeName); 00058 if (!offset) 00059 return -1; // Not found 00060 00061 MimeTypeEntry::Ptr newMimeType(createEntry(offset)); 00062 if (!newMimeType) 00063 return -1; 00064 // Check whether the dictionary was right. 00065 if (newMimeType->name() != mimeTypeName) { 00066 // No it wasn't... 00067 return -1; 00068 } 00069 return newMimeType->serviceOffersOffset(); 00070 } 00071 00072 KMimeTypeFactory::MimeTypeEntry * KMimeTypeFactory::createEntry(int offset) const 00073 { 00074 MimeTypeEntry *newEntry = 0; 00075 KSycocaType type; 00076 QDataStream *str = KSycoca::self()->findEntry(offset, type); 00077 if (!str) return 0; 00078 00079 switch(type) 00080 { 00081 case KST_KMimeTypeEntry: 00082 newEntry = new MimeTypeEntry(*str, offset); 00083 break; 00084 00085 // Old, now unused 00086 case KST_KMimeType: 00087 case KST_KFolderMimeType: 00088 return 0; 00089 00090 default: 00091 kError(7011) << "KMimeTypeFactory: unexpected object entry in KSycoca database (type=" << int(type) << ")"; 00092 break; 00093 } 00094 if (newEntry && !newEntry->isValid()) 00095 { 00096 kError(7011) << "KMimeTypeFactory: corrupt object in KSycoca database!\n"; 00097 delete newEntry; 00098 newEntry = 0; 00099 } 00100 return newEntry; 00101 } 00102 00103 QStringList KMimeTypeFactory::allMimeTypes() 00104 { 00105 // TODO: reimplement in terms of "listing xdgdata-mime", to avoid ksycoca dependency, 00106 // then move to KMimeTypeRepository 00107 QStringList result; 00108 const KSycocaEntry::List list = allEntries(); 00109 for( KSycocaEntry::List::ConstIterator it = list.begin(); 00110 it != list.end(); 00111 ++it) 00112 { 00113 Q_ASSERT( (*it)->isType( KST_KMimeTypeEntry ) ); 00114 result.append( MimeTypeEntry::Ptr::staticCast( *it )->name() ); 00115 } 00116 return result; 00117 } 00118 00120 00121 class KMimeTypeFactory::MimeTypeEntryPrivate : public KSycocaEntryPrivate 00122 { 00123 public: 00124 K_SYCOCATYPE( KST_KMimeTypeEntry, KSycocaEntryPrivate ) 00125 MimeTypeEntryPrivate(const QString& file, const QString& name) 00126 : KSycocaEntryPrivate(file), m_name(name), m_serviceOffersOffset(-1) 00127 { 00128 } 00129 MimeTypeEntryPrivate(QDataStream& s, int offset) 00130 : KSycocaEntryPrivate(s, offset), m_serviceOffersOffset(-1) 00131 { 00132 s >> m_name >> m_serviceOffersOffset; 00133 } 00134 virtual QString name() const { return m_name; } 00135 virtual void save(QDataStream &s) { 00136 KSycocaEntryPrivate::save(s); 00137 s << m_name << m_serviceOffersOffset; 00138 } 00139 00140 QString m_name; 00141 int m_serviceOffersOffset; 00142 }; 00143 00144 KMimeTypeFactory::MimeTypeEntry::MimeTypeEntry(const QString& file, const QString& name) 00145 : KSycocaEntry(*new MimeTypeEntryPrivate(file, name)) 00146 { 00147 } 00148 00149 KMimeTypeFactory::MimeTypeEntry::MimeTypeEntry(QDataStream& s, int offset) 00150 : KSycocaEntry(*new MimeTypeEntryPrivate(s, offset)) 00151 { 00152 } 00153 00154 int KMimeTypeFactory::MimeTypeEntry::serviceOffersOffset() const 00155 { 00156 Q_D(const MimeTypeEntry); 00157 return d->m_serviceOffersOffset; 00158 } 00159 00160 void KMimeTypeFactory::MimeTypeEntry::setServiceOffersOffset(int off) 00161 { 00162 Q_D(MimeTypeEntry); 00163 d->m_serviceOffersOffset = off; 00164 }
KDE 4.6 API Reference