1 #include "OperationModeToggler.h"
2 #include "DeviceManager.h"
3 #include "StartupSettings.h"
4 #include "AlarmSender.h"
5 #include "DeviceConfigurator.h"
6 #include "DeviceDataStorage.h"
10 OperationModeToggler::OperationModeToggler(
11 DeviceManager::OperationMode aMode,
12 StartupSettings *aSettings,
13 AlarmSender *aAlarmSender,
14 DeviceConfigurator *aConfigurator,
15 DeviceDataStorage *aDataStorage
18 iSettings( aSettings ),
19 iAlarmSender( aAlarmSender ),
20 iConfigurator( aConfigurator ),
21 iDataStorage( aDataStorage ),
24 qDebug() << "OperationModeToggler::OperationModeToggler( ... )";
27 OperationModeToggler::~OperationModeToggler()
29 qDebug() << "OperationModeToggler::~OperationModeToggler()";
32 void OperationModeToggler::run()
36 case DeviceManager::EmptyMode:
37 // error occured. Mode cannot be changed
38 createError( DeviceManager::ModeNotFetched );
41 case DeviceManager::StandAloneMode:
43 // change to KioskMode
45 // check if auto turn on/off functionality enabled and send turn on/off alarm events to alarm daemon
46 if ( iSettings->isPowersavingEnabled() )
48 emit changingMode( "Sending the auto launch alarms to alarm daemon." );
49 if ( !iAlarmSender->sendAlarms( iSettings->turnOnAt(), iSettings->turnOffAt() ) )
52 emit changingMode( "Rolling back changes..." );
53 return; //this is critical so returning if no success
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
62 emit changingMode( "Disabling home hardware-key." );
64 if( !iConfigurator->toggleHWKeys( false ) )
67 emit changingMode( "Installing the application init start-up script." );
69 if ( !iConfigurator->toggleInitScript( true ) )
73 emit changingMode( "Disabling the screen switching off and dimming." );
75 if( !iConfigurator->toggleScreenSwitchOff( false ) )
79 emit changingMode( "Storing information about the new operation mode." );
81 if( !storeOperationMode( DeviceManager::KioskMode, iDataStorage ) ) {
82 createError( DeviceManager::ModeNotStored );
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 );
100 case DeviceManager::KioskMode:
101 // change to StandAloneInProgress mode
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." );
109 if( !iConfigurator->toggleHWKeys( true ) )
112 emit changingMode( "Enabling the screen switching off and dimming." );
114 if( !iConfigurator->toggleScreenSwitchOff( true ) )
118 emit changingMode( "Storing information about the new operation mode." );
120 if( !storeOperationMode( DeviceManager::StandAloneModeInProgress, iDataStorage ) ) {
121 createError( DeviceManager::ModeNotStored );
126 emit changingMode( "Removing the auto launch alarms from alarm daemon." );
128 if( !iAlarmSender->removeStoredAlarms() )
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 );
144 default: // StandAloneModeInProgress should never come in question
149 bool OperationModeToggler::success()
151 qDebug() << "OperationModeToggler::success()";
155 bool OperationModeToggler::storeOperationMode( DeviceManager::OperationMode aMode, DeviceDataStorage *aDataStorage )
157 qDebug() << "OperationModeToggler::storeOperationMode( const OperationMode & )";
158 QStringList modeStrList;
160 modeStrList.append( str.append( QString( "%1" ).arg( aMode ) ) );
161 if ( !aDataStorage->storeData( aDataStorage->dataSectionToString( DeviceDataStorage::DeviceMode ), modeStrList ) )
166 void OperationModeToggler::createError( DeviceManager::ErrorCode aCode )
168 qDebug() << "OperationModeToggler::createError( DeviceManager::ErrorCode )";
170 emit error( aCode, empty );