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

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • Main Page
  • 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
  • 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 tdeio/tdeio by doxygen 1.9.8
This website is maintained by Timothy Pearson.