// $Id: config.cpp,v 1.8 2008/03/08 22:31:11 hvengel Exp $
//  
//  Lttle cms Profiler
// 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/>.
//
// Version 1.11

#include "lprofmain-priv.h"
#include "lprofmain.h"
#include "qtlcmswidgets.h" 

#include <qdir.h>
#include <qlayout.h>
#include <qmessagebox.h>
#include <qspinbox.h>
#include <qcombobox.h>
#include <qlineedit.h>
#include <qtextcodec.h>
#include <q3urloperator.h>
#include <QSettings>
#include <qassistantclient.h>
#include <qradiobutton.h>
#include <QDesktopWidget>
//Added by qt3to4:
#include <QTranslator>
#include <Q3VBoxLayout>

#include "ui_lprofmainbase.h"

Q3UrlOperator urlOp;

#if !defined(__WIN32__) && (defined(_WIN32) || defined(WIN32))
#define __WIN32__
#endif

 
#if defined (__APPLE__)
    const QString lprofBaseDir = QString::fromLocal8Bit("/Library/Application Support/lprof");
#else
    const QString lprofBaseDir = QString::fromLocal8Bit("/.lprof");
#endif

static QMutex mutexTempFile;

QString get_QTDIR()
{
    // qDebug(QString::fromLocal8Bit("get_home_dir"));
    const char* qtdir= "QTDIR";
    return QString::fromLocal8Bit(getenv(qtdir));
}

void init_env()
{
    // qDebug(QString::fromLocal8Bit("lprofMain: init_env()"));
    
    const QString ConfigFileconst= lprofBaseDir + QString::fromLocal8Bit("/config/lprof.cfg");
    const QString measfileconst= lprofBaseDir + QString::fromLocal8Bit("/temp/measurement.cgt");
    const QString WorkTempFileNameconst = lprofBaseDir + QString::fromLocal8Bit("/temp/$$q60$$.$$$");
    const QString templateconst = lprofBaseDir + QString::fromLocal8Bit("/templates");
    const QString measure_dir_const = lprofBaseDir + QString::fromLocal8Bit("/measurements");
    const QString monMeasFile_const = lprofBaseDir + QString::fromLocal8Bit("/temp/monmeasurement.cgt");
    const QString monCalFile_const = lprofBaseDir + QString::fromLocal8Bit("/temp/moncalibration.cgt");
    const QString tempDir_const = lprofBaseDir + QString::fromLocal8Bit("/temp");
    const QString ref_dirconst = lprofBaseDir + QString::fromLocal8Bit("/target_refs");
    QDir d;
# ifdef __WIN32__
    QString win = getenv(QString::fromLocal8Bit("SystemRoot"));
        // lets use only forward slashes, not backslashes
    win.replace( '\\', "/" );
    win +=  QString::fromLocal8Bit("/system32/spool/drivers/color");
    mon_profile = win;
    in_profile= win;
    outprofile = win;
# elif defined (__APPLE__)
    mon_profile = QString::fromLocal8Bit("/Library/ColorSync/Profiles");
    in_profile = QString::fromLocal8Bit("/Library/ColorSync/Profiles");
    outprofile = d.homeDirPath() + QString::fromLocal8Bit("/Library/ColorSync/Profiles");
# else
    mon_profile = QString::fromLocal8Bit("/usr/share/color/icc");
    in_profile = QString::fromLocal8Bit("/usr/share/color/icc");
    outprofile = d.homeDirPath() + QString::fromLocal8Bit("/.color/icc");
# endif /* __WIN32__ */
        
    QString home = d.homeDirPath();

    measurement_dir = home + measure_dir_const;

    measfile = home + measfileconst;
    
    templates = home + templateconst;
    
    ref_dir = home + ref_dirconst;
    
    temp_dir = home + tempDir_const;
    
    WorkTempFileName = home + WorkTempFileNameconst;
    
    strcpy(sys.hdr.ref_dir, (const char*)ref_dir.toLocal8Bit());

    strcpy(sys.hdr.temp_file, (home + WorkTempFileNameconst).toLocal8Bit());
    strcpy(sys.hdr.MonMeasurementSheet, (home + monMeasFile_const).toLocal8Bit());
    strcpy(monmeasFile, sys.hdr.MonMeasurementSheet);
    strcpy(sys.hdr.MonCalibrationSheet, (home + monCalFile_const).toLocal8Bit());
    strcpy(moncalFile, sys.hdr.MonCalibrationSheet);
    
    // qDebug(QString::fromLocal8Bit("lprofMain: init_env() done"));

}

void lprofMain::init_mon()
{
    // qDebug(QString::fromLocal8Bit("init_mon"));
    monitorValues.items.wppos=0;
    monitorValues.items.ppos=0;
    monitorValues.items.temp=0;
    monitorValues.items.linked=1;
    monitorValues.items.rchecked=TRUE;
    monitorValues.items.gchecked=FALSE;
    monitorValues.items.bchecked=FALSE;
    monitorValues.items.rgamma= 2.4;
    monitorValues.items.ggamma= 2.4;
    monitorValues.items.bgamma= 2.4;
}

void init_marks()
{
    Marks.ur_x=measure_view_H;
    Marks.ur_y=0;

    Marks.ul_x=0;
    Marks.ul_y=0;

    Marks.lr_x=measure_view_H;
    Marks.lr_y=measure_view_V;

    Marks.ll_x=0;
    Marks.ll_y=measure_view_H;

    corners_ready=FALSE;

}

