19 #include "kservicetypefactory.h"
20 #include "tdesycoca.h"
21 #include "tdesycocatype.h"
22 #include "tdesycocadict.h"
23 #include "kservicetype.h"
24 #include "kmimetype.h"
25 #include "kuserprofile.h"
27 #include <tdeapplication.h>
30 #include <kstringhandler.h>
33 KServiceTypeFactory::KServiceTypeFactory()
34 : KSycocaFactory( KST_KServiceTypeFactory )
37 m_fastPatternOffset = 0;
38 m_otherPatternOffset = 0;
44 m_fastPatternOffset = i;
46 m_otherPatternOffset = i;
58 KSycocaEntry::read(*m_str, str);
60 m_propertyTypeDict.insert(str, i);
67 KServiceTypeFactory::~KServiceTypeFactory()
73 KServiceTypeFactory * KServiceTypeFactory::self()
76 _self =
new KServiceTypeFactory();
80 KServiceType * KServiceTypeFactory::findServiceTypeByName(
const TQString &_name)
82 if (!m_sycocaDict)
return 0L;
83 assert (!KSycoca::self()->isBuilding());
84 int offset = m_sycocaDict->find_string( _name );
85 if (!offset)
return 0;
89 if (newServiceType && (newServiceType->
name() != _name))
92 delete newServiceType;
95 return newServiceType;
98 TQVariant::Type KServiceTypeFactory::findPropertyTypeByName(
const TQString &_name)
101 return TQVariant::Invalid;
103 assert (!KSycoca::self()->isBuilding());
105 TQMapConstIterator<TQString,int> it = m_propertyTypeDict.find(_name);
106 if (it != m_propertyTypeDict.end()) {
107 return (TQVariant::Type)it.data();
110 return TQVariant::Invalid;
113 KMimeType * KServiceTypeFactory::findFromPattern(
const TQString &_filename, TQString *match)
116 if (!m_str)
return 0;
119 TQDataStream *str = m_str;
121 str->device()->at( m_fastPatternOffset );
123 TQ_INT32 nrOfEntries;
124 (*str) >> nrOfEntries;
128 TQ_INT32 fastOffset = str->device()->at( );
130 TQ_INT32 matchingOffset = 0;
134 TQ_INT32 right = nrOfEntries - 1;
137 int lastDot = _filename.findRev(
'.');
138 int ext_len = _filename.length() - lastDot - 1;
139 if (lastDot != -1 && ext_len <= 4)
141 TQString extension = _filename.right( ext_len );
142 extension = extension.leftJustify(4);
145 while (left <= right) {
146 middle = (left + right) / 2;
148 str->device()->at( middle * entrySize + fastOffset );
149 KSycocaEntry::read(*str, pattern);
150 int cmp = pattern.compare( extension );
155 (*str) >> matchingOffset;
159 *match =
"*."+pattern.stripWhiteSpace();
168 if ( m_patterns.isEmpty() ) {
169 str->device()->at( m_otherPatternOffset );
172 TQ_INT32 mimetypeOffset;
176 KSycocaEntry::read(*str, pattern);
177 if (pattern.isEmpty())
179 (*str) >> mimetypeOffset;
180 m_patterns.push_back( pattern );
181 m_pattern_offsets.push_back( mimetypeOffset );
185 assert( m_patterns.size() == m_pattern_offsets.size() );
187 TQStringList::const_iterator it = m_patterns.begin();
188 TQStringList::const_iterator end = m_patterns.end();
189 TQValueVector<TQ_INT32>::const_iterator it_offset = m_pattern_offsets.begin();
191 for ( ; it != end; ++it, ++it_offset )
193 if ( KStringHandler::matchFileName( _filename, *it ) )
195 if ( !matchingOffset || !(*it).endsWith(
"*" ) )
197 matchingOffset = *it_offset;
205 if ( matchingOffset ) {
206 KServiceType *newServiceType = createEntry( matchingOffset );
207 assert (newServiceType && newServiceType->isType( KST_KMimeType ));
214 KMimeType::List KServiceTypeFactory::allMimeTypes()
216 KMimeType::List result;
217 KSycocaEntry::List list = allEntries();
218 for( KSycocaEntry::List::Iterator it = list.begin();
224 result.append( KMimeType::Ptr( newMimeType ) );
229 KServiceType::List KServiceTypeFactory::allServiceTypes()
231 KServiceType::List result;
232 KSycocaEntry::List list = allEntries();
233 for( KSycocaEntry::List::Iterator it = list.begin();
243 result.append( KServiceType::Ptr( newServiceType ) );
248 bool KServiceTypeFactory::checkMimeTypes()
250 TQDataStream *str = KSycoca::self()->findFactory( factoryId() );
251 if (!str)
return false;
254 return (m_beginEntryOffset != m_endEntryOffset);
257 KServiceType * KServiceTypeFactory::createEntry(
int offset)
261 TQDataStream *str = KSycoca::self()->findEntry(offset, type);
266 case KST_KServiceType:
272 case KST_KFolderType:
275 case KST_KDEDesktopMimeType:
278 case KST_KExecMimeType:
283 kdError(7011) << TQString(TQString(
"KServiceTypeFactory: unexpected object entry in KSycoca database (type = %1)").arg((
int)type)) << endl;
286 if (newEntry && !newEntry->
isValid())
288 kdError(7011) <<
"KServiceTypeFactory: corrupt object in KSycoca database!\n" << endl;
295 KServiceTypeFactory *KServiceTypeFactory::_self = 0;
297 void KServiceTypeFactory::virtual_hook(
int id,
void* data )
298 { KSycocaFactory::virtual_hook(
id, data ); }
Represent a mime type, like "text/plain", and the data that is associated with it.
TQString name() const
Returns the name of this service type.
A service type is the generic notion for a mimetype, a type of service instead of a type of file...
Mime type for desktop files.
bool isValid() const
Checks whether the service type is valid.
The mime type for executable files.
static void clear()
Clear all cached information.