taskswitcher/server/src/tsservice.cpp
changeset 116 305818acdca4
child 119 50e220be30d1
equal deleted inserted replaced
112:dbfb5e38438b 116:305818acdca4
       
     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 <QObject>
       
    18 #include <QDateTime>
       
    19 #include <QVariantHash>
       
    20 
       
    21 #include <xqconversions.h>
       
    22 
       
    23 #include "tsservice.h"
       
    24 #include "tsmodelobserver.h"
       
    25 #include "tsserviceobserver.h"
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 LOCAL_C QVariantHash valueL( QObject *aModel, TInt aOffset )
       
    29     {
       
    30     QList<QVariantHash> items;
       
    31     QMetaObject::invokeMethod( aModel, 
       
    32                                "taskList", 
       
    33                                Q_RETURN_ARG(QList<QVariantHash>, 
       
    34                                items ) );
       
    35     if( aOffset >= items.count() ) 
       
    36         {
       
    37         User::Leave(KErrCorrupt);
       
    38         }
       
    39     return items.at(aOffset);
       
    40     }
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 CTsService* CTsService::NewLC(QObject* model)
       
    44     {
       
    45     CTsService *self = new (ELeave)CTsService();
       
    46     CleanupStack::PushL(self);
       
    47     self->ConstructL(model);
       
    48     return self;
       
    49     }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 CTsService::CTsService()
       
    53     {
       
    54     //No implementation required
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 void CTsService::ConstructL( QObject* model )
       
    59     {
       
    60     iModel = model;
       
    61     User::LeaveIfNull(iModel);
       
    62     // @todo: add checking for all necessary methods
       
    63     
       
    64     iServiceObserver = new TsServiceObserver(*this);
       
    65     User::LeaveIfNull(iServiceObserver);
       
    66     QObject::connect(iModel, 
       
    67                      SIGNAL(dataChanged()),
       
    68                      iServiceObserver,
       
    69                      SLOT(dataChanged()));
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 CTsService::~CTsService()
       
    74     {
       
    75     delete iModel;
       
    76     delete iServiceObserver;
       
    77     delete iBuffer;
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 void CTsService::DataChanged()
       
    82     {
       
    83     if(0 != iModelObserver) 
       
    84         {
       
    85         iModelObserver->DataChanged(*this);
       
    86         }
       
    87     }
       
    88 // -----------------------------------------------------------------------------
       
    89 TInt CTsService::Count() const
       
    90     {
       
    91     QList<QVariantHash> items;
       
    92     QMetaObject::invokeMethod(iModel, 
       
    93                               "taskList", 
       
    94                               Q_RETURN_ARG(QList<QVariantHash>, items));
       
    95     return items.count();
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 void CTsService::SetObserver( MTsModelObserver *aObserver )
       
   100     {
       
   101     iModelObserver = aObserver;
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 const TDesC& CTsService::DisplayNameL( TInt aOffset ) const
       
   106     {
       
   107     return StringValueL( aOffset, "TaskName" );
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 TInt CTsService::IconHandleL( TInt aOffset ) const
       
   112     {
       
   113     return IntValueL( aOffset, "TaskScreenshot" );
       
   114     }
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 TTime CTsService::TimestampL( TInt aOffset ) const
       
   118     {
       
   119     return TimeValueL( aOffset, "TaskTimestamp" );
       
   120     }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 TTime CTsService::TimestampUpdateL(TInt offset) const
       
   124 {
       
   125     return TimeValueL(offset, "TaskUpdateTimestamp");
       
   126 }
       
   127 
       
   128 TTsModelItemKey CTsService::KeyL( TInt aOffset ) const
       
   129     {
       
   130     return TTsModelItemKey( IntValueL( aOffset, "TaskId" ), 
       
   131                             reinterpret_cast<TInt>( this ) );
       
   132     }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 TBool CTsService::IsActiveL( TInt aOffset ) const
       
   136     {
       
   137     return IntValueL( aOffset, "TaskIsRunning" );
       
   138     }
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 TBool CTsService::IsClosableL( TInt aOffset ) const
       
   142     {
       
   143     return IntValueL( aOffset, "TaskCanBeClosed" );
       
   144     }
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 TBool CTsService::CloseL( TTsModelItemKey aKey ) const
       
   148     {
       
   149     bool result(false);
       
   150     QMetaObject::invokeMethod( iModel, 
       
   151                                "closeTask", 
       
   152                                Q_RETURN_ARG(bool, result), 
       
   153                                Q_ARG(QVariant, aKey.Key() ) );
       
   154     return result;
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 TBool CTsService::LaunchL( TTsModelItemKey aKey ) const
       
   159     {
       
   160     bool result(false);
       
   161     QMetaObject::invokeMethod( iModel, 
       
   162                                "openTask", 
       
   163                                Q_RETURN_ARG(bool, result), 
       
   164                                Q_ARG(QVariant, aKey.Key() ) );
       
   165     return result;
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 TInt CTsService::IntValueL( TInt aOffset, const char* aKey) const
       
   170     {
       
   171     return valueL(iModel, aOffset).value( aKey ).toInt();
       
   172     }
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 TTime CTsService::TimeValueL(TInt aOffset, const char* aKey) const
       
   176     {
       
   177     // Conversion between TTime which counts from year 0, and QDateTime which uses unix epoch (1st Jan 1970)
       
   178     QDateTime timestamp = valueL( iModel, aOffset ).value( aKey ).toDateTime();
       
   179     
       
   180     return TTime( _L( "19700000:" ) ) + TTimeIntervalSeconds( timestamp.toTime_t() ) +
       
   181                          TTimeIntervalMicroSeconds( timestamp.time().msec() * 1000 );
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 const TDesC& CTsService::StringValueL( TInt aOffset, const char* aKey ) const
       
   186     {
       
   187     delete iBuffer;
       
   188     const_cast<CTsService *>(this)->iBuffer = 0;
       
   189     
       
   190     const QVariantHash item(valueL(iModel, aOffset));
       
   191     QT_TRYCATCH_LEAVING(
       
   192     const_cast<CTsService *>(this)->iBuffer = 
       
   193     XQConversions::qStringToS60Desc(item.value(aKey).toString()));
       
   194     return *iBuffer;
       
   195     }