?? mountpointmanager.cpp
字號:
/* * Copyright (c) 2006-2007 Maximilian Kossick <maximilian.kossick@googlemail.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */#define DEBUG_PREFIX "MountPointManager"#include "debug.h"#include "amarok.h"#include "amarokconfig.h" //used in init()#include "collectiondb.h"#include "devicemanager.h"#include "mountpointmanager.h"#include "pluginmanager.h"#include "statusbar.h"#include <kglobal.h> //used in init()#include <ktrader.h>#include <qfile.h>#include <qstringlist.h>#include <qtimer.h>#include <qvaluelist.h>typedef Medium::List MediumList;MountPointManager::MountPointManager() : QObject( 0, "MountPointManager" ) , m_noDeviceManager( false ){ if ( !Amarok::config( "Collection" )->readBoolEntry( "DynamicCollection", true ) ) { debug() << "Dynamic Collection deactivated in amarokrc, not loading plugins, not connecting signals" << endl; return; } //we are only interested in the mounting or unmounting of mediums //therefore it is enough to listen to DeviceManager's mediumChanged signal if (DeviceManager::instance()->isValid() ) { connect( DeviceManager::instance(), SIGNAL( mediumAdded( const Medium*, QString ) ), SLOT( mediumAdded( const Medium* ) ) ); connect( DeviceManager::instance(), SIGNAL( mediumChanged( const Medium*, QString ) ), SLOT( mediumChanged( const Medium* ) ) ); connect( DeviceManager::instance(), SIGNAL( mediumRemoved( const Medium*, QString ) ), SLOT( mediumRemoved( const Medium* ) ) ); } else { handleMissingMediaManager(); } m_mediumFactories.setAutoDelete( true ); m_remoteFactories.setAutoDelete( true ); init(); CollectionDB *collDB = CollectionDB::instance(); if ( collDB->adminValue( "Database Stats Version" ).toInt() >= 9 && /* make sure that deviceid actually exists*/ collDB->query( "SELECT COUNT(url) FROM statistics WHERE deviceid = -2;" ).first().toInt() != 0 ) { connect( this, SIGNAL( mediumConnected( int ) ), SLOT( migrateStatistics() ) ); QTimer::singleShot( 0, this, SLOT( migrateStatistics() ) ); } connect( this, SIGNAL( mediumConnected( int ) ), SLOT( updateStatisticsURLs() ) ); updateStatisticsURLs();}MountPointManager::~MountPointManager(){ m_handlerMapMutex.lock(); foreachType( HandlerMap, m_handlerMap ) { delete it.data(); } m_handlerMapMutex.unlock();}MountPointManager * MountPointManager::instance( ){ static MountPointManager instance; return &instance;}voidMountPointManager::init(){ DEBUG_BLOCK KTrader::OfferList plugins = PluginManager::query( "[X-KDE-Amarok-plugintype] == 'device'" ); debug() << "Received [" << QString::number( plugins.count() ) << "] device plugin offers" << endl; foreachType( KTrader::OfferList, plugins ) { Amarok::Plugin *plugin = PluginManager::createFromService( *it ); if( plugin ) { DeviceHandlerFactory *factory = static_cast<DeviceHandlerFactory*>( plugin ); if ( factory->canCreateFromMedium() ) m_mediumFactories.append( factory ); else if (factory->canCreateFromConfig() ) m_remoteFactories.append( factory ); else //FIXME max: better error message debug() << "Unknown DeviceHandlerFactory" << endl; } else debug() << "Plugin could not be loaded" << endl; } //we need access to the unfiltered data MediumList list = DeviceManager::instance()->getDeviceList(); foreachType ( MediumList, list ) { mediumChanged( &(*it) ); } if( !KGlobal::config()->hasGroup( "Collection Folders" ) ) { QStringList folders = AmarokConfig::collectionFolders(); if( !folders.isEmpty() ) setCollectionFolders( folders ); }}intMountPointManager::getIdForUrl( KURL url ){ uint mountPointLength = 0; int id = -1; m_handlerMapMutex.lock(); foreachType( HandlerMap, m_handlerMap ) { if ( url.path().startsWith( it.data()->getDevicePath() ) && mountPointLength < it.data()->getDevicePath().length() ) { id = it.key(); mountPointLength = it.data()->getDevicePath().length(); } } m_handlerMapMutex.unlock(); if ( mountPointLength > 0 ) { return id; } else { //default fallback if we could not identify the mount point. //treat -1 as mount point / in al other methods return -1; }}intMountPointManager::getIdForUrl( const QString &url ){ return getIdForUrl( KURL::fromPathOrURL( url ) );}boolMountPointManager::isMounted ( const int deviceId ) const { m_handlerMapMutex.lock(); bool result = m_handlerMap.contains( deviceId ); m_handlerMapMutex.unlock(); return result;}QStringMountPointManager::getMountPointForId( const int id ) const{ QString mountPoint; if ( isMounted( id ) ) { m_handlerMapMutex.lock(); mountPoint = m_handlerMap[id]->getDevicePath(); m_handlerMapMutex.unlock(); } else //TODO better error handling mountPoint = "/"; return mountPoint;}voidMountPointManager::getAbsolutePath( const int deviceId, const KURL& relativePath, KURL& absolutePath) const{ //debug() << "id is " << deviceId << ", relative path is " << relativePath.path() << endl; if ( deviceId == -1 ) { absolutePath.setPath( "/" ); absolutePath.addPath( relativePath.path() ); absolutePath.cleanPath(); //debug() << "Deviceid is -1, using relative Path as absolute Path, returning " << absolutePath.path() << endl; return; } m_handlerMapMutex.lock(); if ( m_handlerMap.contains( deviceId ) ) { m_handlerMap[deviceId]->getURL( absolutePath, relativePath ); m_handlerMapMutex.unlock(); } else { m_handlerMapMutex.unlock(); QStringList lastMountPoint = CollectionDB::instance()->query( QString( "SELECT lastmountpoint FROM devices WHERE id = %1" ) .arg( deviceId ) ); if ( lastMountPoint.count() == 0 ) { //hmm, no device with that id in the DB...serious problem absolutePath.setPath( "/" ); absolutePath.addPath( relativePath.path() ); absolutePath.cleanPath(); warning() << "Device " << deviceId << " not in database, this should never happen! Returning " << absolutePath.path() << endl; } else { absolutePath.setPath( lastMountPoint.first() ); absolutePath.addPath( relativePath.path() ); absolutePath.cleanPath();// debug() << "Device " << deviceId << " not mounted, using last mount point and returning " << absolutePath.path() << endl; } }}QStringMountPointManager::getAbsolutePath( const int deviceId, const QString& relativePath ) const{ KURL rpath; rpath.setProtocol("file"); rpath.setPath( relativePath ); KURL url; getAbsolutePath( deviceId, rpath, url ); return url.path();}voidMountPointManager::getRelativePath( const int deviceId, const KURL& absolutePath, KURL& relativePath ) const{ m_handlerMapMutex.lock(); if ( deviceId != -1 && m_handlerMap.contains( deviceId ) ) { //FIXME max: returns garbage if the absolute path is actually not under the device's mount point QString rpath = KURL::relativePath( m_handlerMap[deviceId]->getDevicePath(), absolutePath.path() ); m_handlerMapMutex.unlock(); relativePath.setPath( rpath ); } else { m_handlerMapMutex.unlock(); //TODO: better error handling QString rpath = KURL::relativePath( "/", absolutePath.path() ); relativePath.setPath( rpath ); }}QStringMountPointManager::getRelativePath( const int deviceId, const QString& absolutePath ) const{ KURL url; getRelativePath( deviceId, KURL::fromPathOrURL( absolutePath ), url ); return url.path();}voidMountPointManager::mediumChanged( const Medium *m ){ DEBUG_BLOCK if ( !m ) return; if ( m->isMounted() ) { 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 } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -