mountpointmanager.cpp

来自「Amarok是一款在LINUX或其他类UNIX操作系统中运行的音频播放器软件。 」· C++ 代码 · 共 605 行 · 第 1/2 页

CPP
605
字号
    }    else    {        m_handlerMapMutex.lock();        foreachType( HandlerMap, m_handlerMap )        {            if ( it.data()->deviceIsMedium( m ) )            {                delete it.data();                int key = it.key();                m_handlerMap.erase( key );                debug() << "removed device " << key << endl;                m_handlerMapMutex.unlock();                emit mediumRemoved( key );                //we found the medium which was removed, so we can abort the loop                return;            }        }        m_handlerMapMutex.unlock();    }}voidMountPointManager::mediumRemoved( const Medium *m ){    DEBUG_BLOCK    if ( !m )    {        //reinit?    }    else    {        //this works for USB devices, special cases might be required for other devices        m_handlerMapMutex.lock();        foreachType( HandlerMap, m_handlerMap )        {            if ( it.data()->deviceIsMedium( m ) )            {                delete it.data();                int key = it.key();                m_handlerMap.erase( key );                debug() << "removed device " << key << endl;                m_handlerMapMutex.unlock();                emit mediumRemoved( key );                //we found the medium which was removed, so we can abort the loop                return;            }        }        m_handlerMapMutex.unlock();    }}voidMountPointManager::mediumAdded( const Medium *m ){    DEBUG_BLOCK    if ( !m ) return;    if ( m->isMounted() )    {        debug() << "Device added and mounted, checking handlers" << endl;        foreachType( FactoryList, m_mediumFactories )        {            if ( (*it)->canHandle ( m ) )            {                debug() << "found handler for " << m->id() << endl;                DeviceHandler *handler = (*it)->createHandler( m );                if( !handler )                {                    debug() << "Factory " << (*it)->type() << "could not create device handler" << endl;                    break;                }                int key = handler->getDeviceID();                m_handlerMapMutex.lock();                if ( m_handlerMap.contains( key ) )                {                    debug() << "Key " << key << " already exists in handlerMap, replacing" << endl;                    delete m_handlerMap[key];                    m_handlerMap.erase( key );                }                m_handlerMap.insert( key, handler );                m_handlerMapMutex.unlock();                debug() << "added device " << key << " with mount point " << m->mountPoint() << endl;                emit mediumConnected( key );                break;  //we found the added medium and don't have to check the other device handlers            }        }    }}IdListMountPointManager::getMountedDeviceIds() const {    m_handlerMapMutex.lock();    IdList list( m_handlerMap.keys() );    m_handlerMapMutex.unlock();    list.append( -1 );    return list;}QStringListMountPointManager::collectionFolders( ){    //TODO max: cache data    QStringList result;    KConfig* const folders = Amarok::config( "Collection Folders" );    IdList ids = getMountedDeviceIds();    foreachType( IdList, ids )    {        QStringList rpaths = folders->readListEntry( QString::number( *it ) );        for( QStringList::ConstIterator strIt = rpaths.begin(), end = rpaths.end(); strIt != end; ++strIt )        {            QString absPath;            if ( *strIt == "./" )            {                absPath = getMountPointForId( *it );            }            else            {                absPath = getAbsolutePath( *it, *strIt );            }            if ( !result.contains( absPath ) )                result.append( absPath );        }    }    return result;}voidMountPointManager::setCollectionFolders( const QStringList &folders ){    //TODO max: cache data    typedef QMap<int, QStringList> FolderMap;    KConfig* const folderConf = Amarok::config( "Collection Folders" );    FolderMap folderMap;    foreach( folders )    {        int id = getIdForUrl( *it );        QString rpath = getRelativePath( id, *it );        if ( folderMap.contains( id ) ) {            if ( !folderMap[id].contains( rpath ) )                folderMap[id].append( rpath );        }        else            folderMap[id] = QStringList( rpath );    }    //make sure that collection folders on devices which are not in foldermap are deleted    IdList ids = getMountedDeviceIds();    foreachType( IdList, ids )    {        if( !folderMap.contains( *it ) )        {            folderConf->deleteEntry( QString::number( *it ) );        }    }    foreachType( FolderMap, folderMap )    {        folderConf->writeEntry( QString::number( it.key() ), it.data() );    }}voidMountPointManager::migrateStatistics(){    QStringList urls = CollectionDB::instance()->query( "SELECT url FROM statistics WHERE deviceid = -2;" );    foreach( urls )    {        if ( QFile::exists( *it) )        {            int deviceid = getIdForUrl( *it );            QString rpath = getRelativePath( deviceid, *it );            QString update = QString( "UPDATE statistics SET deviceid = %1, url = '%2'" )                                      .arg( deviceid )                                      .arg( CollectionDB::instance()->escapeString( rpath ) );            update += QString( " WHERE url = '%1' AND deviceid = -2;" )                               .arg( CollectionDB::instance()->escapeString( *it ) );            CollectionDB::instance()->query( update );        }    }}voidMountPointManager::updateStatisticsURLs( bool changed ){    if ( changed )        QTimer::singleShot( 0, this, SLOT( startStatisticsUpdateJob() ) );}voidMountPointManager::startStatisticsUpdateJob(){    ThreadManager::instance()->queueJob( new UrlUpdateJob( this ) );}voidMountPointManager::handleMissingMediaManager(){    //TODO this method should activate a fallback mode which simply shows all songs and uses the    //device's last mount point to build the absolute path    m_noDeviceManager = true;    //Amarok::StatusBar::instance()->longMessage( i18n( "BlaBla" ), KDE::StatusBar::Warning );}voidMountPointManager::checkDeviceAvailability(){    //code to actively scan for devices which are not supported by KDE mediamanager should go here    //method is not actually called yet}bool UrlUpdateJob::doJob( ){    DEBUG_BLOCK    updateStatistics();    updateLabels();    return true;}void UrlUpdateJob::updateStatistics( ){    CollectionDB *collDB = CollectionDB::instance();    MountPointManager *mpm = MountPointManager::instance();    QStringList urls = collDB->query( "SELECT s.deviceid,s.url "                                      "FROM statistics AS s LEFT JOIN tags AS t ON s.deviceid = t.deviceid AND s.url = t.url "                                      "WHERE t.url IS NULL AND s.deviceid != -2;" );    debug() << "Trying to update " << urls.count() / 2 << " statistics rows" << endl;    foreach( urls )    {        int deviceid = (*it).toInt();        QString rpath = *++it;        QString realURL = mpm->getAbsolutePath( deviceid, rpath );        if( QFile::exists( realURL ) )        {            int newDeviceid = mpm->getIdForUrl( realURL );            if( newDeviceid == deviceid )                continue;            QString newRpath = mpm->getRelativePath( newDeviceid, realURL );            int statCount = collDB->query(                            QString( "SELECT COUNT( url ) FROM statistics WHERE deviceid = %1 AND url = '%2';" )                                        .arg( newDeviceid )                                        .arg( collDB->escapeString( newRpath ) ) ).first().toInt();            if( statCount )                continue;       //statistics row with new URL/deviceid values already exists            QString sql = QString( "UPDATE statistics SET deviceid = %1, url = '%2'" )                                .arg( newDeviceid ).arg( collDB->escapeString( newRpath ) );            sql += QString( " WHERE deviceid = %1 AND url = '%2';" )                                .arg( deviceid ).arg( collDB->escapeString( rpath ) );            collDB->query( sql );        }    }}void UrlUpdateJob::updateLabels( ){    CollectionDB *collDB = CollectionDB::instance();    MountPointManager *mpm = MountPointManager::instance();    QStringList labels = collDB->query( "SELECT l.deviceid,l.url "                                        "FROM tags_labels AS l LEFT JOIN tags as t ON l.deviceid = t.deviceid AND l.url = t.url "                                        "WHERE t.url IS NULL;" );    debug() << "Trying to update " << labels.count() / 2 << " tags_labels rows" << endl;    foreach( labels )    {        int deviceid = (*it).toInt();        QString rpath = *++it;        QString realUrl = mpm->getAbsolutePath( deviceid, rpath );        if( QFile::exists( realUrl ) )        {            int newDeviceid = mpm->getIdForUrl( realUrl );            if( newDeviceid == deviceid )                continue;            QString newRpath = mpm->getRelativePath( newDeviceid, realUrl );            //only update rows if there is not already a row with the new deviceid/rpath and the same labelid            QStringList labelids = collDB->query(                                         QString( "SELECT labelid FROM tags_labels WHERE deviceid = %1 AND url = '%2';" )                                                 .arg( QString::number( newDeviceid ), collDB->escapeString( newRpath ) ) );            QString existingLabelids;            if( !labelids.isEmpty() )            {                existingLabelids = " AND labelid NOT IN (";                foreach( labelids )                {                    if( it != labelids.begin() )                        existingLabelids += ',';                    existingLabelids += *it;                }                existingLabelids += ')';            }            QString sql = QString( "UPDATE tags_labels SET deviceid = %1, url = '%2' "                                    "WHERE deviceid = %3 AND url = '%4'%5;" )                                    .arg( newDeviceid )                                    .arg( collDB->escapeString( newRpath ),                                          QString::number( deviceid ),                                          collDB->escapeString( rpath ),                                          existingLabelids );            collDB->query( sql );        }    }}#include "mountpointmanager.moc"

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?