void lprofMain::init_measurement_tool()
{
    // qDebug(QString::fromLocal8Bit("lprofMain::init_measurement_tool"));
    TheImage        = NULL;
    TheGrid         = NULL;
    CurrentTemplate = QString::fromLocal8Bit("*");
    CurrentSafeFrame = -1;

    int w = ui->CanvasFrame -> width();
    int h = ui->CanvasFrame -> height();
    if (CanvasFrameLayout) delete CanvasFrameLayout;
    CanvasFrameLayout =new Q3VBoxLayout( ui->CanvasFrame );
    CanvasFrameLayout->setSpacing( 1 );
    CanvasFrameLayout->setMargin( 1 );
    if (Canvas) delete Canvas;
    Canvas = new Q3Canvas(measure_view_H, measure_view_V);
    Canvas -> setAdvancePeriod(30);
    if (FigEditor) delete FigEditor;
    FigEditor = new FigureEditor(*this, *Canvas, ui->CanvasFrame);
    QObject::connect(FigEditor, SIGNAL(WPPointsSelected()), this, SLOT(setRGBpatchValues()));
    FigEditor -> clear();
    FigEditor-> resize(w, h);
    FigEditor->setHScrollBarMode(Q3ScrollView::Auto);
    FigEditor->setVScrollBarMode(Q3ScrollView::Auto);

    CanvasFrameLayout->addWidget(FigEditor);

}

void setupHelp(QString &g_path)
{
    bool found = false;
    bool done = false;
    
# ifdef __WIN32__
    QChar pathSep = QChar::fromLatin1(';');
    const QString assist = QString::fromLocal8Bit("assistant.exe");
#elif defined (__APPLE__)
    QChar pathSep = QChar::fromLatin1(':');
    const QString assist = QString::fromLocal8Bit("assistant.app/Contents/MacOS/assistant");
# else
    QChar pathSep = QChar::fromLatin1(':');
    const QString assist = QString::fromLocal8Bit("assistant");
# endif /* __WIN32__ */
    
    QString Path = QString::fromLocal8Bit(getenv("PATH"));
    QString execpath = get_QTDIR() + QString::fromLocal8Bit("/bin");
    QFileInfo fileinfo;

    while (!done)
    {
        fileinfo.setFile(execpath + QString::fromLocal8Bit("/") +  assist);
        if (fileinfo.exists())
        {
            done = true;
            found=true;
        }
        else
        {
            if (Path.length() <=0 || (Path.length() == execpath.length())) done=true;
            else
            {
                execpath = Path.left(Path.find(pathSep));
                Path = Path.right(Path.length() - Path.find(pathSep) - 1);
            }
        }
    }
    QStringList cmdLst;

    if (!found)  // not is QTDIR/bin and not in system PATH
                 // must be Windows.  Look in same location
                 // as lprof.exe
    {
        // FIXME may not be corrected
        // currentDirPath should point to <prefix>/share/lprof/data
        // execution path should be <prefix>/bin
        
        execpath = QDir::currentDirPath().replace(QDir::currentDirPath().length() - 16, 16, QString::fromLocal8Bit("bin"));
        fileinfo.setFile(execpath + QString::fromLocal8Bit("/") +  assist);
        if (fileinfo.exists()) 
            found=true;
        else
        {
            execpath = lprofApp -> applicationDirPath ();  // is it in the same location as lprof?
            fileinfo.setFile(execpath + QString::fromLocal8Bit("/") +  assist);
            if (fileinfo.exists()) 
                found=true;
        }
    }
    if (help) delete help;
    help = new QAssistantClient(execpath);  // so that help calls in other locations don't blow up
    if (found)
    {
       // setup help directory for current system language code

       // If QTextCodec::locale() is not supprted then default to english help files
       // currently only english and Russian is supported

        QString path;
        QString test = QString::fromLocal8Bit(QTextCodec::locale());
        if ((test.find((QString::fromLocal8Bit("C")), 0, false) >= 0))
             path = QDir::currentDirPath() + QString::fromLocal8Bit("/help/") +  QString::fromLocal8Bit("en");
        else
            if (test == QString::fromLocal8Bit("ru"))
                path = QDir::currentDirPath() + QString::fromLocal8Bit("/help/") +  QString::fromLocal8Bit(QTextCodec::locale());
        else // since at this point only english and Russian
               // help pages exist
               // everyone else still gets english help
               // This will need to be updated whenever a new
               // language is added to the help files
            path = QDir::currentDirPath() + QString::fromLocal8Bit("/help/") +  QString::fromLocal8Bit("en");
        g_path.insert(0, path);

        cmdLst << QString::fromLocal8Bit("-profile") << (path + /* QDir::separator() + */ QString::fromLocal8Bit("/lprof-help.adp"));
        help->setArguments( cmdLst );
              
        if (!QFile::exists(path + QString::fromLocal8Bit("/lprof-help.html")))
        {
            QMessageBox mb( QTranslator::tr("Help system files not found"),
                            QTranslator::tr("The path to the help files is not correct.  Please contact LProf support with the contents of this message.  The current help path is set to:\n") + path,
                                    QMessageBox::Critical,
                                    QMessageBox::Ok | QMessageBox::Default,
                                    Qt::NoButton,  Qt::NoButton);

            mb.exec();
        }
    }
    else  // let the user know that help will not work
    {
        QMessageBox mb( QTranslator::tr("Help system executable not found"),
                        QTranslator::tr("The QAssistantClient executable was not found.  Make sure that assistant(.exe)is located either in your PATH or in the $QTDIR/bin directory.   Help will not be availble until this is corrected." ),
                                QMessageBox::Critical,
                                QMessageBox::Ok | QMessageBox::Default,
                                Qt::NoButton,  Qt::NoButton);

        mb.exec();
    }
}

