#include <stdlib.h>

#include <qlayout.h>
#include <qlabel.h>
#include <qcombobox.h>
#include <qtabwidget.h>
#include <qframe.h>
#include <qgroupbox.h>
#include <qpushbutton.h>
#include <qlistview.h>
#include <qheader.h>
#include <qwhatsthis.h>

#include <kkeydialog.h>
#include <kglobal.h>
#include <kconfig.h>
#include <klocale.h>
#include <kstddirs.h>
#include <kprocess.h>
#include <kdebug.h>
#include <kapp.h>

#include "rules.h"
#include "kcmlayout.moc"
#include "pixmap.h"


LayoutConfig::LayoutConfig(QWidget *parent, const char *name)
  : KCModule(parent, name), rules(0)
{
  QVBoxLayout *main = new QVBoxLayout(this, 0,0);
  QTabWidget *tab = new QTabWidget(this);
  main->addWidget(tab);

  QWidget *layout = new QWidget(this);
  QVBoxLayout *vvbox = new QVBoxLayout(layout, 6,6);

  QGroupBox *grp = new QGroupBox(i18n("Configuration"), layout);
  vvbox->addWidget(grp);

  QGridLayout *grid = new QGridLayout(grp, 4,2, 6,6);
  grid->addRowSpacing(0, grp->fontMetrics().height());

  QLabel *l;
  /*  ruleCombo = new QComboBox(grp);
  l = new QLabel(ruleCombo, i18n("Keyboard &Rule Set"), grp);
  grid->addWidget(l, 1,0);
  grid->addWidget(ruleCombo, 1,1);

  connect(ruleCombo, SIGNAL(activated(const QString&)), this, SLOT(ruleChanged(const QString&)));
  connect(ruleCombo, SIGNAL(activated(const QString&)), this, SLOT(configChanged()));
  */

  modelCombo = new QComboBox(grp);
  l = new QLabel(modelCombo, i18n("Keyboard &Model"), grp);
  grid->addWidget(l, 2,0);
  grid->addWidget(modelCombo, 2,1);
  QString wtstr = i18n("Here you can choose a keyboard model. This setting is independent of your"
    " keyboard layout and refers to the \"hardware\" model, i.e. the way your keyboard is manufactured."
    " Modern keyboards that come with your computer usually have two extra keys and are referred to as"
    " \"104-key\" models, which is probably what you want if you don't know what kind of keyboard you have.");
  QWhatsThis::add( modelCombo, wtstr );
  QWhatsThis::add( l, wtstr );

  connect(modelCombo, SIGNAL(activated(int)), this, SLOT(configChanged()));

  layoutCombo = new QComboBox(grp);
  l = new QLabel(layoutCombo, i18n("Primary &Layout"), grp);
  grid->addWidget(l, 3,0);
  grid->addWidget(layoutCombo, 3,1);
  wtstr = i18n("Here you can choose your primary keyboard layout, i.e. the keyboard layout used as default."
    " The keyboard layout defines \"which keys do what\". For example German keyboards have"
    " the letter 'Y' where US keyboards have the letter 'Z'.  ");
  QWhatsThis::add( layoutCombo, wtstr );
  QWhatsThis::add( l, wtstr );

  connect(layoutCombo, SIGNAL(activated(int)), this, SLOT(configChanged()));

  grp = new QGroupBox(i18n("Additional layouts"), layout);
  vvbox->addWidget(grp,1);
  QWhatsThis::add( grp, i18n("You can select an arbitrary number of additional keyboard layouts."
    " If one or more additional layouts have been selected, the KDE panel will offer a docked flag."
    " By clicking on this flag you can easily switch between layouts.") );

  QVBoxLayout *vbox = new QVBoxLayout(grp, 6,6);
  vbox->addSpacing(grp->fontMetrics().height());

  additional = new QListView(grp);
  vbox->addWidget(additional);
  additional->header()->hide();
  additional->addColumn(i18n("Check"), 22);
  additional->addColumn(i18n("Map"), 28);
  additional->addColumn(i18n("Layout"));

  connect(additional, SIGNAL(clicked(QListViewItem*)), this, SLOT(configChanged()));


  keys = new KGlobalAccel();
#include "kxkbbindings.cpp"
  keys->readSettings();
  m_keyMap = keys->keyDict();
  chooser = new KKeyChooser( &m_keyMap, this );
  connect( chooser, SIGNAL( keyChange() ), SLOT( configChanged() ));

  
  tab->addTab(layout, i18n("Layout"));
  tab->addTab( chooser, i18n("Shortcut"));

  load();
}


LayoutConfig::~LayoutConfig()
{
  delete keys;
}


void setCurrent(QComboBox *box, QString text)
{
  for (int i=0; i<box->count(); ++i)
    if (box->text(i) == text)
      {
	box->setCurrentItem(i);
	return;
      }
}


QString lookup(const QDict<char> &dict, QString text)
{
  QDictIterator<char> it(dict);
  while (it.current())
    {
      if ( it.current() == text )
	return it.currentKey();
      ++it;
    }

  return QString::null;
}

QString lookupLocalized(const QDict<char> &dict, QString text)
{

  QDictIterator<char> it(dict);
  while (it.current())
    {
      if ( i18n(it.current()) == text )
        return it.currentKey();
      ++it;
    }

  return QString::null;
}

