Make PMP detection work, who knows why it didn't the other way, leave for debugging
[amarok.git] / src / MediaDeviceCache.cpp
blobcd99b79a548ed33b0f050caf346e8563eef8da29
1 /*
2 * Copyright (c) 2007 Jeff Mitchell <kde-dev@emailgoeshere.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #define DEBUG_PREFIX "MediaDeviceCache"
21 #include <KConfig>
22 #include <solid/device.h>
23 #include <solid/deviceinterface.h>
24 #include <solid/devicenotifier.h>
25 #include <solid/portablemediaplayer.h>
27 #include <QList>
29 #include "amarok.h"
30 #include "debug.h"
31 #include "MediaDeviceCache.h"
34 MediaDeviceCache* MediaDeviceCache::s_instance = 0;
36 MediaDeviceCache::MediaDeviceCache() : QObject()
37 , m_type()
38 , m_name()
40 DEBUG_BLOCK
41 s_instance = this;
42 connect( Solid::DeviceNotifier::instance(), SIGNAL( deviceAdded( const QString & ) ),
43 this, SLOT( addSolidDevice( const QString & ) ) );
44 connect( Solid::DeviceNotifier::instance(), SIGNAL( deviceRemoved( const QString & ) ),
45 this, SLOT( removeSolidDevice( const QString & ) ) );
48 MediaDeviceCache::~MediaDeviceCache()
50 s_instance = 0;
53 void
54 MediaDeviceCache::refreshCache()
56 DEBUG_BLOCK
57 m_type.clear();
58 m_name.clear();
59 QList<Solid::Device> deviceList = Solid::Device::listFromType( Solid::DeviceInterface::PortableMediaPlayer );
60 Solid::Device temp;
61 foreach( Solid::Device device, deviceList )
63 debug() << "Found Solid::DeviceInterface::PortableMediaPlayer with udi = " << device.udi();
64 debug() << "Device name is = " << device.product() << " and was made by " << device.vendor();
65 m_type[device.udi()] = MediaDeviceCache::SolidType;
66 m_name[device.udi()] = device.vendor() + " - " + device.product();
68 KConfigGroup config = Amarok::config( "PortableDevices" );
69 QMap<QString, QString> manualDevices = config.entryMap();
70 foreach( QString udi, manualDevices.keys() )
72 if( udi.startsWith( "manual" ) )
74 debug() << "Found manual device with udi = " << udi;
75 m_type[udi] = MediaDeviceCache::ManualType;
76 m_name[udi] = udi.split( '|' )[2];
81 void
82 MediaDeviceCache::addSolidDevice( const QString &udi )
84 DEBUG_BLOCK
85 Solid::Device device( udi );
86 debug() << "Found new Solid device with udi = " << device.udi();
87 debug() << "Device name is = " << device.product() << " and was made by " << device.vendor();
88 Solid::PortableMediaPlayer* pmp = dynamic_cast<Solid::PortableMediaPlayer*>( device.asDeviceInterface( Solid::DeviceInterface::PortableMediaPlayer ) );
89 if( m_type.contains( udi ) )
91 debug() << "Duplicate UDI trying to be added: " << udi;
92 return;
94 // if( !device.isDeviceInterface( Solid::DeviceInterface::PortableMediaPlayer ) );
95 if( !pmp )
97 debug() << "udi " << udi << " does not describe a portable media player";
98 return;
100 m_type[udi] = MediaDeviceCache::SolidType;
101 m_name[udi] = device.vendor() + " - " + device.product();
102 emit deviceAdded( udi );
105 void
106 MediaDeviceCache::removeSolidDevice( const QString &udi )
108 DEBUG_BLOCK
109 debug() << "udi is: " << udi;
110 if( m_type.contains( udi ) )
112 m_type.remove( udi );
113 m_name.remove( udi );
114 emit deviceRemoved( udi );
115 return;
117 debug() << "Odd, got a deviceRemoved at udi " << udi << " but it didn't seem to exist in the first place...";
120 MediaDeviceCache::DeviceType
121 MediaDeviceCache::deviceType( const QString &udi )
123 DEBUG_BLOCK
124 if( m_type.contains( udi ) )
126 return m_type[udi];
128 return MediaDeviceCache::InvalidType;
131 QString
132 MediaDeviceCache::deviceName( const QString &udi )
134 DEBUG_BLOCK
135 if( m_name.contains( udi ) )
137 return m_name[udi];
139 return "ERR_NO_NAME"; //Should never happen!
142 #include "MediaDeviceCache.moc"