void lprofMain::templateFileCopied( Q3NetworkOperation *op )
{
    // qDebug(QString::fromLocal8Bit("templateFileCopied()"));

    if ( !op )
        return;

    if ( op->state() == Q3NetworkProtocol::StFailed )
    {
        // The copy operation failed
        // Let users know there is a problem
        // qDebug(QString::fromLocal8Bit("Error on template file copy"));
        QMessageBox::critical( this, tr( "ERROR" ), op->protocolDetail() );
    }
    else if(op->operation () == Q3NetworkProtocol::OpPut )
    {
        // qDebug("Template file saved - operation successful. %i", copyNumTempFile);
        mutexTempFile.lock();
        copyNumTempFile = copyNumTempFile - 1;
        mutexTempFile.unlock();
    }
    // qDebug("copyNumTempFile = %i", copyNumTempFile);

}

void lprofMain::Create_Config_Dir(const QString data_path)
{
    QString files;
    QDir* dir = new QDir (dir->homeDirPath());
    
    // Check to see if the home directory exists and is writable
    // if not issue an error message and return
    if (!dir -> exists() || !dir -> isReadable())
    {
        QMessageBox mb( QTranslator::tr("Problem With Home Directory"),
                        QTranslator::tr("Home directory ") + dir->homeDirPath() + QTranslator::tr(" does not exist or can not be writen to.  LProf will not function correctly until this is condition is corrected"),
                                        QMessageBox::Critical,
                                        QMessageBox::Ok | QMessageBox::Default,
                                        Qt::NoButton,  Qt::NoButton);
        mb.exec();
        return;
    }

    // check to see if config directory exists
    // if not create it and move template files into it
    // home_path =  dir->homeDirPath() + QString::fromLocal8Bit("/") + QString::fromLocal8Bit(".lprof"));
    if (!dir->exists(dir->homeDirPath() + lprofBaseDir))
    {
        if (!dir->mkdir(dir->homeDirPath() + lprofBaseDir, TRUE))
        {
            QMessageBox mb( QTranslator::tr("Can Not Create Configuration Directory"),
                            QTranslator::tr("LProf can not create ") + dir->homeDirPath() + lprofBaseDir + QTranslator::tr(" LProf will not function correctly until this is condition is corrected"),
                                            QMessageBox::Critical,
                                            QMessageBox::Ok | QMessageBox::Default,
                                            Qt::NoButton,  Qt::NoButton);
            mb.exec();
            return;
        }

# ifdef __WIN32__
            // hide the user configuration directory
        QString home_path = dir->homeDirPath() + lprofBaseDir;
        files = QString::fromLocal8Bit("attrib +h \"")) +
                dir->convertSeparators(home_path) + QString::fromLocal8Bit("\""));
        system(files.latin1());
# endif /* __WIN32__ */

        if (!dir->exists(dir->homeDirPath() + lprofBaseDir + QString::fromLocal8Bit("/config")))
            dir->mkdir(dir->homeDirPath() + lprofBaseDir + QString::fromLocal8Bit("/config"),  TRUE);

        if (!dir->exists(dir->homeDirPath() + lprofBaseDir + QString::fromLocal8Bit("/target_refs")))
            dir->mkdir(dir->homeDirPath() + lprofBaseDir + QString::fromLocal8Bit("/target_refs"),  TRUE);

        if (!dir->exists(dir->homeDirPath() + lprofBaseDir + QString::fromLocal8Bit("/temp")))
            dir->mkdir(dir->homeDirPath() + lprofBaseDir + QString::fromLocal8Bit("/temp"),  TRUE);

        if (!dir->exists(dir->homeDirPath() + lprofBaseDir + QString::fromLocal8Bit("/templates")))
            dir->mkdir(dir->homeDirPath() + lprofBaseDir + QString::fromLocal8Bit("/templates"),  TRUE);

        if (!dir->exists(dir->homeDirPath() + lprofBaseDir + QString::fromLocal8Bit("/measurements")))
            dir->mkdir(dir->homeDirPath() + lprofBaseDir + QString::fromLocal8Bit("/measurements"),  TRUE);
    }
    // Copy the template files into the users config directory
    // but first check to make sure the data/templates directory is found
    if (dir->exists(data_path + QString::fromLocal8Bit("/template/")))
    {
        QFile f;
        QDir d (data_path + QString::fromLocal8Bit("/template/"));
        d.setFilter(QDir::Files | QDir::Hidden);
        const QFileInfoList& list = d.entryInfoList();
        QList<QFileInfo>::const_iterator itEnd = list.end();
        for ( QList<QFileInfo>::const_iterator it = list.begin(); it != itEnd; ++it)
        {
            if ((*it).fileName().contains(QString::fromLocal8Bit("ITX"), FALSE))
            {
                f.setName(dir->homeDirPath() +
                        lprofBaseDir + QString::fromLocal8Bit("/templates/") +
                        (*it).fileName());
                if (!f.exists())
                {
                    mutexTempFile.lock();
                    copyNumTempFile = copyNumTempFile + 1;
                    urlOpTempFile.copy( data_path + QString::fromLocal8Bit("/template/") +
                            (*it).fileName(), dir->homeDirPath() +
                            lprofBaseDir + QString::fromLocal8Bit("/templates/"));
                    mutexTempFile.unlock();
                }
            }
        }
    } 
}

