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 emit configuringError( DeviceManager::ScreenSettingsNotChanged );
24 QString command = "gconftool-2";
28 QStringList defParams;
29 confs << "/system/osso/dsm/display/display_blank_timeout"
30 << "/system/osso/dsm/display/display_dim_timeout"
31 << "/apps/osso/applet/osso-applet-display/turn_off_display"
32 << "/apps/osso/applet/osso-applet-display/brightness_period";
33 defParams << "300" << "120" << "300" << "120";
34 QStringList origValues;
38 //disabling the screen "auto-switch-off" and "dimming"
40 //using gconftool-2 to get the current values for related configurations
41 for ( int i = 0; i < confs.size(); ++i )
44 args << "-g" << confs[i];
45 if ( systemIO( command, args, result ) )
47 if ( result.toLong() != 0 )
48 origValues.append( result );
51 if ( origValues.size() == confs.size() ) //values succesfully fetched, now trying to store them
53 if ( !iDataStorage->storeData( iDataStorage->dataSectionToString( DeviceDataStorage::ScreenSettings ), origValues ) )
54 emit configuringError( DeviceManager::ScreenSettingsNotStored );
56 else //values not fetched, using the default values instead
58 emit configuringError( DeviceManager::ScreenSettingsNotFetched );
60 for ( int i = 0; i < defParams.size(); ++i )
61 origValues.append( defParams.at( i ) );
64 //using gconftool-2 to change the related configurations
65 for ( int i = 0; i < confs.size(); ++i )
68 args << "-s" << confs[i] << "--type=int" << "6000000";
69 if ( !systemIO( command, args, result ) ) {
70 emit configuringError( DeviceManager::ScreenSettingsNotChanged );
77 //setting the screen's "auto-switch-off" and "dimming" settings back as they were
79 //reading stored data from internal config file
80 if ( !iDataStorage->readData( iDataStorage->dataSectionToString( DeviceDataStorage::ScreenSettings ), origValues ) )
82 //cannot read, using the default values instead
83 emit configuringError( DeviceManager::ScreenSettingsNotFetched );
84 for ( int i = 0; i < defParams.size(); ++i )
85 origValues.append( defParams.at( i ) );
87 for ( int i = 0; i < origValues.size(); ++i )
90 args << "-s" << confs[i] << "--type=int" << origValues.at(i);
91 if ( !systemIO( command, args, result ) ) {
92 emit configuringError( DeviceManager::ScreenSettingsNotChanged );
100 bool DeviceConfigurator::toggleHWKeys( bool aEnable )
102 qDebug() << "DeviceConfigurator::toggleHWKeys( bool )";
103 QStringList mceLines;
104 QStringList mceLinesNew;
105 QString mceSection = "HomeKey";
107 params << "HomeKeyShortAction" << "HomeKeyLongAction";
109 // using the DeviceDataStorage exceptionally for reading data from an external conf file
111 if ( !iDataStorage->readData( mceSection, mceLines, McePath ) )
113 emit configuringError( DeviceManager::KeySettingsNotFetched );
119 // disabling the "home"-hw-key
121 QStringList mceLinesToStore;
122 for ( int i = 0; i < mceLines.size(); ++i )
124 QStringList mceLine = mceLines.at( i ).split( '=' );
125 QString param = mceLine.at( 0 ).trimmed();
126 //check if this is the correct parameter to store and change
127 if ( params.contains( param ) )
129 for ( int j = 0; j < params.size(); ++j )
131 if ( params.at( j ) == param )
133 mceLinesToStore.append( mceLines.at( i ) );
134 mceLinesNew.append( param + "=disabled" );
140 mceLinesNew.append( mceLines.at( i ) );
143 // storing the mce conf file lines
144 if ( !iDataStorage->storeData( iDataStorage->dataSectionToString( DeviceDataStorage::KeySettings ), mceLinesToStore ) )
146 emit configuringError( DeviceManager::KeySettingsNotStored );
152 // setting the "home"-hw-key settings back as they were
154 // reading the stored mce conf file lines
155 QStringList storedMceLines;
156 if ( !iDataStorage->readData( iDataStorage->dataSectionToString( DeviceDataStorage::KeySettings ), storedMceLines ) )
158 emit configuringError( DeviceManager::KeySettingsNotFetched );
162 bool paramFound = false;
163 for ( int i = 0; i < mceLines.size(); ++i )
165 QStringList mceLine = mceLines.at( i ).split( '=' );
166 for ( int j = 0; j < storedMceLines.size(); ++j )
168 QStringList storedMceLine = storedMceLines.at( j ).split( '=' );
169 if ( storedMceLine.at( 0 ).trimmed() == mceLine.at( 0 ).trimmed() )
171 mceLinesNew.append( storedMceLines.at( j ) );
176 mceLinesNew.append( mceLines.at( i ) );
181 // using the datastorage exceptionally but this time for changing data in the external conf file
182 if ( !iDataStorage->storeData( mceSection, mceLinesNew, McePath ) )
184 emit configuringError( DeviceManager::KeySettingsNotChanged );
191 bool DeviceConfigurator::toggleInitScript( bool aEnable )
194 if( !whoAmI( name ) ) {
195 emit configuringError( DeviceManager::InitScriptNotChanged );
199 QString command = InitScript;
204 command.prepend( "sudo " );
207 command.append( " install" );
209 command.append( " remove" );
211 if ( !systemIO( command, empty, result ) ) {
212 emit configuringError( DeviceManager::InitScriptNotChanged );
219 bool DeviceConfigurator::restartDevice()
221 QString command = BinPath + DevStopper;
224 args.append( "restart" );
225 if( !systemIO( command, args, result ) ) {
226 emit configuringError( DeviceManager::DeviceNotRestarted );
232 bool DeviceConfigurator::whoAmI( QByteArray &aName )
234 QString command = "whoami";
236 if( !systemIO( command, empty, aName ) )
241 bool DeviceConfigurator::systemIO( const QString &aCommand, const QStringList &aArgs, QByteArray &aResult )
243 qDebug() << "DeviceConfigurator::systemIO( QString &, QStringList &, QByteArray &)";
244 qDebug() << "Command: " << aCommand;
247 process.start( aCommand, aArgs );
249 process.start( aCommand );
251 if( !process.waitForFinished() )
253 aResult = process.readAll();
254 if( aResult.endsWith( '\n' ) )
257 qDebug() << "Result: " << aResult;