src/api/: Use intended license header
[libquvi.git] / src / api / playlist_get.c
blob138ab7e47ece06adb66eb1da1ec78e9f99cc8e46
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
20 /** @file playlist_get.c */
22 #include "config.h"
24 #include <glib.h>
26 #include "quvi.h"
27 /* -- */
28 #include "_quvi_s.h"
29 #include "_quvi_playlist_s.h"
31 /* Advances the current media pointer to the first media if undefined. */
32 static void _chk_curr_media(_quvi_playlist_t qp, _quvi_playlist_media_t *qpm)
34 if (qp->curr.media == NULL)
35 quvi_playlist_media_next(qp);
37 if (qp->curr.media != NULL)
38 *qpm = (_quvi_playlist_media_t) qp->curr.media->data;
41 static const gchar empty[] = "";
43 static QuviError _playlist_get(_quvi_playlist_t qp,
44 QuviPlaylistProperty n, ...)
46 _quvi_playlist_media_t qpm;
47 QuviError rc;
48 gdouble *dp;
49 va_list arg;
50 gchar **sp;
51 glong *lp;
52 gint type;
54 va_start(arg, n);
55 type = QUVI_PLAYLIST_PROPERTY_TYPE_MASK & (gint) n;
57 dp = NULL;
58 sp = NULL;
59 lp = NULL;
61 rc = QUVI_OK;
62 qpm = NULL;
64 switch (type)
66 case QUVI_PLAYLIST_PROPERTY_TYPE_STRING:
67 sp = va_arg(arg, gchar**);
68 if (sp == NULL)
69 rc = QUVI_ERROR_INVALID_ARG;
70 break;
71 case QUVI_PLAYLIST_PROPERTY_TYPE_LONG:
72 lp = va_arg(arg, glong*);
73 if (lp == NULL)
74 rc = QUVI_ERROR_INVALID_ARG;
75 break;
76 case QUVI_PLAYLIST_PROPERTY_TYPE_DOUBLE:
77 dp = va_arg(arg, gdouble*);
78 if (dp == NULL)
79 rc = QUVI_ERROR_INVALID_ARG;
80 break;
81 default:
82 rc = QUVI_ERROR_INVALID_ARG;
83 break;
85 va_end(arg);
87 if (rc != QUVI_OK)
88 return (rc);
90 switch (n)
92 case QUVI_PLAYLIST_PROPERTY_THUMBNAIL_URL:
93 *sp = qp->url.thumbnail->str;
94 break;
96 case QUVI_PLAYLIST_PROPERTY_TITLE:
97 *sp = qp->title->str;
98 break;
100 case QUVI_PLAYLIST_PROPERTY_ID:
101 *sp = qp->id.playlist->str;
102 break;
105 * (Playlist) Media properties.
107 * quvi_playlist_get is expected to return the property values for
108 * the first media the playlist script returned, just like
109 * quvi_media_get does with media scripts. We must mimic this
110 * behaviour by first attempting to move to the first item in the
111 * playlist media list.
113 * Unlike with quvi_media_get, playlist scripts are allowed to return
114 * nothing. Media scripts are expected to return at least one media
115 * stream.
117 * This means that if the playlist script did not return any such
118 * properties (e.g. media URL):
119 * - returned strings must be set to empty ("")
120 * - returned numeric values must be set to 0
123 case QUVI_PLAYLIST_MEDIA_PROPERTY_TITLE:
124 _chk_curr_media(qp, &qpm);
125 *sp = (qpm != NULL) ? qpm->title->str : (gchar*) empty;
126 break;
128 case QUVI_PLAYLIST_MEDIA_PROPERTY_URL:
129 _chk_curr_media(qp, &qpm);
130 *sp = (qpm != NULL) ? qpm->url->str : (gchar*) empty;
131 break;
133 case QUVI_PLAYLIST_MEDIA_PROPERTY_DURATION_MS:
134 _chk_curr_media(qp, &qpm);
135 *dp = (qpm != NULL) ? qpm->duration_ms : 0;
136 break;
138 default:
139 rc = QUVI_ERROR_INVALID_ARG;
140 break;
142 return (rc);
145 /** @brief Return a playlist property
146 @sa @ref parse_playlist
147 @ingroup playlistprop
149 void quvi_playlist_get(quvi_playlist_t handle,
150 QuviPlaylistProperty property, ...)
152 _quvi_playlist_t qp;
153 va_list arg;
154 gpointer p;
155 _quvi_t q;
157 /* If G_DISABLE_CHECKS is defined then the check is not performed. */
158 g_return_if_fail(handle != NULL);
160 va_start(arg, property);
161 p = va_arg(arg, gpointer);
162 va_end(arg);
164 qp = (_quvi_playlist_t) handle;
165 q = qp->handle.quvi;
167 q->status.rc = _playlist_get(qp, property, p);
170 /* vim: set ts=2 sw=2 tw=72 expandtab: */