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