# HG changeset patch
# User ali@juiblex.co.uk
# Date 1260957981 0
# Node ID 7b0d87ef5e6396bd5cf9874eb22c555e9897fb81
# Parent  6308269b026e5af09475953bdf004228f179d469
Add rules and upcoming lists to gmyth_ls

diff -r 6308269b026e -r 7b0d87ef5e63 gmyth/gmyth/gmyth_programinfo.c
--- a/gmyth/gmyth/gmyth_programinfo.c	Tue Dec 15 20:48:32 2009 +0000
+++ b/gmyth/gmyth/gmyth_programinfo.c	Wed Dec 16 10:06:21 2009 +0000
@@ -300,11 +300,11 @@
     gmyth_string_list_append_int(slist, prog->cardid);  /* 18 */
     gmyth_string_list_append_int(slist, prog->inputid); /* 19 */
     gmyth_string_list_append_int(slist, prog->recpriority); /* 20 */
-    gmyth_string_list_append_int(slist, 0 /* prog->recstatus */ );  /* 21 */
+    gmyth_string_list_append_int(slist, prog->recstatus);  /* 21 */
     gmyth_string_list_append_int(slist, prog->recordid);    /* 22 */
-    gmyth_string_list_append_int(slist, 0 /* prog->rectype */ );    /* 23 */
-    gmyth_string_list_append_int(slist, 0 /* prog->dupin */ );  /* 24 */
-    gmyth_string_list_append_int(slist, 0 /* prog->dupmethod */ );  /* 25 */
+    gmyth_string_list_append_int(slist, prog->rectype);    /* 23 */
+    gmyth_string_list_append_int(slist, prog->dupin);  /* 24 */
+    gmyth_string_list_append_int(slist, prog->dupmethod);  /* 25 */
     gmyth_string_list_append_int(slist, prog->recstartts != NULL ? prog->recstartts->tv_sec : 0);   /* 26 
      */// DATETIME_TO_LIST(recstartts)
     gmyth_string_list_append_int(slist, prog->recendts != NULL ? prog->recendts->tv_sec : 0);   /* 27 
@@ -389,11 +389,12 @@
     prog->cardid = gmyth_string_list_get_int(slist, pos + 18);
     prog->inputid = gmyth_string_list_get_int(slist, pos + 19);
     prog->recpriority = gmyth_string_list_get_int(slist, pos + 20);
-    prog->reactivate = gmyth_string_list_get_int(slist, pos + 21);
+    prog->recstatus = gmyth_string_list_get_int(slist, pos + 21);
+    prog->reactivate = prog->recstatus;
     prog->recordid = gmyth_string_list_get_int(slist, pos + 22);
-    gmyth_string_list_get_int(slist, pos + 23);
-    gmyth_string_list_get_int(slist, pos + 24);
-    gmyth_string_list_get_int(slist, pos + 25);
+    prog->rectype = gmyth_string_list_get_int(slist, pos + 23);
+    prog->dupin = gmyth_string_list_get_int(slist, pos + 24);
+    prog->dupmethod = gmyth_string_list_get_int(slist, pos + 25);
     prog->recstartts = gmyth_util_string_to_time_val((gmyth_util_time_to_isoformat((time_t) gmyth_string_list_get_int(slist, pos + 26)))->str); // DATETIME_TO_LIST(recstartts)
     prog->recendts = gmyth_util_string_to_time_val((gmyth_util_time_to_isoformat((time_t) gmyth_string_list_get_int(slist, pos + 27)))->str);   // DATETIME_TO_LIST(recendts)
     prog->repeat = gmyth_string_list_get_int(slist, pos + 28);
diff -r 6308269b026e -r 7b0d87ef5e63 gmyth/gmyth/gmyth_programinfo.h
--- a/gmyth/gmyth/gmyth_programinfo.h	Tue Dec 15 20:48:32 2009 +0000
+++ b/gmyth/gmyth/gmyth_programinfo.h	Wed Dec 16 10:06:21 2009 +0000
@@ -53,6 +53,32 @@
      */
 };
 
