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