Revert previous commit, was incorrect
[amarok.git] / src / mediadevicemanager.cpp
blob9d2c26695a209076a514aa87d3f7b1e56dcdd026
1 //
2 // C++ Implementation: mediadevicemanager
3 //
4 // Description: Controls device/medium object handling, providing
5 // helper functions for other objects
6 //
7 //
8 // Author: Jeff Mitchell <kde-dev@emailgoeshere.com>, (C) 2006
9 //
10 // Copyright: See COPYING file that comes with this distribution
14 #include "mediadevicemanager.h"
16 #include "amarok.h"
17 #include "amarokconfig.h"
18 #include "debug.h"
19 #include "devicemanager.h"
20 #include "medium.h"
22 #include <KApplication>
24 #include <q3ptrlist.h>
25 #include <QTimer>
27 typedef Medium::List MediumList;
29 MediaDeviceManager* MediaDeviceManager::instance()
31 static MediaDeviceManager dw;
32 return &dw;
35 MediaDeviceManager::MediaDeviceManager()
37 DEBUG_BLOCK
38 connect( DeviceManager::instance(), SIGNAL( mediumAdded( const Medium*, QString ) ), SLOT( slotMediumAdded( const Medium*, QString ) ) );
39 connect( DeviceManager::instance(), SIGNAL( mediumChanged( const Medium*, QString ) ), SLOT( slotMediumChanged( const Medium*, QString ) ) );
40 connect( DeviceManager::instance(), SIGNAL( mediumRemoved( const Medium*, QString ) ), SLOT( slotMediumRemoved( const Medium*, QString ) ) );
41 Medium::List mediums = DeviceManager::instance()->getDeviceList();
42 oldForeachType( Medium::List, mediums )
44 slotMediumAdded( &(*it), (*it).id() );
46 if( !mediums.count() )
48 debug() << "DeviceManager didn't return any devices, we are probably running on a non-KDE system. Trying to reinit media devices later";
49 QTimer::singleShot( 4000, this, SLOT( reinitDevices() ) );
51 //load manual devices
52 QStringList manualDevices;
53 KConfigGroup config = Amarok::config( "MediaBrowser" );
54 QMap<QString,QString> savedDevices = config.entryMap();
55 QMap<QString,QString>::Iterator qit;
56 QString curr, currMountPoint, currName;
57 for( qit = savedDevices.begin(); qit != savedDevices.end(); ++qit )
59 //only handle manual devices, autodetected devices should be added on the fly
60 if( qit.key().startsWith( "manual|" ) )
62 curr = qit.key();
63 curr = curr.remove( "manual|" );
64 currName = curr.left( curr.indexOf( '|' ) );
65 currMountPoint = curr.remove( currName + '|' );
66 manualDevices.append( "false" ); //autodetected
67 manualDevices.append( qit.key() ); //id
68 manualDevices.append( currName ); //name
69 manualDevices.append( currName ); //label
70 manualDevices.append( QString() ); //userLabel
71 manualDevices.append( "unknown" ); //mountable?
72 manualDevices.append( QString() ); //device node
73 manualDevices.append( currMountPoint ); //mountPoint
74 manualDevices.append( "manual" ); //fsType
75 manualDevices.append( "unknown" ); //mounted
76 manualDevices.append( QString() ); //baseURL
77 manualDevices.append( QString() ); //MIMEtype
78 manualDevices.append( QString() ); //iconName
79 manualDevices.append( "---" ); //separator
82 Medium::List manualMediums = Medium::createList( manualDevices );
83 oldForeachType( Medium::List, manualMediums )
85 slotMediumAdded( &(*it), (*it).id() );
89 MediaDeviceManager::~MediaDeviceManager()
93 void
94 MediaDeviceManager::addManualDevice( Medium* added )
96 m_mediumMap[added->name()] = added;
97 added->setFsType( "manual" );
98 emit mediumAdded( added, added->name() );
101 void
102 MediaDeviceManager::removeManualDevice( Medium* removed )
104 emit mediumRemoved( removed, removed->name() );
105 if( m_mediumMap.contains( removed->name() ) )
106 m_mediumMap.remove( removed->name() );
109 void MediaDeviceManager::slotMediumAdded( const Medium *m, QString id)
111 DEBUG_BLOCK
112 if ( m )
114 if ( m->fsType() == "manual" ||
115 ( !m->deviceNode().startsWith( "/dev/hd" ) &&
116 (m->fsType() == "vfat" || m->fsType() == "hfsplus" || m->fsType() == "msdosfs" ) ) )
117 // add other fsTypes that should be auto-detected here later
119 if ( m_mediumMap.contains( m->name() ) )
121 Medium *tempMedium = m_mediumMap[m->name()];
122 m_mediumMap.remove( m->name() );
123 delete tempMedium;
125 m_mediumMap[m->name()] = new Medium( m );
126 emit mediumAdded( m, id );
131 void MediaDeviceManager::slotMediumChanged( const Medium *m, QString id )
133 //nothing to do here
134 emit mediumChanged( m, id);
137 void MediaDeviceManager::slotMediumRemoved( const Medium* , QString id )
139 DEBUG_BLOCK
140 Medium* removedMedium = 0;
141 if ( m_mediumMap.contains(id) )
142 removedMedium = m_mediumMap[id];
143 if ( removedMedium )
144 debug() << "[MediaDeviceManager::slotMediumRemoved] Obtained medium name is " << id << ", id is: " << removedMedium->id();
145 else
146 debug() << "[MediaDeviceManager::slotMediumRemoved] Medium was unknown and is null; name was " << id;
147 //if you get a null pointer from this signal, it means we did not know about the device
148 //before it was removed, i.e. the removal was the first event for the device received while amarok
149 //has been running
150 //There is no point in calling getDevice, since it will not be in the list anyways
151 emit mediumRemoved( removedMedium, id );
152 if ( m_mediumMap.contains(id) )
153 m_mediumMap.remove(id);
154 delete removedMedium;
157 Medium* MediaDeviceManager::getDevice( QString name )
159 return DeviceManager::instance()->getDevice( name );
162 void MediaDeviceManager::reinitDevices( )
164 Medium::List mediums = DeviceManager::instance()->getDeviceList();
165 oldForeachType( Medium::List, mediums )
167 slotMediumAdded( &(*it), (*it).id() );
171 #include "mediadevicemanager.moc"