+typedef enum {
+    GMYTH_REC_STATUS_FAILED = -9,
+    GMYTH_REC_STATUS_TUNER_BUSY = -8,
+    GMYTH_REC_STATUS_LOW_DISK_SPACE = -7,
+    GMYTH_REC_STATUS_CANCELLED = -6,
+    GMYTH_REC_STATUS_MISSED = -5,
+    GMYTH_REC_STATUS_ABORTED = -4,
+    GMYTH_REC_STATUS_RECORDED = -3,
+    GMYTH_REC_STATUS_RECORDING = -2,
+    GMYTH_REC_STATUS_WILL_RECORD = -1,
+    GMYTH_REC_STATUS_UNKNOWN = 0,
+    GMYTH_REC_STATUS_DONT_RECORD = 1,
+    GMYTH_REC_STATUS_PREVIOUS_RECORDING = 2,
+    GMYTH_REC_STATUS_CURRENT_RECORDING = 3,
+    GMYTH_REC_STATUS_EARLIER_SHOWING = 4,
+    GMYTH_REC_STATUS_TOO_MANY_RECORDINGS = 5,
+    GMYTH_REC_STATUS_NOT_LISTED = 6,
+    GMYTH_REC_STATUS_CONFLICT = 7,
+    GMYTH_REC_STATUS_LATER_SHOWING = 8,
+    GMYTH_REC_STATUS_REPEAT = 9,
+    GMYTH_REC_STATUS_INACTIVE = 10,
+    GMYTH_REC_STATUS_NEVER_RECORD = 11,
+    GMYTH_REC_STATUS_OFF_LINE = 12,
+    GMYTH_REC_STATUS_OTHER_SHOWING = 13,
+} GMythRecStatus;
+
 /**
  * The GMythProgramInfo structure represents a program information
  * stored in the database. It could be a program from the EPG data,
@@ -136,7 +162,7 @@
     GString        *recgroup;
     GString        *playgroup;
     gint            rectype;
-    gint            recstatus;
+    GMythRecStatus  recstatus;
     gint            recpriority;
     gint            dupin;
     gint            dupmethod;
diff -r 6308269b026e -r 7b0d87ef5e63 gmyth/gmyth/gmyth_util.c
--- a/gmyth/gmyth/gmyth_util.c	Tue Dec 15 20:48:32 2009 +0000
+++ b/gmyth/gmyth/gmyth_util.c	Wed Dec 16 10:06:21 2009 +0000
@@ -775,6 +775,50 @@
     return program;
 }
 
+GSList *
+gmyth_util_get_all_pending(GMythBackendInfo * backend_info,
+                           gboolean * conflicts)
+{
+    GSList         *program_list = NULL;
+    GMythSocket    *socket;
+    gboolean        res;
+
+    socket = gmyth_socket_new();
+    res = gmyth_socket_connect_to_backend(socket, backend_info->hostname,
+                                          backend_info->port, TRUE);
+
+    if (res) {
+        GMythStringList *slist = gmyth_string_list_new();
+        int              pos, lines, count;
+
+        gmyth_string_list_append_char_array(slist, "QUERY_GETALLPENDING");
+
+        gmyth_socket_sendreceive_stringlist(socket, slist);
+
+        if (gmyth_string_list_length(slist) > 1) {
+            GMythProgramInfo *program;
+
+            *conflicts = gmyth_string_list_get_int(slist, 0);
+            count = gmyth_string_list_get_int(slist, 1);
+            lines = (gmyth_string_list_length(slist) - 2) / count;
+
+            for(pos = 2; pos < 2 + count * lines; pos += lines) {
+                program =
+                  gmyth_program_info_from_string_list_from_pos(slist, pos);
+
+                if (program)
+                    program_list = g_slist_prepend(program_list, program);
+            }
+        }
+        g_object_unref(slist);
+
+        gmyth_socket_close_connection(socket);
+    }
+    g_object_unref(socket);
+
+    return g_slist_reverse(program_list);
+}
+
 #if !GLIB_CHECK_VERSION (2, 10, 0)
 
 /*
diff -r 6308269b026e -r 7b0d87ef5e63 gmyth/gmyth/gmyth_util.h
--- a/gmyth/gmyth/gmyth_util.h	Tue Dec 15 20:48:32 2009 +0000
+++ b/gmyth/gmyth/gmyth_util.h	Wed Dec 16 10:06:21 2009 +0000
@@ -79,6 +79,8 @@
 GMythProgramInfo*
                 gmyth_util_get_recording_from_channel       (GMythBackendInfo *backend_info,
                                                              const gchar *channel);
+GSList*         gmyth_util_get_all_pending                  (GMythBackendInfo * backend_info,
+                                                             gboolean * conflicts);
 
 G_END_DECLS
 #endif                          /* GMYTH_UTIL_H_ */
diff -r 6308269b026e -r 7b0d87ef5e63 gmyth/samples/gmyth_ls.c
--- a/gmyth/samples/gmyth_ls.c	Tue Dec 15 20:48:32 2009 +0000
+++ b/gmyth/samples/gmyth_ls.c	Wed Dec 16 10:06:21 2009 +0000
@@ -14,6 +14,8 @@
 typedef struct {
     GMythBackendInfo *b_info;
     gboolean        list_channels;
+    gboolean        list_rules;
+    gboolean        list_upcoming;
 } ls_options_t;
 
 static ls_options_t *
@@ -41,9 +43,8 @@
     GOptionContext *context;
 
     gchar          *host_ip = NULL;
-    gint            host_port = 0;
+    gint            host_port = 6543;
     gint            db_port = 0;
-    gboolean        list_channels = FALSE;
     gchar          *username = NULL;
     gchar          *password = NULL;
 
@@ -55,11 +56,16 @@
          "Mythtv backend port",
          "PORT"},
 
-        {"list_channels", 'c', 0, G_OPTION_ARG_NONE, &list_channels,
-         "List channels",
-         "LIST_CHANNELS"},
+        {"list_channels", 'c', 0, G_OPTION_ARG_NONE, &options->list_channels,
+         "List channels", NULL},
 
