1 /* This file is part of the KDE libraries
2 * Copyright (C) 1999 David Faure <faure@kde.org>
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;
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.
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.
19 #include "kbuildservicetypefactory.h"
21 #include "ksycocadict.h"
22 #include "kresourcelist.h"
25 #include <kstandarddirs.h>
29 #include <kdesktopfile.h>
30 #include <kconfiggroup.h>
31 #include <QtCore/QHash>
33 KBuildServiceTypeFactory::KBuildServiceTypeFactory() :
36 m_resourceList
= new KSycocaResourceList
;
37 m_resourceList
->add("servicetypes", "*.desktop");
40 // return all service types for this factory
41 // i.e. first arguments to m_resourceList->add() above
42 QStringList
KBuildServiceTypeFactory::resourceTypes()
44 return QStringList() << "servicetypes";
47 KBuildServiceTypeFactory::~KBuildServiceTypeFactory()
49 delete m_resourceList
;
52 KServiceType::Ptr
KBuildServiceTypeFactory::findServiceTypeByName(const QString
&_name
)
54 assert (KSycoca::self()->isBuilding());
55 // We're building a database - the service type must be in memory
56 KSycocaEntry::Ptr servType
= m_entryDict
->value( _name
);
57 return KServiceType::Ptr::staticCast( servType
);
61 KSycocaEntry
* KBuildServiceTypeFactory::createEntry(const QString
&file
, const char *resource
) const
64 int pos
= name
.lastIndexOf('/');
66 name
= name
.mid(pos
+1);
72 KDesktopFile
desktopFile(resource
, file
);
73 const KConfigGroup desktopGroup
= desktopFile
.desktopGroup();
75 if ( desktopGroup
.readEntry( "Hidden", false ) == true )
78 const QString type
= desktopGroup
.readEntry( "Type" );
79 if ( type
!= QLatin1String( "ServiceType" ) ) {
80 kWarning(7012) << "The service type config file " << desktopFile
.fileName() << " has Type=" << type
<< " instead of Type=ServiceType";
84 const QString serviceType
= desktopGroup
.readEntry( "X-KDE-ServiceType" );
86 if ( serviceType
.isEmpty() ) {
87 kWarning(7012) << "The service type config file " << desktopFile
.fileName() << " does not contain a ServiceType=... entry";
91 KServiceType
* e
= new KServiceType( &desktopFile
);
98 if ( !(e
->isValid()) ) {
99 kWarning(7012) << "Invalid ServiceType : " << file
;
108 KBuildServiceTypeFactory::saveHeader(QDataStream
&str
)
110 KSycocaFactory::saveHeader(str
);
111 str
<< (qint32
) m_propertyTypeDict
.count();
112 for (QMap
<QString
, int>::ConstIterator it
= m_propertyTypeDict
.begin(); it
!= m_propertyTypeDict
.end(); ++it
) {
113 str
<< it
.key() << (qint32
)it
.value();
118 KBuildServiceTypeFactory::save(QDataStream
&str
)
120 KSycocaFactory::save(str
);
121 #if 0 // not needed since don't have any additional index anymore
122 int endOfFactoryData
= str
.device()->pos();
124 // Update header (pass #3)
128 str
.device()->seek(endOfFactoryData
);
133 KBuildServiceTypeFactory::addEntry(const KSycocaEntry::Ptr
& newEntry
)
135 KServiceType::Ptr serviceType
= KServiceType::Ptr::staticCast( newEntry
);
136 if ( m_entryDict
->value( newEntry
->name() ) ) {
137 // Already exists -> replace
138 KSycocaFactory::removeEntry(newEntry
->name());
140 KSycocaFactory::addEntry(newEntry
);
142 const QMap
<QString
,QVariant::Type
>& pd
= serviceType
->propertyDefs();
143 QMap
<QString
,QVariant::Type
>::ConstIterator pit
= pd
.begin();
144 for( ; pit
!= pd
.end(); ++pit
) {
145 const QString property
= pit
.key();
146 QMap
<QString
, int>::iterator dictit
= m_propertyTypeDict
.find(property
);
147 if (dictit
== m_propertyTypeDict
.end())
148 m_propertyTypeDict
.insert(property
, pit
.value());
149 else if (*dictit
!= static_cast<int>(pit
.value()))
150 kWarning(7021) << "Property '"<< property
<< "' is defined multiple times ("<< serviceType
->name() <<")";