taskswitcher/server/src/tsservice.cpp
author hgs
Mon, 13 Sep 2010 13:26:33 +0300
changeset 116 305818acdca4
child 119 50e220be30d1
permissions -rw-r--r--
201036
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
116
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* Initial Contributors:
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
*
hgs
parents:
diff changeset
    12
* Contributors:
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* Description:
hgs
parents:
diff changeset
    15
*
hgs
parents:
diff changeset
    16
*/
hgs
parents:
diff changeset
    17
#include <QObject>
hgs
parents:
diff changeset
    18
#include <QDateTime>
hgs
parents:
diff changeset
    19
#include <QVariantHash>
hgs
parents:
diff changeset
    20
hgs
parents:
diff changeset
    21
#include <xqconversions.h>
hgs
parents:
diff changeset
    22
hgs
parents:
diff changeset
    23
#include "tsservice.h"
hgs
parents:
diff changeset
    24
#include "tsmodelobserver.h"
hgs
parents:
diff changeset
    25
#include "tsserviceobserver.h"
hgs
parents:
diff changeset
    26
hgs
parents:
diff changeset
    27
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    28
LOCAL_C QVariantHash valueL( QObject *aModel, TInt aOffset )
hgs
parents:
diff changeset
    29
    {
hgs
parents:
diff changeset
    30
    QList<QVariantHash> items;
hgs
parents:
diff changeset
    31
    QMetaObject::invokeMethod( aModel, 
hgs
parents:
diff changeset
    32
                               "taskList", 
hgs
parents:
diff changeset
    33
                               Q_RETURN_ARG(QList<QVariantHash>, 
hgs
parents:
diff changeset
    34
                               items ) );
hgs
parents:
diff changeset
    35
    if( aOffset >= items.count() ) 
hgs
parents:
diff changeset
    36
        {
hgs
parents:
diff changeset
    37
        User::Leave(KErrCorrupt);
hgs
parents:
diff changeset
    38
        }
hgs
parents:
diff changeset
    39
    return items.at(aOffset);
hgs
parents:
diff changeset
    40
    }
hgs
parents:
diff changeset
    41
hgs
parents:
diff changeset
    42
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    43
CTsService* CTsService::NewLC(QObject* model)
hgs
parents:
diff changeset
    44
    {
hgs
parents:
diff changeset
    45
    CTsService *self = new (ELeave)CTsService();
hgs
parents:
diff changeset
    46
    CleanupStack::PushL(self);
hgs
parents:
diff changeset
    47
    self->ConstructL(model);
hgs
parents:
diff changeset
    48
    return self;
hgs
parents:
diff changeset
    49
    }
hgs
parents:
diff changeset
    50
hgs
parents:
diff changeset
    51
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    52
CTsService::CTsService()
hgs
parents:
diff changeset
    53
    {
hgs
parents:
diff changeset
    54
    //No implementation required
hgs
parents:
diff changeset
    55
    }
hgs
parents:
diff changeset
    56
hgs
parents:
diff changeset
    57
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    58
void CTsService::ConstructL( QObject* model )
hgs
parents:
diff changeset
    59
    {
hgs
parents:
diff changeset
    60
    iModel = model;
hgs
parents:
diff changeset
    61
    User::LeaveIfNull(iModel);
hgs
parents:
diff changeset
    62
    // @todo: add checking for all necessary methods
hgs
parents:
diff changeset
    63
    
hgs
parents:
diff changeset
    64
    iServiceObserver = new TsServiceObserver(*this);
hgs
parents:
diff changeset
    65
    User::LeaveIfNull(iServiceObserver);
hgs
parents:
diff changeset
    66
    QObject::connect(iModel, 
hgs
parents:
diff changeset
    67
                     SIGNAL(dataChanged()),
hgs
parents:
diff changeset
    68
                     iServiceObserver,
hgs
parents:
diff changeset
    69
                     SLOT(dataChanged()));
hgs
parents:
diff changeset
    70
    }
hgs
parents:
diff changeset
    71
