/*------------------------------------------------------------------------------
 src/lib/djWidget/prefs.cxx
 
 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

#include "djWidgetBase.h"
#include <FL/Fl_Preferences.H>
#include <stdlib.h>

namespace dj {

Prefs::Prefs(const Str & name) :
  _prefs(0)
{
  _prefs = new Fl_Preferences(Fl_Preferences::USER, "djv", name.toAscii());
}

Prefs::Prefs(const Prefs & in, const Str & group)
{
  _prefs = new Fl_Preferences(in._prefs, group.toAscii());
}

Prefs::Prefs(Prefs * in, const Str & group)
{
  _prefs = new Fl_Preferences(in->_prefs, group.toAscii());
}

Prefs::~Prefs()
{
  delete _prefs;
}

void Prefs::set(const Str & in, bool value)
{
  _prefs->set(in.toAscii(), boolStr()[value].toAscii());
}

void Prefs::set(const Str & in, int value)
{
  _prefs->set(in.toAscii(), value);
}

void Prefs::set(const Str & in, double value)
{
  _prefs->set(in.toAscii(), value);
}

void Prefs::set(const Str & in, const Str & value)
{
  _prefs->set(in.toAscii(), value.toAscii());
}

void Prefs::set(const Str & in, const StrList & value)
{
  _prefs->set(in.toAscii(), join(value, ':').toAscii());
}

void Prefs::set(const Str & in, const Color & value)
{
  set(in, deflateStr(value));
}

bool Prefs::get(const Str & in, bool * out, bool defaultValue) const
{
  char * tmp = 0;
  const bool found =
    _prefs->get(in.toAscii(), tmp, boolStr()[defaultValue].toAscii());
  *out = find(boolStr(), Str(tmp));
  free(tmp);
  return found;
}

bool Prefs::get(const Str & in, int * out, int defaultValue) const
{
  return _prefs->get(in.toAscii(), *out, defaultValue);
}

bool Prefs::get(const Str & in, double * out, double defaultValue) const
{
  return _prefs->get(in.toAscii(), *out, defaultValue);
}

bool Prefs::get(const Str & in, Str * out, const Str & defaultValue) const
{
  char * buf = 0;
  const bool found = _prefs->get(in.toAscii(), buf, defaultValue.toAscii());
  *out = Str(buf);
  free(buf);
  return found;
}

bool Prefs::get(
  const Str & in, StrList * out, const StrList & defaultValue
) const {
  char * buf = 0;
  const bool found = _prefs->get(
    in.toAscii(), buf, join(defaultValue, ':').toAscii()
  );
  *out = split(Str(buf), ':');
  free(buf);
  return found;
}
  
bool Prefs::get(const Str & in, Color * out, const Color & defaultValue) const
{
  StrList data;
  if (! get(in, &data, deflateStr(defaultValue))) return false;
  inflateStr(data, out);
  return true;
}

const StrList Prefs::nameList() const
{
  StrList out;
  const int size = _prefs->entries();
  for (int i = 0; i < size; ++i)
    out += Str(_prefs->entry(i));
  return out;
}
  
void Prefs::del(const Str & in)
{
  _prefs->deleteEntry(in.toAscii());
}

void Prefs::groupDel(const Str & in)
{
  _prefs->deleteGroup(in.toAscii());
}

const StrList Prefs::groupList() const
{
  StrList out;
  const int size = _prefs->groups();
  for (int i = 0; i < size; ++i)
    out += Str(_prefs->group(i));
  return out;
}

}
