gmyth/samples/gmyth_ls.c
author ali@juiblex.co.uk
Wed Dec 16 10:06:21 2009 +0000 (2009-12-16)
branchtrunk
changeset 951 7b0d87ef5e63
parent 950 6308269b026e
permissions -rw-r--r--
Add rules and upcoming lists to 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     gboolean        list_rules;
    18     gboolean        list_upcoming;
    19 } ls_options_t;
    20 
    21 static ls_options_t *
    22 _ls_options_new()
    23 {
    24     ls_options_t   *options = g_new0(ls_options_t, 1);
    25     options->b_info = gmyth_backend_info_new();
    26 
    27     return options;
    28 }
    29 
    30 static void
    31 _ls_options_free(ls_options_t * options)
    32 {
    33     g_return_if_fail(options != NULL);
    34 
    35     if (options->b_info)
    36         g_object_unref(options->b_info);
    37 }
    38 
    39 static          gboolean
    40 _parse_args(int argc, char *argv[], ls_options_t * options)
    41 {
    42     GError         *error = NULL;
    43     GOptionContext *context;
    44 
    45     gchar          *host_ip = NULL;
    46     gint            host_port = 6543;
    47     gint            db_port = 0;
    48     gchar          *username = NULL;
    49     gchar          *password = NULL;
    50 
    51     GOptionEntry    entries[] = {
    52         {"hostname", 'h', 0, G_OPTION_ARG_STRING, &host_ip,
    53          "Mythtv backend hostname or " "IP address", "IP_ADDRESS"},
    54 
    55         {"port", 'p', 0, G_OPTION_ARG_INT, &host_port,
    56          "Mythtv backend port",
    57          "PORT"},
    58 
    59         {"list_channels", 'c', 0, G_OPTION_ARG_NONE, &options->list_channels,
    60          "List channels", NULL},
    61 
    62         {"list_rules", 'r', 0, G_OPTION_ARG_NONE, &options->list_rules,
    63          "List recording rules", NULL},
    64 
    65         {"list_upcoming", 'u', 0, G_OPTION_ARG_NONE, &options->list_upcoming,
    66          "List upcoming recordings", NULL},
    67 
    68         {"username", 'n', 0, G_OPTION_ARG_STRING, &username,
    69          "Mysql database username. Default: mythtv",
    70          "MYSQL_USER"},
    71 
    72         {"password", 'w', 0, G_OPTION_ARG_STRING, &password,
    73          "Mysql database password. Default: mythtv",
    74          "MYSQL_PASSWD"},
    75 
    76         {"db_port", 'P', 0, G_OPTION_ARG_STRING, &db_port,
    77          "Mysql database port. Default: 0",
    78          "MYSQL_PORT"},
    79 
    80         {NULL}
    81     };
    82 
    83     g_return_val_if_fail(options != NULL, FALSE);
    84 
    85     context = g_option_context_new("- list mythtv recorded programs etc.");
    86     g_option_context_add_main_entries(context, entries, NULL);
    87     g_option_context_parse(context, &argc, &argv, &error);
    88 
    89     g_option_context_free(context);
    90 
    91     if (error) {
    92         g_printerr("%s.\n", error->message);
    93         g_error_free(error);
    94         g_free(host_ip);
    95         return FALSE;
    96     }
    97 
    98     if (!!options->list_channels + !!options->list_rules +
    99       !!options->list_upcoming > 1) {
   100         g_printerr("Only one --list option may be given.\n");
   101         g_free(host_ip);
   102         return FALSE;
   103     }
   104 
   105     gmyth_backend_info_set_hostname(options->b_info,
   106       host_ip ? host_ip : "localhost");
   107     gmyth_backend_info_set_port(options->b_info, host_port);
   108 
   109     if (username)
   110         gmyth_backend_info_set_username(options->b_info, username);
   111     else
   112         gmyth_backend_info_set_username(options->b_info, "mythtv");
   113 
   114     if (password)
   115         gmyth_backend_info_set_password(options->b_info, password);
   116     else
   117         gmyth_backend_info_set_password(options->b_info, "mythtv");
   118 
   119     gmyth_backend_info_set_db_name(options->b_info, "mythconverg");
   120 
   121     g_free(host_ip);
   122 
   123     return TRUE;
   124 }
   125 
   126 static gboolean
   127 _ls_recording_rules(ls_options_t * options)
   128 {
   129     GMythScheduler *scheduler;
   130     GList          *list,
   131                    *iter;
   132     gint            res = 0,
   133                     count = 0;
   134 
   135     g_return_val_if_fail(options != NULL, FALSE);
   136     g_return_val_if_fail(options->b_info != NULL, FALSE);
   137 
   138     scheduler = gmyth_scheduler_new();
   139 
   140     if (gmyth_scheduler_connect_with_timeout(scheduler,
   141                                              options->b_info, 10) == FALSE)
   142     {
   143         g_print("Could not connect to database server.\n");
   144         g_object_unref(scheduler);
   145         return FALSE;
   146     }
   147 
   148     res = gmyth_scheduler_get_schedule_list(scheduler, &list);
   149 
   150     gmyth_scheduler_disconnect(scheduler);
   151     g_object_unref(scheduler);
   152 
   153     if (res < 0) {
   154         g_print("Could not retrieve list of recording rules.\n");
   155         return FALSE;
   156     }
   157 
   158     iter = list;
   159     while (iter) {
   160         ScheduleInfo   *schedule_info = (ScheduleInfo *) iter->data;
   161 
   162         count++;
   163         g_print("%s\n", schedule_info->title->str);
   164         gmyth_schedule_info_free(schedule_info);
   165         iter = g_list_next(iter);
   166     }
   167 
   168     if (!count)
   169         g_print("No recording rules.\n");
   170 
   171     g_list_free(list);
   172 
   173     return TRUE;
   174 }
   175 
   176 static gboolean
   177 _ls_upcoming_recordings(ls_options_t * options)
   178 {
   179     GSList         *list,
   180                    *iter;
   181     gint            count = 0;
   182     gboolean        conflicts;
   183     time_t          recstart;
   184     struct tm      *tm;
   185     char            buffer[64];
   186 
   187     g_return_val_if_fail(options != NULL, FALSE);
   188     g_return_val_if_fail(options->b_info != NULL, FALSE);
   189 
   190     list = gmyth_util_get_all_pending(options->b_info, &conflicts);
   191 
   192     iter = list;
   193     while (iter) {
   194         GMythProgramInfo *program = (GMythProgramInfo *) iter->data;
   195 
   196         if (program->recstatus == GMYTH_REC_STATUS_RECORDING ||
   197           program->recstatus == GMYTH_REC_STATUS_WILL_RECORD) {
   198             count++;
   199             recstart = program->recstartts->tv_sec;
   200             tm = localtime(&recstart);
   201             strftime(buffer, sizeof(buffer), "%F %R", tm);
   202             g_print("%s %s", buffer, program->title->str);
   203             if (program->subtitle->len)
   204                 g_print(": %s", program->subtitle->str);
   205             g_print("\n");
   206         }
   207 
   208         g_object_unref(program);
   209         iter = g_slist_next(iter);
   210     }
   211 
   212     if (!count)
   213         g_print("No upcoming recordings.\n");
   214     else if (conflicts)
   215         g_print("Conflicts found.\n");
   216     else
   217         g_print("No conflicts found.\n");
   218 
   219     g_slist_free(list);
   220 
   221     return TRUE;
   222 }
   223 
   224 static          gboolean
   225 _ls_recorded_files(ls_options_t * options)
   226 {
   227     GMythScheduler *scheduler;
   228     GList          *list,
   229                    *iter;
   230     gint            res = 0,
   231                     count = 0;
   232     GMythSocket    *socket;
   233     GString        *path;
   234     GMythProgramInfo *program;
   235 
   236     g_return_val_if_fail(options != NULL, FALSE);
   237     g_return_val_if_fail(options->b_info != NULL, FALSE);
   238 
   239     scheduler = gmyth_scheduler_new();
   240 
   241     if (gmyth_scheduler_connect_with_timeout(scheduler,
   242                                              options->b_info, 10) == FALSE)
   243     {
   244         g_print("Could not connect to database server.\n");
   245         g_object_unref(scheduler);
   246         return FALSE;
   247     }
   248 
   249     res = gmyth_scheduler_get_recorded_list(scheduler, &list);
   250 
   251     gmyth_scheduler_disconnect(scheduler);
   252     g_object_unref(scheduler);
   253 
   254     if (res < 0) {
   255         g_print("Could not retrieve list of recorded programs.\n");
   256         return FALSE;
   257     }
   258 
   259     iter = list;
   260     socket = gmyth_backend_info_get_connected_socket(options->b_info);
   261     while (iter) {
   262         RecordedInfo   *recorded_info = (RecordedInfo *) iter->data;
   263 
   264         program = gmyth_program_info_new();
   265         program->pathname = g_string_new(recorded_info->basename->str);
   266         program->channel_id = recorded_info->channel_id;
   267         program->recstartts = g_memdup(recorded_info->start_time,
   268                                        sizeof(*program->startts));
   269         path = gmyth_util_check_file_from_socket(socket, program);
   270         if (path) {
   271             count++;
   272             g_print("%s\n", recorded_info->basename->str);
   273             g_string_free(path, TRUE);
   274         }
   275         gmyth_recorded_info_free(recorded_info);
   276         g_object_unref(program);
   277         iter = g_list_next(iter);
   278     }
   279     g_object_unref(socket);
   280 
   281     if (!count)
   282         g_print("No recorded programs.\n");
   283 
   284     g_list_free(list);
   285 
   286     return TRUE;
   287 }
   288 
   289 static          gboolean
   290 _ls_channels(ls_options_t * options)
   291 {
   292     GMythEPG       *epg;
   293     gint            length;
   294     GList          *clist,
   295                    *ch;
   296 
   297     g_return_val_if_fail(options != NULL, FALSE);
   298     g_return_val_if_fail(options->b_info != NULL, FALSE);
   299 
   300 
   301     epg = gmyth_epg_new();
   302     if (!gmyth_epg_connect(epg, options->b_info)) {
   303         g_object_unref(epg);
   304         return FALSE;
   305     }
   306 
   307     length = gmyth_epg_get_channel_list(epg, &clist);
   308     for (ch = clist; ch != NULL; ch = ch->next) {
   309         GMythChannelInfo *info = (GMythChannelInfo *) ch->data;
   310 
   311         if ((info->channel_name == NULL) || (info->channel_num == NULL)) {
   312             continue;
   313         }
   314 
   315         g_print("%s\t\t%s\n", info->channel_num->str,
   316                 info->channel_name->str);
   317     }
   318 
   319     gmyth_free_channel_list(clist);
   320     gmyth_epg_disconnect(epg);
   321     g_object_unref(epg);
   322 
   323     return TRUE;
   324 }
   325 
   326 int
   327 main(int argc, char *argv[])
   328 {
   329     gboolean        res = FALSE;
   330     ls_options_t   *options;
   331 
   332     g_type_init();
   333     g_thread_init(NULL);
   334 
   335     options = _ls_options_new();
   336     res = _parse_args(argc, argv, options);
   337     if (!res) {
   338         g_printerr("Argument invalid. Type --help\n");
   339         return 1;
   340     }
   341 
   342     if (options->list_channels)
   343         res = _ls_channels(options);
   344     else if (options->list_rules)
   345         res = _ls_recording_rules(options);
   346     else if (options->list_upcoming)
   347         res = _ls_upcoming_recordings(options);
   348     else
   349         res = _ls_recorded_files(options);
   350 
   351     _ls_options_free(options);
   352 
   353     return 0;
   354 }