/***************************************************************************
 *   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 <GWorldEngineFactory.h>
#include <GweController.h>
#include <GXmlDataController.h>

#include <GElement.h>
#include <GElementID.h>
#include <GMatrix44.h>
#include <GVector3.h>
#include <GObject.h>
#include <GForm.h>
#include <GEnergy.h>

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

#include <qapplication.h>

#include <stdio.h>
#include <stdlib.h>

#include <exception>

void myMessageOutput( QtMsgType type, const char *msg )  //this one is from the Qt documentation, see QApplication
{
  switch ( type )
  {
    case QtDebugMsg:
      fprintf( stdout, "%s\n", msg );
      break;
    case QtWarningMsg:
//       fprintf( stdout, "guniverse: Warning: %s\n", msg );
      fprintf( stderr, "Warning: %s\n", msg );
      break;
    case QtFatalMsg:
//       fprintf( stdout, "guniverse: Fatal: %s\n", msg );
      fprintf( stderr, "Fatal: %s\n", msg );
      abort();                    // deliberately core dump
    
    /*  // this produces core dumps... ??
    case QtDebugMsg:
      fprintf( stdout, "guniverse, %s: %s\n", QDateTime::currentDateTime().toString().local8Bit(), msg );
      break;
    case QtWarningMsg:
      fprintf( stdout, "guniverse, Warning: %s: %s\n", QDateTime::currentDateTime().toString().local8Bit(), msg );
      fprintf( stderr, "guniverse, Warning: %s: %s\n", QDateTime::currentDateTime().toString().local8Bit(), msg );
      break;
    case QtFatalMsg:
      fprintf( stdout, "guniverse, Fatal: %s: %s\n", QDateTime::currentDateTime().toString().local8Bit(), msg );
      fprintf( stderr, "guniverse, Fatal: %s: %s\n", QDateTime::currentDateTime().toString().local8Bit(), msg );
      abort();                    // deliberately core dump
    */
  }
}


int main(int argc, char** argv)
{
  qInstallMsgHandler( myMessageOutput );
  QApplication a(argc,argv,false);
  
  GWE::GWorldEngineFactory factory;
  
  //let's see if we can load some configuration...
  QString filename("/usr/local/etc/gweconfig.xml");  //default
  if (argc > 1)
  {
    filename=argv[1];
  }
  else
  {
    qWarning(QString("no filename given, using ") + filename);
    qWarning(" note: you can specify a configuration filename as first parameter");
  }
  
  if (filename.length() > 8000)
  {
    qFatal("filename too long!!!");
  }
  
  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!");
      qFatal(QString::number(line) + ":" + QString::number(col) + ": " + error_msg);
    }
    else
    {
      factory.getRootOption()->loadFromXml(xml.documentElement());
      qDebug("GWE factory settings loaded from file");
    }
    file.close();
  }
  else
  {
    qWarning(QString("failed to load configuration file: ") + filename);
    return 1;
  }
  
  qDebug("updating factory options tree");
  factory.getRootOption()->updateTree();
  
  qDebug("initializing the GWE with the factory");
  GWE::GweController* gwe = factory.init();
  if (gwe == NULL)
  {
    qFatal("failed to initialize the GWE");
  }
  
  QObject::connect(gwe,SIGNAL(quit()),&a,SLOT(quit()));

  GWE::GDataController* dc = gwe->getDataController();

  try
  {
    GWE::GXmlDataController* xdc = dynamic_cast<GWE::GXmlDataController*>(dc);
    //if there is no master server, initialize free element IDs...
    if (xdc->isMasterServer())
    {
      qDebug("This server is the MASTER server, creating galaxy object, with energy(10,100000,10) and an ellipsoid of (100000,100000,100000)!");

      GCS::GElementID galaxy_id = GCS::GElementID::getFreeID();

      GCS::GElement* galaxy =
          new GCS::GElement(
          new GCS::GObject(new GCS::GEnergy(10,100000,10),
//                            NULL,  //no form, we're boundless :-)
                           new GCS::GForm(GCS::GVector3(0,0,0),
                                          GCS::GVector3(0,0,0),
                                          GCS::GVector3(100000,100000,100000)),
                           GCS::GElementID(galaxy_id),  //galaxy as parent
                           GCS::GElementID(galaxy_id),  //own ID
                           GCS::GElementID(galaxy_id),  //connect to the parent (connectionID is the same as ID of the galaxy element)
                           new QDomDocument(),
                           dc));

      Q_CHECK_PTR(galaxy);

    //no agents for now...
    
      if (galaxy)
      {
        dc->add(galaxy);
      }
    }
  }
  catch (std::exception e)
  {
  }
  
  qDebug("starting the Qt event loop");
  return a.exec();
}


syntax highlighted by Code2HTML, v. 0.9.1