void lprofMain::templateFilesExist()
{
    // qDebug(QString::fromLocal8Bit("lprofMain::templateFilesExist()"));
    // Copy the template files into the users config directory
    bool OK = true;
    QFile f;
    
    QDir d (QDir::currentDirPath() + QString::fromLocal8Bit("/template/"));
    if (!d.exists())
    {
        QMessageBox::critical(this, QTranslator::tr("data/templates directory not found."), 
                              QTranslator::tr("This means that something is wrong with the LProf installation. LProf may not fuction correctly. Please contact LProf support for assitance in correcting the problem."));
    }
    else
    {
        d.setFilter(QDir::Files | QDir::Hidden);
        const QFileInfoList &list = d.entryInfoList();
        QList<QFileInfo>::const_iterator itEnd = list.end();
        for ( QList<QFileInfo>::const_iterator it = list.begin(); it != itEnd; ++it)
        {
            if ((*it).fileName().contains(QString::fromLocal8Bit("ITX"), FALSE))
            {
                f.setName(d.homeDirPath() +
                        lprofBaseDir + QString::fromLocal8Bit("/templates/") + 
                        (*it).fileName());
                if (!f.exists())
                {
                    OK = false;
                }
            }
        }
        if (!OK)
            QMessageBox::critical(this, QTranslator::tr("Pick Template Files Not Installed"), 
                                  QTranslator::tr("One or more pick templates are missing.\nAs a result LProf may not run correctly."));
    }
    // qDebug(QString::fromLocal8Bit("done lprofMain::templateFilesExist()"));
}

void lprofMain::ReferenceFileExists()
{
    QDir dir;
    dir.setPath(ref_dir);
     // qDebug(ref_dir);
     //  qDebug("Reference file count = %i", dir.entryList().count());
    if (dir.entryList().count() == 2)  // because . and .. are counted
    {
        QMessageBox::warning(this, QTranslator::tr("No reference files found"),
                             QTranslator::tr("Please use the reference file installer to "
                                     "install one or more reference files.  "
                                     "You will not be able to use the Camera/Scanner "
                                     "profiler until at least one reference file is  "
                                     "installed." ));

    }
}

