5 * @file gmyth/gmyth_backend_info.c
7 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
8 * @author Hallyson Melo <hallyson.melo@indt.org.br>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "gmyth_backendinfo.h"
32 #include "gmyth_uri.h"
33 #include "gmyth_debug.h"
35 static void gmyth_backend_info_class_init (GMythBackendInfoClass *klass);
36 static void gmyth_backend_info_init (GMythBackendInfo *object);
38 static void gmyth_backend_info_dispose (GObject *object);
39 static void gmyth_backend_info_finalize (GObject *object);
41 G_DEFINE_TYPE(GMythBackendInfo, gmyth_backend_info, G_TYPE_OBJECT)
44 gmyth_backend_info_class_init (GMythBackendInfoClass *klass)
46 GObjectClass *gobject_class;
48 gobject_class = (GObjectClass *) klass;
50 gobject_class->dispose = gmyth_backend_info_dispose;
51 gobject_class->finalize = gmyth_backend_info_finalize;
55 gmyth_backend_info_init (GMythBackendInfo *backend_info)
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->uri = NULL;
66 gmyth_backend_info_dispose (GObject *object)
68 GMythBackendInfo *backend_info = GMYTH_BACKEND_INFO (object);
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);
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;
81 if ( backend_info->uri != NULL )
83 g_object_unref(backend_info->uri);
84 backend_info->uri = NULL;
87 G_OBJECT_CLASS (gmyth_backend_info_parent_class)->dispose (object);
91 gmyth_backend_info_finalize (GObject *object)
93 g_signal_handlers_destroy (object);
95 G_OBJECT_CLASS (gmyth_backend_info_parent_class)->finalize (object);
98 /** Creates a new instance of GMythBackendInfo.
100 * @return a new instance of GMythBackendInfo.
103 gmyth_backend_info_new ()
105 GMythBackendInfo *backend_info =
106 GMYTH_BACKEND_INFO (g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
112 gmyth_backend_info_new_full (const gchar *hostname, const gchar *username,
113 const gchar *password, const gchar *db_name, gint port)
115 GMythBackendInfo *backend_info =
116 GMYTH_BACKEND_INFO (g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
118 backend_info->uri = gmyth_uri_new_with_value(
119 g_strdup_printf( "myth://%s:%s@%s:%d/?%s", username, password, hostname, port, db_name ) );
121 gmyth_backend_info_set_hostname (backend_info, hostname);
122 gmyth_backend_info_set_username (backend_info, username);
123 gmyth_backend_info_set_password (backend_info, password);
124 gmyth_backend_info_set_db_name (backend_info, db_name);
125 gmyth_backend_info_set_port (backend_info, port);
131 gmyth_backend_info_new_with_uri ( const gchar *uri_str )
133 GMythBackendInfo *backend_info =
134 GMYTH_BACKEND_INFO (g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
136 backend_info->uri = gmyth_uri_new_with_value( uri_str );
138 gchar** path_parts = g_strsplit( gmyth_uri_get_path( backend_info->uri ), "&", -1 );
140 gmyth_backend_info_set_hostname (backend_info, gmyth_uri_get_host ( backend_info->uri ) );
141 gmyth_backend_info_set_username (backend_info, gmyth_uri_get_user( backend_info->uri ) );
142 gmyth_backend_info_set_password (backend_info, gmyth_uri_get_password( backend_info->uri ) );
143 /* gets the path info to database name, from the URI, and removes the trash chars */
144 gmyth_backend_info_set_db_name (backend_info, path_parts != NULL && path_parts[0] != NULL
145 && strlen( path_parts[0] ) > 0 ? g_strstrip( g_strdup( g_strdelimit( path_parts[0], "/?", ' ' ) ) )
146 : gmyth_uri_get_path( backend_info->uri ) );
147 gmyth_backend_info_set_port (backend_info, gmyth_uri_get_port( backend_info->uri ) );
149 g_strfreev( path_parts );
155 gmyth_backend_info_set_hostname (GMythBackendInfo *backend_info, const gchar *hostname)
157 g_return_if_fail (backend_info != NULL);
159 if ( NULL == hostname || strlen(hostname) <= 0 )
161 gmyth_debug ( "Error trying to set a hostname equals to NULL." );
163 backend_info->hostname = g_strdup (hostname);
168 gmyth_backend_info_set_username (GMythBackendInfo *backend_info, const gchar *username)
170 g_return_if_fail (backend_info != NULL);
172 backend_info->username = g_strdup (username);
176 gmyth_backend_info_set_password (GMythBackendInfo *backend_info, const gchar *password)
178 g_return_if_fail (backend_info != NULL);
180 backend_info->password = g_strdup (password);
184 gmyth_backend_info_set_db_name (GMythBackendInfo *backend_info, const gchar *db_name)
186 g_return_if_fail (backend_info != NULL);
188 backend_info->db_name = g_strdup (db_name);
192 gmyth_backend_info_set_port (GMythBackendInfo *backend_info, const gint port )
194 g_return_if_fail (backend_info != NULL);
198 gmyth_debug ( "Error trying to set a hostname equals to NULL (it doesn't using UPnP)." );
200 backend_info->port = port;
205 gmyth_backend_info_get_hostname (GMythBackendInfo *backend_info)
207 g_return_val_if_fail (backend_info != NULL, NULL);
209 return backend_info->hostname;
213 gmyth_backend_info_get_username (GMythBackendInfo *backend_info)
215 g_return_val_if_fail (backend_info != NULL, NULL);
217 return backend_info->username;
221 gmyth_backend_info_get_password (GMythBackendInfo *backend_info)
223 g_return_val_if_fail (backend_info != NULL, NULL);
225 return backend_info->password;
229 gmyth_backend_info_get_db_name (GMythBackendInfo *backend_info)
231 g_return_val_if_fail (backend_info != NULL, NULL);
233 return backend_info->db_name;
237 gmyth_backend_info_get_port (GMythBackendInfo *backend_info)
239 g_return_val_if_fail (backend_info != NULL, -1);
241 return backend_info->port;
245 gmyth_backend_info_get_uri (GMythBackendInfo *backend_info)
248 if ( NULL == backend_info->uri )
250 backend_info->uri = gmyth_uri_new_with_value(
251 g_strdup_printf( "myth://%s:%s@%s:%d/?%s", backend_info->username, backend_info->password,
252 backend_info->hostname, backend_info->port, backend_info->db_name ) );
255 return backend_info->uri;