/* 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 <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <glib/gstdio.h>

#include "global.h" /* home_dir */

#include "hotlist/private.h"
/* -------------------------------------------------------------------------- */
#define HOTLIST_FILENAME ".mc/hotlist"
#define FILEREAD_BUFFER_SIZE 1023
#define END_OF_GROUP 123456

int hotlist_m_process_one_string_from_file (ts_hotlist_element *, FILE *);
int hotlist_m_add_entry_from_string(ts_hotlist_element *, FILE*, char *);
int hotlist_m_add_group_from_string(ts_hotlist_element *, FILE*, char *);
char *hotlist_m_get_param_from_string(char *, const char *);

/* ************************************************************************** */
int hotlist_m_is_needed_load(ts_hotlist *_hotlist, char *_filename){
    struct stat stat_buf;
    char *filename;


    if (_hotlist == NULL ) return TRUE;
    if (! _hotlist->state.loaded) return TRUE;

    filename = hotlist_m_get_file_name(_filename);
    stat (filename, &stat_buf);
    g_free(filename);

    if (_hotlist->state.mtime < stat_buf.st_mtime) return TRUE;
    return FALSE;
}
/* -------------------------------------------------------------------------- */
void hotlist_m_free(ts_hotlist **_hotlist){
    if ((_hotlist == NULL) || (*_hotlist == NULL)) return;
    hotlist_m_element_free((*_hotlist)->root);
    g_free(*_hotlist);
    *_hotlist = NULL;
}
/* -------------------------------------------------------------------------- */
char *hotlist_m_get_file_name(char *_filename){
    if (_filename == NULL) {
	return concat_dir_and_file (home_dir, HOTLIST_FILENAME);
    } else {
	return g_strdup(_filename);
    }
}
/* -------------------------------------------------------------------------- */
int hotlist_m_init(ts_hotlist **_hotlist) {
    ts_hotlist_element *hle;

    *_hotlist = g_new0(ts_hotlist, 1);
    if (*_hotlist == NULL) return FALSE;

    (*_hotlist)->root = hotlist_m_element_create(NULL);

    if ( (*_hotlist)->root == NULL ){
	g_free(*_hotlist);
	return FALSE;
    }
    (*_hotlist)->current = (*_hotlist)->root;

    hle = (*_hotlist)->root;
    hotlist_m_element_set_label(hle, _("Top level group"));
    hotlist_m_element_set_directory(hle, "Hotlist");
    hotlist_m_element_set_type(hle, HL_TYPE_GROUP);

    return TRUE;
}
/* -------------------------------------------------------------------------- */
int hotlist_m_load_from_file(ts_hotlist *_hotlist, char *_filename){
    char *filename;
    FILE *hotlist_file;

    filename = hotlist_m_get_file_name(_filename);
    hotlist_file = fopen (filename, "r");
    g_free(filename);

    if (hotlist_file == NULL) return FALSE;

    while(!feof(hotlist_file)){
	if (hotlist_m_process_one_string_from_file (_hotlist->root, hotlist_file) == FALSE){
	    return FALSE;
	}
    }

    fclose(hotlist_file);
    return TRUE;
}
/* -------------------------------------------------------------------------- */
int hotlist_m_process_one_string_from_file (ts_hotlist_element *_hotlist_element, FILE *_hotlist_file){
    char read_buffer[FILEREAD_BUFFER_SIZE+1];
    read_buffer[FILEREAD_BUFFER_SIZE] = '\0';

    if (fgets(read_buffer,FILEREAD_BUFFER_SIZE,_hotlist_file) == NULL){
	return FALSE;
    }
// BUG: if GROUP named "ENTRY"
    if (g_strstr_len(read_buffer,FILEREAD_BUFFER_SIZE,"ENTRY") != NULL){
	return hotlist_m_add_entry_from_string(_hotlist_element, _hotlist_file, read_buffer);
    }

    if (g_strstr_len(read_buffer,FILEREAD_BUFFER_SIZE,"ENDGROUP") != NULL){
	return END_OF_GROUP;
    }

    if (g_strstr_len(read_buffer,FILEREAD_BUFFER_SIZE,"GROUP") != NULL){
	return hotlist_m_add_group_from_string(_hotlist_element, _hotlist_file, read_buffer);
    }
    
    return TRUE;
}
/* -------------------------------------------------------------------------- */
int hotlist_m_add_entry_from_string(ts_hotlist_element *_hotlist_element, FILE *_hotlist_file, char *_nf_read_buffer){
    ts_hotlist_element *hle;
    char *chotkey;

    hle = hotlist_m_element_create(_hotlist_element);

    hotlist_m_element_set_type		(hle,HL_TYPE_ENTRY);
    hotlist_m_element_set_label		(hle, hotlist_m_get_param_from_string(_nf_read_buffer, "ENTRY"));
    hotlist_m_element_set_directory	(hle, hotlist_m_get_param_from_string(_nf_read_buffer, "URL"));


    chotkey = hotlist_m_get_param_from_string(_nf_read_buffer, "HOTKEY");
    hotlist_m_element_set_hotkey(hle, atoi(chotkey));
    g_free(chotkey);

    return hotlist_m_element_append_child(_hotlist_element, hle);
}
/* -------------------------------------------------------------------------- */
int hotlist_m_add_group_from_string(ts_hotlist_element *_hotlist_element, FILE *_hotlist_file, char *_nf_read_buffer){
    ts_hotlist_element *hle;
    char *chotkey;
    int ret;
    
    hle = hotlist_m_element_create(_hotlist_element);
    hotlist_m_element_set_type		(hle,HL_TYPE_GROUP);
    hotlist_m_element_set_label		(hle, hotlist_m_get_param_from_string(_nf_read_buffer, "GROUP"));

    chotkey = hotlist_m_get_param_from_string(_nf_read_buffer, "HOTKEY");
    hotlist_m_element_set_hotkey(hle, atoi(chotkey));
    g_free(chotkey);

    if (hotlist_m_element_append_child(_hotlist_element, hle) == FALSE) return FALSE;
    while(!feof(_hotlist_file)){
	ret = hotlist_m_process_one_string_from_file (hle, _hotlist_file);
	if (ret == FALSE) return FALSE;
	if (ret == END_OF_GROUP) return TRUE;
    }
    return TRUE;
}
/* -------------------------------------------------------------------------- */
char *hotlist_m_get_param_from_string(char *_nf_read_buffer, const char *_param_name){
    char *param_value;
    char *start_pos, *end_pos, prev_char;

    start_pos = g_strrstr(_nf_read_buffer,_param_name);
    if (start_pos == NULL) return g_strdup("");

    start_pos+=strlen(_param_name)+1;
    while ((*start_pos == ' ') || (*start_pos == '\t')) start_pos++;

    if ((*start_pos == '"')) start_pos++;
    end_pos = start_pos+1;
    prev_char = *start_pos;

    while (1) {
	if ( (*end_pos == '\0') || (*end_pos == '\n') ) break;
	if ( (*end_pos == '"') && (prev_char != '\\') ) break;
	prev_char = *end_pos;
	end_pos++;
    }
    param_value = g_malloc(end_pos - start_pos + 1);
    if (param_value == NULL) return g_strdup("");
    strncpy(param_value, start_pos, end_pos - start_pos);
    param_value[end_pos - start_pos] = '\0';
    return param_value;
}
/* -------------------------------------------------------------------------- */
