branches/gmyth-0.1b/src/gmyth_backendinfo.c
author rosfran
Tue Feb 06 00:33:35 2007 +0000 (2007-02-06)
branchtrunk
changeset 326 81b1f3006eb2
permissions -rw-r--r--
[svn r328] Some fixes on program info, and some memory clean ups.
     1 
     2 /**
     3  * GMyth Library
     4  *
     5  * @file gmyth/gmyth_backend_info.c
     6  * 
     7  * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
     8  * @author Hallyson Melo <hallyson.melo@indt.org.br>
     9  *
    10  *//*
    11  * 
    12  * This program is free software; you can redistribute it and/or modify
    13  * it under the terms of the GNU Lesser General Public License as published by
    14  * the Free Software Foundation; either version 2 of the License, or
    15  * (at your option) any later version.
    16  *
    17  * This program is distributed in the hope that it will be useful,
    18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    20  * GNU General Public License for more details.
    21  *
    22  * You should have received a copy of the GNU Lesser General Public License
    23  * along with this program; if not, write to the Free Software
    24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    25  */
    26  
    27 #ifdef HAVE_CONFIG_H
    28 #include "config.h"
    29 #endif
    30 
    31 #include "gmyth_backendinfo.h"
    32 #include "gmyth_uri.h"
    33 #include "gmyth_debug.h"
    34 
    35 static void gmyth_backend_info_class_init  (GMythBackendInfoClass *klass);
    36 static void gmyth_backend_info_init        (GMythBackendInfo *object);
    37 
    38 static void gmyth_backend_info_dispose  (GObject *object);
    39 static void gmyth_backend_info_finalize (GObject *object);
    40 
    41 G_DEFINE_TYPE(GMythBackendInfo, gmyth_backend_info, G_TYPE_OBJECT)
    42     
    43 static void
    44 gmyth_backend_info_class_init (GMythBackendInfoClass *klass)
    45 {
    46     GObjectClass *gobject_class;
    47 
    48     gobject_class = (GObjectClass *) klass;
    49 
    50     gobject_class->dispose  = gmyth_backend_info_dispose;
    51     gobject_class->finalize = gmyth_backend_info_finalize;	
    52 }
    53 
    54 static void
    55 gmyth_backend_info_init (GMythBackendInfo *backend_info)
    56 {
    57     backend_info->hostname = NULL;
    58     backend_info->username = NULL;
    59     backend_info->password = NULL;    
    60     backend_info->db_name  = NULL;
    61     backend_info->port = -1;
    62     backend_info->uri		   = NULL;
    63 }
    64 
    65 static void
    66 gmyth_backend_info_dispose  (GObject *object)
    67 {
    68     GMythBackendInfo *backend_info = GMYTH_BACKEND_INFO (object);
    69 
    70     g_free (backend_info->hostname);
    71     g_free (backend_info->username);
    72     g_free (backend_info->password);
    73     g_free (backend_info->db_name);
    74 
    75     backend_info->hostname = NULL;
    76     backend_info->username = NULL;
    77     backend_info->password = NULL;
    78     backend_info->db_name  = NULL;
    79     backend_info->port = -1;
    80 
    81     if ( backend_info->uri != NULL )
    82     {
    83     	g_object_unref(backend_info->uri);
    84     	backend_info->uri = NULL;
    85     }
    86 
    87     G_OBJECT_CLASS (gmyth_backend_info_parent_class)->dispose (object);
    88 }
    89 
    90 static void
    91 gmyth_backend_info_finalize (GObject *object)
    92 {
    93     g_signal_handlers_destroy (object);
    94 
    95     G_OBJECT_CLASS (gmyth_backend_info_parent_class)->finalize (object);
    96 }
    97 
    98 /** Creates a new instance of GMythBackendInfo.
    99  * 
   100  * @return a new instance of GMythBackendInfo.
   101  */
   102 GMythBackendInfo*
   103 gmyth_backend_info_new ()
   104 {
   105     GMythBackendInfo *backend_info = 
   106         GMYTH_BACKEND_INFO (g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
   107     
   108     return backend_info;
   109 }
   110 
   111 GMythBackendInfo*
   112 gmyth_backend_info_new_full (const gchar *hostname, const gchar *username,
   113 	const gchar *password, const gchar *db_name, gint port)
   114 {
   115     GMythBackendInfo *backend_info = 
   116         GMYTH_BACKEND_INFO (g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
   117     
   118     backend_info->uri = gmyth_uri_new_with_value( 
   119     				g_strdup_printf( "myth://%s:%s@%s:%d/?%s", username, password, hostname, port, db_name ) );
   120 
   121     gmyth_backend_info_set_hostname (backend_info, hostname);
   122     gmyth_backend_info_set_username (backend_info, username);
   123     gmyth_backend_info_set_password (backend_info, password);
   124     gmyth_backend_info_set_db_name (backend_info, db_name);
   125     gmyth_backend_info_set_port (backend_info, port);
   126 
   127     return backend_info;
   128 }
   129 
   130 GMythBackendInfo*
   131 gmyth_backend_info_new_with_uri ( const gchar *uri_str )
   132 {
   133     GMythBackendInfo *backend_info = 
   134         GMYTH_BACKEND_INFO (g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
   135         
   136     backend_info->uri = gmyth_uri_new_with_value( uri_str );
   137     
   138     gchar** path_parts = g_strsplit( gmyth_uri_get_path( backend_info->uri ), "&", -1 );
   139     
   140     gmyth_backend_info_set_hostname (backend_info, gmyth_uri_get_host ( backend_info->uri ) );
   141     gmyth_backend_info_set_username (backend_info, gmyth_uri_get_user( backend_info->uri ) );
   142     gmyth_backend_info_set_password (backend_info, gmyth_uri_get_password( backend_info->uri ) );
   143     /* gets the path info to database name, from the URI, and removes the trash chars */
   144     gmyth_backend_info_set_db_name (backend_info, path_parts != NULL && path_parts[0] != NULL 
   145     				&& strlen( path_parts[0] ) > 0 ? g_strstrip( g_strdup( g_strdelimit( path_parts[0], "/?", ' ' ) ) ) 
   146     						: gmyth_uri_get_path( backend_info->uri ) );
   147     gmyth_backend_info_set_port (backend_info, gmyth_uri_get_port( backend_info->uri ) );
   148     
   149     g_strfreev( path_parts );
   150 		
   151     return backend_info;
   152 }
   153 
   154 void
   155 gmyth_backend_info_set_hostname (GMythBackendInfo *backend_info, const gchar *hostname)
   156 {
   157     g_return_if_fail (backend_info != NULL);
   158     
   159     if ( NULL == hostname || strlen(hostname) <= 0 )
   160     { 
   161 	  	gmyth_debug ( "Error trying to set a hostname equals to NULL." );
   162     } else {    	
   163     	backend_info->hostname = g_strdup (hostname);
   164     }
   165 }
   166 
   167 void
   168 gmyth_backend_info_set_username (GMythBackendInfo *backend_info, const gchar *username)
   169 {
   170     g_return_if_fail (backend_info != NULL);
   171 
   172     backend_info->username = g_strdup (username);
   173 }
   174 
   175 void
   176 gmyth_backend_info_set_password (GMythBackendInfo *backend_info, const gchar *password)
   177 {
   178     g_return_if_fail (backend_info != NULL);
   179 
   180     backend_info->password = g_strdup (password);
   181 }
   182 
   183 void
   184 gmyth_backend_info_set_db_name (GMythBackendInfo *backend_info, const gchar *db_name)
   185 {
   186     g_return_if_fail (backend_info != NULL);
   187 
   188     backend_info->db_name = g_strdup (db_name);
   189 }
   190 
   191 void
   192 gmyth_backend_info_set_port (GMythBackendInfo *backend_info, const gint port )
   193 {
   194     g_return_if_fail (backend_info != NULL);
   195 
   196     if ( port <= 0 )
   197     { 
   198 	  	gmyth_debug ( "Error trying to set a hostname equals to NULL (it doesn't using UPnP)." );
   199     } else {    	
   200     	backend_info->port = port;
   201     }
   202 }
   203 
   204 const gchar*
   205 gmyth_backend_info_get_hostname (GMythBackendInfo *backend_info)
   206 {
   207     g_return_val_if_fail (backend_info != NULL, NULL);
   208 
   209     return backend_info->hostname;
   210 }
   211 
   212 const gchar*
   213 gmyth_backend_info_get_username (GMythBackendInfo *backend_info)
   214 {
   215     g_return_val_if_fail (backend_info != NULL, NULL);
   216 
   217     return backend_info->username;
   218 }
   219 
   220 const gchar*
   221 gmyth_backend_info_get_password (GMythBackendInfo *backend_info)
   222 {
   223     g_return_val_if_fail (backend_info != NULL, NULL);
   224 
   225     return backend_info->password;
   226 }
   227 
   228 const gchar*
   229 gmyth_backend_info_get_db_name (GMythBackendInfo *backend_info)
   230 {
   231     g_return_val_if_fail (backend_info != NULL, NULL);
   232 
   233     return backend_info->db_name;
   234 }
   235 
   236 gint
   237 gmyth_backend_info_get_port (GMythBackendInfo *backend_info)
   238 {
   239     g_return_val_if_fail (backend_info != NULL, -1);
   240 
   241     return backend_info->port;
   242 }
   243 
   244 const GMythURI*
   245 gmyth_backend_info_get_uri (GMythBackendInfo *backend_info)
   246 {
   247 	
   248 	if ( NULL == backend_info->uri )
   249 	{
   250 		backend_info->uri = gmyth_uri_new_with_value( 
   251 	    				g_strdup_printf( "myth://%s:%s@%s:%d/?%s", backend_info->username, backend_info->password, 
   252 	    				backend_info->hostname, backend_info->port, backend_info->db_name ) );
   253 	}
   254 	
   255 	return backend_info->uri;	
   256 }
   257 
   258