• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kded
 

kded

  • kded
kbuildservicetypefactory.cpp
1 /* This file is part of the KDE libraries
2  * Copyright (C) 1999 David Faure <faure@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 "kbuildservicetypefactory.h"
20 #include "tdesycoca.h"
21 #include "tdesycocadict.h"
22 #include "kresourcelist.h"
23 
24 #include <tdeglobal.h>
25 #include <kstandarddirs.h>
26 #include <kmessageboxwrapper.h>
27 #include <kdebug.h>
28 #include <tdelocale.h>
29 #include <assert.h>
30 #include <kdesktopfile.h>
31 
32 template class TQDict<KMimeType>;
33 
34 KBuildServiceTypeFactory::KBuildServiceTypeFactory() :
35  KServiceTypeFactory()
36 {
37  // Read servicetypes first, since they might be needed to read mimetype properties
38  m_resourceList = new KSycocaResourceList;
39  m_resourceList->add("servicetypes", "*.desktop");
40  m_resourceList->add("servicetypes", "*.kdelnk");
41  m_resourceList->add( "mime", "*.desktop" );
42  m_resourceList->add( "mime", "*.kdelnk" );
43 }
44 
45 // return all service types for this factory
46 // i.e. first arguments to m_resourceList->add() above
47 TQStringList KBuildServiceTypeFactory::resourceTypes()
48 {
49  return TQStringList() << "servicetypes" << "mime";
50 }
51 
52 KBuildServiceTypeFactory::~KBuildServiceTypeFactory()
53 {
54  delete m_resourceList;
55 }
56 
57 KServiceType * KBuildServiceTypeFactory::findServiceTypeByName(const TQString &_name)
58 {
59  assert (KSycoca::self()->isBuilding());
60  // We're building a database - the service type must be in memory
61  KSycocaEntry::Ptr * servType = (*m_entryDict)[ _name ];
62  if (!servType)
63  return 0;
64  return (KServiceType *) ((KSycocaEntry*)*servType);
65 }
66 
67 
68 KSycocaEntry *
69 KBuildServiceTypeFactory::createEntry(const TQString &file, const char *resource)
70 {
71  TQString name = file;
72  int pos = name.findRev('/');
73  if (pos != -1)
74  {
75  name = name.mid(pos+1);
76  }
77 
78  if (name.isEmpty())
79  return 0;
80 
81  KDesktopFile desktopFile(file, true, resource);
82 
83  if ( desktopFile.readBoolEntry( "Hidden", false ) == true )
84  return 0;
85 
86  // TODO check Type field first
87  TQString mime = desktopFile.readEntry( "MimeType" );
88  TQString service = desktopFile.readEntry( "X-TDE-ServiceType" );
89 
90  if ( mime.isEmpty() && service.isEmpty() )
91  {
92  TQString tmp = TQString("The service/mime type config file\n%1\n"
93  "does not contain a ServiceType=...\nor MimeType=... entry").arg( file );
94  kdWarning(7012) << tmp << endl;
95  return 0;
96  }
97 
98  KServiceType* e;
99  if ( mime == "inode/directory" )
100  e = new KFolderType( &desktopFile );
101  else if ( (mime == "application/x-desktop")
102  || (mime == "media/builtin-mydocuments")
103  || (mime == "media/builtin-mycomputer")
104  || (mime == "media/builtin-mynetworkplaces")
105  || (mime == "media/builtin-printers")
106  || (mime == "media/builtin-trash")
107  || (mime == "media/builtin-webbrowser") )
108  e = new KDEDesktopMimeType( &desktopFile );
109  else if ( mime == "application/x-executable" || mime == "application/x-pie-executable" || mime == "application/x-shellscript" )
110  e = new KExecMimeType( &desktopFile );
111  else if ( !mime.isEmpty() )
112  e = new KMimeType( &desktopFile );
113  else
114  e = new KServiceType( &desktopFile );
115 
116  if (e->isDeleted())
117  {
118  delete e;
119  return 0;
120  }
121 
122  if ( !(e->isValid()) )
123  {
124  kdWarning(7012) << "Invalid ServiceType : " << file << endl;
125  delete e;
126  return 0;
127  }
128 
129  return e;
130 }
131 
132 void
133 KBuildServiceTypeFactory::saveHeader(TQDataStream &str)
134 {
135  KSycocaFactory::saveHeader(str);
136  str << (TQ_INT32) m_fastPatternOffset;
137  str << (TQ_INT32) m_otherPatternOffset;
138  str << (TQ_INT32) m_propertyTypeDict.count();
139 
140  TQMapIterator<TQString, int> it;
141  for (it = m_propertyTypeDict.begin(); it != m_propertyTypeDict.end(); ++it)
142  {
143  str << it.key() << (TQ_INT32)it.data();
144  }
145 
146 }
147 
148 void
149 KBuildServiceTypeFactory::save(TQDataStream &str)
150 {
151  KSycocaFactory::save(str);
152 
153  savePatternLists(str);
154 
155  int endOfFactoryData = str.device()->at();
156 
157  // Update header (pass #3)
158  saveHeader(str);
159 
160  // Seek to end.
161  str.device()->at(endOfFactoryData);
162 }
163 
164 void
165 KBuildServiceTypeFactory::savePatternLists(TQDataStream &str)
166 {
167  // Store each patterns in one of the 2 string lists (for sorting)
168  TQStringList fastPatterns; // for *.a to *.abcd
169  TQStringList otherPatterns; // for the rest (core.*, *.tar.bz2, *~) ...
170  TQDict<KMimeType> dict;
171 
172  // For each mimetype in servicetypeFactory
173  for(TQDictIterator<KSycocaEntry::Ptr> it ( *m_entryDict );
174  it.current();
175  ++it)
176  {
177  KSycocaEntry *entry = (*it.current());
178  if ( entry->isType( KST_KMimeType ) )
179  {
180  KMimeType *mimeType = (KMimeType *) entry;
181  TQStringList pat = mimeType->patterns();
182  TQStringList::ConstIterator patit = pat.begin();
183  for ( ; patit != pat.end() ; ++patit )
184  {
185  const TQString &pattern = *patit;
186  if ( pattern.findRev('*') == 0
187  && pattern.findRev('.') == 1
188  && pattern.length() <= 6 )
189  // it starts with "*.", has no other '*' and no other '.', and is max 6 chars
190  // => fast patttern
191  fastPatterns.append( pattern );
192  else if (!pattern.isEmpty()) // some stupid mimetype files have "Patterns=;"
193  otherPatterns.append( pattern );
194  // Assumption : there is only one mimetype for that pattern
195  // It doesn't really make sense otherwise, anyway.
196  dict.replace( pattern, mimeType );
197  }
198  }
199  }
200  // Sort the list - the fast one, useless for the other one
201  fastPatterns.sort();
202 
203  TQ_INT32 entrySize = 0;
204  TQ_INT32 nrOfEntries = 0;
205 
206  m_fastPatternOffset = str.device()->at();
207 
208  // Write out fastPatternHeader (Pass #1)
209  str.device()->at(m_fastPatternOffset);
210  str << nrOfEntries;
211  str << entrySize;
212 
213  // For each fast pattern
214  TQStringList::ConstIterator it = fastPatterns.begin();
215  for ( ; it != fastPatterns.end() ; ++it )
216  {
217  int start = str.device()->at();
218  // Justify to 6 chars with spaces, so that the size remains constant
219  // in the database file.
220  TQString paddedPattern = (*it).leftJustify(6).right(4); // remove leading "*."
221  //kdDebug(7021) << TQString("FAST : '%1' '%2'").arg(paddedPattern).arg(dict[(*it)]->name()) << endl;
222  str << paddedPattern;
223  str << dict[(*it)]->offset();
224  entrySize = str.device()->at() - start;
225  nrOfEntries++;
226  }
227 
228  // store position
229  m_otherPatternOffset = str.device()->at();
230 
231  // Write out fastPatternHeader (Pass #2)
232  str.device()->at(m_fastPatternOffset);
233  str << nrOfEntries;
234  str << entrySize;
235 
236  // For the other patterns
237  str.device()->at(m_otherPatternOffset);
238 
239  it = otherPatterns.begin();
240  for ( ; it != otherPatterns.end() ; ++it )
241  {
242  //kdDebug(7021) << TQString("OTHER : '%1' '%2'").arg(*it).arg(dict[(*it)]->name()) << endl;
243  str << (*it);
244  str << dict[(*it)]->offset();
245  }
246 
247  str << TQString(""); // end of list marker (has to be a string !)
248 }
249 
250 void
251 KBuildServiceTypeFactory::addEntry(KSycocaEntry *newEntry, const char *resource)
252 {
253  KServiceType * serviceType = (KServiceType *) newEntry;
254  if ( (*m_entryDict)[ newEntry->name() ] )
255  {
256  // Already exists
257  if (serviceType->desktopEntryPath().endsWith("kdelnk"))
258  return; // Skip
259 
260  // Replace
261  KSycocaFactory::removeEntry(newEntry);
262  }
263  KSycocaFactory::addEntry(newEntry, resource);
264 
265 
266  const TQMap<TQString,TQVariant::Type>& pd = serviceType->propertyDefs();
267  TQMap<TQString,TQVariant::Type>::ConstIterator pit = pd.begin();
268  for( ; pit != pd.end(); ++pit )
269  {
270  if (!m_propertyTypeDict.contains(pit.key()))
271  m_propertyTypeDict.insert(pit.key(), pit.data());
272  else if (m_propertyTypeDict[pit.key()] != pit.data())
273  kdWarning(7021) << "Property '"<< pit.key() << "' is defined multiple times ("<< serviceType->name() <<")" <<endl;
274  }
275 }
276 
KBuildServiceTypeFactory::KBuildServiceTypeFactory
KBuildServiceTypeFactory()
Create factory.
Definition: kbuildservicetypefactory.cpp:34
KBuildServiceTypeFactory::saveHeader
virtual void saveHeader(TQDataStream &str)
Write out header information.
Definition: kbuildservicetypefactory.cpp:133
KBuildServiceTypeFactory::findServiceTypeByName
virtual KServiceType * findServiceTypeByName(const TQString &_name)
Find a service type in the database file.
Definition: kbuildservicetypefactory.cpp:57
KBuildServiceTypeFactory::resourceTypes
static TQStringList resourceTypes()
Returns all resource types for this service factory.
Definition: kbuildservicetypefactory.cpp:47
KBuildServiceTypeFactory::addEntry
virtual void addEntry(KSycocaEntry *newEntry, const char *resource)
Add entry.
Definition: kbuildservicetypefactory.cpp:251
KBuildServiceTypeFactory::createEntry
virtual KSycocaEntry * createEntry(const TQString &file, const char *resource)
Construct a KServiceType from a config file.
Definition: kbuildservicetypefactory.cpp:69
KBuildServiceTypeFactory::save
virtual void save(TQDataStream &str)
Write out service type specific index files.
Definition: kbuildservicetypefactory.cpp:149

kded

Skip menu "kded"
  • Main Page
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kded

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