KPeople

personpluginmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2013 David Edmundson <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 #include "personpluginmanager.h"
8 #include "backends/basepersonsdatasource.h"
9 
10 #include "kpeople_debug.h"
11 
12 #include <KPluginFactory>
13 #include <KPluginMetaData>
14 
15 #include <QMutex>
16 #include <QMutexLocker>
17 
18 using namespace KPeople;
19 
20 class PersonPluginManagerPrivate
21 {
22 public:
23  PersonPluginManagerPrivate();
24  ~PersonPluginManagerPrivate();
25  QHash<QString /* SourceName*/, BasePersonsDataSource *> dataSourcePlugins;
26 
27  void loadDataSourcePlugins();
28  bool m_autoloadDataSourcePlugins = true;
29  bool m_loadedDataSourcePlugins = false;
30  QMutex m_mutex;
31 };
32 
33 Q_GLOBAL_STATIC(PersonPluginManagerPrivate, s_instance)
34 
35 PersonPluginManagerPrivate::PersonPluginManagerPrivate()
36 {
37 }
38 
39 PersonPluginManagerPrivate::~PersonPluginManagerPrivate()
40 {
41  qDeleteAll(dataSourcePlugins);
42 }
43 
44 void PersonPluginManagerPrivate::loadDataSourcePlugins()
45 {
46  const QVector<KPluginMetaData> pluginList = KPluginMetaData::findPlugins(QStringLiteral("kpeople/datasource"));
47  for (const KPluginMetaData &data : pluginList) {
48  auto dataSourceResult = KPluginFactory::instantiatePlugin<BasePersonsDataSource>(data);
49  if (dataSourceResult) {
50  BasePersonsDataSource *dataSource = dataSourceResult.plugin;
51  const QString pluginId = dataSource->sourcePluginId();
52  if (!dataSourcePlugins.contains(pluginId)) {
53  dataSourcePlugins[pluginId] = dataSource;
54  } else {
55  dataSource->deleteLater();
56  qCDebug(KPEOPLE_LOG) << "Plugin" << pluginId << "was already loaded manually, ignoring...";
57  }
58  } else {
59  qCWarning(KPEOPLE_LOG) << "Failed to create data source " << dataSourceResult.errorText << data.fileName();
60  }
61  }
62 
63  m_loadedDataSourcePlugins = true;
64 }
65 
66 void PersonPluginManager::setAutoloadDataSourcePlugins(bool autoloadDataSourcePlugins)
67 {
68  s_instance->m_autoloadDataSourcePlugins = autoloadDataSourcePlugins;
69 }
70 
71 void PersonPluginManager::addDataSource(const QString &sourceId, BasePersonsDataSource *source)
72 {
73  QMutexLocker(&s_instance->m_mutex);
74  if (s_instance->dataSourcePlugins.contains(sourceId)) {
75  qCWarning(KPEOPLE_LOG) << "Attempting to load data source that is already loaded, overriding!";
76  s_instance->dataSourcePlugins[sourceId]->deleteLater();
77  }
78  s_instance->dataSourcePlugins.insert(sourceId, source);
79 }
80 
82 {
83  QMutexLocker(&s_instance->m_mutex);
84  qDeleteAll(s_instance->dataSourcePlugins);
85  s_instance->dataSourcePlugins = dataSources;
86  s_instance->m_loadedDataSourcePlugins = true;
87 }
88 
89 QList<BasePersonsDataSource *> PersonPluginManager::dataSourcePlugins()
90 {
91  QMutexLocker(&s_instance->m_mutex);
92  if (!s_instance->m_loadedDataSourcePlugins && s_instance->m_autoloadDataSourcePlugins) {
93  s_instance->loadDataSourcePlugins();
94  }
95  return s_instance->dataSourcePlugins.values();
96 }
97 
98 BasePersonsDataSource *PersonPluginManager::dataSource(const QString &sourceId)
99 {
100  QMutexLocker(&s_instance->m_mutex);
101  if (!s_instance->m_loadedDataSourcePlugins && s_instance->m_autoloadDataSourcePlugins) {
102  s_instance->loadDataSourcePlugins();
103  }
104  return s_instance->dataSourcePlugins.value(sourceId);
105 }
106 
107 bool KPeople::PersonPluginManager::addContact(const QVariantMap &properties)
108 {
109  bool ret = false;
110  for (auto p : std::as_const(s_instance->dataSourcePlugins)) {
111  auto v2 = dynamic_cast<BasePersonsDataSourceV2 *>(p);
112  if (!v2) {
113  continue;
114  }
115  const bool added = v2->addContact(properties);
116  ret |= added;
117  }
118  return ret;
119 }
120 
122 {
123  bool ret = false;
124  for (auto p : std::as_const(s_instance->dataSourcePlugins)) {
125  auto v2 = dynamic_cast<BasePersonsDataSourceV2 *>(p);
126  if (!v2) {
127  continue;
128  }
129  const bool deleted = v2->deleteContact(uri);
130  ret |= deleted;
131  }
132  return ret;
133 }
static void setDataSourcePlugins(const QHash< QString, BasePersonsDataSource * > &dataSources)
Instead of loading datasources from plugins, set sources manually This is for unit tests only...
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
static void addDataSource(const QString &sourceId, BasePersonsDataSource *source)
Adds custom data source.
static void setAutoloadDataSourcePlugins(bool autoloadDataSourcePlugins)
Use this if you explicitly don&#39;t want KPeople to autoload all the available data source plugins...
static bool deleteContact(const QString &uri)
Deletes a contact with the specified &p uri.
static bool addContact(const QVariantMap &properties)
Creates a contact with the specified properties.
static QVector< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter={})
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jan 27 2022 22:42:01 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.