Add misc/url.* with m_url_(un)escaped_form
[libquvi.git] / src / api / playlist_get.c
blobf70e61195560eee9734d1ffab49dd811c0c4a902
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <http://quvi.sourceforge.net/>.
6 * This library 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 library 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 library. If not, see
18 * <http://www.gnu.org/licenses/>.
21 /** @file playlist_get.c */
23 #include "config.h"
25 #include <glib.h>
27 #include "quvi.h"
28 /* -- */
29 #include "_quvi_s.h"
30 #include "_quvi_playlist_s.h"
32 /* Advances the current media pointer to the first media if undefined. */
33 static void _chk_curr_media(_quvi_playlist_t qp, _quvi_playlist_media_t *qpm)
35 if (qp->curr.media == NULL)
36 quvi_playlist_media_next(qp);
38 if (qp->curr.media != NULL)
39 *qpm = (_quvi_playlist_media_t) qp->curr.media->data;
42 static const gchar empty[] = "";
44 static QuviError _playlist_get(_quvi_playlist_t qp,
45 QuviPlaylistProperty n, ...)
47 _quvi_playlist_media_t qpm;
48 QuviError rc;
49 gdouble *dp;
50 va_list arg;
51 gchar **sp;
52 glong *lp;
53 gint type;
55 va_start(arg, n);
56 type = QUVI_PLAYLIST_PROPERTY_TYPE_MASK & (gint) n;
58 dp = NULL;
59 sp = NULL;
60 lp = NULL;
62 rc = QUVI_OK;
63 qpm = NULL;
65 switch (type)
67 case QUVI_PLAYLIST_PROPERTY_TYPE_STRING:
68 sp = va_arg(arg, gchar**);
69 if (sp == NULL)
70 rc = QUVI_ERROR_INVALID_ARG;
71 break;
72 case QUVI_PLAYLIST_PROPERTY_TYPE_LONG:
73 lp = va_arg(arg, glong*);
74 if (lp == NULL)
75 rc = QUVI_ERROR_INVALID_ARG;
76 break;
77 case QUVI_PLAYLIST_PROPERTY_TYPE_DOUBLE:
78 dp = va_arg(arg, gdouble*);
79 if (dp == NULL)
80 rc = QUVI_ERROR_INVALID_ARG;
81 break;
82 default:
83 rc = QUVI_ERROR_INVALID_ARG;
84 break;
86 va_end(arg);
88 if (rc != QUVI_OK)
89 return (rc);
91 switch (n)
93 case QUVI_PLAYLIST_PROPERTY_THUMBNAIL_URL:
94 *sp = qp->url.thumbnail->str;
95 break;
97 case QUVI_PLAYLIST_PROPERTY_TITLE:
98 *sp = qp->title->str;
99 break;
101 case QUVI_PLAYLIST_PROPERTY_ID:
102 *sp = qp->id.playlist->str;
103 break;
106 * (Playlist) Media properties.
108 * quvi_playlist_get is expected to return the property values for
109 * the first media the playlist script returned, just like
110 * quvi_media_get does with media scripts. We must mimic this
111 * behaviour by first attempting to move to the first item in the
112 * playlist media list.
114 * Unlike with quvi_media_get, playlist scripts are allowed to return
115 * nothing. Media scripts are expected to return at least one media
116 * stream.
118 * This means that if the playlist script did not return any such
119 * properties (e.g. media URL):
120 * - returned strings must be set to empty ("")
121 * - returned numeric values must be set to 0
124 case QUVI_PLAYLIST_MEDIA_PROPERTY_TITLE:
125 _chk_curr_media(qp, &qpm);
126 *sp = (qpm != NULL) ? qpm->title->str : (gchar*) empty;
127 break;
129 case QUVI_PLAYLIST_MEDIA_PROPERTY_URL:
130 _chk_curr_media(qp, &qpm);
131 *sp = (qpm != NULL) ? qpm->url->str : (gchar*) empty;
132 break;
134 case QUVI_PLAYLIST_MEDIA_PROPERTY_DURATION_MS:
135 _chk_curr_media(qp, &qpm);
136 *dp = (qpm != NULL) ? qpm->duration_ms : 0;
137 break;
139 default:
140 rc = QUVI_ERROR_INVALID_ARG;
141 break;
143 return (rc);
146 /** @brief Return a playlist property
147 @sa @ref parse_playlist
148 @ingroup playlistprop
150 void quvi_playlist_get(quvi_playlist_t handle,
151 QuviPlaylistProperty property, ...)
153 _quvi_playlist_t qp;
154 va_list arg;
155 gpointer p;
156 _quvi_t q;
158 /* If G_DISABLE_CHECKS is defined then the check is not performed. */
159 g_return_if_fail(handle != NULL);
161 va_start(arg, property);
162 p = va_arg(arg, gpointer);
163 va_end(arg);
165 qp = (_quvi_playlist_t) handle;
166 q = qp->handle.quvi;
168 q->status.rc = _playlist_get(qp, property, p);
171 /* vim: set ts=2 sw=2 tw=72 expandtab: */