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 emit error( 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 return; //this is critical so returning if no success
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
61 emit changingMode( "Disabling home hardware-key." );
63 if( !iConfigurator->toggleHWKeys( false ) )
66 emit changingMode( "Installing the application init start-up script." );
68 if ( !iConfigurator->toggleInitScript( true ) )
72 emit changingMode( "Disabling the screen switching off and dimming." );
74 if( !iConfigurator->toggleScreenSwitchOff( false ) )
78 emit changingMode( "Storing information about the new operation mode." );
80 if( !storeOperationMode( DeviceManager::KioskMode, iDataStorage ) ) {
81 emit error( DeviceManager::ModeNotStored );
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 );
98 case DeviceManager::KioskMode:
99 // change to StandAloneInProgress mode
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." );
107 if( !iConfigurator->toggleHWKeys( true ) )
110 emit changingMode( "Enabling the screen switching off and dimming." );
112 if( !iConfigurator->toggleScreenSwitchOff( true ) )
116 emit changingMode( "Storing information about the new operation mode." );
118 if( !storeOperationMode( DeviceManager::StandAloneModeInProgress, iDataStorage ) ) {
119 emit error( DeviceManager::ModeNotStored );
124 emit changingMode( "Removing the auto launch alarms from alarm daemon." );
126 if( !iAlarmSender->removeStoredAlarms() )
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 );
141 default: // StandAloneModeInProgress should never come in question
146 bool OperationModeToggler::success()
151 bool OperationModeToggler::storeOperationMode( DeviceManager::OperationMode aMode, DeviceDataStorage *aDataStorage )
153 qDebug() << "OperationModeToggler::storeOperationMode( const OperationMode & )";
154 QStringList modeStrList;
156 modeStrList.append( str.append( QString( "%1" ).arg( aMode ) ) );
157 if ( !aDataStorage->storeData( aDataStorage->dataSectionToString( DeviceDataStorage::DeviceMode ), modeStrList ) )