BOOL lprofMain::LoadConfig()
{
    // qDebug(QString::fromLocal8Bit("lprofMain::LoadConfig()"));
    QString temp;
    target_template hold;

    // qDebug(QString::fromLocal8Bit("lprofMain::LoadConfig"));
    if (lprofSettings) delete lprofSettings;
    
    QSettings::setPath(QSettings::NativeFormat, QSettings::UserScope,  QDir::homeDirPath() + lprofBaseDir + QString::fromLocal8Bit("/config"));
    lprofSettings = new QSettings(QString::fromLocal8Bit("lprof.sourceforge.net"), QString::fromLocal8Bit("lprof"));
    lprofSettings -> setFallbacksEnabled (FALSE);
    
    ui->SelectTemplate-> Dir = lprofSettings->readEntry(QString::fromLocal8Bit("/LPROF/Files/TEMPLATE_DIR"), QString::fromLocal8Bit(""));

    // do set up serial devices
    // FIXME
    // this is slow perhaps this could be forked as a thread?
    populatePortandInstListView();
    
    if (ui->SelectTemplate->Dir.length() > 0)
    {
        ui->SelectTemplate->setCurrItem(0);

        ui->MonitorProfileSelector -> Dir = lprofSettings->readEntry( QString::fromLocal8Bit("/LPROF/Files/MONITOR_PROFILES_DIR"),  QString::fromLocal8Bit(""));
        ui->MonitorProfileSelector -> RestrictColorspace = icSigRgbData;
        ui->MonitorProfileSelector -> RestrictClass = icSigDisplayClass;
        ui->MonitorProfileSelector -> FillValues();
        temp = lprofSettings->readEntry( QString::fromLocal8Bit("/LPROF/Files/MONITOR_PROFILE"),  QString::fromLocal8Bit(""));
        
        ui->MonitorProfileSelector -> setFilename(lprofSettings->readEntry( QString::fromLocal8Bit("/LPROF/Files/MONITOR_PROFILE"),  QString::fromLocal8Bit("")));

        ui->InputProfileSelector->Dir = lprofSettings->readEntry( QString::fromLocal8Bit("/LPROF/Files/INPUT_PROFILES_DIR") ,  QString::fromLocal8Bit(""));
        ui->InputProfileSelector-> RestrictColorspace = icSigRgbData;
        ui->InputProfileSelector->FillValues();
        ui->InputProfileSelector->setFilename(lprofSettings->readEntry( QString::fromLocal8Bit("/LPROF/Files/INPUT_PROFILE") ,  QString::fromLocal8Bit("")));

        ui->SafeFrame -> setValue(lprofSettings->readNumEntry( QString::fromLocal8Bit("/LPROF/numbers/SAFE_FRAME"),  0));

        ui->TargetSelector->Dir = lprofSettings->readEntry(QString::fromLocal8Bit("/LPROF/Files/TARGET_DIR"), QString::fromLocal8Bit(""));
       
        ui->TargetSelector->setCurrItem(0);

        targetList->clear();
        const int n=lprofSettings->readNumEntry( QString::fromLocal8Bit("/LPROF/numbers/NUMBER_TARGETS"),  0);
        ui->ComboBoxIntent -> setCurrentItem (lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/intent_curr_item"), 0));

        for (int i=0; i < n; i++)
        {
            // file target to template list with information
            temp.sprintf("/LPROF/Files/TARGET%i", i);
            hold.targetFile=lprofSettings->readEntry(temp, QString::fromLocal8Bit(""));
            temp.sprintf("/LPROF/Files/TEMPLATE%i", i);
            hold.templateFile=lprofSettings->readEntry(temp, QString::fromLocal8Bit(""));
            add_target(hold);
        }
        
        if ((uint)ui->TargetSelector->count() > targetList->count())
        {
            // we know that at least one target has been added since the last time the
            // the config file was saved.  So we must up date the targetList->
           // This should never happen since it is an indication that the program crashed
           // the last time it was run or the user manually copied a reference file to the
           // hidden configuration directory
            qDebug("Target not in targetList");
        }

        init_targetlist();
        
        slotTargetRefChanged();
        
        ui->OutputFileEdit_Mon -> setText(lprofSettings->readEntry(
                                          QString::fromLocal8Bit("/LPROF/Files/MON_OUTPUT_PROFILE"), 
                                          QString::fromLocal8Bit("")));
        ui->OutputFileEdit -> setText(lprofSettings->readEntry(
                                      QString::fromLocal8Bit("/LPROF/Files/OUTPUT_PROFILE"), 
                                      QString::fromLocal8Bit("")));
        
        dsizes.reffileH = lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/refFile_h"),0);
        
        if (dsizes.reffileH == 0)
        {
            QDesktopWidget *d = QApplication::desktop();
            if (d -> height() <= 768)
            {
                dsizes.reffileH = 145;
                dsizes.reffileW = 395;
                dsizes.profparmsH = 335;
                dsizes.profparmsW = 475;
                dsizes.profidH = 307;
                dsizes.profidW = 372;
                dsizes.profparmsmonH = 250;
                dsizes.profparmsmonW = 300;
                dsizes.profchH = 440;
                dsizes.profchW = 622;
                dsizes.monvalH = 251;
                dsizes.monvalW = 520;
                dsizes.lprofH = 512;
                dsizes.lprofW = 792;
            }
            else if (d -> height() <= 1024 && d -> height() > 768)
            {
                dsizes.reffileH = 155;
                dsizes.reffileW = 456;
                dsizes.profparmsH = 385;
                dsizes.profparmsW = 585;
                dsizes.profparmsmonH = 350;
                dsizes.profparmsmonW = 400;
                dsizes.profidH = 424;
                dsizes.profidW = 462;
                dsizes.profchH = 500;
                dsizes.profchW = 750;
                dsizes.monvalH = 280;
                dsizes.monvalW = 607;
                dsizes.lprofH = 595;
                dsizes.lprofW = 924;
            }
        }
        else
        {
            dsizes.reffileW = lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/refFile_w"),0);
            dsizes.profparmsH = lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/profparms_h"),0);
            dsizes.profparmsW = lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/profparms_w"),0);
            dsizes.profparmsmonH = lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/profparmsmon_h"),0);
            dsizes.profparmsmonW = lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/profparmsmon_w"),0);
            dsizes.profidH = lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/profid_h"),0);
            dsizes.profidW = lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/profid_w"),0);
            dsizes.profchH = lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/profch_h"),0);
            dsizes.profchW = lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/profch_w"),0);
            dsizes.monvalH = lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/monval_h"),0);
            dsizes.monvalW = lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/monval_w"),0);
            dsizes.lprofH = lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/lprof_h"),0);
            dsizes.lprofW = lprofSettings -> readNumEntry(QString::fromLocal8Bit("/LPROF/numbers/lprof_w"),0);
        }
        // qDebug(QString::fromLocal8Bit("lprofMain::LoadConfig() done"));
        return TRUE;
    }
          //  means no config file found this is first time app has been run
          // config file will be created when program is closed
          // Set everything to default values

    ui->SafeFrame -> setValue(60);
    ui->SelectTemplate -> Dir = templates;
    ui->SelectTemplate->FillValues();
    ui->SelectTemplate->setCurrItem(0);
    ui->TargetSelector->Dir = ref_dir;
    ;
    ui->TargetSelector->setCurrItem(0);
    ui->MonitorProfileSelector ->Dir = mon_profile;
    ui->MonitorProfileSelector ->FillValues();
    ui->InputProfileSelector ->Dir = in_profile;
    ui->InputProfileSelector->FillValues();

    QDesktopWidget *d = QApplication::desktop();
    if (d -> height() <= 768)
    {
        dsizes.reffileH = 145;
        dsizes.reffileW = 395;
        dsizes.profparmsH = 335;
        dsizes.profparmsW = 475;
        dsizes.profidH = 307;
        dsizes.profidW = 372;
        dsizes.profchH = 440;
        dsizes.profchW = 622;
        dsizes.monvalH = 251;
        dsizes.monvalW = 520;
        dsizes.lprofH = 512;
        dsizes.lprofW = 792;
    }
    else if (d -> height() <= 1024 && d -> height() > 768)
    {
        dsizes.reffileH = 155;
        dsizes.reffileW = 456;
        dsizes.profparmsH = 385;
        dsizes.profparmsW = 585;
        dsizes.profidH = 424;
        dsizes.profidW = 462;
        dsizes.profchH = 500;
        dsizes.profchW = 750;
        dsizes.monvalH = 280;
        dsizes.monvalW = 607;
        dsizes.lprofH = 595;
        dsizes.lprofW = 924;
    }
    // qDebug(QString::fromLocal8Bit("lprofMain::LoadConfig() done"));
    return FALSE;

}

void lprofMain::SaveConfig()
{
    QString temp;
    // qDebug(QString::fromLocal8Bit("lprofMain::SaveConfig"));

    lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/Files/TARGET_DIR"), ui->TargetSelector->Dir);
    lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/numbers/NUMBER_TARGETS"), (int) targetList->count());
    if (targetList->count() > 0)
        for (uint i=0; i<=targetList->count()-1; i++)
    {
            // add target to template information
        temp.sprintf("/LPROF/Files/TARGET%i", i);
        lprofSettings->writeEntry(temp, targetList->at(i)->targetFile);
        temp.sprintf("/LPROF/Files/TEMPLATE%i", i);
        lprofSettings->writeEntry(temp, targetList->at(i)->templateFile);
    }

    lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/Files/OUTPUT_PROFILE"), ui->OutputFileEdit -> text());
    lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/Files/MON_OUTPUT_PROFILE"),  ui->OutputFileEdit_Mon ->text());
    lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/Files/TEMPLATE_DIR"), ui->SelectTemplate->Dir);
    // lprofSettings->writeEntry("/LPROF/numbers/TEMPLATE_NUM", (int) ui->SelectTemplate->getCurrItem());
    lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/Files/MONITOR_PROFILES_DIR"), ui->MonitorProfileSelector->Dir);
    lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/Files/MONITOR_PROFILE"), ui->MonitorProfileSelector->getFilename());
    lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/Files/INPUT_PROFILES_DIR"), ui->InputProfileSelector->Dir);
    lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/Files/INPUT_PROFILE"), ui->InputProfileSelector->getFilename());
    lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/numbers/SAFE_FRAME"), (int) ui->SafeFrame->value());
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/lprof_h"), this -> height());
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/lprof_w"), this -> width());
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/refFile_h"),dsizes.reffileH);
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/refFile_w"),dsizes.reffileW);
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/monval_h"),dsizes.monvalH);
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/monval_w"),dsizes.monvalW);
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/profparms_h"),dsizes.profparmsH);
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/profparms_w"),dsizes.profparmsW);
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/profparmsmon_h"),dsizes.profparmsmonH);
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/profparmsmon_w"),dsizes.profparmsmonW);
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/profid_h"),dsizes.profidH);
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/profid_w"),dsizes.profidW);
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/profch_h"),dsizes.profchH);
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/profch_w"),dsizes.profchW);
    lprofSettings -> writeEntry(QString::fromLocal8Bit("/LPROF/numbers/intent_curr_item"),ui->ComboBoxIntent -> currentItem());

//     lprofSettings->~QSettings();
    delete lprofSettings;
//    lprofSettings = 0;
//     targetList -> ~QPtrList();
//    delete targetList;
//    targetList = 0;
}

void lprofMain::init_targetlist()
{
    // qDebug(QString::fromLocal8Bit("lprofMain::init_targetlist()"));
    ui->SelectTemplate->FillValues();
    QString TargetFile = ui->TargetSelector -> getFilename();
    QString TemplateFile = ui->SelectTemplate -> getFilename();
    int i = ui->TargetSelector->getCurrItem();
    int n = ui->SelectTemplate->getCurrItem();
    // reinitialize ui->TargetSelector so that it has the new target ref file/template file pair if one was added
    // This could cause the possition of the currently selected target to change in the drop down
    // So we must get the filename from the curent item to make sure that this item is selected
    // when this is finished and before ui->TargetSelector is reintitalized with FillValues
    ui->TargetSelector->FillValues();  // why dons't this work?
    ui->SelectTemplate->FillValues();

    // find new possition of currently selected target
    int j;
    for (j=0; j<ui->TargetSelector->count(); j++)
    {
        ui->TargetSelector->setCurrItem(j);
        if (ui->TargetSelector->getFilename()==TargetFile)
        {
            i=j;
            j=ui->TargetSelector->count();
        }
    }

    for (j=0; j<ui->SelectTemplate->count(); j++)
    {
        ui->SelectTemplate->setCurrItem(j);
        if (ui->SelectTemplate->getFilename()==TargetFile)
        {
            n=j;
            j=ui->SelectTemplate->count();
        }
    }
    int k, m;
    bool done = false;
    // Now add new target and it's template to the target list
    if (targetList -> count() > 0)
    {
        for (j=0; j<ui->TargetSelector->count(); j++)
        {
            ui->TargetSelector->setCurrItem(j);
            for (k=0; k<ui->SelectTemplate->count(); k++)
            {
                ui->SelectTemplate->setCurrItem(k);
    
                for (m=0; m<targetList->count(); m++)
                {                    
                    if (targetList->at(m)->targetFile==ui->TargetSelector->getFilename() &&
                        targetList->at(m)->templateFile==ui->SelectTemplate->getFilename())
                    {
                        targetList->at(m)->targPos=j;
                        targetList->at(m)->tempPos=k;
                        k = ui->SelectTemplate->count();  // found the correct combo interate j
                        break;
                    }
                }
            }
        }
    }
    ui->TargetSelector->setCurrItem(i);
    ui->SelectTemplate->setCurrItem(n);
}

void lprofMain::saveProfileDetails()
{
    QString slash;
    bool cs_profile = true;
    QDir d;
    QString copy, profile; 

    // qDebug(QString::fromLocal8Bit("lprofMain::saveProfileDetails"));

    if (ui->TabProfiler -> currentPageIndex()== 0)
    {
        profile = ui->OutputFileEdit -> text().mid(ui->OutputFileEdit -> text().findRev(QString::fromLocal8Bit("/")) + 1,
                                             ui->OutputFileEdit -> text().length() - ui->OutputFileEdit -> text().findRev(QString::fromLocal8Bit("/")) - 1);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/Files/OUTPUT_PROFILE"),  ui->OutputFileEdit -> text());

        urlOp.copy(measfile, d.homeDirPath() +
                lprofBaseDir + QString::fromLocal8Bit("/measurements/") +  profile + QString::fromLocal8Bit(".cgt"), false, false);

        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/Files/MEASUREMENT_SHEET") ,   profile + QString::fromLocal8Bit(".cgt"));
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/numbers/ur_x"),  Marks.ur_x);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/numbers/ur_y"),  Marks.ur_y);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/numbers/lr_x"),  Marks.lr_x);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/numbers/lr_y"),  Marks.lr_y);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/numbers/ul_x"),  Marks.ul_x);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/numbers/ul_y"),  Marks.ul_y);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/numbers/ll_x"),  Marks.ll_x);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/numbers/ll_y"),  Marks.ll_y);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/Files/IT8Image"),  image_file_name);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/numbers/RsplSmoothness"),  sys.RsplSmoothness);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/boolians/lOptRGCV"),  sys.lOptRGCV);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-")  + profile + QString::fromLocal8Bit("/numbers/CLUT_POINTS") ,  (int) sys.hdr.CLUTPoints);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-")  + profile + QString::fromLocal8Bit("/numbers/PROFILE_VERBOSITY_LEVEL") ,  (int) sys.hdr.ProfileVerbosityLevel);

        lprofSettings->writeEntry( QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/numbers/TEMPLATE_NUM"),  ui->SelectTemplate->getCurrItem() );
        lprofSettings->writeEntry( QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/numbers/TARGET_NUM"),  ui->TargetSelector->getCurrItem());
        lprofSettings->writeEntry( QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/numbers/TEMPLATE_Filename"),  ui->SelectTemplate->getFilename() );
        lprofSettings->writeEntry( QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/numbers/TARGET_Filename"),  ui->TargetSelector->getFilename());

        // Profile viewing conditions lprofSettings
        // from profile parms dialog
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-")  + profile + QString::fromLocal8Bit("/numbers/surround"),  sys.hdr.device.surround);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-")  + profile + QString::fromLocal8Bit("/numbers/La"),  sys.hdr.device.La);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-")  + profile + QString::fromLocal8Bit("/numbers/Yb"),  sys.hdr.device.Yb);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-")  + profile + QString::fromLocal8Bit("/numbers/PCSsurround"),  sys.hdr.PCS.surround);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-")  + profile + QString::fromLocal8Bit("/numbers/PCSLa"),  sys.hdr.PCS.La);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-")  + profile + QString::fromLocal8Bit("/numbers/PCSYb"),  sys.hdr.PCS.Yb);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-")  + profile + QString::fromLocal8Bit("/numbers/deviceVC"),   sys.hdr.viewingConditionsPredefDevice);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-")  + profile + QString::fromLocal8Bit("/numbers/PCSVC"),  sys.hdr.viewingConditionsPredefPCS);

        // profile ID
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/text/manufacturer"),  sys.hdr.Manufacturer);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/text/model"),  sys.hdr.Model);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/text/copyright"),  sys.hdr.Copyright);
        lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") + profile + QString::fromLocal8Bit("/text/description"),  sys.hdr.Description);

    }
    else if (ui->TabProfiler -> currentPageIndex()== 1)
    {
        profile = ui->OutputFileEdit_Mon -> text().mid(ui->OutputFileEdit_Mon -> text().findRev(QString::fromLocal8Bit("/")) + 1,
                ui->OutputFileEdit_Mon -> text().length() - ui->OutputFileEdit_Mon -> text().findRev(QString::fromLocal8Bit("/")) - 1);
        cs_profile = false;
        lprofSettings->writeEntry((QString::fromLocal8Bit("/LPROF/profile-") +
                profile +
                QString::fromLocal8Bit("/Files/MON_OUTPUT_PROFILE")), 
                ui->OutputFileEdit_Mon -> text());
        if (!ui->RadioCoarse->isChecked())
        { 
            QString meas_file =  d.homeDirPath() + lprofBaseDir + QString::fromLocal8Bit("/measurements/") +  profile + QString::fromLocal8Bit(".cgt");

            urlOp.copy(ui->MonMeasurementSheet->getFilename(), meas_file, false, false);

            QString cal_file =  d.homeDirPath() + lprofBaseDir + QString::fromLocal8Bit("/measurements/") + QString::fromLocal8Bit("cal-") + profile + QString::fromLocal8Bit(".cgt");

            urlOp.copy(ui->MonCalibrationSheet->getFilename(), cal_file, false, false);

            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/Files/MEASUREMENT_SHEET"),   meas_file);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/Files/MON_CAL_SHEET"),   cal_file);

            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/SAFE_FRAME"),  (int) ui->SafeFrame->value());
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/boolians/Course"),  false);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/CLUT_POINTS"),  (int) sys.hdr.CLUTPoints);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/PROFILE_VERBOSITY_LEVEL"),  (int) sys.hdr.ProfileVerbosityLevel);

          // lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/boolians/lUseCIECAM02"),  sys.hdr.lUseCIECAM02);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/surround"),  sys.hdr.device.surround);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/La"),  sys.hdr.device.La);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/Yb"),  sys.hdr.device.Yb);

          // profile ID
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/text/manufacturer"),  sys.hdr.Manufacturer);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/text/model"),  sys.hdr.Model);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/text/copyright"),  sys.hdr.Copyright);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/text/description"),  sys.hdr.Description); 


        }
        else
        {
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/whitePpos"),  monitorValues.items.wppos);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/primePos"),  monitorValues.items.ppos);

            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/rgamma"),  monitorValues.items.rgamma);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/ggamma"),  monitorValues.items.ggamma);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/bgamma"),  monitorValues.items.bgamma);

            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/red_x"),  sys.hdr.Primaries.Red.x);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/red_y"),  sys.hdr.Primaries.Red.y);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/green_x"),  sys.hdr.Primaries.Green.x);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/green_y"),  sys.hdr.Primaries.Green.y);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/blue_x"),  sys.hdr.Primaries.Blue.x);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/blue_y"),  sys.hdr.Primaries.Blue.y);

            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/whitePoint"),  monitorValues.items.temp);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/boolians/Course"),  true);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +   profile + QString::fromLocal8Bit("/boolians/linked"),  monitorValues.items.linked);

            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/CLUT_POINTS"),  (int) sys.hdr.CLUTPoints);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/PROFILE_VERBOSITY_LEVEL"),  (int) sys.hdr.ProfileVerbosityLevel);

          // lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/boolians/lUseCIECAM02"),  sys.hdr.lUseCIECAM02);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/surround"),  sys.hdr.device.surround);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/La"),  sys.hdr.device.La);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/Yb"),  sys.hdr.device.Yb);

          // profile ID
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/text/manufacturer"),  sys.hdr.Manufacturer);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/text/model"),  sys.hdr.Model);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/text/copyright"),  sys.hdr.Copyright);
            lprofSettings->writeEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/text/description"),  sys.hdr.Description);
        }
    }
    else qDebug("Something wrong");  // will be needed later for printer profiler tab
}

