taskswitcher/server/src/tsrunningappmodel.cpp
author Jaakko Haukipuro (Nokia-MS/Oulu) <Jaakko.Haukipuro@nokia.com>
Thu, 16 Sep 2010 12:11:40 +0100
changeset 117 c63ee96dbe5f
permissions -rw-r--r--
Missing activityfw and taskswitcher components - fix for Bug 3670

/*
* 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 "tsrunningappmodel.h"

#include <AknDef.h>
#include <apgwgnam.h>
#include <apgtask.h>

#include "tsdatalist.h"
#include "tsmodelobserver.h"
#include "tsscreenshotprovider.h"

CTsRunningAppModel *CTsRunningAppModel::NewL(MTsResourceManager &resources, MTsWindowGroupsMonitor &monitor)
{
    CTsRunningAppModel *self = CTsRunningAppModel::NewLC(resources, monitor);
    CleanupStack::Pop(self);
    return self;
}

CTsRunningAppModel *CTsRunningAppModel::NewLC(MTsResourceManager &resources, MTsWindowGroupsMonitor &monitor)
{
    CTsRunningAppModel *self = new (ELeave) CTsRunningAppModel(resources);
    CleanupStack::PushL(self);
    self->ConstructL(resources, monitor);
    return self;
}

CTsRunningAppModel::~CTsRunningAppModel()
{
    delete mDataList;
    delete mScreenshotProvider;
}
    
CTsRunningAppModel::CTsRunningAppModel(MTsResourceManager &resources) : mResources(resources)
{
}

void CTsRunningAppModel::ConstructL(MTsResourceManager &resources, MTsWindowGroupsMonitor &monitor)
{
    mDataList = CTsDataList::NewL(resources, monitor, *this);
    mScreenshotProvider = CTsScreenshotProvider::NewL(*mDataList);

    RArray<RWsSession::TWindowGroupChainInfo> wgList;
    CleanupClosePushL(wgList);
    User::LeaveIfError(resources.WsSession().WindowGroupList(0, &wgList));
    mDataList->HandleWindowGroupChanged(resources, wgList.Array());
    CleanupStack::PopAndDestroy(&wgList);
}

// --------------------------------------------------------------------------
// MHsDataObserver interface implementation
// --------------------------------------------------------------------------
//
void CTsRunningAppModel::DataChanged()
{
    if (mObserver) {
        mObserver->dataChanged(*this);
    }
}

// --------------------------------------------------------------------------
// MTsTaskSettings interface implementation
// --------------------------------------------------------------------------
//
TBool CTsRunningAppModel::isSupported(TInt function) const
{
    return mDataList->isSupported(function);
}

void CTsRunningAppModel::handleDataL(TInt function,RReadStream& dataStream)
{
    mDataList->handleDataL(function, dataStream);
}

// --------------------------------------------------------------------------
// MTsModel interface implementation
// --------------------------------------------------------------------------
//
TInt CTsRunningAppModel::count() const
{
    return mDataList->Data().Count();
}

void CTsRunningAppModel::setObserver(MTsModelObserver *observer)
{
    mObserver = observer;
}

const TDesC& CTsRunningAppModel::displayNameL(TInt offset) const
{
    return mDataList->Data()[offset]->AppName();
}

TInt CTsRunningAppModel::iconHandleL(TInt offset) const
{
    CFbsBitmap *bitmap = mDataList->Data()[offset]->Screenshot();
    if (!bitmap) {
        bitmap = mDataList->Data()[offset]->AppIconBitmap();
    }
    
    if (bitmap) {
        return bitmap->Handle();
    } else {
        return KErrNotFound;
    }
}

TTime CTsRunningAppModel::timestampL(TInt offset) const
{
    return mDataList->Data()[offset]->Timestamp();
}

TTsModelItemKey CTsRunningAppModel::keyL(TInt offset) const
{
    return TTsModelItemKey(mDataList->Data()[offset]->Key().WindowGroupId(), 
                           reinterpret_cast<TInt>(this));
}

TBool CTsRunningAppModel::isActiveL(TInt /*offset*/) const
{    
    return ETrue;
}

TBool CTsRunningAppModel::isClosableL(TInt offset) const
{
    return mDataList->Data()[offset]->CloseableApp();
}

TBool CTsRunningAppModel::closeL(TTsModelItemKey key) const
{
    TApaTask task(mResources.WsSession());
    task.SetWgId(key.key());
    task.EndTask();
    return ETrue;
}

TBool CTsRunningAppModel::launchL(TTsModelItemKey key) const 
{
    // find uid by wgid from key
    CApaWindowGroupName *windowGroupName = 
        CApaWindowGroupName::NewLC(mResources.WsSession(), key.key());
    TUid uid = windowGroupName->AppUid();
    CleanupStack::PopAndDestroy(windowGroupName);

    TApaTask task = TApaTaskList(mResources.WsSession()).FindApp(uid);
    task.BringToForeground();
    return task.Exists();
}