/********************************************************************************************************/
/*
 *  manageconfig.c - Load/Save the app parameters between 2 sessions
 *
 *  Copyright (C) 2001-2002  Sigma Designs
 *                           written by Pascal Cannenterre <pascal_cannenterre@sdesigns.com>
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
/***********************************************************************************************************/
#include "define.h"
#include "configfile.h"
#include "playlistfile.h"
#include "manageconfig.h"

static gchar *ovl_keys[] = 
{
  "VideoOutputMode",
  "TvStandard",
  "TvComponentMode",
  "Brightness",
  "Saturation",
  "Contrast",
  "AudioVolume",
  "Balance",
  "AudioChannelMode",
  "AudioOutputFormat",
  "VgaFullscreen"
};

static gchar *localfile_keys[] = {
  "LatestOpenedFilename"
};

/* static gchar *rtspurl_keys[] = { */
/*   "LatestStreamedUrl" */
/* }; */

gboolean load_fmp_config(FMPOVERLAYVDO * ovl)
{
  gint value;
  gboolean b_value;
  ConfigFile *configfile = cfg_open_default_file();
  /* 
   * testing file to see whether or not it's empty
   * it should have, at least, one section ...
   */
  if(configfile->sections == NULL)
    {
      if(!cfg_create_default_config_dir())
	return FALSE;
      save_fmp_config(*ovl);
      configfile = cfg_open_default_file();
      if(configfile->sections == NULL) /* trouble with the config file */
	return FALSE; /* so we stick with default values */
    }
  
  if(cfg_read_int(configfile, FMP_SECTION, ovl_keys[0], &value))
    ovl->video_output_mode = (DWORD)value;
  if(cfg_read_int(configfile, FMP_SECTION, ovl_keys[1], &value))
    ovl->tv_standard = (DWORD)value;
  if(cfg_read_int(configfile, FMP_SECTION, ovl_keys[2], &value))
    ovl->tv_component = (DWORD)value;
  if(cfg_read_int(configfile, FMP_SECTION, ovl_keys[3], &value))
    ovl->brt = (DWORD)value;
  if(cfg_read_int(configfile, FMP_SECTION, ovl_keys[4], &value))
    ovl->sat = (DWORD)value;
  if(cfg_read_int(configfile, FMP_SECTION, ovl_keys[5], &value))
    ovl->con = (DWORD)value;
  if(cfg_read_int(configfile, FMP_SECTION, ovl_keys[6], &value))
    ovl->volume = (DWORD)value;
  if(cfg_read_int(configfile, FMP_SECTION, ovl_keys[7], &value))
    ovl->balance = (DWORD)value;
  if(cfg_read_int(configfile, FMP_SECTION, ovl_keys[8], &value))
    ovl->audio_mode = (DWORD)value;
  if(cfg_read_int(configfile, FMP_SECTION, ovl_keys[9], &value))
    ovl->audio_output = (DWORD)value;
  if(cfg_read_boolean(configfile, FMP_SECTION, ovl_keys[10], &b_value))
    ovl->b_fullscreen = (BOOL)b_value;
  
  return TRUE;
}

gboolean save_fmp_config(FMPOVERLAYVDO ovl)
{
  ConfigFile *configfile = cfg_open_default_file();

  cfg_write_int(configfile, FMP_SECTION, ovl_keys[0], ovl.video_output_mode);
  cfg_write_int(configfile, FMP_SECTION, ovl_keys[1], ovl.tv_standard);
  cfg_write_int(configfile, FMP_SECTION, ovl_keys[2], ovl.tv_component);
  cfg_write_int(configfile, FMP_SECTION, ovl_keys[3], ovl.brt);
  cfg_write_int(configfile, FMP_SECTION, ovl_keys[4], ovl.sat);
  cfg_write_int(configfile, FMP_SECTION, ovl_keys[5], ovl.con);
  cfg_write_int(configfile, FMP_SECTION, ovl_keys[6], ovl.volume);
  cfg_write_int(configfile, FMP_SECTION, ovl_keys[7], ovl.balance);
  cfg_write_int(configfile, FMP_SECTION, ovl_keys[8], ovl.audio_mode);
  cfg_write_int(configfile, FMP_SECTION, ovl_keys[9], ovl.audio_output);
  cfg_write_boolean(configfile, FMP_SECTION, ovl_keys[10], ovl.b_fullscreen);

  return cfg_write_default_file(configfile);
}

gboolean save_localfile_config(INFO info)
{
  ConfigFile *configfile = cfg_open_default_file();
  
  cfg_write_string(configfile, LOCALFILE_SECTION, localfile_keys[0], info.sFilename);

  return cfg_write_default_file(configfile);
}

gboolean load_localfile_config(INFO * info)
{
  gchar *str_value = NULL;
  
  ConfigFile *configfile = cfg_open_default_file();
  if(configfile->sections == NULL)
    {
      if(!cfg_create_default_config_dir())
	return FALSE;
      save_localfile_config(*info);
      configfile = cfg_open_default_file();
      if(configfile->sections == NULL) /* trouble with the config file */
	return FALSE; /* so we stick with default values */
    }

  if(cfg_read_string(configfile, LOCALFILE_SECTION, localfile_keys[0], &str_value))
    info->sFilename = (char*) str_value;
  
  return TRUE;
}

gboolean load_playlist(gchar *filename)
{
  GtkCList *clist = NULL;
  clist = 
    GTK_CLIST(gtk_object_get_data (GTK_OBJECT (GUI.playlistwin), 
				   "playlist"));
  if(clist != NULL)
    {
      if(filename != NULL)
	{
	  return playlist_open_file(clist, filename); 
	}
      else
	{
	  return playlist_open_default_file(clist);
	}
    }
  else
    {
      G_WARNING("clist is null !!!");
      return FALSE;
    }
}


gboolean save_playlist(gchar *filename)
{
  GtkCList *clist = NULL;
  
  clist = 
    GTK_CLIST(gtk_object_get_data (GTK_OBJECT (GUI.playlistwin), 
				   "playlist"));
  if(clist != NULL)
    {
      if(filename != NULL)
	{
	  return playlist_write_file(clist, 0, filename); 
	}
      else
	{
	  return playlist_write_default_file(clist, 0);
	}
    }
  else
    {
      G_WARNING("clist is null !!!");
      return FALSE; 
    }
}
