[svn r656] Added gmyth_ls application to list recorded programs and livetv channels trunk
authormelunko
Mon May 14 23:41:05 2007 +0100 (2007-05-14)
branchtrunk
changeset 6501d8c80fdd430
parent 649 5960c7da87ef
child 651 76a3f315ef42
[svn r656] Added gmyth_ls application to list recorded programs and livetv channels
gmyth/samples/Makefile.am
gmyth/samples/gmyth_ls.c
     1.1 --- a/gmyth/samples/Makefile.am	Mon May 14 21:55:32 2007 +0100
     1.2 +++ b/gmyth/samples/Makefile.am	Mon May 14 23:41:05 2007 +0100
     1.3 @@ -1,8 +1,11 @@
     1.4 -bin_PROGRAMS = gmyth-cat
     1.5 +bin_PROGRAMS = gmyth-cat gmyth-ls
     1.6  
     1.7  gmyth_cat_SOURCES = \
     1.8          gmyth_cat.c
     1.9  
    1.10 +gmyth_ls_SOURCES = \
    1.11 +        gmyth_ls.c
    1.12 +
    1.13  LDADD = \
    1.14          $(top_builddir)/src/libgmyth.la
    1.15  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/gmyth/samples/gmyth_ls.c	Mon May 14 23:41:05 2007 +0100
     2.3 @@ -0,0 +1,219 @@
     2.4 +
     2.5 +#ifdef HAVE_CONFIG_H
     2.6 +#include "config.h"
     2.7 +#endif
     2.8 +
     2.9 +#include <stdio.h>
    2.10 +#include <glib.h>
    2.11 +
    2.12 +#include "gmyth_backendinfo.h"
    2.13 +#include "gmyth_scheduler.h"
    2.14 +#include "gmyth_util.h"
    2.15 +#include "gmyth_epg.h"
    2.16 +
    2.17 +typedef struct {
    2.18 +    GMythBackendInfo *b_info;
    2.19 +    gboolean list_channels;
    2.20 +} ls_options_t;
    2.21 +
    2.22 +static ls_options_t*
    2.23 +_ls_options_new ()
    2.24 +{
    2.25 +    ls_options_t *options = g_new0 (ls_options_t, 1);
    2.26 +    options->b_info = gmyth_backend_info_new ();
    2.27 +
    2.28 +    return options;
    2.29 +}
    2.30 +
    2.31 +static void
    2.32 +_ls_options_free (ls_options_t *options)
    2.33 +{
    2.34 +    g_return_if_fail (options != NULL);
    2.35 +
    2.36 +    if (options->b_info)
    2.37 +        g_object_unref (options->b_info);
    2.38 +}
    2.39 +
    2.40 +static gboolean
    2.41 +_parse_args (int argc, char *argv[], ls_options_t *options)
    2.42 +{
    2.43 +    GError *error = NULL;
    2.44 +    GOptionContext *context;
    2.45 +
    2.46 +    gchar *host_ip = NULL;
    2.47 +    gint   host_port = 0;
    2.48 +    gboolean list_channels = FALSE;
    2.49 +    gchar *username = NULL;
    2.50 +    gchar *password = NULL;
    2.51 +
    2.52 +    GOptionEntry entries[] =
    2.53 +    {
    2.54 +        { "hostname", 'h', 0, G_OPTION_ARG_STRING, &host_ip, "Mythtv backend hostname or "
    2.55 +          "IP address", "IP_ADDRESS" },
    2.56 +
    2.57 +        { "port", 'p', 0, G_OPTION_ARG_INT, &host_port, "Mythtv backend port", "PORT" },
    2.58 +
    2.59 +        { "list_channels", 'c', 0, G_OPTION_ARG_NONE, &list_channels, "List channels", 
    2.60 +          "LIST_CHANNELS" },
    2.61 +
    2.62 +        { "username", 'u', 0, G_OPTION_ARG_STRING, &username, "Mysql database username. Default: mythtv"
    2.63 +          "Mysql user", "MYSQL_USER" },
    2.64 +
    2.65 +        { "password", 'w', 0, G_OPTION_ARG_STRING, &password, "Mysql database password. Default: mythtv"
    2.66 +          "Mysql password", "MYSQL_PASSWD" },
    2.67 +
    2.68 +        { NULL }
    2.69 +    };
    2.70 +
    2.71 +    g_return_val_if_fail (options != NULL, FALSE);
    2.72 +
    2.73 +    context = g_option_context_new ("- list recorded programs and channels from a mythtv backend\n");
    2.74 +    g_option_context_add_main_entries (context, entries, NULL);
    2.75 +    g_option_context_parse (context, &argc, &argv, &error);
    2.76 +    g_option_context_set_help_enabled (context, TRUE);
    2.77 +
    2.78 +    g_option_context_free (context);
    2.79 +
    2.80 +    if ((!host_ip) || (host_port == 0) ) {
    2.81 +        g_free (host_ip);
    2.82 +        return FALSE;
    2.83 +    }
    2.84 +
    2.85 +    gmyth_backend_info_set_hostname (options->b_info, host_ip);
    2.86 +    gmyth_backend_info_set_port (options->b_info, host_port);
    2.87 +
    2.88 +    if (username)
    2.89 +        gmyth_backend_info_set_username (options->b_info, username);
    2.90 +    else
    2.91 +        gmyth_backend_info_set_username (options->b_info, "mythtv");
    2.92 +        
    2.93 +    if (password)
    2.94 +        gmyth_backend_info_set_password (options->b_info, password);
    2.95 +    else 
    2.96 +        gmyth_backend_info_set_password (options->b_info, "mythtv");
    2.97 +        
    2.98 +    gmyth_backend_info_set_db_name (options->b_info, "mythconverg");
    2.99 +
   2.100 +    options->list_channels = list_channels;
   2.101 +
   2.102 +    g_free (host_ip);
   2.103 +
   2.104 +    return TRUE;
   2.105 +}
   2.106 +
   2.107 +static gboolean
   2.108 +_ls_recorded_files (ls_options_t *options)
   2.109 +{
   2.110 +    GMythScheduler *scheduler;
   2.111 +    GList *list, *iter;
   2.112 +    gint res = 0;
   2.113 +
   2.114 +    g_return_val_if_fail (options != NULL, FALSE);
   2.115 +    g_return_val_if_fail (options->b_info != NULL, FALSE);
   2.116 +
   2.117 +    scheduler = gmyth_scheduler_new ();
   2.118 +
   2.119 +    if(gmyth_scheduler_connect_with_timeout (scheduler,
   2.120 +                               options->b_info, 1)
   2.121 +         == FALSE) {
   2.122 +        g_warning ("Could not connect to backend db");
   2.123 +        g_object_unref (scheduler);
   2.124 +        return FALSE;
   2.125 +    }
   2.126 +
   2.127 +    res = gmyth_scheduler_get_recorded_list(scheduler, &list);
   2.128 +    if (res < 0) {
   2.129 +        gmyth_scheduler_disconnect(scheduler);
   2.130 +        g_object_unref (scheduler);
   2.131 +        g_warning ("Could not retrieve recorded list");
   2.132 +        return FALSE;
   2.133 +    }
   2.134 +    
   2.135 +    gmyth_scheduler_disconnect(scheduler);
   2.136 +
   2.137 +    if (res == 0) {
   2.138 +        g_print ("None file was found in the backend.\n");
   2.139 +        gmyth_scheduler_disconnect(scheduler);
   2.140 +        g_object_unref (scheduler);
   2.141 +        return TRUE;
   2.142 +    } 
   2.143 +
   2.144 +    iter = list;
   2.145 +    while (iter) {
   2.146 +        RecordedInfo* recorded_info =  (RecordedInfo*) iter->data;
   2.147 +
   2.148 +        if (gmyth_util_file_exists (options->b_info, recorded_info->basename->str)) {
   2.149 +            g_print ("%s\n", recorded_info->basename->str);
   2.150 +        }
   2.151 +        gmyth_recorded_info_free (recorded_info);
   2.152 +        iter = g_list_next (iter);
   2.153 +    }
   2.154 +
   2.155 +    g_list_free(list);
   2.156 +
   2.157 +    gmyth_scheduler_disconnect (scheduler);
   2.158 +    g_object_unref (scheduler);
   2.159 +
   2.160 +    return TRUE;
   2.161 +}
   2.162 +
   2.163 +static gboolean
   2.164 +_ls_channels (ls_options_t *options)
   2.165 +{
   2.166 +    GMythEPG *epg;
   2.167 +    gint length;
   2.168 +    GList *clist, *ch;
   2.169 + 
   2.170 +    g_return_val_if_fail (options != NULL, FALSE);
   2.171 +    g_return_val_if_fail (options->b_info != NULL, FALSE);
   2.172 +
   2.173 +
   2.174 +    epg = gmyth_epg_new ();
   2.175 +    if (!gmyth_epg_connect (epg, options->b_info)) {
   2.176 +        g_object_unref (epg);
   2.177 +        return FALSE;
   2.178 +    }
   2.179 +
   2.180 +    length = gmyth_epg_get_channel_list (epg, &clist);
   2.181 +    for (ch = clist; ch != NULL; ch = ch->next) {
   2.182 +        GMythChannelInfo *info = (GMythChannelInfo*) ch->data;
   2.183 +
   2.184 +        if ((info->channel_name == NULL) || (info->channel_num == NULL)) {
   2.185 +            continue;
   2.186 +        }
   2.187 +
   2.188 +        g_print ("%s\t\t%s\n", info->channel_num->str, info->channel_name->str);
   2.189 +    }
   2.190 +
   2.191 +    gmyth_free_channel_list (clist);
   2.192 +    gmyth_epg_disconnect (epg);
   2.193 +    g_object_unref (epg);
   2.194 +
   2.195 +    return TRUE;
   2.196 +}
   2.197 +
   2.198 +int
   2.199 +main (int argc, char *argv[])
   2.200 +{
   2.201 +    gboolean res = FALSE;
   2.202 +    ls_options_t *options;
   2.203 +
   2.204 +    g_type_init ();
   2.205 +    g_thread_init (NULL);
   2.206 +
   2.207 +    options = _ls_options_new ();
   2.208 +    res = _parse_args (argc, argv, options);
   2.209 +    if (!res) {
   2.210 +        g_printerr ("Argument invalid. Type --help\n");
   2.211 +        return 1;
   2.212 +    }
   2.213 +
   2.214 +    if (options->list_channels)
   2.215 +        res = _ls_channels (options);
   2.216 +    else 
   2.217 +        res = _ls_recorded_files (options);
   2.218 +
   2.219 +    _ls_options_free (options);
   2.220 +
   2.221 +    return 0;
   2.222 +}