/****************************************************************************
*
* $RCSfile: LocaleManager.c $
* $Revision: 1.1 $
* $Date: 1997/07/01 11:07:21 $
* $Author: ssolie $
*
*****************************************************************************
*
* Copyright (c) 1997 Software Evolution.  All Rights Reserved.
*
*****************************************************************************
*
* LocaleManager.c -- Locale manager source file
*
* This file contains the source for LocaleManager objects.
*/

#include <exec/memory.h>
#include <libraries/locale.h>

#include <proto/exec.h>
#include <proto/locale.h>

#include "Debug.h"
#include "LocaleManager.h"


/*** Global variables ***/
IMPORT struct ExecBase *SysBase;
struct Library *LocaleBase;


/*** Local variables ***/


/*** Local constants ***/
const STRPTR	LOCALE_LIB		= "locale.library";
const ULONG		LOCALE_VER		= 38;
const STRPTR	MSG_NULL_STR	= "*NULL*";


/*** Local data types ***/
struct LocaleManagerClass {
	struct Locale *locale;			/* locale pointer */
	struct Catalog *catalog;		/* catalog pointer */
};


/****** SERL/newLocaleManager ***********************************************
*
*   NAME
*       newLocaleManager -- Create LocaleManager object
*
*   SYNOPSIS
*       locale_mgr = newLocaleManager(catalog, tags)
*
*       LocaleManager = newLocaleManager(STRPTR, struct TagItem *);
*
*   FUNCTION
*       This function creates a new LocaleManager object.  The system
*       locale.library will be opened, the default locale and the given
*       catalog.
*
*       If the locale initialization fails for any reason, the locale
*       manager is still in a usable state and will return default strings.
*
*   INPUTS
*       catalog - Name of the catalog to use.
*       tags - Array of TagItem's (currently set to NULL).
*
*   RESULT
*       Reference to a LocaleManager object or NULL on error.
*
*   NOTES
*       Locale is designed so that failure does not stop the application.
*       The locale manager routines are also designed so that failure does
*       not stop an application.
*
*   WARNING
*       There can only be one valid LocaleManager object per process.
*
*   SEE ALSO
*       SERL/deleteLocaleManager()
*
*****************************************************************************
*
*/
LocaleManager newLocaleManager(STRPTR catalog, struct TagItem *tags)
{
	STATIC struct TagItem cat_tags[] = { {TAG_DONE} };
	LocaleManager this;

	D(bug("newLocaleManager(%s, %lx)\n", catalog, tags));

	if ( catalog == NULL || tags != NULL )
		return(NULL);

	this = AllocVec(sizeof(struct LocaleManagerClass), MEMF_CLEAR);
	if ( this == NULL )
		return(NULL);

	LocaleBase = OpenLibrary(LOCALE_LIB, LOCALE_VER);
	if ( LocaleBase != NULL )  {
		this->locale = OpenLocale(NULL);
		this->catalog = OpenCatalogA(this->locale, catalog, cat_tags);

		D2(bug(" locale=%lx, catalog=%lx\n", this->locale, this->catalog));
	}

	return(this);
}


/****** SERL/deleteLocaleManager ********************************************
*
*   NAME
*       deleteLocaleManager -- Delete LocaleManager object
*
*   SYNOPSIS
*       deleteLocaleManager(locale_mgr)
*
*       VOID deleteLocaleManager(LocaleManager);
*
*   FUNCTION
*       This function deletes a LocaleManager object.  The locale strings
*       will be invalid after calling this function.
*
*   INPUTS
*       locale_mgr - Reference to LocaleManager or NULL.
*
*   SEE ALSO
*       SERL/newLocaleManager()
*
*****************************************************************************
*
*/
VOID deleteLocaleManager(LocaleManager this)
{
	D(bug("deleteLocaleManager(%lx)\n", this));

	if ( this != NULL )  {
		if ( LocaleBase != NULL )  {
			CloseCatalog(this->catalog);
			CloseLocale(this->locale);
			CloseLibrary(LocaleBase);
			LocaleBase = NULL;
		}

		FreeVec(this);
	}
}


/****** SERL/getString ******************************************************
*
*   NAME
*       getString -- Get localized string
*
*   SYNOPSIS
*       string = getString(locale_mgr, id, def_str)
*
*       STRPTR getString(LocaleManager, ULONG, STRPTR);
*
*   FUNCTION
*       This function gets a localized string for the caller.  If the
*       locale is not setup or an error occurs, a valid string pointer
*       will still be returned.
*
*   INPUTS
*       locale_mgr - Reference to a LocaleManager object or NULL.
*       id - String number.
*       def_str - Default string or NULL.
*
*   RESULT
*       A valid string pointer.  If there any problems getting the
*       localized string, the default string is returned.
*
*   NOTES
*       This function is usable with or without a valid LocaleManager.
*
*   SEE ALSO
*
*****************************************************************************
*
*/
STRPTR getString(LocaleManager this, ULONG id, STRPTR def_str)
{
	STRPTR string;

	D(bug("getString(%lx, %lu, %s)\n", this, id, def_str));

	if ( this == NULL || def_str == NULL )
		string = MSG_NULL_STR;
	else  {
		if ( LocaleBase == NULL )
			string = def_str;
		else
			string = GetCatalogStr(this->catalog, id, def_str);
	}

	return(string);
}
