gmyth/src/libgmyth/gmyth_settings.c
branchtrunk
changeset 1 ffdf467315ec
child 6 051875c9e136
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gmyth/src/libgmyth/gmyth_settings.c	Wed Sep 20 23:45:35 2006 +0100
     1.3 @@ -0,0 +1,417 @@
     1.4 +/**
     1.5 + * GMyth Library
     1.6 + *
     1.7 + * @file gmyth/gmyth_settings.c
     1.8 + * 
     1.9 + * @brief <p> This component contains functions acessing and modifying 
    1.10 + * user and program settings.
    1.11 + * 
    1.12 + * The standard settings file is created in the user home folder, in the
    1.13 + * file ~/.mmyth/settings.dat.  
    1.14 + *
    1.15 + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
    1.16 + * @author Alexsandro Jose Virginio dos Santos <alexsandro.santos@indt.org.br>
    1.17 + *
    1.18 + *//*
    1.19 + * 
    1.20 + * This program is free software; you can redistribute it and/or modify
    1.21 + * it under the terms of the GNU Lesser General Public License as published by
    1.22 + * the Free Software Foundation; either version 2 of the License, or
    1.23 + * (at your option) any later version.
    1.24 + *
    1.25 + * This program is distributed in the hope that it will be useful,
    1.26 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.27 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.28 + * GNU General Public License for more details.
    1.29 + *
    1.30 + * You should have received a copy of the GNU Lesser General Public License
    1.31 + * along with this program; if not, write to the Free Software
    1.32 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.33 + */
    1.34 +
    1.35 +#include "gmyth_settings.h"
    1.36 + 
    1.37 +#include <string.h>
    1.38 +#include <stdlib.h>
    1.39 +#include <unistd.h>
    1.40 +#include <fcntl.h>
    1.41 +
    1.42 +#include <glib.h>
    1.43 +#include <glib/gprintf.h>
    1.44 +#include <glib/gstdio.h>
    1.45 +
    1.46 +#define GMYTH_SETTINGS_FILE_NAME	"settings.dat"
    1.47 +#define GMYTH_SETTINGS_DIR		".mmyth"
    1.48 +
    1.49 +static void gmyth_settings_class_init          (GMythSettingsClass *klass);
    1.50 +static void gmyth_settings_init                (GMythSettings *object);
    1.51 +
    1.52 +static void gmyth_settings_dispose  (GObject *object);
    1.53 +static void gmyth_settings_finalize (GObject *object);
    1.54 +
    1.55 +static void fill_settings_data(GMythSettings *gmyth_settings, char* line);
    1.56 +
    1.57 +G_DEFINE_TYPE(GMythSettings, gmyth_settings, G_TYPE_OBJECT)
    1.58 +
    1.59 +static void
    1.60 +gmyth_settings_class_init (GMythSettingsClass *klass)
    1.61 +{
    1.62 +	GObjectClass *gobject_class;
    1.63 +
    1.64 +    gobject_class = (GObjectClass *) klass;
    1.65 +
    1.66 +    gobject_class->dispose  = gmyth_settings_dispose;
    1.67 +    gobject_class->finalize = gmyth_settings_finalize;	
    1.68 +}
    1.69 +
    1.70 +static void
    1.71 +gmyth_settings_init (GMythSettings *gmyth_settings)
    1.72 +{
    1.73 +
    1.74 +	gmyth_settings->settings_file = NULL;
    1.75 +	
    1.76 +    gmyth_settings->backend_hostname = g_string_new("127.0.0.1");
    1.77 +    gmyth_settings->backend_port = 6543;
    1.78 +    gmyth_settings->mysql_dbname = g_string_new("mythconverg");
    1.79 +	gmyth_settings->mysql_username = g_string_new("mythtv");
    1.80 +    gmyth_settings->mysql_password = g_string_new("mythtv");
    1.81 +    gmyth_settings->database_type = g_string_new("mysql");
    1.82 +    	
    1.83 +}
    1.84 +
    1.85 +
    1.86 +static void
    1.87 +gmyth_settings_dispose  (GObject *object)
    1.88 +{
    1.89 +	GMythSettings *gmyth_settings = GMYTH_SETTINGS(object);
    1.90 +
    1.91 +	if ( gmyth_settings->backend_hostname != NULL )
    1.92 +		g_string_free( gmyth_settings->backend_hostname, TRUE );
    1.93 +	if ( gmyth_settings->mysql_dbname != NULL )
    1.94 +		g_string_free( gmyth_settings->mysql_dbname, TRUE );
    1.95 +	if ( gmyth_settings->mysql_username != NULL )
    1.96 +		g_string_free( gmyth_settings->mysql_username, TRUE );
    1.97 +	if ( gmyth_settings->mysql_password != NULL )
    1.98 +		g_string_free( gmyth_settings->mysql_password, TRUE );
    1.99 +	if ( gmyth_settings->database_type != NULL )
   1.100 +		g_string_free( gmyth_settings->database_type, TRUE );
   1.101 +
   1.102 +	G_OBJECT_CLASS (gmyth_settings_parent_class)->dispose (object);
   1.103 +}
   1.104 +
   1.105 +static void
   1.106 +gmyth_settings_finalize (GObject *object)
   1.107 +{
   1.108 +	g_signal_handlers_destroy (object);
   1.109 +
   1.110 +    G_OBJECT_CLASS (gmyth_settings_parent_class)->finalize (object);
   1.111 +}
   1.112 +
   1.113 +/** Creates a new instance of GMythSettings.
   1.114 + * 
   1.115 + * @return a new instance of GMythSettings.
   1.116 + */
   1.117 +GMythSettings*
   1.118 +gmyth_settings_new ()
   1.119 +{
   1.120 +    GMythSettings *gmyth_settings = GMYTH_SETTINGS (g_object_new(GMYTH_SETTINGS_TYPE, NULL));
   1.121 +
   1.122 +    return gmyth_settings;
   1.123 +}
   1.124 +
   1.125 +static gboolean
   1.126 +gmyth_settings_ensure_dir_exists (const gchar *dir)
   1.127 +{
   1.128 +
   1.129 +	g_debug ("[%s] \tdir = %s\n", __FUNCTION__, dir);
   1.130 +
   1.131 +    if (g_file_test (dir, G_FILE_TEST_IS_DIR) == FALSE) {
   1.132 +	    if (g_file_test (dir, G_FILE_TEST_EXISTS) == TRUE) {
   1.133 +            g_warning ("%s exists, please move it out of the way.", dir);
   1.134 +            return FALSE;
   1.135 +	    }
   1.136 +
   1.137 +        if (mkdir (dir, 488) != 0) {
   1.138 +            g_warning ("Failed to create directory %s.", dir);
   1.139 +            return FALSE;
   1.140 +        }
   1.141 +    }
   1.142 +
   1.143 +    return TRUE;
   1.144 +}
   1.145 +
   1.146 +static gboolean
   1.147 +gmyth_settings_ensure_file_exists (const gchar *file_name) {
   1.148 +
   1.149 +	int file = 0;
   1.150 +	
   1.151 +    if ( g_file_test( file_name, G_FILE_TEST_EXISTS ) == FALSE ) {
   1.152 +        g_debug ( "\n\tCreating %s file...\n", file_name );
   1.153 +        file = creat( file_name, S_IRWXU | S_IRWXO | S_IRWXG );
   1.154 +
   1.155 +		if ( close( file ) == -1 )
   1.156 +			return FALSE;
   1.157 +    }
   1.158 +
   1.159 +	return TRUE;
   1.160 +}
   1.161 +
   1.162 +/** Loads the GMyth settings from the given file.
   1.163 + * 
   1.164 + * @param gmyth_settings the GMythSettings instance.
   1.165 + * @param filename The desired file to be opened.
   1.166 + * @return TRUE if success, FALSE if error.
   1.167 + */
   1.168 +gboolean
   1.169 +gmyth_settings_load_from_file (GMythSettings *gmyth_settings, GString *filename)
   1.170 +{
   1.171 +    FILE *file_desc;
   1.172 +    char line[100];
   1.173 +	
   1.174 +	g_debug ("[%s] Loading GMyth settings file: %s", __FUNCTION__, filename->str);
   1.175 +	
   1.176 +    file_desc = fopen(filename->str, "r");
   1.177 +    if( file_desc == NULL) {
   1.178 +        g_warning ("[%s] Settings file %s could not be opened", __FUNCTION__, filename->str);
   1.179 +        return FALSE;
   1.180 +    }
   1.181 +	
   1.182 +	gmyth_settings->settings_file = g_string_new (filename->str);
   1.183 +	
   1.184 +    while(fgets(line, 100, file_desc)) {
   1.185 +        int i;
   1.186 +
   1.187 +        /* Removes the new line characters from the end of line */
   1.188 +        for ( i = strlen(line)-1; i >= 0; i--) {
   1.189 +            if ( !g_ascii_iscntrl (line[i]) ) {
   1.190 +                line[i+1] = '\0';
   1.191 +                break;
   1.192 +            }
   1.193 +        }
   1.194 +        
   1.195 +        fill_settings_data(gmyth_settings, line);
   1.196 +    }
   1.197 +
   1.198 +    fclose (file_desc);
   1.199 +
   1.200 +    return TRUE;
   1.201 +}
   1.202 +
   1.203 +/** Loads the GMyth settings from the standard settings file
   1.204 + * (~/.mmyth/settings.dat).
   1.205 + * 
   1.206 + * @param gmyth_settings the GMythSettings instance.
   1.207 + * @return TRUE if success, FALSE if error.
   1.208 + */
   1.209 +gboolean
   1.210 +gmyth_settings_load (GMythSettings *gmyth_settings)
   1.211 +{
   1.212 +	GString* file_full_path = g_string_new( g_build_filename( g_get_home_dir(), 
   1.213 +				GMYTH_SETTINGS_DIR, GMYTH_SETTINGS_FILE_NAME, NULL ) );
   1.214 +
   1.215 +	gmyth_settings->settings_file = file_full_path;
   1.216 +		
   1.217 +	gmyth_settings_ensure_dir_exists( g_build_filename( g_get_home_dir(), GMYTH_SETTINGS_DIR, NULL ) );
   1.218 +
   1.219 +	// Verifies if the file already exist
   1.220 +	if ( g_file_test( file_full_path->str, G_FILE_TEST_EXISTS ) == FALSE ) {
   1.221 +		g_debug ("[%s] Settings file does not exist. A new one will be created.\n", __FUNCTION__);
   1.222 +
   1.223 +		if ( gmyth_settings_ensure_file_exists( file_full_path->str ) == FALSE )
   1.224 +			return FALSE;
   1.225 +
   1.226 +		// Creates the file with default values (see _init function)
   1.227 +		return gmyth_settings_save (gmyth_settings);
   1.228 +		
   1.229 +	} else {
   1.230 +		g_debug ("[%s] Opening settings from file %s", __FUNCTION__, file_full_path->str);
   1.231 +		
   1.232 +		return gmyth_settings_load_from_file (gmyth_settings, file_full_path);
   1.233 +	}	
   1.234 +}	
   1.235 +
   1.236 +/** Saves the current gmyth_settings to the settings file related to the
   1.237 + * given instance.
   1.238 + * 
   1.239 + * @param gmyth_settings the GMythSettings instance.
   1.240 + * @return TRUE if success, FALSE if error.
   1.241 + */
   1.242 +gboolean
   1.243 +gmyth_settings_save (GMythSettings *gmyth_settings)
   1.244 +{
   1.245 +    FILE *file_desc;
   1.246 +	
   1.247 +	if (gmyth_settings->settings_file == NULL) {
   1.248 +		g_warning ("[%s] Settings were not loaded from file, could not save!", __FUNCTION__);
   1.249 +	}
   1.250 +	
   1.251 +    file_desc = fopen(gmyth_settings->settings_file->str, "w");
   1.252 +    if( file_desc == NULL) {
   1.253 +        g_warning ("GMYTH_SETTINGS: settings file %s not found", gmyth_settings->settings_file->str);
   1.254 +        return FALSE;
   1.255 +    }
   1.256 +    
   1.257 +	g_fprintf(file_desc, "#Maemo-Myth Settings\n");
   1.258 +
   1.259 +	g_fprintf(file_desc, "#General settings related with mythtv database\n");
   1.260 +	g_fprintf(file_desc, "dbname=%s\n", gmyth_settings->mysql_dbname->str);
   1.261 +	g_fprintf(file_desc, "username=%s\n", gmyth_settings->mysql_username->str);
   1.262 +	g_fprintf(file_desc, "password=%s\n", gmyth_settings->mysql_password->str);
   1.263 +
   1.264 +	g_fprintf(file_desc, "\n#General settings related with mythtv backend\n");
   1.265 +	g_fprintf(file_desc, "hostname=%s\n", gmyth_settings->backend_hostname->str);
   1.266 +	g_fprintf(file_desc, "backend_port=%d\n", gmyth_settings->backend_port);
   1.267 +
   1.268 +    fclose (file_desc);
   1.269 +
   1.270 +	g_debug ("[%s] Settings file saved", __FUNCTION__);
   1.271 +    return TRUE;
   1.272 +}
   1.273 +
   1.274 +/** Gets the backend hostname from the settings.
   1.275 + * 
   1.276 + * @param gmyth_settings the GMythSettings instance.
   1.277 + * @return The loaded backend hostname, or the default value "127.0.0.1" if settings
   1.278 + * file was not opened.
   1.279 + */
   1.280 +GString*
   1.281 +gmyth_settings_get_backend_hostname (GMythSettings *gmyth_settings)
   1.282 +{
   1.283 +	return g_string_new (gmyth_settings->backend_hostname->str);
   1.284 +}
   1.285 +
   1.286 +/** Sets the backend hostname to the settings.
   1.287 + * 
   1.288 + * @param gmyth_settings the GMythSettings instance.
   1.289 + * @param new_hostname The new hostname.
   1.290 + */
   1.291 +void
   1.292 +gmyth_settings_set_backend_hostname (GMythSettings *gmyth_settings, GString *new_hostname)
   1.293 +{
   1.294 +	g_string_assign (gmyth_settings->backend_hostname, new_hostname->str);
   1.295 +}
   1.296 +
   1.297 +/** Gets the user name to connect to backend database.
   1.298 + * 
   1.299 + * @param gmyth_settings the GMythSettings instance.
   1.300 + * @return The loaded user name, or the default value "mythtv" if settings
   1.301 + * file was not opened.
   1.302 + */
   1.303 +GString*
   1.304 +gmyth_settings_get_username (GMythSettings *gmyth_settings)
   1.305 +{
   1.306 +	return g_string_new (gmyth_settings->mysql_username->str);
   1.307 +}
   1.308 +
   1.309 +/** Sets the username to connect to backend database.
   1.310 + * 
   1.311 + * @param gmyth_settings the GMythSettings instance.
   1.312 + * @param new_username The new username.
   1.313 + */
   1.314 +void
   1.315 +gmyth_settings_set_username (GMythSettings *gmyth_settings, GString *new_username)
   1.316 +{
   1.317 +	g_string_assign (gmyth_settings->mysql_username, new_username->str);
   1.318 +}
   1.319 +
   1.320 +/** Gets the database password from the settings file.
   1.321 + * 
   1.322 + * @param gmyth_settings the GMythSettings instance.
   1.323 + * @return The loaded password, or the default value "mythtv" if settings
   1.324 + * file was not opened.
   1.325 + */
   1.326 +GString*
   1.327 +gmyth_settings_get_password (GMythSettings *gmyth_settings)
   1.328 +{
   1.329 +	return g_string_new (gmyth_settings->mysql_password->str);	
   1.330 +}
   1.331 +
   1.332 +/** Sets the database password.
   1.333 + * 
   1.334 + * @param gmyth_settings the GMythSettings instance.
   1.335 + * @param new_password The new password.
   1.336 + */
   1.337 +
   1.338 +void
   1.339 +gmyth_settings_set_password (GMythSettings *gmyth_settings, GString *new_password)
   1.340 +{
   1.341 +	g_string_assign (gmyth_settings->mysql_password, new_password->str);
   1.342 +}
   1.343 +
   1.344 +/** Gets the backend database name from the settings file.
   1.345 + * 
   1.346 + * @param gmyth_settings the GMythSettings instance.
   1.347 + * @return The loaded database name, or the default value "mythconverg" if settings
   1.348 + * file was not opened.
   1.349 + */
   1.350 +GString*
   1.351 +gmyth_settings_get_dbname (GMythSettings *gmyth_settings)
   1.352 +{
   1.353 +	return g_string_new (gmyth_settings->mysql_dbname->str);
   1.354 +}
   1.355 +
   1.356 +/** Sets the Mythtv database name to the settings.
   1.357 + * 
   1.358 + * @param gmyth_settings the GMythSettings instance.
   1.359 + * @param new_dbname The new database name.
   1.360 + */
   1.361 +
   1.362 +void
   1.363 +gmyth_settings_set_dbname (GMythSettings *gmyth_settings, GString *new_dbname)
   1.364 +{
   1.365 +	g_string_assign (gmyth_settings->mysql_dbname, new_dbname->str);
   1.366 +}
   1.367 +
   1.368 +/** Gets the backend port from the settings.
   1.369 + * 
   1.370 + * @param gmyth_settings the GMythSettings instance.
   1.371 + * @return The loaded backend port, or the default value "6543" if settings
   1.372 + * file was not opened.
   1.373 + */
   1.374 +int
   1.375 +gmyth_settings_get_backend_port (GMythSettings *gmyth_settings)
   1.376 +{
   1.377 +	return gmyth_settings->backend_port;
   1.378 +}
   1.379 +
   1.380 +/** Sets the backend port.
   1.381 + * 
   1.382 + * @param gmyth_settings the GMythSettings instance.
   1.383 + * @param new_port The new port.
   1.384 + */
   1.385 +void
   1.386 +gmyth_settings_set_backend_port (GMythSettings *gmyth_settings, gint new_port)
   1.387 +{
   1.388 +	gmyth_settings->backend_port = new_port;
   1.389 +}
   1.390 +
   1.391 +
   1.392 +static void
   1.393 +fill_settings_data(GMythSettings *gmyth_settings, char* line)
   1.394 +{
   1.395 +    gchar** str_splited;
   1.396 +
   1.397 +	GString *gstr_splited;
   1.398 +	gstr_splited = g_string_new("");
   1.399 +	str_splited = g_strsplit (line, "=", -1);
   1.400 +
   1.401 +	if(!(strcmp(str_splited[0], "hostname"))){
   1.402 +		g_string_assign(gstr_splited, str_splited[1]);
   1.403 +		gmyth_settings_set_backend_hostname(gmyth_settings, gstr_splited);
   1.404 +	}
   1.405 +	else if (!(strcmp(str_splited[0], "dbname"))){
   1.406 +		g_string_assign(gstr_splited, str_splited[1]);
   1.407 +		gmyth_settings_set_dbname(gmyth_settings, gstr_splited);	
   1.408 +	}
   1.409 +	else if (!(strcmp(str_splited[0], "username"))){
   1.410 +		g_string_assign(gstr_splited, str_splited[1]);
   1.411 +		gmyth_settings_set_username(gmyth_settings, gstr_splited);
   1.412 +	}	
   1.413 +	else if (!(strcmp(str_splited[0], "password"))){
   1.414 +		g_string_assign(gstr_splited, str_splited[1]);
   1.415 +		gmyth_settings_set_password(gmyth_settings, gstr_splited);
   1.416 +	}
   1.417 +	else if (!(strcmp(str_splited[0], "backend_port"))){
   1.418 +		gmyth_settings_set_backend_port(gmyth_settings, atoi(str_splited[1]));
   1.419 +	}		
   1.420 +}