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/>.
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_SUBTITLE
, "subtitle"},
76 {QUVI_SUPPORTS_TYPE_MEDIA
, "media"},
77 {QUVI_SUPPORTS_TYPE_ANY
, "any"},
81 static gchar
**type_sv()
87 while (type_conv
[i
].from
!= NULL
) ++i
;
88 r
= g_new(gchar
*, i
+1);
91 while (type_conv
[j
].from
!= NULL
)
92 r
[i
++] = g_strdup(type_conv
[j
++].from
);
98 static gboolean
chk_type_values()
104 r
= examples_chk_val_s(opts
.type
, v
, &s
);
108 "error: invalid value (`%s') for the option `--type'\n", s
);
117 static QuviSupportsType
type_n()
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
)
134 c
= g_option_context_new("URL");
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
);
149 g_option_context_free(c
);
152 /* Set default values. */
154 if (opts
.type
== NULL
)
155 opts
.type
= g_strdup("any");
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
);
171 static void opts_free()
173 g_strfreev(opts
.url
);
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
));
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
)
208 memset(&opts
, 0, sizeof(struct _opts_s
));
209 setlocale(LC_ALL
, "");
211 r
= opts_new(argc
, argv
);
212 if (r
!= EXIT_SUCCESS
)
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
);
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
]);
243 /* vim: set ts=2 sw=2 tw=72 expandtab: */