/*
 * EveryBuddy 
 *
 * Copyright (C) 1999, Torrey Searle <tsearle@uci.edu>
 *
 * 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
 *
 */

#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "account.h"
#include "service.h"
#include "contact.h"
#include "globals.h"

void write_contact_list()
{
    FILE * fp;
    char buff2[1024];
    GList * l1;
    GList * l2;
    GList * l3;
	
#if defined( _WIN32 )
	g_snprintf( buff2, 1024, "C:\\.everybuddy\\contacts" );
#else
    g_snprintf(buff2, 1024, "%s/.everybuddy/contacts",getenv("HOME"));
#endif
    if(!(fp = fdopen(creat(buff2,0700),"w")))
        return;


    for(l1 = groups; l1; l1=l1->next )
    {
		fprintf(fp,"<GROUP>\n\tNAME=\"%s\"\n", ((grouplist*)l1->data)->name);
        for(l2 = ((grouplist*)l1->data)->members; l2; l2=l2->next )
        {
			struct contact * c = (struct contact*)l2->data;
			fprintf(fp, "\t<CONTACT>\n\t\tNAME=\"%s\"\n\t\tDEFAULT_PROTOCOL=\"%s\"\n", 
					c->nick, eb_services[c->default_chatb].name);

			fprintf(fp, "\t\tTRIGGER_TYPE=\"%s\"\n\t\tTRIGGER_ACTION=\"%s\"\n\t\tTRIGGER_PARAM=\"%s\"\n",
					get_trigger_type_text(c->trigger.type), 
					get_trigger_action_text(c->trigger.action),
					c->trigger.param);
					
            for(l3 = ((struct contact*)l2->data)->accounts; l3; l3=l3->next)
            {
                eb_account * account = (eb_account*)l3->data;
				fprintf( fp, "\t\t<ACCOUNT %s>\n\t\t\tNAME=\"%s\"\n\t\t</ACCOUNT>\n",
						 eb_services[account->service_id].name,
					 	 account->handle );	 

            }
			fprintf( fp, "\t</CONTACT>\n" );
        }
		fprintf( fp, "</GROUP>\n" );
    }

	fclose(fp);
}

int load_accounts()
{
    FILE * fp;
    char buff2[1024];
	extern int accountparse();
	extern FILE * accountin;
	
#if defined( _WIN32 )
	g_snprintf( buff2, 1024, "C:\\.everybuddy\\accounts" );
#else
    g_snprintf(buff2, 1024, "%s/.everybuddy/accounts",getenv("HOME"));
#endif
    if(!(fp = fopen(buff2,"r")))
        return 0;
    accounts = NULL;
	
	accountin = fp;
	accountparse();
#if 0
    while(fgets(buff, 255, fp))
    {
        int id;
        //buff[strlen(buff)-1] = '\0';
		g_strchomp(buff);
        id = get_service_id(buff);
        if(id >= 0 )
        {
            eb_local_account * ela = eb_services[id].sc->read_local_account_config(fp);
            accounts = g_list_append(accounts, ela);
            eb_services[id].sc->login(ela);
        }
    }
#endif
#ifdef DEBUG
    printf("closing fp\n");
#endif
    fclose(fp);
    return accounts != NULL;
}

#if 0
struct contact * load_contact(FILE * config, gchar * nick, grouplist *gl) 
{
	char buff[255];
	struct contact * person = g_new0(struct contact, 1);
	person->group = gl;
	person->chatwindow = NULL;
	person->infowindow = NULL;
	person->list_item = NULL;
	person->tree = NULL;
	person->pix = NULL;
	person->status = NULL;
	person->icon_handler = -1;
	person->status_handler = -1;
	person->online = 0;
	strncpy(person->nick, nick, 255);
	person->accounts = NULL;
	fgets(buff, 255, config);
    //buff[strlen(buff)-1] = '\0';
	g_strchomp(buff);
	person->default_chatb = get_service_id(buff);
	fgets(buff, 255, config);
    //buff[strlen(buff)-1] = '\0';
	g_strchomp(buff);
	person->default_filetransb = get_service_id(buff);
    
	fgets(buff, 255, config);
    //buff[strlen(buff)-1] = '\0';
	g_strchomp(buff);

	while(strcmp(buff, "END"))
    {
        int id;
        id = get_service_id(buff);
        if(id >= 0 )
        {
            eb_account * ea = eb_services[id].sc->read_account_config(config, person);
            person->accounts = g_list_append(person->accounts, ea);
#ifdef DEBUG
            printf("%s\n__",ea->handle);
#endif
        }
		fgets(buff, 255, config);
        //buff[strlen(buff)-1] = '\0';
		g_strchomp(buff);
    }
	return person;
}
#endif
		
int load_contacts()
{
    FILE * fp;
    char buff2[1024];
	extern int contactparse();
	extern FILE * contactin;
//	grouplist * gl;
#if defined( _WIN32 )
    g_snprintf(buff2, 1024, "C:\\.everybuddy\\contacts" );
#else
    g_snprintf(buff2, 1024, "%s/.everybuddy/contacts",getenv("HOME"));
#endif
    if(!(fp = fopen(buff2,"r")))
        return 0;
//	gl = NULL;
	contactin = fp;
	groups = NULL;

	contactparse();
#if 0
    while(fgets(buff, 255, fp)&&strlen(buff)>1)
    {
        //buff[strlen(buff)-1] = '\0';
		g_strchomp(buff);
		if( !strncmp( buff, "GROUP", strlen("GROUP") ) )
		{	
			gl = g_new(grouplist,1);
			strncpy(gl->name, buff+strlen("GROUP "), 255);
			gl->members = NULL;
			groups = g_list_append( groups, gl );
			
		}
			
			
		else if(gl)
		{
			struct contact * c = load_contact(fp, buff, gl);
#ifdef DEBUG
			printf("loading contact %s\n",buff);
#endif
			gl->members = g_list_append(gl->members,(gpointer)c);
		}
		
    }
#ifdef DEBUG
    printf("closing fp\n");
#endif

#endif
    fclose(fp);
    return 1;
}


