/*
    GDAM - Geoff & Dave's Audio Mixer
    Copyright (C) 1999    Dave Benson, Geoff Matters.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

    Contact:
        daveb@ffem.org <Dave Benson>
        geoff@ugcs.caltech.edu <Geoff Matters>
*/
#ifndef __GDAM_SONG_DATABASE_H_
#define __GDAM_SONG_DATABASE_H_

#include <stdio.h>

typedef struct _GdamSongDatabase GdamSongDatabase;
typedef struct _GdamSongDatabaseClass GdamSongDatabaseClass;

#include "gdamsonginfo.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include <gtk/gtkobject.h>

GtkType gdam_song_database_get_type();
#define GDAM_TYPE_SONG_DATABASE			(gdam_song_database_get_type ())
#define GDAM_SONG_DATABASE(obj)              (GTK_CHECK_CAST ((obj), GDAM_TYPE_SONG_DATABASE, GdamSongDatabase))
#define GDAM_SONG_DATABASE_CLASS(klass)      (GTK_CHECK_CLASS_CAST ((klass), GDAM_TYPE_SONG_DATABASE, GdamSongDatabaseClass))
#define GDAM_IS_SONG_DATABASE(obj)           (GTK_CHECK_TYPE ((obj), GDAM_TYPE_SONG_DATABASE))
#define GDAM_IS_SONG_DATABASE_CLASS(klass)   (GTK_CHECK_CLASS_TYPE ((klass), GDAM_TYPE_SONG_DATABASE))

struct _GdamSongDatabaseClass {
	GtkObjectClass		object_class;

	void                  (*changed)(GdamSongDatabase*);
	void                  (*song_info_changed)(GdamSongDatabase*,
	                                           GdamSongInfo*);
};

typedef gboolean (*GdamSongInfoDecideFunc) (GdamSongInfo* song_info, 
					    gpointer      ptr);
					

struct _GdamSongDatabase {
	GtkObject		object;

	char*          		dbname;
	GTree*         		by_order;	/* trees of GdamSongInfo's */
	GTree*         		by_name;
	GTree*         		by_length;
	GTree*         		by_bpm;
	GTree*         		by_artist;
	GTree*         		by_album;

	GHashTable*    		name_lookup;
	int			frozen_count;
	gboolean		changed_while_frozen;
};

/* The default database. */
GdamSongDatabase*    gdam_song_database_system  ();

/* Basic database manipulation functions... */
GdamSongDatabase*    gdam_song_database_new_from_file
                                                (const char* filename);
/* 'database' can be NULL, equivalent to new_from_file.  */
GdamSongDatabase*    gdam_song_database_add_from_file
                                                (GdamSongDatabase* database,
						 const char*       filename);

void                 gdam_song_database_add     (GdamSongDatabase* db,
                                                 GdamSongInfo*     song_info);
void                 gdam_song_database_remove  (GdamSongDatabase* db,
                                                 const char*       filename);


void                 gdam_song_database_write_fp(GdamSongDatabase*, 
                                                 FILE*);
void                 gdam_song_database_write   (GdamSongDatabase*,
                                                 const char* fname);
  /*  TODO:  If we want to write songinfos selectively, we should use a
   *  function pointer   gboolean show?(songinfo*)  */
void                 gdam_song_database_write_ext(GdamSongDatabase*,
                                                  const char* fname,
						  GdamSongInfoDecideFunc,
						  gpointer ptr);

GdamSongInfo*	     gdam_song_database_find	(GdamSongDatabase*,
						 const char* fname);

void                 gdam_song_database_freeze(GdamSongDatabase*);
void                 gdam_song_database_thaw  (GdamSongDatabase*);


/* Set by command-line flags before gdam_song_database_system is called. */
extern const char* gdam_song_database_default_filename;

/* Empty database with given filename. */
GdamSongDatabase*    gdam_song_database_new     (const char* database_name);
#endif