hgs
parents:
diff changeset
    72
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    73
CTsService::~CTsService()
hgs
parents:
diff changeset
    74
    {
hgs
parents:
diff changeset
    75
    delete iModel;
hgs
parents:
diff changeset
    76
    delete iServiceObserver;
hgs
parents:
diff changeset
    77
    delete iBuffer;
hgs
parents:
diff changeset
    78
    }
hgs
parents:
diff changeset
    79
hgs
parents:
diff changeset
    80
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    81
void CTsService::DataChanged()
hgs
parents:
diff changeset
    82
    {
hgs
parents:
diff changeset
    83
    if(0 != iModelObserver) 
hgs
parents:
diff changeset
    84
        {
hgs
parents:
diff changeset
    85
        iModelObserver->DataChanged(*this);
hgs
parents:
diff changeset
    86
        }
hgs
parents:
diff changeset
    87
    }
hgs
parents:
diff changeset
    88
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    89
TInt CTsService::Count() const
hgs
parents:
diff changeset
    90
    {
hgs
parents:
diff changeset
    91
    QList<QVariantHash> items;
hgs
parents:
diff changeset
    92
    QMetaObject::invokeMethod(iModel, 
hgs
parents:
diff changeset
    93
                              "taskList", 
hgs
parents:
diff changeset
    94
                              Q_RETURN_ARG(QList<QVariantHash>, items));
hgs
parents:
diff changeset
    95
    return items.count();
hgs
parents:
diff changeset
    96
    }
hgs
parents:
diff changeset
    97
hgs
parents:
diff changeset
    98
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    99
void CTsService::SetObserver( MTsModelObserver *aObserver )
hgs
parents:
diff changeset
   100
    {
hgs
parents:
diff changeset
   101
    iModelObserver = aObserver;
hgs
parents:
diff changeset
   102
    }
hgs
parents:
diff changeset
   103
hgs
parents:
diff changeset
   104
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   105
const TDesC& CTsService::DisplayNameL( TInt aOffset ) const
hgs
parents:
diff changeset
   106
    {
hgs
parents:
diff changeset
   107
    return StringValueL( aOffset, "TaskName" );
hgs
parents:
diff changeset
   108
    }
hgs
parents:
diff changeset
   109
hgs
parents:
diff changeset
   110
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   111
TInt CTsService::IconHandleL( TInt aOffset ) const
hgs
parents:
diff changeset
   112
    {
hgs
parents:
diff changeset
   113
    return IntValueL( aOffset, "TaskScreenshot" );
hgs
parents:
diff changeset
   114
    }
hgs
parents:
diff changeset
   115
hgs
parents:
diff changeset
   116
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   117
TTime CTsService::TimestampL( TInt aOffset ) const
hgs
parents:
diff changeset
   118
    {
hgs
parents:
diff changeset
   119
    return TimeValueL( aOffset, "TaskTimestamp" );
hgs
parents:
diff changeset
   120
    }
hgs
parents:
diff changeset
   121
hgs
parents:
diff changeset
   122
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   123
TTime CTsService::TimestampUpdateL(TInt offset) const
hgs
parents:
diff changeset
   124
{
hgs
parents:
diff changeset
   125
    return TimeValueL(offset, "TaskUpdateTimestamp");
hgs
parents:
diff changeset
   126
}
hgs
parents:
diff changeset
   127
hgs
parents:
diff changeset
   128
TTsModelItemKey CTsService::KeyL( TInt aOffset ) const
hgs
parents:
diff changeset
   129
    {
hgs
parents:
diff changeset
   130
    return TTsModelItemKey( IntValueL( aOffset, "TaskId" ), 
hgs
parents:
diff changeset
   131
                            reinterpret_cast<TInt>( this ) );
hgs
parents:
diff changeset
   132
    }
hgs
parents:
diff changeset
   133
hgs
parents:
diff changeset
   134
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   135
TBool CTsService::IsActiveL( TInt aOffset ) const
hgs
parents:
diff changeset
   136
    {
hgs
parents:
diff changeset
   137
    return IntValueL( aOffset, "TaskIsRunning" );
hgs
parents:
diff changeset
   138
    }
hgs
parents:
diff changeset
   139
