/***************************************************************************
                          kkeyleddtlcfg.cpp  -  description
                             -------------------
    begin                : Tue Aug 14 2001
    copyright            : (C) 2001 by Dieter Landolt
    email                : dieter.landolt@secs.ch
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 <kconfig.h>
#include <klocale.h>
#include <kfiledialog.h>
#include <qcheckbox.h>
#include <kcombobox.h>
#include <kstandarddirs.h>
#include <qfile.h>
#include <kapp.h>
#include <qmovie.h>
#include "kkeyleddtlcfg.h"
#include "ksoundcfg.h"
#include "dockwidget.h"


kkeyleddtlcfg::kkeyleddtlcfg(QWidget *parent, const char *name, bool modal, WFlags fl) : ledcfgdia(parent,name,modal,fl) 
{
    // Bugworkaround for QT designer and signal usage
    // ThemSupport->setEnabled(false);
    mainFrame->setEnabled(false);
    soundFrame->setEnabled(false);
    iconFrame->setEnabled(false);

     readConfig("Ledpictures");

     connect(ThemeSupport,SIGNAL(clicked()), this,SLOT(enableApply()));
     connect(PlaySounds,SIGNAL(clicked()), this,SLOT(enableApply()));
     connect(PaintPix,SIGNAL(clicked()), this,SLOT(enableApply()));

     connect(PlaySounds,SIGNAL(clicked()), this,SLOT(enableSave()));
     connect(PaintPix,SIGNAL(clicked()), this,SLOT(enableSave()));


     connect(numlockoff,SIGNAL(clicked()),  this,SLOT(numlockoff_click()));
     connect(numlockon,SIGNAL(clicked()),   this,SLOT(numlockon_click()));
     connect(capslockoff,SIGNAL(clicked()), this,SLOT(capslockoff_click()));
     connect(capslockon,SIGNAL(clicked()),  this,SLOT(capslockon_click()));
     connect(scrollockoff,SIGNAL(clicked()),this,SLOT(scrollockoff_click()));
     connect(scrollockon,SIGNAL(clicked()), this,SLOT(scrollockon_click()));
     
     connect(ApplyButton,SIGNAL(clicked()), this,SLOT(applyConfig()));
     connect(OkButton,SIGNAL(clicked()), this,SLOT(applyConfig()));
     
     // Clearing path's
     connect(clearnumlock,SIGNAL(clicked()), this, SLOT(clearnum()));
     connect(clearcapslock,SIGNAL(clicked()), this, SLOT(clearcaps()));
     connect(clearscrollock,SIGNAL(clicked()), this, SLOT(clearscroll()));
     
     // Setting Paths for dnd's 
     connect(numlockoff,SIGNAL(newPath(QString)),this, SLOT(setNumOffPath(QString)));
     connect(numlockon,SIGNAL(newPath(QString)),this, SLOT(setNumOnPath(QString)));
     connect(capslockoff,SIGNAL(newPath(QString)),this, SLOT(setCapsOffPath(QString)));
     connect(capslockon,SIGNAL(newPath(QString)),this, SLOT(setCapsOnPath(QString)));
     connect(scrollockoff,SIGNAL(newPath(QString)),this, SLOT(setScrollOffPath(QString)));
     connect(scrollockon,SIGNAL(newPath(QString)),this, SLOT(setScrollOnPath(QString)));

     // Setting Paths for sound Paths
     connect(numOffSound,SIGNAL(newPath(QString)), this, SLOT(setSoundPath(QString)));
     connect(numOnSound,SIGNAL(newPath(QString)), this, SLOT(setSoundPath(QString)));
     connect(scrollOffSound,SIGNAL(newPath(QString)), this, SLOT(setSoundPath(QString)));
     connect(scrollOnSound,SIGNAL(newPath(QString)), this, SLOT(setSoundPath(QString)));
     connect(capsOffSound,SIGNAL(newPath(QString)), this, SLOT(setSoundPath(QString)));
     connect(capsOnSound,SIGNAL(newPath(QString)), this, SLOT(setSoundPath(QString)));

     // Signals to handel for themes
     connect(ThemeBox,SIGNAL(textChanged(const QString&)),this,SLOT(setCurrentTheme()));
     connect(ThemeSave,SIGNAL(clicked()),this, SLOT(saveThisTheme()));
     connect(ThemeBox,SIGNAL(activated(int)),this,SLOT(activateTheme(int)));
     connect(ThemeDel,SIGNAL(clicked()),this, SLOT(delThisTheme()));
     connect(ThemeRename,SIGNAL(clicked()),this,SLOT(renThisTheme()));

}
kkeyleddtlcfg::~kkeyleddtlcfg(){
}

/** Apply Configuration */
void kkeyleddtlcfg::applyConfig(){
  
     ApplyButton->setEnabled(false);     
     KGlobal::config()->setGroup("Ledpictures");
     
     KGlobal::config()->writeEntry("UseTheme",ThemeSupport->isChecked(), true,false,false );

     if( ! ThemeSave->isEnabled() ) {
          KGlobal::config()->writeEntry("ActualTheme",ThemeBox->currentItem(),true,false,false);
     } else {          
          KGlobal::config()->writeEntry("ActualTheme",-1,true,false,false);
     }
     saveTheme("Ledpictures");     
     
     configChanged(); // Call signal to notify
}

