1/****************************************************************************
2**
3** Copyright (C) 2015 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the QtVersit module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL21$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 or version 3 as published by the Free
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22** following information to ensure the GNU Lesser General Public License
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25**
26** As a special exception, The Qt Company gives you certain additional
27** rights. These rights are described in The Qt Company LGPL Exception
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29**
30** $QT_END_LICENSE$
31**
32****************************************************************************/
33
34#include "qversitcontactpluginloader_p.h"
35
36#include <QtCore/qpluginloader.h>
37
38#include "qversitpluginsearch_p.h"
39
40QT_BEGIN_NAMESPACE_VERSIT
41
42/*!
43 A less-than function for factory indices (see QVersitContactHandlerFactory::index()).
44 Positive values come first (ascendingly), then zero, then negative values (ascendingly).
45 */
46bool factoryLessThan(QVersitContactHandlerFactory* a, QVersitContactHandlerFactory* b) {
47 if ((a->index() > 0 && b->index() > 0)
48 || (a->index() < 0 && b->index() < 0))
49 // same sign
50 return a->index() < b->index();
51 else
52 // a is zero
53 // or b is zero
54 // or opposite sign
55 return b->index() < a->index();
56}
57
58QVersitContactPluginLoader* QVersitContactPluginLoader::mInstance = NULL;
59
60/*!
61 * \class QVersitContactPluginLoader
62 * This is a singleton class that loads Versit plugins for contacts processing
63 \inmodule QtVersit
64 */
65
66QVersitContactPluginLoader::QVersitContactPluginLoader()
67{
68}
69
70/*!
71 * Returns the singleton instance of the QVersitContactPluginLoader.
72 */
73QVersitContactPluginLoader* QVersitContactPluginLoader::instance()
74{
75 if (!mInstance)
76 mInstance = new QVersitContactPluginLoader;
77 return mInstance;
78}
79
80void QVersitContactPluginLoader::loadPlugins() {
81 QStringList plugins = mobilityPlugins(QStringLiteral("versit"));
82 if (plugins != mPluginPaths) {
83 mPluginPaths = plugins;
84
85 foreach (const QString& pluginPath, mPluginPaths) {
86 QPluginLoader qpl(pluginPath);
87 QObject* plugin = qpl.instance();
88 QVersitContactHandlerFactory* contactPlugin =
89 qobject_cast<QVersitContactHandlerFactory*>(object: plugin);
90 if (contactPlugin && !mLoadedFactories.contains(value: contactPlugin->name())) {
91 mLoadedFactories.insert(value: contactPlugin->name());
92 mContactHandlerFactories.append(t: contactPlugin);
93 }
94 }
95 std::sort(first: mContactHandlerFactories.begin(), last: mContactHandlerFactories.end(), comp: factoryLessThan);
96 }
97}
98
99/*!
100 * Creates and returns handlers from the plugin. If \a profiles is the empty string, only handlers
101 * with an empty profile list are returned. If \a profiles is nonempty, only handlers with either
102 * an empty profile list or a profile list that contains the given \a profiles are returned.
103 *
104 * The caller is responsible for deleting all returned handlers.
105 */
106QList<QVersitContactHandler*> QVersitContactPluginLoader::createContactHandlers(const QStringList& profiles)
107{
108 loadPlugins();
109
110 QList<QVersitContactHandler*> handlers;
111 foreach (const QVersitContactHandlerFactory* factory, mContactHandlerFactories) {
112 // if the plugin specifies no profiles, include it
113 QSet<QString> factoryProfiles(factory->profiles());
114 bool includePlugin = factory->profiles().isEmpty();
115 if (!includePlugin) {
116 // if the plugin's profile list intersects with the requested profile list, include it.
117 foreach (const QString& profile, profiles) {
118 if (factoryProfiles.contains(value: profile)) {
119 includePlugin = true;
120 break;
121 }
122 }
123 }
124
125 if (includePlugin) {
126 QVersitContactHandler* handler = factory->createHandler();
127 handlers.append(t: handler);
128 }
129 }
130 return handlers;
131}
132
133QT_END_NAMESPACE_VERSIT
134

source code of qtpim/src/versit/qversitcontactpluginloader_p.cpp