|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qcontactmanager.h" |
|
43 #include "qcontactmanager_p.h" |
|
44 #include "qcontactmanagerengine.h" |
|
45 #include "qcontactmanagerenginefactory.h" |
|
46 |
|
47 #include "qcontact_p.h" |
|
48 |
|
49 #include "qcontactaction.h" |
|
50 #include "qcontactactiondescriptor.h" |
|
51 #include "qcontactactionfactory.h" |
|
52 |
|
53 #include <QSharedData> |
|
54 #include <QtPlugin> |
|
55 #include <QPluginLoader> |
|
56 |
|
57 #include <QDebug> |
|
58 #include <QDir> |
|
59 #include <QFile> |
|
60 |
|
61 #include <QApplication> |
|
62 |
|
63 #if defined(Q_OS_SYMBIAN) |
|
64 # include <f32file.h> |
|
65 #endif |
|
66 |
|
67 #include "qcontactmemorybackend_p.h" |
|
68 #include "qcontactinvalidbackend_p.h" |
|
69 #include "qmobilitypluginsearch.h" |
|
70 |
|
71 QTM_BEGIN_NAMESPACE |
|
72 |
|
73 /* Shared QContactManager stuff here, default engine stuff below */ |
|
74 QList<QContactActionFactory*> QContactManagerData::m_actionfactories; // list of all factories |
|
75 QList<QContactActionDescriptor> QContactManagerData::m_descriptors; |
|
76 QHash<QString, QContactManagerEngineFactory*> QContactManagerData::m_engines; |
|
77 QContactManagerData::DescriptorHash QContactManagerData::m_descriptormap; |
|
78 QHash<QString, int> QContactManagerData::m_vendormap; |
|
79 QHash<QString, int> QContactManagerData::m_actionmap; |
|
80 |
|
81 bool QContactManagerData::m_discovered; |
|
82 bool QContactManagerData::m_discoveredStatic; |
|
83 QStringList QContactManagerData::m_pluginPaths; |
|
84 |
|
85 static void qContactsCleanEngines() |
|
86 { |
|
87 QContactManagerData::m_discovered = false; |
|
88 QList<QContactManagerEngineFactory*> factories = QContactManagerData::m_engines.values(); |
|
89 QList<QContactActionFactory*> actionfactories = QContactManagerData::m_actionfactories; |
|
90 |
|
91 for (int i=0; i < factories.count(); i++) { |
|
92 delete factories.at(i); |
|
93 } |
|
94 for(int i=0; i < actionfactories.count(); i++) { |
|
95 delete actionfactories.at(i); |
|
96 } |
|
97 QContactManagerData::m_engines.clear(); |
|
98 QContactManagerData::m_actionfactories.clear(); |
|
99 QContactManagerData::m_descriptors.clear(); |
|
100 QContactManagerData::m_descriptormap.clear(); |
|
101 QContactManagerData::m_actionmap.clear(); |
|
102 QContactManagerData::m_vendormap.clear(); |
|
103 } |
|
104 |
|
105 |
|
106 static int parameterValue(const QMap<QString, QString>& parameters, const char* key, int defaultValue) |
|
107 { |
|
108 if (parameters.contains(QString::fromAscii(key))) { |
|
109 bool ok; |
|
110 int version = parameters.value(QString::fromAscii(key)).toInt(&ok); |
|
111 |
|
112 if (ok) |
|
113 return version; |
|
114 } |
|
115 return defaultValue; |
|
116 } |
|
117 |
|
118 void QContactManagerData::createEngine(const QString& managerName, const QMap<QString, QString>& parameters) |
|
119 { |
|
120 m_engine = 0; |
|
121 |
|
122 QString builtManagerName = managerName.isEmpty() ? QContactManager::availableManagers().value(0) : managerName; |
|
123 if (builtManagerName == QLatin1String("memory")) { |
|
124 m_engine = QContactMemoryEngine::createMemoryEngine(parameters); |
|
125 } else { |
|
126 int implementationVersion = parameterValue(parameters, QTCONTACTS_IMPLEMENTATION_VERSION_NAME, -1); |
|
127 |
|
128 bool found = false; |
|
129 bool loadedDynamic = false; |
|
130 |
|
131 /* First check static factories */ |
|
132 loadStaticFactories(); |
|
133 |
|
134 /* See if we got a fast hit */ |
|
135 QList<QContactManagerEngineFactory*> factories = m_engines.values(builtManagerName); |
|
136 m_error = QContactManager::NoError; |
|
137 |
|
138 while(!found) { |
|
139 foreach (QContactManagerEngineFactory* f, factories) { |
|
140 QList<int> versions = f->supportedImplementationVersions(); |
|
141 if (implementationVersion == -1 ||//no given implementation version required |
|
142 versions.isEmpty() || //the manager engine factory does not report any version |
|
143 versions.contains(implementationVersion)) { |
|
144 m_engine = f->engine(parameters, &m_error); |
|
145 found = true; |
|
146 break; |
|
147 } |
|
148 } |
|
149 |
|
150 // Break if found or if this is the second time through |
|
151 if (loadedDynamic || found) |
|
152 break; |
|
153 |
|
154 // otherwise load dynamic factories and reloop |
|
155 loadFactories(); |
|
156 factories = m_engines.values(builtManagerName); |
|
157 loadedDynamic = true; |
|
158 } |
|
159 |
|
160 // XXX remove this |
|
161 // the engine factory could lie to us, so check the real implementation version |
|
162 if (m_engine && (implementationVersion != -1 && m_engine->managerVersion() != implementationVersion)) { |
|
163 m_error = QContactManager::VersionMismatchError; |
|
164 m_engine = 0; |
|
165 } |
|
166 |
|
167 if (!m_engine) { |
|
168 if (m_error == QContactManager::NoError) |
|
169 m_error = QContactManager::DoesNotExistError; |
|
170 m_engine = new QContactInvalidEngine(); |
|
171 } |
|
172 } |
|
173 } |
|
174 |
|
175 |
|
176 void QContactManagerData::loadStaticFactories() |
|
177 { |
|
178 if (!m_discoveredStatic) { |
|
179 #if !defined QT_NO_DEBUG |
|
180 const bool showDebug = qgetenv("QT_DEBUG_PLUGINS").toInt() > 0; |
|
181 #endif |
|
182 |
|
183 m_discoveredStatic = true; |
|
184 |
|
185 /* Clean stuff up at the end */ |
|
186 qAddPostRoutine(qContactsCleanEngines); |
|
187 |
|
188 /* Loop over all the static plugins */ |
|
189 QObjectList staticPlugins = QPluginLoader::staticInstances(); |
|
190 for (int i=0; i < staticPlugins.count(); i++ ){ |
|
191 QContactManagerEngineFactory *f = qobject_cast<QContactManagerEngineFactory*>(staticPlugins.at(i)); |
|
192 QContactActionFactory *g = qobject_cast<QContactActionFactory*>(staticPlugins.at(i)); |
|
193 if (f) { |
|
194 QString name = f->managerName(); |
|
195 #if !defined QT_NO_DEBUG |
|
196 if (showDebug) |
|
197 qDebug() << "Static: found an engine plugin" << f << "with name" << name; |
|
198 #endif |
|
199 if (name != QLatin1String("memory") && name != QLatin1String("invalid") && !name.isEmpty()) { |
|
200 // we also need to ensure that we haven't already loaded this factory. |
|
201 if (m_engines.keys().contains(name)) { |
|
202 qWarning() << "Static contacts plugin" << name << "has the same name as a currently loaded plugin; ignored"; |
|
203 } else { |
|
204 m_engines.insertMulti(name, f); |
|
205 } |
|
206 } else { |
|
207 qWarning() << "Static contacts plugin with reserved name" << name << "ignored"; |
|
208 } |
|
209 } |
|
210 |
|
211 if (g) { |
|
212 QString name = g->name(); |
|
213 #if !defined QT_NO_DEBUG |
|
214 if (showDebug) |
|
215 qDebug() << "Static: found an action factory" << g << "with name" << name; |
|
216 #endif |
|
217 if (m_actionfactories.contains(g)) { |
|
218 qWarning() << "Static contacts plugin" << name << "has the same name as currently loaded plugin; ignored"; |
|
219 } else { |
|
220 m_actionfactories.append(g); |
|
221 |
|
222 QList<QContactActionDescriptor> actions = g->actionDescriptors(); |
|
223 QMap<QContactActionDescriptor, QContactActionFactory*>::iterator it; |
|
224 for (int j = 0; j < actions.size(); j++) { |
|
225 QContactActionDescriptor desc = actions.at(j); |
|
226 m_descriptormap.insert(desc, g); |
|
227 m_descriptors.append(desc); |
|
228 m_actionmap.insertMulti(desc.actionName(), m_descriptors.count() - 1); |
|
229 m_vendormap.insertMulti(desc.vendorName(), m_descriptors.count() - 1); |
|
230 } |
|
231 } |
|
232 } |
|
233 } |
|
234 } |
|
235 } |
|
236 |
|
237 |
|
238 /* Plugin loader */ |
|
239 void QContactManagerData::loadFactories() |
|
240 { |
|
241 #if !defined QT_NO_DEBUG |
|
242 const bool showDebug = qgetenv("QT_DEBUG_PLUGINS").toInt() > 0; |
|
243 #endif |
|
244 |
|
245 // Always do this.. |
|
246 loadStaticFactories(); |
|
247 |
|
248 QStringList plugins; |
|
249 plugins = mobilityPlugins(QLatin1String("contacts")); |
|
250 |
|
251 if (!m_discovered || plugins != m_pluginPaths) { |
|
252 m_discovered = true; |
|
253 m_pluginPaths = plugins; |
|
254 |
|
255 /* Now discover the dynamic plugins */ |
|
256 for (int i=0; i < m_pluginPaths.count(); i++) { |
|
257 QPluginLoader qpl(m_pluginPaths.at(i)); |
|
258 QContactManagerEngineFactory *f = qobject_cast<QContactManagerEngineFactory*>(qpl.instance()); |
|
259 QContactActionFactory *g = qobject_cast<QContactActionFactory*>(qpl.instance()); |
|
260 |
|
261 if (f) { |
|
262 QString name = f->managerName(); |
|
263 #if !defined QT_NO_DEBUG |
|
264 if (showDebug) |
|
265 qDebug() << "Dynamic: found a contact engine plugin" << f << "with name" << name; |
|
266 #endif |
|
267 if (name != QLatin1String("memory") && name != QLatin1String("invalid") && !name.isEmpty()) { |
|
268 // we also need to ensure that we haven't already loaded this factory. |
|
269 if (m_engines.keys().contains(name)) { |
|
270 qWarning() << "Contacts plugin" << m_pluginPaths.at(i) << "has the same name as currently loaded plugin" << name << "; ignored"; |
|
271 } else { |
|
272 m_engines.insertMulti(name, f); |
|
273 } |
|
274 } else { |
|
275 qWarning() << "Contacts plugin" << m_pluginPaths.at(i) << "with reserved name" << name << "ignored"; |
|
276 } |
|
277 } |
|
278 |
|
279 if (g) { |
|
280 QString name = g->name(); |
|
281 #if !defined QT_NO_DEBUG |
|
282 if (showDebug) |
|
283 qDebug() << "Dynamic: found a contact action factory" << g << "with name" << name; |
|
284 #endif |
|
285 // we also need to ensure that we haven't already loaded this factory. |
|
286 if (m_actionfactories.contains(g)) { |
|
287 qWarning() << "Contacts plugin" << plugins.at(i) << "has the same name as currently loaded plugin" << name << "; ignored"; |
|
288 } else { |
|
289 m_actionfactories.append(g); |
|
290 |
|
291 QList<QContactActionDescriptor> actions = g->actionDescriptors(); |
|
292 QMap<QContactActionDescriptor, QContactActionFactory*>::iterator it; |
|
293 for (int j = 0; j < actions.size(); j++) { |
|
294 const QContactActionDescriptor& desc = actions.at(j); |
|
295 m_descriptormap.insert(desc, g); |
|
296 m_descriptors.append(desc); |
|
297 m_actionmap.insertMulti(desc.actionName(), m_descriptors.count() - 1); |
|
298 m_vendormap.insertMulti(desc.vendorName(), m_descriptors.count() - 1); |
|
299 } |
|
300 } |
|
301 } |
|
302 |
|
303 /* Debugging */ |
|
304 #if !defined QT_NO_DEBUG |
|
305 if (showDebug && !f && !g) { |
|
306 qDebug() << "Unknown plugin:" << qpl.errorString(); |
|
307 if (qpl.instance()) { |
|
308 qDebug() << "[qobject:" << qpl.instance() << "]"; |
|
309 } |
|
310 } |
|
311 #endif |
|
312 } |
|
313 |
|
314 QStringList engineNames; |
|
315 foreach (QContactManagerEngineFactory* f, m_engines.values()) { |
|
316 QStringList versions; |
|
317 foreach (int v, f->supportedImplementationVersions()) { |
|
318 versions << QString::fromAscii("%1").arg(v); |
|
319 } |
|
320 engineNames << QString::fromAscii("%1[%2]").arg(f->managerName()).arg(versions.join(QString::fromAscii(","))); |
|
321 } |
|
322 #if !defined QT_NO_DEBUG |
|
323 if (showDebug) { |
|
324 qDebug() << "Found engines:" << engineNames; |
|
325 qDebug() << "Found actions:" << m_actionmap.keys(); |
|
326 } |
|
327 #endif |
|
328 } |
|
329 } |
|
330 |
|
331 QList<QContactActionDescriptor> QContactManagerData::actionDescriptors(const QString& actionName, const QString& vendorName, int implementationVersion) |
|
332 { |
|
333 loadFactories(); |
|
334 |
|
335 bool restrict = false; |
|
336 QSet<int> subset; |
|
337 QList<QContactActionDescriptor> descriptors; |
|
338 |
|
339 // Go through our list of descriptors, looking for a match |
|
340 if (!actionName.isEmpty()) { |
|
341 subset = m_actionmap.values(actionName).toSet(); |
|
342 restrict = true; |
|
343 } |
|
344 |
|
345 if (!vendorName.isEmpty()) { |
|
346 if (restrict) |
|
347 subset &= m_vendormap.values(vendorName).toSet(); |
|
348 else |
|
349 subset = m_vendormap.values(vendorName).toSet(); |
|
350 restrict = true; |
|
351 |
|
352 /* We still have to check versions, since we don't hash that */ |
|
353 if (implementationVersion != -1) { |
|
354 QMutableSetIterator<int> it(subset); |
|
355 while(it.hasNext()) { |
|
356 if (m_descriptors.at(it.next()).implementationVersion() != implementationVersion) |
|
357 it.remove(); |
|
358 } |
|
359 } |
|
360 } |
|
361 |
|
362 if (restrict) { |
|
363 QSetIterator<int> it(subset); |
|
364 while(it.hasNext()) { |
|
365 descriptors << m_descriptors.at(it.next()); |
|
366 } |
|
367 } else { |
|
368 /* No restrictions, just iterate over all descriptors and return all actions (!) */ |
|
369 descriptors = m_descriptors; |
|
370 } |
|
371 |
|
372 return descriptors; |
|
373 } |
|
374 |
|
375 QContactAction* QContactManagerData::action(const QContactActionDescriptor& actionDescriptor) |
|
376 { |
|
377 loadFactories(); |
|
378 QContactActionFactory* actionFactory = m_descriptormap.value(actionDescriptor, 0); |
|
379 if (actionFactory) |
|
380 return actionFactory->instance(actionDescriptor); |
|
381 return 0; |
|
382 } |
|
383 |
|
384 // trampoline for private classes |
|
385 QContactManagerEngine* QContactManagerData::engine(const QContactManager* manager) |
|
386 { |
|
387 if (manager) |
|
388 return manager->d->m_engine; |
|
389 return 0; |
|
390 } |
|
391 |
|
392 QTM_END_NAMESPACE |
|
393 |