bluetoothengine/btui/btdevsettingframework/src/btdevsettingpluginloader.cpp
branchRCL_3
changeset 56 9386f31cc85b
parent 55 613943a21004
child 61 269724087bed
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0""
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 #include <btdevsettingpluginloader.h>
       
    18 #include <QString>
       
    19 #include <QDir>
       
    20 #include <QFileInfo>
       
    21 #include <QPluginLoader>
       
    22 #include <btdevsettinginterface.h>
       
    23 #include <btdevsettingglobal.h>
       
    24 
       
    25 /*!
       
    26     \class BtDevSettingPluginLoader
       
    27     \brief The BtDevSettingPluginLoader class loads BT device setting plugins.
       
    28  */
       
    29 
       
    30 #ifdef WIN32
       
    31     #define PLUGINFILE_SUFFIX "dll"
       
    32 #else
       
    33     #define PLUGINFILE_SUFFIX "qtplugin"
       
    34 #endif
       
    35 
       
    36 
       
    37 static QStringList getDrives()
       
    38 {
       
    39     static QStringList drives;
       
    40     if (drives.empty()) {
       
    41 #ifdef WIN32
       
    42         drives.append("C:");
       
    43 #else
       
    44         QFileInfoList fileInfoList = QDir::drives();
       
    45         foreach(const QFileInfo &fileInfo,fileInfoList) {
       
    46             QString str = fileInfo.filePath();
       
    47             if (str.length() > 2) {
       
    48                 str = str.left(2);
       
    49             }
       
    50             drives.append(str);
       
    51         }
       
    52 #endif
       
    53     }
       
    54     return drives;
       
    55 }
       
    56 
       
    57 static QStringList directoriesFromAllDrives(const QString &baseDir)
       
    58 {
       
    59     QStringList dirs;
       
    60     QStringList drives = getDrives();
       
    61     foreach(const QString &drive,drives) {
       
    62         QString dir = drive + baseDir + QDir::separator();
       
    63         if (QFileInfo(dir).exists()) {
       
    64             dirs.append(dir);
       
    65         }
       
    66     }
       
    67     return dirs;
       
    68 }
       
    69 
       
    70 static BtDevSettingInterface* loadPluginInterface(const QFileInfo &pluginFile)
       
    71 {    
       
    72     QPluginLoader loader(pluginFile.absoluteFilePath());
       
    73     BtDevSettingInterface *plugin = 
       
    74             qobject_cast<BtDevSettingInterface*> (loader.instance());
       
    75     if (!plugin) {
       
    76         loader.unload();
       
    77     }
       
    78     return plugin;
       
    79 }
       
    80 
       
    81 /*!
       
    82    Load all setting interfaces.
       
    83    
       
    84    \return the list of plugin interface.
       
    85  */
       
    86 QList<BtDevSettingInterface*> BtDevSettingPluginLoader::loadDevSettingInterfaces()
       
    87 {
       
    88     QList<BtDevSettingInterface*> interfaces;
       
    89     
       
    90     static QStringList pluginDirs;
       
    91     if (pluginDirs.empty()) {
       
    92         pluginDirs = directoriesFromAllDrives(BTDEVSETTING_PLUGIN_PATH);
       
    93     }
       
    94     foreach(const QString &dirStr,pluginDirs) {
       
    95         QDir pluginDir( dirStr );
       
    96         QFileInfoList fileInfoList = pluginDir.entryInfoList( QDir::Files );
       
    97         foreach(const QFileInfo &fileInfo,fileInfoList) {
       
    98             int diff = fileInfo.suffix().compare(PLUGINFILE_SUFFIX,Qt::CaseInsensitive);
       
    99             if (!diff) {
       
   100                 BtDevSettingInterface* settingIf = loadPluginInterface(fileInfo);
       
   101                 if ( settingIf ) {
       
   102                     interfaces.append( settingIf );
       
   103                 }
       
   104             }
       
   105         }
       
   106     }
       
   107     
       
   108     return interfaces;
       
   109 }
       
   110