/*
 * Copyright 2010  Jonas Emanuel Müller <zanoi@zanoi.net>
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include "config.h" //krazy:exclude=includes

#ifdef KDE
#include "mainwindowkde.h"

#include <KApplication>
#include <KAboutData>
#include <KCmdLineArgs>
#include <KMessageBox>
#include <KStandardDirs>
#include <KUrl>
#include <QString>
#else
#include "mainwindowqt.h"

#include <QApplication>
#include <QMessageBox>
#endif

#include <QSqlDatabase>
#include <QSqlError>

using namespace Videocatcher;

bool createConnection(const QString &fileName="default");

int main(int argc, char *argv[])
{

#ifdef KDE
    KAboutData aboutData(
        "videocatcher",
        0,
        ki18n("Videocatcher"),
        QString("%1.%2.%3").arg(VIDEOCATCHER_VERSION_NUMBER_MAJOR).arg(VIDEOCATCHER_VERSION_NUMBER_MINOR).arg(VIDEOCATCHER_VERSION_NUMBER_PATCH).toAscii(),
        ki18n("An Internet television application powered by KDE"),
        KAboutData::License_GPL_V2,
        ki18n("(c) 2010 Jonas Emanuel Müller"),
        KLocalizedString(),
        "",
        "zanoi@zanoi.net");
        
    aboutData.addAuthor(ki18n("Jonas Emanuel Müller"), KLocalizedString(), "zanoi@zanoi.net");
    aboutData.addCredit(ki18n("Omar Plummer"), ki18n("Patch for information panel, quick search feature and context menu"), "rkoder@gmail.com");
#else
#endif

#ifdef KDE
    KCmdLineArgs::init(argc, argv, &aboutData);
    KCmdLineOptions options;
    options.add("+[file]", ki18n("Database to use"));
    KCmdLineArgs::addCmdLineOptions(options);

    KApplication app;
#else
    QApplication app(argc, argv);
#endif

#ifdef KDE
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    if(args->count()) {
        if (!createConnection(args->url(0).url())) {
            return 1;
        }        
    } else if (!createConnection()) {
        return 1;
    }
#else
    if (!createConnection()) {
        return 1;
    }
#endif

    MainWindow* window = new MainWindow();
    window->show();

    int result = app.exec();
#if KDE_IS_VERSION(4,4,0)
    //need to delete window manually since WA_DeleteOnClose is set to false
    //and all database objects need to be destroyed before program exits
    delete window;
#endif
    return result;
}

bool createConnection(const QString &fileName)
{
    QString path;
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setHostName("localhost");
#ifdef KDE
    if(fileName == QString("default")) {
        path = KStandardDirs::locateLocal("appdata", DB_NAME);
    } else {
        path = KUrl(fileName).pathOrUrl();
    }
#else
    //TODO: Qt!
    QString path = fileName;
#endif
    db.setDatabaseName(path);
    if (!db.open()) {
#ifdef KDE
        KMessageBox::error(0, db.lastError().text(), i18n("Database Error"));
#else
        QMessageBox::critical(0, db.lastError().text(), QObject::tr("Database Error")); //krazy:exclude=qclasses
#endif
        return false;
    }

    return true;
}
