taskswitcher/client/s60/src/tstaskmonitor_p.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 "tstaskmonitor_p.h"
#include "tstaskmonitor.h"

#include <eikenv.h>
#include <fbs.h>
#include <s32mem.h>

#include <XQConversions>

#include "tstaskmonitorclient.h"
#include "tstask.h"
#include "tstaskcontent.h"
#include "tstaskchangeinfo.h"
#include "tsutils.h"
using TaskSwitcher::CleanupResetAndDestroyPushL;

TsTaskMonitorPrivate::TsTaskMonitorPrivate(TsTaskMonitor *q) : q_ptr(q), mClient(0), mWsSession(CEikonEnv::Static()->WsSession())
{
    QT_TRAP_THROWING(mClient = CTsTaskMonitorClient::NewL());
    mClient->Subscribe(*this);
}

TsTaskMonitorPrivate::~TsTaskMonitorPrivate()
{
    mClient->CancelSubscribe();
    delete mClient;
}


QList<TsTaskChange> TsTaskMonitorPrivate::changeList()
{
    QList<TsTaskChange> changes;
    TsTaskChangeInfo qtChangeItem(-2,-2);

       QT_TRAP_THROWING (
               
           HBufC8 *data = mClient->TasksContentLC();
           if (data->Size() == 0) {
               CleanupStack::PopAndDestroy(data);
               return changes;
           }
           TPtr8 dataPointer(data->Des());
           RDesReadStream dataStream(dataPointer);
           CleanupClosePushL(dataStream);
           
           int count = dataStream.ReadInt32L();
           QT_TRYCATCH_LEAVING(
               changes.append( TsTaskChange( qtChangeItem, QSharedPointer<TsTask>()));
           for (int iter(0); iter < count; iter++) {
               qtChangeItem = TsTaskChangeInfo(iter, TsTaskChangeInfo::KInvalidOffset);
               QScopedPointer<TsTaskContent> content(new TsTaskContent);
               internalizeContentL(dataStream, content);
                   changes.append( TsTaskChange( qtChangeItem, QSharedPointer<TsTask>(new TsTask(content.take(), *this))));
           }
           )//QT_TRYCATCH_LEAVING
           CleanupStack::PopAndDestroy(&dataStream);
           CleanupStack::PopAndDestroy(data);
       );//QT_TRAP_THROWING
       
       return changes;
}

void TsTaskMonitorPrivate::internalizeContentL(RDesReadStream &  dataStream, QScopedPointer<TsTaskContent> &content)
{
    // get name
    TInt nameLength(dataStream.ReadInt32L());
    if (0 < nameLength) {
      HBufC* name = HBufC::NewLC(dataStream, nameLength);
      content->mName = XQConversions::s60DescToQString(*name);
      CleanupStack::PopAndDestroy(name);
    }
    
    // get screenshot
    TInt screenshotHandle = dataStream.ReadInt32L();
    CFbsBitmap *screenshot = new (ELeave) CFbsBitmap;
    CleanupStack::PushL(screenshot);
    if (KErrNone == screenshot->Duplicate(screenshotHandle)) {
      content->mScreenshot = QPixmap::fromSymbianCFbsBitmap(screenshot);
    }
    CleanupStack::PopAndDestroy(screenshot);
              
    // get key
    TInt keyLength(dataStream.ReadInt32L());
    if (0 < keyLength) {
      HBufC8* key = HBufC8::NewLC(keyLength);
      TPtr8 des(key->Des());
      dataStream.ReadL(des, keyLength);
      content->mKey = XQConversions::s60Desc8ToQByteArray(*key);
      CleanupStack::PopAndDestroy(key);
    }
    
    // get other values
    content->mActive = dataStream.ReadInt32L();
    content->mClosable = dataStream.ReadInt32L();    
}

void TsTaskMonitorPrivate::HandleRunningAppChange()
{
    emit q_ptr->taskListChanged();
}

void TsTaskMonitorPrivate::openTask(const QByteArray &key)
{
    TPtrC8 desC(reinterpret_cast<const TUint8*>(key.constData()), key.length());
    mClient->OpenTask(desC);
    
}

void TsTaskMonitorPrivate::closeTask(const QByteArray &key)
{
    TPtrC8 desC(reinterpret_cast<const TUint8*>(key.constData()), key.length());
    mClient->CloseTask(desC);
}