/**********************************************************************************************/
/*
 *  playlistfile.c - Utility functions to manage the application config file
 *
 *  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 "playlistcb.h"

gboolean playlist_write_file(GtkCList * clist, gint selected_column, gchar *filename)
{
  FILE *file = NULL;
  GList *node = NULL;
  gchar *text = NULL;
  guint total_row = 0;
  guint i = 0;
  
  if (!(file = fopen(filename, "w")))
    {
      G_WARNING("Can't open file to write in !!");
      return FALSE;
    }
  
  if(clist == NULL)
    {
      G_WARNING("CLIST is NULL !!");
      return FALSE;
    }
  
  gtk_clist_select_all(clist);

  node = clist->selection; 
  
  if(node == NULL)
    {
      G_WARNING("No rows in that clist !!");
      return FALSE;
    }
  
  total_row = g_list_length(node);
  
  for(i=0;i<total_row;i++)
    {
      gtk_clist_get_text(clist, i, 
			 selected_column, &text);
      
      if(text == NULL)
	{
	  G_WARNING("Couldn't retrieve data from row !!");
	  return FALSE;
	}
      
      fprintf(file,"%s\n",text);
    }
  
  gtk_clist_unselect_all(clist);
  fclose(file);
  return TRUE;
}

gchar *playlist_get_default_filename(void)
{
	static gchar *filename = NULL;
	if (!filename)
	  {
	    filename = g_strconcat(g_get_home_dir(),"/",
				   CONFIGFILE_DEFAULT_DIRECTORY,
				   g_get_prgname(),
				   PLAYLISTFILE_EXTENSION, NULL);
	    G_WARNING("Playlist file name is A%sA", filename);
	  }
	return filename;
}



gboolean playlist_open_file(GtkCList * clist, gchar * filename)
{
	FILE *file = NULL;
	gchar *buffer = NULL;
	gchar ** lines = NULL;
	gint i;
	struct stat stats;

	if (lstat(filename, &stats) == -1)
		return FALSE;
	if (!(file = fopen(filename, "r")))
		return FALSE;

	buffer = g_new0(gchar, stats.st_size + 1);
	if (fread(buffer, 1, stats.st_size, file) != stats.st_size)
	{
		g_free(buffer);
		fclose(file);
		return FALSE;
	}
	fclose(file);
	buffer[stats.st_size] = '\0';
	
	lines = g_strsplit(buffer, "\n", 0);
	g_free(buffer);
	i = 0;
	while ((lines[i]) != NULL)
	  {
	    playlist_add(lines[i]);
	    i++;
	  }
	g_strfreev(lines);
	return TRUE;
}


gboolean playlist_open_default_file(GtkCList * clist)
{
  return playlist_open_file(clist, playlist_get_default_filename());
}


gboolean playlist_write_default_file(GtkCList * clist, gint selected_column)
{
  return playlist_write_file(clist, selected_column, playlist_get_default_filename());
}

