// 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. 

// Copyright 2003 Liam Girdwood  

#include "library.hh"
#include "config.h"

#define HOME	"~/.nova/catalogs"

namespace Pollux
{

Library* Library::m_instance = 0;

/*! \fn Library::Library ()
* 
* Constructor
* Set up default library and catalog paths
*
* This is private, Library is a singleton and instantiated by
* caling Library* Library::get_library ()	
*/
Library::Library ()
{
	// install default catalog paths
	std::string* p1 = new std::string(HOME);
	std::string* p2 = new std::string(NOVA_CATALOG_DIR);
	
	m_path.push_back(p1);
	m_path.push_back(p2);
	
	// disable progress
	m_cat = 0;
}


/*! \fn Library::~Library ()
*
* Destructor.
* Free library resources
*/
Library::~Library ()
{
	m_path.clear();
	m_cat_list.clear();
}

/* \fn Library* Library::get_library ()
* \return Pointer to Library
*
* Library is a singleton object and can only be created
* by calling this function.
*/
Library* Library::get_library ()
{
	if (!m_instance)
		m_instance = new Library();
	return m_instance;
}


/*! \fn Catalog* Library::get_catalog(std::string& name, Glib::Dispatcher* signal)
* \param name Catalog name
* \param signal Pointer to a Glib::Dispatcher object for updating progress
* \return Pointer to catalog if successful, otherwise 0
*
* Get an astro catalog from the library. If the catalog is not in the
* library, then try and load it off disk.
*/
Catalog* Library::get_catalog(std::string& name, Glib::Dispatcher* signal)
{
	bool success = false;
	std::string name2;
	
	// are we looking for the default catalog ?
	if (name == "default")
		name = DEFAULT;
	
	// check cat_list for catalog
	for (std::vector<Catalog*>::iterator j = m_cat_list.begin(); j != m_cat_list.end(); j++)  {
		(*j)->get_name(name2);
		std::cout << "cat " << name << " " << name2 << std::endl;
		if (name == name2) {
			return *j;
		}
	}
	
	// ok, catalog not in memory, so try and load it
	Catalog* cat = new Catalog();
	if (signal)
		m_cat = cat;
	
	// iterate through path for catalog
	for (std::vector<std::string*>::iterator j = m_path.begin(); j != m_path.end(); j++)  {
		std::string file = **j + "/";
		if (cat->load (name, file, signal)) {
			success = true;
			break;  // got it
		}
	}
	
	// did we get it ?
	if (success) {
		add_catalog (cat);
		m_cat = 0;
		return cat;
	} else {
		m_cat = 0;
		delete cat;
		return 0;
	}
}	

/*! \fn double Library::get_progress ()
* \return Catalog progress
*
* Return the current progress of a catalog load / save operation.
* Note: This is between 0 and 1
*/
double Library::get_progress ()
{
	if (m_cat)
		return (m_cat->get_progress());
}


/*! \fn void Library::add_search_path (std::string& path)
* \param path New path
*
* Add a new path to the catalog search path.
*/
void Library::add_search_path (std::string& path)
{
	std::string* p = new std::string(path);
	m_path.push_back(p);
}


/*! \fn void Library::add_catalog (Catalog* cat)
* \param cat Catalog to be added
*
* Add a catalog to the Nova library.
*/
void Library::add_catalog (Catalog* cat)
{
	std::string name;
	cat->get_name(name);

	m_cat_list.push_back(cat);
}

}


syntax highlighted by Code2HTML, v. 0.9.1