Changeset View
Changeset View
Standalone View
Standalone View
src/urifilters/ikws/searchproviderregistry.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * This file is part of the KDE project | ||||
3 | * Copyright 2017 David Faure <faure@kde.org> | ||||
4 | * | ||||
5 | * This program is free software; you can redistribute it and/or | ||||
6 | * modify it under the terms of the GNU General Public License as | ||||
7 | * published by the Free Software Foundation; either version 2 of | ||||
8 | * the License or (at your option) version 3 or any later version | ||||
9 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
10 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
11 | * defined in Section 14 of version 3 of the license. | ||||
12 | * | ||||
13 | * This program is distributed in the hope that it will be useful, | ||||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
16 | * GNU General Public License for more details. | ||||
17 | * | ||||
18 | * You should have received a copy of the GNU General Public License | ||||
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
20 | */ | ||||
21 | | ||||
22 | #include "searchproviderregistry.h" | ||||
23 | #include "searchprovider.h" | ||||
24 | | ||||
25 | #include <QStandardPaths> | ||||
26 | #include <QDir> | ||||
27 | #include <QDebug> | ||||
28 | | ||||
29 | SearchProviderRegistry::SearchProviderRegistry() | ||||
30 | { | ||||
31 | reload(); | ||||
32 | } | ||||
33 | | ||||
34 | SearchProviderRegistry::~SearchProviderRegistry() | ||||
35 | { | ||||
36 | qDeleteAll(m_searchProviders); | ||||
37 | } | ||||
38 | | ||||
39 | QStringList SearchProviderRegistry::directories() const | ||||
40 | { | ||||
41 | const QString testDir = QFile::decodeName(qgetenv("KIO_SEARCHPROVIDERS_DIR")); // for unittests | ||||
42 | if (!testDir.isEmpty()) | ||||
43 | return { testDir }; | ||||
44 | return QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("kservices5/searchproviders/"), QStandardPaths::LocateDirectory); | ||||
45 | } | ||||
46 | | ||||
47 | void SearchProviderRegistry::reload() | ||||
48 | { | ||||
49 | m_searchProvidersByKey.clear(); | ||||
50 | m_searchProvidersByDesktopName.clear(); | ||||
51 | qDeleteAll(m_searchProviders); | ||||
52 | m_searchProviders.clear(); | ||||
53 | | ||||
54 | const QStringList servicesDirs = directories(); | ||||
55 | for (const QString &dirPath : servicesDirs) { | ||||
56 | QDir dir(dirPath); | ||||
57 | for (const QString &file : dir.entryList({QStringLiteral("*.desktop")}, QDir::Files)) { | ||||
58 | if (!m_searchProvidersByDesktopName.contains(file)) { | ||||
59 | const QString filePath = dir.path() + QLatin1Char('/') + file; | ||||
60 | auto *provider = new SearchProvider(filePath); | ||||
61 | m_searchProvidersByDesktopName.insert(file, provider); | ||||
62 | m_searchProviders.append(provider); | ||||
63 | for (const QString &key : provider->keys()) { | ||||
64 | m_searchProvidersByKey.insert(key, provider); | ||||
65 | } | ||||
66 | } | ||||
67 | } | ||||
68 | } | ||||
69 | } | ||||
70 | | ||||
71 | QList<SearchProvider *> SearchProviderRegistry::findAll() | ||||
72 | { | ||||
73 | return m_searchProviders; | ||||
74 | } | ||||
75 | | ||||
76 | SearchProvider* SearchProviderRegistry::findByKey(const QString& key) const | ||||
77 | { | ||||
78 | return m_searchProvidersByKey.value(key); | ||||
79 | } | ||||
80 | | ||||
81 | SearchProvider* SearchProviderRegistry::findByDesktopName(const QString &name) const | ||||
82 | { | ||||
83 | return m_searchProvidersByDesktopName.value(name + ".desktop"); | ||||
84 | } |