homescreenapp/hsdomainmodel/src/hswidgethost.cpp
changeset 55 03646e8da489
parent 51 4785f57bf3d4
child 60 30f14686fb04
child 62 341166945d65
--- a/homescreenapp/hsdomainmodel/src/hswidgethost.cpp	Thu May 27 12:46:08 2010 +0300
+++ b/homescreenapp/hsdomainmodel/src/hswidgethost.cpp	Fri Jun 11 13:30:16 2010 +0300
@@ -15,190 +15,90 @@
 *
 */
 
-#include <QMetaObject>
+#include <QApplication>
+#include <QStateMachine>
+#include <QState>
+#include <QFinalState>
 #include <QGraphicsLinearLayout>
 #include <QParallelAnimationGroup>
 #include <QPropertyAnimation>
 #include <QGraphicsDropShadowEffect>
 #include <QGraphicsSceneResizeEvent>
+#include <QGesture>
+#include <QGraphicsScene>
 
 #include <qservicemanager.h>
 #include <qservicefilter.h>
 #include <qserviceinterfacedescriptor.h>
 
-#include <HbInstance>
 #include <HbInstantFeedback>
 
-#include "hswidgethost.h"
 #include "hsdatabase.h"
 #include "hsdomainmodeldatastructures.h"
+#include "hsscene.h"
 #include "hspage.h"
-#include "hsapp_defs.h"
-#include "hsscene.h"
+#include "hswidgethost.h"
 #include "hswidgetcomponentregistry.h"
 #include "hswidgetcomponent.h"
+#include "hsconfiguration.h"
+
+// Helper macros for connecting state entry and exit actions.
+#define ENTRY_ACTION(state, action) \
+    connect(state, SIGNAL(entered()), SLOT(action()));
+#define EXIT_ACTION(state, action) \
+    connect(state, SIGNAL(exited()), SLOT(action()));
 
 QTM_USE_NAMESPACE
 
-/*!
-    \class HsWidgetHost
-    \ingroup group_hsutils
-    \brief Homescreen widget runner.
-    Is responsible of running a homescreen widget. Each
-    homescreen widget has its own host.
-*/
-
-HsWidgetHost* HsWidgetHost::createInstance(HsWidgetData &widgetData,
-                                           const QVariantHash &preferences)
+HsWidgetHost::HsWidgetHost(int databaseId, QGraphicsItem *parent)
+  : HbWidget(parent),
+    mDatabaseId(databaseId),
+    mStateMachine(0),
+    mWidget(0),
+    mPage(0),
+    mComponent(0),
+    mIsFinishing(false)
 {
-    HsDatabase* db = HsDatabase::instance();
-    Q_ASSERT(db);
-
-    if (db->insertWidget(widgetData)) {
-        db->setWidgetPreferences(widgetData.id, preferences);
-        return new HsWidgetHost(widgetData.id);
-    }
+    setFlag(QGraphicsItem::ItemClipsChildrenToShape);
+    setFlag(QGraphicsItem::ItemHasNoContents);
 
-    return 0;
-}
-/*!
-    Construct a widget host for the given \a databaseId.
-    \a parent becomes the parent item for the host.
-*/
-HsWidgetHost::HsWidgetHost(int databaseId, QGraphicsItem *parent)
-    : HbWidget(parent),
-      mWidget(0),
-      mPage(0),
-      mState(Unloaded),
-      mDatabaseId(databaseId)
-{
-    setFlags(QGraphicsItem::ItemClipsChildrenToShape);
-
-    HsDatabase *db = HsDatabase::instance();
+    grabGesture(Qt::TapGesture);
+    grabGesture(Qt::TapAndHoldGesture);
+    grabGesture(Qt::PanGesture);
+    grabGesture(Qt::PinchGesture);
+    grabGesture(Qt::SwipeGesture);
+    grabGesture(Qt::CustomGesture);
 
-    // Find the widget data.
-    HsWidgetData data;
-    data.id = mDatabaseId;
-    if (!db->widget(data)) {
-       return;
-    }
-
-    mUri = data.uri;
-
-    // bind host to component
-    HsWidgetComponent *component = HsWidgetComponentRegistry::instance()->component(mUri);
-    connect(component, SIGNAL(uninstalled()), SLOT(onFinished()));
-    connect(component, SIGNAL(aboutToUninstall()), SLOT(onAboutToUninstall()));
-    connect(component, SIGNAL(updated()), SLOT(onUpdated()));
-    connect(component, SIGNAL(unavailable()), SLOT(onUnavailable()));
-    connect(component, SIGNAL(available()), SLOT(onAvailable()));
-
-    /* TODO: Uncomment after the Qt bug has been fixed.
-    QGraphicsDropShadowEffect *effect =
-        new QGraphicsDropShadowEffect(this);
-    effect->setColor(QColor(0, 0, 0, 150));
-    effect->setBlurRadius(5);
-    effect->setOffset(3);
-    setGraphicsEffect(effect);
-    */
+    setupEffects();
+    setupStates();
 }
 