void lprofMain::getConfigParmsandID(QString profile)
{
    // qDebug(QString::fromLocal8Bit("lprofMain::getConfigParmsandID"));
    sys.hdr.CLUTPoints = lprofSettings->readNumEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/CLUT_POINTS"),  10);
    sys.hdr.ProfileVerbosityLevel = lprofSettings->readNumEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/PROFILE_VERBOSITY_LEVEL"),  0);
    sys.RsplSmoothness = lprofSettings->readNumEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/RsplSmoothness"),  0);
    sys.lOptRGCV = lprofSettings->readBoolEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/boolians/lOptRGCV"),  true);
         // sys.hdr.lUseCIECAM02 = lprofSettings->readBoolEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/boolians/lUseCIECAM02"),  false);
    sys.hdr.device.surround = lprofSettings->readNumEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/surround"),  1);
    sys.hdr.device.La = lprofSettings->readDoubleEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/La"),  20);
    sys.hdr.device.Yb = lprofSettings->readDoubleEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/Yb"),  20);
    sys.hdr.PCS.surround = lprofSettings->readNumEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/PCSsurround"),  1);
    sys.hdr.PCS.La = lprofSettings->readDoubleEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/PCSLa"),  20);
    sys.hdr.PCS.Yb = lprofSettings->readDoubleEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/PCSYb"),  20);
    sys.hdr.viewingConditionsPredefDevice = lprofSettings->readNumEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/deviceVC"),  userdef);
    sys.hdr.viewingConditionsPredefPCS = lprofSettings->readNumEntry(QString::fromLocal8Bit("/LPROF/profile-") +  profile + QString::fromLocal8Bit("/numbers/PCSVC"),  userdef);

         // profile ID information
    strcpy(sys.hdr.Manufacturer, 
           lprofSettings->readEntry(QString::fromLocal8Bit("LPROF/profile-") 
                                    +  profile 
                                    + QString::fromLocal8Bit("/text/manufacturer"),  
                                    QString::fromLocal8Bit(sys.hdr.Manufacturer)).toLocal8Bit());
    strcpy(sys.hdr.Model, 
           lprofSettings->readEntry(QString::fromLocal8Bit("LPROF/profile-") 
                                    +  profile 
                                    + QString::fromLocal8Bit("/text/model"),  
                                    QString::fromLocal8Bit(sys.hdr.Model)).toLocal8Bit());
    strcpy(sys.hdr.Copyright, 
           lprofSettings->readEntry(QString::fromLocal8Bit("LPROF/profile-") 
                                    +  profile 
                                    + QString::fromLocal8Bit("/text/copyright"),  
                                    QString::fromLocal8Bit(sys.hdr.Copyright)).toLocal8Bit());
    strcpy(sys.hdr.Description, 
           lprofSettings->readEntry(QString::fromLocal8Bit("LPROF/profile-") 
                                    +  profile 
                                    + QString::fromLocal8Bit("/text/description"),  
                                    QString::fromLocal8Bit(sys.hdr.Description)).toLocal8Bit());

}
