KIO

searchproviderregistry.cpp
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
14using namespace KIO;
15SearchProviderRegistry::SearchProviderRegistry()
16{
17 reload();
18}
19
20SearchProviderRegistry::~SearchProviderRegistry()
21{
22 qDeleteAll(m_searchProviders);
23}
24
25QStringList SearchProviderRegistry::directories() const
26{
27 const QString testDir = QFile::decodeName(qgetenv("KIO_SEARCHPROVIDERS_DIR")); // for unittests
28 if (!testDir.isEmpty()) {
29 return {testDir};
30 }
32}
33
34void SearchProviderRegistry::reload()
35{
36 m_searchProvidersByKey.clear();
37 m_searchProvidersByDesktopName.clear();
38 qDeleteAll(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({QStringLiteral("*.desktop")}, QDir::Files);
45 for (const QString &file : files) {
46 if (!m_searchProvidersByDesktopName.contains(file)) {
47 const QString filePath = dir.path() + QLatin1Char('/') + file;
48 auto *provider = new SearchProvider(filePath);
49 m_searchProvidersByDesktopName.insert(file, provider);
50 m_searchProviders.append(provider);
51 const auto keys = provider->keys();
52 for (const QString &key : keys) {
53 m_searchProvidersByKey.insert(key, provider);
54 }
55 }
56 }
57 }
58}
59
60QList<SearchProvider *> SearchProviderRegistry::findAll()
61{
62 return m_searchProviders;
63}
64
65SearchProvider *SearchProviderRegistry::findByKey(const QString &key) const
66{
67 return m_searchProvidersByKey.value(key);
68}
69
70SearchProvider *SearchProviderRegistry::findByDesktopName(const QString &name) const
71{
72 return m_searchProvidersByDesktopName.value(name + QLatin1String(".desktop"));
73}
A namespace for KIO globals.
KIOCORE_EXPORT QString dir(const QString &fileClass)
Returns the most recently used directory associated with this file-class.
const QList< QKeySequence > & reload()
QString decodeName(const QByteArray &localFileName)
T value(qsizetype i) const const
QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:49:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.