/* libsswf_tag_import.c++ -- written by Alexis WILKE for Made to Order Software Corp. (c) 2002-2008 */

/*

Copyright (c) 2002-2008 Made to Order Software Corp.

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and
associated documentation files (the "Software"), to
deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

/** \file
 *
 * \brief The implementation of the sswf::TagImport class
 *
 * This file declares the body of the functions which are not
 * inline. It is part of the SSWF library.
 */

#include	"sswf/libsswf.h"

using namespace sswf;

/////////////////////////////////////////// TagImport


/** \class sswf::TagImport
 *
 * \brief Import a set of object defined in another movie
 *
 * This object is used to declare a list of objects defined
 * in another movie but are used in this movie.
 *
 * The other movie must have a corresponding TagExport with
 * the different names specified in this TagImport tag.
 *
 * \sa sswf::TagExport
 * \sa <a href="../SWFalexref.html#tag_import">SWF Alexis' Reference&mdash;Import</a>
 * \sa <a href="../SWFalexref.html#swf_tag">SWF Alexis' Reference&mdash;swf_tag</a>
 */


/** \brief Initialize the tag import object
 *
 * This object can be created with the TagHeader as a parent.
 *
 * It expects a URL and Filename to know which other movie it
 * is to load, and a list of the objects to load from that
 * other movie.
 *
 * \param[in] parent The parent of the tag import
 */
TagImport::TagImport(TagBase *parent)
	: TagBase("import", parent)
{
	f_url = 0;
	f_filename = 0;
	//f_objects = ...
}


/** \brief The type of the TagImport
 *
 * The TagImport is of type SWF_TYPE_DEFINE.
 *
 * \return SWF_TYPE_DEFINE
 */
TagBase::swf_type_t TagImport::TypeFlags(void) const
{
	return SWF_TYPE_DEFINE;
}


/** \brief Search the name for a given identifier
 *
 * This function searches for an object with the specified identifier.
 *
 * If the identifier is found, then the name is returned, otherwise
 * NULL is returned.
 *
 * \param[in] id The identifier to search for
 *
 * \return The corresponding name or NULL
 */
const char *TagImport::HasID(sswf_id_t id) const
{
	long		idx;
	import_t	*i;

	idx = f_objects.Count();
	while(idx > 0) {
		idx--;
		i = dynamic_cast<import_t *>(f_objects.Get(idx));
		if(i->f_id == id) {
			return i->f_name;
		}
	}

	return 0;
}



/** \brief Search the identifier for a given name
 *
 * This function searches for an object with the given name.
 *
 * If the name is found, then the corresponding identifier is
 * returned otherwise it returns zero.
 *
 * \param[in] name The name of the object to search for
 *
 * \return The corresponding identifier or zero
 */
sswf_id_t TagImport::HasName(const char *name) const
{
	long		idx;
	import_t	*i;

	idx = f_objects.Count();
	while(idx > 0) {
		idx--;
		i = dynamic_cast<import_t *>(f_objects.Get(idx));
		if(strcmp(i->f_name, name) == 0) {
			return i->f_id;
		}
	}

	return 0;
}



/** \brief Set the URL of the input movie
 *
 * This URL points to the input movie which exports the objects
 * named in this TagImport. This is a hard coded URL as used on
 * your server.
 *
 * \param[in] url The URL to load the movie from
 */
void TagImport::SetURL(const char *url)
{
	MemFree(f_url);
	f_url = StrDup(url);
}


/** \brief Filename of the original input movie
 *
 * This filename is used to specify the original filename. This
 * is not saved in the output Flash movie. A tool can use this
 * name to also build the input movie. The access to the input
 * movie must be defined using the SetURL() function.
 *
 * \param[in] filename The name of the file to compile to have an input movie
 */
void TagImport::SetFilename(const char *filename)
{
	MemFree(f_filename);
	f_filename = StrDup(filename);
}


/** \brief Add an object to import
 *
 * This function adds one object by name to the list of objects to
 * import. The name must be the same as the name found in the
 * TagExport of the input movie.
 *
 * The type is only informational and it is not saved in the
 * Flash animation.
 *
 * The identifier of the imported object is automatically assigned
 * using the sswf::TagHeader::NextID() function. You can retrieve
 * the identifier with the HasName() function after you added an
 * object.
 *
 * \param[in] name The name of the object
 * \param[in] type The type of the object
 */
void TagImport::AddName(const char *name, const char *type)
{
	import_t	*i;

	i = new import_t;
	MemAttach(i, sizeof(import_t), "TagImport::AddName() -- object info for import");
	i->f_id = Header()->NextID();
	i->f_name = StrDup(name);
	i->f_type = StrDup(type);

	f_objects.Set(-1, i);
}


/** \brief Ensures the minimum version
 *
 * This function requests that the movie be at least of version 5.
 *
 * \return ErrorManager::ERROR_CODE_NONE when no error occured, an error otherwise
 */
ErrorManager::error_code_t TagImport::PreSave(void)
{
	MinimumVersion(5);

	return ErrorManager::ERROR_CODE_NONE;
}


// defined in TagBase
ErrorManager::error_code_t TagImport::Save(Data& data)
{
	Data		sub_data;
	long		idx, max;
	import_t	*i;
	unsigned char	version;

	version = Version();

	if(SaveString(sub_data, f_url) < 0) {
		// TODO: an error occured and this string
		// wasn't saved!
	}

	if(version >= 8) {
		sub_data.PutByte(1);
		sub_data.PutByte(0);
	}

	max = f_objects.Count();
	sub_data.PutShort((unsigned short) max);

	for(idx = 0; idx < max; idx++) {
		i = dynamic_cast<import_t *>(f_objects.Get(idx));
		sub_data.PutShort(i->f_id);
		if(SaveString(sub_data, i->f_name) < 0) {
			// TODO: an error occured and this string
			// wasn't saved!
		}
		// here the type is ignored
	}

	if(version >= 8) {
		SaveTag(data, SWF_TAG_IMPORT2, sub_data.ByteSize());
	}
	else {
		SaveTag(data, SWF_TAG_IMPORT, sub_data.ByteSize());
	}

	data.Append(sub_data);

	return ErrorManager::ERROR_CODE_NONE;
}






/* The following options fold the documentation; use 'zi' to turn on and off
 *
 * vim: foldexpr=getline(v\:lnum)!~'^/\\*\\*'&&getline(v\:lnum)!~'^\ \\*'?0\:1 foldcolumn=2 foldmethod=expr
 */
