gmyth/samples/gmyth_ls.c
author ali@juiblex.co.uk
Sun Dec 13 23:02:37 2009 +0000 (2009-12-13)
branchtrunk
changeset 949 70e24f6fea25
parent 820 a223e9d89b01
child 950 6308269b026e
permissions -rw-r--r--
Better messages in gmyth-ls
melunko@650
     1
melunko@650
     2
#ifdef HAVE_CONFIG_H
melunko@650
     3
#include "config.h"
melunko@650
     4
#endif
melunko@650
     5
melunko@650
     6
#include <stdio.h>
melunko@650
     7
#include <glib.h>
melunko@650
     8
melunko@650
     9
#include "gmyth_backendinfo.h"
melunko@650
    10
#include "gmyth_scheduler.h"
melunko@650
    11
#include "gmyth_util.h"
melunko@650
    12
#include "gmyth_epg.h"
melunko@650
    13
renatofilho@754
    14
typedef struct {
renatofilho@754
    15
    GMythBackendInfo *b_info;
renatofilho@754
    16
    gboolean        list_channels;
melunko@650
    17
} ls_options_t;
melunko@650
    18
renatofilho@750
    19
static ls_options_t *
renatofilho@750
    20
_ls_options_new()
melunko@650
    21
{
renatofilho@754
    22
    ls_options_t   *options = g_new0(ls_options_t, 1);
renatofilho@754
    23
    options->b_info = gmyth_backend_info_new();
melunko@650
    24
renatofilho@754
    25
    return options;
melunko@650
    26
}
melunko@650
    27
melunko@650
    28
static void
renatofilho@750
    29
_ls_options_free(ls_options_t * options)
melunko@650
    30
{
renatofilho@754
    31
    g_return_if_fail(options != NULL);
melunko@650
    32
renatofilho@754
    33
    if (options->b_info)
renatofilho@754
    34
        g_object_unref(options->b_info);
melunko@650
    35
}
melunko@650
    36
renatofilho@754
    37
static          gboolean
renatofilho@750
    38
