File indexing completed on 2024-05-12 15:42:13
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 2017 David Faure <faure@kde.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0006 */ 0007 0008 #include "searchproviderregistry.h" 0009 #include "searchprovider.h" 0010 0011 #include <QDir> 0012 #include <QStandardPaths> 0013 0014 SearchProviderRegistry::SearchProviderRegistry() 0015 { 0016 reload(); 0017 } 0018 0019 SearchProviderRegistry::~SearchProviderRegistry() 0020 { 0021 qDeleteAll(m_searchProviders); 0022 } 0023 0024 QStringList SearchProviderRegistry::directories() const 0025 { 0026 const QString testDir = QFile::decodeName(qgetenv("KIO_SEARCHPROVIDERS_DIR")); // for unittests 0027 if (!testDir.isEmpty()) { 0028 return {testDir}; 0029 } 0030 return QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("kservices5/searchproviders/"), QStandardPaths::LocateDirectory); 0031 } 0032 0033 void SearchProviderRegistry::reload() 0034 { 0035 m_searchProvidersByKey.clear(); 0036 m_searchProvidersByDesktopName.clear(); 0037 qDeleteAll(m_searchProviders); 0038 m_searchProviders.clear(); 0039 0040 const QStringList servicesDirs = directories(); 0041 for (const QString &dirPath : servicesDirs) { 0042 QDir dir(dirPath); 0043 const auto files = dir.entryList({QStringLiteral("*.desktop")}, QDir::Files); 0044 for (const QString &file : files) { 0045 if (!m_searchProvidersByDesktopName.contains(file)) { 0046 const QString filePath = dir.path() + QLatin1Char('/') + file; 0047 auto *provider = new SearchProvider(filePath); 0048 m_searchProvidersByDesktopName.insert(file, provider); 0049 m_searchProviders.append(provider); 0050 const auto keys = provider->keys(); 0051 for (const QString &key : keys) { 0052 m_searchProvidersByKey.insert(key, provider); 0053 } 0054 } 0055 } 0056 } 0057 } 0058 0059 QList<SearchProvider *> SearchProviderRegistry::findAll() 0060 { 0061 return m_searchProviders; 0062 } 0063 0064 SearchProvider *SearchProviderRegistry::findByKey(const QString &key) const 0065 { 0066 return m_searchProvidersByKey.value(key); 0067 } 0068 0069 SearchProvider *SearchProviderRegistry::findByDesktopName(const QString &name) const 0070 { 0071 return m_searchProvidersByDesktopName.value(name + QLatin1String(".desktop")); 0072 }