aboutsummaryrefslogblamecommitdiff
path: root/src/espik_config.c
blob: 5b00fb58c6a215b9e021b87897fc2d783a944cc8 (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                         
                        
 
                                                                           
 
                               
 
                                              
 

                              
 
                        

 
                                         
 

                                       
 
                                  
 
                                              
 


                                        
 
                        

 
                                   
 
                                     
 

                                                                             
 
                                                 
 
                                                             
                                                                
                                                             
                                                                
                                                             
                                                                
 
                                                                      
 
                            

 
                            
 




                                                                   
 
                             

 
                                         
 

                                                      
 
                                    
 

                                                                                 

                                                                                         
                                
                                                                            
                                
                                                                            
                                
                                                                            
 
                             

 
                                        
 



                                                                                 
 


                                                                                   
 
                             
 
#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>

#include <Ecore_Config.h>

#include "espik_config.h"
#include "espik_error.h"

t_servinfo*	espik_server_config_set (char* server, unsigned short port)
{
	t_servinfo*	s_info;

	s_info = malloc (sizeof (t_servinfo));

	s_info->host = server;
	s_info->port = port;

	return (s_info);
}

t_userinfo*	espik_user_sysinfo_get ()
{
	struct passwd*	pw;
	t_userinfo*		u_info;

	pw = getpwuid (getuid ());

	u_info = malloc (sizeof (t_userinfo));

	u_info->nickname = pw->pw_name;
	u_info->username = pw->pw_name;
	u_info->realname = pw->pw_gecos;

	return (u_info);
}

void	espik_config_default_set ()
{
	t_userinfo*	user_sysinfo;

	ecore_config_string_default ("/config/server/0/host", "beber.melee");
	ecore_config_int_default ("/config/server/0/port", 6667);

	user_sysinfo = espik_user_sysinfo_get ();

	ecore_config_string_default ("/config/user/nickname",
					user_sysinfo->nickname);
	ecore_config_string_default ("/config/user/username",
					user_sysinfo->username);
	ecore_config_string_default ("/config/user/realname",
					user_sysinfo->realname);

	ecore_config_string_default ("/espik/version", ESPIK_VERSION);

	free (user_sysinfo);
}

void	espik_config_init ()
{
	if (ecore_config_init (APPS_NAME) != ECORE_CONFIG_ERR_SUCC)
	{
		printf ("Cannot init Ecore_Config");
		exit (-1);
	}

	ecore_config_load ();
}

void	espik_config_get (t_info *s_info)
{
	s_info->server = malloc (sizeof (t_servinfo));
	s_info->client = malloc (sizeof (t_userinfo));

	espik_config_default_set ();

	s_info->server->host = ecore_config_string_get ("/config/server/0/host");
	s_info->server->port
			= (unsigned short)ecore_config_int_get ("/config/server/0/port");

	s_info->client->nickname
			= ecore_config_string_get ("/config/user/nickname");
	s_info->client->username
			= ecore_config_string_get ("/config/user/username");
	s_info->client->realname
			= ecore_config_string_get ("/config/user/realname");

	ecore_config_save ();
}

void	espik_config_set (t_info config)
{
	ecore_config_int_set ("/config/server/len", 1);
	ecore_config_string_set ("/config/server/0/host", config.server->host);
	ecore_config_int_set ("/config/server/0/port", (int)config.server->port);
	ecore_config_int_set ("/config/server/0/autoconnect", 1);

	ecore_config_string_set ("/config/user/nickname", config.client->nickname);
	ecore_config_string_set ("/config/user/username", config.client->username);
	ecore_config_string_set ("/config/user/realname", config.client->realname);

	ecore_config_save ();
}