_parse_args(int argc, char *argv[], ls_options_t * options)
melunko@650
    39
{
renatofilho@754
    40
    GError         *error = NULL;
renatofilho@754
    41
    GOptionContext *context;
melunko@650
    42
renatofilho@754
    43
    gchar          *host_ip = NULL;
renatofilho@754
    44
    gint            host_port = 0;
melunko@820
    45
    gint            db_port = 0;
renatofilho@754
    46
    gboolean        list_channels = FALSE;
renatofilho@754
    47
    gchar          *username = NULL;
renatofilho@754
    48
    gchar          *password = NULL;
melunko@650
    49
renatofilho@754
    50
    GOptionEntry    entries[] = {
renatofilho@754
    51
        {"hostname", 'h', 0, G_OPTION_ARG_STRING, &host_ip,
renatofilho@754
    52
         "Mythtv backend hostname or " "IP address", "IP_ADDRESS"},
melunko@650
    53
renatofilho@754
    54
        {"port", 'p', 0, G_OPTION_ARG_INT, &host_port,
renatofilho@754
    55
         "Mythtv backend port",
renatofilho@754
    56
         "PORT"},
melunko@650
    57
renatofilho@754
    58
        {"list_channels", 'c', 0, G_OPTION_ARG_NONE, &list_channels,
renatofilho@754
    59
         "List channels",
renatofilho@754
    60
         "LIST_CHANNELS"},
melunko@650
    61
renatofilho@754
    62
        {"username", 'u', 0, G_OPTION_ARG_STRING, &username,
melunko@820
    63
         "Mysql database username. Default: mythtv",
renatofilho@754
    64
         "MYSQL_USER"},
melunko@650
    65
renatofilho@754
    66
        {"password", 'w', 0, G_OPTION_ARG_STRING, &password,
melunko@820
    67
         "Mysql database password. Default: mythtv",
renatofilho@754
    68
         "MYSQL_PASSWD"},
melunko@650
    69
melunko@820
    70
        {"db_port", 'P', 0, G_OPTION_ARG_STRING, &db_port,
melunko@820
    71
         "Mysql database port. Default: 0",
melunko@820
    72
         "MYSQL_PORT"},
melunko@820
    73
renatofilho@754
    74
        {NULL}
renatofilho@754
    75
    };
melunko@650
    76
renatofilho@754
    77
    g_return_val_if_fail(options != NULL, FALSE);
melunko@650
    78
renatofilho@754
    79
    context =
renatofilho@754
    80
        g_option_context_new
renatofilho@754
    81
        ("- list recorded programs and channels from a mythtv backend\n");
renatofilho@754
    82
    g_option_context_add_main_entries(context, entries, NULL);
renatofilho@754
    83
    g_option_context_parse(context, &argc, &argv, &error);
renatofilho@754
    84
    g_option_context_set_help_enabled(context, TRUE);
melunko@650
    85
renatofilho@754
    86
    g_option_context_free(context);
melunko@650
    87
renatofilho@754
    88
    if ((!host_ip) || (host_port == 0)) {
renatofilho@754
    89
        g_free(host_ip);
renatofilho@754
    90
        return FALSE;
renatofilho@754
    91
    }
melunko@650
    92
renatofilho@754
    93
    gmyth_backend_info_set_hostname(options->b_info, host_ip);
renatofilho@754
    94
    gmyth_backend_info_set_port(options->b_info, host_port);
melunko@650
    95
renatofilho@754
    96
    if (username)
renatofilho@754
    97
        gmyth_backend_info_set_username(options->b_info, username);
renatofilho@754
    98
    else
renatofilho@754
    99
        gmyth_backend_info_set_username(options->b_info, "mythtv");
melunko@650
   100
renatofilho@754
   101
    if (password)
renatofilho@754
   102
        gmyth_backend_info_set_password(options->b_info, password);
renatofilho@754
   103
    else
renatofilho@754
   104
        gmyth_backend_info_set_password(options->b_info, "mythtv");
melunko@650
   105
renatofilho@754
   106
    gmyth_backend_info_set_db_name(options->b_info, "mythconverg");
melunko@650
   107
renatofilho@754
   108
    options->list_channels = list_channels;
renatofilho@750
   109
renatofilho@754
   110
    g_free(host_ip);
renatofilho@750
   111
renatofilho@754
   112
    return TRUE;
melunko@650
   113
}
melunko@650
   114
renatofilho@754
   115
static          gboolean
renatofilho@750
   116
_ls_recorded_files(ls_options_t * options)
melunko@650
   117
{
renatofilho@754
   118
    GMythScheduler *scheduler;
renatofilho@754
   119
    GList          *list,
renatofilho@754
   120
                   *iter;
ali@949
   121
    gint            res = 0,
ali@949
   122
                    count = 0;
melunko@650
   123
renatofilho@754
   124
    g_return_val_if_fail(options != NULL, FALSE);
renatofilho@754
   125
    g_return_val_if_fail(options->b_info != NULL, FALSE);
melunko@650
   126
renatofilho@754
   127
    scheduler = gmyth_scheduler_new();
melunko@650
   128
renatofilho@754
   129
    if (gmyth_scheduler_connect_with_timeout(scheduler,
renatofilho@754
   130
                                             options->b_info, 10) == FALSE)
renatofilho@754
   131
    {
ali@949
   132
        g_print("Could not connect to database server.\n");
renatofilho@754
   133
        g_object_unref(scheduler);
renatofilho@754
   134
        return FALSE;
renatofilho@754
   135
    }
melunko@650
   136
renatofilho@754
   137
    res = gmyth_scheduler_get_recorded_list(scheduler, &list);
melunko@650
   138
renatofilho@754
   139
    gmyth_scheduler_disconnect(scheduler);
ali@949
   140
    g_object_unref(scheduler);
melunko@650
   141
ali@949
   142
    if (res < 0) {
ali@949
   143
        g_print("Could not retrieve list of recorded programs.\n");
ali@949
   144
        return FALSE;
renatofilho@754
   145
    }
melunko@650
   146
renatofilho@754
   147
    iter = list;
renatofilho@754
   148
    while (iter) {
renatofilho@754
   149
        RecordedInfo   *recorded_info = (RecordedInfo *) iter->data;
melunko@650
   150
renatofilho@754
   151
        if (gmyth_util_file_exists
renatofilho@754
   152
            (options->b_info, recorded_info->basename->str)) {
ali@949
   153
            count++;
renatofilho@754
   154
            g_print("%s\n", recorded_info->basename->str);
renatofilho@754
   155
        }
renatofilho@754
   156
        gmyth_recorded_info_free(recorded_info);
renatofilho@754
   157
        iter = g_list_next(iter);
renatofilho@754
   158
    }
melunko@650
   159
ali@949
   160
    if (!count)
ali@949
   161
        g_print("No recorded programs.\n");
ali@949
   162
renatofilho@754
   163
    g_list_free(list);
melunko@650
   164
renatofilho@754
   165
    return TRUE;
melunko@650
   166
}
melunko@650
   167
