1 #include "DeviceConfigurator.h"
2 #include "DeviceDataStorage.h"
3 #include "DeviceConstants.h"
8 DeviceConfigurator::DeviceConfigurator( DeviceDataStorage *aDataStorage )
10 qDebug() << "DeviceConfigurator( DeviceDataStorage * )";
11 iDataStorage = aDataStorage;
14 DeviceConfigurator::~DeviceConfigurator()
16 qDebug() << "~DeviceConfigurator()";
19 bool DeviceConfigurator::toggleScreenSwitchOff( bool aEnable )
21 qDebug() << "DeviceConfigurator::toggleScreenSwitchOff( bool )";
22 QString command = "gconftool-2";
26 QStringList defParams;
27 confs << "/system/osso/dsm/display/display_blank_timeout"
28 << "/system/osso/dsm/display/display_dim_timeout"
29 << "/apps/osso/applet/osso-applet-display/turn_off_display"
30 << "/apps/osso/applet/osso-applet-display/brightness_period";
31 defParams << "300" << "120" << "300" << "120";
32 QStringList origValues;
36 //disabling the screen "auto-switch-off" and "dimming"
38 //using gconftool-2 to get the current values for related configurations
39 for ( int i = 0; i < confs.size(); ++i )
42 args << "-g" << confs[i];
43 if ( systemIO( command, args, result ) )
45 if ( result.toLong() != 0 )
46 origValues.append( result );
49 if ( origValues.size() == confs.size() ) //values succesfully fetched, now trying to store them
51 if ( !iDataStorage->storeData( iDataStorage->dataSectionToString( DeviceDataStorage::ScreenSettings ), origValues ) )
52 emit configuringError( DeviceManager::ScreenSettingsNotStored );
54 else //values not fetched, using the default values instead
56 emit configuringError( DeviceManager::ScreenSettingsNotFetched );
58 for ( int i = 0; i < defParams.size(); ++i )
59 origValues.append( defParams.at( i ) );
62 //using gconftool-2 to change the related configurations
63 for ( int i = 0; i < confs.size(); ++i )
66 args << "-s" << confs[i] << "--type=int" << "6000000";
67 if ( !systemIO( command, args, result ) ) {
68 emit configuringError( DeviceManager::ScreenSettingsNotChanged );
75 //setting the screen's "auto-switch-off" and "dimming" settings back as they were
77 //reading stored data from internal config file
78 if ( !iDataStorage->readData( iDataStorage->dataSectionToString( DeviceDataStorage::ScreenSettings ), origValues ) )
80 //cannot read, using the default values instead
81 emit configuringError( DeviceManager::ScreenSettingsNotFetched );
82 for ( int i = 0; i < defParams.size(); ++i )
83 origValues.append( defParams.at( i ) );
85 for ( int i = 0; i < origValues.size(); ++i )
88 args << "-s" << confs[i] << "--type=int" << origValues.at(i);
89 if ( !systemIO( command, args, result ) ) {
90 emit configuringError( DeviceManager::ScreenSettingsNotChanged );
98 bool DeviceConfigurator::toggleHWKeys( bool aEnable )
100 qDebug() << "DeviceConfigurator::toggleHWKeys( bool )";
101 QStringList mceLines;
102 QStringList mceLinesNew;
103 QString mceSection = "HomeKey";
105 params << "HomeKeyShortAction" << "HomeKeyLongAction";
107 // using the DeviceDataStorage exceptionally for reading data from an external conf file
109 if ( !iDataStorage->readData( mceSection, mceLines, McePath ) )
111 emit configuringError( DeviceManager::KeySettingsNotFetched );
117 // disabling the "home"-hw-key
119 QStringList mceLinesToStore;
120 for ( int i = 0; i < mceLines.size(); ++i )
122 QStringList mceLine = mceLines.at( i ).split( '=' );
123 QString param = mceLine.at( 0 ).trimmed();
124 //check if this is the correct parameter to store and change
125 if ( params.contains( param ) )
127 for ( int j = 0; j < params.size(); ++j )
129 if ( params.at( j ) == param )
131 mceLinesToStore.append( mceLines.at( i ) );
132 mceLinesNew.append( param + "=disabled" );
138 mceLinesNew.append( mceLines.at( i ) );
141 // storing the mce conf file lines
142 if ( !iDataStorage->storeData( iDataStorage->dataSectionToString( DeviceDataStorage::KeySettings ), mceLinesToStore ) )
144 emit configuringError( DeviceManager::KeySettingsNotStored );
150 // setting the "home"-hw-key settings back as they were
152 // reading the stored mce conf file lines
153 QStringList storedMceLines;
154 if ( !iDataStorage->readData( iDataStorage->dataSectionToString( DeviceDataStorage::KeySettings ), storedMceLines ) )
156 emit configuringError( DeviceManager::KeySettingsNotFetched );
160 bool paramFound = false;
161 for ( int i = 0; i < mceLines.size(); ++i )
163 QStringList mceLine = mceLines.at( i ).split( '=' );
164 for ( int j = 0; j < storedMceLines.size(); ++j )
166 QStringList storedMceLine = storedMceLines.at( j ).split( '=' );
167 if ( storedMceLine.at( 0 ).trimmed() == mceLine.at( 0 ).trimmed() )
169 mceLinesNew.append( storedMceLines.at( j ) );
174 mceLinesNew.append( mceLines.at( i ) );
179 // using the datastorage exceptionally but this time for changing data in the external conf file
180 if ( !iDataStorage->storeData( mceSection, mceLinesNew, McePath ) )
182 emit configuringError( DeviceManager::KeySettingsNotChanged );
189 bool DeviceConfigurator::toggleInitScript( bool aEnable )
192 if( !whoAmI( name ) ) {
193 emit configuringError( DeviceManager::InitScriptNotChanged );
197 QString command = InitScript;
202 command.prepend( "sudo " );
205 command.append( " install" );
207 command.append( " remove" );
209 if ( !systemIO( command, empty, result ) ) {
210 emit configuringError( DeviceManager::InitScriptNotChanged );
217 bool DeviceConfigurator::restartDevice()
219 QString command = BinPath + DevStopper;
222 args.append( "restart" );
223 if( !systemIO( command, args, result ) ) {
224 emit configuringError( DeviceManager::DeviceNotRestarted );
230 bool DeviceConfigurator::whoAmI( QByteArray &aName )
232 QString command = "whoami";
234 if( !systemIO( command, empty, aName ) )
239 bool DeviceConfigurator::systemIO( const QString &aCommand, const QStringList &aArgs, QByteArray &aResult )
241 qDebug() << "DeviceConfigurator::systemIO( QString &, QStringList &, QByteArray &)";
242 qDebug() << "Command: " << aCommand;
245 process.start( aCommand, aArgs );
247 process.start( aCommand );
249 if( !process.waitForFinished() )
251 aResult = process.readAll();
252 if( aResult.endsWith( '\n' ) )
255 qDebug() << "Result: " << aResult;