taskswitcher/server/src/tsstorage.cpp
changeset 121 0b3699f6c654
child 119 50e220be30d1
equal deleted inserted replaced
115:3ab5c078b490 121:0b3699f6c654
       
     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 aDataProviders - list of data providers
       
    24  * @return storage instane
       
    25  */
       
    26 CTsStorage* CTsStorage::NewL(const TArray<MTsModel*> &aDataProviders)
       
    27     {
       
    28     CTsStorage* self = new(ELeave) CTsStorage();
       
    29     CleanupStack::PushL( self );
       
    30     self->ConstructL( aDataProviders );
       
    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 < iDataProviders.Count(); ++iter )
       
    42         {
       
    43         iDataProviders[iter]->SetObserver( 0 );
       
    44         }
       
    45     iData.Close();
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 /**
       
    50  * First phase construction.
       
    51  */
       
    52 CTsStorage::CTsStorage()
       
    53     {
       
    54     //No implementation required
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 /**
       
    59  * Second phase construction. Function make subscribtion for data changes notifications
       
    60  * @param aDataProviders - list of data providers
       
    61  */
       
    62 void CTsStorage::ConstructL( const TArray<MTsModel*> &aDataProviders )
       
    63     {
       
    64     for (TInt iter(0); iter < aDataProviders.Count(); ++iter)
       
    65         {
       
    66         iDataProviders.AppendL( aDataProviders[iter] );
       
    67         aDataProviders[iter]->SetObserver( this );
       
    68         }
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 /**
       
    73  * Interface implementation
       
    74  * @see MTsDataObserver::DataChanged()
       
    75  */
       
    76 void CTsStorage::DataChanged()
       
    77     {
       
    78     ResetModel();
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 TBool CTsStorage::IsSupported( TInt aFunction ) const
       
    83     {
       
    84     return ( OpenTaskMessage == aFunction || CloseTaskMessage == aFunction );
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 void CTsStorage::HandleDataL( TInt aFunction, RReadStream& aDataStream )
       
    89     {
       
    90     if( !IsSupported(aFunction) ) 
       
    91         {
       
    92         User::Leave( KErrCorrupt );
       
    93         }
       
    94     CTsModelItemKeyMsg* msg = CTsModelItemKeyMsg::NewLC( aDataStream );
       
    95     OpenTaskMessage == aFunction ? LaunchL( msg->Key() ) : CloseL( msg->Key() );
       
    96     CleanupStack::PopAndDestroy( msg );
       
    97     }
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 /**
       
   101  * Interface implementation
       
   102  * @see MTsModelObserver::dataChanged(MTsModel &)
       
   103  */
       
   104 void CTsStorage::DataChanged(MTsModel &/*model*/)
       
   105     {
       
   106     ResetModel();
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 /**
       
   111  * Interface implementation
       
   112  * @see MTsModel::count()
       
   113  */
       
   114 TInt CTsStorage::Count() const
       
   115     {
       
   116     return iData.Count();
       
   117     }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 /**
       
   121  * Interface implementation
       
   122  * @see MTsModel::SetObserver(MTsModelObserver *)
       
   123  */
       
   124 void CTsStorage::SetObserver( MTsModelObserver* aObserver ) 
       
   125     {
       
   126     iDataObserver = aObserver;
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 /**
       
   131  * Interface implementation
       
   132  * @see MTsModel::DisplayNameL(TInt)
       
   133  */
       
   134 const TDesC& CTsStorage::DisplayNameL( TInt aOffset ) const 
       
   135     {
       
   136     return iData[aOffset].DisplayNameL();
       
   137     }
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 /**
       
   141  * Interface implementation
       
   142  * @see MTsModel::IconHandleL(TInt)
       
   143  */
       
   144 TInt CTsStorage::IconHandleL( TInt aOffset ) const 
       
   145     {
       
   146     return iData[aOffset].IconHandleL();
       
   147     }
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 /**
       
   151  * Interface implementation
       
   152  * @see MTsModel::TimestampL(TInt)
       
   153  */
       
   154 TTime CTsStorage::TimestampL( TInt aOffset ) const 
       
   155     {
       
   156     return iData[aOffset].TimestampL();
       
   157     }
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 /**
       
   161  * Interface implementation
       
   162  * @see MTsModel::TimestampUpdateL(TInt)
       
   163  */
       
   164 TTime CTsStorage::TimestampUpdateL( TInt offset ) const 
       
   165 {
       
   166     return iData[offset].TimestampUpdateL();
       
   167 }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 /**
       
   171  * Interface implementation
       
   172  * @see MTsModel::KeyL(TInt)
       
   173  */
       
   174 TTsModelItemKey CTsStorage::KeyL( TInt aoffset ) const 
       
   175     {
       
   176     return iData[aoffset].KeyL();
       
   177     }
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 /**
       
   181  * Interface implementation
       
   182  * @see MTsModel::IsActiveL(TInt)
       
   183  */
       
   184 TBool CTsStorage::IsActiveL( TInt aOffset ) const 
       
   185     {
       
   186     return iData[aOffset].IsActiveL();
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 /**
       
   191  * Interface implementation
       
   192  * @see MTsModel::IsClosableL(TInt)
       
   193  */
       
   194 TBool CTsStorage::IsClosableL( TInt aOffset ) const 
       
   195     {
       
   196     return iData[aOffset].IsClosableL();
       
   197     }
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 /**
       
   201  * Interface implementation
       
   202  * @see MTsModel::CloseL(TTsModelItemKey)
       
   203  */
       
   204 TBool CTsStorage::CloseL( TTsModelItemKey aKey ) const 
       
   205     {
       
   206     return FindL(aKey).CloseL();
       
   207     }
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 /**
       
   211  * Interface implementation
       
   212  * @see MTsModel::launchL(TTsModelItemKey)
       
   213  */
       
   214 TBool CTsStorage::LaunchL(TTsModelItemKey aKey) const 
       
   215     {
       
   216     return FindL(aKey).LaunchL(); 
       
   217     }
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 TTsModelItem CTsStorage::FindL( TTsModelItemKey aKey ) const
       
   221     {
       
   222     for( TInt offset(0); offset < iData.Count(); ++offset ) 
       
   223         {
       
   224         if( iData[offset].KeyL() == aKey )
       
   225             {
       
   226             return iData[offset];
       
   227             }
       
   228         }
       
   229     User::Leave(KErrNotFound);
       
   230     return ItemL(0);//just avoid compilation warnings
       
   231     }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 /**
       
   235  * Retrieve shallow copy of data from known data providers and sort entries
       
   236  */
       
   237 TInt CTsStorage::ResetModel()
       
   238     {
       
   239     TRAPD(errNo, ResetModelL());
       
   240     return errNo;
       
   241     }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 /**
       
   245  * Retrieve shallow copy of data from known data providers and sort entries
       
   246  */
       
   247 void CTsStorage::ResetModelL() 
       
   248     {
       
   249     iData.Reset();
       
   250     for(TInt iter(0); iter < iDataProviders.Count(); ++iter )
       
   251         {
       
   252         PullDataL(*(iDataProviders[iter]));
       
   253         }
       
   254     ReorderDataL();
       
   255     if (0 != iDataObserver)
       
   256         {
       
   257         iDataObserver->DataChanged(*this);
       
   258         }
       
   259     }
       
   260 
       
   261 // -----------------------------------------------------------------------------
       
   262 /**
       
   263  * Retrieve shallow copy of data from source model
       
   264  * @param aSrc - source model 
       
   265  */
       
   266 void CTsStorage::PullDataL( const MTsModel& aSrc )
       
   267     {
       
   268     for( TInt iter(0); iter < aSrc.Count(); ++iter )
       
   269         {
       
   270         iData.AppendL(aSrc.ItemL(iter));
       
   271         }
       
   272     }
       
   273 
       
   274 // -----------------------------------------------------------------------------
       
   275 /**
       
   276  * Sort internal data model 
       
   277  */
       
   278 void CTsStorage::ReorderDataL()
       
   279     {
       
   280     for( TInt prev(0); prev < iData.Count(); ++prev ) 
       
   281         {
       
   282         for( TInt next(prev + 1); next < iData.Count(); ++next )
       
   283             {
       
   284             const TTsModelItem prevItem(iData[prev]), nextItem(iData[next]);
       
   285             if( prevItem.TimestampL() < nextItem.TimestampL() )
       
   286                 {
       
   287                 iData.Remove(prev);
       
   288                 iData.InsertL(nextItem, prev);
       
   289                 
       
   290                 iData.Remove(next);
       
   291                 iData.InsertL(prevItem, next);
       
   292                 }
       
   293             }
       
   294         }
       
   295     }