gmyth/src/gmyth_backendinfo.c
author renatofilho
Mon Feb 04 18:23:49 2008 +0000 (2008-02-04)
branchtrunk
changeset 911 da7ce603d47f
parent 884 d3d62eca131c
permissions -rw-r--r--
[svn r918] moved debian dir from gst-gmyth and libgnomevfs2-mythtv to packages project
melunko@117
     1
/**
melunko@117
     2
 * GMyth Library
melunko@117
     3
 *
melunko@117
     4
 * @file gmyth/gmyth_backend_info.c
melunko@117
     5
 * 
rosfran@420
     6
 * @brief <p> This component represents all the MythTV backend server
rosfran@420
     7
 * 						configuration information.
rosfran@420
     8
 *
melunko@117
     9
 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
melunko@117
    10
 * @author Hallyson Melo <hallyson.melo@indt.org.br>
rosfran@420
    11
 * @author Rosfran Borges <rosfran.borges@indt.org.br>
melunko@117
    12
 *
rosfran@701
    13
 * 
rosfran@701
    14
 * This program is free software; you can redistribute it and/or modify
rosfran@701
    15
 * it under the terms of the GNU Lesser General Public License as published by
rosfran@701
    16
 * the Free Software Foundation; either version 2 of the License, or
rosfran@701
    17
 * (at your option) any later version.
rosfran@701
    18
 *
rosfran@701
    19
 * This program is distributed in the hope that it will be useful,
rosfran@701
    20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rosfran@701
    21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
rosfran@701
    22
 * GNU General Public License for more details.
rosfran@701
    23
 *
rosfran@701
    24
 * You should have received a copy of the GNU Lesser General Public License
rosfran@701
    25
 * along with this program; if not, write to the Free Software
rosfran@701
    26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
rosfran@701
    27
 */
rosfran@698
    28
leo_sobral@213
    29
#ifdef HAVE_CONFIG_H
leo_sobral@213
    30
#include "config.h"
leo_sobral@213
    31
#endif
melunko@117
    32
melunko@117
    33
#include "gmyth_backendinfo.h"
rosfran@119
    34
#include "gmyth_uri.h"
renatofilho@131
    35
#include "gmyth_debug.h"
melunko@117
    36
renatofilho@754
    37
static void     gmyth_backend_info_class_init(GMythBackendInfoClass *
renatofilho@754
    38
                                              klass);
renatofilho@754
    39
static void     gmyth_backend_info_init(GMythBackendInfo * object);
melunko@117
    40
renatofilho@754
    41
static void     gmyth_backend_info_dispose(GObject * object);
renatofilho@754
    42
static void     gmyth_backend_info_finalize(GObject * object);
melunko@117
    43
renatofilho@911
    44
G_DEFINE_TYPE(GMythBackendInfo, gmyth_backend_info, G_TYPE_OBJECT);
renatofilho@911
    45
static void     gmyth_backend_info_class_init(GMythBackendInfoClass *
renatofilho@754
    46
                                                  klass)
melunko@117
    47
{
renatofilho@754
    48
    GObjectClass   *gobject_class;
melunko@117
    49
renatofilho@754
    50
    gobject_class = (GObjectClass *) klass;
melunko@117
    51
renatofilho@754
    52
    gobject_class->dispose = gmyth_backend_info_dispose;
renatofilho@754
    53
    gobject_class->finalize = gmyth_backend_info_finalize;
melunko@117
    54
}
melunko@117
    55
melunko@117
    56
static void
renatofilho@750
    57
gmyth_backend_info_init(GMythBackendInfo * backend_info)
melunko@117
    58
{
renatofilho@754
    59
    backend_info->hostname = NULL;
melunko@818
    60
    backend_info->port = -1;
renatofilho@754
    61
    backend_info->username = NULL;
renatofilho@754
    62
    backend_info->password = NULL;
renatofilho@754
    63
    backend_info->db_name = NULL;
melunko@818
    64
    backend_info->db_port = 0;
renatofilho@754
    65
    backend_info->status_port = -1;
melunko@117
    66
}
melunko@117
    67
melunko@117
    68
static void
renatofilho@750
    69
