taskswitcher/server/src/tsservice.cpp
changeset 117 c63ee96dbe5f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/taskswitcher/server/src/tsservice.cpp	Thu Sep 16 12:11:40 2010 +0100
@@ -0,0 +1,175 @@
+/*
+* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
+* All rights reserved.
+* This component and the accompanying materials are made available
+* under the terms of "Eclipse Public License v1.0"
+* which accompanies this distribution, and is available
+* at the URL "http://www.eclipse.org/legal/epl-v10.html".
+*
+* Initial Contributors:
+* Nokia Corporation - initial contribution.
+*
+* Contributors:
+*
+* Description:
+*
+*/
+#include <QObject>
+#include <QDateTime>
+#include <QVariantHash>
+
+#include <xqconversions.h>
+
+#include "tsservice.h"
+#include "tsmodelobserver.h"
+#include "tsserviceobserver.h"
+
+// -----------------------------------------------------------------------------
+LOCAL_C QVariantHash valueL(QObject *model, TInt offset)
+{
+    QList<QVariantHash> items;
+    QMetaObject::invokeMethod(model, "taskList", Q_RETURN_ARG(QList<QVariantHash>, items));
+    
+    if (offset >= items.count()) {
+        User::Leave(KErrCorrupt);
+    }
+    
+    return items.at(offset);
+}
+
+// -----------------------------------------------------------------------------
+CTsService* CTsService::NewLC(QObject* model)
+{
+    CTsService *self = new (ELeave)CTsService();
+    CleanupStack::PushL(self);
+    self->ConstructL(model);
+    return self;
+}
+
+// -----------------------------------------------------------------------------
+CTsService::CTsService()
+{
+    //No implementation required
+}
+
+// -----------------------------------------------------------------------------
+void CTsService::ConstructL(QObject* model)
+{
+    mModel = model;    
+    User::LeaveIfNull(mModel);
+    // @todo: add checking for all necessary methods
+    
+    mServiceObserver = new TsServiceObserver(*this);
+    User::LeaveIfNull(mServiceObserver);
+    QObject::connect(mModel, 
+                     SIGNAL(dataChanged()),
+                     mServiceObserver,
+                     SLOT(dataChanged()));
+}
+
+// -----------------------------------------------------------------------------
+CTsService::~CTsService()
+{
+    delete mModel;
+    delete mServiceObserver;
+    delete mBuffer;
+}
+
+// -----------------------------------------------------------------------------
+void CTsService::DataChanged()
+{
+    if (0 != mModelObserver) {
+        mModelObserver->dataChanged(*this);
+    }
+}
+// -----------------------------------------------------------------------------
+TInt CTsService::count() const
+{
+    QList<QVariantHash> items;
+    QMetaObject::invokeMethod(mModel, "taskList", Q_RETURN_ARG(QList<QVariantHash>, items));
+    return items.count();
+}
+
+// -----------------------------------------------------------------------------
+void CTsService::setObserver(MTsModelObserver *observer)
+{
+    mModelObserver = observer;
+}
+
+// -----------------------------------------------------------------------------
+const TDesC& CTsService::displayNameL(TInt offset) const
+{
+    return stringValueL(offset, "TaskName");
+}
+
+// -----------------------------------------------------------------------------
+TInt CTsService::iconHandleL(TInt offset) const
+{
+    return intValueL(offset, "TaskScreenshot");
+}
+
+// -----------------------------------------------------------------------------
+TTime CTsService::timestampL(TInt offset) const
+{
+    return timeValueL(offset, "TaskTimestamp");
+}
+
+// -----------------------------------------------------------------------------
+TTsModelItemKey CTsService::keyL(TInt offset) const
+{
+    return TTsModelItemKey(intValueL(offset, "TaskId"), reinterpret_cast<TInt>(this));
+}
+
+// -----------------------------------------------------------------------------
+TBool CTsService::isActiveL(TInt offset) const
+{
+    return intValueL(offset, "TaskIsRunning");
+}
+
+// -----------------------------------------------------------------------------
+TBool CTsService::isClosableL(TInt offset) const
+{
+    return intValueL(offset, "TaskCanBeClosed");
+}
+
+// -----------------------------------------------------------------------------
+TBool CTsService::closeL(TTsModelItemKey key) const
+{
+    bool result(false);
+    QMetaObject::invokeMethod(mModel, "closeTask", Q_RETURN_ARG(bool, result), Q_ARG(QVariant, key.key()));
+    return result;
+}
+
+// -----------------------------------------------------------------------------
+TBool CTsService::launchL(TTsModelItemKey key) const
+{
+    bool result(false);
+    QMetaObject::invokeMethod(mModel, "openTask", Q_RETURN_ARG(bool, result), Q_ARG(QVariant, key.key()));
+    return result;
+}
+
+// -----------------------------------------------------------------------------
+TInt CTsService::intValueL(TInt offset, const char *key) const
+{
+    return valueL(mModel, offset).value(key).toInt();
+}
+
+// -----------------------------------------------------------------------------
+TTime CTsService::timeValueL(TInt offset, const char *key) const
+{
+    // Conversion between TTime which counts from year 0, and QDateTime which uses unix epoch (1st Jan 1970)
+    return TTime(_L("19700000:")) + TTimeIntervalSeconds(valueL(mModel, offset).value(key).toDateTime().toTime_t());
+}
+
+// -----------------------------------------------------------------------------
+const TDesC& CTsService::stringValueL(TInt offset, const char *key) const
+{
+    delete mBuffer;
+    const_cast<CTsService *>(this)->mBuffer = 0;
+    
+    const QVariantHash item(valueL(mModel, offset));
+    QT_TRYCATCH_LEAVING(
+    const_cast<CTsService *>(this)->mBuffer = 
+    XQConversions::qStringToS60Desc(item.value(key).toString()));
+    return *mBuffer;
+}