gmyth/src/gmyth_backendinfo.c
author rosfran
Tue Mar 20 21:50:24 2007 +0000 (2007-03-20)
branchtrunk
changeset 421 d04e08f9893a
parent 420 c1601c03cc78
child 446 d260ed30f4de
permissions -rw-r--r--
[svn r426] Put more DOxygen documentation.
     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 && path_parts[0] != NULL 
   162     				&& strlen( path_parts[0] ) > 0 ? g_strstrip( g_strdup( g_strdelimit( path_parts[0], "/?", ' ' ) ) ) 
   163     						: gmyth_uri_get_path (uri) );
   164     gmyth_backend_info_set_port ( backend_info, gmyth_uri_get_port (uri) );
   165     
   166     g_object_unref (uri);
   167     g_strfreev( path_parts );
   168 		
   169     return backend_info;
   170 }
   171 
   172 void
   173 gmyth_backend_info_set_hostname (GMythBackendInfo *backend_info, const gchar *hostname)
   174 {
   175     g_return_if_fail (backend_info != NULL);
   176     
   177     if ( NULL == hostname || strlen(hostname) <= 0 ) { 
   178         gmyth_debug ( "Error trying to set a hostname equals to NULL." );
   179     } else {    	
   180         backend_info->hostname = g_strdup (hostname);
   181     }
   182 }
   183 
   184 void
   185 gmyth_backend_info_set_username (GMythBackendInfo *backend_info, const gchar *username)
   186 {
   187     g_return_if_fail (backend_info != NULL);
   188 
   189     backend_info->username = g_strdup (username);
   190 }
   191 
   192 void
   193 gmyth_backend_info_set_password (GMythBackendInfo *backend_info, const gchar *password)
   194 {
   195     g_return_if_fail (backend_info != NULL);
   196 
   197     backend_info->password = g_strdup (password);
   198 }
   199 
   200 void
   201 gmyth_backend_info_set_db_name (GMythBackendInfo *backend_info, const gchar *db_name)
   202 {
   203     g_return_if_fail (backend_info != NULL);
   204 
   205     backend_info->db_name = g_strdup (db_name);
   206 }
   207 
   208 void
   209 gmyth_backend_info_set_port (GMythBackendInfo *backend_info, const gint port )
   210 {
   211     g_return_if_fail (backend_info != NULL);
   212 
   213     if ( port <= 0 ) { 
   214         gmyth_debug ( "Error trying to set a port less than 0." );
   215     } else {    	
   216         backend_info->port = port;
   217     }
   218 }
   219 
   220 void
   221 gmyth_backend_info_set_status_port (GMythBackendInfo *backend_info, const gint port )
   222 {
   223     g_return_if_fail (backend_info != NULL);
   224 
   225     if ( port <= 0 ) { 
   226         gmyth_debug ( "Error trying to set the status port to less than zero." );
   227     } else {    	
   228         backend_info->status_port = port;
   229     }
   230 }
   231 
   232 const gchar*
   233 gmyth_backend_info_get_hostname (GMythBackendInfo *backend_info)
   234 {
   235     g_return_val_if_fail (backend_info != NULL, NULL);
   236 
   237     return backend_info->hostname;
   238 }
   239 
   240 const gchar*
   241 gmyth_backend_info_get_username (GMythBackendInfo *backend_info)
   242 {
   243     g_return_val_if_fail (backend_info != NULL, NULL);
   244 
   245     return backend_info->username;
   246 }
   247 
   248 const gchar*
   249 gmyth_backend_info_get_password (GMythBackendInfo *backend_info)
   250 {
   251     g_return_val_if_fail (backend_info != NULL, NULL);
   252 
   253     return backend_info->password;
   254 }
   255 
   256 const gchar*
   257 gmyth_backend_info_get_db_name (GMythBackendInfo *backend_info)
   258 {
   259     g_return_val_if_fail (backend_info != NULL, NULL);
   260 
   261     return backend_info->db_name;
   262 }
   263 
   264 gint
   265 gmyth_backend_info_get_port (GMythBackendInfo *backend_info)
   266 {
   267     g_return_val_if_fail (backend_info != NULL, -1);
   268 
   269     return backend_info->port;
   270 }
   271 
   272 /** 
   273  * Creates a new instance of GMythURI, based on the GMythBackendInfo instance to the 
   274  * MythTV's backend server.
   275  * 
   276  * @param backend_info The GMythBackendInfo instance.
   277  * 
   278  * @return an instance of GMythURI, created from a GMythBackendInfo.
   279  */
   280 GMythURI*
   281 gmyth_backend_info_get_uri (GMythBackendInfo *backend_info)
   282 {
   283     GMythURI *uri = NULL;
   284     gchar* uri_str = g_strdup_printf( "myth://%s:%s@%s:%d/?%s", backend_info->username, backend_info->password,
   285                                            backend_info->hostname, backend_info->port, backend_info->db_name );
   286     uri = gmyth_uri_new_with_value (uri_str);
   287 
   288     g_free (uri_str);
   289     	
   290     return uri;	
   291 }
   292 
   293