KDED
kbuildservicetypefactory.cpp
Go to the documentation of this file.00001 /* This file is part of the KDE libraries 00002 * Copyright (C) 1999 David Faure <faure@kde.org> 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Library General Public 00006 * License version 2 as published by the Free Software Foundation; 00007 * 00008 * This library is distributed in the hope that it will be useful, 00009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * Library General Public License for more details. 00012 * 00013 * You should have received a copy of the GNU Library General Public License 00014 * along with this library; see the file COPYING.LIB. If not, write to 00015 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 * Boston, MA 02110-1301, USA. 00017 **/ 00018 00019 #include "kbuildservicetypefactory.h" 00020 #include "ksycoca.h" 00021 #include "ksycocadict.h" 00022 #include "kresourcelist.h" 00023 00024 #include <kglobal.h> 00025 #include <kstandarddirs.h> 00026 #include <kdebug.h> 00027 #include <klocale.h> 00028 #include <assert.h> 00029 #include <kdesktopfile.h> 00030 #include <kconfiggroup.h> 00031 #include <QtCore/QHash> 00032 00033 KBuildServiceTypeFactory::KBuildServiceTypeFactory() : 00034 KServiceTypeFactory() 00035 { 00036 m_resourceList = new KSycocaResourceList; 00037 m_resourceList->add("servicetypes", "*.desktop"); 00038 } 00039 00040 // return all service types for this factory 00041 // i.e. first arguments to m_resourceList->add() above 00042 QStringList KBuildServiceTypeFactory::resourceTypes() 00043 { 00044 return QStringList() << "servicetypes"; 00045 } 00046 00047 KBuildServiceTypeFactory::~KBuildServiceTypeFactory() 00048 { 00049 delete m_resourceList; 00050 } 00051 00052 KServiceType::Ptr KBuildServiceTypeFactory::findServiceTypeByName(const QString &_name) 00053 { 00054 assert (KSycoca::self()->isBuilding()); 00055 // We're building a database - the service type must be in memory 00056 KSycocaEntry::Ptr servType = m_entryDict->value( _name ); 00057 return KServiceType::Ptr::staticCast( servType ); 00058 } 00059 00060 00061 KSycocaEntry* KBuildServiceTypeFactory::createEntry(const QString &file, const char *resource) const 00062 { 00063 QString name = file; 00064 int pos = name.lastIndexOf('/'); 00065 if (pos != -1) { 00066 name = name.mid(pos+1); 00067 } 00068 00069 if (name.isEmpty()) 00070 return 0; 00071 00072 KDesktopFile desktopFile(resource, file); 00073 const KConfigGroup desktopGroup = desktopFile.desktopGroup(); 00074 00075 if ( desktopGroup.readEntry( "Hidden", false ) == true ) 00076 return 0; 00077 00078 const QString type = desktopGroup.readEntry( "Type" ); 00079 if ( type != QLatin1String( "ServiceType" ) ) { 00080 kWarning(7012) << "The service type config file " << desktopFile.fileName() << " has Type=" << type << " instead of Type=ServiceType"; 00081 return 0; 00082 } 00083 00084 const QString serviceType = desktopGroup.readEntry( "X-KDE-ServiceType" ); 00085 00086 if ( serviceType.isEmpty() ) { 00087 kWarning(7012) << "The service type config file " << desktopFile.fileName() << " does not contain a ServiceType=... entry"; 00088 return 0; 00089 } 00090 00091 KServiceType* e = new KServiceType( &desktopFile ); 00092 00093 if (e->isDeleted()) { 00094 delete e; 00095 return 0; 00096 } 00097 00098 if ( !(e->isValid()) ) { 00099 kWarning(7012) << "Invalid ServiceType : " << file; 00100 delete e; 00101 return 0; 00102 } 00103 00104 return e; 00105 } 00106 00107 void 00108 KBuildServiceTypeFactory::saveHeader(QDataStream &str) 00109 { 00110 KSycocaFactory::saveHeader(str); 00111 str << (qint32) m_propertyTypeDict.count(); 00112 for (QMap<QString, int>::ConstIterator it = m_propertyTypeDict.begin(); it != m_propertyTypeDict.end(); ++it) { 00113 str << it.key() << (qint32)it.value(); 00114 } 00115 } 00116 00117 void 00118 KBuildServiceTypeFactory::save(QDataStream &str) 00119 { 00120 KSycocaFactory::save(str); 00121 #if 0 // not needed since don't have any additional index anymore 00122 int endOfFactoryData = str.device()->pos(); 00123 00124 // Update header (pass #3) 00125 saveHeader(str); 00126 00127 // Seek to end. 00128 str.device()->seek(endOfFactoryData); 00129 #endif 00130 } 00131 00132 void 00133 KBuildServiceTypeFactory::addEntry(const KSycocaEntry::Ptr& newEntry) 00134 { 00135 KServiceType::Ptr serviceType = KServiceType::Ptr::staticCast( newEntry ); 00136 if ( m_entryDict->value( newEntry->name() ) ) { 00137 // Already exists -> replace 00138 KSycocaFactory::removeEntry(newEntry->name()); 00139 } 00140 KSycocaFactory::addEntry(newEntry); 00141 00142 const QMap<QString,QVariant::Type>& pd = serviceType->propertyDefs(); 00143 QMap<QString,QVariant::Type>::ConstIterator pit = pd.begin(); 00144 for( ; pit != pd.end(); ++pit ) { 00145 const QString property = pit.key(); 00146 QMap<QString, int>::iterator dictit = m_propertyTypeDict.find(property); 00147 if (dictit == m_propertyTypeDict.end()) 00148 m_propertyTypeDict.insert(property, pit.value()); 00149 else if (*dictit != static_cast<int>(pit.value())) 00150 kWarning(7021) << "Property '"<< property << "' is defined multiple times ("<< serviceType->name() <<")"; 00151 } 00152 } 00153