add mp3 and ogg torrent url info to JamendoAlbum
[amarok.git] / src / devicemanager.cpp
blobbc87acb661dfc86e786d09702dac80b76c465c98
1 //
2 // C++ Implementation: devicemanager
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 // Maximilian Kossick <maximilian.kossick@googlemail.com>, (C) 2006
11 // Copyright: See COPYING file that comes with this distribution
15 #include "devicemanager.h"
17 #include "amarok.h"
18 #include "amarokconfig.h"
19 #include "debug.h"
20 #include "medium.h"
21 #include "mediumpluginmanager.h"
23 #include <KApplication>
25 #include <QTimer>
26 #include <QByteArray>
27 #include <QDBusInterface>
28 #include <QDBusReply>
31 typedef Medium::List MediumList;
32 typedef QMap<QString, Medium*>::Iterator MediumIterator;
34 DeviceManager* DeviceManager::instance()
36 static DeviceManager dw;
37 return &dw;
40 DeviceManager::DeviceManager()
42 DEBUG_BLOCK
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()
54 void
55 DeviceManager::deviceAdded( const QString &udi)
57 DEBUG_BLOCK
58 if ( !m_valid )
59 return;
60 Medium* addedMedium = getDevice(name);
61 if ( addedMedium != 0 )
62 debug() << "[DeviceManager::mediumAdded] Obtained medium name is " << name << ", id is: " << addedMedium->id();
63 else
64 debug() << "[DeviceManager::mediumAdded] Obtained medium is null; name was " << name;
65 emit mediumAdded( addedMedium, name );
69 void
70 DeviceManager::deviceRemoved( const QString &udi )
72 DEBUG_BLOCK
73 if ( !m_valid )
74 return;
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();
80 else
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
84 //has been running
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);
96 BIG FAT WARNING:
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.
107 Medium::List
108 DeviceManager::getDeviceList()
110 QStringList devices = getDeviceStringList();
111 return Medium::createList( devices );
114 QStringList
115 DeviceManager::getDeviceStringList()
117 DEBUG_BLOCK
118 MediumList currMediumList;
120 //TODO fix devicemanager, has to be ported to Solid
121 if ( true )
123 QStringList blah;
124 return blah;
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);
133 QStringList result;
134 arg << 5;
135 if (!m_dc->call("kded", "mediamanager", "fullList()", data, replyType, replyData))
137 debug() << "Error during DCOP call";
139 else
141 QDataStream reply(replyData, QIODevice::ReadOnly);
142 while(!reply.atEnd())
144 reply >> result;
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"));
151 autodetect_insert--;
152 if (autodetect_insert == -1)
153 autodetect_insert = Medium::PROPERTIES_COUNT - 1;
157 return result;*/
160 Medium*
161 DeviceManager::getDevice( const QString name )
163 DEBUG_BLOCK
164 if ( !m_valid )
165 return 0;
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 );
181 else
183 //No previous version of this Medium - create it
184 returnedMedium = new Medium( *it );
185 m_mediumMap[ name ] = returnedMedium;
187 break;
190 return returnedMedium;
193 void
194 DeviceManager::reconcileMediumMap()
196 DEBUG_BLOCK
197 if ( !m_valid )
198 return;
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 );
211 else
212 m_mediumMap[ (*it).name() ] = new Medium(*it);
215 //Sanity check
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?
223 QString device;
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";
233 } else
234 device = QString();
236 else
237 device = url;
239 return device;
242 #include "devicemanager.moc"