bool PluginFactory::loaded( const PluginInfoPair& pair )
{
    PluginInfo *pluginInfo = pair.first;
    if( !pluginInfo ) return false;

    return pluginInfo->get();
}
void PluginFactory::destroyPlugin( const PluginInfoPair& pair )
{
    PluginInfo *pluginInfo = pair.first;
    Plugin *plugin = pluginInfo->get();
    if( !plugin ) return;

    PTRACE( 6, "Exiting plugin... " << pluginInfo->name );
    plugin->exit();

    //remove plugin from the lists
    Plugin::Type type = pluginInfo->type;
    PluginList& list = pluginMap[ type ];
    list.removeRef( plugin );
    idMap.remove(pluginInfo->id);

    PTRACE( 6, "Destroying plugin" );
    delete plugin;
}
PluginInfoList& PluginFactory::rescan()
{
    //remove plugins that are not loaded

    PluginInfoList::Iterator it = infoList.begin();
    while( it != infoList.end() ) {
        PluginInfo *info = (*it).first;
        if( !info ) continue;

        //plugin is not loaded
        if( !info->get() ) {
            QLibrary *lib = (*it).second;
            lib->unload();
            delete lib;
            it = infoList.remove(it);
            continue;
        }
        ++it;
    }

    //load all plugins (and avoid collisions)

    QRegExp libRxp = QRegExp( "^lib.*\\.so[0-9.]*$" );
    //QRegExp libRxp = QRegExp( "^lib.*" );

    PluginInfoList pluginList;

    QStringList libraryPaths = ancaConf->readListEntry( LIBRARY_PATHS, LIBRARY_PATHS_DEFAULT );
#ifdef PREL
    libraryPaths.append( PREL "/share/anca/plugins");
#endif
    libraryPaths.append(QDir::homeDirPath() + "/.anca/plugins");

    //iterate all paths where plugins should be
    for ( QStringList::iterator it = libraryPaths.begin(); it != libraryPaths.end(); ++it ) {
        PTRACE( 3, "Searching directory " << ( *it ).latin1() << " for plugins" );
        QDir dir( *it );
        if ( !dir.exists() ) continue;

        dir.setFilter( QDir::Files | QDir::NoSymLinks );

        const QFileInfoList *fileList = dir.entryInfoList();
        QFileInfo *file;
        QLibrary *lib;
        //iterate all files in the directory
        for ( QFileInfoListIterator fit( *fileList ); ( file = *fit ); ++fit ) {
            //is the file plugin?
            if ( !file->fileName().contains( libRxp ) ) continue;

            //open plugin
            lib = new QLibrary( file->filePath().latin1() );

            if( !lib->load() ) {
                PTRACE( 3, "Library " << file->fileName().latin1() << " could not be loaded" );
                continue;
            }

            //resolve symbol 'getPluginInfo'
            if ( GetPluginInfo * getPluginInfo = ( GetPluginInfo* ) lib->resolve( "getPluginInfo" ) ) {
                PluginInfo * pluginInfo = getPluginInfo();

                pluginList.append( PluginInfoPair(pluginInfo, lib) );
            } else {
                PTRACE( 3, "Symbol \"getPluginInfo\" not found in library " << file->fileName().latin1() );
                delete lib;
            }
        }
    }

    //insert that pluginInfo to class infoList that in not already there
    for( PluginInfoList::iterator it = pluginList.begin(); it != pluginList.end(); ++it ) {
        QLibrary *lib = (*it).second;
        if( !lib ) continue;

        if( !infoList.contains( *it ) )
            infoList.append( *it );
        else {
            lib->unload();
            delete lib;
        }
    }

    // sort plugins according to type
    qHeapSort(infoList);

    return infoList;
}