/***************************************************************************
                          GlobalConfigWidget.cpp  -  description
                             -------------------
    begin                : jeu fév 2 2006
    copyright            : (C) 2006 by Gibaud Alain
    email                : alain.gibaud@free.fr
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "GlobalConfigWidget.h"
#include "PikMain.h"
#include "Config.h"
#include "XQPushButton.h"

#include <qcombobox.h>
#include <qgroupbox.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qtooltip.h>

#include <kiconloader.h>
#include <klocale.h>
#include <kapplication.h>
#include <kfiledialog.h>
#include <kmessagebox.h>

#include <iostream>
using namespace std ;


GlobalConfigWidget::GlobalConfigWidget(PikMain *pikmain) :
        ConfigWidget(0) , main_widget(pikmain)
{
    QVBox  *central = new QVBox(this) ;
    central->setSpacing(8) ;
    central->setFrameStyle( QFrame::Panel | QFrame::Sunken );
    central->setLineWidth( 2 );
    central->setMargin(8) ;

    QGroupBox *programmersbox = new QGroupBox(1, Qt::Horizontal, i18n("Programmers") ,  central ) ;
    programmersbox->setInsideMargin(14) ;
    programmers = new QComboBox(programmersbox) ;

    // get current programmer shortname
    QString shortname ;
    Config::getCurrentProgrammer(shortname) ;

    // get programmers names
    QValueVector<ProgrammerBase *>& progs = main_widget->programmersList() ;
    int cp = 0 ;
    QString path ;
    for(int i = 0 ; i < (int)progs.size() ; ++i)
    {
        programmers->insertItem(progs[i]->longName()) ;
        if(progs[i]->shortName() == shortname)
            cp = i ;
    }
    // display current programmer
    programmers->setCurrentItem(cp) ;

    programmersbox->setFixedHeight(programmersbox->sizeHint().height());

    QGroupBox *toolsbox = new QGroupBox(3, Qt::Horizontal, i18n("External tools") ,  central ) ;
    toolsbox->setInsideMargin(14) ;

    QStringList toolnames ;
    Config::getToolNames(toolnames) ;
    QStringList::iterator t ;
    int nb ;
    KIconLoader loader;
    QIconSet iconset = loader.loadIcon("fileopen", KIcon::Toolbar) ;

    for(t = toolnames.begin(), nb = 0 ; t != toolnames.end() ; ++t, ++nb)
    {
        QString path, info , URL ;
        bool mand ;
        Config::getToolEntry(*t, path, info, URL, mand) ;

        QLabel *lab = new QLabel(*t, toolsbox) ;
        tool_names.push_back(lab) ;
        QLineEdit *p = new QLineEdit( path , toolsbox) ;
        tool_paths.push_back(p) ;

        QString tttext = info ;
        tttext += i18n("\nDownload:  ") ;
        tttext += URL ;
        QToolTip::add(p, tttext) ;

        XQPushButton *b = new XQPushButton(QString::null, toolsbox, nb) ;
        connect(b, SIGNAL(clicked(int)), this , SLOT(chooseExecutable( int ))) ;
        b->setIconSet(iconset) ;
    }

    new QVBox(central) ;
}
GlobalConfigWidget::~GlobalConfigWidget()
{}
/** short name of configurator */
QString GlobalConfigWidget::configName()
{
    return i18n("Global") ;
}
/** title string for this configurator */
QString GlobalConfigWidget::configTitle()
{
    return i18n("Global settings");
}
/** return icon of this configurator  */
QPixmap GlobalConfigWidget::icon()
{
    KIconLoader loader;
    return loader.loadIcon("pikdev_config_general", KIcon::Toolbar, KIcon::SizeMedium) ;
}


/** apply configured data (ie: save them to config file) */
void GlobalConfigWidget::doApply()
{
    QString longprogname = programmers->currentText() ;

    QValueVector<ProgrammerBase *> &progs =  main_widget->programmersList() ;

    // update paths for each tool
    for(int  i = tool_names.size() -1  ; i >= 0 ; --i)
    {
        QString path, URL, info, toolname = tool_names[i]->text()   ;
        bool mand ;
        Config::getToolEntry(toolname, path, info, URL, mand) ;
        if( mand &&  tool_paths[i]->text() == "")
        {
            KMessageBox::information(this,
                                     i18n("Pikdev needs %1 to work properly.\nPlease install this tool (see %2)").arg(toolname).arg(URL),
                                     i18n("Installation needed")
                                    ) ;
        }
        else
	{
		QString exename = tool_paths[i]->text() ;
		
		if( QFile::exists(exename) )
            		Config::setToolPath(toolname, tool_paths[i]->text()) ;
		else if( exename != "")
		{
            			KMessageBox::information(this,
                                     i18n("External tool %1 doesn't exist.\nPlease fix this path.").arg(exename),
                                     i18n("Incorrect path") ) ;
		}
		
	}
    }

    // get programmer long name from combobox
    // and find corresponding short name
    QString programmername  ;
    for( int i =  progs.size()-1 ; i >= 0 ; --i )
    {
        if( longprogname == progs[i]->longName())
        {
            programmername  = progs[i]->shortName();
            break ;
        }
    }

    // is programmer installed & configured ?
    QString path ;
    Config::getToolPath(programmername, path ) ;
    if( programmername != "Builtin" && path == "")
    {
        KMessageBox::information(this,
                                 i18n("Programming tool not found.\nPlease install %1 application").arg(programmername),
                                 i18n("Installation needed")) ;
        Config::setCurrentProgrammer( "Builtin" ) ;
    }
    else 
    {
        Config::setCurrentProgrammer(  programmername  ) ;

    }
    main_widget->updateProgrammer() ;

}


/*!
    \fn GlobalConfigWidget::chooseExecutable(int i)
 */
void GlobalConfigWidget::chooseExecutable(int i)
{
    QString fname ;

    fname = KFileDialog::getOpenFileName ( "/usr" , i18n("* (All files)"),  this ) ;
    tool_paths[i]->setText(fname) ;
}
