0f44687ec60ccd971ec2c650758b44d161241766
[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                         emit error( 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                                         return; //this is critical so returning if no success
53                                 }
54                         }
55                         
56                         // - disable the certain hw keys (only "home"-hw-key at the moment)
57                         // - register init script to launch the application when ever the device is launched
58                         // - disable the screen "auto-switch-off" and "dimming"
59                         // - store info about the new operation mode
60                         
61                         emit changingMode( "Disabling home hardware-key." );
62                         sleep( 2 );
63                         if( !iConfigurator->toggleHWKeys( false ) )
64                                 iSuccess = false;
65                         if( iSuccess ) {
66                                 emit changingMode( "Installing the application init start-up script." );
67                                 sleep( 2 );
68                                 if ( !iConfigurator->toggleInitScript( true ) )
69                                         iSuccess = false;
70                         }
71                         if( iSuccess ) {
72                                 emit changingMode( "Disabling the screen switching off and dimming." );
73                                 sleep( 2 );
74                                 if( !iConfigurator->toggleScreenSwitchOff( false ) )
75                                         iSuccess = false;
76                         }
77                         if( iSuccess ) {
78                                 emit changingMode( "Storing information about the new operation mode." );
79                                 sleep( 2 );
80                                 if( !storeOperationMode( DeviceManager::KioskMode, iDataStorage ) ) {
81                                         emit error( DeviceManager::ModeNotStored );
82                                         iSuccess = false;
83                                 }       
84                         }
85                         if( !iSuccess ) {
86                                 // we have to roll back if something fails
87                                 // of course rolling back may fail as well but it is impossible to catch
88                                 emit toggleErrorSending( false );
89                                 iAlarmSender->removeAlarms();
90                                 iConfigurator->toggleHWKeys( true );
91                                 iConfigurator->toggleInitScript( false );
92                                 iConfigurator->toggleScreenSwitchOff( true );
93                                 emit toggleErrorSending( true );
94                                 return;
95                         }
96                         break;
97
98                 case DeviceManager::KioskMode:
99                         // change to StandAloneInProgress mode
100
101                         // - enable the certain hw keys (only "home"-hw-key at the moment)
102                         // - unregister the init script
103                         // - enable the screen "auto-switch-off" and "dimming"
104                         // - store info about the new operation mode
105                         emit changingMode( "Enabling home hardware-key." );
106                         sleep( 2 );
107                         if( !iConfigurator->toggleHWKeys( true ) )
108                                 iSuccess = false;
109                         if( iSuccess ) {
110                                 emit changingMode( "Enabling the screen switching off and dimming." );
111                                 sleep( 2 );
112                                 if( !iConfigurator->toggleScreenSwitchOff( true ) )
113                                         iSuccess = false;
114                         }
115                         if( iSuccess ) {
116                                 emit changingMode( "Storing information about the new operation mode." );
117                                 sleep( 2 );
118                                 if( !storeOperationMode( DeviceManager::StandAloneModeInProgress, iDataStorage ) ) {
119                                         emit error( DeviceManager::ModeNotStored );
120                                         iSuccess = false;
121                                 }
122                         }
123                         if( iSuccess ) {
124                                 emit changingMode( "Removing the auto launch alarms from alarm daemon." );
125                                 sleep( 2 );
126                                 if( !iAlarmSender->removeStoredAlarms() )
127                                         iSuccess = false;
128                         }
129                         if( !iSuccess ) {
130                                 // we have to roll back if something fails
131                                 // of course rolling back may fail as well but it is impossible to catch
132                                 emit toggleErrorSending( false );
133                                 iConfigurator->toggleHWKeys( false );
134                                 iConfigurator->toggleInitScript( true );
135                                 iConfigurator->toggleScreenSwitchOff( false );
136                                 storeOperationMode( DeviceManager::KioskMode, iDataStorage );
137                                 emit toggleErrorSending( true );
138                                 return;
139                         }
140                         break;
141                 default: // StandAloneModeInProgress should never come in question
142                         break;
143         }
144 }
145
146 bool OperationModeToggler::success()
147 {
148         return iSuccess;
149 }
150                         
151 bool OperationModeToggler::storeOperationMode( DeviceManager::OperationMode aMode, DeviceDataStorage *aDataStorage )
152 {
153         qDebug() << "OperationModeToggler::storeOperationMode( const OperationMode & )";
154         QStringList modeStrList;
155         QString str;
156         modeStrList.append( str.append( QString( "%1" ).arg( aMode ) ) );
157         if ( !aDataStorage->storeData( aDataStorage->dataSectionToString( DeviceDataStorage::DeviceMode ), modeStrList ) )
158                 return false;
159         return true;
160 }