qtmobility/src/contacts/qcontactmanager_p.cpp
changeset 15 1f895d8a5b2b
parent 11 06b8e2af4411
equal deleted inserted replaced
14:6fbed849b4f4 15:1f895d8a5b2b
    44 #include "qcontactmanagerengine.h"
    44 #include "qcontactmanagerengine.h"
    45 #include "qcontactmanagerenginefactory.h"
    45 #include "qcontactmanagerenginefactory.h"
    46 
    46 
    47 #include "qcontact_p.h"
    47 #include "qcontact_p.h"
    48 
    48 
       
    49 #include "qcontactaction.h"
       
    50 #include "qcontactactiondescriptor.h"
       
    51 #include "qcontactactionfactory.h"
       
    52 
    49 #include <QSharedData>
    53 #include <QSharedData>
    50 #include <QtPlugin>
    54 #include <QtPlugin>
    51 #include <QPluginLoader>
    55 #include <QPluginLoader>
    52 
    56 
    53 #include <QDebug>
    57 #include <QDebug>
    65 #include "qmobilitypluginsearch.h"
    69 #include "qmobilitypluginsearch.h"
    66 
    70 
    67 QTM_BEGIN_NAMESPACE
    71 QTM_BEGIN_NAMESPACE
    68 
    72 
    69 /* Shared QContactManager stuff here, default engine stuff below */
    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;
    70 QHash<QString, QContactManagerEngineFactory*> QContactManagerData::m_engines;
    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;
    71 
    80 
    72 bool QContactManagerData::m_discovered;
    81 bool QContactManagerData::m_discovered;
    73 bool QContactManagerData::m_discoveredStatic;
    82 bool QContactManagerData::m_discoveredStatic;
    74 QStringList QContactManagerData::m_pluginPaths;
    83 QStringList QContactManagerData::m_pluginPaths;
    75 
    84 
    76 static void qContactsCleanEngines()
    85 static void qContactsCleanEngines()
    77 {
    86 {
    78     QContactManagerData::m_discovered = false;
    87     QContactManagerData::m_discovered = false;
    79     QList<QContactManagerEngineFactory*> factories = QContactManagerData::m_engines.values();
    88     QList<QContactManagerEngineFactory*> factories = QContactManagerData::m_engines.values();
       
    89     QList<QContactActionFactory*> actionfactories = QContactManagerData::m_actionfactories;
       
    90 
    80     for (int i=0; i < factories.count(); i++) {
    91     for (int i=0; i < factories.count(); i++) {
    81         delete factories.at(i);
    92         delete factories.at(i);
    82     }
    93     }
       
    94     for(int i=0; i < actionfactories.count(); i++) {
       
    95         delete actionfactories.at(i);
       
    96     }
    83     QContactManagerData::m_engines.clear();
    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();
    84 }
   103 }
    85 
   104 
    86 
   105 
    87 static int parameterValue(const QMap<QString, QString>& parameters, const char* key, int defaultValue)
   106 static int parameterValue(const QMap<QString, QString>& parameters, const char* key, int defaultValue)
    88 {
   107 {
   168 
   187 
   169         /* Loop over all the static plugins */
   188         /* Loop over all the static plugins */
   170         QObjectList staticPlugins = QPluginLoader::staticInstances();
   189         QObjectList staticPlugins = QPluginLoader::staticInstances();
   171         for (int i=0; i < staticPlugins.count(); i++ ){
   190         for (int i=0; i < staticPlugins.count(); i++ ){
   172             QContactManagerEngineFactory *f = qobject_cast<QContactManagerEngineFactory*>(staticPlugins.at(i));
   191             QContactManagerEngineFactory *f = qobject_cast<QContactManagerEngineFactory*>(staticPlugins.at(i));
       
   192             QContactActionFactory *g = qobject_cast<QContactActionFactory*>(staticPlugins.at(i));
   173             if (f) {
   193             if (f) {
   174                 QString name = f->managerName();
   194                 QString name = f->managerName();
   175 #if !defined QT_NO_DEBUG
   195 #if !defined QT_NO_DEBUG
   176                 if (showDebug)
   196                 if (showDebug)
   177                     qDebug() << "Static: found an engine plugin" << f << "with name" << name;
   197                     qDebug() << "Static: found an engine plugin" << f << "with name" << name;
   185                     }
   205                     }
   186                 } else {
   206                 } else {
   187                     qWarning() << "Static contacts plugin with reserved name" << name << "ignored";
   207                     qWarning() << "Static contacts plugin with reserved name" << name << "ignored";
   188                 }
   208                 }
   189             }
   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             }
   190         }
   233         }
   191     }
   234     }
   192 }
   235 }
   193 
   236 
   194 
   237 
   211 
   254 
   212         /* Now discover the dynamic plugins */
   255         /* Now discover the dynamic plugins */
   213         for (int i=0; i < m_pluginPaths.count(); i++) {
   256         for (int i=0; i < m_pluginPaths.count(); i++) {
   214             QPluginLoader qpl(m_pluginPaths.at(i));
   257             QPluginLoader qpl(m_pluginPaths.at(i));
   215             QContactManagerEngineFactory *f = qobject_cast<QContactManagerEngineFactory*>(qpl.instance());
   258             QContactManagerEngineFactory *f = qobject_cast<QContactManagerEngineFactory*>(qpl.instance());
       
   259             QContactActionFactory *g = qobject_cast<QContactActionFactory*>(qpl.instance());
       
   260 
   216             if (f) {
   261             if (f) {
   217                 QString name = f->managerName();
   262                 QString name = f->managerName();
   218 #if !defined QT_NO_DEBUG
   263 #if !defined QT_NO_DEBUG
   219                 if (showDebug)
   264                 if (showDebug)
   220                     qDebug() << "Dynamic: found a contact engine plugin" << f << "with name" << name;
   265                     qDebug() << "Dynamic: found a contact engine plugin" << f << "with name" << name;
   229                 } else {
   274                 } else {
   230                     qWarning() << "Contacts plugin" << m_pluginPaths.at(i) << "with reserved name" << name << "ignored";
   275                     qWarning() << "Contacts plugin" << m_pluginPaths.at(i) << "with reserved name" << name << "ignored";
   231                 }
   276                 }
   232             }
   277             }
   233 
   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 
   234             /* Debugging */
   303             /* Debugging */
   235 #if !defined QT_NO_DEBUG
   304 #if !defined QT_NO_DEBUG
   236             if (showDebug && !f) {
   305             if (showDebug && !f && !g) {
   237                 qDebug() << "Unknown plugin:" << qpl.errorString();
   306                 qDebug() << "Unknown plugin:" << qpl.errorString();
   238                 if (qpl.instance()) {
   307                 if (qpl.instance()) {
   239                     qDebug() << "[qobject:" << qpl.instance() << "]";
   308                     qDebug() << "[qobject:" << qpl.instance() << "]";
   240                 }
   309                 }
   241             }
   310             }
   251             engineNames << QString::fromAscii("%1[%2]").arg(f->managerName()).arg(versions.join(QString::fromAscii(",")));
   320             engineNames << QString::fromAscii("%1[%2]").arg(f->managerName()).arg(versions.join(QString::fromAscii(",")));
   252         }
   321         }
   253 #if !defined QT_NO_DEBUG
   322 #if !defined QT_NO_DEBUG
   254         if (showDebug) {
   323         if (showDebug) {
   255             qDebug() << "Found engines:" << engineNames;
   324             qDebug() << "Found engines:" << engineNames;
   256         }
   325             qDebug() << "Found actions:" << m_actionmap.keys();
   257 #endif
   326         }
   258     }
   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;
   259 }
   382 }
   260 
   383 
   261 // trampoline for private classes
   384 // trampoline for private classes
   262 QContactManagerEngine* QContactManagerData::engine(const QContactManager* manager)
   385 QContactManagerEngine* QContactManagerData::engine(const QContactManager* manager)
   263 {
   386 {