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( ... )";
26 qRegisterMetaType<DeviceManager::ErrorCode>("DeviceManager::ErrorCode");
28 connect( iAlarmSender, SIGNAL( alarmSendingFailed( DeviceManager::ErrorCode, const QString& ) ),
29 this, SIGNAL( error( DeviceManager::ErrorCode, const QString& ) ) );
30 connect( iConfigurator, SIGNAL( configuringError( DeviceManager::ErrorCode ) ),
31 this, SLOT( createError( DeviceManager::ErrorCode ) ) );
32 connect( iDataStorage, SIGNAL( dataStorageInitFailed( DeviceManager::ErrorCode ) ),
33 this, SLOT( createError( DeviceManager::ErrorCode ) ) );
36 OperationModeToggler::~OperationModeToggler()
38 qDebug() << "OperationModeToggler::~OperationModeToggler()";
41 void OperationModeToggler::run()
45 case DeviceManager::EmptyMode:
46 // error occured. Mode cannot be changed
47 createError( DeviceManager::ModeNotFetched );
50 case DeviceManager::StandAloneMode:
52 // change to KioskMode
54 // check if auto turn on/off functionality enabled and send turn on/off alarm events to alarm daemon
55 if ( iSettings->isPowersavingEnabled() )
57 emit changingMode( "Sending the auto launch alarms to alarm daemon." );
58 if ( !iAlarmSender->sendAlarms( iSettings->turnOnAt(), iSettings->turnOffAt() ) )
61 return; //this is critical so returning if no success
65 // - disable the certain hw keys (only "home"-hw-key at the moment)
66 // - register init script to launch the application when ever the device is launched
67 // - disable the screen "auto-switch-off" and "dimming"
68 // - store info about the new operation mode
70 emit changingMode( "Disabling home hardware-key." );
72 if( !iConfigurator->toggleHWKeys( false ) )
75 emit changingMode( "Installing the application init start-up script." );
77 if ( !iConfigurator->toggleInitScript( true ) )
81 emit changingMode( "Disabling the screen switching off and dimming." );
83 if( !iConfigurator->toggleScreenSwitchOff( false ) )
87 emit changingMode( "Storing information about the new operation mode." );
89 if( !storeOperationMode( DeviceManager::KioskMode, iDataStorage ) ) {
90 createError( DeviceManager::ModeNotStored );
95 // we have to roll back if something fails
96 // of course rolling back may fail as well but it is impossible to catch
97 emit toggleErrorSending( false );
98 iAlarmSender->removeAlarms();
99 iConfigurator->toggleHWKeys( true );
100 iConfigurator->toggleInitScript( false );
101 iConfigurator->toggleScreenSwitchOff( true );
102 emit toggleErrorSending( true );
107 case DeviceManager::KioskMode:
108 // change to StandAloneInProgress mode
110 // - enable the certain hw keys (only "home"-hw-key at the moment)
111 // - unregister the init script
112 // - enable the screen "auto-switch-off" and "dimming"
113 // - store info about the new operation mode
114 emit changingMode( "Enabling home hardware-key." );
116 if( !iConfigurator->toggleHWKeys( true ) )
119 emit changingMode( "Enabling the screen switching off and dimming." );
121 if( !iConfigurator->toggleScreenSwitchOff( true ) )
125 emit changingMode( "Storing information about the new operation mode." );
127 if( !storeOperationMode( DeviceManager::StandAloneModeInProgress, iDataStorage ) ) {
128 createError( DeviceManager::ModeNotStored );
133 emit changingMode( "Removing the auto launch alarms from alarm daemon." );
135 if( !iAlarmSender->removeStoredAlarms() )
139 // we have to roll back if something fails
140 // of course rolling back may fail as well but it is impossible to catch
141 emit toggleErrorSending( false );
142 iConfigurator->toggleHWKeys( false );
143 iConfigurator->toggleInitScript( true );
144 iConfigurator->toggleScreenSwitchOff( false );
145 storeOperationMode( DeviceManager::KioskMode, iDataStorage );
146 emit toggleErrorSending( true );
150 default: // StandAloneModeInProgress should never come in question
155 bool OperationModeToggler::success()
157 qDebug() << "OperationModeToggler::success()";
161 bool OperationModeToggler::storeOperationMode( DeviceManager::OperationMode aMode, DeviceDataStorage *aDataStorage )
163 qDebug() << "OperationModeToggler::storeOperationMode( const OperationMode & )";
164 QStringList modeStrList;
166 modeStrList.append( str.append( QString( "%1" ).arg( aMode ) ) );
167 if ( !aDataStorage->storeData( aDataStorage->dataSectionToString( DeviceDataStorage::DeviceMode ), modeStrList ) )
172 void OperationModeToggler::createError( DeviceManager::ErrorCode aCode )
174 qDebug() << "OperationModeToggler::createError( DeviceManager::ErrorCode )";
176 emit error( aCode, empty );