• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdeio
 

tdeio/tdeio

  • tdeio
  • tdeio
kservicetypefactory.cpp
1 /* This file is part of the KDE libraries
2  * Copyright (C) 1999 Waldo Bastian <bastian@kde.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License version 2 as published by the Free Software Foundation;
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Library General Public License for more details.
12  *
13  * You should have received a copy of the GNU Library General Public License
14  * along with this library; see the file COPYING.LIB. If not, write to
15  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  * Boston, MA 02110-1301, USA.
17  **/
18 
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"
26 
27 #include <tdeapplication.h>
28 #include <kdebug.h>
29 #include <assert.h>
30 #include <kstringhandler.h>
31 #include <tqfile.h>
32 
33 KServiceTypeFactory::KServiceTypeFactory()
34  : KSycocaFactory( KST_KServiceTypeFactory )
35 {
36  _self = this;
37  m_fastPatternOffset = 0;
38  m_otherPatternOffset = 0;
39  if (m_str)
40  {
41  // Read Header
42  TQ_INT32 i,n;
43  (*m_str) >> i;
44  m_fastPatternOffset = i;
45  (*m_str) >> i;
46  m_otherPatternOffset = i;
47  (*m_str) >> n;
48 
49  if (n > 1024)
50  {
51  KSycoca::flagError();
52  }
53  else
54  {
55  TQString str;
56  for(;n;n--)
57  {
58  KSycocaEntry::read(*m_str, str);
59  (*m_str) >> i;
60  m_propertyTypeDict.insert(str, i);
61  }
62  }
63  }
64 }
65 
66 
67 KServiceTypeFactory::~KServiceTypeFactory()
68 {
69  _self = 0L;
70  KServiceTypeProfile::clear();
71 }
72 
73 KServiceTypeFactory * KServiceTypeFactory::self()
74 {
75  if (!_self)
76  _self = new KServiceTypeFactory();
77  return _self;
78 }
79 
80 KServiceType * KServiceTypeFactory::findServiceTypeByName(const TQString &_name)
81 {
82  if (!m_sycocaDict) return 0L; // Error!
83  assert (!KSycoca::self()->isBuilding());
84  int offset = m_sycocaDict->find_string( _name );
85  if (!offset) return 0; // Not found
86  KServiceType * newServiceType = createEntry(offset);
87 
88  // Check whether the dictionary was right.
89  if (newServiceType && (newServiceType->name() != _name))
90  {
91  // No it wasn't...
92  delete newServiceType;
93  newServiceType = 0; // Not found
94  }
95  return newServiceType;
96 }
97 
98 TQVariant::Type KServiceTypeFactory::findPropertyTypeByName(const TQString &_name)
99 {
100  if (!m_sycocaDict)
101  return TQVariant::Invalid; // Error!
102 
103  assert (!KSycoca::self()->isBuilding());
104 
105  TQMapConstIterator<TQString,int> it = m_propertyTypeDict.find(_name);
106  if (it != m_propertyTypeDict.end()) {
107  return (TQVariant::Type)it.data();
108  }
109 
110  return TQVariant::Invalid;
111 }
112 
113 KMimeType * KServiceTypeFactory::findFromPattern(const TQString &_filename, TQString *match)
114 {
115  // Assume we're NOT building a database
116  if (!m_str) return 0;
117 
118  // Get stream to the header
119  TQDataStream *str = m_str;
120 
121  str->device()->at( m_fastPatternOffset );
122 
123  TQ_INT32 nrOfEntries;
124  (*str) >> nrOfEntries;
125  TQ_INT32 entrySize;
126  (*str) >> entrySize;
127 
128  TQ_INT32 fastOffset = str->device()->at( );
129 
130  TQ_INT32 matchingOffset = 0;
131 
132  // Let's go for a binary search in the "fast" pattern index
133  TQ_INT32 left = 0;
134  TQ_INT32 right = nrOfEntries - 1;
135  TQ_INT32 middle;
136  // Extract extension
137  int lastDot = _filename.findRev('.');
138  int ext_len = _filename.length() - lastDot - 1;
139  if (lastDot != -1 && ext_len <= 4) // if no '.', skip the extension lookup
140  {
141  TQString extension = _filename.right( ext_len );
142  extension = extension.leftJustify(4);
143 
144  TQString pattern;
145  while (left <= right) {
146  middle = (left + right) / 2;
147  // read pattern at position "middle"
148  str->device()->at( middle * entrySize + fastOffset );
149  KSycocaEntry::read(*str, pattern);
150  int cmp = pattern.compare( extension );
151  if (cmp < 0)
152  left = middle + 1;
153  else if (cmp == 0) // found
154  {
155  (*str) >> matchingOffset;
156  // don't return newServiceType - there may be an "other" pattern that
157  // matches best this file, like *.tar.bz
158  if (match)
159  *match = "*."+pattern.stripWhiteSpace();
160  break; // but get out of the fast patterns
161  }
162  else
163  right = middle - 1;
164  }
165  }
166 
167  // Now try the "other" Pattern table
168  if ( m_patterns.isEmpty() ) {
169  str->device()->at( m_otherPatternOffset );
170 
171  TQString pattern;
172  TQ_INT32 mimetypeOffset;
173 
174  while (true)
175  {
176  KSycocaEntry::read(*str, pattern);
177  if (pattern.isEmpty()) // end of list
178  break;
179  (*str) >> mimetypeOffset;
180  m_patterns.push_back( pattern );
181  m_pattern_offsets.push_back( mimetypeOffset );
182  }
183  }
184 
185  assert( m_patterns.size() == m_pattern_offsets.size() );
186 
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();
190 
191  for ( ; it != end; ++it, ++it_offset )
192  {
193  if ( KStringHandler::matchFileName( _filename, *it ) )
194  {
195  if ( !matchingOffset || !(*it).endsWith( "*" ) ) // *.html wins over Makefile.*
196  {
197  matchingOffset = *it_offset;
198  if (match)
199  *match = *it;
200  break;
201  }
202  }
203  }
204 
205  if ( matchingOffset ) {
206  KServiceType *newServiceType = createEntry( matchingOffset );
207  assert (newServiceType && newServiceType->isType( KST_KMimeType ));
208  return (KMimeType *) newServiceType;
209  }
210  else
211  return 0;
212 }
213 
214 KMimeType::List KServiceTypeFactory::allMimeTypes()
215 {
216  KMimeType::List result;
217  KSycocaEntry::List list = allEntries();
218  for( KSycocaEntry::List::Iterator it = list.begin();
219  it != list.end();
220  ++it)
221  {
222  KMimeType *newMimeType = dynamic_cast<KMimeType *>((*it).data());
223  if (newMimeType)
224  result.append( KMimeType::Ptr( newMimeType ) );
225  }
226  return result;
227 }
228 
229 KServiceType::List KServiceTypeFactory::allServiceTypes()
230 {
231  KServiceType::List result;
232  KSycocaEntry::List list = allEntries();
233  for( KSycocaEntry::List::Iterator it = list.begin();
234  it != list.end();
235  ++it)
236  {
237 #ifndef Q_WS_QWS
238  KServiceType *newServiceType = dynamic_cast<KServiceType *>((*it).data());
239 #else //FIXME
240  KServiceType *newServiceType = (KServiceType*)(*it).data();
241 #endif
242  if (newServiceType)
243  result.append( KServiceType::Ptr( newServiceType ) );
244  }
245  return result;
246 }
247 
248 bool KServiceTypeFactory::checkMimeTypes()
249 {
250  TQDataStream *str = KSycoca::self()->findFactory( factoryId() );
251  if (!str) return false;
252 
253  // check if there are mimetypes/servicetypes
254  return (m_beginEntryOffset != m_endEntryOffset);
255 }
256 
257 KServiceType * KServiceTypeFactory::createEntry(int offset)
258 {
259  KServiceType *newEntry = 0;
260  KSycocaType type;
261  TQDataStream *str = KSycoca::self()->findEntry(offset, type);
262  if (!str) return 0;
263 
264  switch(type)
265  {
266  case KST_KServiceType:
267  newEntry = new KServiceType(*str, offset);
268  break;
269  case KST_KMimeType:
270  newEntry = new KMimeType(*str, offset);
271  break;
272  case KST_KFolderType:
273  newEntry = new KFolderType(*str, offset);
274  break;
275  case KST_KDEDesktopMimeType:
276  newEntry = new KDEDesktopMimeType(*str, offset);
277  break;
278  case KST_KExecMimeType:
279  newEntry = new KExecMimeType(*str, offset);
280  break;
281 
282  default:
283  kdError(7011) << TQString(TQString("KServiceTypeFactory: unexpected object entry in KSycoca database (type = %1)").arg((int)type)) << endl;
284  break;
285  }
286  if (newEntry && !newEntry->isValid())
287  {
288  kdError(7011) << "KServiceTypeFactory: corrupt object in KSycoca database!\n" << endl;
289  delete newEntry;
290  newEntry = 0;
291  }
292  return newEntry;
293 }
294 
295 KServiceTypeFactory *KServiceTypeFactory::_self = 0;
296 
297 void KServiceTypeFactory::virtual_hook( int id, void* data )
298 { KSycocaFactory::virtual_hook( id, data ); }
KMimeType
Represent a mime type, like "text/plain", and the data that is associated with it.
Definition: kmimetype.h:47
KServiceType::name
TQString name() const
Returns the name of this service type.
Definition: kservicetype.h:106
KServiceType
A service type is the generic notion for a mimetype, a type of service instead of a type of file...
Definition: kservicetype.h:45
KDEDesktopMimeType
Mime type for desktop files.
Definition: kmimetype.h:506
KFolderType
Folder mime type.
Definition: kmimetype.h:477
KServiceType::isValid
bool isValid() const
Checks whether the service type is valid.
Definition: kservicetype.h:158
KExecMimeType
The mime type for executable files.
Definition: kmimetype.h:622
KServiceTypeProfile::clear
static void clear()
Clear all cached information.
Definition: kuserprofile.cpp:99

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdeio by doxygen 1.8.8
This website is maintained by Timothy Pearson.