gmyth/samples/gmyth_ls.c
branchtrunk
changeset 951 7b0d87ef5e63
parent 950 6308269b026e
     1.1 --- a/gmyth/samples/gmyth_ls.c	Tue Dec 15 20:48:32 2009 +0000
     1.2 +++ b/gmyth/samples/gmyth_ls.c	Wed Dec 16 10:06:21 2009 +0000
     1.3 @@ -14,6 +14,8 @@
     1.4  typedef struct {
     1.5      GMythBackendInfo *b_info;
     1.6      gboolean        list_channels;
     1.7 +    gboolean        list_rules;
     1.8 +    gboolean        list_upcoming;
     1.9  } ls_options_t;
    1.10  
    1.11  static ls_options_t *
    1.12 @@ -41,9 +43,8 @@
    1.13      GOptionContext *context;
    1.14  
    1.15      gchar          *host_ip = NULL;
    1.16 -    gint            host_port = 0;
    1.17 +    gint            host_port = 6543;
    1.18      gint            db_port = 0;
    1.19 -    gboolean        list_channels = FALSE;
    1.20      gchar          *username = NULL;
    1.21      gchar          *password = NULL;
    1.22  
    1.23 @@ -55,11 +56,16 @@
    1.24           "Mythtv backend port",
    1.25           "PORT"},
    1.26  
    1.27 -        {"list_channels", 'c', 0, G_OPTION_ARG_NONE, &list_channels,
    1.28 -         "List channels",
    1.29 -         "LIST_CHANNELS"},
    1.30 +        {"list_channels", 'c', 0, G_OPTION_ARG_NONE, &options->list_channels,
    1.31 +         "List channels", NULL},
    1.32  
    1.33 -        {"username", 'u', 0, G_OPTION_ARG_STRING, &username,
    1.34 +        {"list_rules", 'r', 0, G_OPTION_ARG_NONE, &options->list_rules,
    1.35 +         "List recording rules", NULL},
    1.36 +
    1.37 +        {"list_upcoming", 'u', 0, G_OPTION_ARG_NONE, &options->list_upcoming,
    1.38 +         "List upcoming recordings", NULL},
    1.39 +
    1.40 +        {"username", 'n', 0, G_OPTION_ARG_STRING, &username,
    1.41           "Mysql database username. Default: mythtv",
    1.42           "MYSQL_USER"},
    1.43  
    1.44 @@ -76,21 +82,28 @@
    1.45  
    1.46      g_return_val_if_fail(options != NULL, FALSE);
    1.47  
    1.48 -    context =
    1.49 -        g_option_context_new
    1.50 -        ("- list recorded programs and channels from a mythtv backend\n");
    1.51 +    context = g_option_context_new("- list mythtv recorded programs etc.");
    1.52      g_option_context_add_main_entries(context, entries, NULL);
    1.53      g_option_context_parse(context, &argc, &argv, &error);
    1.54 -    g_option_context_set_help_enabled(context, TRUE);
    1.55  
    1.56      g_option_context_free(context);
    1.57  
    1.58 -    if ((!host_ip) || (host_port == 0)) {
    1.59 +    if (error) {
    1.60 +        g_printerr("%s.\n", error->message);
    1.61 +        g_error_free(error);
    1.62          g_free(host_ip);
    1.63          return FALSE;
    1.64      }
    1.65  
    1.66 -    gmyth_backend_info_set_hostname(options->b_info, host_ip);
    1.67 +    if (!!options->list_channels + !!options->list_rules +
    1.68 +      !!options->list_upcoming > 1) {
    1.69 +        g_printerr("Only one --list option may be given.\n");
    1.70 +        g_free(host_ip);
    1.71 +        return FALSE;
    1.72 +    }
    1.73 +
    1.74 +    gmyth_backend_info_set_hostname(options->b_info,
    1.75 +      host_ip ? host_ip : "localhost");
    1.76      gmyth_backend_info_set_port(options->b_info, host_port);
    1.77  
    1.78      if (username)
    1.79 @@ -105,9 +118,105 @@
    1.80  
    1.81      gmyth_backend_info_set_db_name(options->b_info, "mythconverg");
    1.82  
    1.83 -    options->list_channels = list_channels;
    1.84 +    g_free(host_ip);
    1.85  
    1.86 -    g_free(host_ip);
    1.87 +    return TRUE;
    1.88 +}
    1.89 +
    1.90 +static gboolean
    1.91 +_ls_recording_rules(ls_options_t * options)
    1.92 +{
    1.93 +    GMythScheduler *scheduler;
    1.94 +    GList          *list,
    1.95 +                   *iter;
    1.96 +    gint            res = 0,
    1.97 +                    count = 0;
    1.98 +
    1.99 +    g_return_val_if_fail(options != NULL, FALSE);
   1.100 +    g_return_val_if_fail(options->b_info != NULL, FALSE);
   1.101 +
   1.102 +    scheduler = gmyth_scheduler_new();
   1.103 +
   1.104 +    if (gmyth_scheduler_connect_with_timeout(scheduler,
   1.105 +                                             options->b_info, 10) == FALSE)
   1.106 +    {
   1.107 +        g_print("Could not connect to database server.\n");
   1.108 +        g_object_unref(scheduler);
   1.109 +        return FALSE;
   1.110 +    }
   1.111 +
   1.112 +    res = gmyth_scheduler_get_schedule_list(scheduler, &list);
   1.113 +
   1.114 +    gmyth_scheduler_disconnect(scheduler);
   1.115 +    g_object_unref(scheduler);
   1.116 +
   1.117 +    if (res < 0) {
   1.118 +        g_print("Could not retrieve list of recording rules.\n");
   1.119 +        return FALSE;
   1.120 +    }
   1.121 +
   1.122 +    iter = list;
   1.123 +    while (iter) {
   1.124 +        ScheduleInfo   *schedule_info = (ScheduleInfo *) iter->data;
   1.125 +
   1.126 +        count++;
   1.127 +        g_print("%s\n", schedule_info->title->str);
   1.128 +        gmyth_schedule_info_free(schedule_info);
   1.129 +        iter = g_list_next(iter);
   1.130 +    }
   1.131 +
   1.132 +    if (!count)
   1.133 +        g_print("No recording rules.\n");
   1.134 +
   1.135 +    g_list_free(list);
   1.136 +
   1.137 +    return TRUE;
   1.138 +}
   1.139 +
   1.140 +static gboolean
   1.141 +_ls_upcoming_recordings(ls_options_t * options)
   1.142 +{
   1.143 +    GSList         *list,
   1.144 +                   *iter;
   1.145 +    gint            count = 0;
   1.146 +    gboolean        conflicts;
   1.147 +    time_t          recstart;
   1.148 +    struct tm      *tm;
   1.149 +    char            buffer[64];
   1.150 +
   1.151 +    g_return_val_if_fail(options != NULL, FALSE);
   1.152 +    g_return_val_if_fail(options->b_info != NULL, FALSE);
   1.153 +
   1.154 +    list = gmyth_util_get_all_pending(options->b_info, &conflicts);
   1.155 +
   1.156 +    iter = list;
   1.157 +    while (iter) {
   1.158 +        GMythProgramInfo *program = (GMythProgramInfo *) iter->data;
   1.159 +
   1.160 +        if (program->recstatus == GMYTH_REC_STATUS_RECORDING ||
   1.161 +          program->recstatus == GMYTH_REC_STATUS_WILL_RECORD) {
   1.162 +            count++;
   1.163 +            recstart = program->recstartts->tv_sec;
   1.164 +            tm = localtime(&recstart);
   1.165 +            strftime(buffer, sizeof(buffer), "%F %R", tm);
   1.166 +            g_print("%s %s", buffer, program->title->str);
   1.167 +            if (program->subtitle->len)
   1.168 +                g_print(": %s", program->subtitle->str);
   1.169 +            g_print("\n");
   1.170 +        }
   1.171 +
   1.172 +        g_object_unref(program);
   1.173 +        iter = g_slist_next(iter);
   1.174 +    }
   1.175 +
   1.176 +    if (!count)
   1.177 +        g_print("No upcoming recordings.\n");
   1.178 +    else if (conflicts)
   1.179 +        g_print("Conflicts found.\n");
   1.180 +    else
   1.181 +        g_print("No conflicts found.\n");
   1.182 +
   1.183 +    g_slist_free(list);
   1.184  
   1.185      return TRUE;
   1.186  }
   1.187 @@ -232,6 +341,10 @@
   1.188  
   1.189      if (options->list_channels)
   1.190          res = _ls_channels(options);
   1.191 +    else if (options->list_rules)
   1.192 +        res = _ls_recording_rules(options);
   1.193 +    else if (options->list_upcoming)
   1.194 +        res = _ls_upcoming_recordings(options);
   1.195      else
   1.196          res = _ls_recorded_files(options);
   1.197