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/>.
40 static struct _opts_s opts
;
42 static const GOptionEntry entries
[] =
45 "type", 't', 0, G_OPTION_ARG_STRING
, &opts
.type
,
49 "online", 's', 0, G_OPTION_ARG_NONE
, &opts
.online
,
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
}
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"},
80 static gchar
**type_sv()
86 while (type_conv
[i
].from
!= NULL
) ++i
;
87 r
= g_new(gchar
*, i
+1);
90 while (type_conv
[j
].from
!= NULL
)
91 r
[i
++] = g_strdup(type_conv
[j
++].from
);
97 static gboolean
chk_type_values()
103 r
= examples_chk_val_s(opts
.type
, v
, &s
);
107 "error: invalid value (`%s') for the option `--type'\n", s
);
116 static QuviSupportsType
type_n()
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
)
133 c
= g_option_context_new("URL");
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
);
148 g_option_context_free(c
);
151 /* Set default values. */
153 if (opts
.type
== NULL
)
154 opts
.type
= g_strdup("any");
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
);
170 static void opts_free()
172 g_strfreev(opts
.url
);
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
)
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
));
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
)
209 memset(&opts
, 0, sizeof(struct _opts_s
));
210 setlocale(LC_ALL
, "");
212 r
= opts_new(argc
, argv
);
213 if (r
!= EXIT_SUCCESS
)
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
);
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
]);
244 /* vim: set ts=2 sw=2 tw=72 expandtab: */