Update NEWS for v0.9.5
[quvi-tool.git] / src / print / rfc2483_print.c
bloba2269c210add332e8369fd5fe3164e49cbd38665
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/>.
22 * An URI list conforming to the text/uri-list mime type defined in
23 * RFC2483 into individual URIs.
25 * media: default/selected stream URL only
26 * streams: all media stream URLs
27 * scan: embedded media URLs only
28 * subtitle: language URLs only
29 * playlist: media URLs only
31 * This module prints directory to std{out,err}, and reuses the
32 * lprint_enum_errmsg function from the enum module.
35 #include "config.h"
37 #include <stdlib.h>
38 #include <glib/gprintf.h>
39 #include <glib/gi18n.h>
40 #include <quvi.h>
42 #include "lprint.h"
43 #include "lutil.h"
45 struct rfc2483_s
47 quvi_media_t qm;
48 quvi_t q;
51 typedef struct rfc2483_s *rfc2483_t;
53 static gint _rfc2483_handle_new(quvi_t q, quvi_media_t qm, gpointer *dst)
55 rfc2483_t n;
57 g_assert(dst != NULL);
59 n = g_new0(struct rfc2483_s, 1);
60 n->qm = qm;
61 n->q = q;
62 *dst = n;
64 return (EXIT_SUCCESS);
67 static gint _rfc2483_handle_free(gpointer data, const gint r)
69 g_free(data);
70 return (r);
73 extern const gchar *reserved_chars;
75 static gint _print(const rfc2483_t p, const lutilPropertyType pt,
76 const gchar *n, const gchar *s, const gdouble d,
77 const gboolean comment_out, const gboolean escape)
79 const gchar *h;
80 gint r;
82 r = lutil_chk_property_ok(p->q, pt, n, lprint_enum_errmsg);
83 if (r != EXIT_SUCCESS) /* Reuse the lprint_enum_errmsg. */
84 return (EXIT_FAILURE);
86 h = (comment_out == TRUE) ? "# ":"";
88 if (s != NULL)
90 if (escape == TRUE)
92 gchar *e = g_uri_escape_string(s, reserved_chars, FALSE);
93 g_print("%s%s\n", h, e);
94 g_free(e);
96 else
97 g_print("%s%s\n", h, s);
99 else
100 g_print("%s%.0f\n", h, d);
102 return (EXIT_SUCCESS);
105 /* media */
107 gint lprint_rfc2483_media_new(quvi_t q, quvi_media_t qm, gpointer *dst)
109 return (_rfc2483_handle_new(q, qm, dst));
112 void lprint_rfc2483_media_free(gpointer data)
114 _rfc2483_handle_free(data, -1);
117 gint lprint_rfc2483_media_print_buffer(gpointer data)
119 /* Nothing to buffer: everything gets printed to stdout immediately */
120 return (EXIT_SUCCESS);
123 typedef lprint_cb_errmsg cem;
125 static gint _mp_s(const rfc2483_t p, const QuviMediaProperty qmp,
126 const gchar *n, const gboolean c, const gboolean e)
128 gchar *s = NULL;
129 quvi_media_get(p->qm, qmp, &s);
130 return (_print(p, UTIL_PROPERTY_TYPE_MEDIA, n, s, -1, c, e));
133 #define _print_mp_s(n,c,e) /* c=commented-out, e=escaped */ \
134 do {\
135 if (_mp_s(p, n, #n, c, e) != EXIT_SUCCESS)\
136 return (EXIT_FAILURE);\
137 } while (0)
139 gint
140 lprint_rfc2483_media_stream_properties(quvi_http_metainfo_t qmi,
141 gpointer data)
143 rfc2483_t p = (rfc2483_t) data;
145 g_assert(data != NULL);
147 _print_mp_s(QUVI_MEDIA_STREAM_PROPERTY_ID, TRUE, FALSE);
148 _print_mp_s(QUVI_MEDIA_STREAM_PROPERTY_URL, FALSE, TRUE);
150 return (EXIT_SUCCESS);
153 gint lprint_rfc2483_media_streams_available(quvi_t q, quvi_media_t qm)
155 rfc2483_t p;
156 gint r;
158 if (_rfc2483_handle_new(q, qm, (gpointer*) &p) != EXIT_SUCCESS)
159 return (EXIT_FAILURE);
161 g_print(_("# Media streams\n#\n"));
163 r = EXIT_SUCCESS;
164 while (quvi_media_stream_next(qm) == QUVI_TRUE && r ==EXIT_SUCCESS)
165 r = lprint_rfc2483_media_stream_properties(NULL, p);
167 return (_rfc2483_handle_free(p, r));
170 #undef _print_mp_s
172 gint lprint_rfc2483_media_properties(gpointer data)
174 return (EXIT_SUCCESS);
177 /* playlist */
179 gint lprint_rfc2483_playlist_new(quvi_t q, gpointer *dst)
181 return (_rfc2483_handle_new(q, NULL, dst));
184 void lprint_rfc2483_playlist_free(gpointer data)
186 /* Nothing to release. */
189 gint lprint_rfc2483_playlist_print_buffer(gpointer data)
191 /* Nothing to buffer: everything gets printed to stdout immediately */
192 return (EXIT_SUCCESS);
195 static gint _pp_s(const rfc2483_t p, const quvi_playlist_t qp,
196 const QuviPlaylistProperty qpp, const gchar *n,
197 const gboolean c, const gboolean e)
199 gchar *s = NULL;
200 quvi_playlist_get(qp, qpp, &s);
201 return (_print(p, UTIL_PROPERTY_TYPE_PLAYLIST, n, s, -1, c, e));
204 #define _print_pp_s(n,c,e) /* c=commented-out, e=escaped */ \
205 do {\
206 if (_pp_s(p, qp, n, #n, c, e) != EXIT_SUCCESS)\
207 return (EXIT_FAILURE);\
208 } while (0)
210 static gint _pp_d(const rfc2483_t p, const quvi_playlist_t qp,
211 const QuviPlaylistProperty qpp, const gchar *n,
212 const gboolean c)
214 gdouble d = 0;
215 quvi_playlist_get(qp, qpp, &d);
216 return (_print(p, UTIL_PROPERTY_TYPE_PLAYLIST, n, NULL, d, c, FALSE));
219 #define _print_pp_d(n,c) /* c=commented-out */ \
220 do {\
221 if (_pp_d(p, qp, n, #n, c) != EXIT_SUCCESS)\
222 return (EXIT_FAILURE);\
223 } while (0)
225 gint lprint_rfc2483_playlist_properties(quvi_playlist_t qp, gpointer data)
227 rfc2483_t p = (rfc2483_t) data;
229 g_assert(data != NULL);
230 g_assert(qp != NULL);
232 g_print(_("# Playlist media URLs\n#\n"));
234 _print_pp_s(QUVI_PLAYLIST_PROPERTY_TITLE, TRUE, FALSE);
235 _print_pp_s(QUVI_PLAYLIST_PROPERTY_ID, TRUE, FALSE);
236 _print_pp_s(QUVI_PLAYLIST_PROPERTY_THUMBNAIL_URL, TRUE, FALSE);
238 g_print("#\n");
240 while (quvi_playlist_media_next(qp) == QUVI_TRUE)
242 _print_pp_s(QUVI_PLAYLIST_MEDIA_PROPERTY_TITLE, TRUE, FALSE);
243 _print_pp_d(QUVI_PLAYLIST_MEDIA_PROPERTY_DURATION_MS, TRUE);
244 _print_pp_s(QUVI_PLAYLIST_MEDIA_PROPERTY_URL, FALSE, TRUE);
246 return (EXIT_SUCCESS);
249 #undef _print_pp_s
251 /* scan */
253 gint lprint_rfc2483_scan_new(quvi_t q, gpointer *dst)
255 return (_rfc2483_handle_new(q, NULL, dst));
258 void lprint_rfc2483_scan_free(gpointer data)
260 _rfc2483_handle_free(data, -1);
263 gint lprint_rfc2483_scan_print_buffer(gpointer data)
265 /* Nothing to buffer: everything gets printed to stdout immediately */
266 return (EXIT_SUCCESS);
269 gint lprint_rfc2483_scan_properties(quvi_scan_t qs, gpointer data)
271 const gchar *s;
273 g_assert(qs != NULL);
275 /* 'scan' interface does not have any properties. */
277 g_print(_("# Embedded media URLs\n#\n"));
278 while ( (s = quvi_scan_next_media_url(qs)) != NULL)
280 gchar *e = g_uri_escape_string(s, reserved_chars, FALSE);
281 g_print("%s\n", e);
282 g_free(e);
284 return (EXIT_SUCCESS);
287 /* subtitle */
289 gint lprint_rfc2483_subtitle_new(quvi_t q, gpointer *dst)
291 return (_rfc2483_handle_new(q, NULL, dst));
294 void lprint_rfc2483_subtitle_free(gpointer data)
296 _rfc2483_handle_free(data, -1);
299 gint lprint_rfc2483_subtitle_print_buffer(gpointer data)
301 /* Nothing to buffer: everything gets printed to stdout immediately */
302 return (EXIT_SUCCESS);
305 static gint _slp_s(const rfc2483_t p, const quvi_subtitle_lang_t qsl,
306 const QuviSubtitleLangProperty qslp, const gchar *n,
307 const gboolean c, const gboolean e)
309 gchar *s = NULL;
310 quvi_subtitle_lang_get(qsl, qslp, &s);
311 return (_print(p, UTIL_PROPERTY_TYPE_SUBTITLE_LANGUAGE, n, s, -1, c, e));
314 #define _print_slp_s(n,c,e) /* c=commented-out, e=escaped */ \
315 do {\
316 if (_slp_s(p, l, n, #n, c, e) != EXIT_SUCCESS)\
317 return (EXIT_FAILURE);\
318 } while (0)
320 gint
321 lprint_rfc2483_subtitle_lang_properties(quvi_subtitle_lang_t l,
322 gpointer data)
324 rfc2483_t p;
326 g_assert(data != NULL);
327 p = (rfc2483_t) data;
329 _print_slp_s(QUVI_SUBTITLE_LANG_PROPERTY_ID, TRUE, FALSE);
330 _print_slp_s(QUVI_SUBTITLE_LANG_PROPERTY_URL, FALSE, TRUE);
332 return (EXIT_SUCCESS);
335 gint lprint_rfc2483_subtitles_available(quvi_t q, quvi_subtitle_t qsub)
337 quvi_subtitle_type_t t;
338 quvi_subtitle_lang_t l;
339 rfc2483_t p;
340 gint r;
342 if (_rfc2483_handle_new(q, NULL, (gpointer*) &p) != EXIT_SUCCESS)
343 return (EXIT_FAILURE);
345 g_print(_("# Subtitles\n#\n"));
347 r = EXIT_SUCCESS;
348 while ( (t = quvi_subtitle_type_next(qsub)) != NULL && r ==EXIT_SUCCESS)
350 while ( (l = quvi_subtitle_lang_next(t)) != NULL && r ==EXIT_SUCCESS)
351 r = lprint_rfc2483_subtitle_lang_properties(l, p);
353 return (_rfc2483_handle_free(p, r));
356 #undef _print_slp_s
358 /* vim: set ts=2 sw=2 tw=72 expandtab: */