gmyth_backend_info_dispose(GObject * object)
melunko@117
    70
{
renatofilho@754
    71
    GMythBackendInfo *backend_info = GMYTH_BACKEND_INFO(object);
melunko@117
    72
renatofilho@754
    73
    g_free(backend_info->hostname);
renatofilho@754
    74
    g_free(backend_info->username);
renatofilho@754
    75
    g_free(backend_info->password);
renatofilho@754
    76
    g_free(backend_info->db_name);
renatofilho@817
    77
renatofilho@817
    78
    if (backend_info->sock)
renatofilho@817
    79
        g_object_unref (backend_info->sock);
melunko@117
    80
renatofilho@754
    81
    backend_info->hostname = NULL;
melunko@818
    82
    backend_info->port = -1;
renatofilho@754
    83
    backend_info->username = NULL;
renatofilho@754
    84
    backend_info->password = NULL;
renatofilho@754
    85
    backend_info->db_name = NULL;
melunko@818
    86
    backend_info->db_port = 0;
renatofilho@754
    87
    backend_info->status_port = -1;
renatofilho@817
    88
    backend_info->sock = NULL;
melunko@117
    89
renatofilho@754
    90
    G_OBJECT_CLASS(gmyth_backend_info_parent_class)->dispose(object);
melunko@117
    91
}
melunko@117
    92
melunko@117
    93
static void
renatofilho@750
    94
gmyth_backend_info_finalize(GObject * object)
melunko@117
    95
{
renatofilho@754
    96
    g_signal_handlers_destroy(object);
melunko@117
    97
renatofilho@754
    98
    G_OBJECT_CLASS(gmyth_backend_info_parent_class)->finalize(object);
melunko@117
    99
}
melunko@117
   100
rosfran@421
   101
/** 
rosfran@421
   102
 * Creates a new instance of GMythBackendInfo.
melunko@117
   103
 * 
melunko@117
   104
 * @return a new instance of GMythBackendInfo.
melunko@117
   105
 */
rosfran@698
   106
GMythBackendInfo *
renatofilho@750
   107
