qtmobility/src/contacts/qcontactmanager_p.cpp
changeset 5 453da2cfceef
parent 4 90517678cc4f
child 8 71781823f776
equal deleted inserted replaced
4:90517678cc4f 5:453da2cfceef
    37 **
    37 **
    38 ** $QT_END_LICENSE$
    38 ** $QT_END_LICENSE$
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 
    42 #include <src/global/qbuildcfg.h>
    43 #include "qcontactmanager.h"
    43 #include "qcontactmanager.h"
    44 #include "qcontactmanager_p.h"
    44 #include "qcontactmanager_p.h"
    45 #include "qcontactmanagerengine.h"
    45 #include "qcontactmanagerengine.h"
    46 #include "qcontactmanagerenginefactory.h"
    46 #include "qcontactmanagerenginefactory.h"
    47 
    47 
    48 #include "qcontact_p.h"
    48 #include "qcontact_p.h"
    49 
    49 
    50 #include "qcontactaction.h"
       
    51 #include "qcontactactiondescriptor.h"
       
    52 #include "qcontactactionfactory.h"
       
    53 
       
    54 #include <QSharedData>
    50 #include <QSharedData>
    55 #include <QtPlugin>
    51 #include <QtPlugin>
    56 #include <QPluginLoader>
    52 #include <QPluginLoader>
    57 
    53 
    58 #include <QDebug>
    54 #include <QDebug>
    69 #include "qcontactinvalidbackend_p.h"
    65 #include "qcontactinvalidbackend_p.h"
    70 
    66 
    71 QTM_BEGIN_NAMESPACE
    67 QTM_BEGIN_NAMESPACE
    72 
    68 
    73 /* Shared QContactManager stuff here, default engine stuff below */
    69 /* 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;
    70 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 
    71 
    81 bool QContactManagerData::m_discovered;
    72 bool QContactManagerData::m_discovered;
    82 bool QContactManagerData::m_discoveredStatic;
    73 bool QContactManagerData::m_discoveredStatic;
    83 QStringList QContactManagerData::m_pluginPaths;
    74 QStringList QContactManagerData::m_pluginPaths;
    84 
    75 
    85 static void qContactsCleanEngines()
    76 static void qContactsCleanEngines()
    86 {
    77 {
    87     QContactManagerData::m_discovered = false;
    78     QContactManagerData::m_discovered = false;
    88     QList<QContactManagerEngineFactory*> factories = QContactManagerData::m_engines.values();
    79     QList<QContactManagerEngineFactory*> factories = QContactManagerData::m_engines.values();
    89     QList<QContactActionFactory*> actionfactories = QContactManagerData::m_actionfactories;
       
    90 
       
    91     for (int i=0; i < factories.count(); i++) {
    80     for (int i=0; i < factories.count(); i++) {
    92         delete factories.at(i);
    81         delete factories.at(i);
    93     }
    82     }
    94     for(int i=0; i < actionfactories.count(); i++) {
       
    95         delete actionfactories.at(i);
       
    96     }
       
    97     QContactManagerData::m_engines.clear();
    83     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 }
    84 }
   104 
    85 
   105 
    86 
   106 static int parameterValue(const QMap<QString, QString>& parameters, const char* key, int defaultValue)
    87 static int parameterValue(const QMap<QString, QString>& parameters, const char* key, int defaultValue)
   107 {
    88 {
   187 
   168 
   188         /* Loop over all the static plugins */
   169         /* Loop over all the static plugins */
   189         QObjectList staticPlugins = QPluginLoader::staticInstances();
   170         QObjectList staticPlugins = QPluginLoader::staticInstances();
   190         for (int i=0; i < staticPlugins.count(); i++ ){
   171         for (int i=0; i < staticPlugins.count(); i++ ){
   191             QContactManagerEngineFactory *f = qobject_cast<QContactManagerEngineFactory*>(staticPlugins.at(i));
   172             QContactManagerEngineFactory *f = qobject_cast<QContactManagerEngineFactory*>(staticPlugins.at(i));
   192             QContactActionFactory *g = qobject_cast<QContactActionFactory*>(staticPlugins.at(i));
       
   193             if (f) {
   173             if (f) {
   194                 QString name = f->managerName();
   174                 QString name = f->managerName();
   195 #if !defined QT_NO_DEBUG
   175 #if !defined QT_NO_DEBUG
   196                 if (showDebug)
   176                 if (showDebug)
   197                     qDebug() << "Static: found an engine plugin" << f << "with name" << name;
   177                     qDebug() << "Static: found an engine plugin" << f << "with name" << name;
   203                     } else {
   183                     } else {
   204                         m_engines.insertMulti(name, f);
   184                         m_engines.insertMulti(name, f);
   205                     }
   185                     }
   206                 } else {
   186                 } else {
   207                     qWarning() << "Static contacts plugin with reserved name" << name << "ignored";
   187                     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                 }
   188                 }
   232             }
   189             }
   233         }
   190         }
   234     }
   191     }
   235 }
   192 }
   311 
   268 
   312         QStringList paths;
   269         QStringList paths;
   313         QSet<QString> processed;
   270         QSet<QString> processed;
   314 
   271 
   315         paths << QApplication::applicationDirPath() << QApplication::libraryPaths();
   272         paths << QApplication::applicationDirPath() << QApplication::libraryPaths();
       
   273         QString val = qt_mobility_configure_prefix_path_str;
       
   274         if(val.length() > 0){
       
   275             paths << val;
       
   276         }
   316 #if !defined QT_NO_DEBUG
   277 #if !defined QT_NO_DEBUG
   317         if (showDebug)
   278         if (showDebug)
   318             qDebug() << "Plugin paths:" << paths;
   279             qDebug() << "Plugin paths:" << paths;
   319 #endif
   280 #endif
   320 
   281 
   360 
   321 
   361         /* Now discover the dynamic plugins */
   322         /* Now discover the dynamic plugins */
   362         for (int i=0; i < plugins.count(); i++) {
   323         for (int i=0; i < plugins.count(); i++) {
   363             QPluginLoader qpl(plugins.at(i));
   324             QPluginLoader qpl(plugins.at(i));
   364             QContactManagerEngineFactory *f = qobject_cast<QContactManagerEngineFactory*>(qpl.instance());
   325             QContactManagerEngineFactory *f = qobject_cast<QContactManagerEngineFactory*>(qpl.instance());
   365             QContactActionFactory *g = qobject_cast<QContactActionFactory*>(qpl.instance());
       
   366 
       
   367             if (f) {
   326             if (f) {
   368                 QString name = f->managerName();
   327                 QString name = f->managerName();
   369 #if !defined QT_NO_DEBUG
   328 #if !defined QT_NO_DEBUG
   370                 if (showDebug)
   329                 if (showDebug)
   371                     qDebug() << "Dynamic: found a contact engine plugin" << f << "with name" << name;
   330                     qDebug() << "Dynamic: found a contact engine plugin" << f << "with name" << name;
   380                 } else {
   339                 } else {
   381                     qWarning() << "Contacts plugin" << plugins.at(i) << "with reserved name" << name << "ignored";
   340                     qWarning() << "Contacts plugin" << plugins.at(i) << "with reserved name" << name << "ignored";
   382                 }
   341                 }
   383             }
   342             }
   384 
   343 
   385             if (g) {
       
   386                 QString name = g->name();
       
   387 #if !defined QT_NO_DEBUG
       
   388                 if (showDebug)
       
   389                     qDebug() << "Dynamic: found a contact action factory" << g << "with name" << name;
       
   390 #endif
       
   391                 // we also need to ensure that we haven't already loaded this factory.
       
   392                 if (m_actionfactories.contains(g)) {
       
   393                     qWarning() << "Contacts plugin" << plugins.at(i) << "has the same name as currently loaded plugin" << name << "; ignored";
       
   394                 } else {
       
   395                     m_actionfactories.append(g);
       
   396 
       
   397                     QList<QContactActionDescriptor> actions = g->actionDescriptors();
       
   398                     QMap<QContactActionDescriptor, QContactActionFactory*>::iterator it;
       
   399                     for (int j = 0; j < actions.size(); j++) {
       
   400                         const QContactActionDescriptor& desc = actions.at(j);
       
   401                         m_descriptormap.insert(desc, g);
       
   402                         m_descriptors.append(desc);
       
   403                         m_actionmap.insertMulti(desc.actionName(), m_descriptors.count() - 1);
       
   404                         m_vendormap.insertMulti(desc.vendorName(), m_descriptors.count() - 1);
       
   405                     }
       
   406                 }
       
   407             }
       
   408 
       
   409             /* Debugging */
   344             /* Debugging */
   410 #if !defined QT_NO_DEBUG
   345 #if !defined QT_NO_DEBUG
   411             if (showDebug && !f && !g) {
   346             if (showDebug && !f) {
   412                 qDebug() << "Unknown plugin:" << qpl.errorString();
   347                 qDebug() << "Unknown plugin:" << qpl.errorString();
   413                 if (qpl.instance()) {
   348                 if (qpl.instance()) {
   414                     qDebug() << "[qobject:" << qpl.instance() << "]";
   349                     qDebug() << "[qobject:" << qpl.instance() << "]";
   415                 }
   350                 }
   416             }
   351             }
   426             engineNames << QString::fromAscii("%1[%2]").arg(f->managerName()).arg(versions.join(QString::fromAscii(",")));
   361             engineNames << QString::fromAscii("%1[%2]").arg(f->managerName()).arg(versions.join(QString::fromAscii(",")));
   427         }
   362         }
   428 #if !defined QT_NO_DEBUG
   363 #if !defined QT_NO_DEBUG
   429         if (showDebug) {
   364         if (showDebug) {
   430             qDebug() << "Found engines:" << engineNames;
   365             qDebug() << "Found engines:" << engineNames;
   431             qDebug() << "Found actions:" << m_actionmap.keys();
   366         }
   432         }
   367 #endif
   433 #endif
   368     }
   434     }
       
   435 }
       
   436 
       
   437 QList<QContactActionDescriptor> QContactManagerData::actionDescriptors(const QString& actionName, const QString& vendorName, int implementationVersion)
       
   438 {
       
   439     loadFactories();
       
   440 
       
   441     bool restrict = false;
       
   442     QSet<int> subset;
       
   443     QList<QContactActionDescriptor> descriptors;
       
   444 
       
   445     // Go through our list of descriptors, looking for a match
       
   446     if (!actionName.isEmpty()) {
       
   447         subset = m_actionmap.values(actionName).toSet();
       
   448         restrict = true;
       
   449     }
       
   450 
       
   451     if (!vendorName.isEmpty()) {
       
   452         if (restrict)
       
   453             subset &= m_vendormap.values(vendorName).toSet();
       
   454         else
       
   455             subset = m_vendormap.values(vendorName).toSet();
       
   456         restrict = true;
       
   457 
       
   458         /* We still have to check versions, since we don't hash that */
       
   459         if (implementationVersion != -1) {
       
   460             QMutableSetIterator<int> it(subset);
       
   461             while(it.hasNext()) {
       
   462                 if (m_descriptors.at(it.next()).implementationVersion() != implementationVersion)
       
   463                     it.remove();
       
   464             }
       
   465         }
       
   466     }
       
   467 
       
   468     if (restrict) {
       
   469         QSetIterator<int> it(subset);
       
   470         while(it.hasNext()) {
       
   471             descriptors << m_descriptors.at(it.next());
       
   472         }
       
   473     } else {
       
   474         /* No restrictions, just iterate over all descriptors and return all actions (!) */
       
   475         descriptors = m_descriptors;
       
   476     }
       
   477 
       
   478     return descriptors;
       
   479 }
       
   480 
       
   481 QContactAction* QContactManagerData::action(const QContactActionDescriptor& actionDescriptor)
       
   482 {
       
   483     loadFactories();
       
   484     QContactActionFactory* actionFactory = m_descriptormap.value(actionDescriptor, 0);
       
   485     if (actionFactory)
       
   486         return actionFactory->instance(actionDescriptor);
       
   487     return 0;
       
   488 }
   369 }
   489 
   370 
   490 // trampoline for private classes
   371 // trampoline for private classes
   491 QContactManagerEngine* QContactManagerData::engine(const QContactManager* manager)
   372 QContactManagerEngine* QContactManagerData::engine(const QContactManager* manager)
   492 {
   373 {