man1: Revise Makefile.am for a2x
[quvi-tool.git] / src / print / enum_print.c
bloba9015989206a12a5673fe514119451f1795d8b18
1 /* quvi
2 * Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of quvi <http://quvi.sourceforge.net/>.
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
21 /* This module prints directly to std{out,err}. */
23 #include "config.h"
25 #include <stdlib.h>
26 #include <glib/gprintf.h>
27 #include <glib/gi18n.h>
28 #include <quvi.h>
30 #include "lutil.h"
31 /* -- */
32 #include "lprint.h"
34 struct enum_s
36 quvi_media_t qm;
37 quvi_t q;
40 typedef struct enum_s *enum_t;
42 static gint _enum_handle_new(quvi_t q, quvi_media_t qm, gpointer *dst)
44 enum_t s;
46 g_assert(dst != NULL);
48 s = g_new0(struct enum_s, 1);
49 s->qm = qm;
50 s->q = q;
51 *dst = s;
53 return (EXIT_SUCCESS);
56 static gint _enum_handle_free(gpointer p, const gint r)
58 g_free(p);
59 return (r);
62 static gint _print(const enum_t p, const lutilPropertyType pt,
63 const gchar *n, const gchar *s, const gdouble d)
65 gint r;
67 r = lutil_chk_property_ok(p->q, pt, n, lprint_enum_errmsg);
68 if (r != EXIT_SUCCESS)
69 return (EXIT_FAILURE);
71 if (s != NULL)
72 g_print("%s=%s\n", n, s);
73 else
74 g_print("%s=%.0f\n", n, d);
76 return (EXIT_SUCCESS);
79 void lprint_enum_errmsg(const gchar *fmt, ...)
81 va_list args;
82 gchar *s;
84 va_start(args, fmt);
85 if (g_vasprintf(&s, fmt, args) >0)
87 g_printerr(_("error: %s\n"), s);
88 g_free(s);
90 va_end(args);
93 typedef lprint_cb_errmsg cem;
95 /* media */
97 gint lprint_enum_media_new(quvi_t q, quvi_media_t qm, gpointer *dst)
99 return (_enum_handle_new(q, qm, dst));
102 void lprint_enum_media_free(gpointer data)
104 _enum_handle_free(data, -1);
107 gint lprint_enum_media_print_buffer(gpointer data)
109 /* Nothing to buffer: everything gets printed to stdout immediately */
110 return (EXIT_SUCCESS);
113 static gint _mp_s(const enum_t p, const QuviMediaProperty qmp, const gchar *n)
115 gchar *s = NULL;
116 quvi_media_get(p->qm, qmp, &s);
117 return (_print(p, UTIL_PROPERTY_TYPE_MEDIA, n, s, -1));
120 #define _print_mp_s(n)\
121 do {\
122 if (_mp_s(p, n, #n) != EXIT_SUCCESS)\
123 return (EXIT_FAILURE);\
124 } while (0)
126 static gint _mp_d(const enum_t p, const QuviMediaProperty qmp, const gchar *n)
128 gdouble d = 0;
129 quvi_media_get(p->qm, qmp, &d);
130 return (_print(p, UTIL_PROPERTY_TYPE_MEDIA, n, NULL, d));
133 #define _print_mp_d(n)\
134 do {\
135 if (_mp_d(p, n, #n) != EXIT_SUCCESS)\
136 return (EXIT_FAILURE);\
137 } while (0)
139 static gint _mi_s(const enum_t p, const quvi_http_metainfo_t qmi,
140 const QuviHTTPMetaInfoProperty qmip, const gchar *n)
142 gchar *s = NULL;
143 quvi_http_metainfo_get(qmi, qmip, &s);
144 return (_print(p, UTIL_PROPERTY_TYPE_HTTP_METAINFO, n, s, -1));
147 #define _print_mi_s(n)\
148 do {\
149 if (_mi_s(p, qmi, n, #n) != EXIT_SUCCESS)\
150 return (EXIT_FAILURE);\
151 } while (0)
153 static gint _mi_d(const enum_t p, const quvi_http_metainfo_t qmi,
154 const QuviMediaProperty qmip, const gchar *n)
156 gdouble d = 0;
157 quvi_http_metainfo_get(qmi, qmip, &d);
158 return (_print(p, UTIL_PROPERTY_TYPE_HTTP_METAINFO, n, NULL, d));
161 #define _print_mi_d(n)\
162 do {\
163 if (_mi_d(p, qmi, n, #n) != EXIT_SUCCESS)\
164 return (EXIT_FAILURE);\
165 } while (0)
167 static gint
168 _print_media_stream_properties(const enum_t p, const quvi_http_metainfo_t qmi)
170 _print_mp_s(QUVI_MEDIA_STREAM_PROPERTY_VIDEO_ENCODING);
171 _print_mp_s(QUVI_MEDIA_STREAM_PROPERTY_AUDIO_ENCODING);
172 _print_mp_s(QUVI_MEDIA_STREAM_PROPERTY_CONTAINER);
173 _print_mp_s(QUVI_MEDIA_STREAM_PROPERTY_URL);
174 _print_mp_s(QUVI_MEDIA_STREAM_PROPERTY_ID);
176 _print_mp_d(QUVI_MEDIA_STREAM_PROPERTY_VIDEO_BITRATE_KBIT_S);
177 _print_mp_d(QUVI_MEDIA_STREAM_PROPERTY_AUDIO_BITRATE_KBIT_S);
178 _print_mp_d(QUVI_MEDIA_STREAM_PROPERTY_VIDEO_HEIGHT);
179 _print_mp_d(QUVI_MEDIA_STREAM_PROPERTY_VIDEO_WIDTH);
181 if (qmi != NULL)
183 _print_mi_s(QUVI_HTTP_METAINFO_PROPERTY_FILE_EXTENSION);
184 _print_mi_s(QUVI_HTTP_METAINFO_PROPERTY_CONTENT_TYPE);
185 _print_mi_d(QUVI_HTTP_METAINFO_PROPERTY_LENGTH_BYTES);
187 return (EXIT_SUCCESS);
190 #undef _print_mi_s
191 #undef _print_mi_d
193 gint
194 lprint_enum_media_stream_properties(quvi_http_metainfo_t qmi, gpointer data)
196 return (_print_media_stream_properties(data, qmi));
199 gint lprint_enum_media_streams_available(quvi_t q, quvi_media_t qm)
201 enum_t p;
202 gint r;
204 if (_enum_handle_new(q, qm, (gpointer*) &p) != EXIT_SUCCESS)
205 return (EXIT_FAILURE);
207 r = EXIT_SUCCESS;
208 while (quvi_media_stream_next(qm) == QUVI_TRUE && r ==EXIT_SUCCESS)
209 r = _print_media_stream_properties(p, NULL);
211 return (_enum_handle_free(p, r));
214 gint lprint_enum_media_properties(gpointer data)
216 enum_t p;
218 g_assert(data != NULL);
219 p = (enum_t) data;
221 _print_mp_s(QUVI_MEDIA_PROPERTY_THUMBNAIL_URL);
222 _print_mp_s(QUVI_MEDIA_PROPERTY_TITLE);
223 _print_mp_s(QUVI_MEDIA_PROPERTY_ID);
225 _print_mp_d(QUVI_MEDIA_PROPERTY_START_TIME_MS);
226 _print_mp_d(QUVI_MEDIA_PROPERTY_DURATION_MS);
228 return (EXIT_SUCCESS);
231 #undef _print_mp_s
232 #undef _print_mp_d
234 /* playlist */
236 gint lprint_enum_playlist_new(quvi_t q, gpointer *dst)
238 return (_enum_handle_new(q, NULL, dst));
241 void lprint_enum_playlist_free(gpointer data)
243 _enum_handle_free(data, -1);
246 gint lprint_enum_playlist_print_buffer(gpointer data)
248 /* Nothing to buffer: everything gets printed to stdout immediately */
249 return (EXIT_SUCCESS);
252 static gint _pp_s(const enum_t p, const quvi_playlist_t qp,
253 const QuviPlaylistProperty qpp, const gchar *n)
255 gchar *s = NULL;
256 quvi_playlist_get(qp, qpp, &s);
257 return (_print(p, UTIL_PROPERTY_TYPE_PLAYLIST, n, s, -1));
260 #define _print_pp_s(n)\
261 do {\
262 if (_pp_s(p, qp, n, #n) != EXIT_SUCCESS)\
263 return (EXIT_FAILURE);\
264 } while (0)
266 static gint _pp_d(const enum_t p, const quvi_playlist_t qp,
267 const QuviPlaylistProperty qpp, const gchar *n)
269 gdouble d = 0;
270 quvi_playlist_get(qp, qpp, &d);
271 return (_print(p, UTIL_PROPERTY_TYPE_PLAYLIST, n, NULL, d));
274 #define _print_pp_d(n)\
275 do {\
276 if (_pp_d(p, qp, n, #n) != EXIT_SUCCESS)\
277 return (EXIT_FAILURE);\
278 } while (0)
280 gint lprint_enum_playlist_properties(quvi_playlist_t qp, gpointer data)
282 enum_t p;
284 g_assert(data != NULL);
285 p = (enum_t) data;
287 _print_pp_s(QUVI_PLAYLIST_PROPERTY_THUMBNAIL_URL);
288 _print_pp_s(QUVI_PLAYLIST_PROPERTY_TITLE);
289 _print_pp_s(QUVI_PLAYLIST_PROPERTY_ID);
291 while (quvi_playlist_media_next(qp) == QUVI_TRUE)
293 _print_pp_d(QUVI_PLAYLIST_MEDIA_PROPERTY_DURATION_MS);
294 _print_pp_s(QUVI_PLAYLIST_MEDIA_PROPERTY_TITLE);
295 _print_pp_s(QUVI_PLAYLIST_MEDIA_PROPERTY_URL);
297 return (EXIT_SUCCESS);
300 #undef _print_pp_s
301 #undef _print_pp_d
303 /* scan */
305 gint lprint_enum_scan_new(quvi_t q, gpointer *dst)
307 return (_enum_handle_new(q, NULL, dst));
310 void lprint_enum_scan_free(gpointer data)
312 _enum_handle_free(data, -1);
315 gint lprint_enum_scan_print_buffer(gpointer data)
317 /* Nothing to buffer: everything gets printed to stdout immediately */
318 return (EXIT_SUCCESS);
321 gint lprint_enum_scan_properties(quvi_scan_t qs, gpointer data)
323 const gchar *s;
325 g_assert(qs != NULL);
328 * 'scan' interface does not have any "properties". Use the function
329 * name instead of an enum.
332 while ( (s = quvi_scan_next_media_url(qs)) != NULL)
333 g_print("quvi_scan_next_media_url=%s\n", s);
335 return (EXIT_SUCCESS);
338 /* subtitle */
340 gint lprint_enum_subtitle_new(quvi_t q, gpointer *dst)
342 return (_enum_handle_new(q, NULL, dst));
345 void lprint_enum_subtitle_free(gpointer data)
347 _enum_handle_free(data, -1);
350 gint lprint_enum_subtitle_print_buffer(gpointer data)
352 /* Nothing to buffer: everything gets printed to stdout immediately */
353 return (EXIT_SUCCESS);
356 static gint _stp_d(const enum_t p, const quvi_subtitle_type_t qst,
357 const QuviSubtitleTypeProperty qstp, const gchar *n)
359 gdouble d = 0;
360 quvi_subtitle_type_get(qst, qstp, &d);
361 return (_print(p, UTIL_PROPERTY_TYPE_SUBTITLE_TYPE, n, NULL, d));
364 #define _print_stp_d(n)\
365 do {\
366 if (_stp_d(p, t, n, #n) != EXIT_SUCCESS)\
367 return (EXIT_FAILURE);\
368 } while (0)
370 static gint _slp_s(const enum_t p, const quvi_subtitle_lang_t qsl,
371 const QuviSubtitleLangProperty qslp, const gchar *n)
373 gchar *s = NULL;
374 quvi_subtitle_lang_get(qsl, qslp, &s);
375 return (_print(p, UTIL_PROPERTY_TYPE_SUBTITLE_LANGUAGE, n, s, -1));
378 #define _print_slp_s(n)\
379 do {\
380 if (_slp_s(p, l, n, #n) != EXIT_SUCCESS)\
381 return (EXIT_FAILURE);\
382 } while (0)
384 gint
385 lprint_enum_subtitle_lang_properties(quvi_subtitle_lang_t l, gpointer data)
387 enum_t p;
389 g_assert(data != NULL);
390 p = (enum_t) data;
392 _print_slp_s(QUVI_SUBTITLE_LANG_PROPERTY_TRANSLATED);
393 _print_slp_s(QUVI_SUBTITLE_LANG_PROPERTY_ORIGINAL);
394 _print_slp_s(QUVI_SUBTITLE_LANG_PROPERTY_CODE);
395 _print_slp_s(QUVI_SUBTITLE_LANG_PROPERTY_URL);
396 _print_slp_s(QUVI_SUBTITLE_LANG_PROPERTY_ID);
398 return (EXIT_SUCCESS);
401 gint lprint_enum_subtitles_available(quvi_t q, quvi_subtitle_t qsub)
403 quvi_subtitle_type_t t;
404 quvi_subtitle_lang_t l;
405 enum_t p;
406 gint r;
408 if (_enum_handle_new(q, NULL, (gpointer*) &p) != EXIT_SUCCESS)
409 return (EXIT_FAILURE);
411 r = EXIT_SUCCESS;
412 while ( (t = quvi_subtitle_type_next(qsub)) != NULL && r ==EXIT_SUCCESS)
414 _print_stp_d(QUVI_SUBTITLE_TYPE_PROPERTY_FORMAT);
415 _print_stp_d(QUVI_SUBTITLE_TYPE_PROPERTY_TYPE);
417 while ( (l = quvi_subtitle_lang_next(t)) != NULL && r ==EXIT_SUCCESS)
418 r = lprint_enum_subtitle_lang_properties(l, p);
420 return (_enum_handle_free(p, r));
423 #undef _print_stp_d
424 #undef _print_slp_s
426 /* vim: set ts=2 sw=2 tw=72 expandtab: */