4 * @file gmyth/gmyth_backend_info.c
6 * @brief <p> This component represents all the MythTV backend server
7 * configuration information.
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>
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.
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.
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
33 #include "gmyth_backendinfo.h"
34 #include "gmyth_uri.h"
35 #include "gmyth_debug.h"
37 static void gmyth_backend_info_class_init(GMythBackendInfoClass * klass);
38 static void gmyth_backend_info_init(GMythBackendInfo * object);
40 static void gmyth_backend_info_dispose(GObject * object);
41 static void gmyth_backend_info_finalize(GObject * object);
43 G_DEFINE_TYPE(GMythBackendInfo, gmyth_backend_info, G_TYPE_OBJECT)
44 static void 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->status_port = -1;
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;
80 backend_info->status_port = -1;
82 G_OBJECT_CLASS(gmyth_backend_info_parent_class)->dispose(object);
86 gmyth_backend_info_finalize(GObject * object)
88 g_signal_handlers_destroy(object);
90 G_OBJECT_CLASS(gmyth_backend_info_parent_class)->finalize(object);
94 * Creates a new instance of GMythBackendInfo.
96 * @return a new instance of GMythBackendInfo.
99 gmyth_backend_info_new()
101 GMythBackendInfo *backend_info =
102 GMYTH_BACKEND_INFO(g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
108 * Creates a new instance of GMythBackendInfo, based on a given set of
109 * configuration parameters.
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).
117 * @return a new instance of GMythBackendInfo.
120 gmyth_backend_info_new_full(const gchar * hostname, const gchar * username,
121 const gchar * password, const gchar * db_name,
124 GMythBackendInfo *backend_info =
125 GMYTH_BACKEND_INFO(g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
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);
137 * Creates a new instance of GMythBackendInfo, based on the
138 * MythTV's backend server URI string.
140 * @param uri_str The URI string pointing to the MythTV backend server.
142 * @return a new instance of GMythBackendInfo.
145 gmyth_backend_info_new_with_uri(const gchar * uri_str)
147 GMythBackendInfo *backend_info =
148 GMYTH_BACKEND_INFO(g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
150 GMythURI *uri = gmyth_uri_new_with_value(uri_str);
152 gchar **path_parts = g_strsplit(gmyth_uri_get_path(uri), "&", -1);
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));
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));
165 gmyth_backend_info_set_port(backend_info, gmyth_uri_get_port(uri));
168 g_strfreev(path_parts);
174 gmyth_backend_info_set_hostname(GMythBackendInfo * backend_info,
175 const gchar * hostname)
177 g_return_if_fail(backend_info != NULL);
179 if (NULL == hostname || strlen(hostname) <= 0)
181 gmyth_debug("Error trying to set a hostname equals to NULL.");
185 backend_info->hostname = g_strdup(hostname);
190 gmyth_backend_info_set_username(GMythBackendInfo * backend_info,
191 const gchar * username)
193 g_return_if_fail(backend_info != NULL);
195 backend_info->username = g_strdup(username);
199 gmyth_backend_info_set_password(GMythBackendInfo * backend_info,
200 const gchar * password)
202 g_return_if_fail(backend_info != NULL);
204 backend_info->password = g_strdup(password);
208 gmyth_backend_info_set_db_name(GMythBackendInfo * backend_info,
209 const gchar * db_name)
211 g_return_if_fail(backend_info != NULL);
213 backend_info->db_name = g_strdup(db_name);
217 gmyth_backend_info_set_port(GMythBackendInfo * backend_info, gint port)
219 g_return_if_fail(backend_info != NULL);
223 gmyth_debug("Error trying to set a port less than 0.");
227 backend_info->port = port;
232 gmyth_backend_info_set_status_port(GMythBackendInfo * backend_info, gint port)
234 g_return_if_fail(backend_info != NULL);
238 gmyth_debug("Error trying to set the status port to less than zero.");
242 backend_info->status_port = port;
247 gmyth_backend_info_get_hostname(GMythBackendInfo * backend_info)
249 g_return_val_if_fail(backend_info != NULL, NULL);
251 return backend_info->hostname;
255 gmyth_backend_info_get_username(GMythBackendInfo * backend_info)
257 g_return_val_if_fail(backend_info != NULL, NULL);
259 return backend_info->username;
263 gmyth_backend_info_get_password(GMythBackendInfo * backend_info)
265 g_return_val_if_fail(backend_info != NULL, NULL);
267 return backend_info->password;
271 gmyth_backend_info_get_db_name(GMythBackendInfo * backend_info)
273 g_return_val_if_fail(backend_info != NULL, NULL);
275 return backend_info->db_name;
279 gmyth_backend_info_get_port(GMythBackendInfo * backend_info)
281 g_return_val_if_fail(backend_info != NULL, -1);
283 return backend_info->port;
287 * Creates a new instance of GMythURI, based on the GMythBackendInfo instance to the
288 * MythTV's backend server.
290 * @param backend_info The GMythBackendInfo instance.
292 * @return an instance of GMythURI, created from a GMythBackendInfo.
295 gmyth_backend_info_get_uri(GMythBackendInfo * backend_info)
297 GMythURI *uri = NULL;
298 gchar *uri_str = NULL;
299 gchar *user_info = NULL;
300 gchar *db_data = NULL;
302 if ((backend_info->username != NULL && strlen(backend_info->username) > 0))
304 g_strdup_printf("%s:%s@", backend_info->username,
305 backend_info->password);
307 if (backend_info->db_name != NULL && strlen(backend_info->db_name) > 0)
309 if ((g_strrstr(backend_info->db_name, "_") != NULL))
310 db_data = g_strdup(backend_info->db_name);
312 db_data = g_strdup_printf("?%s&", backend_info->db_name);
314 //else if ( ( ( g_strrstr( backend_info->path, "livetv" ) != NULL ) ||
315 // ( g_strrstr( backend_info->path, "/?" ) != NULL ) )
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,
321 && strlen(db_data) > 0 ? db_data : "");
322 uri = gmyth_uri_new_with_value(uri_str);
324 if (user_info != NULL)
336 gmyth_backend_info_is_local_file(GMythBackendInfo * backend_info)
338 g_return_val_if_fail(backend_info != NULL, FALSE);
340 return gmyth_uri_is_local_file(gmyth_backend_info_get_uri(backend_info));