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 QtVersitOrganizer 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 "qversitorganizerpluginloader_p.h" |
35 | #include "qversitorganizerpluginsearch_p.h" |
36 | |
37 | #include <QtCore/qpluginloader.h> |
38 | |
39 | #include "qversitorganizerhandler.h" |
40 | #include "qversittimezonehandler.h" |
41 | |
42 | QT_BEGIN_NAMESPACE_VERSITORGANIZER |
43 | |
44 | /*! |
45 | A less-than function for factory indices (see QVersitOrganizerHandlerFactory::index()). |
46 | Positive values come first (ascendingly), then zero, then negative values (ascendingly). |
47 | */ |
48 | bool factoryLessThan(QVersitOrganizerHandlerFactory* a, QVersitOrganizerHandlerFactory* b) { |
49 | if ((a->index() > 0 && b->index() > 0) |
50 | || (a->index() < 0 && b->index() < 0)) |
51 | // same sign |
52 | return a->index() < b->index(); |
53 | else |
54 | // a is zero |
55 | // or b is zero |
56 | // or opposite sign |
57 | return b->index() < a->index(); |
58 | } |
59 | |
60 | QVersitOrganizerPluginLoader* QVersitOrganizerPluginLoader::mInstance = NULL; |
61 | |
62 | /*! |
63 | * \class QVersitOrganizerPluginLoader |
64 | * \internal |
65 | * This is a singleton class that loads Versit plugins for organizer item processing |
66 | */ |
67 | |
68 | QVersitOrganizerPluginLoader::QVersitOrganizerPluginLoader() : mTimeZoneHandler(NULL) |
69 | { |
70 | } |
71 | |
72 | /*! |
73 | * Returns the singleton instance of the QVersitOrganizerPluginLoader. |
74 | */ |
75 | QVersitOrganizerPluginLoader* QVersitOrganizerPluginLoader::instance() |
76 | { |
77 | if (!mInstance) |
78 | mInstance = new QVersitOrganizerPluginLoader; |
79 | return mInstance; |
80 | } |
81 | |
82 | void QVersitOrganizerPluginLoader::loadPlugins() { |
83 | QStringList plugins = mobilityPlugins(QStringLiteral("versit" )); |
84 | if (plugins != mPluginPaths) { |
85 | mPluginPaths = plugins; |
86 | |
87 | foreach (const QString& pluginPath, mPluginPaths) { |
88 | QPluginLoader qpl(pluginPath); |
89 | QObject* plugin = qpl.instance(); |
90 | QVersitOrganizerHandlerFactory* organizerPlugin = |
91 | qobject_cast<QVersitOrganizerHandlerFactory*>(object: plugin); |
92 | if (organizerPlugin && !mLoadedFactories.contains(value: organizerPlugin->name())) { |
93 | mLoadedFactories.insert(value: organizerPlugin->name()); |
94 | mOrganizerHandlerFactories.append(t: organizerPlugin); |
95 | } else if (!mTimeZoneHandler) { |
96 | QVersitTimeZoneHandler* timeZonePlugin = |
97 | qobject_cast<QVersitTimeZoneHandler*>(object: plugin); |
98 | if (timeZonePlugin) { |
99 | mTimeZoneHandler = timeZonePlugin; |
100 | } |
101 | } |
102 | } |
103 | std::sort(first: mOrganizerHandlerFactories.begin(), last: mOrganizerHandlerFactories.end(), comp: factoryLessThan); |
104 | } |
105 | } |
106 | |
107 | /*! |
108 | * Creates and returns handlers from the plugin. If \a profile is the empty string, only handlers |
109 | * with an empty profile list are returned. If \a profile is nonempty, only handlers with either |
110 | * an empty profile list or a profile list that contains the given \a profile are returned. |
111 | * |
112 | * The caller is responsible for deleting all returned handlers. |
113 | */ |
114 | QList<QVersitOrganizerHandler*> QVersitOrganizerPluginLoader::createOrganizerHandlers(const QString& profile) |
115 | { |
116 | loadPlugins(); |
117 | |
118 | QList<QVersitOrganizerHandler*> handlers; |
119 | foreach (const QVersitOrganizerHandlerFactory* factory, mOrganizerHandlerFactories) { |
120 | if (factory->profiles().isEmpty() || |
121 | (!profile.isEmpty() && factory->profiles().contains(value: profile))) { |
122 | QVersitOrganizerHandler* handler = factory->createHandler(); |
123 | handlers.append(t: handler); |
124 | } |
125 | } |
126 | return handlers; |
127 | } |
128 | |
129 | QVersitTimeZoneHandler* QVersitOrganizerPluginLoader::timeZoneHandler() |
130 | { |
131 | loadPlugins(); |
132 | |
133 | return mTimeZoneHandler; |
134 | } |
135 | |
136 | QT_END_NAMESPACE_VERSITORGANIZER |
137 | |