2 // C++ Implementation: mediadevicemanager
4 // Description: Controls device/medium object handling, providing
5 // helper functions for other objects
8 // Author: Jeff Mitchell <kde-dev@emailgoeshere.com>, (C) 2006
10 // Copyright: See COPYING file that comes with this distribution
15 #include "amarokconfig.h"
17 #include "devicemanager.h"
18 #include "mediadevicemanager.h"
21 #include <q3ptrlist.h>
24 #include <kapplication.h>
26 typedef Medium::List MediumList
;
28 MediaDeviceManager
* MediaDeviceManager::instance()
30 static MediaDeviceManager dw
;
34 MediaDeviceManager::MediaDeviceManager()
37 connect( DeviceManager::instance(), SIGNAL( mediumAdded( const Medium
*, QString
) ), SLOT( slotMediumAdded( const Medium
*, QString
) ) );
38 connect( DeviceManager::instance(), SIGNAL( mediumChanged( const Medium
*, QString
) ), SLOT( slotMediumChanged( const Medium
*, QString
) ) );
39 connect( DeviceManager::instance(), SIGNAL( mediumRemoved( const Medium
*, QString
) ), SLOT( slotMediumRemoved( const Medium
*, QString
) ) );
40 Medium::List mediums
= DeviceManager::instance()->getDeviceList();
41 oldForeachType( Medium::List
, mediums
)
43 slotMediumAdded( &(*it
), (*it
).id() );
45 if( !mediums
.count() )
47 debug() << "DeviceManager didn't return any devices, we are probably running on a non-KDE system. Trying to reinit media devices later" << endl
;
48 QTimer::singleShot( 4000, this, SLOT( reinitDevices() ) );
51 QStringList manualDevices
;
52 KSharedConfigPtr config
= Amarok::config( "MediaBrowser" );
53 QMap
<QString
,QString
> savedDevices
= config
->entryMap( "MediaBrowser" );
54 QMap
<QString
,QString
>::Iterator qit
;
55 QString curr
, currMountPoint
, currName
;
56 for( qit
= savedDevices
.begin(); qit
!= savedDevices
.end(); ++qit
)
58 //only handle manual devices, autodetected devices should be added on the fly
59 if( qit
.key().startsWith( "manual|" ) )
62 curr
= curr
.remove( "manual|" );
63 currName
= curr
.left( curr
.indexOf( '|' ) );
64 currMountPoint
= curr
.remove( currName
+ '|' );
65 manualDevices
.append( "false" ); //autodetected
66 manualDevices
.append( qit
.key() ); //id
67 manualDevices
.append( currName
); //name
68 manualDevices
.append( currName
); //label
69 manualDevices
.append( QString() ); //userLabel
70 manualDevices
.append( "unknown" ); //mountable?
71 manualDevices
.append( QString() ); //device node
72 manualDevices
.append( currMountPoint
); //mountPoint
73 manualDevices
.append( "manual" ); //fsType
74 manualDevices
.append( "unknown" ); //mounted
75 manualDevices
.append( QString() ); //baseURL
76 manualDevices
.append( QString() ); //MIMEtype
77 manualDevices
.append( QString() ); //iconName
78 manualDevices
.append( "---" ); //separator
81 Medium::List manualMediums
= Medium::createList( manualDevices
);
82 oldForeachType( Medium::List
, manualMediums
)
84 slotMediumAdded( &(*it
), (*it
).id() );
88 MediaDeviceManager::~MediaDeviceManager()
93 MediaDeviceManager::addManualDevice( Medium
* added
)
95 m_mediumMap
[added
->name()] = added
;
96 added
->setFsType( "manual" );
97 emit
mediumAdded( added
, added
->name() );
101 MediaDeviceManager::removeManualDevice( Medium
* removed
)
103 emit
mediumRemoved( removed
, removed
->name() );
104 if( m_mediumMap
.contains( removed
->name() ) )
105 m_mediumMap
.remove( removed
->name() );
108 void MediaDeviceManager::slotMediumAdded( const Medium
*m
, QString id
)
113 if ( m
->fsType() == "manual" ||
114 ( !m
->deviceNode().startsWith( "/dev/hd" ) &&
115 (m
->fsType() == "vfat" || m
->fsType() == "hfsplus" || m
->fsType() == "msdosfs" ) ) )
116 // add other fsTypes that should be auto-detected here later
118 if ( m_mediumMap
.contains( m
->name() ) )
120 Medium
*tempMedium
= m_mediumMap
[m
->name()];
121 m_mediumMap
.remove( m
->name() );
124 m_mediumMap
[m
->name()] = new Medium( m
);
125 emit
mediumAdded( m
, id
);
130 void MediaDeviceManager::slotMediumChanged( const Medium
*m
, QString id
)
133 emit
mediumChanged( m
, id
);
136 void MediaDeviceManager::slotMediumRemoved( const Medium
* , QString id
)
139 Medium
* removedMedium
= 0;
140 if ( m_mediumMap
.contains(id
) )
141 removedMedium
= m_mediumMap
[id
];
143 debug() << "[MediaDeviceManager::slotMediumRemoved] Obtained medium name is " << id
<< ", id is: " << removedMedium
->id() << endl
;
145 debug() << "[MediaDeviceManager::slotMediumRemoved] Medium was unknown and is null; name was " << id
<< endl
;
146 //if you get a null pointer from this signal, it means we did not know about the device
147 //before it was removed, i.e. the removal was the first event for the device received while amarok
149 //There is no point in calling getDevice, since it will not be in the list anyways
150 emit
mediumRemoved( removedMedium
, id
);
151 if ( m_mediumMap
.contains(id
) )
152 m_mediumMap
.remove(id
);
153 delete removedMedium
;
156 Medium
* MediaDeviceManager::getDevice( QString name
)
158 return DeviceManager::instance()->getDevice( name
);
161 void MediaDeviceManager::reinitDevices( )
163 Medium::List mediums
= DeviceManager::instance()->getDeviceList();
164 oldForeachType( Medium::List
, mediums
)
166 slotMediumAdded( &(*it
), (*it
).id() );
170 #include "mediadevicemanager.moc"