Make plasma libs build.
[amarok.git] / src / MediaDeviceCache.cpp
blobde4f8121683e93c39e42203c0957263a6a22f7e5
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>
26 #include <solid/storageaccess.h>
27 #include <solid/storagedrive.h>
28 #include <solid/storagevolume.h>
30 #include <QList>
32 #include "amarok.h"
33 #include "debug.h"
34 #include "MediaDeviceCache.h"
37 MediaDeviceCache* MediaDeviceCache::s_instance = 0;
39 MediaDeviceCache::MediaDeviceCache() : QObject()
40 , m_type()
41 , m_name()
42 , m_volumes()
44 DEBUG_BLOCK
45 s_instance = this;
46 connect( Solid::DeviceNotifier::instance(), SIGNAL( deviceAdded( const QString & ) ),
47 this, SLOT( slotAddSolidDevice( const QString & ) ) );
48 connect( Solid::DeviceNotifier::instance(), SIGNAL( deviceRemoved( const QString & ) ),
49 this, SLOT( slotRemoveSolidDevice( const QString & ) ) );
52 MediaDeviceCache::~MediaDeviceCache()
54 s_instance = 0;
57 void
58 MediaDeviceCache::refreshCache()
60 DEBUG_BLOCK
61 m_type.clear();
62 m_name.clear();
63 QList<Solid::Device> deviceList = Solid::Device::listFromType( Solid::DeviceInterface::PortableMediaPlayer );
64 Solid::Device temp;
65 foreach( Solid::Device device, deviceList )
67 if( device.as<Solid::StorageDrive>() )
69 debug() << "Found Solid PMP that is also a StorageDrive, skipping";
70 continue;
72 debug() << "Found Solid::DeviceInterface::PortableMediaPlayer with udi = " << device.udi();
73 debug() << "Device name is = " << device.product() << " and was made by " << device.vendor();
74 m_type[device.udi()] = MediaDeviceCache::SolidPMPType;
75 m_name[device.udi()] = device.vendor() + " - " + device.product();
77 deviceList = Solid::Device::listFromType( Solid::DeviceInterface::StorageAccess );
78 foreach( Solid::Device device, deviceList )
80 debug() << "Found Solid::DeviceInterface::StorageAccess with udi = " << device.udi();
81 debug() << "Device name is = " << device.product() << " and was made by " << device.vendor();
82 Solid::StorageAccess* ssa = device.as<Solid::StorageAccess>();
83 if( ssa )
85 if( !m_volumes.contains( device.udi() ) )
87 connect( ssa, SIGNAL( accessibilityChanged(bool, const QString&) ),
88 this, SLOT( slotAccessibilityChanged(bool, const QString&) ) );
89 m_volumes.append( device.udi() );
91 if( ssa->isAccessible() )
93 m_type[device.udi()] = MediaDeviceCache::SolidVolumeType;
94 m_name[device.udi()] = device.parent().vendor() + " - " + device.parent().product();
96 else
98 debug() << "Solid device is not accessible, will wait until it is to consider it added.";
102 KConfigGroup config = Amarok::config( "PortableDevices" );
103 QMap<QString, QString> manualDevices = config.entryMap();
104 foreach( const QString &udi, manualDevices.keys() )
106 if( udi.startsWith( "manual" ) )
108 debug() << "Found manual device with udi = " << udi;
109 m_type[udi] = MediaDeviceCache::ManualType;
110 m_name[udi] = udi.split( '|' )[1];
115 void
116 MediaDeviceCache::slotAddSolidDevice( const QString &udi )
118 DEBUG_BLOCK
119 Solid::Device device( udi );
120 debug() << "Found new Solid device with udi = " << device.udi();
121 debug() << "Device name is = " << device.product() << " and was made by " << device.vendor();
122 if( m_type.contains( udi ) )
124 debug() << "Duplicate UDI trying to be added: " << udi;
125 return;
127 if( device.as<Solid::StorageDrive>() )
129 debug() << "Storage drive found, will wait for the volume";
130 return;
132 else if( device.as<Solid::StorageAccess>() )
134 debug() << "volume is generic storage";
135 if( !m_volumes.contains( device.udi() ) )
137 connect( device.as<Solid::StorageAccess>(), SIGNAL( accessibilityChanged(bool, const QString&) ),
138 this, SLOT( slotAccessibilityChanged(bool, const QString&) ) );
139 m_volumes.append( device.udi() );
141 if( device.as<Solid::StorageAccess>()->isAccessible() )
143 m_type[udi] = MediaDeviceCache::SolidVolumeType;
144 m_name[udi] = device.parent().vendor() + " - " + device.parent().product();
146 else
148 debug() << "storage volume is not accessible right now, not adding.";
149 return;
152 else if( device.as<Solid::PortableMediaPlayer>() )
154 debug() << "device is a PMP";
155 m_type[udi] = MediaDeviceCache::SolidPMPType;
156 m_name[udi] = device.vendor() + " - " + device.product();
158 else
160 debug() << "udi " << udi << " does not describe a portable media player or storage volume";
161 return;
163 emit deviceAdded( udi );
166 void
167 MediaDeviceCache::slotRemoveSolidDevice( const QString &udi )
169 DEBUG_BLOCK
170 debug() << "udi is: " << udi;
171 Solid::Device device( udi );
172 if( m_volumes.contains( udi ) )
174 disconnect( device.as<Solid::StorageAccess>(), SIGNAL( accessibilityChanged(bool, const QString&) ),
175 this, SLOT( slotAccessibilityChanged(bool, const QString&) ) );
176 m_volumes.removeAll( udi );
178 if( m_type.contains( udi ) )
180 m_type.remove( udi );
181 m_name.remove( udi );
182 emit deviceRemoved( udi );
183 return;
185 debug() << "Odd, got a deviceRemoved at udi " << udi << " but it didn't seem to exist in the first place...";
188 void
189 MediaDeviceCache::slotAccessibilityChanged( bool accessible, const QString &udi )
191 DEBUG_BLOCK
192 debug() << "accessibility of device " << udi << " has changed to accessible = " << (accessible ? "true":"false");
193 if( accessible )
195 Solid::Device device( udi );
196 m_type[udi] = MediaDeviceCache::SolidVolumeType;
197 m_name[udi] = device.parent().vendor() + " - " + device.parent().product();
198 emit deviceAdded( udi );
199 return;
201 else
203 if( m_type.contains( udi ) )
205 m_type.remove( udi );
206 m_name.remove( udi );
207 emit deviceRemoved( udi );
208 return;
210 debug() << "Got accessibility changed to false but wasn't there in the first place...";
214 const MediaDeviceCache::DeviceType
215 MediaDeviceCache::deviceType( const QString &udi ) const
217 DEBUG_BLOCK
218 if( m_type.contains( udi ) )
220 return m_type[udi];
222 return MediaDeviceCache::InvalidType;
225 const QString
226 MediaDeviceCache::deviceName( const QString &udi ) const
228 DEBUG_BLOCK
229 if( m_name.contains( udi ) )
231 return m_name[udi];
233 return "ERR_NO_NAME"; //Should never happen!
236 bool
237 MediaDeviceCache::isGenericEnabled( const QString &udi ) const
239 DEBUG_BLOCK
240 if( m_type[udi] != MediaDeviceCache::SolidVolumeType )
242 debug() << "Not SolidVolumeType, returning false";
243 return false;
245 Solid::Device device( udi );
246 Solid::StorageAccess* ssa = device.as<Solid::StorageAccess>();
247 if( !ssa || !ssa->isAccessible() )
249 debug() << "Not able to convert to StorageAccess or not accessible, returning false";
250 return false;
252 if( device.parent().as<Solid::PortableMediaPlayer>() )
254 debug() << "Could convert parent to PortableMediaPlayer, returning true";
255 return true;
257 KIO::UDSEntry udsentry;
258 if( !KIO::NetAccess::stat( ssa->filePath() + "/.is_audio_player", udsentry, Amarok::mainWindow() ) )
260 debug() << "Did not stat .is_audio_player successfully, returning false";
261 return false;
263 debug() << "Returning true";
264 return true;
267 const QString
268 MediaDeviceCache::volumeMountPoint( const QString &udi ) const
270 DEBUG_BLOCK
271 Solid::Device device( udi );
272 Solid::StorageAccess* ssa = device.as<Solid::StorageAccess>();
273 if( !ssa || !ssa->isAccessible() )
275 debug() << "Not able to convert to StorageAccess or not accessible, returning empty";
276 return QString();
278 return ssa->filePath();
281 #include "MediaDeviceCache.moc"