gmyth/samples/gmyth_upnp_search.c
author renatofilho
Fri Feb 01 19:21:52 2008 +0000 (2008-02-01)
branchtrunk
changeset 907 9fa6794e53fb
parent 750 312d6bc514f3
permissions -rw-r--r--
[svn r913] fixed gmyth version on control packages
     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-upnp/gmyth_upnp.h>
    10 
    11 #include "gmyth_backendinfo.h"
    12 #include "gmyth_file_transfer.h"
    13 #include "gmyth_livetv.h"
    14 #include "gmyth_util.h"
    15 #include "gmyth_common.h"
    16 
    17 typedef struct {
    18     GMythBackendInfo *b_info;
    19     gchar          *mythtv_id;
    20 } gupnp_options_t;
    21 
    22 static gupnp_options_t *
    23 _gupnp_options_new()
    24 {
    25     gupnp_options_t *options = g_new0(gupnp_options_t, 1);
    26     options->b_info = gmyth_backend_info_new();
    27 
    28     return options;
    29 }
    30 
    31 static void
    32 _gupnp_options_free(gupnp_options_t * options)
    33 {
    34     g_return_if_fail(options != NULL);
    35 
    36     if (options->b_info)
    37         g_object_unref(options->b_info);
    38     g_free(options->mythtv_id);
    39 }
    40 
    41 static          gboolean
    42 _parse_args(int argc, char *argv[], gupnp_options_t * options)
    43 {
    44     GError         *error = NULL;
    45     GOptionContext *context;
    46 
    47     gchar          *mythtv_id = NULL;
    48 
    49     GOptionEntry    entries[] = {
    50         {"mythtvid", 'm', 0, G_OPTION_ARG_STRING, &mythtv_id,
    51          "MythTV UPnP service " "identifigupnpion", "UPNP_ID"},
    52 
    53         {NULL}
    54     };
    55 
    56     g_return_val_if_fail(options != NULL, FALSE);
    57 
    58     context =
    59         g_option_context_new
    60         ("- searches for a list of connected mythtv backend recorded "
    61          "file and prints it on the standard output\n");
    62     g_option_context_add_main_entries(context, entries, NULL);
    63     g_option_context_parse(context, &argc, &argv, &error);
    64     g_option_context_set_help_enabled(context, TRUE);
    65 
    66     g_option_context_free(context);
    67 
    68     g_free(mythtv_id);
    69 
    70     return TRUE;
    71 }
    72 
    73 
    74 static void
    75 _got_upnp_device(GMythUPnP * gupnp, GMythUPnPDeviceStatus status,
    76                  gchar * udn)
    77 {
    78     g_debug("Got Device !!! [%s, %s]",
    79             gmyth_upnp_device_status_to_string(status), udn);
    80 }
    81 
    82 static          gboolean
    83 _gupnp_search_devices(gupnp_options_t * options)
    84 {
    85     GList          *upnp_servers = NULL;
    86     GMythUPnP      *gupnp;
    87     GMythBackendInfo *backend_info = gmyth_backend_info_new();
    88 
    89     g_return_val_if_fail(options != NULL, FALSE);
    90     g_return_val_if_fail(options->b_info != NULL, FALSE);
    91 
    92     gupnp = gmyth_upnp_new(backend_info, _got_upnp_device);
    93     upnp_servers = gmyth_upnp_do_search_sync(gupnp);
    94 
    95     g_list_free(upnp_servers);
    96     g_object_unref(gupnp);
    97 
    98     return TRUE;
    99 }
   100 
   101 int
   102 main(int argc, char *argv[])
   103 {
   104     gboolean        res = FALSE;
   105     gupnp_options_t *options;
   106 
   107     g_type_init();
   108     if (!g_thread_supported())
   109         g_thread_init(NULL);
   110 
   111     options = _gupnp_options_new();
   112     res = _parse_args(argc, argv, options);
   113     if (!res) {
   114         g_printerr("Argument invalid. Type --help\n");
   115         return 1;
   116     }
   117 
   118     res = _gupnp_search_devices(options);
   119 
   120     _gupnp_options_free(options);
   121 
   122     return 0;
   123 }