FIX: examples/supports.c: type_n: Return correct type
[libquvi.git] / examples / supports.c
blob133c961dda27d387103050eb97a29f225c756a4d
1 /* libquvi
2 * Copyright (C) 2012 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_MEDIA, "media"},
76 {QUVI_SUPPORTS_TYPE_ANY, "any"},
77 {0, NULL}
80 static gchar **type_sv()
82 gchar **r;
83 gint i,j;
85 i=0;
86 while (type_conv[i].from != NULL) ++i;
87 r = g_new(gchar*, i+1);
89 i=j=0;
90 while (type_conv[j].from != NULL)
91 r[i++] = g_strdup(type_conv[j++].from);
92 r[i] = NULL;
94 return (r);
97 static gboolean chk_type_values()
99 gchar **v, *s;
100 gboolean r;
102 v = type_sv();
103 r = examples_chk_val_s(opts.type, v, &s);
104 if (r == FALSE)
106 g_printerr(
107 "error: invalid value (`%s') for the option `--type'\n", s);
110 g_strfreev(v);
111 v = NULL;
113 return (r);
116 static QuviSupportsType type_n()
118 gint i;
119 for (i=0; type_conv[i].from != NULL; ++i)
121 if (g_strcmp0(opts.type, type_conv[i].from) == 0)
122 return (type_conv[i].to);
124 return (QUVI_SUPPORTS_TYPE_MEDIA);
127 static gint opts_new(gint argc, gchar **argv)
129 GOptionContext *c;
130 GError *e;
131 gint r;
133 c = g_option_context_new("URL");
134 r = EXIT_SUCCESS;
135 e = NULL;
137 g_option_context_set_help_enabled(c, TRUE);
138 g_option_context_add_main_entries(c, entries, NULL);
140 if (g_option_context_parse(c, &argc, &argv, &e) == FALSE)
142 g_printerr("error: %s\n", e->message);
143 g_error_free(e);
144 r = EXIT_FAILURE;
145 e = NULL;
148 g_option_context_free(c);
149 c = NULL;
151 /* Set default values. */
153 if (opts.type == NULL)
154 opts.type = g_strdup("any");
156 /* Check input. */
158 if (chk_type_values() == FALSE)
159 return (EXIT_FAILURE);
161 if (opts.url == NULL)
163 g_printerr("error: no input URL\n");
164 return (EXIT_FAILURE);
167 return (r);
170 static void opts_free()
172 g_strfreev(opts.url);
173 opts.url = NULL;
175 g_free(opts.type);
176 opts.type = NULL;
179 static QuviSupportsMode mode = QUVI_SUPPORTS_MODE_OFFLINE;
180 static QuviSupportsType type = QUVI_SUPPORTS_TYPE_ANY;
182 static void chk_support(const gchar *url)
184 const QuviBoolean r = quvi_supports(q, url, mode, type);
186 /* Always check for any network errors with QUVI_SUPPORTS_MODE_ONLINE. */
187 if (r == FALSE && mode == QUVI_SUPPORTS_MODE_ONLINE)
189 glong ec = 0;
190 quvi_get(q, QUVI_INFO_ERROR_CODE, &ec);
192 if (ec != QUVI_ERROR_NO_SUPPORT)
194 g_printerr("\nerror: %s\n", quvi_errmsg(q));
195 return;
198 g_print("%s: %s\n", url, (r == QUVI_TRUE) ? "yes":"no");
201 typedef quvi_callback_status qcs;
203 gint main(gint argc, gchar **argv)
205 gint i,r;
207 g_assert(q == NULL);
209 memset(&opts, 0, sizeof(struct _opts_s));
210 setlocale(LC_ALL, "");
212 r = opts_new(argc, argv);
213 if (r != EXIT_SUCCESS)
214 return (r);
216 q = quvi_new();
217 examples_exit_if_error();
219 if (opts.autoproxy == TRUE)
220 examples_enable_autoproxy();
222 if (opts.verbose == TRUE)
223 examples_enable_verbose();
225 if (opts.online == TRUE)
226 mode = QUVI_SUPPORTS_MODE_ONLINE;
228 quvi_set(q, QUVI_OPTION_CALLBACK_STATUS, (qcs) examples_status);
229 type = type_n();
231 g_printerr("[%s] type=%s (0x%x), mode=0x%x\n",
232 __func__, opts.type, type, mode);
234 for (i=0; opts.url[i] != NULL; ++i)
235 chk_support(opts.url[i]);
237 opts_free();
238 examples_cleanup();
240 g_assert(q == NULL);
241 return (r);
244 /* vim: set ts=2 sw=2 tw=72 expandtab: */