-        {"username", 'u', 0, G_OPTION_ARG_STRING, &username,
+        {"list_rules", 'r', 0, G_OPTION_ARG_NONE, &options->list_rules,
+         "List recording rules", NULL},
+
+        {"list_upcoming", 'u', 0, G_OPTION_ARG_NONE, &options->list_upcoming,
+         "List upcoming recordings", NULL},
+
+        {"username", 'n', 0, G_OPTION_ARG_STRING, &username,
          "Mysql database username. Default: mythtv",
          "MYSQL_USER"},
 
@@ -76,21 +82,28 @@
 
     g_return_val_if_fail(options != NULL, FALSE);
 
-    context =
-        g_option_context_new
-        ("- list recorded programs and channels from a mythtv backend\n");
+    context = g_option_context_new("- list mythtv recorded programs etc.");
     g_option_context_add_main_entries(context, entries, NULL);
     g_option_context_parse(context, &argc, &argv, &error);
-    g_option_context_set_help_enabled(context, TRUE);
 
     g_option_context_free(context);
 
-    if ((!host_ip) || (host_port == 0)) {
+    if (error) {
+        g_printerr("%s.\n", error->message);
+        g_error_free(error);
         g_free(host_ip);
         return FALSE;
     }
 
-    gmyth_backend_info_set_hostname(options->b_info, host_ip);
+    if (!!options->list_channels + !!options->list_rules +
+      !!options->list_upcoming > 1) {
+        g_printerr("Only one --list option may be given.\n");
+        g_free(host_ip);
+        return FALSE;
+    }
+
+    gmyth_backend_info_set_hostname(options->b_info,
+      host_ip ? host_ip : "localhost");
     gmyth_backend_info_set_port(options->b_info, host_port);
 
     if (username)
@@ -105,9 +118,105 @@
 
     gmyth_backend_info_set_db_name(options->b_info, "mythconverg");
 
-    options->list_channels = list_channels;
+    g_free(host_ip);
 
-    g_free(host_ip);
+    return TRUE;
+}
+
+static gboolean
+_ls_recording_rules(ls_options_t * options)
+{
+    GMythScheduler *scheduler;
+    GList          *list,
+                   *iter;
+    gint            res = 0,
+                    count = 0;
+
+    g_return_val_if_fail(options != NULL, FALSE);
+    g_return_val_if_fail(options->b_info != NULL, FALSE);
+
+    scheduler = gmyth_scheduler_new();
+
+    if (gmyth_scheduler_connect_with_timeout(scheduler,
+                                             options->b_info, 10) == FALSE)
+    {
+        g_print("Could not connect to database server.\n");
+        g_object_unref(scheduler);
+        return FALSE;
+    }
+
+    res = gmyth_scheduler_get_schedule_list(scheduler, &list);
+
+    gmyth_scheduler_disconnect(scheduler);
+    g_object_unref(scheduler);
+
+    if (res < 0) {
+        g_print("Could not retrieve list of recording rules.\n");
+        return FALSE;
+    }
+
+    iter = list;
+    while (iter) {
+        ScheduleInfo   *schedule_info = (ScheduleInfo *) iter->data;
+
+        count++;
+        g_print("%s\n", schedule_info->title->str);
+        gmyth_schedule_info_free(schedule_info);
+        iter = g_list_next(iter);
+    }
+
+    if (!count)
+        g_print("No recording rules.\n");
+
+    g_list_free(list);
+
+    return TRUE;
+}
+
+static gboolean
+_ls_upcoming_recordings(ls_options_t * options)
+{
+    GSList         *list,
+                   *iter;
+    gint            count = 0;
+    gboolean        conflicts;
+    time_t          recstart;
+    struct tm      *tm;
+    char            buffer[64];
+
+    g_return_val_if_fail(options != NULL, FALSE);
+    g_return_val_if_fail(options->b_info != NULL, FALSE);
+
+    list = gmyth_util_get_all_pending(options->b_info, &conflicts);
+
+    iter = list;
+    while (iter) {
+        GMythProgramInfo *program = (GMythProgramInfo *) iter->data;
+
+        if (program->recstatus == GMYTH_REC_STATUS_RECORDING ||
+          program->recstatus == GMYTH_REC_STATUS_WILL_RECORD) {
+            count++;
+            recstart = program->recstartts->tv_sec;
+            tm = localtime(&recstart);
+            strftime(buffer, sizeof(buffer), "%F %R", tm);
+            g_print("%s %s", buffer, program->title->str);
+            if (program->subtitle->len)
+                g_print(": %s", program->subtitle->str);
+            g_print("\n");
+        }
+
+        g_object_unref(program);
+        iter = g_slist_next(iter);
+    }
+
+    if (!count)
+        g_print("No upcoming recordings.\n");
+    else if (conflicts)
+        g_print("Conflicts found.\n");
+    else
+        g_print("No conflicts found.\n");
+
+    g_slist_free(list);
 
     return TRUE;
 }
@@ -232,6 +341,10 @@
 
     if (options->list_channels)
         res = _ls_channels(options);
+    else if (options->list_rules)
+        res = _ls_recording_rules(options);
+    else if (options->list_upcoming)
+        res = _ls_upcoming_recordings(options);
     else
         res = _ls_recorded_files(options);