renatofilho@320: renatofilho@320: /** renatofilho@320: * GMyth Library renatofilho@320: * renatofilho@320: * @file gmyth/gmyth_backend_info.c renatofilho@320: * renatofilho@320: * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia. renatofilho@320: * @author Hallyson Melo renatofilho@320: * renatofilho@320: *//* renatofilho@320: * renatofilho@320: * This program is free software; you can redistribute it and/or modify renatofilho@320: * it under the terms of the GNU Lesser General Public License as published by renatofilho@320: * the Free Software Foundation; either version 2 of the License, or renatofilho@320: * (at your option) any later version. renatofilho@320: * renatofilho@320: * This program is distributed in the hope that it will be useful, renatofilho@320: * but WITHOUT ANY WARRANTY; without even the implied warranty of renatofilho@320: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the renatofilho@320: * GNU General Public License for more details. renatofilho@320: * renatofilho@320: * You should have received a copy of the GNU Lesser General Public License renatofilho@320: * along with this program; if not, write to the Free Software renatofilho@320: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA renatofilho@320: */ renatofilho@320: renatofilho@320: #ifdef HAVE_CONFIG_H renatofilho@320: #include "config.h" renatofilho@320: #endif renatofilho@320: renatofilho@320: #include "gmyth_backendinfo.h" renatofilho@320: #include "gmyth_uri.h" renatofilho@320: #include "gmyth_debug.h" renatofilho@320: renatofilho@320: static void gmyth_backend_info_class_init (GMythBackendInfoClass *klass); renatofilho@320: static void gmyth_backend_info_init (GMythBackendInfo *object); renatofilho@320: renatofilho@320: static void gmyth_backend_info_dispose (GObject *object); renatofilho@320: static void gmyth_backend_info_finalize (GObject *object); renatofilho@320: renatofilho@320: G_DEFINE_TYPE(GMythBackendInfo, gmyth_backend_info, G_TYPE_OBJECT) renatofilho@320: renatofilho@320: static void renatofilho@320: gmyth_backend_info_class_init (GMythBackendInfoClass *klass) renatofilho@320: { renatofilho@320: GObjectClass *gobject_class; renatofilho@320: renatofilho@320: gobject_class = (GObjectClass *) klass; renatofilho@320: renatofilho@320: gobject_class->dispose = gmyth_backend_info_dispose; renatofilho@320: gobject_class->finalize = gmyth_backend_info_finalize; renatofilho@320: } renatofilho@320: renatofilho@320: static void renatofilho@320: gmyth_backend_info_init (GMythBackendInfo *backend_info) renatofilho@320: { renatofilho@320: backend_info->hostname = NULL; renatofilho@320: backend_info->username = NULL; renatofilho@320: backend_info->password = NULL; renatofilho@320: backend_info->db_name = NULL; renatofilho@320: backend_info->port = -1; renatofilho@320: backend_info->uri = NULL; renatofilho@320: } renatofilho@320: renatofilho@320: static void renatofilho@320: gmyth_backend_info_dispose (GObject *object) renatofilho@320: { renatofilho@320: GMythBackendInfo *backend_info = GMYTH_BACKEND_INFO (object); renatofilho@320: renatofilho@320: g_free (backend_info->hostname); renatofilho@320: g_free (backend_info->username); renatofilho@320: g_free (backend_info->password); renatofilho@320: g_free (backend_info->db_name); renatofilho@320: renatofilho@320: backend_info->hostname = NULL; renatofilho@320: backend_info->username = NULL; renatofilho@320: backend_info->password = NULL; renatofilho@320: backend_info->db_name = NULL; renatofilho@320: backend_info->port = -1; renatofilho@320: renatofilho@320: if ( backend_info->uri != NULL ) renatofilho@320: { renatofilho@320: g_object_unref(backend_info->uri); renatofilho@320: backend_info->uri = NULL; renatofilho@320: } renatofilho@320: renatofilho@320: G_OBJECT_CLASS (gmyth_backend_info_parent_class)->dispose (object); renatofilho@320: } renatofilho@320: renatofilho@320: static void renatofilho@320: gmyth_backend_info_finalize (GObject *object) renatofilho@320: { renatofilho@320: g_signal_handlers_destroy (object); renatofilho@320: renatofilho@320: G_OBJECT_CLASS (gmyth_backend_info_parent_class)->finalize (object); renatofilho@320: } renatofilho@320: renatofilho@320: /** Creates a new instance of GMythBackendInfo. renatofilho@320: * renatofilho@320: * @return a new instance of GMythBackendInfo. renatofilho@320: */ renatofilho@320: GMythBackendInfo* renatofilho@320: gmyth_backend_info_new () renatofilho@320: { renatofilho@320: GMythBackendInfo *backend_info = renatofilho@320: GMYTH_BACKEND_INFO (g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL)); renatofilho@320: renatofilho@320: return backend_info; renatofilho@320: } renatofilho@320: renatofilho@320: GMythBackendInfo* renatofilho@320: gmyth_backend_info_new_full (const gchar *hostname, const gchar *username, renatofilho@320: const gchar *password, const gchar *db_name, gint port) renatofilho@320: { renatofilho@320: GMythBackendInfo *backend_info = renatofilho@320: GMYTH_BACKEND_INFO (g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL)); renatofilho@320: renatofilho@320: backend_info->uri = gmyth_uri_new_with_value( renatofilho@320: g_strdup_printf( "myth://%s:%s@%s:%d/?%s", username, password, hostname, port, db_name ) ); renatofilho@320: renatofilho@320: gmyth_backend_info_set_hostname (backend_info, hostname); renatofilho@320: gmyth_backend_info_set_username (backend_info, username); renatofilho@320: gmyth_backend_info_set_password (backend_info, password); renatofilho@320: gmyth_backend_info_set_db_name (backend_info, db_name); renatofilho@320: gmyth_backend_info_set_port (backend_info, port); renatofilho@320: renatofilho@320: return backend_info; renatofilho@320: } renatofilho@320: renatofilho@320: GMythBackendInfo* renatofilho@320: gmyth_backend_info_new_with_uri ( const gchar *uri_str ) renatofilho@320: { renatofilho@320: GMythBackendInfo *backend_info = renatofilho@320: GMYTH_BACKEND_INFO (g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL)); renatofilho@320: renatofilho@320: backend_info->uri = gmyth_uri_new_with_value( uri_str ); renatofilho@320: renatofilho@320: gchar** path_parts = g_strsplit( gmyth_uri_get_path( backend_info->uri ), "&", -1 ); renatofilho@320: renatofilho@320: gmyth_backend_info_set_hostname (backend_info, gmyth_uri_get_host ( backend_info->uri ) ); renatofilho@320: gmyth_backend_info_set_username (backend_info, gmyth_uri_get_user( backend_info->uri ) ); renatofilho@320: gmyth_backend_info_set_password (backend_info, gmyth_uri_get_password( backend_info->uri ) ); renatofilho@320: /* gets the path info to database name, from the URI, and removes the trash chars */ renatofilho@320: gmyth_backend_info_set_db_name (backend_info, path_parts != NULL && path_parts[0] != NULL renatofilho@320: && strlen( path_parts[0] ) > 0 ? g_strstrip( g_strdup( g_strdelimit( path_parts[0], "/?", ' ' ) ) ) renatofilho@320: : gmyth_uri_get_path( backend_info->uri ) ); renatofilho@320: gmyth_backend_info_set_port (backend_info, gmyth_uri_get_port( backend_info->uri ) ); renatofilho@320: renatofilho@320: g_strfreev( path_parts ); renatofilho@320: renatofilho@320: return backend_info; renatofilho@320: } renatofilho@320: renatofilho@320: void renatofilho@320: gmyth_backend_info_set_hostname (GMythBackendInfo *backend_info, const gchar *hostname) renatofilho@320: { renatofilho@320: g_return_if_fail (backend_info != NULL); renatofilho@320: renatofilho@320: if ( NULL == hostname || strlen(hostname) <= 0 ) renatofilho@320: { renatofilho@320: gmyth_debug ( "Error trying to set a hostname equals to NULL." ); renatofilho@320: } else { renatofilho@320: backend_info->hostname = g_strdup (hostname); renatofilho@320: } renatofilho@320: } renatofilho@320: renatofilho@320: void renatofilho@320: gmyth_backend_info_set_username (GMythBackendInfo *backend_info, const gchar *username) renatofilho@320: { renatofilho@320: g_return_if_fail (backend_info != NULL); renatofilho@320: renatofilho@320: backend_info->username = g_strdup (username); renatofilho@320: } renatofilho@320: renatofilho@320: void renatofilho@320: gmyth_backend_info_set_password (GMythBackendInfo *backend_info, const gchar *password) renatofilho@320: { renatofilho@320: g_return_if_fail (backend_info != NULL); renatofilho@320: renatofilho@320: backend_info->password = g_strdup (password); renatofilho@320: } renatofilho@320: renatofilho@320: void renatofilho@320: gmyth_backend_info_set_db_name (GMythBackendInfo *backend_info, const gchar *db_name) renatofilho@320: { renatofilho@320: g_return_if_fail (backend_info != NULL); renatofilho@320: renatofilho@320: backend_info->db_name = g_strdup (db_name); renatofilho@320: } renatofilho@320: renatofilho@320: void renatofilho@320: gmyth_backend_info_set_port (GMythBackendInfo *backend_info, const gint port ) renatofilho@320: { renatofilho@320: g_return_if_fail (backend_info != NULL); renatofilho@320: renatofilho@320: if ( port <= 0 ) renatofilho@320: { renatofilho@320: gmyth_debug ( "Error trying to set a hostname equals to NULL (it doesn't using UPnP)." ); renatofilho@320: } else { renatofilho@320: backend_info->port = port; renatofilho@320: } renatofilho@320: } renatofilho@320: renatofilho@320: const gchar* renatofilho@320: gmyth_backend_info_get_hostname (GMythBackendInfo *backend_info) renatofilho@320: { renatofilho@320: g_return_val_if_fail (backend_info != NULL, NULL); renatofilho@320: renatofilho@320: return backend_info->hostname; renatofilho@320: } renatofilho@320: renatofilho@320: const gchar* renatofilho@320: gmyth_backend_info_get_username (GMythBackendInfo *backend_info) renatofilho@320: { renatofilho@320: g_return_val_if_fail (backend_info != NULL, NULL); renatofilho@320: renatofilho@320: return backend_info->username; renatofilho@320: } renatofilho@320: renatofilho@320: const gchar* renatofilho@320: gmyth_backend_info_get_password (GMythBackendInfo *backend_info) renatofilho@320: { renatofilho@320: g_return_val_if_fail (backend_info != NULL, NULL); renatofilho@320: renatofilho@320: return backend_info->password; renatofilho@320: } renatofilho@320: renatofilho@320: const gchar* renatofilho@320: gmyth_backend_info_get_db_name (GMythBackendInfo *backend_info) renatofilho@320: { renatofilho@320: g_return_val_if_fail (backend_info != NULL, NULL); renatofilho@320: renatofilho@320: return backend_info->db_name; renatofilho@320: } renatofilho@320: renatofilho@320: gint renatofilho@320: gmyth_backend_info_get_port (GMythBackendInfo *backend_info) renatofilho@320: { renatofilho@320: g_return_val_if_fail (backend_info != NULL, -1); renatofilho@320: renatofilho@320: return backend_info->port; renatofilho@320: } renatofilho@320: renatofilho@320: const GMythURI* renatofilho@320: gmyth_backend_info_get_uri (GMythBackendInfo *backend_info) renatofilho@320: { renatofilho@320: renatofilho@320: if ( NULL == backend_info->uri ) renatofilho@320: { renatofilho@320: backend_info->uri = gmyth_uri_new_with_value( renatofilho@320: g_strdup_printf( "myth://%s:%s@%s:%d/?%s", backend_info->username, backend_info->password, renatofilho@320: backend_info->hostname, backend_info->port, backend_info->db_name ) ); renatofilho@320: } renatofilho@320: renatofilho@320: return backend_info->uri; renatofilho@320: } renatofilho@320: renatofilho@320: