camerauis/cameraxui/cxui/src/cxuiapplicationstate.cpp
author hgs
Thu, 15 Jul 2010 01:53:45 +0300
changeset 42 feebad15db8c
parent 41 67457b2ffb33
child 58 ddba9caa7f32
permissions -rw-r--r--
201027_1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* Initial Contributors:
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
*
hgs
parents:
diff changeset
    12
* Contributors:
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* Description:
hgs
parents:
diff changeset
    15
*
hgs
parents:
diff changeset
    16
*/
hgs
parents:
diff changeset
    17
hgs
parents:
diff changeset
    18
#include <QMetaEnum>
hgs
parents:
diff changeset
    19
hgs
parents:
diff changeset
    20
#include "cxutils.h"
hgs
parents:
diff changeset
    21
#include "cxuieventlog.h"
hgs
parents:
diff changeset
    22
#include "cxuierrormanager.h"
hgs
parents:
diff changeset
    23
#include "cxuiapplicationstate.h"
hgs
parents:
diff changeset
    24
hgs
parents:
diff changeset
    25
namespace
hgs
parents:
diff changeset
    26
{
hgs
parents:
diff changeset
    27
    static const char *EVENT_APPLICATION_STATE = "application state";
hgs
parents:
diff changeset
    28
}
hgs
parents:
diff changeset
    29
hgs
parents:
diff changeset
    30
/*!
hgs
parents:
diff changeset
    31
* Constructor.
hgs
parents:
diff changeset
    32
*/
hgs
parents:
diff changeset
    33
CxuiApplicationState::CxuiApplicationState(CxuiApplication &application,
hgs
parents:
diff changeset
    34
                                           CxeSettings &settings,
hgs
parents:
diff changeset
    35
                                           CxuiDocumentLoader *documentLoader)
hgs
parents:
diff changeset
    36
    : mState(Background), mApplicationMonitor(NULL), mErrorManager(NULL), mEventLog(NULL)
hgs
parents:
diff changeset
    37
{
hgs
parents:
diff changeset
    38
    mApplicationMonitor = new CxuiApplicationFrameworkMonitor(application, settings);
hgs
parents:
diff changeset
    39
    mErrorManager = new CxuiErrorManager(documentLoader);
hgs
parents:
diff changeset
    40
hgs
parents:
diff changeset
    41
    // Foreground state change signals
hgs
parents:
diff changeset
    42
    connect(mApplicationMonitor, SIGNAL(foregroundStateChanged(CxuiApplicationFrameworkMonitor::ForegroundState)),
hgs
parents:
diff changeset
    43
            this, SLOT(handleForegroundStateChanged(CxuiApplicationFrameworkMonitor::ForegroundState)));
hgs
parents:
diff changeset
    44
hgs
parents:
diff changeset
    45
    // Battery empty signal
hgs
parents:
diff changeset
    46
    connect(mApplicationMonitor, SIGNAL(batteryEmpty()), this, SLOT(handleBatteryEmpty()));
hgs
parents:
diff changeset
    47
hgs
parents:
diff changeset
    48
    // USB mass memory mode signal
hgs
parents:
diff changeset
    49
    connect(mApplicationMonitor, SIGNAL(usbMassMemoryModeToggled(bool)),
hgs
parents:
diff changeset
    50
            this, SLOT(handleUsbMassMemoryModeChanged(bool)));
hgs
parents:
diff changeset
    51
hgs
parents:
diff changeset
    52
    // Severe error signals
hgs
parents:
diff changeset
    53
    connect(mErrorManager, SIGNAL(errorPopupShown()), this, SLOT(handleSevereError()));
hgs
parents:
diff changeset
    54
    connect(mErrorManager, SIGNAL(errorPopupClosed()), this, SLOT(handleErrorCleared()));
hgs
parents:
diff changeset
    55
hgs
parents:
diff changeset
    56
#ifdef CX_DEBUG
hgs
parents:
diff changeset
    57
    mEventLog = new CxuiEventLog("CxuiApplicationState");
hgs
parents:
diff changeset
    58
#endif
hgs
parents:
diff changeset
    59
}
hgs
parents:
diff changeset
    60
