2 // C++ Implementation: devicemanager
4 // Description: Controls device/medium object handling, providing
5 // helper functions for other objects
8 // Author: Jeff Mitchell <kde-dev@emailgoeshere.com>, (C) 2006
9 // Maximilian Kossick <maximilian.kossick@googlemail.com>, (C) 2006
11 // Copyright: See COPYING file that comes with this distribution
15 #include "devicemanager.h"
18 #include "amarokconfig.h"
21 #include "mediumpluginmanager.h"
23 #include <KApplication>
27 #include <QDBusInterface>
31 typedef Medium::List MediumList
;
32 typedef QMap
<QString
, Medium
*>::Iterator MediumIterator
;
34 DeviceManager
* DeviceManager::instance()
36 static DeviceManager dw
;
40 DeviceManager::DeviceManager()
44 connect( Solid::DeviceNotifier::instance(), SIGNAL(deviceAdded(const QString
&)),
45 this, SLOT(deviceAdded(const QString
&)));
46 connect( Solid::DeviceNotifier::instance(), SIGNAL(deviceRemoved(const QString
&)),
47 this, SLOT(deviceRemoved(const QString
&)));
50 DeviceManager::~DeviceManager()
55 DeviceManager::deviceAdded( const QString
&udi
)
60 Medium
* addedMedium
= getDevice(name
);
61 if ( addedMedium
!= 0 )
62 debug() << "[DeviceManager::mediumAdded] Obtained medium name is " << name
<< ", id is: " << addedMedium
->id();
64 debug() << "[DeviceManager::mediumAdded] Obtained medium is null; name was " << name
;
65 emit
mediumAdded( addedMedium
, name
);
70 DeviceManager::deviceRemoved( const QString
&udi
)
75 Medium
* removedMedium
= 0;
76 if ( m_mediumMap
.contains(name
) )
77 removedMedium
= m_mediumMap
[name
];
78 if ( removedMedium
!= 0 )
79 debug() << "[DeviceManager::mediumRemoved] Obtained medium name is " << name
<< ", id is: " << removedMedium
->id();
81 debug() << "[DeviceManager::mediumRemoved] Medium was unknown and is null; name was " << name
;
82 //if you get a null pointer from this signal, it means we did not know about the device
83 //before it was removed, i.e. the removal was the first event for the device received while amarok
85 //There is no point in calling getDevice, since it will not be in the list anyways
86 emit
mediumRemoved( removedMedium
, name
);
87 if ( m_mediumMap
.contains(name
) )
89 delete removedMedium
; //If we are to remove it from the map, delete it first
90 m_mediumMap
.remove(name
);
97 Values returned from the below function should not be counted on being unique!
98 For instance, there may be a Medium object in the QMap that can be accessed through
99 other functions that has the same data as the Medium object returned, but is a different object.
100 As you should not be writing to this object, this is okay, however:
102 Use the Medium's name or id, not the pointer value, for equality comparison!!!
104 This function does rebuild the map every time it is called, however this should be rare enough
105 that it is not a problem.
108 DeviceManager::getDeviceList()
110 QStringList devices
= getDeviceStringList();
111 return Medium::createList( devices
);
115 DeviceManager::getDeviceStringList()
118 MediumList currMediumList
;
120 //TODO fix devicemanager, has to be ported to Solid
127 /* //normal kded Medium doesn't have autodetect, so decrease by 1
128 int autodetect_insert = Medium::PROPERTIES_COUNT - 1;
130 QByteArray data, replyData;
131 QByteArray replyType;
132 QDataStream arg(data, QIODevice::WriteOnly);
135 if (!m_dc->call("kded", "mediamanager", "fullList()", data, replyType, replyData))
137 debug() << "Error during DCOP call";
141 QDataStream reply(replyData, QIODevice::ReadOnly);
142 while(!reply.atEnd())
146 QStringList::Iterator it;
147 for( it = result.begin(); it != result.end(); ++it )
149 if (autodetect_insert == Medium::PROPERTIES_COUNT - 1)
150 result.insert(it, QString("true"));
152 if (autodetect_insert == -1)
153 autodetect_insert = Medium::PROPERTIES_COUNT - 1;
161 DeviceManager::getDevice( const QString name
)
166 debug() << "DeviceManager: getDevice called with name argument = " << name
;
167 Medium
* returnedMedium
= 0;
168 MediumList currMediumList
= getDeviceList();
170 for ( Medium::List::iterator it
= currMediumList
.begin(); it
!= currMediumList
.end(); ++it
)
172 if ( (*it
).name() == name
)
174 MediumIterator secIt
;
175 if ( (secIt
= m_mediumMap
.find( name
)) != m_mediumMap
.end() )
177 //Refresh the Medium by reconstructing then copying it over.
178 returnedMedium
= *secIt
;
179 *returnedMedium
= Medium( *it
);
183 //No previous version of this Medium - create it
184 returnedMedium
= new Medium( *it
);
185 m_mediumMap
[ name
] = returnedMedium
;
190 return returnedMedium
;
194 DeviceManager::reconcileMediumMap()
200 MediumList currMediumList
= getDeviceList();
202 Medium::List::iterator it
;
203 for ( it
= currMediumList
.begin(); it
!= currMediumList
.end(); ++it
)
205 MediumIterator locIt
;
206 if ( (locIt
= m_mediumMap
.find( (*it
).name() )) != m_mediumMap
.end() )
208 Medium
* mediumHolder
= (*locIt
);
209 *mediumHolder
= Medium( *it
);
212 m_mediumMap
[ (*it
).name() ] = new Medium(*it
);
216 if ( currMediumList
.size() != m_mediumMap
.size() )
217 warning() << "Number of devices does not equal expected number";
220 QString
DeviceManager::convertMediaUrlToDevice( QString url
)
222 //do we still need this?
224 if ( url
.startsWith( "media:" ) || url
.startsWith( "system:" ) )
226 KUrl
devicePath( url
);
227 QDBusInterface
mediamanager( "org.kde.kded", "/modules/mediamanager", "org.kde.MediaManager" );
228 QDBusReply
<QStringList
> reply
= mediamanager
.call( "properties",devicePath
.fileName() );
229 if ( reply
.isValid() ) {
230 QStringList properties
= reply
;
231 device
= properties
[ 5 ];
232 //kDebug() << "DeviceManager::convertMediaUrlToDevice() munged to: " << device << "\n";
242 #include "devicemanager.moc"