src/multimedia/audio/qaudiopluginloader.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     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 "qaudiosystemplugin.h"
       
    43 #include "qaudiopluginloader_p.h"
       
    44 
       
    45 #include <QtCore/qcoreapplication.h>
       
    46 #include <QtGui/qapplication.h>
       
    47 #include <QtCore/qpluginloader.h>
       
    48 #include <QtCore/qfactoryinterface.h>
       
    49 #include <QtCore/qdir.h>
       
    50 #include <QtCore/qdebug.h>
       
    51 
       
    52 QT_BEGIN_NAMESPACE
       
    53 
       
    54 QAudioPluginLoader::QAudioPluginLoader(const char *iid, const QString &location, Qt::CaseSensitivity):
       
    55     m_iid(iid)
       
    56 {
       
    57     m_location = location + "/";
       
    58     load();
       
    59 }
       
    60 
       
    61 QAudioPluginLoader::~QAudioPluginLoader()
       
    62 {
       
    63     for (int i = 0; i < m_plugins.count(); i++ ) {
       
    64         delete m_plugins.at(i);
       
    65     }
       
    66 }
       
    67 
       
    68 QStringList QAudioPluginLoader::pluginList() const
       
    69 {
       
    70 #if !defined QT_NO_DEBUG
       
    71     const bool showDebug = qgetenv("QT_DEBUG_PLUGINS").toInt() > 0;
       
    72 #endif
       
    73 
       
    74     QStringList paths = QApplication::libraryPaths();
       
    75 #ifdef QTM_PLUGIN_PATH
       
    76     paths << QLatin1String(QTM_PLUGIN_PATH);
       
    77 #endif
       
    78 #if !defined QT_NO_DEBUG
       
    79     if (showDebug)
       
    80         qDebug() << "Plugin paths:" << paths;
       
    81 #endif
       
    82 
       
    83     //temp variable to avoid multiple identic path
       
    84     QSet<QString> processed;
       
    85 
       
    86     /* Discover a bunch o plugins */
       
    87     QStringList plugins;
       
    88 
       
    89     /* Enumerate our plugin paths */
       
    90     for (int i=0; i < paths.count(); i++) {
       
    91         if (processed.contains(paths.at(i)))
       
    92             continue;
       
    93         processed.insert(paths.at(i));
       
    94         QDir pluginsDir(paths.at(i)+m_location);
       
    95         if (!pluginsDir.exists())
       
    96             continue;
       
    97 
       
    98         QStringList files = pluginsDir.entryList(QDir::Files);
       
    99 #if !defined QT_NO_DEBUG
       
   100         if (showDebug)
       
   101             qDebug()<<"Looking for plugins in "<<pluginsDir.path()<<files;
       
   102 #endif
       
   103         for (int j=0; j < files.count(); j++) {
       
   104             plugins <<  pluginsDir.absoluteFilePath(files.at(j));
       
   105         }
       
   106     }
       
   107     return  plugins;
       
   108 }
       
   109 
       
   110 QStringList QAudioPluginLoader::keys() const
       
   111 {
       
   112     QMutexLocker(m_mutex());
       
   113 
       
   114     QStringList list;
       
   115     for (int i = 0; i < m_plugins.count(); i++) {
       
   116         QAudioSystemPlugin* p = qobject_cast<QAudioSystemPlugin*>(m_plugins.at(i)->instance());
       
   117         if (p) list << p->keys();
       
   118     }
       
   119 
       
   120     return list;
       
   121 }
       
   122 
       
   123 QObject* QAudioPluginLoader::instance(QString const &key)
       
   124 {
       
   125     QMutexLocker(mutex());
       
   126 
       
   127     for (int i = 0; i < m_plugins.count(); i++) {
       
   128         QAudioSystemPlugin* p = qobject_cast<QAudioSystemPlugin*>(m_plugins.at(i)->instance());
       
   129         if (p && p->keys().contains(key))
       
   130             return m_plugins.at(i)->instance();
       
   131     }
       
   132     return 0;
       
   133 }
       
   134 
       
   135 QList<QObject*> QAudioPluginLoader::instances(QString const &key)
       
   136 {
       
   137     QMutexLocker(mutex());
       
   138 
       
   139     QList<QObject*> list;
       
   140     for (int i = 0; i < m_plugins.count(); i++) {
       
   141         QAudioSystemPlugin* p = qobject_cast<QAudioSystemPlugin*>(m_plugins.at(i)->instance());
       
   142         if (p && p->keys().contains(key))
       
   143             list << m_plugins.at(i)->instance();
       
   144     }
       
   145     return list;
       
   146 }
       
   147 
       
   148 void QAudioPluginLoader::load()
       
   149 {
       
   150     if (!m_plugins.isEmpty())
       
   151         return;
       
   152 
       
   153     QStringList plugins = pluginList();
       
   154     for (int i=0; i < plugins.count(); i++) {
       
   155         QPluginLoader* loader = new QPluginLoader(plugins.at(i));
       
   156         QObject *o = loader->instance();
       
   157         if (o != 0 && o->qt_metacast(m_iid) != 0)
       
   158             m_plugins.append(loader);
       
   159         else {
       
   160             qWarning() << "QAudioPluginLoader: Failed to load plugin: "
       
   161                 << plugins.at(i) << loader->errorString();
       
   162         }
       
   163     }
       
   164 }
       
   165 QT_END_NAMESPACE
       
   166