taskswitcher/server/src/tsstorage.cpp
changeset 117 c63ee96dbe5f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/taskswitcher/server/src/tsstorage.cpp	Thu Sep 16 12:11:40 2010 +0100
@@ -0,0 +1,285 @@
+/*
+* 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 <tstaskmonitorglobals.h>
+#include "tsstorage.h"
+#include "tsmodelitemkeymsg.h"
+// -----------------------------------------------------------------------------
+/**
+ * Two phase construction. Create and initialize storage instance.
+ * @param dataProviders - list of data providers
+ * @return storage instane
+ */
+CTsStorage* CTsStorage::NewL(const TArray<MTsModel*> &dataProviders)
+{
+    CTsStorage* self = new(ELeave) CTsStorage();
+    CleanupStack::PushL(self);
+    self->ConstructL(dataProviders);
+    CleanupStack::Pop(self);
+    return self;
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Destructor. Function cancel subscribtion for data change notyfications
+ */
+CTsStorage::~CTsStorage()
+{
+    for (TInt iter(0); iter < mDataProviders.Count(); ++iter) {
+        mDataProviders[iter]->setObserver(0);
+    }
+    mData.Close();
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * First phase construction.
+ */
+CTsStorage::CTsStorage()
+{
+    //No implementation required
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Second phase construction. Function make subscribtion for data changes notifications
+ * @param dataProviders - list of data providers
+ */
+void CTsStorage::ConstructL(const TArray<MTsModel*> &dataProviders)
+{
+    for (TInt iter(0); iter < dataProviders.Count(); ++iter) {
+        mDataProviders.AppendL(dataProviders[iter]);
+        dataProviders[iter]->setObserver(this);
+    }
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Interface implementation
+ * @see MTsDataObserver::DataChanged()
+ */
+void CTsStorage::DataChanged()
+{
+    resetModel();
+}
+
+// -----------------------------------------------------------------------------
+TBool CTsStorage::isSupported(TInt function) const
+{
+    return (OpenTaskMessage == function || CloseTaskMessage == function);
+}
+// -----------------------------------------------------------------------------
+void CTsStorage::handleDataL(TInt function, RReadStream& dataStream)
+{
+    if(!isSupported(function)) {
+        User::Leave(KErrCorrupt);
+    }
+    CTsModelItemKeyMsg* msg = CTsModelItemKeyMsg::NewLC(dataStream);
+    OpenTaskMessage == function ? launchL(msg->key()) : closeL(msg->key());
+    CleanupStack::PopAndDestroy(msg);
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Interface implementation
+ * @see MTsModelObserver::dataChanged(MTsModel &)
+ */
+void CTsStorage::dataChanged(MTsModel &/*model*/)
+{
+    resetModel();
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Interface implementation
+ * @see MTsModelObserver::dataChanged(const TTsModelItem &)
+ */
+void CTsStorage::dataChanged(const TTsModelItem &/*item*/)
+{
+    resetModel();
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Interface implementation
+ * @see MTsModel::count()
+ */
+TInt CTsStorage::count() const
+{
+    return mData.Count();
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Interface implementation
+ * @see MTsModel::setObserver(MTsModelObserver *)
+ */
+void CTsStorage::setObserver(MTsModelObserver *observer) 
+{
+    mDataObserver = observer;
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Interface implementation
+ * @see MTsModel::displayNameL(TInt)
+ */
+const TDesC& CTsStorage::displayNameL(TInt offset) const 
+{
+    return mData[offset].displayNameL();
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Interface implementation
+ * @see MTsModel::iconHandleL(TInt)
+ */
+TInt CTsStorage::iconHandleL(TInt offset) const 
+{
+    return mData[offset].iconHandleL();
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Interface implementation
+ * @see MTsModel::timestampL(TInt)
+ */
+TTime CTsStorage::timestampL(TInt offset) const 
+{
+    return mData[offset].timestampL();
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Interface implementation
+ * @see MTsModel::keyL(TInt)
+ */
+TTsModelItemKey CTsStorage::keyL(TInt offset) const 
+{
+    return mData[offset].keyL();
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Interface implementation
+ * @see MTsModel::isActiveL(TInt)
+ */
+TBool CTsStorage::isActiveL(TInt offset) const 
+{
+    return mData[offset].isActiveL();
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Interface implementation
+ * @see MTsModel::isClosableL(TInt)
+ */
+TBool CTsStorage::isClosableL(TInt offset) const 
+{
+    return mData[offset].isClosableL();
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Interface implementation
+ * @see MTsModel::closeL(TTsModelItemKey)
+ */
+TBool CTsStorage::closeL(TTsModelItemKey key) const 
+{
+    return findL(key).closeL();
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Interface implementation
+ * @see MTsModel::launchL(TTsModelItemKey)
+ */
+TBool CTsStorage::launchL(TTsModelItemKey key) const 
+{
+    return findL(key).launchL(); 
+}
+
+
+// -----------------------------------------------------------------------------
+TTsModelItem CTsStorage::findL(TTsModelItemKey key) const
+{
+    for(TInt offset(0); offset < mData.Count(); ++offset) {
+        if(mData[offset].keyL() == key){
+            return mData[offset];
+        }
+    }
+    User::Leave(KErrNotFound);
+    return itemL(0);//just avoid compilation warnings
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Retrieve shallow copy of data from known data providers and sort entries
+ */
+TInt CTsStorage::resetModel()
+{
+    TRAPD(errNo, resetModelL());
+    return errNo;
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Retrieve shallow copy of data from known data providers and sort entries
+ */
+void CTsStorage::resetModelL() 
+{
+    mData.Reset();
+    for (TInt iter(0); iter < mDataProviders.Count(); ++iter) {
+        pullDataL(*(mDataProviders[iter]));
+    }
+    reorderDataL();
+    if (0 != mDataObserver) {
+        mDataObserver->dataChanged(*this);
+    }
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Retrieve shallow copy of data from source model
+ * @param src - source model 
+ */
+void CTsStorage::pullDataL(const MTsModel& src)
+{
+    for (TInt iter(0); iter < src.count(); ++iter) {
+        mData.AppendL(src.itemL(iter));
+    }
+}
+
+// -----------------------------------------------------------------------------
+/**
+ * Sort internal data model 
+ */
+void CTsStorage::reorderDataL()
+{
+    for (TInt prev(0); prev < mData.Count(); ++prev) {
+        for(TInt next(prev + 1); next < mData.Count(); ++next) {
+            const TTsModelItem prevItem(mData[prev]), nextItem(mData[next]);
+            if(prevItem.timestampL() < nextItem.timestampL()) {
+                mData.Remove(prev);
+                mData.InsertL(nextItem, prev);
+                
+                mData.Remove(next);
+                mData.InsertL(prevItem, next);
+            }
+        }
+    }
+}
+