#include <QtDebug>

#include "hsqml.h"
#include "HsQMLManager.h"
#include "HsQMLEngine.h"
#include "HsQMLObject.h"
#include "HsQMLWindow.h"

HsQMLEngine::HsQMLEngine(HsQMLEngineConfig& config)
{
  mEngine.rootContext()->setContextObject(config.globalObject->object());

  HsQMLWindow* win = new HsQMLWindow(this);
  win->setSource(config.initialURL);
  win->setVisible(true);
  mWindows.insert(win);
}

HsQMLEngine::~HsQMLEngine()
{
}

QDeclarativeEngine* HsQMLEngine::engine()
{
  return &mEngine;
}

extern "C" void hsqml_create_engine(
  HsQMLObjectHandle* globalObject,
  const char* initialURL)
{
  HsQMLEngineConfig config;
  config.globalObject = (HsQMLObjectProxy*)globalObject;
  config.initialURL = QUrl(QString(initialURL));

  Q_ASSERT (gManager);
  QMetaObject::invokeMethod(
    gManager, "createEngine", Qt::QueuedConnection,
    Q_ARG(HsQMLEngineConfig, config));
}

#include <QtDebug>

#include "hsqml.h"
#include "HsQMLManager.h"
#include "HsQMLEngine.h"
#include "HsQMLObject.h"
#include "HsQMLWindow.h"

HsQMLEngine::HsQMLEngine(HsQMLEngineConfig& config)
{
  // Obtain, re-parent, and set QML global object
  QObject* globalObject = config.globalObject->object();
  globalObject->setParent(this);
  mEngine.rootContext()->setContextObject(globalObject);

  // Create window
  HsQMLWindow* win = new HsQMLWindow(this);
  win->setSource(config.initialURL);
  win->setVisible(true);
  mWindows.insert(win);
}

HsQMLEngine::~HsQMLEngine()
{
}

QDeclarativeEngine* HsQMLEngine::engine()
{
  return &mEngine;
}

extern "C" void hsqml_create_engine(
  HsQMLObjectHandle* globalObject,
  const char* initialURL)
{
  HsQMLEngineConfig config;
  config.globalObject = (HsQMLObjectProxy*)globalObject;
  config.initialURL = QUrl(QString(initialURL));

  Q_ASSERT (gManager);
  QMetaObject::invokeMethod(
    gManager, "createEngine", Qt::QueuedConnection,
    Q_ARG(HsQMLEngineConfig, config));
}

Fix leaking QML global object on engine termination.
Fix leaking QML global object on engine termination.

Ignore-this: 44b1eb2549692562150736cbdf5e8477

darcs-hash:20120528214902-4d2ae-007f9a88d8572ff57d5ce90d9ae31143163e7118
