activityfw/testapplications/activitytestletter/viewmanager.cpp
author Jaakko Haukipuro (Nokia-MS/Oulu) <Jaakko.Haukipuro@nokia.com>
Thu, 16 Sep 2010 12:11:40 +0100
changeset 117 c63ee96dbe5f
permissions -rw-r--r--
Missing activityfw and taskswitcher components - fix for Bug 3670

/*
* 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 <qservicemanager.h>
#include <HbInstance>
#include <QEventLoop>
#include <QStringList>
#include <QCoreApplication>
#include <hbapplication.h>
#include "viewmanager.h"
#include "boldwidget.h"
#include "italicwidget.h"
#include "underlinewidget.h"
#include "letterwidget.h"
#include "afstorageglobals.h"

QTM_USE_NAMESPACE

viewmanager::viewmanager(HbApplication *hbapp):
    HbMainWindow(),  mHbApp(hbapp)
{
    QServiceManager serviceManager;
    mActivityClient = serviceManager.loadInterface("com.nokia.qt.activities.ActivityClient");
    if (!mActivityClient) {
        qFatal("load plugin failed");
    }

    connect(mActivityClient, SIGNAL(activityRequested(QString)), this, SLOT(loadActivityData(QString)));
    

    mBoldWidget = new boldwidget();
    mItalicWidget = new italicwidget();
    mUnderlineWidget = new underlinewidget();
    mBoldView = addView(mBoldWidget);
    mBoldView->setTitle("Bold");
    mItalicView = addView(mItalicWidget);
    mItalicView->setTitle("Italic");
    mUnderlineView = addView(mUnderlineWidget);
    mUnderlineView->setTitle("Underline");
    toitalic();
    connect(mBoldWidget, SIGNAL(toitalic()), this, SLOT(toitalic()));
    connect(mBoldWidget, SIGNAL(tounderline()), this, SLOT(tounderline()));

    connect(mItalicWidget, SIGNAL(tounderline()), this, SLOT(tounderline()));
    connect(mItalicWidget, SIGNAL(tobold()), this, SLOT(tobold()));

    connect(mUnderlineWidget, SIGNAL(tobold()), this, SLOT(tobold()));
    connect(mUnderlineWidget, SIGNAL(toitalic()), this, SLOT(toitalic()));

    connect(mBoldWidget, SIGNAL(save()), this, SLOT(save()));
    connect(mItalicWidget, SIGNAL(save()), this, SLOT(save()));
    connect(mUnderlineWidget, SIGNAL(save()), this, SLOT(save()));
    connect(mBoldWidget, SIGNAL(del()), this, SLOT(del()));
    connect(mItalicWidget, SIGNAL(del()), this, SLOT(del()));
    connect(mUnderlineWidget, SIGNAL(del()), this, SLOT(del()));

    
    //startup case    
    QStringList arglist = qApp->arguments(); 
    QVariantHash actdata;
    bool ok = QMetaObject::invokeMethod(mActivityClient, "parseCommandLine", Q_RETURN_ARG(QVariantHash, actdata), Q_ARG(QStringList, arglist));
    if (!ok) {
        qFatal("Startup failed");
    }
    QString actid = actdata.value("activityname").toString();
    if ( !actid.isNull() )
        {
        QVariant data;
        ok = QMetaObject::invokeMethod(mActivityClient, "activityData", Q_RETURN_ARG(QVariant, data), Q_ARG(QString, actid));
        if (!ok) {
            qFatal("Get data failed");
        }
        loadActivityData(data, true);
        }
    //startup case end
}

viewmanager::~viewmanager()
{
}

void viewmanager::toitalic()
{
    setCurrentView(mItalicView);
    mStatename = "italic";
}

void viewmanager::tobold()
{
    setCurrentView(mBoldView);
    mStatename = "bold";
}

void viewmanager::tounderline()
{
    setCurrentView(mUnderlineView);
    mStatename = "underline";
}


void viewmanager::save()
{
    QString color;
    QString visible;
    QString persistent;
    QString name;
    letterwidget* widget = currentWidget();
    color = widget->color();
    visible = widget->visibility();
    persistent = widget->persistent();
    name = widget->name();

    QStringList list;
    list.append(mStatename);
    list.append(color);
    mSaveVariant.clear();
    mSaveVariant = list;

    HbMainWindow *mainWindow = hbInstance->allMainWindows().first();
    QPixmap screenshot = QPixmap::grabWidget(mainWindow, mainWindow->rect());
    QVariantHash metadata;
    metadata.insert(ActivityScreenshotKeyword, screenshot);
    if ( visible == "true" )
        {
        metadata.insert(ActivityVisibility, true);    
        }
    else if ( visible == "false" )
        {
        metadata.insert(ActivityVisibility, false);    
        }
    
    if ( persistent == "true" )
        {
        metadata.insert(ActivityPersistence, true);    
        }
    else if ( persistent == "false" )
        {
        metadata.insert(ActivityPersistence, false);    
        }
    
    if ( name != "none" )
        {
        metadata.insert(ActivityApplicationName, name);    
        }
    
    bool retok;
    bool ok = QMetaObject::invokeMethod(mActivityClient, "removeActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, mStatename));
    if (!ok) {
        qFatal("Remove failed");
    }

    ok = QMetaObject::invokeMethod(mActivityClient, "addActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, mStatename), Q_ARG(QVariant, mSaveVariant), Q_ARG(QVariantHash, metadata));
    if (!ok || !retok) {
        qFatal("Add failed");
    }
}
void viewmanager::del()
{
    bool retok;
    bool ok = QMetaObject::invokeMethod(mActivityClient, "removeActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, mStatename));
    if (!ok) {
        qFatal("Remove failed");
    }
}

void viewmanager::loadActivityData(const QString &name)
{
    QVariant data;
    bool ok = QMetaObject::invokeMethod(mActivityClient, "activityData", Q_RETURN_ARG(QVariant, data), Q_ARG(QString, name));
    if (!ok) {
        qFatal("Get data failed");
    }
    loadActivityData(data, false);
}

void viewmanager::loadActivityData(const QVariant &data, bool startup)
{
    HbView *oldView = currentView();

    QStringList list = data.toStringList();
    QString statename = list.at(0);
    QString color = list.at(1);
    if (statename == "bold") {
        mBoldWidget->setcolor(color);
        tobold();
        mBoldWidget->update();
    } else if (statename == "italic") {
        mItalicWidget->setcolor(color);
        toitalic();
        mItalicWidget->update();
    } else if (statename == "underline") {
        mUnderlineWidget->setcolor(color);
        tounderline();
        mUnderlineWidget->update();
    } else {
        Q_ASSERT(false);
    }
    
    if (currentView() != oldView && !startup) {
        QEventLoop loop;
        loop.connect(this, SIGNAL(viewReady()), SLOT(quit()));
        loop.exec();
    }
}

letterwidget* viewmanager::currentWidget()
	{
	HbView* view = currentView();
	QGraphicsWidget* gwidget = view->widget();
	letterwidget* lwidget = qobject_cast<letterwidget*>(gwidget);
	}