m_media_new: Store input URL in unescaped form
[libquvi.git] / examples / supports.c
blob32d6c1384d6f8674a9d9a4f164dc560dd056728c
1 /* libquvi
2 * Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <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 #include "config.h"
23 #include <locale.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <quvi.h>
29 #include "examples.h"
31 struct _opts_s
33 gboolean autoproxy;
34 gboolean verbose;
35 gboolean online;
36 gchar *type;
37 gchar **url;
40 static struct _opts_s opts;
42 static const GOptionEntry entries[] =
45 "type", 't', 0, G_OPTION_ARG_STRING, &opts.type,
46 "Script type", "TYPE"
49 "online", 's', 0, G_OPTION_ARG_NONE, &opts.online,
50 "Check online", NULL
53 "autoproxy", 'a', 0, G_OPTION_ARG_NONE, &opts.autoproxy,
54 "Enable the autoproxy feature", NULL
57 "verbose", 'v', 0, G_OPTION_ARG_NONE, &opts.verbose,
58 "Verbose libcurl output", NULL
61 G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &opts.url, "URL"
63 {NULL, 0, 0, 0, NULL, NULL, NULL}
66 struct _type_lookup_s
68 QuviSupportsType to;
69 const gchar *from;
72 static const struct _type_lookup_s type_conv[] =
74 {QUVI_SUPPORTS_TYPE_PLAYLIST, "playlist"},
75 {QUVI_SUPPORTS_TYPE_SUBTITLE, "subtitle"},
76 {QUVI_SUPPORTS_TYPE_MEDIA, "media"},
77 {QUVI_SUPPORTS_TYPE_ANY, "any"},
78 {0, NULL}
81 static gchar **type_sv()
83 gchar **r;
84 gint i,j;
86 i=0;
87 while (type_conv[i].from != NULL) ++i;
88 r = g_new(gchar*, i+1);
90 i=j=0;
91 while (type_conv[j].from != NULL)
92 r[i++] = g_strdup(type_conv[j++].from);
93 r[i] = NULL;
95 return (r);
98 static gboolean chk_type_values()
100 gchar **v, *s;
101 gboolean r;
103 v = type_sv();
104 r = examples_chk_val_s(opts.type, v, &s);
105 if (r == FALSE)
107 g_printerr(
108 "error: invalid value (`%s') for the option `--type'\n", s);
111 g_strfreev(v);
112 v = NULL;
114 return (r);
117 static QuviSupportsType type_n()
119 gint i;
120 for (i=0; type_conv[i].from != NULL; ++i)
122 if (g_strcmp0(opts.type, type_conv[i].from) == 0)
123 return (type_conv[i].to);
125 return (QUVI_SUPPORTS_TYPE_MEDIA);
128 static gint opts_new(gint argc, gchar **argv)
130 GOptionContext *c;
131 GError *e;
132 gint r;
134 c = g_option_context_new("URL");
135 r = EXIT_SUCCESS;
136 e = NULL;
138 g_option_context_set_help_enabled(c, TRUE);
139 g_option_context_add_main_entries(c, entries, NULL);
141 if (g_option_context_parse(c, &argc, &argv, &e) == FALSE)
143 g_printerr("error: %s\n", e->message);
144 g_error_free(e);
145 r = EXIT_FAILURE;
146 e = NULL;
149 g_option_context_free(c);
150 c = NULL;
152 /* Set default values. */
154 if (opts.type == NULL)
155 opts.type = g_strdup("any");
157 /* Check input. */
159 if (chk_type_values() == FALSE)
160 return (EXIT_FAILURE);
162 if (opts.url == NULL)
164 g_printerr("error: no input URL\n");
165 return (EXIT_FAILURE);
168 return (r);
171 static void opts_free()
173 g_strfreev(opts.url);
174 opts.url = NULL;
176 g_free(opts.type);
177 opts.type = NULL;
180 static QuviSupportsMode mode = QUVI_SUPPORTS_MODE_OFFLINE;
181 static QuviSupportsType type = QUVI_SUPPORTS_TYPE_ANY;
183 static void chk_support(const gchar *url)
185 const QuviBoolean r = quvi_supports(q, url, mode, type);
187 /* Always check for any network errors with QUVI_SUPPORTS_MODE_ONLINE. */
188 if (r == FALSE && mode == QUVI_SUPPORTS_MODE_ONLINE)
190 const glong ec = quvi_errcode(q);
191 if (ec != QUVI_ERROR_NO_SUPPORT)
193 g_printerr("\nerror: %s\n", quvi_errmsg(q));
194 return;
197 g_print("%s: %s\n", url, (r == QUVI_TRUE) ? "yes":"no");
200 typedef quvi_callback_status qcs;
202 gint main(gint argc, gchar **argv)
204 gint i,r;
206 g_assert(q == NULL);
208 memset(&opts, 0, sizeof(struct _opts_s));
209 setlocale(LC_ALL, "");
211 r = opts_new(argc, argv);
212 if (r != EXIT_SUCCESS)
213 return (r);
215 q = quvi_new();
216 examples_exit_if_error();
218 if (opts.autoproxy == TRUE)
219 examples_enable_autoproxy();
221 if (opts.verbose == TRUE)
222 examples_enable_verbose();
224 if (opts.online == TRUE)
225 mode = QUVI_SUPPORTS_MODE_ONLINE;
227 quvi_set(q, QUVI_OPTION_CALLBACK_STATUS, (qcs) examples_status);
228 type = type_n();
230 g_printerr("[%s] type=%s (0x%x), mode=0x%x\n",
231 __func__, opts.type, type, mode);
233 for (i=0; opts.url[i] != NULL; ++i)
234 chk_support(opts.url[i]);
236 opts_free();
237 examples_cleanup();
239 g_assert(q == NULL);
240 return (r);
243 /* vim: set ts=2 sw=2 tw=72 expandtab: */