/***************************************************************************
 *   Copyright (c) 2009  Peter Mylemans <peter.mylemans@gmail.com>         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include "App.h"
#include <KApplication>
#include <KAboutData>
#include <KCmdLineArgs>

#include "collection/LocalCollection.h"
#include "views/MovieView.h"

App * App::m_app = NULL;

App * App::self() 
{
    return m_app;
}

App::App(int argc, char *argv[])
    : m_mainWindow(NULL), m_collectionManager(NULL)
{
    Q_ASSERT(m_app == NULL);

    init(argc, argv);

    m_app = this;
}

App::~App()
{
    delete KApplication::kApplication();
    if (m_collectionManager != NULL)
        delete m_collectionManager;
}

void App::init(int argc, char *argv[])
{
    KAboutData aboutData("falkor", 0,
        ki18n("Falkor"), "1.0",
        ki18n("TODO"),
        KAboutData::License_GPL,
        ki18n("Copyright (c) 2009 Peter Mylemans"));

    KCmdLineArgs::init(argc, argv, &aboutData);
    new KApplication();
}

void App::createMainWindow()
{
    if (m_mainWindow != NULL)
        return;
    m_mainWindow = new MainWindow();
}

void App::createCollectionManager()
{
    if (m_collectionManager != NULL)
        return;
    m_collectionManager = new CollectionManager();
}

void App::loadPlugins()
{
    mainWindow()->context()->registerView(new MovieView());
    LocalCollection *localCollection = new LocalCollection();
    m_collectionManager->addCollection(localCollection);
    localCollection->query("");
}

int App::exec()
{
    return kapp->exec();
}
