/* Directory hotlist -- for the Midnight Commander
 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
 * 2003, 2004, 2005, 2006, 2007,2008  Free Software Foundation, Inc.
 * 
 * 
 * See OrigAuthors.txt in top directory of module
 * 
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  
 */
/* ************************************************************************** */
#define USE_MC_MODULE__HOTLIST_PRIVATE_H 1
/* -------------------------------------------------------------------------- */
#include <config.h>

#include <stdlib.h>

#include "hotlist/private.h"
/* -------------------------------------------------------------------------- */
/* ************************************************************************** */
int hotlist_m_element_append_child(ts_hotlist_element *_parent, ts_hotlist_element *_hotlist_element){

    if ((_hotlist_element == NULL) || (_parent == NULL)) return FALSE;

    _hotlist_element->parent = (struct s_hotlist_element *) _parent;
    _parent->list = g_list_append(_parent->list, (gpointer) _hotlist_element);

    return TRUE;
}
/* -------------------------------------------------------------------------- */
ts_hotlist_element *hotlist_m_element_get_child_by_index(ts_hotlist_element *_hotlist_element, int _index){
    GList *srch;

    if ((_hotlist_element == NULL) || (_index < 0)) return NULL;
    srch = g_list_first(_hotlist_element->list);

    if (srch == NULL) return NULL;
    if (_index == 0 ) return (ts_hotlist_element *) srch->data;
    while (_index--){
	srch = g_list_next(srch);
	if (srch == NULL) return NULL;
    }
    return (ts_hotlist_element *) srch->data;
}
/* -------------------------------------------------------------------------- */
ts_hotlist_element *hotlist_m_element_create(ts_hotlist_element *_parent){
    ts_hotlist_element *hle;

    hle = g_new0(ts_hotlist_element, 1);
    if (hle == NULL) return FALSE;

    hle->parent		= (struct s_hotlist_element *) _parent;
    hle->label		= NULL;
    hle->directory	= NULL;
    hle->hotkey		= 0;
    hle->type		= -1;
    hle->list		= NULL;
    return hle;
}
/* -------------------------------------------------------------------------- */
void hotlist_m_element_free(ts_hotlist_element *_hotlist_element){
    if (_hotlist_element == NULL) return;

    if (_hotlist_element->list != NULL){
	GList *srch;
	srch = g_list_first(_hotlist_element->list);
	while (srch){
	    hotlist_m_element_free((ts_hotlist_element *) srch->data);
	    srch = g_list_next(srch);
	}
    }
    hotlist_m_element_set_label(_hotlist_element, NULL);
    hotlist_m_element_set_directory(_hotlist_element, NULL);
    g_list_free(_hotlist_element->list);
    g_free(_hotlist_element);
}
/* -------------------------------------------------------------------------- */
void hotlist_m_element_set_label(ts_hotlist_element *_hotlist_element, char *_label){
    if (_hotlist_element == NULL ) return;
    if (_hotlist_element->label != NULL) {
	g_free(_hotlist_element->label);
	_hotlist_element->label = NULL;
    }
    if (_label == NULL) return;
    _hotlist_element->label = g_strdup (_label);
}
/* -------------------------------------------------------------------------- */
void hotlist_m_element_set_directory(ts_hotlist_element *_hotlist_element, char *_directory){
    if (_hotlist_element == NULL ) return;
    if (_hotlist_element->directory != NULL){
	g_free(_hotlist_element->directory);
	_hotlist_element->directory = NULL;
    }
    if (_directory == NULL) return;
    _hotlist_element->directory = g_strdup (_directory);
}
/* -------------------------------------------------------------------------- */
void hotlist_m_element_set_hotkey(ts_hotlist_element *_hotlist_element, int _hotkey){
    if (_hotlist_element == NULL ) return;
    _hotlist_element->hotkey = _hotkey;
}
/* -------------------------------------------------------------------------- */
void hotlist_m_element_set_type(ts_hotlist_element *_hotlist_element, enum  e_hotlist_type _type){
    if (_hotlist_element == NULL ) return;
    _hotlist_element->type = _type;
}
/* -------------------------------------------------------------------------- */
