/***************************************************************************
 *   Copyright (C) 2005 Novell, Inc.                                       *
 *                                                                         *
 *   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 <kerryapp.h>
#include <QObject>
#include <QDir>
#include <qdom.h>

#include <kaboutdata.h>
#include <kcmdlineargs.h>
#include <klocale.h>
#include <kconfig.h>
#include <QFileInfo>
#include <QStringList>
#include <kconfig.h>
#include <kstandarddirs.h>
#include <kdeversion.h>
#include <kconfig.h>
#include <ksessionmanager.h>
#include <unistd.h>

static const char *description =
    I18N_NOOP("KDE Frontend to Beagle");

static const char *version = "0.2.90";

bool KSessionManager::saveState(QSessionManager&)
{
//  KConfig* config = kapp->sessionConfig();
//  config->setGroup("General");
//  config->writeEntry("LastSearches", "foobar");

    return true;
}

bool KSessionManager::commitData(QSessionManager&)
{
    return true;
}

int main(int argc, char **argv)
{
    if (!getuid()) {
      QFile file( "/root/.beagle/config/daemon.xml" );
      bool fail = true;
      if ( file.open( QIODevice::ReadOnly ) ) {
        QDomDocument doc( "mydocument" );
        if ( doc.setContent( &file ) ) {
          QDomElement docElem = doc.documentElement();
          QDomNode n = docElem.firstChild();
          while( !n.isNull() ) {
            QDomElement e = n.toElement();
            if( !e.isNull() && e.tagName()=="AllowRoot")
                 fail = e.text()!="true";
            n = n.nextSibling();
          }
        }
        file.close();
      }
      if (fail) {
        printf("beagled will not run as root. Kerry will quit now because of that.\n");
        return 1;
      }
    }

    KAboutData about("kerry", 0, ki18n("Kerry Beagle Search"),
                     version, ki18n(description),
                     KAboutData::License_GPL,
                     ki18n("(c) 2005-2007 Novell, Inc."),
                     KLocalizedString(), "http://opensuse.org/kerry");
    about.addAuthor(ki18n("Stephan Binner"), KLocalizedString(), "binner@kde.org");

    KCmdLineOptions options;
    options.add("+term", ki18n("A term to search"), 0);
    options.add("show-searchdialog", ki18n( "Show search dialog on startup" ), 0);

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

    KSessionManager ksm;

    KerryApplication app;
    KGlobal::locale()->insertCatalog("libkonq");

    return app.exec();
}
