branches/gmyth-0.1b/src/gmyth_backendinfo.c
branchtrunk
changeset 333 f9d778bb88a2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/branches/gmyth-0.1b/src/gmyth_backendinfo.c	Wed Feb 07 20:38:39 2007 +0000
     1.3 @@ -0,0 +1,258 @@
     1.4 +
     1.5 +/**
     1.6 + * GMyth Library
     1.7 + *
     1.8 + * @file gmyth/gmyth_backend_info.c
     1.9 + * 
    1.10 + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
    1.11 + * @author Hallyson Melo <hallyson.melo@indt.org.br>
    1.12 + *
    1.13 + *//*
    1.14 + * 
    1.15 + * This program is free software; you can redistribute it and/or modify
    1.16 + * it under the terms of the GNU Lesser General Public License as published by
    1.17 + * the Free Software Foundation; either version 2 of the License, or
    1.18 + * (at your option) any later version.
    1.19 + *
    1.20 + * This program is distributed in the hope that it will be useful,
    1.21 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.22 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.23 + * GNU General Public License for more details.
    1.24 + *
    1.25 + * You should have received a copy of the GNU Lesser General Public License
    1.26 + * along with this program; if not, write to the Free Software
    1.27 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.28 + */
    1.29 + 
    1.30 +#ifdef HAVE_CONFIG_H
    1.31 +#include "config.h"
    1.32 +#endif
    1.33 +
    1.34 +#include "gmyth_backendinfo.h"
    1.35 +#include "gmyth_uri.h"
    1.36 +#include "gmyth_debug.h"
    1.37 +
    1.38 +static void gmyth_backend_info_class_init  (GMythBackendInfoClass *klass);
    1.39 +static void gmyth_backend_info_init        (GMythBackendInfo *object);
    1.40 +
    1.41 +static void gmyth_backend_info_dispose  (GObject *object);
    1.42 +static void gmyth_backend_info_finalize (GObject *object);
    1.43 +
    1.44 +G_DEFINE_TYPE(GMythBackendInfo, gmyth_backend_info, G_TYPE_OBJECT)
    1.45 +    
    1.46 +static void
    1.47 +gmyth_backend_info_class_init (GMythBackendInfoClass *klass)
    1.48 +{
    1.49 +    GObjectClass *gobject_class;
    1.50 +
    1.51 +    gobject_class = (GObjectClass *) klass;
    1.52 +
    1.53 +    gobject_class->dispose  = gmyth_backend_info_dispose;
    1.54 +    gobject_class->finalize = gmyth_backend_info_finalize;	
    1.55 +}
    1.56 +
    1.57 +static void
    1.58 +gmyth_backend_info_init (GMythBackendInfo *backend_info)
    1.59 +{
    1.60 +    backend_info->hostname = NULL;
    1.61 +    backend_info->username = NULL;
    1.62 +    backend_info->password = NULL;    
    1.63 +    backend_info->db_name  = NULL;
    1.64 +    backend_info->port = -1;
    1.65 +    backend_info->uri		   = NULL;
    1.66 +}
    1.67 +
    1.68 +static void
    1.69 +gmyth_backend_info_dispose  (GObject *object)
    1.70 +{
    1.71 +    GMythBackendInfo *backend_info = GMYTH_BACKEND_INFO (object);
    1.72 +
    1.73 +    g_free (backend_info->hostname);
    1.74 +    g_free (backend_info->username);
    1.75 +    g_free (backend_info->password);
    1.76 +    g_free (backend_info->db_name);
    1.77 +
    1.78 +    backend_info->hostname = NULL;
    1.79 +    backend_info->username = NULL;
    1.80 +    backend_info->password = NULL;
    1.81 +    backend_info->db_name  = NULL;
    1.82 +    backend_info->port = -1;
    1.83 +
    1.84 +    if ( backend_info->uri != NULL )
    1.85 +    {
    1.86 +    	g_object_unref(backend_info->uri);
    1.87 +    	backend_info->uri = NULL;
    1.88 +    }
    1.89 +
    1.90 +    G_OBJECT_CLASS (gmyth_backend_info_parent_class)->dispose (object);
    1.91 +}
    1.92 +
    1.93 +static void
    1.94 +gmyth_backend_info_finalize (GObject *object)
    1.95 +{
    1.96 +    g_signal_handlers_destroy (object);
    1.97 +
    1.98 +    G_OBJECT_CLASS (gmyth_backend_info_parent_class)->finalize (object);
    1.99 +}
   1.100 +
   1.101 +/** Creates a new instance of GMythBackendInfo.
   1.102 + * 
   1.103 + * @return a new instance of GMythBackendInfo.
   1.104 + */
   1.105 +GMythBackendInfo*
   1.106 +gmyth_backend_info_new ()
   1.107 +{
   1.108 +    GMythBackendInfo *backend_info = 
   1.109 +        GMYTH_BACKEND_INFO (g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
   1.110 +    
   1.111 +    return backend_info;
   1.112 +}
   1.113 +
   1.114 +GMythBackendInfo*
   1.115 +gmyth_backend_info_new_full (const gchar *hostname, const gchar *username,
   1.116 +	const gchar *password, const gchar *db_name, gint port)
   1.117 +{
   1.118 +    GMythBackendInfo *backend_info = 
   1.119 +        GMYTH_BACKEND_INFO (g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
   1.120 +    
   1.121 +    backend_info->uri = gmyth_uri_new_with_value( 
   1.122 +    				g_strdup_printf( "myth://%s:%s@%s:%d/?%s", username, password, hostname, port, db_name ) );
   1.123 +
   1.124 +    gmyth_backend_info_set_hostname (backend_info, hostname);
   1.125 +    gmyth_backend_info_set_username (backend_info, username);
   1.126 +    gmyth_backend_info_set_password (backend_info, password);
   1.127 +    gmyth_backend_info_set_db_name (backend_info, db_name);
   1.128 +    gmyth_backend_info_set_port (backend_info, port);
   1.129 +
   1.130 +    return backend_info;
   1.131 +}
   1.132 +
   1.133 +GMythBackendInfo*
   1.134 +gmyth_backend_info_new_with_uri ( const gchar *uri_str )
   1.135 +{
   1.136 +    GMythBackendInfo *backend_info = 
   1.137 +        GMYTH_BACKEND_INFO (g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
   1.138 +        
   1.139 +    backend_info->uri = gmyth_uri_new_with_value( uri_str );
   1.140 +    
   1.141 +    gchar** path_parts = g_strsplit( gmyth_uri_get_path( backend_info->uri ), "&", -1 );
   1.142 +    
   1.143 +    gmyth_backend_info_set_hostname (backend_info, gmyth_uri_get_host ( backend_info->uri ) );
   1.144 +    gmyth_backend_info_set_username (backend_info, gmyth_uri_get_user( backend_info->uri ) );
   1.145 +    gmyth_backend_info_set_password (backend_info, gmyth_uri_get_password( backend_info->uri ) );
   1.146 +    /* gets the path info to database name, from the URI, and removes the trash chars */
   1.147 +    gmyth_backend_info_set_db_name (backend_info, path_parts != NULL && path_parts[0] != NULL 
   1.148 +    				&& strlen( path_parts[0] ) > 0 ? g_strstrip( g_strdup( g_strdelimit( path_parts[0], "/?", ' ' ) ) ) 
   1.149 +    						: gmyth_uri_get_path( backend_info->uri ) );
   1.150 +    gmyth_backend_info_set_port (backend_info, gmyth_uri_get_port( backend_info->uri ) );
   1.151 +    
   1.152 +    g_strfreev( path_parts );
   1.153 +		
   1.154 +    return backend_info;
   1.155 +}
   1.156 +
   1.157 +void
   1.158 +gmyth_backend_info_set_hostname (GMythBackendInfo *backend_info, const gchar *hostname)
   1.159 +{
   1.160 +    g_return_if_fail (backend_info != NULL);
   1.161 +    
   1.162 +    if ( NULL == hostname || strlen(hostname) <= 0 )
   1.163 +    { 
   1.164 +	  	gmyth_debug ( "Error trying to set a hostname equals to NULL." );
   1.165 +    } else {    	
   1.166 +    	backend_info->hostname = g_strdup (hostname);
   1.167 +    }
   1.168 +}
   1.169 +
   1.170 +void
   1.171 +gmyth_backend_info_set_username (GMythBackendInfo *backend_info, const gchar *username)
   1.172 +{
   1.173 +    g_return_if_fail (backend_info != NULL);
   1.174 +
   1.175 +    backend_info->username = g_strdup (username);
   1.176 +}
   1.177 +
   1.178 +void
   1.179 +gmyth_backend_info_set_password (GMythBackendInfo *backend_info, const gchar *password)
   1.180 +{
   1.181 +    g_return_if_fail (backend_info != NULL);
   1.182 +
   1.183 +    backend_info->password = g_strdup (password);
   1.184 +}
   1.185 +
   1.186 +void
   1.187 +gmyth_backend_info_set_db_name (GMythBackendInfo *backend_info, const gchar *db_name)
   1.188 +{
   1.189 +    g_return_if_fail (backend_info != NULL);
   1.190 +
   1.191 +    backend_info->db_name = g_strdup (db_name);
   1.192 +}
   1.193 +
   1.194 +void
   1.195 +gmyth_backend_info_set_port (GMythBackendInfo *backend_info, const gint port )
   1.196 +{
   1.197 +    g_return_if_fail (backend_info != NULL);
   1.198 +
   1.199 +    if ( port <= 0 )
   1.200 +    { 
   1.201 +	  	gmyth_debug ( "Error trying to set a hostname equals to NULL (it doesn't using UPnP)." );
   1.202 +    } else {    	
   1.203 +    	backend_info->port = port;
   1.204 +    }
   1.205 +}
   1.206 +
   1.207 +const gchar*
   1.208 +gmyth_backend_info_get_hostname (GMythBackendInfo *backend_info)
   1.209 +{
   1.210 +    g_return_val_if_fail (backend_info != NULL, NULL);
   1.211 +
   1.212 +    return backend_info->hostname;
   1.213 +}
   1.214 +
   1.215 +const gchar*
   1.216 +gmyth_backend_info_get_username (GMythBackendInfo *backend_info)
   1.217 +{
   1.218 +    g_return_val_if_fail (backend_info != NULL, NULL);
   1.219 +
   1.220 +    return backend_info->username;
   1.221 +}
   1.222 +
   1.223 +const gchar*
   1.224 +gmyth_backend_info_get_password (GMythBackendInfo *backend_info)
   1.225 +{
   1.226 +    g_return_val_if_fail (backend_info != NULL, NULL);
   1.227 +
   1.228 +    return backend_info->password;
   1.229 +}
   1.230 +
   1.231 +const gchar*
   1.232 +gmyth_backend_info_get_db_name (GMythBackendInfo *backend_info)
   1.233 +{
   1.234 +    g_return_val_if_fail (backend_info != NULL, NULL);
   1.235 +
   1.236 +    return backend_info->db_name;
   1.237 +}
   1.238 +
   1.239 +gint
   1.240 +gmyth_backend_info_get_port (GMythBackendInfo *backend_info)
   1.241 +{
   1.242 +    g_return_val_if_fail (backend_info != NULL, -1);
   1.243 +
   1.244 +    return backend_info->port;
   1.245 +}
   1.246 +
   1.247 +const GMythURI*
   1.248 +gmyth_backend_info_get_uri (GMythBackendInfo *backend_info)
   1.249 +{
   1.250 +	
   1.251 +	if ( NULL == backend_info->uri )
   1.252 +	{
   1.253 +		backend_info->uri = gmyth_uri_new_with_value( 
   1.254 +	    				g_strdup_printf( "myth://%s:%s@%s:%d/?%s", backend_info->username, backend_info->password, 
   1.255 +	    				backend_info->hostname, backend_info->port, backend_info->db_name ) );
   1.256 +	}
   1.257 +	
   1.258 +	return backend_info->uri;	
   1.259 +}
   1.260 +
   1.261 +