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