[svn r804] Added function to retrieve backend details (total space, free space, etc)
9 #include "gmyth_backendinfo.h"
10 #include "gmyth_file_transfer.h"
11 #include "gmyth_livetv.h"
12 #include "gmyth_util.h"
13 #include "gmyth_common.h"
16 GMythBackendInfo *b_info;
21 static cat_options_t *
24 cat_options_t *options = g_new0(cat_options_t, 1);
25 options->b_info = gmyth_backend_info_new();
31 _cat_options_free(cat_options_t * options)
33 g_return_if_fail(options != NULL);
36 g_object_unref(options->b_info);
37 g_free(options->filename);
38 g_free(options->channel);
42 _parse_args(int argc, char *argv[], cat_options_t * options)
45 GOptionContext *context;
47 gchar *host_ip = NULL;
49 gchar *filename = NULL;
50 gchar *channel = NULL;
52 GOptionEntry entries[] = {
53 {"hostname", 'h', 0, G_OPTION_ARG_STRING, &host_ip,
54 "Mythtv backend hostname or " "IP address", "IP_ADDRESS"},
56 {"port", 'p', 0, G_OPTION_ARG_INT, &host_port,
57 "Mythtv backend port",
60 {"filename", 'f', 0, G_OPTION_ARG_STRING, &filename,
61 "Recorded file name available " "in the Mythtv backend", "FILE"},
63 {"channel", 'c', 0, G_OPTION_ARG_STRING, &channel,
64 "Mythtv channel number", "CHANNEL"},
69 g_return_val_if_fail(options != NULL, FALSE);
73 ("- loads a mythtv backend recorded file and prints "
74 "it on the standard output\n");
75 g_option_context_add_main_entries(context, entries, NULL);
76 g_option_context_parse(context, &argc, &argv, &error);
77 g_option_context_set_help_enabled(context, TRUE);
79 g_option_context_free(context);
81 if ((!host_ip) || (host_port == 0)) {
88 gmyth_backend_info_set_hostname(options->b_info, host_ip);
89 gmyth_backend_info_set_port(options->b_info, host_port);
91 options->filename = g_strdup(filename);
93 options->channel = g_strdup(channel);
103 _cat_recorded_file(cat_options_t * options)
105 GArray *array = NULL;
106 GMythFileTransfer *transfer;
110 g_return_val_if_fail(options != NULL, FALSE);
111 g_return_val_if_fail(options->b_info != NULL, FALSE);
112 g_return_val_if_fail(options->filename != NULL, FALSE);
114 if (!gmyth_util_file_exists(options->b_info, options->filename)) {
115 g_printerr("File %s was not found in the mythtv server\n",
120 transfer = gmyth_file_transfer_new(options->b_info);
121 if (!gmyth_file_transfer_open(transfer, options->filename)) {
122 g_printerr("File %s could not be opened\n", options->filename);
126 size = gmyth_file_transfer_get_filesize(transfer);
127 fprintf(stderr, "Size:%" G_GUINT64_FORMAT "\n", size);
129 array = g_array_new(FALSE, TRUE, sizeof(gchar));
131 while (total != size) {
132 GMythFileReadResult res;
134 res = gmyth_file_transfer_read(transfer, (GByteArray *) array,
136 64000 ? 64000 : (size - total),
138 if ((res != GMYTH_FILE_READ_OK) && (res != GMYTH_FILE_READ_EOF)) {
139 g_array_free(array, TRUE);
140 g_printerr("Error while reading the file: aborting!!\n");
144 fwrite(array->data, array->len, 1, stdout);
148 fprintf(stderr, "%" G_GUINT64_FORMAT "\n", total);
149 g_array_remove_range(array, 0, array->len);
153 gmyth_file_transfer_close(transfer);
154 g_array_free(array, TRUE);
155 g_object_unref(transfer);
161 _cat_channel(cat_options_t * options)
163 GMythLiveTV *livetv = NULL;
164 GMythFile *gmyth_file = NULL;
165 GArray *array = NULL;
167 gint file_transf_ret;
169 g_return_val_if_fail(options != NULL, FALSE);
170 g_return_val_if_fail(options->b_info != NULL, FALSE);
171 g_return_val_if_fail(options->channel != NULL, FALSE);
172 livetv = gmyth_livetv_new(options->b_info);
174 if (gmyth_livetv_channel_name_setup(livetv, options->channel) == FALSE) {
175 g_printerr("Could not setup remote livetv");
176 g_object_unref(livetv);
180 gmyth_file = GMYTH_FILE(gmyth_livetv_create_file_transfer(livetv));
181 if (gmyth_file == NULL) {
182 g_printerr("Could not open livetv recording file for transfer");
183 g_object_unref(livetv);
187 if (!gmyth_file_transfer_open(GMYTH_FILE_TRANSFER(gmyth_file),
188 livetv->uri != NULL ?
189 gmyth_uri_get_path(livetv->uri) :
190 livetv->proginfo->pathname->str)) {
192 g_printerr("Couldn't open MythTV. FileTransfer is NULL!\n");
196 array = g_array_new(FALSE, TRUE, sizeof(gchar));
198 while (((file_transf_ret = gmyth_file_transfer_read
199 (GMYTH_FILE_TRANSFER(gmyth_file),
200 (GByteArray *) array, 64000, TRUE)) == GMYTH_FILE_READ_OK) ||
201 file_transf_ret == GMYTH_FILE_READ_NEXT_PROG_CHAIN) {
203 fwrite(array->data, array->len, 1, stdout);
205 g_array_remove_range(array, 0, array->len);
207 g_main_context_iteration(g_main_context_default(), FALSE);
210 g_array_free(array, TRUE);
211 g_object_unref(gmyth_file);
212 g_object_unref(livetv);
218 main(int argc, char *argv[])
220 gboolean res = FALSE;
221 cat_options_t *options;
224 if (!g_thread_supported())
227 options = _cat_options_new();
228 res = _parse_args(argc, argv, options);
230 g_printerr("Argument invalid. Type --help\n");
234 if (options->filename)
235 res = _cat_recorded_file(options);
236 else if (options->channel)
237 res = _cat_channel(options);
240 ("Argument invalid. You must specify --filename or --channel.\n"
241 "Type --help for more information.\n");
243 _cat_options_free(options);