File indexing completed on 2024-05-12 05:11:18

0001 /*
0002  * This file is part of the KDE Akonadi Search Project
0003  * SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  *
0007  */
0008 
0009 #include "searchstore.h"
0010 #include "akonadi_search_core_debug.h"
0011 
0012 #include <QCoreApplication>
0013 #include <QDir>
0014 #include <QMutex>
0015 #include <QPluginLoader>
0016 #include <QSharedPointer>
0017 #include <QThreadStorage>
0018 
0019 using namespace Akonadi::Search;
0020 
0021 SearchStore::SearchStore(QObject *parent)
0022     : QObject(parent)
0023 {
0024 }
0025 
0026 SearchStore::~SearchStore() = default;
0027 
0028 QUrl SearchStore::url(int)
0029 {
0030     return {};
0031 }
0032 
0033 QString SearchStore::icon(int)
0034 {
0035     return {};
0036 }
0037 
0038 QString SearchStore::text(int)
0039 {
0040     return {};
0041 }
0042 
0043 QString SearchStore::property(int, const QString &)
0044 {
0045     return {};
0046 }
0047 
0048 Q_GLOBAL_STATIC(SearchStore::List, s_overrideSearchStores)
0049 
0050 void SearchStore::overrideSearchStores(const QList<SearchStore *> &overrideSearchStores)
0051 {
0052     List *list = &(*s_overrideSearchStores);
0053     list->clear();
0054     list->reserve(overrideSearchStores.count());
0055 
0056     for (SearchStore *store : overrideSearchStores) {
0057         list->append(QSharedPointer<SearchStore>(store));
0058     }
0059 }
0060 
0061 //
0062 // Search Stores
0063 //
0064 // static
0065 SearchStore::List SearchStore::searchStores()
0066 {
0067     static QMutex mutex;
0068     QMutexLocker lock(&mutex);
0069 
0070     if (s_overrideSearchStores && !s_overrideSearchStores->isEmpty()) {
0071         qCDebug(AKONADI_SEARCH_CORE_LOG) << "Overriding search stores.";
0072         return *s_overrideSearchStores;
0073     }
0074 
0075     // Get all the plugins
0076     QStringList plugins;
0077     QStringList pluginPaths;
0078 
0079     const QStringList paths = QCoreApplication::libraryPaths();
0080     for (const QString &libraryPath : paths) {
0081         const QString path(libraryPath + QStringLiteral("/pim6/akonadi"));
0082         QDir dir(path);
0083 
0084         if (!dir.exists()) {
0085             continue;
0086         }
0087 
0088         const QStringList entryList = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
0089         for (const QString &fileName : entryList) {
0090             if (plugins.contains(fileName)) {
0091                 continue;
0092             }
0093 
0094             plugins << fileName;
0095             pluginPaths << dir.absoluteFilePath(fileName);
0096         }
0097     }
0098     plugins.clear();
0099 
0100     SearchStore::List stores;
0101     for (const QString &pluginPath : std::as_const(pluginPaths)) {
0102         QPluginLoader loader(pluginPath);
0103 
0104         const QVariantMap metadata = loader.metaData().toVariantMap()[QStringLiteral("MetaData")].toMap();
0105         if (metadata[QStringLiteral("X-Akonadi-PluginType")].toString() != QLatin1StringView("SearchStore")) {
0106             continue;
0107         }
0108         if (!loader.load()) {
0109             qCWarning(AKONADI_SEARCH_CORE_LOG) << "Could not create Akonadi Search Store: " << pluginPath;
0110             qCWarning(AKONADI_SEARCH_CORE_LOG) << loader.errorString();
0111             continue;
0112         }
0113         QObject *obj = loader.instance();
0114         if (obj) {
0115             SearchStore *ex = qobject_cast<SearchStore *>(obj);
0116             if (ex) {
0117                 stores << QSharedPointer<SearchStore>(ex);
0118                 qCDebug(AKONADI_SEARCH_CORE_LOG) << " Loaded plugins: " << pluginPath;
0119             } else {
0120                 qCDebug(AKONADI_SEARCH_CORE_LOG) << "Plugin could not be converted to an Akonadi::Search::SearchStore " << pluginPath;
0121             }
0122         } else {
0123             qCDebug(AKONADI_SEARCH_CORE_LOG) << "Plugin could not create instance" << pluginPath;
0124         }
0125     }
0126     return stores;
0127 }
0128 
0129 #include "moc_searchstore.cpp"