1 /* libquvi
2  * Copyright (C) 2012  Toni Gundogdu <legatvs@gmail.com>
3  *
4  * This file is part of libquvi <http://quvi.sourceforge.net/>.
5  *
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.
10  *
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.
15  *
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/>.
19  */
20 
21 /** @file playlist_get.c */
22 
23 #include "config.h"
24 
25 #include <glib.h>
26 
27 #include "quvi.h"
28 /* -- */
29 #include "_quvi_s.h"
30 #include "_quvi_playlist_s.h"
31 
32 /* Advances the current media pointer to the first media if undefined. */
_chk_curr_media(_quvi_playlist_t qp,_quvi_playlist_media_t * qpm)33 static void _chk_curr_media(_quvi_playlist_t qp, _quvi_playlist_media_t *qpm)
34 {
35   if (qp->curr.media == NULL)
36     quvi_playlist_media_next(qp);
37 
38   if (qp->curr.media != NULL)
39     *qpm = (_quvi_playlist_media_t) qp->curr.media->data;
40 }
41 
42 static const gchar empty[] = "";
43 
_playlist_get(_quvi_playlist_t qp,QuviPlaylistProperty n,...)44 static QuviError _playlist_get(_quvi_playlist_t qp,
45                                QuviPlaylistProperty n, ...)
46 {
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;
54 
55   va_start(arg, n);
56   type = QUVI_PLAYLIST_PROPERTY_TYPE_MASK & (gint) n;
57 
58   dp = NULL;
59   sp = NULL;
60   lp = NULL;
61 
62   rc = QUVI_OK;
63   qpm = NULL;
64 
65   switch (type)
66     {
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;
85     }
86   va_end(arg);
87 
88   if (rc != QUVI_OK)
89     return (rc);
90 
91   switch (n)
92     {
93     case QUVI_PLAYLIST_PROPERTY_THUMBNAIL_URL:
94       *sp = qp->url.thumbnail->str;
95       break;
96 
97     case QUVI_PLAYLIST_PROPERTY_TITLE:
98       *sp = qp->title->str;
99       break;
100 
101     case QUVI_PLAYLIST_PROPERTY_ID:
102       *sp = qp->id.playlist->str;
103       break;
104 
105       /*
106        * (Playlist) Media properties.
107        *
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.
113        *
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.
117        *
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
122        */
123 
124     case QUVI_PLAYLIST_MEDIA_PROPERTY_TITLE:
125       _chk_curr_media(qp, &qpm);
126       *sp = (qpm != NULL) ? qpm->title->str : (gchar*) empty;
127       break;
128 
129     case QUVI_PLAYLIST_MEDIA_PROPERTY_URL:
130       _chk_curr_media(qp, &qpm);
131       *sp = (qpm != NULL) ? qpm->url->str : (gchar*) empty;
132       break;
133 
134     case QUVI_PLAYLIST_MEDIA_PROPERTY_DURATION_MS:
135       _chk_curr_media(qp, &qpm);
136       *dp = (qpm != NULL) ? qpm->duration_ms : 0;
137       break;
138 
139     default:
140       rc = QUVI_ERROR_INVALID_ARG;
141       break;
142     }
143   return (rc);
144 }
145 
146 /** @brief Return a playlist property
147 @sa @ref parse_playlist
148 @ingroup playlistprop
149 @note URLs will be returned in the escaped form
150 */
quvi_playlist_get(quvi_playlist_t handle,QuviPlaylistProperty property,...)151 void quvi_playlist_get(quvi_playlist_t handle,
152                        QuviPlaylistProperty property, ...)
153 {
154   _quvi_playlist_t qp;
155   va_list arg;
156   gpointer p;
157   _quvi_t q;
158 
159   /* If G_DISABLE_CHECKS is defined then the check is not performed. */
160   g_return_if_fail(handle != NULL);
161 
162   va_start(arg, property);
163   p = va_arg(arg, gpointer);
164   va_end(arg);
165 
166   qp = (_quvi_playlist_t) handle;
167   q = qp->handle.quvi;
168 
169   q->status.rc = _playlist_get(qp, property, p);
170 }
171 
172 /* vim: set ts=2 sw=2 tw=72 expandtab: */
173