1/***************************************************************************
2 * Copyright (C) 2009 by Ben Cooksley <ben@eclipse.endoftheinternet.org> *
3 * *
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. *
8 * *
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. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
18 ***************************************************************************/
19
20#include "SolidActionData.h"
21
22#include <QList>
23#include <QMetaProperty>
24
25#include <KGlobal>
26#include <kdesktopfileactions.h>
27#include <KStandardDirs>
28#include <KStringHandler>
29#include <KDesktopFile>
30#include <KConfigGroup>
31
32#include <Solid/AcAdapter>
33#include <Solid/AudioInterface>
34#include <Solid/Battery>
35#include <Solid/Block>
36#include <Solid/Button>
37#include <Solid/Camera>
38#include <Solid/DvbInterface>
39#include <Solid/GenericInterface>
40#include <Solid/NetworkInterface>
41#include <Solid/PortableMediaPlayer>
42#include <Solid/Processor>
43#include <Solid/StorageAccess>
44#include <Solid/StorageDrive>
45#include <Solid/OpticalDrive>
46#include <Solid/StorageVolume>
47#include <Solid/OpticalDisc>
48#include <solid/video.h>
49#include <solid/serialinterface.h>
50#include <solid/smartcardreader.h>
51
52static SolidActionData * actData = 0;
53
54SolidActionData::SolidActionData(bool includeFiles)
55{
56 QStringList allPossibleDevices;
57 int propertyOffset = Solid::DeviceInterface::staticMetaObject.propertyOffset();
58
59 QList<QMetaObject> interfaceList = fillInterfaceList();
60 foreach( const QMetaObject &interface, interfaceList ) {
61 QString ifaceName = interface.className();
62 ifaceName.remove(0, ifaceName.lastIndexOf(':') + 1);
63 Solid::DeviceInterface::Type ifaceDev = Solid::DeviceInterface::stringToType( ifaceName );
64 QString cleanName = Solid::DeviceInterface::typeDescription( ifaceDev );
65 types.insert( ifaceDev, cleanName );
66
67 QMap<QString, QString> deviceValues;
68 for( int doneProps = propertyOffset; interface.propertyCount() > doneProps; doneProps = doneProps + 1 ) {
69 QMetaProperty ifaceProp = interface.property(doneProps);
70 deviceValues.insert( ifaceProp.name(), generateUserString(ifaceProp.name()) );
71 }
72 values.insert( ifaceDev, deviceValues );
73 }
74
75 if( includeFiles ) {
76 // Fill the lists of possible device types / device values
77 allPossibleDevices = KGlobal::dirs()->findAllResources("data", "solid/devices/");
78 // List all the known device actions, then add their name and all values to the appropriate lists
79 foreach( const QString &desktop, allPossibleDevices ) {
80 KDesktopFile deviceFile(desktop);
81 KConfigGroup deviceType = deviceFile.desktopGroup(); // Retrieve the configuration group where the user friendly name is
82
83 QString ifaceName = deviceType.readEntry("X-KDE-Solid-Actions-Type");
84 Solid::DeviceInterface::Type ifaceDev = Solid::DeviceInterface::stringToType( ifaceName );
85 QString cleanName = Solid::DeviceInterface::typeDescription( ifaceDev );
86
87 types.insert( ifaceDev, cleanName ); // Read the user friendly name
88
89 QMap<QString,QString> deviceValues = values.value( ifaceDev );
90 foreach( const QString &text, deviceFile.readActions() ) { // We want every single action
91 KConfigGroup actionType = deviceFile.actionGroup( text );
92 deviceValues.insert( text, actionType.readEntry("Name") ); // Add to the type - actions map
93 }
94 values.insert( ifaceDev, deviceValues );
95 }
96 }
97}
98
99QList<QString> SolidActionData::propertyList( Solid::DeviceInterface::Type devInterface )
100{
101 return values.value( devInterface ).values();
102}
103
104QList<QString> SolidActionData::propertyInternalList( Solid::DeviceInterface::Type devInterface )
105{
106 return values.value( devInterface ).keys();
107}
108
109QString SolidActionData::propertyInternal( Solid::DeviceInterface::Type devInterface, QString property )
110{
111 return values.value( devInterface ).key( property );
112}
113
114QString SolidActionData::propertyName( Solid::DeviceInterface::Type devInterface, QString property )
115{
116 return values.value( devInterface ).value( property );
117}
118
119int SolidActionData::propertyPosition( Solid::DeviceInterface::Type devInterface, QString property )
120{
121 return values.value( devInterface ).keys().indexOf( property );
122}
123
124QList<QString> SolidActionData::interfaceList()
125{
126 return types.values();
127}
128
129QList<Solid::DeviceInterface::Type> SolidActionData::interfaceTypeList()
130{
131 return types.keys();
132}
133
134Solid::DeviceInterface::Type SolidActionData::interfaceFromName( const QString& name )
135{
136 return types.key( name );
137}
138
139QString SolidActionData::nameFromInterface( Solid::DeviceInterface::Type devInterface )
140{
141 return types.value( devInterface );
142}
143
144int SolidActionData::interfacePosition( Solid::DeviceInterface::Type devInterface )
145{
146 return types.keys().indexOf( devInterface );
147}
148
149QString SolidActionData::generateUserString( QString className )
150{
151 QString finalString;
152 QRegExp camelCase("([A-Z])"); // Create the split regexp
153
154 finalString = className.remove(0, className.lastIndexOf(':') + 1); // Remove any Class information
155 finalString = finalString.replace( camelCase, " \\1" ); // Use Camel Casing to add spaces
156 finalString = KStringHandler::capwords( finalString ); // Captialise everything
157 return finalString.trimmed();
158}
159
160SolidActionData * SolidActionData::instance()
161{
162 if( actData == 0 ) {
163 actData = new SolidActionData( true );
164 }
165 return actData;
166}
167
168QList<QMetaObject> SolidActionData::fillInterfaceList()
169{
170 QList<QMetaObject> interfaces;
171 interfaces.append( Solid::AcAdapter::staticMetaObject );
172 interfaces.append( Solid::AudioInterface::staticMetaObject );
173 interfaces.append( Solid::Battery::staticMetaObject );
174 interfaces.append( Solid::Block::staticMetaObject );
175 interfaces.append( Solid::Button::staticMetaObject );
176 interfaces.append( Solid::Camera::staticMetaObject );
177 interfaces.append( Solid::DvbInterface::staticMetaObject );
178 interfaces.append( Solid::NetworkInterface::staticMetaObject );
179 interfaces.append( Solid::PortableMediaPlayer::staticMetaObject );
180 interfaces.append( Solid::Processor::staticMetaObject );
181 interfaces.append( Solid::SerialInterface::staticMetaObject );
182 interfaces.append( Solid::StorageAccess::staticMetaObject );
183 interfaces.append( Solid::StorageDrive::staticMetaObject );
184 interfaces.append( Solid::OpticalDrive::staticMetaObject );
185 interfaces.append( Solid::StorageVolume::staticMetaObject );
186 interfaces.append( Solid::OpticalDisc::staticMetaObject );
187 interfaces.append( Solid::Video::staticMetaObject );
188 interfaces.append( Solid::SmartCardReader::staticMetaObject );
189 return interfaces;
190}
191
192#include "SolidActionData.moc"
193