taskswitcher/server/src/tsservice.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 <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 *model, TInt offset)
       
    29 {
       
    30     QList<QVariantHash> items;
       
    31     QMetaObject::invokeMethod(model, "taskList", Q_RETURN_ARG(QList<QVariantHash>, items));
       
    32     
       
    33     if (offset >= items.count()) {
       
    34         User::Leave(KErrCorrupt);
       
    35     }
       
    36     
       
    37     return items.at(offset);
       
    38 }
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 CTsService* CTsService::NewLC(QObject* model)
       
    42 {
       
    43     CTsService *self = new (ELeave)CTsService();
       
    44     CleanupStack::PushL(self);
       
    45     self->ConstructL(model);
       
    46     return self;
       
    47 }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 CTsService::CTsService()
       
    51 {
       
    52     //No implementation required
       
    53 }
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 void CTsService::ConstructL(QObject* model)
       
    57 {
       
    58     mModel = model;    
       
    59     User::LeaveIfNull(mModel);
       
    60     // @todo: add checking for all necessary methods
       
    61     
       
    62     mServiceObserver = new TsServiceObserver(*this);
       
    63     User::LeaveIfNull(mServiceObserver);
       
    64     QObject::connect(mModel, 
       
    65                      SIGNAL(dataChanged()),
       
    66                      mServiceObserver,
       
    67                      SLOT(dataChanged()));
       
    68 }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 CTsService::~CTsService()
       
    72 {
       
    73     delete mModel;
       
    74     delete mServiceObserver;
       
    75     delete mBuffer;
       
    76 }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 void CTsService::DataChanged()
       
    80 {
       
    81     if (0 != mModelObserver) {
       
    82         mModelObserver->dataChanged(*this);
       
    83     }
       
    84 }
       
    85 // -----------------------------------------------------------------------------
       
    86 TInt CTsService::count() const
       
    87 {
       
    88     QList<QVariantHash> items;
       
    89     QMetaObject::invokeMethod(mModel, "taskList", Q_RETURN_ARG(QList<QVariantHash>, items));
       
    90     return items.count();
       
    91 }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 void CTsService::setObserver(MTsModelObserver *observer)
       
    95 {
       
    96     mModelObserver = observer;
       
    97 }
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 const TDesC& CTsService::displayNameL(TInt offset) const
       
   101 {
       
   102     return stringValueL(offset, "TaskName");
       
   103 }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 TInt CTsService::iconHandleL(TInt offset) const
       
   107 {
       
   108     return intValueL(offset, "TaskScreenshot");
       
   109 }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 TTime CTsService::timestampL(TInt offset) const
       
   113 {
       
   114     return timeValueL(offset, "TaskTimestamp");
       
   115 }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 TTsModelItemKey CTsService::keyL(TInt offset) const
       
   119 {
       
   120     return TTsModelItemKey(intValueL(offset, "TaskId"), reinterpret_cast<TInt>(this));
       
   121 }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 TBool CTsService::isActiveL(TInt offset) const
       
   125 {
       
   126     return intValueL(offset, "TaskIsRunning");
       
   127 }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 TBool CTsService::isClosableL(TInt offset) const
       
   131 {
       
   132     return intValueL(offset, "TaskCanBeClosed");
       
   133 }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 TBool CTsService::closeL(TTsModelItemKey key) const
       
   137 {
       
   138     bool result(false);
       
   139     QMetaObject::invokeMethod(mModel, "closeTask", Q_RETURN_ARG(bool, result), Q_ARG(QVariant, key.key()));
       
   140     return result;
       
   141 }
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 TBool CTsService::launchL(TTsModelItemKey key) const
       
   145 {
       
   146     bool result(false);
       
   147     QMetaObject::invokeMethod(mModel, "openTask", Q_RETURN_ARG(bool, result), Q_ARG(QVariant, key.key()));
       
   148     return result;
       
   149 }
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 TInt CTsService::intValueL(TInt offset, const char *key) const
       
   153 {
       
   154     return valueL(mModel, offset).value(key).toInt();
       
   155 }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 TTime CTsService::timeValueL(TInt offset, const char *key) const
       
   159 {
       
   160     // Conversion between TTime which counts from year 0, and QDateTime which uses unix epoch (1st Jan 1970)
       
   161     return TTime(_L("19700000:")) + TTimeIntervalSeconds(valueL(mModel, offset).value(key).toDateTime().toTime_t());
       
   162 }
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 const TDesC& CTsService::stringValueL(TInt offset, const char *key) const
       
   166 {
       
   167     delete mBuffer;
       
   168     const_cast<CTsService *>(this)->mBuffer = 0;
       
   169     
       
   170     const QVariantHash item(valueL(mModel, offset));
       
   171     QT_TRYCATCH_LEAVING(
       
   172     const_cast<CTsService *>(this)->mBuffer = 
       
   173     XQConversions::qStringToS60Desc(item.value(key).toString()));
       
   174     return *mBuffer;
       
   175 }