KPeople

personpluginmanager.cpp
1 /*
2  Copyright (C) 2013 David Edmundson <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "personpluginmanager.h"
20 #include "backends/basepersonsdatasource.h"
21 
22 #include <KPluginMetaData>
23 #include <KPluginLoader>
24 #include <KPluginFactory>
25 
26 #include <QMutex>
27 #include <QMutexLocker>
28 #include "kpeople_debug.h"
29 
30 
31 using namespace KPeople;
32 
33 class PersonPluginManagerPrivate
34 {
35 public:
36  PersonPluginManagerPrivate();
37  ~PersonPluginManagerPrivate();
38  QHash<QString /* SourceName*/, BasePersonsDataSource *> dataSourcePlugins;
39 
40  void loadDataSourcePlugins();
41  bool m_autoloadDataSourcePlugins;
42  bool m_loadedDataSourcePlugins;
43  QMutex m_mutex;
44 
45 };
46 
47 Q_GLOBAL_STATIC(PersonPluginManagerPrivate, s_instance)
48 
49 PersonPluginManagerPrivate::PersonPluginManagerPrivate()
50  : m_autoloadDataSourcePlugins(true)
51  , m_loadedDataSourcePlugins(false)
52 {
53 }
54 
55 PersonPluginManagerPrivate::~PersonPluginManagerPrivate()
56 {
57  qDeleteAll(dataSourcePlugins);
58 }
59 
60 void PersonPluginManagerPrivate::loadDataSourcePlugins()
61 {
62  const QVector<KPluginMetaData> pluginList = KPluginLoader::findPlugins(QStringLiteral("kpeople/datasource"));
63  for (const KPluginMetaData &service : pluginList) {
64  KPluginLoader loader(service.fileName());
65  KPluginFactory *factory = loader.factory();
66  BasePersonsDataSource *dataSource = factory->create<BasePersonsDataSource>();
67  if (dataSource) {
68  const QString pluginId = dataSource->sourcePluginId();
69  if (!dataSourcePlugins.contains(pluginId)) {
70  dataSourcePlugins[pluginId] = dataSource;
71  } else {
72  dataSource->deleteLater();
73  qCDebug(KPEOPLE_LOG) << "Plugin" << pluginId << "was already loaded manually, ignoring...";
74  }
75  } else {
76  qCWarning(KPEOPLE_LOG) << "Failed to create data source " << service.name() << service.fileName();
77  }
78  }
79 
80  m_loadedDataSourcePlugins = true;
81 }
82 
83 void PersonPluginManager::setAutoloadDataSourcePlugins(bool autoloadDataSourcePlugins)
84 {
85  s_instance->m_autoloadDataSourcePlugins = autoloadDataSourcePlugins;
86 }
87 
88 void PersonPluginManager::addDataSource(const QString &sourceId, BasePersonsDataSource *source)
89 {
90  QMutexLocker(&s_instance->m_mutex);
91  if (s_instance->dataSourcePlugins.contains(sourceId)) {
92  qCWarning(KPEOPLE_LOG) << "Attempting to load data source that is already loaded, overriding!";
93  s_instance->dataSourcePlugins[sourceId]->deleteLater();
94  }
95  s_instance->dataSourcePlugins.insert(sourceId, source);
96 }
97 
99 {
100  QMutexLocker(&s_instance->m_mutex);
101  qDeleteAll(s_instance->dataSourcePlugins);
102  s_instance->dataSourcePlugins = dataSources;
103  s_instance->m_loadedDataSourcePlugins = true;
104 }
105 
106 QList<BasePersonsDataSource *> PersonPluginManager::dataSourcePlugins()
107 {
108  QMutexLocker(&s_instance->m_mutex);
109  if (!s_instance->m_loadedDataSourcePlugins && s_instance->m_autoloadDataSourcePlugins) {
110  s_instance->loadDataSourcePlugins();
111  }
112  return s_instance->dataSourcePlugins.values();
113 }
114 
115 BasePersonsDataSource *PersonPluginManager::dataSource(const QString &sourceId)
116 {
117  QMutexLocker(&s_instance->m_mutex);
118  if (!s_instance->m_loadedDataSourcePlugins && s_instance->m_autoloadDataSourcePlugins) {
119  s_instance->loadDataSourcePlugins();
120  }
121  return s_instance->dataSourcePlugins.value(sourceId);
122 }
123 
124 bool KPeople::PersonPluginManager::addContact(const QVariantMap& properties)
125 {
126  bool ret = false;
127  for (auto p : qAsConst(s_instance->dataSourcePlugins)) {
128  auto v2 = dynamic_cast<BasePersonsDataSourceV2*>(p);
129  if (!v2)
130  continue;
131  const bool added = v2->addContact(properties);
132  ret |= added;
133  }
134  return ret;
135 }
136 
138 {
139  bool ret = false;
140  for (auto p : qAsConst(s_instance->dataSourcePlugins)) {
141  auto v2 = dynamic_cast<BasePersonsDataSourceV2*>(p);
142  if (!v2)
143  continue;
144  const bool deleted = v2->deleteContact(uri);
145  ret |= deleted;
146  }
147  return ret;
148 }
static void setDataSourcePlugins(const QHash< QString, BasePersonsDataSource * > &dataSources)
Instead of loading datasources from plugins, set sources manually This is for unit tests only...
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 via K...
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=std::function< bool(const KPluginMetaData &)>())
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Aug 5 2020 22:43:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.