/***************************************************************************
 *   Copyright (C) 2005 Nicolas Hadacek <hadacek@kde.org>                  *
 *   Copyright (C) 2003-2004 Alain Gibaud <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 "global_config.h"

#include <tdeapplication.h>
#include <tdeconfig.h>
#include <tdelocale.h>

#include "progs/list/prog_list.h"
#include "tools/list/tool_list.h"

const BaseGlobalConfig::Data BaseGlobalConfig::DATA[Nb_Types] = {
  { "auto_rebuild_modified", I18N_NOOP("Automatically rebuild project before programming if it is modified."), TQVariant(true) },
  { "program_after_build", I18N_NOOP("Program device after successful build."), TQVariant(false) },
  { "user_id_set_to_checksum", I18N_NOOP("Set User Ids to unprotected checksum (if User Ids are empty)."), TQVariant(false) },
  { "show_tab_close_buttons", I18N_NOOP("Show close buttons on tabs (need restart to take effect)."), TQVariant(true) }
};

PURL::Url GlobalConfig::openedProject()
{
  GenericConfig config(TQString::null);
  return PURL::Url::fromPathOrUrl(config.readEntry("project", TQString::null));
}
void GlobalConfig::writeOpenedProject(const PURL::Url &p)
{
  GenericConfig config(TQString::null);
  config.writeEntry("project", p.filepath());
}

PURL::UrlList GlobalConfig::openedFiles()
{
  GenericConfig config(TQString::null);
  PURL::UrlList files;
  uint i = 0;
  for (;;) {
    TQString file = config.readEntry(TQString("file%1").arg(i), TQString::null);
    if ( file.isEmpty() ) break;
    files += PURL::Url::fromPathOrUrl(file);
    i++;
  }
  return files;
}
void GlobalConfig::writeOpenedFiles(const PURL::UrlList &files)
{
  GenericConfig config(TQString::null);
  for (uint i=0; i<=files.count(); i++) {
    TQString s = (i==files.count() ? TQString::null : files[i].filepath());
    config.writeEntry(TQString("file%1").arg(i), s);
  }
}

void GlobalConfig::writeProgrammerGroup(const Programmer::Group &group)
{
  GenericConfig config(TQString::null);
  config.writeEntry("programmer", group.name());
}
const Programmer::Group &GlobalConfig::programmerGroup()
{
  GenericConfig config(TQString::null);
  TQString s = config.readEntry("programmer");
  const Programmer::Group *group = Programmer::lister().group(s);
  if ( group==0 ) return *Programmer::lister().begin().data();
  return *group;
}

void GlobalConfig::writeDebugLevel(Log::DebugLevel level)
{
  GenericConfig config(TQString::null);
  config.writeEnumEntry<Log::DebugLevel>("log_debug_level", level);
}
Log::DebugLevel GlobalConfig::debugLevel()
{
  GenericConfig config(TQString::null);
  return config.readEnumEntry<Log::DebugLevel>("log_debug_level", Log::DebugLevel::Normal);
}

void GlobalConfig::writeLogOutputType(Log::OutputType type)
{
  GenericConfig config(TQString::null);
  config.writeEntry("log_output_type", type);
}
Log::OutputType GlobalConfig::logOutputType()
{
  GenericConfig config(TQString::null);
  uint output = config.readUIntEntry("log_output_type", Log::GuiOnly);
  if ( output>=Log::Nb_OutputTypes ) return Log::GuiOnly;
  return Log::OutputType(output);
}

void GlobalConfig::writeShowLineNumbers(bool show)
{
  GenericConfig config(TQString::null);
  config.writeEntry("show_line_numbers", show);
}
bool GlobalConfig::showLineNumbers()
{
  GenericConfig config(TQString::null);
  return config.readBoolEntry("show_line_numbers", false);
}
