/***************************************************************************
 *   Copyright (C) 2005 by the G System Team                               *
 *   http://www.g-system.at                                                *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/


#include "GClientFactory.h"

#include <GWorldEngineFactory.h>
#include <GweController.h>

#include <GCamera.h>
#include <GOpenGLFrame.h>

#include <kapplication.h>
#include <kmainwindow.h>
#include <kcmdlineargs.h>
#include <kaboutdata.h>
#include <kmessagebox.h>

#include <qstring.h>
#include <qfile.h>
#include <qdom.h>

#include <stdlib.h>


int main(int argc, char** argv)
{
  /*
  KAboutData::KAboutData   (   const char *   appName, 
  const char *   programName, 
  const char *   version, 
  const char *   shortDescription = 0, 
  int   licenseType = License_Unknown, 
  const char *   copyrightStatement = 0, 
  const char *   text = 0, 
  const char *   homePageAddress = 0, 
  const char *   bugsEmailAddress = "submit@bugs.kde.org"
  ) 
     
  */
  
//   KCmdLineArgs::init(1,argv,"universeclient","G Universe Client","Client to the G System Universe","0.5",true);
  KAboutData about("guniverseclient","G Universe Client","0.5","Client to the G System Universe",KAboutData::License_BSD,"(c) 2003-2005, G System Team",0,"http://www.g-system.at","g-bugs@lists.g-system.at");
  KCmdLineArgs::init(&about);
  KApplication a;

//   qDebug("KDE app created");

  GWE::GWorldEngineFactory factory;

  QString default_file = "/usr/local/etc/guniverseclient.xml";
  
  QString user_home = getenv("HOME");
  
  if (!user_home.isEmpty())
  {
    default_file = user_home + "/.guniverseclient.xml";
  }
  
//   QString default_file = "/usr/home/raphael/devel/G/trunk/client.xml";
  QString filename=default_file;
  if (argc > 1)
  {
    filename = argv[1];
  }
  if (filename.length() > 8000)
  {
    qWarning("filename argument too long, using built-in default");
    filename=default_file;
  }
  
  qDebug(QString("Using configuration file: %1").arg(filename));
  qDebug("Note: you can specify the path to a desired configuration file as first parameter");
  qDebug("      if the default doesn't work.");

  QFile file(filename);
  
  if (file.open(IO_ReadOnly))
  {
    qDebug(QString("file opened in read only mode: ") + filename);
    QDomDocument xml;
    QString error_msg;
    int line,col;
    if (!xml.setContent(&file,&error_msg,&line,&col))
    {
      qWarning("failed to load XML data from file!");
      qWarning(QString::number(line) + ":" + QString::number(col) + ": " + error_msg);
      QTimer::singleShot(10,&a,SLOT(quit()));
    }
    else
    {
      factory.getRootOption()->loadFromXml(xml.documentElement());
      qDebug("GWE factory settings loaded from file");
    }
    file.close();
    
    factory.getRootOption()->updateTree();

    GWE::GweController* gwe = factory.init();
    if (gwe == NULL)
    {
      qWarning("failed to initialize the GWE");
      QTimer::singleShot(10,&a,SLOT(quit()));
    }

    QObject::connect(&a,SIGNAL(lastWindowClosed()),gwe,SLOT(shutdown()));
    QObject::connect(gwe,SIGNAL(quit()),&a,SLOT(quit()));

    //DO NOT USE THE STACK, IT WOULD BE DELETED AT THE END OF THE IF CLAUSE
    GClientFactory* clientfactory = new GClientFactory();
    clientfactory->init(gwe);
  }
  else
  {
    qWarning(QString("failed to load configuration file: ") + filename);
    KMessageBox::error(0,QString("The configuration %1 could not be loaded, please run \"god\" to create the desired configuration! Then restart the client. Details can be found in the G System README file.\n\nIf you don't know what to do, contact us at http://www.g-system.at/").arg(filename),"Client not configured!");
    QTimer::singleShot(10,&a,SLOT(quit()));
  }
  

  return a.exec();
}


syntax highlighted by Code2HTML, v. 0.9.1