Merge branch 'dev_itkonma'
[qtmeetings] / src / IO / DeviceControl / OperationModeToggler.cpp
1 #include "OperationModeToggler.h"
2 #include "DeviceManager.h"
3 #include "StartupSettings.h"
4 #include "AlarmSender.h"
5 #include "DeviceConfigurator.h"
6 #include "DeviceDataStorage.h"
7
8 #include <QtDebug>
9
10 OperationModeToggler::OperationModeToggler( 
11                 DeviceManager::OperationMode aMode, 
12                 StartupSettings *aSettings, 
13                 AlarmSender *aAlarmSender, 
14                 DeviceConfigurator *aConfigurator,
15                 DeviceDataStorage *aDataStorage
16                 ) :
17         iMode( aMode ),
18         iSettings( aSettings ),
19         iAlarmSender( aAlarmSender ),
20         iConfigurator( aConfigurator ),
21         iDataStorage( aDataStorage ),
22         iSuccess( true )
23 {
24         qDebug() << "OperationModeToggler::OperationModeToggler( ... )";
25 }
26
27 OperationModeToggler::~OperationModeToggler()
28 {
29         qDebug() << "OperationModeToggler::~OperationModeToggler()";
30 }
31
32 void OperationModeToggler::run()
33 {
34         switch ( iMode )
35         {
36                 case DeviceManager::EmptyMode:
37                         // error occured. Mode cannot be changed
38                         createError( DeviceManager::ModeNotFetched );
39                         iSuccess = false;
40                         return;
41                 case DeviceManager::StandAloneMode:
42                         
43                         // change to KioskMode
44                         
45                         // check if auto turn on/off functionality enabled and send turn on/off alarm events to alarm daemon
46                         if ( iSettings->isPowersavingEnabled() )
47                         {
48                                 emit changingMode( "Sending the auto launch alarms to alarm daemon." );
49                                 if ( !iAlarmSender->sendAlarms( iSettings->turnOnAt(), iSettings->turnOffAt() ) )
50                                 {
51                                         iSuccess = false;
52                                         emit changingMode( "Rolling back changes..." );
53                                         return; //this is critical so returning if no success
54                                 }
55                         }
56                         
57                         // - disable the certain hw keys (only "home"-hw-key at the moment)
58                         // - register init script to launch the application when ever the device is launched
59                         // - disable the screen "auto-switch-off" and "dimming"
60                         // - store info about the new operation mode
61                         
62                         emit changingMode( "Disabling home hardware-key." );
63                         sleep( 2 );
64                         if( !iConfigurator->toggleHWKeys( false ) )
65                                 iSuccess = false;
66                         if( iSuccess ) {
67                                 emit changingMode( "Installing the application init start-up script." );
68                                 sleep( 2 );
69                                 if ( !iConfigurator->toggleInitScript( true ) )
70                                         iSuccess = false;
71                         }
72                         if( iSuccess ) {
73                                 emit changingMode( "Disabling the screen switching off and dimming." );
74                                 sleep( 2 );
75                                 if( !iConfigurator->toggleScreenSwitchOff( false ) )
76                                         iSuccess = false;
77                         }
78                         if( iSuccess ) {
79                                 emit changingMode( "Storing information about the new operation mode." );
80                                 sleep( 2 );
81                                 if( !storeOperationMode( DeviceManager::KioskMode, iDataStorage ) ) {
82                                         createError( DeviceManager::ModeNotStored );
83                                         iSuccess = false;
84                                 }       
85                         }
86                         if( !iSuccess ) {
87                                 // we have to roll back if something fails
88                                 // of course rolling back may fail as well but it is impossible to catch
89                                 emit toggleErrorSending( false );
90                                 emit changingMode( "Rolling back changes..." );
91                                 iAlarmSender->removeAlarms();
92                                 iConfigurator->toggleHWKeys( true );
93                                 iConfigurator->toggleInitScript( false );
94                                 iConfigurator->toggleScreenSwitchOff( true );
95                                 emit toggleErrorSending( true );
96                                 return;
97                         }
98                         break;
99
100                 case DeviceManager::KioskMode:
101                         // change to StandAloneInProgress mode
102
103                         // - enable the certain hw keys (only "home"-hw-key at the moment)
104                         // - unregister the init script
105                         // - enable the screen "auto-switch-off" and "dimming"
106                         // - store info about the new operation mode
107                         emit changingMode( "Enabling home hardware-key." );
108                         sleep( 2 );
109                         if( !iConfigurator->toggleHWKeys( true ) )
110                                 iSuccess = false;
111                         if( iSuccess ) {
112                                 emit changingMode( "Enabling the screen switching off and dimming." );
113                                 sleep( 2 );
114                                 if( !iConfigurator->toggleScreenSwitchOff( true ) )
115                                         iSuccess = false;
116                         }
117                         if( iSuccess ) {
118                                 emit changingMode( "Storing information about the new operation mode." );
119                                 sleep( 2 );
120                                 if( !storeOperationMode( DeviceManager::StandAloneModeInProgress, iDataStorage ) ) {
121                                         createError( DeviceManager::ModeNotStored );
122                                         iSuccess = false;
123                                 }
124                         }
125                         if( iSuccess ) {
126                                 emit changingMode( "Removing the auto launch alarms from alarm daemon." );
127                                 sleep( 2 );
128                                 if( !iAlarmSender->removeStoredAlarms() )
129                                         iSuccess = false;
130                         }
131                         if( !iSuccess ) {
132                                 // we have to roll back if something fails
133                                 // of course rolling back may fail as well but it is impossible to catch
134                                 emit toggleErrorSending( false );
135                                 emit changingMode( "Rolling back changes..." );
136                                 iConfigurator->toggleHWKeys( false );
137                                 iConfigurator->toggleInitScript( true );
138                                 iConfigurator->toggleScreenSwitchOff( false );
139                                 storeOperationMode( DeviceManager::KioskMode, iDataStorage );
140                                 emit toggleErrorSending( true );
141                                 return;
142                         }
143                         break;
144                 default: // StandAloneModeInProgress should never come in question
145                         break;
146         }
147 }
148
149 bool OperationModeToggler::success()
150 {
151         qDebug() << "OperationModeToggler::success()";
152         return iSuccess;
153 }
154                         
155 bool OperationModeToggler::storeOperationMode( DeviceManager::OperationMode aMode, DeviceDataStorage *aDataStorage )
156 {
157         qDebug() << "OperationModeToggler::storeOperationMode( const OperationMode & )";
158         QStringList modeStrList;
159         QString str;
160         modeStrList.append( str.append( QString( "%1" ).arg( aMode ) ) );
161         if ( !aDataStorage->storeData( aDataStorage->dataSectionToString( DeviceDataStorage::DeviceMode ), modeStrList ) )
162                 return false;
163         return true;
164 }
165
166 void OperationModeToggler::createError( DeviceManager::ErrorCode aCode )
167 {
168         qDebug() << "OperationModeToggler::createError( DeviceManager::ErrorCode )";
169         QString empty = "";
170         emit error( aCode, empty );
171 }
172