hgs
parents:
diff changeset
    61
/*!
hgs
parents:
diff changeset
    62
* Destructor.
hgs
parents:
diff changeset
    63
*/
hgs
parents:
diff changeset
    64
CxuiApplicationState::~CxuiApplicationState()
hgs
parents:
diff changeset
    65
{
hgs
parents:
diff changeset
    66
    delete mErrorManager;
hgs
parents:
diff changeset
    67
    delete mApplicationMonitor;
hgs
parents:
diff changeset
    68
    delete mEventLog;
hgs
parents:
diff changeset
    69
}
hgs
parents:
diff changeset
    70
hgs
parents:
diff changeset
    71
/*!
hgs
parents:
diff changeset
    72
* Get current application state.
hgs
parents:
diff changeset
    73
*/
hgs
parents:
diff changeset
    74
CxuiApplicationState::State CxuiApplicationState::currentState() const
hgs
parents:
diff changeset
    75
{
hgs
parents:
diff changeset
    76
    return mState;
hgs
parents:
diff changeset
    77
}
hgs
parents:
diff changeset
    78
hgs
parents:
diff changeset
    79
/*!
hgs
parents:
diff changeset
    80
* Start monitoring the application state.
hgs
parents:
diff changeset
    81
* Initial state is checked and signal emitted about state change *unless* state is Background.
hgs
parents:
diff changeset
    82
*/
hgs
parents:
diff changeset
    83
void CxuiApplicationState::startMonitoring()
hgs
parents:
diff changeset
    84
{
hgs
parents:
diff changeset
    85
    CX_DEBUG_ENTER_FUNCTION();
hgs
parents:
diff changeset
    86
    // Foreground handling checks for errors if needed.
hgs
parents:
diff changeset
    87
    handleForegroundStateChanged(mApplicationMonitor->foregroundState());
hgs
parents:
diff changeset
    88
    CX_DEBUG_EXIT_FUNCTION();
hgs
parents:
diff changeset
    89
}
hgs
parents:
diff changeset
    90
hgs
parents:
diff changeset
    91
/*!
hgs
parents:
diff changeset
    92
* Handle error from UI or engine.
hgs
parents:
diff changeset
    93
* Error will be checked and if it is severe, application state will become Error and error note will be shown.
hgs
parents:
diff changeset
    94
* If error is not severe, a warning note will be shown.
hgs
parents:
diff changeset
    95
* @param error The error id.
hgs
parents:
diff changeset
    96
*/
hgs
parents:
diff changeset
    97
void CxuiApplicationState::handleApplicationError(CxeError::Id error)
hgs
parents:
diff changeset
    98
{
hgs
parents:
diff changeset
    99
    CX_DEBUG_ENTER_FUNCTION();
hgs
parents:
diff changeset
   100
    if (error != CxeError::None) {
hgs
parents:
diff changeset
   101
        mErrorManager->check(error);
hgs
parents:
diff changeset
   102
        // If error manager sees this error as severe one, it will signal that back.
hgs
parents:
diff changeset
   103
        // We will handle updating state in handleSevereError().
hgs
parents:
diff changeset
   104
        // If only warning note (or nothing) is needed, application state is not changed.
hgs
parents:
diff changeset
   105
    }
hgs
parents:
diff changeset
   106
    CX_DEBUG_EXIT_FUNCTION();
hgs
parents:
diff changeset
   107
}
hgs
parents:
diff changeset
   108
hgs
parents:
diff changeset
   109
/*!
hgs
parents:
diff changeset
   110
* Handle change in application foreground status.
hgs
parents:
diff changeset
   111
* @param state New foreground status.
hgs
parents:
diff changeset
   112
*/
hgs
parents:
diff changeset
   113
