/****************************************************************************
**
**  main.cpp             of the IndeView project, see http://www.indeview.org
**
**
**  The IndeView application is a stand-alone viewer to display presentations
**  created by IndeView creator modules like IndeViewExport.bas or kpr2iv.sh.
**  -------------------------------------------------------------------------
**  This enables you to show OpenOffice/StarOffice or KOffice presentations
**  on machines where the respective presentation program is not available.
**
**
**  Copyright (C) 2003..2004 Michael & Karl-Heinz Zimmer.
**  All rights reserved.
**
**  This file is distributed under the terms of the
**  GNU Lesser General Public License version 2.1 as published by the
**  Free Software Foundation and appearing in the file LICENSE included
**  in the packaging of this file.
**
**  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
**  THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
**  PURPOSE.
**
**  See http://www.indeview.org for further information.
**
**  Contact info@indeview.org if any conditions of this
**  licensing are not clear to you.
**
**
**  Please consider sending
**  a little picture postcard to   Michael & Karl-Heinz Zimmer
**                                 Butzengasse 15
**                                 54343 Föhren
**                                 Germany
**
**  Your feedback will be very much appreciated: we would be glad about
**  receiving a visual representation of where on earth you are living
**  using our little module.
**
*****************************************************************************/

#include <qapplication.h>
#include <qwidget.h>
#include <qurl.h>
#include <qfile.h>
#include <qmessagebox.h>

#include "indeview_main.h"


IndeViewMain* getIVMain( const QStringList& params, const QString& ctrlFile )
{
  IndeViewSlides* slides = new IndeViewSlides( ctrlFile );
  if( slides->structureOK() )
    return new IndeViewMain( params, slides, true );
  delete slides;
  return 0;
}

bool changePresentation( IndeViewWidget& widget, const QString& ctrlFile )
{
  IndeViewSlides* slides = new IndeViewSlides( ctrlFile );
  const bool bOK = slides->structureOK();
  if( bOK )
    widget.setSlides( slides, true );
  else
    delete slides;
  return bOK;
}


void showVersion( QApplication& a )
{
  QMessageBox::information(a.desktop(), "IndeView", "<b>Independent Presentation Viewer</b><br>&nbsp;<br>Version 0.6.1<br>&nbsp;<br>released: 02.02.2004<br>&nbsp;<br>learn more: http://www.indeview.org");
}

void showHelp( QApplication& a )
{
  showVersion( a );  
}

int main( int argc, char **argv )
{
  int res = 0;

  QApplication a( argc, argv );

  QString ctrlFile;
  QStringList params;
  if( argc <= 1 ){
    ctrlFile = "./INDEVIEW.RUN";
    if( !QFile::exists( ctrlFile )){
      QString path(a.applicationDirPath());
      // look in the application binary's directory and in all of it's parent directories
      ctrlFile = path + "/INDEVIEW.RUN";
      int i = path.findRev("/");
      while( !QFile::exists( ctrlFile ) && -1<i ){
        path.truncate(i);
        ctrlFile = path + "/INDEVIEW.RUN";
        i = path.findRev("/");
      }
      if( !QFile::exists( ctrlFile ) ){
        ctrlFile.truncate(0);
        QMessageBox::critical(a.desktop(), "IndeView", "Sorry, no presentation found.\n\nIf the file INDEVIEW.RUN is NOT located\nin the same directory as the IndeView program\n(or in one of it's parent directories, resp.)\nit must be specified as command parameter.\n\nNote that you may use any filename as long as it does not start with a Minus (\"-\") character.");
        res = 1;
      }
    }
  }else{
    for ( int i=1; i<argc; i++ ){
      QString s = QString(argv[i]).stripWhiteSpace();
      if( s.startsWith("-") ){
        const QString sU( s.upper() );
        if( sU == "-V" || sU == "--VERSION" ){
          showVersion( a );
          return 0;
        }
        if( sU == "-H" || sU == "--HELP" ){
          showHelp( a );
          return 0;
        }
        params << s;
      }else{
        if( s.startsWith("@") )
          ctrlFile = a.applicationDirPath() + "/" + s.mid(1);
        else
          ctrlFile = s;
      }
    }
  }
  if( !ctrlFile.isEmpty() ){
    IndeViewMain* w = getIVMain( params, ctrlFile );
    if( w ){
      w->show();
      w->initAll();

      QObject::connect(qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()));
      res = a.exec();
    }else{
      QMessageBox::critical(a.desktop(), "IndeView", "Sorry, presentation could not be read");
      res = 2;
    }
  }
  return res;
}
