/*
    Copyright (c) 2008 Louai Al-Khanji

    This file is part of Safra.

    Safra is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2 as published
    by the Free Software Foundation.

    Safra 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 Safra; see the file COPYING. If not, see
    <http://www.gnu.org/licenses/>.
*/

#include <QPointer>

#include <KUniqueApplication>
#include <KCmdLineArgs>
#include <KAboutData>
#include <KLocale>
#include <KGlobal>

#include "mainwindow.h"
#include "settings.h"

class Application : public KUniqueApplication
{
    Q_OBJECT
    public:
        Application() : KUniqueApplication(true, true), m_mainWindow(0)
        {}

        virtual int newInstance()
        {
            // Keep the process running in the background *unless* we allow
            // multiple instances. In that case presumable the user always wants
            // a new instance. I wonder why having it only in the constructor
            // doesn't work properly.
            if (Settings::multipleInstances() == false) {
                KGlobal::ref();
            }

            KCmdLineArgs* args = KCmdLineArgs::parsedArgs();

            if (args->isSet("quit")) {
                quit();
                return 0;
            }

            if (args->isSet("background-only")) {
                return 0;
            }

            QStringList urls;
            int count = args->count();
            for (int i = 0; i < count; i++) {
                urls << args->arg(i);
            }

            if (!m_mainWindow || args->isSet("new-window")) {
                m_mainWindow = new MainWindow(urls);
                m_mainWindow->show();
            } else {
                foreach(QString url, urls) {
                    m_mainWindow->openLinkInNewTab(url);
                }
            }

            setActiveWindow(m_mainWindow);
            KUniqueApplication::newInstance();

            return 0;
        }

    private:
        QPointer<MainWindow> m_mainWindow;
};

static const char* DESC = I18N_NOOP("Safra is a web browser intending to be "
                                    "small and light, yet feature-complete and "
                                    "useful at the same time.");

int main(int argc, char** argv)
{
    KAboutData aboutData("safra", 0, ki18n("Safra"), "0.1",
                         ki18n(DESC), KAboutData::License_GPL,
                         KLocalizedString(), KLocalizedString(), QByteArray(),
                         "louai.khanji@gmail.com");
    aboutData.addAuthor(ki18n("Louai Al-Khanji"), ki18n("Project Lead"),
                        "louai.khanji@gmail.com");
    aboutData.addAuthor(ki18n("Teemu Rytilahti"), ki18n("Developer"));

    KCmdLineOptions options;
    options.add("background-only",
        ki18n("Do not create any windows, just start the background process."));
    options.add("quit",
        ki18n("Close the background process and all associated windows."));
    options.add("new-window", ki18n("Create a new top-level browser window."));
    options.add("+urls", ki18n("The URLs to load"));

    KCmdLineArgs::addCmdLineOptions(options);
    KCmdLineArgs::addStdCmdLineOptions();
    KUniqueApplication::addCmdLineOptions();
    KCmdLineArgs::init(argc, argv, &aboutData);

    Application app;
    app.setQuitOnLastWindowClosed(false);
    return app.exec();
}

#include "main.moc"
