Settings view fixed.
[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         qRegisterMetaType<DeviceManager::ErrorCode>("DeviceManager::ErrorCode");
27         
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 ) ) );
34 }
35
36 OperationModeToggler::~OperationModeToggler()
37 {
38         qDebug() << "OperationModeToggler::~OperationModeToggler()";
39 }
40
41 void OperationModeToggler::run()
42 {
43         switch ( iMode )
44         {
45                 case DeviceManager::EmptyMode:
46                         // error occured. Mode cannot be changed
47                         createError( DeviceManager::ModeNotFetched );
48                         iSuccess = false;
49                         return;
50                 case DeviceManager::StandAloneMode:
51                         
52                         // change to KioskMode
53                         
54                         // check if auto turn on/off functionality enabled and send turn on/off alarm events to alarm daemon
55                         if ( iSettings->isPowersavingEnabled() )
56                         {
57                                 emit changingMode( "Sending the auto launch alarms to alarm daemon." );
58                                 if ( !iAlarmSender->sendAlarms( iSettings->turnOnAt(), iSettings->turnOffAt() ) )
59                                 {
60                                         iSuccess = false;
61                                         return; //this is critical so returning if no success
62                                 }
63                         }
64                         
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
69                         
70                         emit changingMode( "Disabling home hardware-key." );
71                         sleep( 2 );
72                         if( !iConfigurator->toggleHWKeys( false ) )
73                                 iSuccess = false;
74                         if( iSuccess ) {
75                                 emit changingMode( "Installing the application init start-up script." );
76                                 sleep( 2 );
77                                 if ( !iConfigurator->toggleInitScript( true ) )
78                                         iSuccess = false;
79                         }
80                         if( iSuccess ) {
81                                 emit changingMode( "Disabling the screen switching off and dimming." );
82                                 sleep( 2 );
83                                 if( !iConfigurator->toggleScreenSwitchOff( false ) )
84                                         iSuccess = false;
85                         }
86                         if( iSuccess ) {
87                                 emit changingMode( "Storing information about the new operation mode." );
88                                 sleep( 2 );
89                                 if( !storeOperationMode( DeviceManager::KioskMode, iDataStorage ) ) {
90                                         createError( DeviceManager::ModeNotStored );
91                                         iSuccess = false;
92                                 }       
93                         }
94                         if( !iSuccess ) {
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 );
103                                 return;
104                         }
105                         break;
106
107                 case DeviceManager::KioskMode:
108                         // change to StandAloneInProgress mode
109
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." );
115                         sleep( 2 );
116                         if( !iConfigurator->toggleHWKeys( true ) )
117                                 iSuccess = false;
118                         if( iSuccess ) {
119                                 emit changingMode( "Enabling the screen switching off and dimming." );
120                                 sleep( 2 );
121                                 if( !iConfigurator->toggleScreenSwitchOff( true ) )
122                                         iSuccess = false;
123                         }
124                         if( iSuccess ) {
125                                 emit changingMode( "Storing information about the new operation mode." );
126                                 sleep( 2 );
127                                 if( !storeOperationMode( DeviceManager::StandAloneModeInProgress, iDataStorage ) ) {
128                                         createError( DeviceManager::ModeNotStored );
129                                         iSuccess = false;
130                                 }
131                         }
132                         if( iSuccess ) {
133                                 emit changingMode( "Removing the auto launch alarms from alarm daemon." );
134                                 sleep( 2 );
135                                 if( !iAlarmSender->removeStoredAlarms() )
136                                         iSuccess = false;
137                         }
138                         if( !iSuccess ) {
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 );
147                                 return;
148                         }
149                         break;
150                 default: // StandAloneModeInProgress should never come in question
151                         break;
152         }
153 }
154
155 bool OperationModeToggler::success()
156 {
157         qDebug() << "OperationModeToggler::success()";
158         return iSuccess;
159 }
160                         
161 bool OperationModeToggler::storeOperationMode( DeviceManager::OperationMode aMode, DeviceDataStorage *aDataStorage )
162 {
163         qDebug() << "OperationModeToggler::storeOperationMode( const OperationMode & )";
164         QStringList modeStrList;
165         QString str;
166         modeStrList.append( str.append( QString( "%1" ).arg( aMode ) ) );
167         if ( !aDataStorage->storeData( aDataStorage->dataSectionToString( DeviceDataStorage::DeviceMode ), modeStrList ) )
168                 return false;
169         return true;
170 }
171
172 void OperationModeToggler::createError( DeviceManager::ErrorCode aCode )
173 {
174         qDebug() << "OperationModeToggler::createError( DeviceManager::ErrorCode )";
175         QString empty = "";
176         emit error( aCode, empty );
177 }
178