/*
 * Copyright (c) 2011 Nokia Corporation.
 */

#include "mirroritem.h"

#include <QtGui>
#include <QApplication>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDebug>

// Lock Symbian orientation
#ifdef Q_OS_SYMBIAN
#include <eikenv.h>
#include <eikappui.h>
#include <aknenv.h>
#include <aknappui.h>
#endif


#ifdef Q_OS_SYMBIAN
#include <QSymbianEvent>
#include <w32std.h>

#endif

// TODO: Where exists these values?
static const int KGoomMemoryLowEvent = 0x10282DBF;
static const int KGoomMemoryGoodEvent = 0x20026790;

/* ***************************************************************************
* MyApplication
*/
class MyApplication : public QApplication
{
public:
  MyApplication( int argc, char** argv ) : QApplication( argc, argv ) {}

#ifdef Q_OS_SYMBIAN
protected:
  bool symbianEventFilter( const QSymbianEvent* symbianEvent ) {
    const TWsEvent *event = symbianEvent->windowServerEvent();
    if( !event ) {
      return false;
    }
    switch( event->Type() ) {
      // GOOM handling enabled
      // http://wiki.forum.nokia.com/index.php/Graphics_memory_handling
      case EEventUser: {
        TApaSystemEvent* eventData = reinterpret_cast<TApaSystemEvent*>(event->EventData());
        if ((*eventData) == EApaSystemEventShutdown) {
            qDebug() << "EApaSystemEventShutdown";
            eventData++;
            if ((*eventData) == KGoomMemoryLowEvent) {
                qDebug() << "KGoomMemoryLowEvent";
             return true;
            }
        }
        break;
      }
      default:
        break;
    };

    // Always return false so we don't stop
    // the event from being processed
    return false;
  }
#endif
};

/* ***************************************************************************
* MyHelper
*/
class MyHelper : public QObject
{
public:
    MyHelper(QDeclarativeItem* qml, QObject* parent=0):QObject(parent)
    {
        m_qml = qml;
    }

    void printObjectTree(QObject* parent)
    {
        if (parent) {
            qDebug() << parent->objectName();
            qDebug() << parent->metaObject()->className();

            QObjectList list = parent->children();
            QObject* item;
            foreach(item,list) {
                if (item->children().count()>0) {
                    qDebug() << "--Childrens found--";
                    printObjectTree(item);
                } else {
                    qDebug() << item->objectName();
                    qDebug() << item->metaObject()->className();
                }
            }
        }
    }

    virtual ~MyHelper()
    {
    }

private:
    QDeclarativeItem* m_qml;
};




/* ***************************************************************************
* main
*/
int main(int argc, char *argv[])
{
    MyApplication a(argc, argv);

    qmlRegisterType<MirrorItem>("CustomItems", 1, 0, "MirrorItem");

    // Lock Symbian orientation
#ifdef Q_OS_SYMBIAN
    CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
    TRAP_IGNORE( 
    if (appUi) {
        appUi->SetOrientationL(CAknAppUi::EAppUiOrientationPortrait);
    }
    );
#endif

    QDeclarativeView* view = new QDeclarativeView();
    view->setSource(QUrl("qrc:/main.qml"));
    view->setResizeMode(QDeclarativeView::SizeRootObjectToView);

    //QObject *ro = view->rootObject();
    //QDeclarativeItem* qmlItem = static_cast<QDeclarativeItem*>(ro);
    //MyHelper* helper = new MyHelper(qmlItem);
    //view->rootContext()->setContextProperty("MyHelper", helper);
    //helper->printObjectTree(qmlItem);


    // For Qt.quit() from QML
    QObject::connect((QObject*)view->engine(), SIGNAL(quit()), &a, SLOT(quit()));

    view->showFullScreen();

    int ret = a.exec();

    delete view;
    //delete helper;

    return ret;
}
