/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/
#include <tswindowgroupsobserver.h>
#include "tstaskmonitorglobals.h"
#include "hsrunningappserver.h"
#include "hsrunningappsession.h"
#include "tsbacksteppingactivation.h"
#include "tsmodel.h"
#include "tsstorage.h"
#include "tsservicesprovider.h"
#include "tsserializeddataprovider.h"
#include "tsrunningappmodel.h"
_LIT(KErrObserverExists, "Observer already exists");
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
CRunningAppServer::CRunningAppServer()
:
CServer2(EPriorityStandard)
{
// No implementation required
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
CRunningAppServer::~CRunningAppServer()
{
delete mBacksteppingEngine;
delete mStorage;
delete mAppsModel;
delete mServiceProvider;
delete mSerializer;
delete mMonitor;
mObservers.ResetAndDestroy();
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
CRunningAppServer* CRunningAppServer::NewLC()
{
CRunningAppServer* self = new (ELeave) CRunningAppServer();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
void CRunningAppServer::ConstructL()
{
StartL(KRunningAppServerName);
User::LeaveIfError(mWsSession.Connect());
mResources = CTsResourceManager::NewL();
mMonitor = CTsWindowGroupsMonitor::NewL(*mResources);
mSerializer = CTsSerializedDataProvider::NewL(*this);
RPointerArray<MTsModel> providers;
CleanupClosePushL(providers);
mAppsModel = CTsRunningAppModel::NewL(*mResources, *mMonitor);
providers.AppendL(mAppsModel);
CTsServiceProviderConfig *cfg = CTsServiceProviderConfig::NewLC();
mServiceProvider = CTsServiceProvider::NewL(*cfg);
CleanupStack::PopAndDestroy(cfg);
addProviders(providers, *mServiceProvider);
mStorage = CTsStorage::NewL(providers.Array());
mStorage->setObserver(mSerializer);
CleanupStack::PopAndDestroy(&providers);
// load initial data
mStorage->DataChanged();
TRAP_IGNORE(mBacksteppingEngine = CTsBacksteppingActivation::NewL(*mMonitor);)
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
CSession2* CRunningAppServer::NewSessionL(const TVersion &, const RMessage2&) const
{
RPointerArray<MTsDataStorage> dataStorages;
CleanupClosePushL(dataStorages);
dataStorages.AppendL(const_cast<CRunningAppServer *>(this)->mAppsModel);
dataStorages.AppendL(const_cast<CRunningAppServer *>(this)->mStorage);
CSession2* retVal = CRunningAppSession::NewL(*const_cast<CRunningAppServer *>(this),
*const_cast<CRunningAppServer *>(this)->mSerializer,
dataStorages.Array());
CleanupStack::PopAndDestroy(&dataStorages);
return retVal;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
void CRunningAppServer::DataChanged()
{
while (0 < mObservers.Count()) {
mObservers[0]->DataChanged();
}
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
void CRunningAppServer::PushL(CTsDataObserver* observer)
{
( 0 <= mObservers.Find(observer)) ?
User::Panic(KErrObserverExists, KErrAlreadyExists) :
mObservers.AppendL(observer);
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
void CRunningAppServer::Pop(CTsDataObserver* observer)
{
const TInt offset(mObservers.Find(observer));
if(0 <= offset) {
mObservers.Remove(offset);
}
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
void CRunningAppServer::Cancel(const RMessage2& reason)
{
for (TInt iter(mObservers.Count() - 1); 0 <= iter; --iter) {
mObservers[iter]->Cancel(reason);
}
reason.Complete(KErrNone);
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
void CRunningAppServer::Invalidate(const CSession2* session)
{
for (TInt iter(mObservers.Count() - 1); 0 <= iter; --iter) {
if(mObservers[iter]->IsParent(session)) {
delete mObservers[iter];
mObservers.Remove(iter);
}
}
}
// -----------------------------------------------------------------------------
void CRunningAppServer::addProviders(RPointerArray<MTsModel> &dst,
const CTsServiceProvider& serviceProvider)
{
for (TInt offset(0); offset < serviceProvider.count(); ++offset) {
dst.Append(&serviceProvider[offset]);
}
}