hgs
parents:
diff changeset
   140
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   141
TBool CTsService::IsClosableL( TInt aOffset ) const
hgs
parents:
diff changeset
   142
    {
hgs
parents:
diff changeset
   143
    return IntValueL( aOffset, "TaskCanBeClosed" );
hgs
parents:
diff changeset
   144
    }
hgs
parents:
diff changeset
   145
hgs
parents:
diff changeset
   146
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   147
TBool CTsService::CloseL( TTsModelItemKey aKey ) const
hgs
parents:
diff changeset
   148
    {
hgs
parents:
diff changeset
   149
    bool result(false);
hgs
parents:
diff changeset
   150
    QMetaObject::invokeMethod( iModel, 
hgs
parents:
diff changeset
   151
                               "closeTask", 
hgs
parents:
diff changeset
   152
                               Q_RETURN_ARG(bool, result), 
hgs
parents:
diff changeset
   153
                               Q_ARG(QVariant, aKey.Key() ) );
hgs
parents:
diff changeset
   154
    return result;
hgs
parents:
diff changeset
   155
    }
hgs
parents:
diff changeset
   156
hgs
parents:
diff changeset
   157
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   158
TBool CTsService::LaunchL( TTsModelItemKey aKey ) const
hgs
parents:
diff changeset
   159
    {
hgs
parents:
diff changeset
   160
    bool result(false);
hgs
parents:
diff changeset
   161
    QMetaObject::invokeMethod( iModel, 
hgs
parents:
diff changeset
   162
                               "openTask", 
hgs
parents:
diff changeset
   163
                               Q_RETURN_ARG(bool, result), 
hgs
parents:
diff changeset
   164
                               Q_ARG(QVariant, aKey.Key() ) );
hgs
parents:
diff changeset
   165
    return result;
hgs
parents:
diff changeset
   166
    }
hgs
parents:
diff changeset
   167
hgs
parents:
diff changeset
   168
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   169
TInt CTsService::IntValueL( TInt aOffset, const char* aKey) const
hgs
parents:
diff changeset
   170
    {
hgs
parents:
diff changeset
   171
    return valueL(iModel, aOffset).value( aKey ).toInt();
hgs
parents:
diff changeset
   172
    }
hgs
parents:
diff changeset
   173
hgs
parents:
diff changeset
   174
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   175
TTime CTsService::TimeValueL(TInt aOffset, const char* aKey) const
hgs
parents:
diff changeset
   176
    {
hgs
parents:
diff changeset
   177
    // Conversion between TTime which counts from year 0, and QDateTime which uses unix epoch (1st Jan 1970)
hgs
parents:
diff changeset
   178
    QDateTime timestamp = valueL( iModel, aOffset ).value( aKey ).toDateTime();
hgs
parents:
diff changeset
   179
    
hgs
parents:
diff changeset
   180
    return TTime( _L( "19700000:" ) ) + TTimeIntervalSeconds( timestamp.toTime_t() ) +
hgs
parents:
diff changeset
   181
                         TTimeIntervalMicroSeconds( timestamp.time().msec() * 1000 );
hgs
parents:
diff changeset
   182
    }
hgs
parents:
diff changeset
   183
hgs
parents:
diff changeset
   184
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   185
const TDesC& CTsService::StringValueL( TInt aOffset, const char* aKey ) const
hgs
parents:
diff changeset
   186
    {
hgs
parents:
diff changeset
   187
    delete iBuffer;
hgs
parents:
diff changeset
   188
    const_cast<CTsService *>(this)->iBuffer = 0;
hgs
parents:
diff changeset
   189
    
hgs
parents:
diff changeset
   190
    const QVariantHash item(valueL(iModel, aOffset));
hgs
parents:
diff changeset
   191
    QT_TRYCATCH_LEAVING(
hgs
parents:
diff changeset
   192
    const_cast<CTsService *>(this)->iBuffer = 
hgs
parents:
diff changeset
   193
    XQConversions::qStringToS60Desc(item.value(aKey).toString()));
hgs
parents:
diff changeset
   194
    return *iBuffer;
hgs
parents:
diff changeset
   195
    }