| 1 | /* |
|---|---|
| 2 | This file is part of the KDE project |
| 3 | SPDX-FileCopyrightText: 2017 David Faure <faure@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL |
| 6 | */ |
| 7 | |
| 8 | #include "searchprovider.h" |
| 9 | #include "searchproviderregistry_p.h" |
| 10 | |
| 11 | #include <QDir> |
| 12 | #include <QStandardPaths> |
| 13 | |
| 14 | using namespace KIO; |
| 15 | SearchProviderRegistry::SearchProviderRegistry() |
| 16 | { |
| 17 | reload(); |
| 18 | } |
| 19 | |
| 20 | SearchProviderRegistry::~SearchProviderRegistry() |
| 21 | { |
| 22 | qDeleteAll(c: m_searchProviders); |
| 23 | } |
| 24 | |
| 25 | QStringList SearchProviderRegistry::directories() const |
| 26 | { |
| 27 | const QString testDir = QFile::decodeName(localFileName: qgetenv(varName: "KIO_SEARCHPROVIDERS_DIR")); // for unittests |
| 28 | if (!testDir.isEmpty()) { |
| 29 | return {testDir}; |
| 30 | } |
| 31 | return QStandardPaths::locateAll(type: QStandardPaths::GenericDataLocation, QStringLiteral("kf6/searchproviders/"), options: QStandardPaths::LocateDirectory); |
| 32 | } |
| 33 | |
| 34 | void SearchProviderRegistry::reload() |
| 35 | { |
| 36 | m_searchProvidersByKey.clear(); |
| 37 | m_searchProvidersByDesktopName.clear(); |
| 38 | qDeleteAll(c: m_searchProviders); |
| 39 | m_searchProviders.clear(); |
| 40 | |
| 41 | const QStringList servicesDirs = directories(); |
| 42 | for (const QString &dirPath : servicesDirs) { |
| 43 | QDir dir(dirPath); |
| 44 | const auto files = dir.entryList(nameFilters: {QStringLiteral("*.desktop")}, filters: QDir::Files); |
| 45 | for (const QString &file : files) { |
| 46 | if (!m_searchProvidersByDesktopName.contains(key: file)) { |
| 47 | const QString filePath = dir.path() + QLatin1Char('/') + file; |
| 48 | auto *provider = new SearchProvider(filePath); |
| 49 | m_searchProvidersByDesktopName.insert(key: file, value: provider); |
| 50 | m_searchProviders.append(t: provider); |
| 51 | const auto keys = provider->keys(); |
| 52 | for (const QString &key : keys) { |
| 53 | m_searchProvidersByKey.insert(key, value: provider); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | QList<SearchProvider *> SearchProviderRegistry::findAll() |
| 61 | { |
| 62 | return m_searchProviders; |
| 63 | } |
| 64 | |
| 65 | SearchProvider *SearchProviderRegistry::findByKey(const QString &key) const |
| 66 | { |
| 67 | return m_searchProvidersByKey.value(key); |
| 68 | } |
| 69 | |
| 70 | SearchProvider *SearchProviderRegistry::findByDesktopName(const QString &name) const |
| 71 | { |
| 72 | return m_searchProvidersByDesktopName.value(key: name + QLatin1String(".desktop")); |
| 73 | } |
| 74 |
