Operation mode changing related defects corrected.
[qtmeetings] / src / IO / DeviceControl / DeviceConfigurator.cpp
1 #include "DeviceConfigurator.h"
2 #include "DeviceDataStorage.h"
3 #include "DeviceConstants.h"
4
5 #include <QtDebug>
6 #include <QProcess>
7
8 DeviceConfigurator::DeviceConfigurator( DeviceDataStorage *aDataStorage )
9 {
10         qDebug() << "DeviceConfigurator( DeviceDataStorage * )";
11         iDataStorage = aDataStorage;
12 }
13
14 DeviceConfigurator::~DeviceConfigurator()
15 {
16         qDebug() << "~DeviceConfigurator()";
17 }
18
19 bool DeviceConfigurator::toggleScreenSwitchOff( bool aEnable )
20 {       
21         qDebug() << "DeviceConfigurator::toggleScreenSwitchOff( bool )";
22         emit configuringError( DeviceManager::ScreenSettingsNotChanged );
23         return false;
24         QString command = "gconftool-2";
25         QStringList args;
26         QByteArray result;
27         QStringList confs;
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;
35
36         if ( !aEnable )
37         {
38                 //disabling the screen "auto-switch-off" and "dimming"
39
40                 //using gconftool-2 to get the current values for related configurations
41                 for ( int i = 0; i < confs.size(); ++i )
42                 {
43                         args.clear();
44                         args << "-g" << confs[i];
45                         if ( systemIO( command, args, result ) )
46                         {
47                                 if ( result.toLong() != 0 )
48                                         origValues.append( result );
49                         }
50                 }
51                 if ( origValues.size() == confs.size() )  //values succesfully fetched, now trying to store them
52                 {
53                         if ( !iDataStorage->storeData( iDataStorage->dataSectionToString( DeviceDataStorage::ScreenSettings ), origValues ) )
54                                 emit configuringError( DeviceManager::ScreenSettingsNotStored );
55                 }
56                 else   //values not fetched, using the default values instead
57                 {
58                         emit configuringError( DeviceManager::ScreenSettingsNotFetched );
59                         origValues.clear();
60                         for ( int i = 0; i < defParams.size(); ++i )
61                                 origValues.append( defParams.at( i ) );
62                 }
63
64                 //using gconftool-2 to change the related configurations
65                 for ( int i = 0; i < confs.size(); ++i )
66                 {
67                         args.clear();
68                         args << "-s" << confs[i] << "--type=int" << "6000000";
69                         if ( !systemIO( command, args, result ) ) {
70                                 emit configuringError( DeviceManager::ScreenSettingsNotChanged );
71                                 return false;
72                         }
73                 }
74         }
75         else
76         {
77                 //setting the screen's "auto-switch-off" and "dimming" settings back as they were
78
79                 //reading stored data from internal config file
80                 if ( !iDataStorage->readData( iDataStorage->dataSectionToString( DeviceDataStorage::ScreenSettings ), origValues ) )
81                 {
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 ) );
86                 }
87                 for ( int i = 0; i < origValues.size(); ++i )
88                 {
89                         args.clear();
90                         args << "-s" << confs[i] << "--type=int" << origValues.at(i);
91                         if ( !systemIO( command, args, result ) ) {
92                                 emit configuringError( DeviceManager::ScreenSettingsNotChanged );
93                                 return false;
94                         }
95                 }
96         }
97         return true;
98 }
99
100 bool DeviceConfigurator::toggleHWKeys( bool aEnable )
101 {
102         qDebug() << "DeviceConfigurator::toggleHWKeys( bool )";
103         QStringList mceLines;
104         QStringList mceLinesNew;
105         QString mceSection = "HomeKey";
106         QStringList params;
107         params << "HomeKeyShortAction" << "HomeKeyLongAction";
108
109         // using the DeviceDataStorage exceptionally for reading data from an external conf file
110         // /etc/mce/mce.ini
111         if ( !iDataStorage->readData( mceSection, mceLines, McePath ) )
112         {
113                 emit configuringError( DeviceManager::KeySettingsNotFetched );
114                 return false;
115         }
116
117         if ( !aEnable )
118         {
119                 // disabling the "home"-hw-key
120
121                 QStringList mceLinesToStore;
122                 for ( int i = 0; i < mceLines.size(); ++i )
123                 {
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 ) )
128                         {
129                                 for ( int j = 0; j < params.size(); ++j )
130                                 {
131                                         if ( params.at( j ) == param )
132                                         {
133                                                 mceLinesToStore.append( mceLines.at( i ) );
134                                                 mceLinesNew.append( param + "=disabled" );
135                                                 break;
136                                         }
137                                 }
138                         }
139                         else
140                                 mceLinesNew.append( mceLines.at( i ) );
141                 }
142
143                 // storing the mce conf file lines
144                 if ( !iDataStorage->storeData( iDataStorage->dataSectionToString( DeviceDataStorage::KeySettings ), mceLinesToStore ) )
145                 {
146                         emit configuringError( DeviceManager::KeySettingsNotStored );
147                         return false;
148                 }
149         }
150         else
151         {
152                 // setting the "home"-hw-key settings back as they were
153
154                 // reading the stored mce conf file lines
155                 QStringList storedMceLines;
156                 if ( !iDataStorage->readData( iDataStorage->dataSectionToString( DeviceDataStorage::KeySettings ), storedMceLines ) )
157                 {
158                         emit configuringError( DeviceManager::KeySettingsNotFetched );
159                         return false;
160                 }
161
162                 bool paramFound = false;
163                 for ( int i = 0; i < mceLines.size(); ++i )
164                 {
165                         QStringList mceLine = mceLines.at( i ).split( '=' );
166                         for ( int j = 0; j < storedMceLines.size(); ++j )
167                         {
168                                 QStringList storedMceLine = storedMceLines.at( j ).split( '=' );
169                                 if ( storedMceLine.at( 0 ).trimmed() == mceLine.at( 0 ).trimmed() )
170                                 {
171                                         mceLinesNew.append( storedMceLines.at( j ) );
172                                         paramFound = true;
173                                 }
174                         }
175                         if ( !paramFound )
176                                 mceLinesNew.append( mceLines.at( i ) );
177                         else
178                                 paramFound = false;
179                 }
180         }
181         // using the datastorage exceptionally but this time for changing data in the external conf file
182         if ( !iDataStorage->storeData( mceSection, mceLinesNew, McePath ) )
183         {
184                 emit configuringError( DeviceManager::KeySettingsNotChanged );
185                 return false;
186         }
187         
188         return true;
189 }
190
191 bool DeviceConfigurator::toggleInitScript( bool aEnable )
192 {
193         QByteArray name;
194         if( !whoAmI( name ) ) {
195                 emit configuringError( DeviceManager::InitScriptNotChanged );
196                 return false;
197         }
198         
199         QString command = InitScript;
200         QStringList empty;
201         QByteArray result;
202         
203         if( name != "root" )
204                 command.prepend( "sudo " );
205         
206         if( aEnable )
207                 command.append( " install" );
208         else
209                 command.append( " remove" );
210         
211         if ( !systemIO( command, empty, result ) ) {
212                 emit configuringError( DeviceManager::InitScriptNotChanged );
213                 return false;
214         }
215         
216         return true;
217 }
218
219 bool DeviceConfigurator::restartDevice()
220 {
221         QString command = BinPath + DevStopper;
222         QStringList args;
223         QByteArray result;
224         args.append( "restart" );
225         if( !systemIO( command, args, result ) ) {
226                 emit configuringError( DeviceManager::DeviceNotRestarted );
227                 return false;
228         }
229         return true;
230 }
231
232 bool DeviceConfigurator::whoAmI( QByteArray &aName )
233 {
234         QString command = "whoami";
235         QStringList empty;
236         if( !systemIO( command, empty, aName ) )
237                 return false;
238         return true;
239 }
240
241 bool DeviceConfigurator::systemIO( const QString &aCommand, const QStringList &aArgs, QByteArray &aResult )
242 {
243         qDebug() << "DeviceConfigurator::systemIO( QString &, QStringList &, QByteArray &)";
244         qDebug() << "Command: " << aCommand;
245         QProcess process;
246         if( !aArgs.empty() )
247                 process.start( aCommand, aArgs );
248         else
249                 process.start( aCommand );
250         
251         if( !process.waitForFinished() )
252                 return false;
253         aResult = process.readAll();
254         if( aResult.endsWith( '\n' ) )
255                 aResult.chop( 1 );
256         
257         qDebug() << "Result: " << aResult;
258         
259         return true;
260 }