gmyth_backend_info_new()
melunko@117
   108
{
renatofilho@754
   109
    GMythBackendInfo *backend_info =
renatofilho@754
   110
        GMYTH_BACKEND_INFO(g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
rosfran@698
   111
renatofilho@754
   112
    return backend_info;
melunko@117
   113
}
melunko@117
   114
rosfran@421
   115
/** 
rosfran@421
   116
 * Creates a new instance of GMythBackendInfo, based on a given set of 
rosfran@421
   117
 * configuration parameters.
rosfran@421
   118
 * 
rosfran@421
   119
 * @param hostname The hostname to the MythTV backend server.
rosfran@421
   120
 * @param username The user name to the MythTV backend MySQL server.
rosfran@421
   121
 * @param password The password to the user of the MythTV backend MySQL server.
rosfran@421
   122
 * @param db_name The database name of the MythTV backend, stored on the MySQL server.
rosfran@421
   123
 * @param port The port number of the MythTV backend server (commonly is 6543).
rosfran@421
   124
 * 
rosfran@421
   125
 * @return a new instance of GMythBackendInfo.
rosfran@421
   126
 */
rosfran@698
   127
GMythBackendInfo *
renatofilho@750
   128
gmyth_backend_info_new_full(const gchar * hostname, const gchar * username,
renatofilho@754
   129
                            const gchar * password, const gchar * db_name,
renatofilho@754
   130
                            gint port)
melunko@117
   131
{
renatofilho@754
   132
    GMythBackendInfo *backend_info =
renatofilho@754
   133
        GMYTH_BACKEND_INFO(g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
rosfran@242
   134
renatofilho@754
   135
    gmyth_backend_info_set_hostname(backend_info, hostname);
renatofilho@754
   136
    gmyth_backend_info_set_username(backend_info, username);
renatofilho@754
   137
    gmyth_backend_info_set_password(backend_info, password);
renatofilho@754
   138
    gmyth_backend_info_set_db_name(backend_info, db_name);
renatofilho@754
   139
    gmyth_backend_info_set_port(backend_info, port);
melunko@117
   140
renatofilho@754
   141
    return backend_info;
melunko@117
   142
}
melunko@117
   143
rosfran@421
   144
/** 
rosfran@421
   145
 * Creates a new instance of GMythBackendInfo, based on the 
rosfran@421
   146
 * MythTV's backend server URI string.
rosfran@421
   147
 * 
rosfran@421
   148
 * @param uri_str The URI string pointing to the MythTV backend server.
rosfran@421
   149
 * 
rosfran@421
   150
 * @return a new instance of GMythBackendInfo.
rosfran@421
   151
 */
rosfran@698
   152
GMythBackendInfo *
renatofilho@750
   153
gmyth_backend_info_new_with_uri(const gchar * uri_str)
rosfran@119
   154
{
renatofilho@858
   155
    GMythBackendInfo *backend_info;
renatofilho@858
   156
    GMythURI       *uri;
renatofilho@858
   157
    gchar         **path_parts;
renatofilho@884
   158
    gchar          *db;
rosfran@698
   159
renatofilho@858
   160
    backend_info = GMYTH_BACKEND_INFO(g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
renatofilho@858
   161
    uri = gmyth_uri_new_with_value (uri_str);
renatofilho@858
   162
    path_parts = g_strsplit(gmyth_uri_get_path(uri), "&", -1);
renatofilho@754
   163
    gmyth_backend_info_set_hostname(backend_info, gmyth_uri_get_host(uri));
renatofilho@754
   164
    gmyth_backend_info_set_username(backend_info, gmyth_uri_get_user(uri));
renatofilho@754
   165
    gmyth_backend_info_set_password(backend_info,
renatofilho@754
   166
                                    gmyth_uri_get_password(uri));
melunko@412
   167
renatofilho@754
   168
    /*
renatofilho@754
   169
     * gets the path info to database name, from the URI, and removes the
renatofilho@754
   170
     * trash chars 
renatofilho@754
   171
     */
renatofilho@884
   172
    if ((path_parts != NULL) && (strlen (path_parts[0]) > 0))
renatofilho@884
   173
    {
renatofilho@884
   174
        db =  path_parts[0]+2;
renatofilho@884
   175
    }
renatofilho@884
   176
    else
renatofilho@884
   177
    {
renatofilho@884
   178
        db = gmyth_uri_get_path(uri);
renatofilho@884
   179
    }
renatofilho@884
   180
renatofilho@884
   181
    gmyth_backend_info_set_db_name(backend_info, db);
leo_sobral@446
   182
renatofilho@754
   183
    gmyth_backend_info_set_port(backend_info, gmyth_uri_get_port(uri));
rosfran@698
   184
renatofilho@754
   185
    g_object_unref(uri);
renatofilho@754
   186
    g_strfreev(path_parts);
rosfran@698
   187
renatofilho@754
   188
    return backend_info;
rosfran@119
   189
}
rosfran@119
   190
melunko@117
   191
void
renatofilho@750
   192
gmyth_backend_info_set_hostname(GMythBackendInfo * backend_info,
renatofilho@754
   193
                                const gchar * hostname)
melunko@117
   194
{
renatofilho@754
   195
    g_return_if_fail(backend_info != NULL);
rosfran@698
   196
renatofilho@754
   197
    if (NULL == hostname || strlen(hostname) <= 0) {
renatofilho@754
   198
        gmyth_debug("Error trying to set a hostname equals to NULL.");
renatofilho@754
   199
    } else {
renatofilho@754
   200
        backend_info->hostname = g_strdup(hostname);
renatofilho@754
   201
    }
melunko@117
   202
}
melunko@117
   203
melunko@117
   204
void
renatofilho@750
   205
gmyth_backend_info_set_username(GMythBackendInfo * backend_info,
renatofilho@754
   206
                                const gchar * username)
melunko@117
   207
{
renatofilho@754
   208
    g_return_if_fail(backend_info != NULL);
melunko@117
   209
renatofilho@754
   210
    backend_info->username = g_strdup(username);
melunko@117
   211
}
melunko@117
   212
melunko@117
   213
void
renatofilho@750
   214
gmyth_backend_info_set_password(GMythBackendInfo * backend_info,
renatofilho@754
   215
                                const gchar * password)
melunko@117
   216
{
renatofilho@754
   217
    g_return_if_fail(backend_info != NULL);
melunko@117
   218
renatofilho@754
   219
    backend_info->password = g_strdup(password);
melunko@117
   220
}
melunko@117
   221
melunko@117
   222
void
renatofilho@750
   223
gmyth_backend_info_set_db_name(GMythBackendInfo * backend_info,
renatofilho@754
   224
                               const gchar * db_name)
melunko@117
   225
{
renatofilho@754
   226
    g_return_if_fail(backend_info != NULL);
melunko@117
   227
renatofilho@754
   228
    backend_info->db_name = g_strdup(db_name);
melunko@117
   229
}
melunko@117
   230
rosfran@119
   231
void
melunko@818
   232
gmyth_backend_info_set_db_port(GMythBackendInfo * backend_info, gint db_port)
melunko@818
   233
{
melunko@818
   234
    g_return_if_fail(backend_info != NULL);
melunko@818
   235
melunko@818
   236
    if (db_port <= 0) {
melunko@818
   237
        gmyth_debug("Error trying to set a port less than 0.");
melunko@818
   238
    } else {
melunko@818
   239
        backend_info->db_port = db_port;
melunko@818
   240
    }
melunko@818
   241
}
melunko@818
   242
melunko@818
   243
void
renatofilho@750
   244
gmyth_backend_info_set_port(GMythBackendInfo * backend_info, gint port)
rosfran@119
   245
{
renatofilho@754
   246
    g_return_if_fail(backend_info != NULL);
rosfran@119
   247
renatofilho@754
   248
    if (port <= 0) {
renatofilho@754
   249
        gmyth_debug("Error trying to set a port less than 0.");
renatofilho@754
   250
    } else {
renatofilho@754
   251
        backend_info->port = port;
renatofilho@754
   252
    }
rosfran@119
   253
}
rosfran@119
   254
morphbr@352
   255
void
renatofilho@754
   256
gmyth_backend_info_set_status_port(GMythBackendInfo * backend_info,
renatofilho@754
   257
                                   gint port)
morphbr@352
   258
{
renatofilho@754
   259
    g_return_if_fail(backend_info != NULL);
morphbr@352
   260
renatofilho@754
   261
    if (port <= 0) {
renatofilho@754
   262
        gmyth_debug
renatofilho@754
   263
            ("Error trying to set the status port to less than zero.");
renatofilho@754
   264
    } else {
renatofilho@754
   265
        backend_info->status_port = port;
renatofilho@754
   266
    }
morphbr@352
   267
}
morphbr@352
   268
renatofilho@754
   269
const gchar    *
renatofilho@750
   270
gmyth_backend_info_get_hostname(GMythBackendInfo * backend_info)
melunko@117
   271
{
renatofilho@754
   272
    g_return_val_if_fail(backend_info != NULL, NULL);
melunko@117
   273
renatofilho@754
   274
    return backend_info->hostname;
melunko@117
   275
}
melunko@117
   276
renatofilho@754
   277
const gchar    *
renatofilho@750
   278
gmyth_backend_info_get_username(GMythBackendInfo * backend_info)
melunko@117
   279
{
renatofilho@754
   280
    g_return_val_if_fail(backend_info != NULL, NULL);
melunko@117
   281
renatofilho@754
   282
    return backend_info->username;
melunko@117
   283
}
melunko@117
   284
renatofilho@754
   285
const gchar    *
renatofilho@750
   286
gmyth_backend_info_get_password(GMythBackendInfo * backend_info)
melunko@117
   287
{
renatofilho@754
   288
    g_return_val_if_fail(backend_info != NULL, NULL);
melunko@117
   289
renatofilho@754
   290
    return backend_info->password;
melunko@117
   291
}
melunko@117
   292
renatofilho@754
   293
const gchar    *
renatofilho@750
   294
gmyth_backend_info_get_db_name(GMythBackendInfo * backend_info)
melunko@117
   295
{
renatofilho@754
   296
    g_return_val_if_fail(backend_info != NULL, NULL);
melunko@117
   297
renatofilho@754
   298
    return backend_info->db_name;
melunko@117
   299
}
melunko@117
   300
melunko@117
   301
gint
melunko@818
   302
gmyth_backend_info_get_idb_port(GMythBackendInfo * backend_info)
melunko@818
   303
{
melunko@818
   304
    g_return_val_if_fail(backend_info != NULL, -1);
melunko@818
   305
melunko@818
   306
    return backend_info->db_port;
melunko@818
   307
}
melunko@818
   308
melunko@818
   309
melunko@818
   310
gint
renatofilho@750
   311
gmyth_backend_info_get_port(GMythBackendInfo * backend_info)
melunko@117
   312
{
renatofilho@754
   313
    g_return_val_if_fail(backend_info != NULL, -1);
melunko@117
   314
renatofilho@754
   315
    return backend_info->port;
melunko@117
   316
}
rosfran@178
   317
rosfran@421
   318
/** 
rosfran@421
   319
 * Creates a new instance of GMythURI, based on the GMythBackendInfo instance to the 
rosfran@421
   320
 * MythTV's backend server.
rosfran@421
   321
 * 
rosfran@421
   322
 * @param backend_info The GMythBackendInfo instance.
rosfran@421
   323
 * 
rosfran@421
   324
 * @return an instance of GMythURI, created from a GMythBackendInfo.
rosfran@421
   325
 */
renatofilho@754
   326
GMythURI       *
renatofilho@750
   327
gmyth_backend_info_get_uri(GMythBackendInfo * backend_info)
rosfran@307
   328
{
renatofilho@754
   329
    GMythURI       *uri = NULL;
renatofilho@754
   330
    gchar          *uri_str = NULL;
renatofilho@754
   331
    gchar          *user_info = NULL;
renatofilho@754
   332
    gchar          *db_data = NULL;
rosfran@529
   333
renatofilho@754
   334
    if ((backend_info->username != NULL
renatofilho@754
   335
         && strlen(backend_info->username) > 0))
renatofilho@754
   336
        user_info =
renatofilho@754
   337
            g_strdup_printf("%s:%s@", backend_info->username,
renatofilho@754
   338
                            backend_info->password);
rosfran@698
   339
renatofilho@754
   340
    if (backend_info->db_name != NULL && strlen(backend_info->db_name) > 0) {
renatofilho@754
   341
        if ((g_strrstr(backend_info->db_name, "_") != NULL))
renatofilho@754
   342
            db_data = g_strdup(backend_info->db_name);
renatofilho@754
   343
        else
renatofilho@754
   344
            db_data = g_strdup_printf("?%s&", backend_info->db_name);
renatofilho@754
   345
    }
renatofilho@754
   346
    // else if ( ( ( g_strrstr( backend_info->path, "livetv" ) != NULL )
renatofilho@754
   347
    // || 
renatofilho@754
   348
    // ( g_strrstr( backend_info->path, "/?" ) != NULL ) )
rosfran@698
   349
renatofilho@754
   350
    uri_str = g_strdup_printf("myth://%s%s:%d/%s", user_info != NULL
renatofilho@754
   351
                              && strlen(user_info) > 0 ? user_info : "",
renatofilho@754
   352
                              backend_info->hostname, backend_info->port,
renatofilho@754
   353
                              db_data != NULL
renatofilho@754
   354
                              && strlen(db_data) > 0 ? db_data : "");
renatofilho@754
   355
    uri = gmyth_uri_new_with_value(uri_str);
melunko@412
   356
renatofilho@754
   357
    if (user_info != NULL)
renatofilho@754
   358
        g_free(user_info);
rosfran@529
   359
renatofilho@754
   360
    if (db_data != NULL)
renatofilho@754
   361
        g_free(db_data);
rosfran@698
   362
renatofilho@754
   363
    g_free(uri_str);
rosfran@698
   364
renatofilho@754
   365
    return uri;
rosfran@307
   366
}
rosfran@307
   367
rosfran@529
   368
gboolean
renatofilho@750
   369
gmyth_backend_info_is_local_file(GMythBackendInfo * backend_info)
rosfran@529
   370
{
renatofilho@754
   371
    g_return_val_if_fail(backend_info != NULL, FALSE);
rosfran@698
   372
renatofilho@754
   373
    return
renatofilho@754
   374
        gmyth_uri_is_local_file(gmyth_backend_info_get_uri(backend_info));
rosfran@529
   375
}
renatofilho@769
   376
renatofilho@769
   377
GMythSocket*
renatofilho@769
   378
gmyth_backend_info_get_connected_socket (GMythBackendInfo *backend_info)
renatofilho@769
   379
{
renatofilho@770
   380
    if (backend_info->sock == NULL) {
renatofilho@769
   381
        gboolean res;
renatofilho@769
   382
        backend_info->sock = gmyth_socket_new ();
renatofilho@771
   383
        res =  gmyth_socket_connect_to_backend (backend_info->sock,
renatofilho@769
   384
                                                backend_info->hostname,
renatofilho@769
   385
                                                backend_info->port,
renatofilho@769
   386
                                                TRUE);
renatofilho@769
   387
        if (res == FALSE) {
renatofilho@769
   388
            g_object_unref (backend_info->sock);
renatofilho@769
   389
            backend_info->sock = NULL;
renatofilho@771
   390
            return NULL;
renatofilho@769
   391
        }
renatofilho@769
   392
    }
renatofilho@769
   393
renatofilho@769
   394
    return g_object_ref (backend_info->sock);
renatofilho@769
   395
}