gmyth/samples/gmyth_ls.c
author renatofilho
Thu Jun 14 18:21:08 2007 +0100 (2007-06-14)
branchtrunk
changeset 751 3cf3c6019e3b
parent 686 b29ea6deb6f8
child 754 cb885ee44618
permissions -rw-r--r--
[svn r757] fixed indent using GNU Style
     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 {
    16   GMythBackendInfo *b_info;
    17   gboolean list_channels;
    18 } ls_options_t;
    19 
    20 static ls_options_t *
    21 _ls_options_new()
    22 {
    23   ls_options_t *options = g_new0(ls_options_t, 1);
    24   options->b_info = gmyth_backend_info_new();
    25 
    26   return options;
    27 }
    28 
    29 static void
    30 _ls_options_free(ls_options_t * options)
    31 {
    32   g_return_if_fail(options != NULL);
    33 
    34   if (options->b_info)
    35 	g_object_unref(options->b_info);
    36 }
    37 
    38 static gboolean
    39 _parse_args(int argc, char *argv[], ls_options_t * options)
    40 {
    41   GError *error = NULL;
    42   GOptionContext *context;
    43 
    44   gchar *host_ip = NULL;
    45   gint host_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, "Mythtv backend port",
    55 	 "PORT"},
    56 
    57 	{"list_channels", 'c', 0, G_OPTION_ARG_NONE, &list_channels,
    58 	 "List channels",
    59 	 "LIST_CHANNELS"},
    60 
    61 	{"username", 'u', 0, G_OPTION_ARG_STRING, &username,
    62 	 "Mysql database username. Default: mythtv" "Mysql user", "MYSQL_USER"},
    63 
    64 	{"password", 'w', 0, G_OPTION_ARG_STRING, &password,
    65 	 "Mysql database password. Default: mythtv" "Mysql password",
    66 	 "MYSQL_PASSWD"},
    67 
    68 	{NULL}
    69   };
    70 
    71   g_return_val_if_fail(options != NULL, FALSE);
    72 
    73   context =
    74 	g_option_context_new
    75 	("- list recorded programs and channels from a mythtv backend\n");
    76   g_option_context_add_main_entries(context, entries, NULL);
    77   g_option_context_parse(context, &argc, &argv, &error);
    78   g_option_context_set_help_enabled(context, TRUE);
    79 
    80   g_option_context_free(context);
    81 
    82   if ((!host_ip) || (host_port == 0))
    83 	{
    84 	  g_free(host_ip);
    85 	  return FALSE;
    86 	}
    87 
    88   gmyth_backend_info_set_hostname(options->b_info, host_ip);
    89   gmyth_backend_info_set_port(options->b_info, host_port);
    90 
    91   if (username)
    92 	gmyth_backend_info_set_username(options->b_info, username);
    93   else
    94 	gmyth_backend_info_set_username(options->b_info, "mythtv");
    95 
    96   if (password)
    97 	gmyth_backend_info_set_password(options->b_info, password);
    98   else
    99 	gmyth_backend_info_set_password(options->b_info, "mythtv");
   100 
   101   gmyth_backend_info_set_db_name(options->b_info, "mythconverg");
   102 
   103   options->list_channels = list_channels;
   104 
   105   g_free(host_ip);
   106 
   107   return TRUE;
   108 }
   109 
   110 static gboolean
   111 _ls_recorded_files(ls_options_t * options)
   112 {
   113   GMythScheduler *scheduler;
   114   GList *list, *iter;
   115   gint res = 0;
   116 
   117   g_return_val_if_fail(options != NULL, FALSE);
   118   g_return_val_if_fail(options->b_info != NULL, FALSE);
   119 
   120   scheduler = gmyth_scheduler_new();
   121 
   122   if (gmyth_scheduler_connect_with_timeout(scheduler,
   123 										   options->b_info, 10) == FALSE)
   124 	{
   125 	  g_warning("Could not connect to backend db");
   126 	  g_object_unref(scheduler);
   127 	  return FALSE;
   128 	}
   129 
   130   res = gmyth_scheduler_get_recorded_list(scheduler, &list);
   131   if (res < 0)
   132 	{
   133 	  gmyth_scheduler_disconnect(scheduler);
   134 	  g_object_unref(scheduler);
   135 	  g_warning("Could not retrieve recorded list");
   136 	  return FALSE;
   137 	}
   138 
   139   gmyth_scheduler_disconnect(scheduler);
   140 
   141   if (res == 0)
   142 	{
   143 	  g_print("None file was found in the backend.\n");
   144 	  gmyth_scheduler_disconnect(scheduler);
   145 	  g_object_unref(scheduler);
   146 	  return TRUE;
   147 	}
   148 
   149   iter = list;
   150   while (iter)
   151 	{
   152 	  RecordedInfo *recorded_info = (RecordedInfo *) iter->data;
   153 
   154 	  if (gmyth_util_file_exists
   155 		  (options->b_info, recorded_info->basename->str))
   156 		{
   157 		  g_print("%s\n", recorded_info->basename->str);
   158 		}
   159 	  gmyth_recorded_info_free(recorded_info);
   160 	  iter = g_list_next(iter);
   161 	}
   162 
   163   g_list_free(list);
   164 
   165   gmyth_scheduler_disconnect(scheduler);
   166   g_object_unref(scheduler);
   167 
   168   return TRUE;
   169 }
   170 
   171 static gboolean
   172 _ls_channels(ls_options_t * options)
   173 {
   174   GMythEPG *epg;
   175   gint length;
   176   GList *clist, *ch;
   177 
   178   g_return_val_if_fail(options != NULL, FALSE);
   179   g_return_val_if_fail(options->b_info != NULL, FALSE);
   180 
   181 
   182   epg = gmyth_epg_new();
   183   if (!gmyth_epg_connect(epg, options->b_info))
   184 	{
   185 	  g_object_unref(epg);
   186 	  return FALSE;
   187 	}
   188 
   189   length = gmyth_epg_get_channel_list(epg, &clist);
   190   for (ch = clist; ch != NULL; ch = ch->next)
   191 	{
   192 	  GMythChannelInfo *info = (GMythChannelInfo *) ch->data;
   193 
   194 	  if ((info->channel_name == NULL) || (info->channel_num == NULL))
   195 		{
   196 		  continue;
   197 		}
   198 
   199 	  g_print("%s\t\t%s\n", info->channel_num->str, info->channel_name->str);
   200 	}
   201 
   202   gmyth_free_channel_list(clist);
   203   gmyth_epg_disconnect(epg);
   204   g_object_unref(epg);
   205 
   206   return TRUE;
   207 }
   208 
   209 int
   210 main(int argc, char *argv[])
   211 {
   212   gboolean res = FALSE;
   213   ls_options_t *options;
   214 
   215   g_type_init();
   216   g_thread_init(NULL);
   217 
   218   options = _ls_options_new();
   219   res = _parse_args(argc, argv, options);
   220   if (!res)
   221 	{
   222 	  g_printerr("Argument invalid. Type --help\n");
   223 	  return 1;
   224 	}
   225 
   226   if (options->list_channels)
   227 	res = _ls_channels(options);
   228   else
   229 	res = _ls_recorded_files(options);
   230 
   231   _ls_options_free(options);
   232 
   233   return 0;
   234 }