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

tdeio/tdeio

  • tdeio
  • tdeio
kservicetype.cpp
1 /* This file is part of the KDE libraries
2  * Copyright (C) 1999 Waldo Bastian <bastian@kde.org>
3  * David Faure <faure@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License version 2 as published by the Free Software Foundation;
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  **/
19 
20 #include "kservice.h"
21 #include "tdesycoca.h"
22 #include "kservicetype.h"
23 #include "kservicetypefactory.h"
24 #include "kservicefactory.h"
25 #include "kuserprofile.h"
26 #include <assert.h>
27 #include <kdebug.h>
28 #include <kdesktopfile.h>
29 
30 template TQDataStream& operator>> <TQString, TQVariant>(TQDataStream&, TQMap<TQString, TQVariant>&);
31 template TQDataStream& operator<< <TQString, TQVariant>(TQDataStream&, const TQMap<TQString, TQVariant>&);
32 
33 class KServiceType::KServiceTypePrivate
34 {
35 public:
36  KServiceTypePrivate() : parentTypeLoaded(false) { }
37 
38  KServiceType::Ptr parentType;
39  KService::List services;
40  bool parentTypeLoaded;
41 };
42 
43 KServiceType::KServiceType( const TQString & _fullpath)
44  : KSycocaEntry(_fullpath), d(0)
45 {
46  KDesktopFile config( _fullpath );
47 
48  init(&config);
49 }
50 
51 KServiceType::KServiceType( KDesktopFile *config )
52  : KSycocaEntry(config->fileName()), d(0)
53 {
54  init(config);
55 }
56 
57 void
58 KServiceType::init( KDesktopFile *config)
59 {
60  // Is it a mimetype ?
61  m_strName = config->readEntry( "MimeType" );
62 
63  // Or is it a servicetype ?
64  if ( m_strName.isEmpty() )
65  {
66  m_strName = config->readEntry( "X-TDE-ServiceType" );
67  }
68 
69  m_strComment = config->readComment();
70  m_bDeleted = config->readBoolEntry( "Hidden", false );
71  m_strIcon = config->readIcon();
72 
73  // We store this as property to preserve BC, we can't change that
74  // because KSycoca needs to remain BC between KDE 2.x and KDE 3.x
75  TQString sDerived = config->readEntry( "X-TDE-Derived" );
76  m_bDerived = !sDerived.isEmpty();
77  if ( m_bDerived )
78  m_mapProps.insert( "X-TDE-Derived", sDerived );
79 
80  TQStringList tmpList = config->groupList();
81  TQStringList::Iterator gIt = tmpList.begin();
82 
83  for( ; gIt != tmpList.end(); ++gIt )
84  {
85  if ( (*gIt).find( "Property::" ) == 0 )
86  {
87  config->setGroup( *gIt );
88  TQVariant v = config->readPropertyEntry( "Value",
89  TQVariant::nameToType( config->readEntry( "Type" ).ascii() ) );
90  if ( v.isValid() )
91  m_mapProps.insert( (*gIt).mid( 10 ), v );
92  }
93  }
94 
95  gIt = tmpList.begin();
96  for( ; gIt != tmpList.end(); ++gIt )
97  {
98  if( (*gIt).find( "PropertyDef::" ) == 0 )
99  {
100  config->setGroup( *gIt );
101  m_mapPropDefs.insert( (*gIt).mid( 13 ),
102  TQVariant::nameToType( config->readEntry( "Type" ).ascii() ) );
103  }
104  }
105 
106  m_bValid = !m_strName.isEmpty();
107 }
108 
109 KServiceType::KServiceType( const TQString & _fullpath, const TQString& _type,
110  const TQString& _icon, const TQString& _comment )
111  : KSycocaEntry(_fullpath), d(0)
112 {
113  m_strName = _type;
114  m_strIcon = _icon;
115  m_strComment = _comment;
116  m_bValid = !m_strName.isEmpty();
117 }
118 
119 KServiceType::KServiceType( TQDataStream& _str, int offset )
120  : KSycocaEntry( _str, offset ), d(0)
121 {
122  load( _str);
123 }
124 
125 void
126 KServiceType::load( TQDataStream& _str )
127 {
128  TQ_INT8 b;
129  _str >> m_strName >> m_strIcon >> m_strComment >> m_mapProps >> m_mapPropDefs
130  >> b;
131  m_bValid = b;
132  m_bDerived = m_mapProps.contains("X-TDE-Derived");
133 }
134 
135 void
136 KServiceType::save( TQDataStream& _str )
137 {
138  KSycocaEntry::save( _str );
139  // !! This data structure should remain binary compatible at all times !!
140  // You may add new fields at the end. Make sure to update the version
141  // number in tdesycoca.h
142  _str << m_strName << m_strIcon << m_strComment << m_mapProps << m_mapPropDefs
143  << (TQ_INT8)m_bValid;
144 }
145 
146 KServiceType::~KServiceType()
147 {
148  delete d;
149 }
150 
151 TQString KServiceType::parentServiceType() const
152 {
153  TQVariant v = property("X-TDE-Derived");
154  return v.toString();
155 }
156 
157 bool KServiceType::inherits( const TQString& servTypeName ) const
158 {
159  if ( name() == servTypeName )
160  return true;
161  TQString st = parentServiceType();
162  while ( !st.isEmpty() )
163  {
164  KServiceType::Ptr ptr = KServiceType::serviceType( st );
165  if (!ptr) return false; //error
166  if ( ptr->name() == servTypeName )
167  return true;
168  st = ptr->parentServiceType();
169  }
170  return false;
171 }
172 
173 TQVariant
174 KServiceType::property( const TQString& _name ) const
175 {
176  TQVariant v;
177 
178  if ( _name == "Name" )
179  v = TQVariant( m_strName );
180  else if ( _name == "Icon" )
181  v = TQVariant( m_strIcon );
182  else if ( _name == "Comment" )
183  v = TQVariant( m_strComment );
184  else {
185  TQMap<TQString,TQVariant>::ConstIterator it = m_mapProps.find( _name );
186  if ( it != m_mapProps.end() )
187  v = it.data();
188  }
189 
190  return v;
191 }
192 
193 TQStringList
194 KServiceType::propertyNames() const
195 {
196  TQStringList res;
197 
198  TQMap<TQString,TQVariant>::ConstIterator it = m_mapProps.begin();
199  for( ; it != m_mapProps.end(); ++it )
200  res.append( it.key() );
201 
202  res.append( "Name" );
203  res.append( "Comment" );
204  res.append( "Icon" );
205 
206  return res;
207 }
208 
209 TQVariant::Type
210 KServiceType::propertyDef( const TQString& _name ) const
211 {
212  TQMap<TQString,TQVariant::Type>::ConstIterator it = m_mapPropDefs.find( _name );
213  if ( it == m_mapPropDefs.end() )
214  return TQVariant::Invalid;
215  return it.data();
216 }
217 
218 TQStringList
219 KServiceType::propertyDefNames() const
220 {
221  TQStringList l;
222 
223  TQMap<TQString,TQVariant::Type>::ConstIterator it = m_mapPropDefs.begin();
224  for( ; it != m_mapPropDefs.end(); ++it )
225  l.append( it.key() );
226 
227  return l;
228 }
229 
230 KServiceType::Ptr KServiceType::serviceType( const TQString& _name )
231 {
232  KServiceType * p = KServiceTypeFactory::self()->findServiceTypeByName( _name );
233  return KServiceType::Ptr( p );
234 }
235 
236 static void addUnique(KService::List &lst, TQDict<KService> &dict, const KService::List &newLst, bool lowPrio)
237 {
238  TQValueListConstIterator<KService::Ptr> it = newLst.begin();
239  for( ; it != newLst.end(); ++it )
240  {
241  KService *service = static_cast<KService*>(*it);
242  if (dict.find(service->desktopEntryPath()))
243  continue;
244  dict.insert(service->desktopEntryPath(), service);
245  lst.append(service);
246  if (lowPrio)
247  service->setInitialPreference( 0 );
248  }
249 }
250 
251 KService::List KServiceType::offers( const TQString& _servicetype )
252 {
253  TQDict<KService> dict(53);
254  KService::List lst;
255 
256  // Services associated directly with this servicetype (the normal case)
257  KServiceType::Ptr serv = KServiceTypeFactory::self()->findServiceTypeByName( _servicetype );
258  if ( serv )
259  addUnique(lst, dict, KServiceFactory::self()->offers( serv->offset() ), false);
260  else
261  kdWarning(7009) << "KServiceType::offers : servicetype " << _servicetype << " not found" << endl;
262 
263  // Find services associated with any mimetype parents. e.g. text/x-java -> text/plain
264  KMimeType::Ptr mime = dynamic_cast<KMimeType*>(static_cast<KServiceType *>(serv));
265  bool isAMimeType = (mime != 0);
266  if (mime)
267  {
268  while(true)
269  {
270  TQString parent = mime->parentMimeType();
271  if (parent.isEmpty())
272  break;
273  mime = dynamic_cast<KMimeType *>(KServiceTypeFactory::self()->findServiceTypeByName( parent ));
274  if (!mime)
275  break;
276 
277  addUnique(lst, dict, KServiceFactory::self()->offers( mime->offset() ), false);
278  }
279  }
280  serv = mime = 0;
281 
282  //TQValueListIterator<KService::Ptr> it = lst.begin();
283  //for( ; it != lst.end(); ++it )
284  // kdDebug() << (*it).data() << " " << (*it)->name() << endl;
285 
286  // Support for all/* is deactivated by KServiceTypeProfile::configurationMode()
287  // (and makes no sense when querying for an "all" servicetype itself
288  // nor for non-mimetypes service types)
289  if ( !KServiceTypeProfile::configurationMode()
290  && isAMimeType
291  && _servicetype.left(4) != "all/" )
292  {
293  // Support for services associated with "all"
294  KServiceType * servAll = KServiceTypeFactory::self()->findServiceTypeByName( "all/all" );
295  if ( servAll )
296  {
297  addUnique(lst, dict, KServiceFactory::self()->offers( servAll->offset() ), true);
298  }
299  else
300  kdWarning(7009) << "KServiceType::offers : servicetype all/all not found" << endl;
301  delete servAll;
302 
303  // Support for services associated with "allfiles"
304  if ( _servicetype != "inode/directory" && _servicetype != "inode/directory-locked" )
305  {
306  KServiceType * servAllFiles = KServiceTypeFactory::self()->findServiceTypeByName( "all/allfiles" );
307  if ( servAllFiles )
308  {
309  addUnique(lst, dict, KServiceFactory::self()->offers( servAllFiles->offset() ), true);
310  }
311  else
312  kdWarning(7009) << "KServiceType::offers : servicetype all/allfiles not found" << endl;
313  delete servAllFiles;
314  }
315  }
316 
317  return lst;
318 }
319 
320 KServiceType::List KServiceType::allServiceTypes()
321 {
322  return KServiceTypeFactory::self()->allServiceTypes();
323 }
324 
325 KServiceType::Ptr KServiceType::parentType()
326 {
327  if (d && d->parentTypeLoaded)
328  return d->parentType;
329 
330  if (!d)
331  d = new KServiceTypePrivate;
332 
333  TQString parentSt = parentServiceType();
334  if (!parentSt.isEmpty())
335  {
336  d->parentType = KServiceTypeFactory::self()->findServiceTypeByName( parentSt );
337  if (!d->parentType)
338  kdWarning(7009) << "'" << desktopEntryPath() << "' specifies undefined mimetype/servicetype '"<< parentSt << "'" << endl;
339  }
340 
341  d->parentTypeLoaded = true;
342 
343  return d->parentType;
344 }
345 
346 void KServiceType::addService(KService::Ptr service)
347 {
348  if (!d)
349  d = new KServiceTypePrivate;
350 
351  if (d->services.count() && d->services.last() == service)
352  return;
353 
354  d->services.append(service);
355 }
356 
357 KService::List KServiceType::services()
358 {
359  if (d)
360  return d->services;
361 
362  return KService::List();
363 }
364 
365 void KServiceType::virtual_hook( int id, void* data )
366 { KSycocaEntry::virtual_hook( id, data ); }
KServiceType::allServiceTypes
static List allServiceTypes()
Returns a list of all the supported servicetypes.
Definition: kservicetype.cpp:320
KService
Represent a service, i.e.
Definition: kservice.h:48
KMimeType
Represent a mime type, like "text/plain", and the data that is associated with it.
Definition: kmimetype.h:47
KMimeType::parentMimeType
TQString parentMimeType() const
If this mimetype inherits from ("is also") another mimetype, return the name of the parent...
Definition: kmimetype.cpp:561
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
KServiceType::parentServiceType
TQString parentServiceType() const
If this service type inherits from another service type, return the name of the parent.
Definition: kservicetype.cpp:151
KServiceType::offers
static KService::List offers(const TQString &_servicetype)
Returns all services supporting the given servicetype name.
Definition: kservicetype.cpp:251
KServiceType::propertyNames
virtual TQStringList propertyNames() const
Returns the list of all properties of this service type.
Definition: kservicetype.cpp:194
KService::desktopEntryPath
TQString desktopEntryPath() const
Returns the path to the location where the service desktop entry is stored.
Definition: kservice.h:174
KServiceType::serviceType
static Ptr serviceType(const TQString &_name)
Returns a pointer to the servicetype '_name' or 0L if the service type is unknown.
Definition: kservicetype.cpp:230
KServiceType::propertyDef
virtual TQVariant::Type propertyDef(const TQString &_name) const
Returns the type of the property with the given _name.
Definition: kservicetype.cpp:210
KServiceType::inherits
bool inherits(const TQString &servTypeName) const
Checks whether this service type is or inherits from servTypeName.
Definition: kservicetype.cpp:157
KServiceType::desktopEntryPath
TQString desktopEntryPath() const
Returns the relative path to the desktop entry file responsible for this servicetype.
Definition: kservicetype.h:114
KServiceType::KServiceType
KServiceType(const TQString &_fullpath, const TQString &_name, const TQString &_icon, const TQString &_comment)
Constructor.
Definition: kservicetype.cpp:109
KServiceType::property
virtual TQVariant property(const TQString &_name) const
Returns the requested property.
Definition: kservicetype.cpp:174

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.