void LayoutConfig::load()
{
  // fill the rules combo
  /*
  ruleCombo->clear();
  ruleCombo->insertStringList(KeyRules::rules());
  */

  // open the config file
  KConfig *config = new KConfig("kxkbrc", true);
  config->setGroup("Layout");

  // find out which rule applies
  QString rule = config->readEntry("Rule", "xfree86");
  //  setCurrent(ruleCombo, rule);

  // update the other files
  ruleChanged(rule);

  // find out about the layout and the model
  QString model = config->readEntry("Model", "pc104");
  QString layout = config->readEntry("Layout", "us");

  // TODO: When no model and no layout are known (1st start), we really
  // should ask the X-server about it's settings!

  QString m_name = rules->models()[model];
  setCurrent(modelCombo, i18n(m_name.local8Bit()));

  QString l_name = rules->layouts()[layout];
  setCurrent(layoutCombo, i18n(l_name.local8Bit()));

  // fill in the additional layouts
  additional->clear();
  QStringList tmp;
  QDictIterator<char> it2(rules->layouts());
  while (it2.current())
    {
      tmp.append(it2.current());
      ++it2;
    }
  tmp.sort();

  QStringList adds = config->readListEntry("Additional");

  QStringList::Iterator l;
  for (l = tmp.begin(); l != tmp.end(); ++l)
    {
      QCheckListItem *item = new QCheckListItem(additional, "", QCheckListItem::CheckBox);
      if (adds.contains(lookup(rules->layouts(), *l)))
	item->setOn(true);
      item->setPixmap(1, findPixmap(lookup(rules->layouts(), *l)));
      item->setText(2, i18n((*l).local8Bit())  );
    }
  additional->setSorting(2);

  delete config;

  for (KKeyEntryMap::Iterator it = m_keyMap.begin(); it != m_keyMap.end(); ++it)
  {
      (*it).aConfigKeyCode = (*it).aCurrentKeyCode;
  }
  chooser->listSync();
}


void LayoutConfig::ruleChanged(const QString &rule)
{
  QString model, layout;
  if (rules)
    {
      model = lookupLocalized(rules->models(), modelCombo->currentText());
      layout = lookupLocalized(rules->layouts(), layoutCombo->currentText());
    }

  delete rules;
  rules = new KeyRules(rule);

  QStringList tmp;
  modelCombo->clear();
  QDictIterator<char> it(rules->models());
  while (it.current())
    {
      tmp.append(i18n(it.current()));
      ++it;
    }
  tmp.sort();
  modelCombo->insertStringList(tmp);

  tmp.clear();
  layoutCombo->clear();
  QDictIterator<char> it2(rules->layouts());
  while (it2.current())
    {
      tmp.append(it2.current());
      ++it2;
    }
  tmp.sort();

  QStringList::Iterator l;
  for (l = tmp.begin(); l != tmp.end(); ++l)
    {
      layoutCombo->insertItem(findPixmap(lookup(rules->layouts(), *l)), i18n((*l).local8Bit()));
    }

  QString m_name = rules->models()[model];
  setCurrent(modelCombo, m_name);

  QString l_name = rules->layouts()[layout];
  setCurrent(layoutCombo, l_name);
}


void LayoutConfig::save()
{
  KConfig *config = new KConfig("kxkbrc", false);
  config->setGroup("Layout");
  //  config->writeEntry("Rule", ruleCombo->currentText());

  QString layout = lookupLocalized(rules->layouts(), layoutCombo->currentText());
  config->writeEntry("Model", lookupLocalized(rules->models(), modelCombo->currentText()));
  config->writeEntry("Layout", layout );
  config->writeEntry("Encoding", rules->encodings().find( layout ) );

  config->writeEntry("Test", "Test");

  QStringList tmp;
  QStringList encodings;
  QListViewItem *item = additional->firstChild();
  while (item)
    {
      QCheckListItem *cli = (QCheckListItem*) item;
      if (cli->isOn()) {
	  QString layout = lookupLocalized(rules->layouts(), cli->text(2));
	tmp.append(layout);
	encodings.append(rules->encodings().find( layout ) );
      }
      item = item->nextSibling();
    }
  config->writeEntry("Additional", tmp);
  config->writeEntry("AdditionalEncodings", encodings);

  config->writeEntry("Use", true);

  config->sync();

  delete config;

  keys->setKeyDict( m_keyMap );
  keys->writeSettings();

  kapp->kdeinitExec("kxkb");
}


void LayoutConfig::defaults()
{
  ruleChanged("xfree86");

  setCurrent(modelCombo, "pc104");
  setCurrent(layoutCombo, "us");

  QListViewItem *item = additional->firstChild();
  while (item)
    {
      QCheckListItem *cli = (QCheckListItem*) item;
      cli->setOn(false);
      item = item->nextSibling();
    }
}

QString LayoutConfig::quickHelp() const
{
  return i18n("<h1>Keyboard Layout & Model</h1> Here you can choose your keyboard layout and model."
    " The 'model' refers to the type of keyboard that is connected to your computer, while the keyboard"
    " layout defines \"which key does what\" and may be different for different countries.<p>"
    " Apart of a 'primary layout' which will be used as default, you can specify additional layouts,"
    " between which you can easily switch using the KDE panel.");
}

void LayoutConfig::configChanged()
{
  emit changed(true);
}


extern "C"
{
  KCModule *create_layout(QWidget *parent, const char *name)
  {
    KGlobal::locale()->insertCatalogue("kcmlayout");
    return new LayoutConfig(parent, name);
  };

  void init_layout()
  {
     KConfig *config = new KConfig("kxkbrc", true, false);
     config->setGroup("Layout");
     if (config->readBoolEntry("Use", false) == true)
        kapp->startServiceByDesktopName("kxkb");
  }
}