void CxuiApplicationState::handleForegroundStateChanged(CxuiApplicationFrameworkMonitor::ForegroundState state)
hgs
parents:
diff changeset
   114
{
hgs
parents:
diff changeset
   115
    CX_DEBUG_ENTER_FUNCTION();
hgs
parents:
diff changeset
   116
    if (state == CxuiApplicationFrameworkMonitor::ForegroundFullyLost) {
hgs
parents:
diff changeset
   117
        CX_DEBUG(("CxuiApplicationState - application is in background"));
hgs
parents:
diff changeset
   118
        // Background overwrites even any error.  We clear any active errors.
hgs
parents:
diff changeset
   119
        // When returning to background, error situation will be re-checked.
hgs
parents:
diff changeset
   120
        setState(Background);
hgs
parents:
diff changeset
   121
        mErrorManager->clear();
hgs
parents:
diff changeset
   122
    } else {
hgs
parents:
diff changeset
   123
        CX_DEBUG(("CxuiApplicationState - application is in partial / full foreground"));
hgs
parents:
diff changeset
   124
        // Check that we were in background. Switching between partial and full background
hgs
parents:
diff changeset
   125
        // needs no actions.
hgs
parents:
diff changeset
   126
        if (currentState() == Background) {
hgs
parents:
diff changeset
   127
           CX_DEBUG(("CxuiApplicationState - application was in background before, moving to foreground"));
hgs
parents:
diff changeset
   128
            // Check that there's no active errors that have been ignored in background.
hgs
parents:
diff changeset
   129
            checkErrors();
hgs
parents:
diff changeset
   130
            if (currentState() != Error) {
hgs
parents:
diff changeset
   131
                setState(Normal);
hgs
parents:
diff changeset
   132
            }
hgs
parents:
diff changeset
   133
        }
hgs
parents:
diff changeset
   134
    }
hgs
parents:
diff changeset
   135
hgs
parents:
diff changeset
   136
    CX_DEBUG_EXIT_FUNCTION();
hgs
parents:
diff changeset
   137
}
hgs
parents:
diff changeset
   138
hgs
parents:
diff changeset
   139
/*!
hgs
parents:
diff changeset
   140
* Handle USB mass memory mode (USB MMM) activating or deactivating.
hgs
parents:
diff changeset
   141
* If USB MMM activates, we enter error state and display error note.
hgs
parents:
diff changeset
   142
* When USB MMM deactivates, we hide the note and move to standby mode.
hgs
parents:
diff changeset
   143
* @param active Is the USB mass memory mode active.
hgs
parents:
diff changeset
   144
*/
hgs
parents:
diff changeset
   145
void CxuiApplicationState::handleUsbMassMemoryModeChanged(bool active)
hgs
parents:
diff changeset
   146
{
hgs
parents:
diff changeset
   147
    CX_DEBUG_ENTER_FUNCTION();
hgs
parents:
diff changeset
   148
    if (active) {
hgs
parents:
diff changeset
   149
        // USB error is not handled if:
hgs
parents:
diff changeset
   150
        // (a) other severe error already active
hgs
parents:
diff changeset
   151
        // (b) application is in background
hgs
parents:
diff changeset
   152
        if (currentState() == Normal || currentState() == Standby) {
hgs
parents:
diff changeset
   153
            // Emulate memory not accessible error.
hgs
parents:
diff changeset
   154
            handleApplicationError(CxeError::MemoryNotAccessible);
hgs
parents:
diff changeset
   155
        }
hgs
parents:
diff changeset
   156
    } else {
hgs
parents:
diff changeset
   157
        // If we had USB error, clear it now.
hgs
parents:
diff changeset
   158
        if (currentState() == Error) {
hgs
parents:
diff changeset
   159
            setState(Standby);
hgs
parents:
diff changeset
   160
            // Clear memory not accessible error.
hgs
parents:
diff changeset
   161
            mErrorManager->clear();
hgs
parents:
diff changeset
   162
        }
hgs
parents:
diff changeset
   163
    }
hgs
parents:
diff changeset
   164
    CX_DEBUG_EXIT_FUNCTION();
hgs
parents:
diff changeset
   165
}
hgs
parents:
diff changeset
   166