/** Handler for all buttons */
void kkeyleddtlcfg::lockbutton_click(kDndButton *button){
     QString retPath;
     // QPixmap *fp = new QPixmap(20,20);
     retPath = KFileDialog::getOpenFileName(QString::null,
               i18n("*.png *.mng *.gif *.xpm|All supported image format\n*.png|Portable network graphics (PNG)\n*.xpm|Pixmap format (XPM)\n*.gif|Grafic image format (GIF)\n*.mng|Multiple-image network graphics format(MNG)"),
               this,i18n("Select the right graphic file"));
     if (retPath != button->path && retPath != QString::null ) {
          // qDebug(retPath);
          button->setMovie(retPath);
          // button->setPixmap(QPixmap(retPath)); old Function
          button->path = retPath;
          ApplyButton->setEnabled(true);
          ThemeSave->setEnabled(true);          
     }
}
/** clear the path of the numlock buttons */
void kkeyleddtlcfg::clearnum(){
     if ( numlockoff->path != QString::null || numlockon->path != QString::null ) {

          numlockoff->setMovie("");
          numlockoff->setPixmap( DockWidget::paintIcon(DockWidget::Off) );
          numlockon->setMovie("");
          numlockon->setPixmap( DockWidget::paintIcon(DockWidget::On) );
     
          numlockon->path = numlockoff->path = QString::null;
          ApplyButton->setEnabled(true);          
          ThemeSave->setEnabled(true);          
     }
}
/** clear the capslock button */
void kkeyleddtlcfg::clearcaps(){
     if ( capslockoff->path != QString::null || capslockon->path != QString::null ) {

          capslockoff->setMovie("");
          capslockoff->setPixmap( DockWidget::paintIcon(DockWidget::Off) );
          capslockon->setMovie("");
          capslockon->setPixmap( DockWidget::paintIcon(DockWidget::On) );
     
          capslockon->path = capslockoff->path = QString::null;
                    
          ApplyButton->setEnabled(true);          
          ThemeSave->setEnabled(true);          
     }
}
/** clear the scrollockbuttons */
void kkeyleddtlcfg::clearscroll(){
     if ( scrollockoff->path != QString::null || scrollockon->path != QString::null ) {

          scrollockoff->setMovie("");
          scrollockoff->setPixmap( DockWidget::paintIcon(DockWidget::Off) );
          scrollockon->setMovie("");
          scrollockon->setPixmap( DockWidget::paintIcon(DockWidget::On) );
     
          scrollockon->path = scrollockoff->path = QString::null;
          
          ApplyButton->setEnabled(true);          
          ThemeSave->setEnabled(true);          
     }
}
/** read a spezific configuration. for active or theme */
void kkeyleddtlcfg::readConfig(QString confName){
     // KSimpleConfig conf ("Kkeyledrc");
     
     KGlobal::config()->setGroup(confName);
     
     if ( confName == "Ledpictures" ) { // Ledpicures hold's the activ configuration
          ThemeSupport->setChecked(KGlobal::config()->readBoolEntry("UseThemes",true ));
     }

      /***/

     loadTheme(confName);
     
     ThemeBox->insertStringList(KGlobal::config()->readListEntry("Themes"));
     // If locale is not possible read independend entry
     if(ThemeBox->count() == 0) {                              
          ThemeDel->setEnabled(false);
     }    
     
     ThemeBox->setCurrentItem(KGlobal::config()->readNumEntry("ActualTheme",-1));
}
/** Loads a them to activate */
void kkeyleddtlcfg::loadTheme(QString theme,bool display){

     KGlobal::config()->setGroup(theme);

     PaintPix->setChecked( KGlobal::config()->readBoolEntry("UsePictures",false ));
     PlaySounds->setChecked( KGlobal::config()->readBoolEntry("UseSounds",false ));


     numlockoff->path    = locate("data",KGlobal::config()->readPathEntry("numoff_path"));
     numlockon->path     = locate("data",KGlobal::config()->readPathEntry("numon_path"));
     capslockoff->path   = locate("data",KGlobal::config()->readPathEntry("capsoff_path"));
     capslockon->path    = locate("data",KGlobal::config()->readPathEntry("capson_path"));
     scrollockoff->path  = locate("data",KGlobal::config()->readPathEntry("scrolloff_path"));
     scrollockon->path   = locate("data",KGlobal::config()->readPathEntry("scrollon_path"));

     numOffSound->setText(locate("data",KGlobal::config()->readPathEntry("numoffsound_path")));
     numOnSound->setText(locate("data",KGlobal::config()->readPathEntry("numonsound_path")));
     capsOffSound->setText(locate("data",KGlobal::config()->readPathEntry("capsoffsound_path")));
     capsOnSound->setText(locate("data",KGlobal::config()->readPathEntry("capsonsound_path")));
     scrollOffSound->setText(locate("data",KGlobal::config()->readPathEntry("scrolloffsound_path")));
     scrollOnSound->setText(locate("data",KGlobal::config()->readPathEntry("scrollonsound_path")));

     if ( display ) {
     
          if (numlockoff->path != QString::null && QFile::exists(numlockoff->path) ) {
               // numlockoff->setPixmap(QPixmap(numlockoff->path));
               numlockoff->setMovie(numlockoff->path);
          } else {
               numlockoff->setMovie("");
               numlockoff->setPixmap( DockWidget::paintIcon(DockWidget::Off) );
               numlockoff->path = QString::null;
          }    
          if (numlockon->path != QString::null && QFile::exists(numlockon->path) ) {
               // numlockon->setPixmap(QPixmap(numlockon->path));
               numlockon->setMovie(numlockon->path);
          } else {
               numlockon->setMovie("");
               numlockon->setPixmap( DockWidget::paintIcon(DockWidget::On) );
               numlockon->path = QString::null;
          }
          if (capslockoff->path != QString::null && QFile::exists(capslockoff->path) ) {
               // capslockoff->setPixmap(QPixmap(capslockoff->path));
               capslockoff->setMovie(capslockoff->path);
          } else {
               capslockoff->setMovie("");
               capslockoff->setPixmap( DockWidget::paintIcon(DockWidget::Off) );
               capslockoff->path = QString::null;
          }
          if (capslockon->path != QString::null && QFile::exists(capslockon->path) ) {
               // capslockon->setPixmap(QPixmap(capslockon->path));
               capslockon->setMovie(capslockon->path);
          } else {
               capslockon->setMovie("");
               capslockon->setPixmap( DockWidget::paintIcon(DockWidget::On) );
               capslockon->path = QString::null;
          }
          if (scrollockoff->path != QString::null && QFile::exists(scrollockoff->path) ) {
               // scrollockoff->setPixmap(QPixmap(scrollockoff->path));
               scrollockoff->setMovie(scrollockoff->path);
          } else {
               scrollockoff->setMovie("");
               scrollockoff->setPixmap( DockWidget::paintIcon(DockWidget::Off) );
               scrollockoff->path = QString::null;
          }
          if (scrollockon->path != QString::null && QFile::exists(scrollockon->path) ) {
               // scrollockon->setPixmap(QPixmap(scrollockon->path));
               scrollockon->setMovie(scrollockon->path);
          } else {
               scrollockon->setMovie("");
               scrollockon->setPixmap( DockWidget::paintIcon(DockWidget::On) );
               scrollockon->path = QString::null;
          }
          ThemeSave->setEnabled(false);
          ThemeRename->setEnabled(false);
     }
}
/** Function witch saves a spezific theme */
void kkeyleddtlcfg::saveTheme(QString theme){
     // KSimpleConfig conf ("Kkeyledrc");
     KGlobal::config()->setGroup(theme);
     
     KGlobal::config()->writeEntry("UsePictures", PaintPix->isChecked(), true,false,false);
     KGlobal::config()->writePathEntry("scrolloff_path",KGlobal::dirs()->relativeLocation("data",scrollockoff->path), true,false,false);
     KGlobal::config()->writePathEntry("scrollon_path", KGlobal::dirs()->relativeLocation("data",scrollockon->path), true,false,false);
     KGlobal::config()->writePathEntry("numoff_path",   KGlobal::dirs()->relativeLocation("data",numlockoff->path), true,false,false);
     KGlobal::config()->writePathEntry("numon_path",    KGlobal::dirs()->relativeLocation("data",numlockon->path), true,false,false);
     KGlobal::config()->writePathEntry("capsoff_path",  KGlobal::dirs()->relativeLocation("data",capslockoff->path), true,false,false);
     KGlobal::config()->writePathEntry("capson_path",   KGlobal::dirs()->relativeLocation("data",capslockon->path), true,false,false);

     // Save the Sound Part of the thme +*+
     KGlobal::config()->writeEntry("UseSounds", PlaySounds->isChecked(), true,false,false);
     KGlobal::config()->writePathEntry("numoffsound_path",   KGlobal::dirs()->relativeLocation("data",numOffSound->text()),    true,false,false);
     KGlobal::config()->writePathEntry("numonsound_path",    KGlobal::dirs()->relativeLocation("data",numOnSound->text()),     true,false,false);
     KGlobal::config()->writePathEntry("capsoffsound_path",  KGlobal::dirs()->relativeLocation("data",capsOffSound->text()),   true,false,false);
     KGlobal::config()->writePathEntry("capsonsound_path",   KGlobal::dirs()->relativeLocation("data",capsOnSound->text()),    true,false,false);
     KGlobal::config()->writePathEntry("scrolloffsound_path",KGlobal::dirs()->relativeLocation("data",scrollOffSound->text()), true,false,false);
     KGlobal::config()->writePathEntry("scrollonsound_path", KGlobal::dirs()->relativeLocation("data",scrollOnSound->text()),  true,false,false);

}
/** set the them from the combobox */
void kkeyleddtlcfg::setCurrentTheme(){
     ThemeSave->setEnabled(true);
     ThemeRename->setEnabled(true);
     ThemeDel->setEnabled(true);
}
/** save the them under the Name and into the list of the box */
void kkeyleddtlcfg::saveThisTheme(){
     QString themeName;
     QStringList allEntrys;
     bool inList = false;
     
     for(int i=0;i < ThemeBox->count();i++) {
          allEntrys.append(ThemeBox->text(i));
          if ( ThemeBox->text(i) == ThemeBox->currentText() ) {
               inList = true;
          }
     }          

     int tIndex = 0;
     themeName = ThemeBox->currentText();
     tIndex = ThemeBox->currentItem();
     if( ! inList ) {
          ThemeBox->insertItem(themeName);
          tIndex = ThemeBox->count() -1;
          allEntrys.append(themeName);
     } 
     saveTheme("Theme" + QString::number( tIndex ));
     ThemeSave->setEnabled(false);
     ThemeDel->setEnabled(true);
     
     // KSimpleConfig conf ("Kkeyledrc");
     KGlobal::config()->setGroup("Ledpictures");
     
     // conf->writeEntry("Themes",allEntrys,',',true,false,true); // Language specific
     KGlobal::config()->writeEntry("Themes",allEntrys,',',true,false,true); // Language unspecific if a switch is done
}
/** For renaming a displayed Theme */
void kkeyleddtlcfg::renThisTheme(){
     QStringList allEntrys;
     ThemeBox->changeItem(ThemeBox->currentText(),ThemeBox->currentItem());
     
     for(int i=0;i < ThemeBox->count();i++) {
          allEntrys.append(ThemeBox->text(i));
     }          
     ThemeRename->setEnabled(false);
     
     KGlobal::config()->setGroup("Ledpictures");
     KGlobal::config()->writeEntry("Themes",allEntrys,',',true,false,true); // Language unspecific if a switch is done
}
/** Called to activate the them selected in the Combobox */
void kkeyleddtlcfg::activateTheme(int themeNr){
     loadTheme("Theme" + QString::number( themeNr ));
     ApplyButton->setEnabled(true);
}
/** Notfy's that the displayed Theme should be deleted */
void kkeyleddtlcfg::delThisTheme(){
     delTheme(ThemeBox->currentText());
     activateTheme(ThemeBox->currentItem());
     saveThisTheme();
}
/** delete a theme width this name (theme) i must be esisting in the combobox */
void kkeyleddtlcfg::delTheme(QString theme){
     // QString themeName;
     QStringList allEntrys;
     bool inList = false;
     int posList =-1;
     
     for(int i=0;i < ThemeBox->count();i++) {
          // allEntrys.append(ThemeBox->text(i));
          if ( ThemeBox->text(i) == theme ) {
               inList = true;
               posList = i;
          }
     }
     for ( int i=posList; i<ThemeBox->count();i++) {
          
          // qDebug("move theme from " + QString::number(i + 1) + " to " + QString::number(i) + "\n");
          loadTheme("Theme" + QString::number(i + 1) );
          saveTheme("Theme" + QString::number(i) );
     }
     
     ThemeBox->removeItem(posList);
}


syntax highlighted by Code2HTML, v. 0.9.1