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 "ksycoca.h"
21#include "ksycocadict_p.h"
22#include "ksycocaresourcelist.h"
23
24#include <kglobal.h>
25#include <kstandarddirs.h>
26#include <kdebug.h>
27#include <klocale.h>
28#include <assert.h>
29#include <kdesktopfile.h>
30#include <kconfiggroup.h>
31#include <QtCore/QHash>
32
33KBuildServiceTypeFactory::KBuildServiceTypeFactory() :
34 KServiceTypeFactory()
35{
36 m_resourceList = new KSycocaResourceList;
37 m_resourceList->add("servicetypes", "*.desktop");
38}
39
40// return all service types for this factory
41// i.e. first arguments to m_resourceList->add() above
42QStringList KBuildServiceTypeFactory::resourceTypes()
43{
44 return QStringList() << "servicetypes";
45}
46
47KBuildServiceTypeFactory::~KBuildServiceTypeFactory()
48{
49 delete m_resourceList;
50}
51
52KServiceType::Ptr KBuildServiceTypeFactory::findServiceTypeByName(const QString &_name)
53{
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 );
58}
59
60
61KSycocaEntry* KBuildServiceTypeFactory::createEntry(const QString &file, const char *resource) const
62{
63 QString name = file;
64 int pos = name.lastIndexOf('/');
65 if (pos != -1) {
66 name = name.mid(pos+1);
67 }
68
69 if (name.isEmpty())
70 return 0;
71
72 KDesktopFile desktopFile(resource, file);
73 const KConfigGroup desktopGroup = desktopFile.desktopGroup();
74
75 if ( desktopGroup.readEntry( "Hidden", false ) == true )
76 return 0;
77
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";
81 return 0;
82 }
83
84 const QString serviceType = desktopGroup.readEntry( "X-KDE-ServiceType" );
85
86 if ( serviceType.isEmpty() ) {
87 kWarning(7012) << "The service type config file " << desktopFile.fileName() << " does not contain a ServiceType=... entry";
88 return 0;
89 }
90
91 KServiceType* e = new KServiceType( &desktopFile );
92
93 if (e->isDeleted()) {
94 delete e;
95 return 0;
96 }
97
98 if ( !(e->isValid()) ) {
99 kWarning(7012) << "Invalid ServiceType : " << file;
100 delete e;
101 return 0;
102 }
103
104 return e;
105}
106
107void
108KBuildServiceTypeFactory::saveHeader(QDataStream &str)
109{
110 KSycocaFactory::saveHeader(str);
111 str << (qint32) m_propertyTypeDict.count();
112 for (QMap<QString, int>::ConstIterator it = m_propertyTypeDict.constBegin(); it != m_propertyTypeDict.constEnd(); ++it) {
113 str << it.key() << (qint32)it.value();
114 }
115}
116
117void
118KBuildServiceTypeFactory::save(QDataStream &str)
119{
120 KSycocaFactory::save(str);
121#if 0 // not needed since don't have any additional index anymore
122 int endOfFactoryData = str.device()->pos();
123
124 // Update header (pass #3)
125 saveHeader(str);
126
127 // Seek to end.
128 str.device()->seek(endOfFactoryData);
129#endif
130}
131
132void
133KBuildServiceTypeFactory::addEntry(const KSycocaEntry::Ptr& newEntry)
134{
135 KSycocaFactory::addEntry(newEntry);
136
137 KServiceType::Ptr serviceType = KServiceType::Ptr::staticCast( newEntry );
138
139 const QMap<QString,QVariant::Type>& pd = serviceType->propertyDefs();
140 QMap<QString,QVariant::Type>::ConstIterator pit = pd.begin();
141 for( ; pit != pd.end(); ++pit ) {
142 const QString property = pit.key();
143 QMap<QString, int>::iterator dictit = m_propertyTypeDict.find(property);
144 if (dictit == m_propertyTypeDict.end())
145 m_propertyTypeDict.insert(property, pit.value());
146 else if (*dictit != static_cast<int>(pit.value()))
147 kWarning(7021) << "Property '"<< property << "' is defined multiple times ("<< serviceType->name() <<")";
148 }
149}
150
151