taskswitcher/server/src/tsstorage.cpp
changeset 117 c63ee96dbe5f
equal deleted inserted replaced
115:3ab5c078b490 117:c63ee96dbe5f
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 #include <tstaskmonitorglobals.h>
       
    18 #include "tsstorage.h"
       
    19 #include "tsmodelitemkeymsg.h"
       
    20 // -----------------------------------------------------------------------------
       
    21 /**
       
    22  * Two phase construction. Create and initialize storage instance.
       
    23  * @param dataProviders - list of data providers
       
    24  * @return storage instane
       
    25  */
       
    26 CTsStorage* CTsStorage::NewL(const TArray<MTsModel*> &dataProviders)
       
    27 {
       
    28     CTsStorage* self = new(ELeave) CTsStorage();
       
    29     CleanupStack::PushL(self);
       
    30     self->ConstructL(dataProviders);
       
    31     CleanupStack::Pop(self);
       
    32     return self;
       
    33 }
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 /**
       
    37  * Destructor. Function cancel subscribtion for data change notyfications
       
    38  */
       
    39 CTsStorage::~CTsStorage()
       
    40 {
       
    41     for (TInt iter(0); iter < mDataProviders.Count(); ++iter) {
       
    42         mDataProviders[iter]->setObserver(0);
       
    43     }
       
    44     mData.Close();
       
    45 }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 /**
       
    49  * First phase construction.
       
    50  */
       
    51 CTsStorage::CTsStorage()
       
    52 {
       
    53     //No implementation required
       
    54 }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 /**
       
    58  * Second phase construction. Function make subscribtion for data changes notifications
       
    59  * @param dataProviders - list of data providers
       
    60  */
       
    61 void CTsStorage::ConstructL(const TArray<MTsModel*> &dataProviders)
       
    62 {
       
    63     for (TInt iter(0); iter < dataProviders.Count(); ++iter) {
       
    64         mDataProviders.AppendL(dataProviders[iter]);
       
    65         dataProviders[iter]->setObserver(this);
       
    66     }
       
    67 }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 /**
       
    71  * Interface implementation
       
    72  * @see MTsDataObserver::DataChanged()
       
    73  */
       
    74 void CTsStorage::DataChanged()
       
    75 {
       
    76     resetModel();
       
    77 }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 TBool CTsStorage::isSupported(TInt function) const
       
    81 {
       
    82     return (OpenTaskMessage == function || CloseTaskMessage == function);
       
    83 }
       
    84 // -----------------------------------------------------------------------------
       
    85 void CTsStorage::handleDataL(TInt function, RReadStream& dataStream)
       
    86 {
       
    87     if(!isSupported(function)) {
       
    88         User::Leave(KErrCorrupt);
       
    89     }
       
    90     CTsModelItemKeyMsg* msg = CTsModelItemKeyMsg::NewLC(dataStream);
       
    91     OpenTaskMessage == function ? launchL(msg->key()) : closeL(msg->key());
       
    92     CleanupStack::PopAndDestroy(msg);
       
    93 }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 /**
       
    97  * Interface implementation
       
    98  * @see MTsModelObserver::dataChanged(MTsModel &)
       
    99  */
       
   100 void CTsStorage::dataChanged(MTsModel &/*model*/)
       
   101 {
       
   102     resetModel();
       
   103 }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 /**
       
   107  * Interface implementation
       
   108  * @see MTsModelObserver::dataChanged(const TTsModelItem &)
       
   109  */
       
   110 void CTsStorage::dataChanged(const TTsModelItem &/*item*/)
       
   111 {
       
   112     resetModel();
       
   113 }
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 /**
       
   117  * Interface implementation
       
   118  * @see MTsModel::count()
       
   119  */
       
   120 TInt CTsStorage::count() const
       
   121 {
       
   122     return mData.Count();
       
   123 }
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 /**
       
   127  * Interface implementation
       
   128  * @see MTsModel::setObserver(MTsModelObserver *)
       
   129  */
       
   130 void CTsStorage::setObserver(MTsModelObserver *observer) 
       
   131 {
       
   132     mDataObserver = observer;
       
   133 }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 /**
       
   137  * Interface implementation
       
   138  * @see MTsModel::displayNameL(TInt)
       
   139  */
       
   140 const TDesC& CTsStorage::displayNameL(TInt offset) const 
       
   141 {
       
   142     return mData[offset].displayNameL();
       
   143 }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 /**
       
   147  * Interface implementation
       
   148  * @see MTsModel::iconHandleL(TInt)
       
   149  */
       
   150 TInt CTsStorage::iconHandleL(TInt offset) const 
       
   151 {
       
   152     return mData[offset].iconHandleL();
       
   153 }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 /**
       
   157  * Interface implementation
       
   158  * @see MTsModel::timestampL(TInt)
       
   159  */
       
   160 TTime CTsStorage::timestampL(TInt offset) const 
       
   161 {
       
   162     return mData[offset].timestampL();
       
   163 }
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 /**
       
   167  * Interface implementation
       
   168  * @see MTsModel::keyL(TInt)
       
   169  */
       
   170 TTsModelItemKey CTsStorage::keyL(TInt offset) const 
       
   171 {
       
   172     return mData[offset].keyL();
       
   173 }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 /**
       
   177  * Interface implementation
       
   178  * @see MTsModel::isActiveL(TInt)
       
   179  */
       
   180 TBool CTsStorage::isActiveL(TInt offset) const 
       
   181 {
       
   182     return mData[offset].isActiveL();
       
   183 }
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 /**
       
   187  * Interface implementation
       
   188  * @see MTsModel::isClosableL(TInt)
       
   189  */
       
   190 TBool CTsStorage::isClosableL(TInt offset) const 
       
   191 {
       
   192     return mData[offset].isClosableL();
       
   193 }
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 /**
       
   197  * Interface implementation
       
   198  * @see MTsModel::closeL(TTsModelItemKey)
       
   199  */
       
   200 TBool CTsStorage::closeL(TTsModelItemKey key) const 
       
   201 {
       
   202     return findL(key).closeL();
       
   203 }
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 /**
       
   207  * Interface implementation
       
   208  * @see MTsModel::launchL(TTsModelItemKey)
       
   209  */
       
   210 TBool CTsStorage::launchL(TTsModelItemKey key) const 
       
   211 {
       
   212     return findL(key).launchL(); 
       
   213 }
       
   214 
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 TTsModelItem CTsStorage::findL(TTsModelItemKey key) const
       
   218 {
       
   219     for(TInt offset(0); offset < mData.Count(); ++offset) {
       
   220         if(mData[offset].keyL() == key){
       
   221             return mData[offset];
       
   222         }
       
   223     }
       
   224     User::Leave(KErrNotFound);
       
   225     return itemL(0);//just avoid compilation warnings
       
   226 }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 /**
       
   230  * Retrieve shallow copy of data from known data providers and sort entries
       
   231  */
       
   232 TInt CTsStorage::resetModel()
       
   233 {
       
   234     TRAPD(errNo, resetModelL());
       
   235     return errNo;
       
   236 }
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 /**
       
   240  * Retrieve shallow copy of data from known data providers and sort entries
       
   241  */
       
   242 void CTsStorage::resetModelL() 
       
   243 {
       
   244     mData.Reset();
       
   245     for (TInt iter(0); iter < mDataProviders.Count(); ++iter) {
       
   246         pullDataL(*(mDataProviders[iter]));
       
   247     }
       
   248     reorderDataL();
       
   249     if (0 != mDataObserver) {
       
   250         mDataObserver->dataChanged(*this);
       
   251     }
       
   252 }
       
   253 
       
   254 // -----------------------------------------------------------------------------
       
   255 /**
       
   256  * Retrieve shallow copy of data from source model
       
   257  * @param src - source model 
       
   258  */
       
   259 void CTsStorage::pullDataL(const MTsModel& src)
       
   260 {
       
   261     for (TInt iter(0); iter < src.count(); ++iter) {
       
   262         mData.AppendL(src.itemL(iter));
       
   263     }
       
   264 }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 /**
       
   268  * Sort internal data model 
       
   269  */
       
   270 void CTsStorage::reorderDataL()
       
   271 {
       
   272     for (TInt prev(0); prev < mData.Count(); ++prev) {
       
   273         for(TInt next(prev + 1); next < mData.Count(); ++next) {
       
   274             const TTsModelItem prevItem(mData[prev]), nextItem(mData[next]);
       
   275             if(prevItem.timestampL() < nextItem.timestampL()) {
       
   276                 mData.Remove(prev);
       
   277                 mData.InsertL(nextItem, prev);
       
   278                 
       
   279                 mData.Remove(next);
       
   280                 mData.InsertL(prevItem, next);
       
   281             }
       
   282         }
       
   283     }
       
   284 }
       
   285