1/* This file is part of the KDE libraries
2 * Copyright (C) 2000 Waldo Bastian <bastian@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 "kbuildservicegroupfactory.h"
20#include "ksycoca.h"
21#include "ksycocadict_p.h"
22#include "ksycocaresourcelist.h"
23#include <kservicegroup_p.h>
24
25#include <kglobal.h>
26#include <kstandarddirs.h>
27#include <kdebug.h>
28#include <klocale.h>
29#include <assert.h>
30#include <QtCore/QHash>
31
32KBuildServiceGroupFactory::KBuildServiceGroupFactory() :
33 KServiceGroupFactory()
34{
35 m_resourceList = new KSycocaResourceList;
36// m_resourceList->add( "apps", "*.directory" );
37
38 m_baseGroupDict = new KSycocaDict();
39}
40
41// return all service types for this factory
42// i.e. first arguments to m_resourceList->add() above
43QStringList KBuildServiceGroupFactory::resourceTypes()
44{
45 return QStringList(); // << "apps";
46}
47
48KBuildServiceGroupFactory::~KBuildServiceGroupFactory()
49{
50 delete m_resourceList;
51}
52
53KServiceGroup *
54KBuildServiceGroupFactory::createEntry( const QString&, const char * ) const
55{
56 // Unused
57 kWarning(7021) << "called!";
58 return 0;
59}
60
61
62void KBuildServiceGroupFactory::addNewEntryTo( const QString &menuName, const KService::Ptr& newEntry)
63{
64 KSycocaEntry::Ptr ptr = m_entryDict->value(menuName);
65 KServiceGroup::Ptr entry;
66 if (ptr && ptr->isType(KST_KServiceGroup))
67 entry = KServiceGroup::Ptr::staticCast( ptr );
68
69 if (!entry)
70 {
71 kWarning(7021) << "( " << menuName << ", " << newEntry->name() << " ): menu does not exists!";
72 return;
73 }
74 entry->addEntry( KSycocaEntry::Ptr::staticCast( newEntry ) );
75}
76
77KServiceGroup::Ptr
78KBuildServiceGroupFactory::addNew( const QString &menuName, const QString& file, KServiceGroup::Ptr entry, bool isDeleted)
79{
80 KSycocaEntry::Ptr ptr = m_entryDict->value(menuName);
81 if (ptr)
82 {
83 kWarning(7021) << "( " << menuName << ", " << file << " ): menu already exists!";
84 return KServiceGroup::Ptr::staticCast( ptr );
85 }
86
87 // Create new group entry
88 if (!entry)
89 entry = new KServiceGroup(file, menuName);
90
91 entry->d_func()->m_childCount = -1; // Recalculate
92
93 addEntry( KSycocaEntry::Ptr::staticCast(entry) );
94
95 if (menuName != "/")
96 {
97 // Make sure parent dir exists.
98 QString parent = menuName.left(menuName.length()-1);
99 int i = parent.lastIndexOf('/');
100 if (i > 0) {
101 parent = parent.left(i+1);
102 } else {
103 parent = '/';
104 }
105
106
107 KServiceGroup::Ptr parentEntry;
108 ptr = m_entryDict->value(parent);
109 if (ptr && ptr->isType(KST_KServiceGroup))
110 parentEntry = KServiceGroup::Ptr::staticCast( ptr );
111 if (!parentEntry)
112 {
113 kWarning(7021) << "( " << menuName << ", " << file << " ): parent menu does not exist!";
114 }
115 else
116 {
117 if (!isDeleted && !entry->isDeleted())
118 parentEntry->addEntry( KSycocaEntry::Ptr::staticCast( entry ) );
119 }
120 }
121 return entry;
122}
123
124void
125KBuildServiceGroupFactory::addNewChild( const QString &parent, const KSycocaEntry::Ptr& newEntry)
126{
127 QString name = "#parent#"+parent;
128
129 KServiceGroup::Ptr entry;
130 KSycocaEntry::Ptr ptr = m_entryDict->value(name);
131 if (ptr && ptr->isType(KST_KServiceGroup))
132 entry = KServiceGroup::Ptr::staticCast( ptr );
133
134 if (!entry)
135 {
136 entry = new KServiceGroup(name);
137 addEntry( KSycocaEntry::Ptr::staticCast( entry ) );
138 }
139 if (newEntry)
140 entry->addEntry( newEntry );
141}
142
143void
144KBuildServiceGroupFactory::addEntry( const KSycocaEntry::Ptr& newEntry)
145{
146 KSycocaFactory::addEntry(newEntry);
147
148 KServiceGroup::Ptr serviceGroup = KServiceGroup::Ptr::staticCast( newEntry );
149 serviceGroup->d_func()->m_serviceList.clear();
150
151 if ( !serviceGroup->baseGroupName().isEmpty() )
152 {
153 m_baseGroupDict->add( serviceGroup->baseGroupName(), newEntry );
154 }
155}
156
157void
158KBuildServiceGroupFactory::saveHeader(QDataStream &str)
159{
160 KSycocaFactory::saveHeader(str);
161
162 str << (qint32) m_baseGroupDictOffset;
163}
164
165void
166KBuildServiceGroupFactory::save(QDataStream &str)
167{
168 KSycocaFactory::save(str);
169
170 m_baseGroupDictOffset = str.device()->pos();
171 m_baseGroupDict->save(str);
172
173 int endOfFactoryData = str.device()->pos();
174
175 // Update header (pass #3)
176 saveHeader(str);
177
178 // Seek to end.
179 str.device()->seek(endOfFactoryData);
180}
181
182KServiceGroup::Ptr KBuildServiceGroupFactory::findGroupByDesktopPath( const QString &_name, bool deep )
183{
184 assert (KSycoca::self()->isBuilding());
185 Q_UNUSED(deep); // ?
186 // We're building a database - the service type must be in memory
187 KSycocaEntry::Ptr group = m_entryDict->value( _name );
188 return KServiceGroup::Ptr::staticCast( group );
189}
190