gmyth/src/gmyth_backendinfo.c
author renatofilho
Thu Jun 14 18:19:52 2007 +0100 (2007-06-14)
branchtrunk
changeset 750 312d6bc514f3
parent 701 2f28edb4d804
child 754 cb885ee44618
permissions -rw-r--r--
[svn r756] fixed indent using GNU Style
     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  * This program is free software; you can redistribute it and/or modify
    15  * it under the terms of the GNU Lesser General Public License as published by
    16  * the Free Software Foundation; either version 2 of the License, or
    17  * (at your option) any later version.
    18  *
    19  * This program is distributed in the hope that it will be useful,
    20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    22  * GNU General Public License for more details.
    23  *
    24  * You should have received a copy of the GNU Lesser General Public License
    25  * along with this program; if not, write to the Free Software
    26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    27  */
    28 
    29 #ifdef HAVE_CONFIG_H
    30 #include "config.h"
    31 #endif
    32 
    33 #include "gmyth_backendinfo.h"
    34 #include "gmyth_uri.h"
    35 #include "gmyth_debug.h"
    36 
    37 static void gmyth_backend_info_class_init(GMythBackendInfoClass * klass);
    38 static void gmyth_backend_info_init(GMythBackendInfo * object);
    39 
    40 static void gmyth_backend_info_dispose(GObject * object);
    41 static void gmyth_backend_info_finalize(GObject * object);
    42 
    43 G_DEFINE_TYPE(GMythBackendInfo, gmyth_backend_info, G_TYPE_OBJECT)
    44 	 static void 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->status_port = -1;
    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   backend_info->status_port = -1;
    81 
    82   G_OBJECT_CLASS(gmyth_backend_info_parent_class)->dispose(object);
    83 }
    84 
    85 static void
    86 gmyth_backend_info_finalize(GObject * object)
    87 {
    88   g_signal_handlers_destroy(object);
    89 
    90   G_OBJECT_CLASS(gmyth_backend_info_parent_class)->finalize(object);
    91 }
    92 
    93 /** 
    94  * Creates a new instance of GMythBackendInfo.
    95  * 
    96  * @return a new instance of GMythBackendInfo.
    97  */
    98 GMythBackendInfo *
    99 gmyth_backend_info_new()
   100 {
   101   GMythBackendInfo *backend_info =
   102 	GMYTH_BACKEND_INFO(g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
   103 
   104   return backend_info;
   105 }
   106 
   107 /** 
   108  * Creates a new instance of GMythBackendInfo, based on a given set of 
   109  * configuration parameters.
   110  * 
   111  * @param hostname The hostname to the MythTV backend server.
   112  * @param username The user name to the MythTV backend MySQL server.
   113  * @param password The password to the user of the MythTV backend MySQL server.
   114  * @param db_name The database name of the MythTV backend, stored on the MySQL server.
   115  * @param port The port number of the MythTV backend server (commonly is 6543).
   116  * 
   117  * @return a new instance of GMythBackendInfo.
   118  */
   119 GMythBackendInfo *
   120 gmyth_backend_info_new_full(const gchar * hostname, const gchar * username,
   121 							const gchar * password, const gchar * db_name,
   122 							gint port)
   123 {
   124   GMythBackendInfo *backend_info =
   125 	GMYTH_BACKEND_INFO(g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
   126 
   127   gmyth_backend_info_set_hostname(backend_info, hostname);
   128   gmyth_backend_info_set_username(backend_info, username);
   129   gmyth_backend_info_set_password(backend_info, password);
   130   gmyth_backend_info_set_db_name(backend_info, db_name);
   131   gmyth_backend_info_set_port(backend_info, port);
   132 
   133   return backend_info;
   134 }
   135 
   136 /** 
   137  * Creates a new instance of GMythBackendInfo, based on the 
   138  * MythTV's backend server URI string.
   139  * 
   140  * @param uri_str The URI string pointing to the MythTV backend server.
   141  * 
   142  * @return a new instance of GMythBackendInfo.
   143  */
   144 GMythBackendInfo *
   145 gmyth_backend_info_new_with_uri(const gchar * uri_str)
   146 {
   147   GMythBackendInfo *backend_info =
   148 	GMYTH_BACKEND_INFO(g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
   149 
   150   GMythURI *uri = gmyth_uri_new_with_value(uri_str);
   151 
   152   gchar **path_parts = g_strsplit(gmyth_uri_get_path(uri), "&", -1);
   153 
   154   gmyth_backend_info_set_hostname(backend_info, gmyth_uri_get_host(uri));
   155   gmyth_backend_info_set_username(backend_info, gmyth_uri_get_user(uri));
   156   gmyth_backend_info_set_password(backend_info, gmyth_uri_get_password(uri));
   157 
   158  /* gets the path info to database name, from the URI, and removes the trash chars */
   159   gmyth_backend_info_set_db_name(backend_info, path_parts != NULL &&
   160 								 strlen(path_parts[0]) > 0 ?
   161 								 g_strstrip(g_strdelimit
   162 											(path_parts[0], "/?",
   163 											 ' ')) : gmyth_uri_get_path(uri));
   164 
   165   gmyth_backend_info_set_port(backend_info, gmyth_uri_get_port(uri));
   166 
   167   g_object_unref(uri);
   168   g_strfreev(path_parts);
   169 
   170   return backend_info;
   171 }
   172 
   173 void
   174 gmyth_backend_info_set_hostname(GMythBackendInfo * backend_info,
   175 								const gchar * hostname)
   176 {
   177   g_return_if_fail(backend_info != NULL);
   178 
   179   if (NULL == hostname || strlen(hostname) <= 0)
   180 	{
   181 	  gmyth_debug("Error trying to set a hostname equals to NULL.");
   182 	}
   183   else
   184 	{
   185 	  backend_info->hostname = g_strdup(hostname);
   186 	}
   187 }
   188 
   189 void
   190 gmyth_backend_info_set_username(GMythBackendInfo * backend_info,
   191 								const gchar * username)
   192 {
   193   g_return_if_fail(backend_info != NULL);
   194 
   195   backend_info->username = g_strdup(username);
   196 }
   197 
   198 void
   199 gmyth_backend_info_set_password(GMythBackendInfo * backend_info,
   200 								const gchar * password)
   201 {
   202   g_return_if_fail(backend_info != NULL);
   203 
   204   backend_info->password = g_strdup(password);
   205 }
   206 
   207 void
   208 gmyth_backend_info_set_db_name(GMythBackendInfo * backend_info,
   209 							   const gchar * db_name)
   210 {
   211   g_return_if_fail(backend_info != NULL);
   212 
   213   backend_info->db_name = g_strdup(db_name);
   214 }
   215 
   216 void
   217 gmyth_backend_info_set_port(GMythBackendInfo * backend_info, gint port)
   218 {
   219   g_return_if_fail(backend_info != NULL);
   220 
   221   if (port <= 0)
   222 	{
   223 	  gmyth_debug("Error trying to set a port less than 0.");
   224 	}
   225   else
   226 	{
   227 	  backend_info->port = port;
   228 	}
   229 }
   230 
   231 void
   232 gmyth_backend_info_set_status_port(GMythBackendInfo * backend_info, gint port)
   233 {
   234   g_return_if_fail(backend_info != NULL);
   235 
   236   if (port <= 0)
   237 	{
   238 	  gmyth_debug("Error trying to set the status port to less than zero.");
   239 	}
   240   else
   241 	{
   242 	  backend_info->status_port = port;
   243 	}
   244 }
   245 
   246 const gchar *
   247 gmyth_backend_info_get_hostname(GMythBackendInfo * backend_info)
   248 {
   249   g_return_val_if_fail(backend_info != NULL, NULL);
   250 
   251   return backend_info->hostname;
   252 }
   253 
   254 const gchar *
   255 gmyth_backend_info_get_username(GMythBackendInfo * backend_info)
   256 {
   257   g_return_val_if_fail(backend_info != NULL, NULL);
   258 
   259   return backend_info->username;
   260 }
   261 
   262 const gchar *
   263 gmyth_backend_info_get_password(GMythBackendInfo * backend_info)
   264 {
   265   g_return_val_if_fail(backend_info != NULL, NULL);
   266 
   267   return backend_info->password;
   268 }
   269 
   270 const gchar *
   271 gmyth_backend_info_get_db_name(GMythBackendInfo * backend_info)
   272 {
   273   g_return_val_if_fail(backend_info != NULL, NULL);
   274 
   275   return backend_info->db_name;
   276 }
   277 
   278 gint
   279 gmyth_backend_info_get_port(GMythBackendInfo * backend_info)
   280 {
   281   g_return_val_if_fail(backend_info != NULL, -1);
   282 
   283   return backend_info->port;
   284 }
   285 
   286 /** 
   287  * Creates a new instance of GMythURI, based on the GMythBackendInfo instance to the 
   288  * MythTV's backend server.
   289  * 
   290  * @param backend_info The GMythBackendInfo instance.
   291  * 
   292  * @return an instance of GMythURI, created from a GMythBackendInfo.
   293  */
   294 GMythURI *
   295 gmyth_backend_info_get_uri(GMythBackendInfo * backend_info)
   296 {
   297   GMythURI *uri = NULL;
   298   gchar *uri_str = NULL;
   299   gchar *user_info = NULL;
   300   gchar *db_data = NULL;
   301 
   302   if ((backend_info->username != NULL && strlen(backend_info->username) > 0))
   303 	user_info =
   304 	  g_strdup_printf("%s:%s@", backend_info->username,
   305 					  backend_info->password);
   306 
   307   if (backend_info->db_name != NULL && strlen(backend_info->db_name) > 0)
   308 	{
   309 	  if ((g_strrstr(backend_info->db_name, "_") != NULL))
   310 		db_data = g_strdup(backend_info->db_name);
   311 	  else
   312 		db_data = g_strdup_printf("?%s&", backend_info->db_name);
   313 	}
   314  //else if ( ( ( g_strrstr( backend_info->path, "livetv" ) != NULL ) || 
   315  //            ( g_strrstr( backend_info->path, "/?" ) != NULL ) )
   316 
   317   uri_str = g_strdup_printf("myth://%s%s:%d/%s", user_info != NULL
   318 							&& strlen(user_info) > 0 ? user_info : "",
   319 							backend_info->hostname, backend_info->port,
   320 							db_data != NULL
   321 							&& strlen(db_data) > 0 ? db_data : "");
   322   uri = gmyth_uri_new_with_value(uri_str);
   323 
   324   if (user_info != NULL)
   325 	g_free(user_info);
   326 
   327   if (db_data != NULL)
   328 	g_free(db_data);
   329 
   330   g_free(uri_str);
   331 
   332   return uri;
   333 }
   334 
   335 gboolean
   336 gmyth_backend_info_is_local_file(GMythBackendInfo * backend_info)
   337 {
   338   g_return_val_if_fail(backend_info != NULL, FALSE);
   339 
   340   return gmyth_uri_is_local_file(gmyth_backend_info_get_uri(backend_info));
   341 }