// $Id: main.cpp,v 1.43 2008/03/20 01:50:09 hvengel Exp $
//  Little cms - profiler construction set
//Copyright (C) 2005-2007 Hal Engel
//
// THIS SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
// WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
// INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
// OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
// LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
// OF THIS SOFTWARE.
//
// 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 3 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 <qapplication.h>
#include <qdir.h>
#include <qtranslator.h>
#include <qtextcodec.h>
#include "main.h"
#include <profileparms.h>
#include <lprofmain.h>
#include <installreffile.h>
#include <qsettings.h>
#include <QSplashScreen>

#ifdef __cplusplus
extern "C" {
#endif

#include "config.h"
#include "inst.h"
#include "ioerrorhandler.h"
#include "numsup.h"

#ifdef __cplusplus
}
#endif

#include "spyd2setup.h"
                 
QString appExecDir;

QApplication *lprofApp;

bool checkSpyd2OK()
{
    QString exec;    
    char execPath[512];
    
    exec = getAppExecDir() + QString::fromLocal8Bit("/");
    strcpy(execPath, exec.local8Bit());
    if (setup_spyd2(execPath) == 0)
        // Device will not work.
        return FALSE;
    else
        return TRUE;    
}

QString getAppExecDir()
{
    return appExecDir;
}

void set_Current_Dir(const QString a)
{	
    QString prefix_path;

    prefix_path=a;
    QString files;

     QDir* dir = new QDir ();

     // Find the path to the data directory.  It is either in the build directory
     // or in prefix/share/lprof/data or the /data directory of the Lprof bundle on OS X.  
	 // Check for build directory first.
     // Need to do this not only for creating the users config directory
     // but also so the help system can locate the help files.
     // Otherwise it would be inside of the if below
     prefix_path.truncate(prefix_path.find( QString::fromLocal8Bit("/build") ));
     prefix_path = prefix_path + QString::fromLocal8Bit("/data");
     if ( dir->exists(prefix_path) )
     {
         prefix_path = prefix_path + QString::fromLocal8Bit("/");
     }
     else
     {
# ifdef __APPLE__
          // not in the build directory so need to check for the /data subfolder of the bundle
          prefix_path = a;
          prefix_path = prefix_path + QString::fromLocal8Bit("/data/");
# else
          // not in the build directory so need to check for and remove /bin
          prefix_path = a;
          prefix_path.truncate(prefix_path.length()-3);
          prefix_path = prefix_path + QString::fromLocal8Bit("share/lprof/data/");
# endif /* __APPLE__ */	 
     }
     dir->setCurrent(prefix_path);  // used to find help files later on
     delete dir;
}

int main( int argc, char **argv )
{
    char execPath[512];
    
    lprofApp = new QApplication ( argc, argv );

    appExecDir = lprofApp -> applicationDirPath();
    set_Current_Dir(lprofApp -> applicationDirPath());
	
    QTranslator translator( 0 );
    // QString::fromLocal8Bit translations = ":/translations/lprof_" + (QString::fromLocal8Bit) QTextCodec::locale();
    translator.load( QString::fromLocal8Bit("lprof_") + QString::fromLocal8Bit(QTextCodec::locale()), QString::fromLocal8Bit(":/") );
    lprofApp -> installTranslator( &translator );
    
    QString temp = getAppExecDir() + QString::fromLocal8Bit("/");
    strcpy(execPath, temp.toLocal8Bit());
    setup_spyd2(execPath);
    
    QPixmap splashPixmap(QString::fromLocal8Bit(":src/images/logo.png"));   
    if (splashPixmap.isNull())
        qDebug("Logo pixmap is null"); 
    QSplashScreen* splash = new QSplashScreen(splashPixmap);
    splash -> show(); 
    QApplication::processEvents();

    lprofMain w;
    w.show();
    splash->finish(&w);
    delete splash;
    lprofApp -> connect( lprofApp, SIGNAL( lastWindowClosed() ), lprofApp, SLOT( quit() ) );
    
    // Setup AryllCMS error handling
    // This will trap AryllCMS errors system wide if
    // setjmp(ioErrorJumpBuffer) is called first in a given function.
    // see src/moncalqt/monitorcal.cpp 
    // MonitorCal::initInstrument() for an example
    error = errorArgyllCMS;
    
    return lprofApp -> exec();
}