renatofilho@754
   168
static          gboolean
renatofilho@750
   169
_ls_channels(ls_options_t * options)
melunko@650
   170
{
renatofilho@754
   171
    GMythEPG       *epg;
renatofilho@754
   172
    gint            length;
renatofilho@754
   173
    GList          *clist,
renatofilho@754
   174
                   *ch;
melunko@650
   175
renatofilho@754
   176
    g_return_val_if_fail(options != NULL, FALSE);
renatofilho@754
   177
    g_return_val_if_fail(options->b_info != NULL, FALSE);
melunko@650
   178
melunko@650
   179
renatofilho@754
   180
    epg = gmyth_epg_new();
renatofilho@754
   181
    if (!gmyth_epg_connect(epg, options->b_info)) {
renatofilho@754
   182
        g_object_unref(epg);
renatofilho@754
   183
        return FALSE;
renatofilho@754
   184
    }
melunko@650
   185
renatofilho@754
   186
    length = gmyth_epg_get_channel_list(epg, &clist);
renatofilho@754
   187
    for (ch = clist; ch != NULL; ch = ch->next) {
renatofilho@754
   188
        GMythChannelInfo *info = (GMythChannelInfo *) ch->data;
melunko@650
   189
renatofilho@754
   190
        if ((info->channel_name == NULL) || (info->channel_num == NULL)) {
renatofilho@754
   191
            continue;
renatofilho@754
   192
        }
melunko@650
   193
renatofilho@754
   194
        g_print("%s\t\t%s\n", info->channel_num->str,
renatofilho@754
   195
                info->channel_name->str);
renatofilho@754
   196
    }
melunko@650
   197
renatofilho@754
   198
    gmyth_free_channel_list(clist);
renatofilho@754
   199
    gmyth_epg_disconnect(epg);
renatofilho@754
   200
    g_object_unref(epg);
renatofilho@750
   201
renatofilho@754
   202
    return TRUE;
melunko@650
   203
}
melunko@650
   204
melunko@650
   205
int
renatofilho@750
   206
main(int argc, char *argv[])
melunko@650
   207
{
renatofilho@754
   208
    gboolean        res = FALSE;
renatofilho@754
   209
    ls_options_t   *options;
melunko@650
   210
renatofilho@754
   211
    g_type_init();
renatofilho@754
   212
    g_thread_init(NULL);
melunko@650
   213
renatofilho@754
   214
    options = _ls_options_new();
renatofilho@754
   215
    res = _parse_args(argc, argv, options);
renatofilho@754
   216
    if (!res) {
renatofilho@754
   217
        g_printerr("Argument invalid. Type --help\n");
renatofilho@754
   218
        return 1;
renatofilho@754
   219
    }
melunko@650
   220
renatofilho@754
   221
    if (options->list_channels)
renatofilho@754
   222
        res = _ls_channels(options);
renatofilho@754
   223
    else
renatofilho@754
   224
        res = _ls_recorded_files(options);
melunko@650
   225
renatofilho@754
   226
    _ls_options_free(options);
melunko@650
   227
renatofilho@754
   228
    return 0;
melunko@650
   229
}