-/*!
-    Destructor.
-*/
 HsWidgetHost::~HsWidgetHost()
 {
 }
 
-/*!
-    Load hosted widget from plugin and validate it.
-    Returns true if widget construction is successfull.
-*/
-bool HsWidgetHost::load()
+HsWidgetHost *HsWidgetHost::createInstance(HsWidgetData &widgetData, 
+                                           const QVariantHash &preferences)
 {
-    if (mState != Unloaded) {
-        return false;
-    }
-    if (mWidget) {
-        return false;
-    }
-
-
-    // Create the hosted widget.
-    QServiceManager manager;
-    QServiceFilter filter("com.nokia.symbian.IHomeScreenWidget");
-    filter.setServiceName(mUri);
-    QList<QServiceInterfaceDescriptor> interfaces = manager.findInterfaces(filter);
-    if (interfaces.isEmpty()) {
-        return false;
-    }
-
-    QObject *widgetObject = manager.loadInterface(interfaces.first());
-    mWidget = qobject_cast<QGraphicsWidget *>(widgetObject);
-
-    if (!mWidget ||
-        !setMethod("onShow()", mOnShowMethod) ||
-        !setMethod("onHide()", mOnHideMethod)) {
-        mWidget = 0;
-        delete widgetObject;
-        return false;
-    }
+    HsDatabase *db = HsDatabase::instance();
 
-    setProperty("isOnline", mIsOnlineProperty);
-	setProperty("rootPath", mRootPathProperty);
-    
-    setMethod("isPannable(QGraphicsSceneMouseEvent*)", mIsPannable);
-    setMethod("onInitialize()", mOnInitializeMethod);
-    setMethod("onUninitialize()", mOnUninitializeMethod);
-
-    if (hasSignal("setPreferences(const QStringList&)")) {
-        connect(mWidget, SIGNAL(setPreferences(QStringList)),
-                SLOT(onSetPreferences(QStringList)));
-    }
-    if (hasSignal("finished()")) {
-        connect(mWidget, SIGNAL(finished()),
-                SLOT(onFinished()));
+    if (db->insertWidget(widgetData)) {
+        db->setWidgetPreferences(widgetData.id, preferences);
+        return new HsWidgetHost(widgetData.id);
+    } else {
+        return 0;
     }
-    if (hasSignal("error()")) {
-        connect(mWidget, SIGNAL(error()),
-                SLOT(onError()));
-    }
-
-    mWidget->installEventFilter(this);
-
-	loadWidgetPresentation();
-
-    HsScene *scene = HsScene::instance();
-    setMaximumSize(scene->maximumWidgetSizeInPixels());
-    setMinimumSize(scene->minimumWidgetSizeInPixels());
-
-    mWidget->setParentItem(this);
-
-    setNewSize(mWidget->size());
-    mState = Loaded;
-
-    return true;
 }
 
-void HsWidgetHost::unload()
+int HsWidgetHost::databaseId() const
 {
-    if (mState != Uninitialized) {
-        return;
-    }
-    if (mWidget) {
-        mWidget->setParentItem(0);
-    }
-    delete mWidget;
-    QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
-    mWidget = 0;
-    mState = Unloaded;
+    return mDatabaseId;
 }
 
 bool HsWidgetHost::setPage(HsPage *page)
 {
-    HsDatabase* db = HsDatabase::instance();
-    Q_ASSERT(db);
-
+    HsDatabase *db = HsDatabase::instance();
+    
     HsWidgetData data;
     data.id = mDatabaseId;
     if (db->widget(data)) {
@@ -217,266 +117,129 @@
     mPage = page;
     return true;
 }
-
+ 
 HsPage *HsWidgetHost::page() const
 {
     return mPage;
 }
 
-/*!
-    Returns true if this host has a valid widget set.
-    Otherwise, return false.
-*/
-bool HsWidgetHost::isValid() const
-{
-    return mWidget;
-}
-
-/*!
-    Returns database id
-*/
-int HsWidgetHost::databaseId() const
-{
-    return mDatabaseId;
-}
-
-/*!
-    Returns true if this the database operation succeeds,
-    false otherwise
-*/
-bool HsWidgetHost::deleteFromDatabase()
+bool HsWidgetHost::isPannable(QGraphicsSceneMouseEvent *event)
 {
-    HsDatabase *db = HsDatabase::instance();
-
-    if (!db->deleteWidget(mDatabaseId)) {
-        return false;
-    }
-
-    mDatabaseId = -1;
-    return true;
-}
-
-/*!
-    Set widget presentation by using current values.
-    Return true if successfull.
-*/
-bool HsWidgetHost::setWidgetPresentation()
-{
-    HsDatabase *db = HsDatabase::instance();
-    Q_ASSERT(db);
-
-    QString key = HsScene::orientation() == Qt::Vertical ?
-        "portrait" : "landscape";
-
-    HsWidgetPresentationData data;
-    data.key      = key;
-    data.setPos(pos());
-    data.zValue   = zValue();
-    data.widgetId = databaseId();
-
-    return db->setWidgetPresentation(data);
+    bool result = false;
+    mIsPannableMethod.invoke(mWidget, Q_RETURN_ARG(bool, result), Q_ARG(QGraphicsSceneMouseEvent *,event));
+    return result;
 }
 
-/*!
-    Set widget presentation data. Return true if successfull.
-*/
-bool HsWidgetHost::setWidgetPresentationData(HsWidgetPresentationData &presentationData)
+bool HsWidgetHost::loadPresentation()
 {
-    HsDatabase *db = HsDatabase::instance();
-    Q_ASSERT(db);
-
-    presentationData.widgetId = mDatabaseId;
-    return db->setWidgetPresentation(presentationData);
-}
-
-/*!
-    Get widget presentation data matching given \a key.
-    Data is returned on given empty \a presentationData. Return true if successfull
-*/
-bool HsWidgetHost::widgetPresentationData(const QString &key,
-                                          HsWidgetPresentationData &presentationData)
-{
-    HsDatabase *db = HsDatabase::instance();
-    Q_ASSERT(db);
-
-    presentationData.key = key;
-    presentationData.widgetId = mDatabaseId;
-    return db->widgetPresentation(presentationData);
+    return loadPresentation(HsScene::orientation());
 }
 
-/*!
-    Return HsWidgetPresentationData for given \a orientation
-*/
-HsWidgetPresentationData HsWidgetHost::widgetPresentation(Qt::Orientation orientation)
+bool HsWidgetHost::loadPresentation(Qt::Orientation orientation)
 {
     HsDatabase *db = HsDatabase::instance();
-    Q_ASSERT(db);
-
-    QString key = orientation == Qt::Vertical ?
-        "portrait" : "landscape";
-
+        
     HsWidgetPresentationData data;
-    data.key = key;
-    data.widgetId = mDatabaseId;
-    if (db->widgetPresentation(data)) {
-        return data;
-    } else {
-        return HsWidgetPresentationData();
-    }
-}
-
-/*!
-    Load HsWidgetPresentationData for current orientation
-*/
-bool HsWidgetHost::loadWidgetPresentation()
-{
-    HsDatabase *db = HsDatabase::instance();
-
-    QString key = HsScene::orientation() == Qt::Vertical ?
-        "portrait" : "landscape";
-
-    HsWidgetPresentationData data;
-    data.key = key;
+    data.orientation = orientation;
     data.widgetId = mDatabaseId;
     if (!db->widgetPresentation(data)) {
         return false;
     }
-
     setPos(data.x, data.y);
     setZValue(data.zValue);
-
     return true;
 }
 
-/*!
-    Delete HsWidgetPresentationData for given \a orientation.
-    Return true if successfull.
-*/
-bool HsWidgetHost::deleteWidgetPresentation(Qt::Orientation orientation)
+bool HsWidgetHost::savePresentation()
+{
+    return savePresentation(HsScene::orientation());
+}
+
+bool HsWidgetHost::savePresentation(Qt::Orientation orientation)
+{
+    HsDatabase *db = HsDatabase::instance();
+        
+    HsWidgetPresentationData data;
+    data.orientation = orientation;
+    data.setPos(pos());
+    data.zValue = zValue();
+    data.widgetId = mDatabaseId;
+    return db->setWidgetPresentation(data);
+}
+
+bool HsWidgetHost::savePresentation(HsWidgetPresentationData &presentation)
+{
+    HsDatabase *db = HsDatabase::instance();
+    
+    presentation.widgetId = mDatabaseId;
+    return db->setWidgetPresentation(presentation);
+}
+
+bool HsWidgetHost::getPresentation(HsWidgetPresentationData &presentation)
 {
     HsDatabase *db = HsDatabase::instance();
-    Q_ASSERT(db);
-
-    QString key = orientation == Qt::Vertical ?
-        "portrait" : "landscape";
+        
+    presentation.widgetId = mDatabaseId;
+    return db->widgetPresentation(presentation);
+}
 
-    return db->deleteWidgetPresentation(mDatabaseId, key);
-}
-/*!
-    Check wheter widget uses pan gestures
-    Return true if successfull.
-*/
-bool HsWidgetHost::isPannable(QGraphicsSceneMouseEvent *event)
+bool HsWidgetHost::removePresentation(Qt::Orientation orientation)
 {
-    bool ret(false);
-    mIsPannable.invoke(mWidget,Q_RETURN_ARG(bool,ret),Q_ARG(QGraphicsSceneMouseEvent *,event));
-    return ret;
+    HsDatabase *db = HsDatabase::instance();
+    return db->deleteWidgetPresentation(mDatabaseId, orientation);
 }
-/*!
-    \fn void HsWidgetHost::widgetFinished()
-    This signal is emitted after the contained widget
-    reported is completion.
-*/
-
-/*!
-    \fn void HsWidgetHost::widgetError()
-    This signal is emitted after the contained widget
-    reported an error.
-*/
 
-/*!
-    \fn void HsWidgetHost::widgetResized()
-    This signal is emitted after the contained widget
-    sends a resize event.
-*/
-/*!
-    \fn void HsWidgetHost::mousePressEventIgnored()
-    This signal is emitted if managed widget ignores mouse press event
-
-*/
+void HsWidgetHost::startWidget(bool show)
+{
+    if (!mStateMachine->isRunning()) {
+        mStateMachine->start();
+        // This is needed because QStateMachine::start() starts
+        // the state machine asynchronously via the eventloop. 
+        // Here we want the machine to start synchronously.
+        QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
+    }
 
-/*!
-    Calls the widget's onInitialize() slot if the
-    widget defines it.
-*/
-void HsWidgetHost::initializeWidget()
-{
-    if (mState != Loaded) {
-        return;
-    }
-    HsWidgetComponent *component = HsWidgetComponentRegistry::instance()->component(mUri);
-    Q_ASSERT(component);
-    mRootPathProperty.write(mWidget, component->rootPath());
-    setPreferencesToWidget();
-    setOnline(HsScene::instance()->isOnline());
-    mOnInitializeMethod.invoke(mWidget);
-
-    if (mState != Finished &&
-        mState != Faulted) {
-        mState = Initialized;
+    if (show) {
+        emit event_startAndShow();
+    } else {
+        emit event_startAndHide();
     }
 }
-
-/*!
-    Calls the widget's onShow() slot if the
-    widget defines it.
-*/
+ 
 void HsWidgetHost::showWidget()
 {
-    if (mState != Initialized &&
-        mState != Hidden ) {
-        return;
-    }
-
-    mOnShowMethod.invoke(mWidget);
-
-    mState = Visible;
+    emit event_show();
 }
-
-/*!
-    Calls the widget's onHide() slot if the
-    widget defines it.
-*/
+    
 void HsWidgetHost::hideWidget()
 {
-    if (mState != Initialized &&
-        mState != Visible) {
-        return;
-    }
-
-    mOnHideMethod.invoke(mWidget);
-
-    mState = Hidden;
+    emit event_hide();
 }
 
-/*!
-    Calls the widget's onUninitialize() slot if the
-    widget defines it.
-*/
-void HsWidgetHost::uninitializeWidget()
-{
-    if (mState != Visible &&
-        mState != Hidden) {
-        return;
-    }
-
-    mOnUninitializeMethod.invoke(mWidget);
-
-    mState = Uninitialized;
-}
-
-/*!
-    Calls the widget's widgetOnlineState property if the
-    widget defines it.
-*/
 void HsWidgetHost::setOnline(bool online)
 {
     mIsOnlineProperty.write(mWidget, online);
 }
 
-/*!
-    Starts the widget drag animation.
-*/
+void HsWidgetHost::remove()
+{
+    if (mStateMachine->isRunning()) {
+        emit event_remove();
+    } else {
+        action_remove();
+        deleteLater();
+    }
+}
+ 
+void HsWidgetHost::close()
+{
+    if (mStateMachine->isRunning()) {
+        emit event_close();
+    } else {
+        deleteLater();
+    }
+}
+
 void HsWidgetHost::startDragEffect()
 {
     /* TODO: Uncomment after the Qt bug has been fixed.
@@ -490,7 +253,7 @@
     QParallelAnimationGroup *animationGroup = new QParallelAnimationGroup();
 
     QPropertyAnimation *animation = new QPropertyAnimation(this, "scale");
-    animation->setDuration(200);
+    animation->setDuration(HsConfiguration::widgetDragEffectDuration());
     animation->setEndValue(1.1);
     animationGroup->addAnimation(animation);
 
@@ -504,9 +267,6 @@
     animationGroup->start(QAbstractAnimation::DeleteWhenStopped);
 }
 
-/*!
-    Starts the widget drop animation.
-*/
 void HsWidgetHost::startDropEffect()
 {
     /* TODO: Uncomment after the Qt bug has been fixed.
@@ -518,7 +278,7 @@
     QParallelAnimationGroup *animationGroup = new QParallelAnimationGroup;
 
     QPropertyAnimation *animation = new QPropertyAnimation(this, "scale");
-    animation->setDuration(200);
+    animation->setDuration(HsConfiguration::widgetDropEffectDuration());
     animation->setEndValue(1);
     animationGroup->addAnimation(animation);
 
@@ -531,56 +291,122 @@
 
     animationGroup->start(QAbstractAnimation::DeleteWhenStopped);
 }
-/*!
-    Overwritten to stop event propogation
-*/
-void HsWidgetHost::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) 
-{ 
-    Q_UNUSED(event) 
-}
-/*!
-    Overwritten to stop event propogation
-*/
-void HsWidgetHost::mouseMoveEvent(QGraphicsSceneMouseEvent *event) 
-{ 
-    Q_UNUSED(event)  
-}
-/*!
-    Overwritten to stop event propogation
-*/
-void HsWidgetHost::mousePressEvent(QGraphicsSceneMouseEvent *event) 
-{ 
-    Q_UNUSED(event)  
-    
-}
-/*!
-    Overwritten to stop event propogation
-*/
-void HsWidgetHost::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { 
-    Q_UNUSED(event)
-}    
-/*!
-    Filters resize events from widgets and resizes inside max/min size boundaries if needed.
-*/
-bool HsWidgetHost::eventFilter(QObject *obj, QEvent *event)
+
+bool HsWidgetHost::eventFilter(QObject *watched, QEvent *event)
 {
     if (event->type() == QEvent::GraphicsSceneResize ) {
-        QGraphicsSceneResizeEvent *resizeEvent = static_cast<QGraphicsSceneResizeEvent*>(event);
+        QGraphicsSceneResizeEvent *resizeEvent = 
+            static_cast<QGraphicsSceneResizeEvent *>(event);
         setNewSize(resizeEvent->newSize());
-        emit widgetResized(this);
-        return true;
-    } else {
-         // standard event processing
-         return HbWidget::eventFilter(obj, event);
+        emit resized();
     }
+    return HbWidget::eventFilter(watched, event);
+}
+
+void HsWidgetHost::setupEffects()
+{
+    /* TODO: Uncomment after the Qt bug has been fixed.
+    QGraphicsDropShadowEffect *effect =
+        new QGraphicsDropShadowEffect(this);
+    effect->setColor(QColor(0, 0, 0, 150));
+    effect->setBlurRadius(5);
+    effect->setOffset(3);
+    setGraphicsEffect(effect);
+    */
 }
 
-/*!
-    Checks if a property with the given \a name
-    in the contained widget. If the property exists the \a
-    metaProperty is made to reference to it. Returns true if
-    the property was found. Otherwise, returns false.
-*/
+void HsWidgetHost::setupStates()
+{
+    // State machine
+
+    mStateMachine = new QStateMachine(this);
+    mStateMachine->setAnimated(false);
+    
+    // States
+
+    QState *state_component = new QState;
+    QState *state_unloaded = new QState(state_component);
+    QState *state_running = new QState(state_component);
+    QState *state_show = new QState(state_running);
+    QState *state_hide = new QState(state_running);
+    QState *state_finished = new QState;
+    QState *state_faulted = new QState;
+    QState *state_remove = new QState;
+    QFinalState *state_final = new QFinalState;
+
+    mStateMachine->addState(state_component);
+    mStateMachine->addState(state_finished);
+    mStateMachine->addState(state_faulted);
+    mStateMachine->addState(state_remove);
+    mStateMachine->addState(state_final);
+
+    mStateMachine->setInitialState(state_component);
+    state_component->setInitialState(state_unloaded);
+    state_running->setInitialState(state_hide);
+
+    // Transitions
+
+    state_component->addTransition(
+        this, SIGNAL(event_close()), state_final);
+    state_component->addTransition(
+        this, SIGNAL(event_remove()), state_remove);
+    state_component->addTransition(
+        this, SIGNAL(event_finished()), state_finished);
+    state_component->addTransition(
+        this, SIGNAL(event_faulted()), state_faulted);
+
+    state_unloaded->addTransition(
+        this, SIGNAL(event_startAndShow()), state_show);
+    state_unloaded->addTransition(
+        this, SIGNAL(event_startAndHide()), state_hide);
+
+    state_running->addTransition(
+        this, SIGNAL(event_unload()), state_unloaded);
+    
+    state_show->addTransition(
+        this, SIGNAL(event_hide()), state_hide);
+
+    state_hide->addTransition(
+        this, SIGNAL(event_show()), state_show);
+
+    state_finished->addTransition(
+        this, SIGNAL(event_remove()), state_remove);
+    state_finished->addTransition(
+        this, SIGNAL(event_close()), state_final);
+
+    state_faulted->addTransition(
+        this, SIGNAL(event_remove()), state_remove);
+    state_faulted->addTransition(
+        this, SIGNAL(event_close()), state_final);
+
+    state_remove->addTransition(state_final);
+
+    // Actions
+
+    ENTRY_ACTION(state_component, action_connectComponent)
+    EXIT_ACTION(state_component, action_disconnectComponent)
+
+    ENTRY_ACTION(state_running, action_load)
+    ENTRY_ACTION(state_running, action_initialize)
+    EXIT_ACTION(state_running, action_uninitialize)
+    EXIT_ACTION(state_running, action_unload)
+
+    ENTRY_ACTION(state_show, action_show)
+
+    ENTRY_ACTION(state_hide, action_hide)
+
+    ENTRY_ACTION(state_finished, action_finished)
+
+    ENTRY_ACTION(state_faulted, action_faulted)
+
+    ENTRY_ACTION(state_remove, action_remove)
+
+    // Delete on finish.
+
+    connect(mStateMachine, SIGNAL(finished()), SLOT(deleteLater()), 
+            Qt::QueuedConnection);
+}
+
 bool HsWidgetHost::setProperty(const char *name, QMetaProperty &property)
 {
     const QMetaObject *object = mWidget->metaObject();
@@ -589,12 +415,6 @@
     return index >= 0;
 }
 
-/*!
-    Checks if a slot with the given \a signature exists
-    in the contained widget. If the slot exists the \a
-    method is made to reference to it. Returns true if
-    the slot was found. Otherwise, returns false.
-*/
 bool HsWidgetHost::setMethod(const char *signature, QMetaMethod &method)
 {
     const QMetaObject *object = mWidget->metaObject();
@@ -604,11 +424,6 @@
     return index >= 0;
 }
 
-/*!
-    Returns true if a signal with the given \a signature
-    exists in the contained widget. Otherwise, returns
-    false.
-*/
 bool HsWidgetHost::hasSignal(const char *signature)
 {
     const QMetaObject *object = mWidget->metaObject();
@@ -616,22 +431,24 @@
         QMetaObject::normalizedSignature(signature));
     return index >= 0;
 }
-/*!
-    Returns true if fetching widget preferences from db and setting those
-    to widget is successfull
-*/
+
+void HsWidgetHost::setNewSize(const QSizeF &size)
+{
+    resize(size);
+    setPreferredSize(size);
+}
+
 bool HsWidgetHost::setPreferencesToWidget()
 {
     HsDatabase *db = HsDatabase::instance();
-    Q_ASSERT(db);
-
+    
     QVariantHash preferences;
     if (!db->widgetPreferences(mDatabaseId, preferences)) {
         return false;
     }
 
     QStringList names = preferences.keys();
-    foreach(QString name, names) {
+    foreach (QString name, names) {
         mWidget->setProperty(name.toLatin1(),
                              preferences.value(name));
     }
@@ -639,23 +456,159 @@
     return true;
 }
 
-/*!
-    Resizes and sets preferred size for widget layouts
-*/
-void HsWidgetHost::setNewSize(const QSizeF &newSize)
+void HsWidgetHost::action_connectComponent()
 {
-    resize(newSize);
-    setPreferredSize(newSize);
+    HsDatabase *db = HsDatabase::instance();
+    
+    HsWidgetData data;
+    data.id = mDatabaseId;
+    if (!db->widget(data)) {
+        emit event_faulted();
+        return;
+    }
+    
+    mComponent = HsWidgetComponentRegistry::instance()->component(data.uri);
+    
+    connect(mComponent, SIGNAL(aboutToUninstall()), SIGNAL(event_unload()));
+    connect(mComponent, SIGNAL(uninstalled()), SIGNAL(event_finished()));
+    connect(mComponent, SIGNAL(unavailable()), SIGNAL(event_unload()));
+    connect(mComponent, SIGNAL(unavailable()), SIGNAL(unavailable()));
+    connect(mComponent, SIGNAL(available()), SIGNAL(available()));
+    connect(mComponent, SIGNAL(updated()), SIGNAL(available()));
+}
+
+void HsWidgetHost::action_disconnectComponent()
+{
+    mComponent->disconnect(this);
 }
 
-/*!
-    This slot is connected to the contained widget's
-    setPreferences() signal, if it was defined for
-    the widget. The widget emits the signal for persisting
-    its preferences named with \a names. The given
-    preferences are read, validated, and written to
-    the database.
-*/
+void HsWidgetHost::action_load()
+{
+    QServiceManager manager;
+    QServiceFilter filter("com.nokia.symbian.IHomeScreenWidget");
+    filter.setServiceName(mComponent->uri());
+    QList<QServiceInterfaceDescriptor> interfaces = manager.findInterfaces(filter);
+    if (interfaces.isEmpty()) {
+        emit event_faulted();
+        return;
+    }
+
+    QObject *widgetObject = manager.loadInterface(interfaces.first());
+    mWidget = qobject_cast<QGraphicsWidget *>(widgetObject);
+
+    if (!mWidget ||
+        !setMethod("onShow()", mOnShowMethod) ||
+        !setMethod("onHide()", mOnHideMethod)) {
+        mWidget = 0;
+        delete widgetObject;
+        emit event_faulted();
+        return;
+    }
+
+    setMethod("onInitialize()", mOnInitializeMethod);
+    setMethod("onUninitialize()", mOnUninitializeMethod);
+    setMethod("isPannable(QGraphicsSceneMouseEvent*)", mIsPannableMethod);
+
+    setProperty("isOnline", mIsOnlineProperty);
+	setProperty("rootPath", mRootPathProperty);
+    
+    if (hasSignal("setPreferences(const QStringList&)")) {
+        connect(mWidget, SIGNAL(setPreferences(QStringList)),
+                SLOT(onSetPreferences(QStringList)));
+    }
+    if (hasSignal("finished()")) {
+        connect(mWidget, SIGNAL(finished()),
+                SLOT(onFinished()));
+    }
+    if (hasSignal("error()")) {
+        connect(mWidget, SIGNAL(error()),
+                SLOT(onError()));
+    }
+
+    mWidget->installEventFilter(this);
+
+    HsScene *scene = HsScene::instance();
+    setMaximumSize(scene->maximumWidgetSizeInPixels());
+    setMinimumSize(scene->minimumWidgetSizeInPixels());
+
+    loadPresentation();
+
+    mWidget->setParentItem(this);
+
+    setNewSize(mWidget->size());
+}
+
+void HsWidgetHost::action_unload()
+{
+    delete mWidget;
+    mWidget = 0;
+
+    mOnInitializeMethod = QMetaMethod();
+    mOnShowMethod = QMetaMethod();
+    mOnHideMethod = QMetaMethod();
+    mOnUninitializeMethod = QMetaMethod();
+    mIsPannableMethod = QMetaMethod();
+    mIsOnlineProperty = QMetaProperty();
+	mRootPathProperty = QMetaProperty();    
+}
+
+void HsWidgetHost::action_initialize()
+{    
+    mRootPathProperty.write(mWidget, mComponent->rootPath());
+    setPreferencesToWidget();
+    setOnline(HsScene::instance()->isOnline());
+    mOnInitializeMethod.invoke(mWidget);
+}
+
+void HsWidgetHost::action_uninitialize()
+{
+    mOnUninitializeMethod.invoke(mWidget);
+}
+
+void HsWidgetHost::action_show()
+{
+    if (!mIsFinishing) {
+        mOnShowMethod.invoke(mWidget);
+    }
+}
+
+void HsWidgetHost::action_hide()
+{
+    if (!mIsFinishing) {
+        mOnHideMethod.invoke(mWidget);
+    }
+}
+
+void HsWidgetHost::action_finished()
+{
+    emit finished();
+}
+
+void HsWidgetHost::action_faulted()
+{
+    emit faulted();
+}
+
+void HsWidgetHost::action_remove()
+{
+    HsDatabase *db = HsDatabase::instance();
+    
+    db->deleteWidget(mDatabaseId);
+    mDatabaseId = -1;
+}
+
+void HsWidgetHost::onFinished()
+{
+    mIsFinishing = true;
+    emit event_finished();
+}
+ 
+void HsWidgetHost::onError()
+{
+    mIsFinishing = true;
+    emit event_faulted();
+}
+
 void HsWidgetHost::onSetPreferences(const QStringList &names)
 {
     if (names.isEmpty()) {
@@ -669,85 +622,8 @@
         preferences.insert(name, value);
     }
 
-    HsDatabase *db = HsDatabase::instance();
-    Q_ASSERT(db);
-
+    HsDatabase *db = HsDatabase::instance();    
     if (!db->setWidgetPreferences(mDatabaseId, preferences)) {
         onError();
     }
 }
-
-/*!
-    This slot reacts to the widgets finished() signal, if
-    it was defined for the widget. The widget emits the signal
-    when it has finished its execution and is ready for
-    removal from the homescreen.
-*/
-void HsWidgetHost::onFinished()
-{
-    mState = Finished;
-    emit widgetFinished(this);
-}
-
-/*!
-    This slot reacts to the widgets error() signal, if it was
-    defined for the widget. The widget emits the signal in
-    failure cases.
-*/
-void HsWidgetHost::onError()
-{
-    mState = Faulted;
-    emit widgetError(this);
-}
-/*!
-    This slot is called when component is about to uninstall or
-    update. Widget need to release all handles to resources installing
-    to succeed.
-*/
-void HsWidgetHost::onAboutToUninstall()
-{
-    uninitializeWidget();
-    unload();
-}
-
-void HsWidgetHost::onUpdated()
-{
-    if(mState != Unloaded) {
-        return;
-    }
-    load();
-    initializeWidget();
-    if (HsScene::instance()->activePage() == mPage) {
-       showWidget();
-    } else {
-        hideWidget();
-    }
-
-}
-void HsWidgetHost::onUnavailable()
-{
-    if (mState != Visible && mState != Hidden) {
-        return;
-    }
-    uninitializeWidget();
-    unload();
-}
-
-void HsWidgetHost::onAvailable()
-{
-    if (mState != Unloaded) {
-        return;
-    }
-    load();
-    initializeWidget();
-    if (HsScene::instance()->activePage() == mPage) {
-        showWidget();
-    } else {
-        hideWidget();
-    }
-}
-
-
-
-
-