File indexing completed on 2024-04-28 03:55:43
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 "searchprovider.h" 0009 #include "searchproviderregistry_p.h" 0010 0011 #include <QDir> 0012 #include <QStandardPaths> 0013 0014 using namespace KIO; 0015 SearchProviderRegistry::SearchProviderRegistry() 0016 { 0017 reload(); 0018 } 0019 0020 SearchProviderRegistry::~SearchProviderRegistry() 0021 { 0022 qDeleteAll(m_searchProviders); 0023 } 0024 0025 QStringList SearchProviderRegistry::directories() const 0026 { 0027 const QString testDir = QFile::decodeName(qgetenv("KIO_SEARCHPROVIDERS_DIR")); // for unittests 0028 if (!testDir.isEmpty()) { 0029 return {testDir}; 0030 } 0031 return QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("kf6/searchproviders/"), QStandardPaths::LocateDirectory); 0032 } 0033 0034 void SearchProviderRegistry::reload() 0035 { 0036 m_searchProvidersByKey.clear(); 0037 m_searchProvidersByDesktopName.clear(); 0038 qDeleteAll(m_searchProviders); 0039 m_searchProviders.clear(); 0040 0041 const QStringList servicesDirs = directories(); 0042 for (const QString &dirPath : servicesDirs) { 0043 QDir dir(dirPath); 0044 const auto files = dir.entryList({QStringLiteral("*.desktop")}, QDir::Files); 0045 for (const QString &file : files) { 0046 if (!m_searchProvidersByDesktopName.contains(file)) { 0047 const QString filePath = dir.path() + QLatin1Char('/') + file; 0048 auto *provider = new SearchProvider(filePath); 0049 m_searchProvidersByDesktopName.insert(file, provider); 0050 m_searchProviders.append(provider); 0051 const auto keys = provider->keys(); 0052 for (const QString &key : keys) { 0053 m_searchProvidersByKey.insert(key, provider); 0054 } 0055 } 0056 } 0057 } 0058 } 0059 0060 QList<SearchProvider *> SearchProviderRegistry::findAll() 0061 { 0062 return m_searchProviders; 0063 } 0064 0065 SearchProvider *SearchProviderRegistry::findByKey(const QString &key) const 0066 { 0067 return m_searchProvidersByKey.value(key); 0068 } 0069 0070 SearchProvider *SearchProviderRegistry::findByDesktopName(const QString &name) const 0071 { 0072 return m_searchProvidersByDesktopName.value(name + QLatin1String(".desktop")); 0073 }