hgs
parents:
diff changeset
   167
/*!
hgs
parents:
diff changeset
   168
* Handle battery emptying. We need to stop all activity and exit the application.
hgs
parents:
diff changeset
   169
*/
hgs
parents:
diff changeset
   170
void CxuiApplicationState::handleBatteryEmpty()
hgs
parents:
diff changeset
   171
{
hgs
parents:
diff changeset
   172
    CX_DEBUG_ENTER_FUNCTION();
hgs
parents:
diff changeset
   173
    setState(Background);
hgs
parents:
diff changeset
   174
    CX_DEBUG_EXIT_FUNCTION();
hgs
parents:
diff changeset
   175
}
hgs
parents:
diff changeset
   176
hgs
parents:
diff changeset
   177
/*!
hgs
parents:
diff changeset
   178
* Handle a severe error after Error Manager has analyzed it.
hgs
parents:
diff changeset
   179
*/
hgs
parents:
diff changeset
   180
void CxuiApplicationState::handleSevereError()
hgs
parents:
diff changeset
   181
{
hgs
parents:
diff changeset
   182
    CX_DEBUG_ENTER_FUNCTION();
hgs
parents:
diff changeset
   183
    // In background we do not change the state from Background to anything else.
hgs
parents:
diff changeset
   184
    if (currentState() != Background) {
hgs
parents:
diff changeset
   185
        setState(Error);
hgs
parents:
diff changeset
   186
    }
hgs
parents:
diff changeset
   187
    CX_DEBUG_EXIT_FUNCTION();
hgs
parents:
diff changeset
   188
}
hgs
parents:
diff changeset
   189
hgs
parents:
diff changeset
   190
/*!
hgs
parents:
diff changeset
   191
* Severe error has been cleared.
hgs
parents:
diff changeset
   192
* Check if we should return to normal or background state.
hgs
parents:
diff changeset
   193
*/
hgs
parents:
diff changeset
   194
void CxuiApplicationState::handleErrorCleared()
hgs
parents:
diff changeset
   195
{
hgs
parents:
diff changeset
   196
    CX_DEBUG_ENTER_FUNCTION();
hgs
parents:
diff changeset
   197
    // No state change if we are not currently in Error state.
hgs
parents:
diff changeset
   198
    if (currentState() == Error) {
hgs
parents:
diff changeset
   199
        setState(Normal);
hgs
parents:
diff changeset
   200
    }
hgs
parents:
diff changeset
   201
    CX_DEBUG_EXIT_FUNCTION();
hgs
parents:
diff changeset
   202
}
hgs
parents:
diff changeset
   203
hgs
parents:
diff changeset
   204
/*!
hgs
parents:
diff changeset
   205
* Slot for requesting Standby state to be entered.
hgs
parents:
diff changeset
   206
* Request is accepted only if current state is Normal. Otherwise call has no effect.
hgs
parents:
diff changeset
   207
*/
hgs
parents:
diff changeset
   208
void CxuiApplicationState::enterStandby()
hgs
parents:
diff changeset
   209
{
hgs
parents:
diff changeset
   210
    CX_DEBUG_ENTER_FUNCTION();
hgs
parents:
diff changeset
   211
    if (currentState() == Normal) {
hgs
parents:
diff changeset
   212
        setState(Standby);
hgs
parents:
diff changeset
   213
    }
hgs
parents:
diff changeset
   214
    CX_DEBUG_EXIT_FUNCTION();
hgs
parents:
diff changeset
   215
}
hgs
parents:
diff changeset
   216
hgs
parents:
diff changeset
   217
