gmyth/samples/gmyth_ls.c
author ali@juiblex.co.uk
Sun Dec 13 23:02:37 2009 +0000 (2009-12-13)
branchtrunk
changeset 949 70e24f6fea25
parent 820 a223e9d89b01
child 950 6308269b026e
permissions -rw-r--r--
Better messages in gmyth-ls
     1 
     2 #ifdef HAVE_CONFIG_H
     3 #include "config.h"
     4 #endif
     5 
     6 #include <stdio.h>
     7 #include <glib.h>
     8 
     9 #include "gmyth_backendinfo.h"
    10 #include "gmyth_scheduler.h"
    11 #include "gmyth_util.h"
    12 #include "gmyth_epg.h"
    13 
    14 typedef struct {
    15     GMythBackendInfo *b_info;
    16     gboolean        list_channels;
    17 } ls_options_t;
    18 
    19 static ls_options_t *
    20 _ls_options_new()
    21 {
    22     ls_options_t   *options = g_new0(ls_options_t, 1);
    23     options->b_info = gmyth_backend_info_new();
    24 
    25     return options;
    26 }
    27 
    28 static void
    29 _ls_options_free(ls_options_t * options)
    30 {
    31     g_return_if_fail(options != NULL);
    32 
    33     if (options->b_info)
    34         g_object_unref(options->b_info);
    35 }
    36 
    37 static          gboolean
    38 _parse_args(int argc, char *argv[], ls_options_t * options)
    39 {
    40     GError         *error = NULL;
    41     GOptionContext *context;
    42 
    43     gchar          *host_ip = NULL;
    44     gint            host_port = 0;
    45     gint            db_port = 0;
    46     gboolean        list_channels = FALSE;
    47     gchar          *username = NULL;
    48     gchar          *password = NULL;
    49 
    50     GOptionEntry    entries[] = {
    51         {"hostname", 'h', 0, G_OPTION_ARG_STRING, &host_ip,
    52          "Mythtv backend hostname or " "IP address", "IP_ADDRESS"},
    53 
    54         {"port", 'p', 0, G_OPTION_ARG_INT, &host_port,
    55          "Mythtv backend port",
    56          "PORT"},
    57 
    58         {"list_channels", 'c', 0, G_OPTION_ARG_NONE, &list_channels,
    59          "List channels",
    60          "LIST_CHANNELS"},
    61 
    62         {"username", 'u', 0, G_OPTION_ARG_STRING, &username,
    63          "Mysql database username. Default: mythtv",
    64          "MYSQL_USER"},
    65 
    66         {"password", 'w', 0, G_OPTION_ARG_STRING, &password,
    67          "Mysql database password. Default: mythtv",
    68          "MYSQL_PASSWD"},
    69 
    70         {"db_port", 'P', 0, G_OPTION_ARG_STRING, &db_port,
    71          "Mysql database port. Default: 0",
    72          "MYSQL_PORT"},
    73 
    74         {NULL}
    75     };
    76 
    77     g_return_val_if_fail(options != NULL, FALSE);
    78 
    79     context =
    80         g_option_context_new
    81         ("- list recorded programs and channels from a mythtv backend\n");
    82     g_option_context_add_main_entries(context, entries, NULL);
    83     g_option_context_parse(context, &argc, &argv, &error);
    84     g_option_context_set_help_enabled(context, TRUE);
    85 
    86     g_option_context_free(context);
    87 
    88     if ((!host_ip) || (host_port == 0)) {
    89         g_free(host_ip);
    90         return FALSE;
    91     }
    92 
    93     gmyth_backend_info_set_hostname(options->b_info, host_ip);
    94     gmyth_backend_info_set_port(options->b_info, host_port);
    95 
    96     if (username)
    97         gmyth_backend_info_set_username(options->b_info, username);
    98     else
    99         gmyth_backend_info_set_username(options->b_info, "mythtv");
   100 
   101     if (password)
   102         gmyth_backend_info_set_password(options->b_info, password);
   103     else
   104         gmyth_backend_info_set_password(options->b_info, "mythtv");
   105 
   106     gmyth_backend_info_set_db_name(options->b_info, "mythconverg");
   107 
   108     options->list_channels = list_channels;
   109 
   110     g_free(host_ip);
   111 
   112     return TRUE;
   113 }
   114 
   115 static          gboolean
   116 _ls_recorded_files(ls_options_t * options)
   117 {
   118     GMythScheduler *scheduler;
   119     GList          *list,
   120                    *iter;
   121     gint            res = 0,
   122                     count = 0;
   123 
   124     g_return_val_if_fail(options != NULL, FALSE);
   125     g_return_val_if_fail(options->b_info != NULL, FALSE);
   126 
   127     scheduler = gmyth_scheduler_new();
   128 
   129     if (gmyth_scheduler_connect_with_timeout(scheduler,
   130                                              options->b_info, 10) == FALSE)
   131     {
   132         g_print("Could not connect to database server.\n");
   133         g_object_unref(scheduler);
   134         return FALSE;
   135     }
   136 
   137     res = gmyth_scheduler_get_recorded_list(scheduler, &list);
   138 
   139     gmyth_scheduler_disconnect(scheduler);
   140     g_object_unref(scheduler);
   141 
   142     if (res < 0) {
   143         g_print("Could not retrieve list of recorded programs.\n");
   144         return FALSE;
   145     }
   146 
   147     iter = list;
   148     while (iter) {
   149         RecordedInfo   *recorded_info = (RecordedInfo *) iter->data;
   150 
   151         if (gmyth_util_file_exists
   152             (options->b_info, recorded_info->basename->str)) {
   153             count++;
   154             g_print("%s\n", recorded_info->basename->str);
   155         }
   156         gmyth_recorded_info_free(recorded_info);
   157         iter = g_list_next(iter);
   158     }
   159 
   160     if (!count)
   161         g_print("No recorded programs.\n");
   162 
   163     g_list_free(list);
   164 
   165     return TRUE;
   166 }
   167 
   168 static          gboolean
   169 _ls_channels(ls_options_t * options)
   170 {
   171     GMythEPG       *epg;
   172     gint            length;
   173     GList          *clist,
   174                    *ch;
   175 
   176     g_return_val_if_fail(options != NULL, FALSE);
   177     g_return_val_if_fail(options->b_info != NULL, FALSE);
   178 
   179 
   180     epg = gmyth_epg_new();
   181     if (!gmyth_epg_connect(epg, options->b_info)) {
   182         g_object_unref(epg);
   183         return FALSE;
   184     }
   185 
   186     length = gmyth_epg_get_channel_list(epg, &clist);
   187     for (ch = clist; ch != NULL; ch = ch->next) {
   188         GMythChannelInfo *info = (GMythChannelInfo *) ch->data;
   189 
   190         if ((info->channel_name == NULL) || (info->channel_num == NULL)) {
   191             continue;
   192         }
   193 
   194         g_print("%s\t\t%s\n", info->channel_num->str,
   195                 info->channel_name->str);
   196     }
   197 
   198     gmyth_free_channel_list(clist);
   199     gmyth_epg_disconnect(epg);
   200     g_object_unref(epg);
   201 
   202     return TRUE;
   203 }
   204 
   205 int
   206 main(int argc, char *argv[])
   207 {
   208     gboolean        res = FALSE;
   209     ls_options_t   *options;
   210 
   211     g_type_init();
   212     g_thread_init(NULL);
   213 
   214     options = _ls_options_new();
   215     res = _parse_args(argc, argv, options);
   216     if (!res) {
   217         g_printerr("Argument invalid. Type --help\n");
   218         return 1;
   219     }
   220 
   221     if (options->list_channels)
   222         res = _ls_channels(options);
   223     else
   224         res = _ls_recorded_files(options);
   225 
   226     _ls_options_free(options);
   227 
   228     return 0;
   229 }