src/versit/qversitcontactpluginloader_p.cpp
changeset 5 603d3f8b6302
equal deleted inserted replaced
3:e4ebb16b39ea 5:603d3f8b6302
       
     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 <QPluginLoader>
       
    43 
       
    44 #include "qversitcontactpluginloader_p.h"
       
    45 #include "qmobilitypluginsearch.h"
       
    46 
       
    47 QTM_USE_NAMESPACE
       
    48 
       
    49 /*!
       
    50    A less-than function for factory indices (see QVersitContactHandlerFactory::index()).
       
    51    Positive values come first (ascendingly), then zero, then negative values (ascendingly).
       
    52  */
       
    53 bool factoryLessThan(QVersitContactHandlerFactory* a, QVersitContactHandlerFactory* b) {
       
    54     if ((a->index() > 0 && b->index() > 0)
       
    55             || (a->index() < 0 && b->index() < 0))
       
    56         // same sign
       
    57         return a->index() < b->index();
       
    58     else
       
    59         // a is zero
       
    60         // or b is zero
       
    61         // or opposite sign
       
    62         return b->index() < a->index();
       
    63 }
       
    64 
       
    65 QVersitContactPluginLoader* QVersitContactPluginLoader::mInstance = NULL;
       
    66 
       
    67 /*!
       
    68  * \class QVersitContactPluginLoader
       
    69  * This is a singleton class that loads Versit plugins for contacts processing
       
    70  */
       
    71 
       
    72 QVersitContactPluginLoader::QVersitContactPluginLoader()
       
    73 {
       
    74 }
       
    75 
       
    76 /*!
       
    77  * Returns the singleton instance of the QVersitContactPluginLoader.
       
    78  */
       
    79 QVersitContactPluginLoader* QVersitContactPluginLoader::instance()
       
    80 {
       
    81     if (!mInstance)
       
    82         mInstance = new QVersitContactPluginLoader;
       
    83     return mInstance;
       
    84 }
       
    85 
       
    86 void QVersitContactPluginLoader::loadPlugins() {
       
    87     QStringList plugins = mobilityPlugins(QLatin1String("versit"));
       
    88     if (plugins != mPluginPaths) {
       
    89         mPluginPaths = plugins;
       
    90 
       
    91         foreach (const QString& pluginPath, mPluginPaths) {
       
    92             QPluginLoader qpl(pluginPath);
       
    93             QObject* plugin = qpl.instance();
       
    94             QVersitContactHandlerFactory* contactPlugin =
       
    95                 qobject_cast<QVersitContactHandlerFactory*>(plugin);
       
    96             if (contactPlugin && !mLoadedFactories.contains(contactPlugin->name())) {
       
    97                 mLoadedFactories.insert(contactPlugin->name());
       
    98                 mContactHandlerFactories.append(contactPlugin);
       
    99             }
       
   100         }
       
   101         qSort(mContactHandlerFactories.begin(), mContactHandlerFactories.end(), factoryLessThan);
       
   102     }
       
   103 }
       
   104 
       
   105 /*!
       
   106  * Creates and returns handlers from the plugin.  If \a profile is the empty string, only handlers
       
   107  * with an empty profile list are returned.  If \a profile is nonempty, only handlers with either
       
   108  * an empty profile list or a profile list that contains the given \a profile are returned.
       
   109  *
       
   110  * The caller is responsible for deleting all returned handlers.
       
   111  */
       
   112 QList<QVersitContactHandler*> QVersitContactPluginLoader::createContactHandlers(const QString& profile)
       
   113 {
       
   114     loadPlugins();
       
   115 
       
   116     QList<QVersitContactHandler*> handlers;
       
   117     foreach (const QVersitContactHandlerFactory* factory, mContactHandlerFactories) {
       
   118         if (factory->profiles().isEmpty() ||
       
   119                 (!profile.isEmpty() && factory->profiles().contains(profile))) {
       
   120             QVersitContactHandler* handler = factory->createHandler();
       
   121             handlers.append(handler);
       
   122         }
       
   123     }
       
   124     return handlers;
       
   125 }