/*!
hgs
parents:
diff changeset
   218
* Slot for requesting state change from Standby to Normal state.
hgs
parents:
diff changeset
   219
* Request is accepted only if current state is Standby. Otherwise call has no effect.
hgs
parents:
diff changeset
   220
*/
hgs
parents:
diff changeset
   221
void CxuiApplicationState::exitStandby()
hgs
parents:
diff changeset
   222
{
hgs
parents:
diff changeset
   223
    CX_DEBUG_ENTER_FUNCTION();
hgs
parents:
diff changeset
   224
    if (currentState() == Standby) {
hgs
parents:
diff changeset
   225
        setState(Normal);
hgs
parents:
diff changeset
   226
    }
hgs
parents:
diff changeset
   227
    CX_DEBUG_EXIT_FUNCTION();
hgs
parents:
diff changeset
   228
}
hgs
parents:
diff changeset
   229
hgs
parents:
diff changeset
   230
/*!
hgs
parents:
diff changeset
   231
* Set new state.
hgs
parents:
diff changeset
   232
* If state is actually changing, stateChanged() signal is emitted.
hgs
parents:
diff changeset
   233
*/
hgs
parents:
diff changeset
   234
void CxuiApplicationState::setState(State newState)
hgs
parents:
diff changeset
   235
{
hgs
parents:
diff changeset
   236
    if (mState != newState) {
hgs
parents:
diff changeset
   237
#ifdef CX_DEBUG
hgs
parents:
diff changeset
   238
        if (mEventLog) {
hgs
parents:
diff changeset
   239
            mEventLog->append(EVENT_APPLICATION_STATE,
hgs
parents:
diff changeset
   240
                              CxuiApplicationState::staticMetaObject.enumerator(
hgs
parents:
diff changeset
   241
                                  CxuiApplicationState::staticMetaObject.
hgs
parents:
diff changeset
   242
                                        indexOfEnumerator("State")).valueToKey(newState));
hgs
parents:
diff changeset
   243
            mEventLog->print();
hgs
parents:
diff changeset
   244
        }
hgs
parents:
diff changeset
   245
#endif // CX_DEBUG
hgs
parents:
diff changeset
   246
hgs
parents:
diff changeset
   247
        State oldState = mState;
hgs
parents:
diff changeset
   248
        mState = newState;
hgs
parents:
diff changeset
   249
        emit stateChanged(newState, oldState);
hgs
parents:
diff changeset
   250
    }
hgs
parents:
diff changeset
   251
}
hgs
parents:
diff changeset
   252
hgs
parents:
diff changeset
   253
/*!
hgs
parents:
diff changeset
   254
* Check if we have known errors active at the moment.
hgs
parents:
diff changeset
   255
* State is set to CxuiApplicationState::Error, if error is found.
hgs
parents:
diff changeset
   256
*/
hgs
parents:
diff changeset
   257
void CxuiApplicationState::checkErrors()
hgs
parents:
diff changeset
   258
{
hgs
parents:
diff changeset
   259
    CX_DEBUG_ENTER_FUNCTION();
hgs
parents:
diff changeset
   260
    if (mApplicationMonitor->isUsbMassMemoryModeActive()) {
hgs
parents:
diff changeset
   261
        // Force Error state even if Background is still the current state.
hgs
parents:
diff changeset
   262
        // We use this method to check errors also when returning from background.
hgs
parents:
diff changeset
   263
        // Normally in Background state we do not enter Error state.
hgs
parents:
diff changeset
   264
        setState(Error);
hgs
parents:
diff changeset
   265
        handleApplicationError(CxeError::MemoryNotAccessible);
hgs
parents:
diff changeset
   266
    }
hgs
parents:
diff changeset
   267
    CX_DEBUG_EXIT_FUNCTION();
hgs
parents:
diff changeset
   268
}
hgs
parents:
diff changeset
   269
hgs
parents:
diff changeset
   270
hgs
parents:
diff changeset
   271
// end of file