Blame examples/supports.c

Packit 3ff1e7
/* libquvi
Packit 3ff1e7
 * Copyright (C) 2012,2013  Toni Gundogdu <legatvs@gmail.com>
Packit 3ff1e7
 *
Packit 3ff1e7
 * This file is part of libquvi <http://quvi.sourceforge.net/>.
Packit 3ff1e7
 *
Packit 3ff1e7
 * This program is free software: you can redistribute it and/or
Packit 3ff1e7
 * modify it under the terms of the GNU Affero General Public
Packit 3ff1e7
 * License as published by the Free Software Foundation, either
Packit 3ff1e7
 * version 3 of the License, or (at your option) any later version.
Packit 3ff1e7
 *
Packit 3ff1e7
 * This program is distributed in the hope that it will be useful,
Packit 3ff1e7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3ff1e7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 3ff1e7
 * GNU Affero General Public License for more details.
Packit 3ff1e7
 *
Packit 3ff1e7
 * You should have received a copy of the GNU Affero General
Packit 3ff1e7
 * Public License along with this program.  If not, see
Packit 3ff1e7
 * <http://www.gnu.org/licenses/>.
Packit 3ff1e7
 */
Packit 3ff1e7
Packit 3ff1e7
#include "config.h"
Packit 3ff1e7
Packit 3ff1e7
#include <locale.h>
Packit 3ff1e7
#include <stdlib.h>
Packit 3ff1e7
#include <string.h>
Packit 3ff1e7
#include <glib.h>
Packit 3ff1e7
#include <quvi.h>
Packit 3ff1e7
Packit 3ff1e7
#include "examples.h"
Packit 3ff1e7
Packit 3ff1e7
struct _opts_s
Packit 3ff1e7
{
Packit 3ff1e7
  gboolean autoproxy;
Packit 3ff1e7
  gboolean verbose;
Packit 3ff1e7
  gboolean online;
Packit 3ff1e7
  gchar *type;
Packit 3ff1e7
  gchar **url;
Packit 3ff1e7
};
Packit 3ff1e7
Packit 3ff1e7
static struct _opts_s opts;
Packit 3ff1e7
Packit 3ff1e7
static const GOptionEntry entries[] =
Packit 3ff1e7
{
Packit 3ff1e7
  {
Packit 3ff1e7
    "type", 't', 0, G_OPTION_ARG_STRING, &opts.type,
Packit 3ff1e7
    "Script type", "TYPE"
Packit 3ff1e7
  },
Packit 3ff1e7
  {
Packit 3ff1e7
    "online", 's', 0, G_OPTION_ARG_NONE, &opts.online,
Packit 3ff1e7
    "Check online", NULL
Packit 3ff1e7
  },
Packit 3ff1e7
  {
Packit 3ff1e7
    "autoproxy", 'a', 0, G_OPTION_ARG_NONE, &opts.autoproxy,
Packit 3ff1e7
    "Enable the autoproxy feature", NULL
Packit 3ff1e7
  },
Packit 3ff1e7
  {
Packit 3ff1e7
    "verbose", 'v', 0, G_OPTION_ARG_NONE, &opts.verbose,
Packit 3ff1e7
    "Verbose libcurl output", NULL
Packit 3ff1e7
  },
Packit 3ff1e7
  {
Packit 3ff1e7
    G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &opts.url, "URL"
Packit 3ff1e7
  },
Packit 3ff1e7
  {NULL, 0, 0, 0, NULL, NULL, NULL}
Packit 3ff1e7
};
Packit 3ff1e7
Packit 3ff1e7
struct _type_lookup_s
Packit 3ff1e7
{
Packit 3ff1e7
  QuviSupportsType to;
Packit 3ff1e7
  const gchar *from;
Packit 3ff1e7
};
Packit 3ff1e7
Packit 3ff1e7
static const struct _type_lookup_s type_conv[] =
Packit 3ff1e7
{
Packit 3ff1e7
  {QUVI_SUPPORTS_TYPE_PLAYLIST, "playlist"},
Packit 3ff1e7
  {QUVI_SUPPORTS_TYPE_SUBTITLE, "subtitle"},
Packit 3ff1e7
  {QUVI_SUPPORTS_TYPE_MEDIA,    "media"},
Packit 3ff1e7
  {QUVI_SUPPORTS_TYPE_ANY,      "any"},
Packit 3ff1e7
  {0, NULL}
Packit 3ff1e7
};
Packit 3ff1e7
Packit 3ff1e7
static gchar **type_sv()
Packit 3ff1e7
{
Packit 3ff1e7
  gchar **r;
Packit 3ff1e7
  gint i,j;
Packit 3ff1e7
Packit 3ff1e7
  i=0;
Packit 3ff1e7
  while (type_conv[i].from != NULL) ++i;
Packit 3ff1e7
  r = g_new(gchar*, i+1);
Packit 3ff1e7
Packit 3ff1e7
  i=j=0;
Packit 3ff1e7
  while (type_conv[j].from != NULL)
Packit 3ff1e7
    r[i++] = g_strdup(type_conv[j++].from);
Packit 3ff1e7
  r[i] = NULL;
Packit 3ff1e7
Packit 3ff1e7
  return (r);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static gboolean chk_type_values()
Packit 3ff1e7
{
Packit 3ff1e7
  gchar **v, *s;
Packit 3ff1e7
  gboolean r;
Packit 3ff1e7
Packit 3ff1e7
  v = type_sv();
Packit 3ff1e7
  r = examples_chk_val_s(opts.type, v, &s);
Packit 3ff1e7
  if (r == FALSE)
Packit 3ff1e7
    {
Packit 3ff1e7
      g_printerr(
Packit 3ff1e7
        "error: invalid value (`%s') for the option `--type'\n", s);
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  g_strfreev(v);
Packit 3ff1e7
  v = NULL;
Packit 3ff1e7
Packit 3ff1e7
  return (r);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static QuviSupportsType type_n()
Packit 3ff1e7
{
Packit 3ff1e7
  gint i;
Packit 3ff1e7
  for (i=0; type_conv[i].from != NULL; ++i)
Packit 3ff1e7
    {
Packit 3ff1e7
      if (g_strcmp0(opts.type, type_conv[i].from) == 0)
Packit 3ff1e7
        return (type_conv[i].to);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (QUVI_SUPPORTS_TYPE_MEDIA);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static gint opts_new(gint argc, gchar **argv)
Packit 3ff1e7
{
Packit 3ff1e7
  GOptionContext *c;
Packit 3ff1e7
  GError *e;
Packit 3ff1e7
  gint r;
Packit 3ff1e7
Packit 3ff1e7
  c = g_option_context_new("URL");
Packit 3ff1e7
  r = EXIT_SUCCESS;
Packit 3ff1e7
  e = NULL;
Packit 3ff1e7
Packit 3ff1e7
  g_option_context_set_help_enabled(c, TRUE);
Packit 3ff1e7
  g_option_context_add_main_entries(c, entries, NULL);
Packit 3ff1e7
Packit 3ff1e7
  if (g_option_context_parse(c, &argc, &argv, &e) == FALSE)
Packit 3ff1e7
    {
Packit 3ff1e7
      g_printerr("error: %s\n", e->message);
Packit 3ff1e7
      g_error_free(e);
Packit 3ff1e7
      r = EXIT_FAILURE;
Packit 3ff1e7
      e = NULL;
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  g_option_context_free(c);
Packit 3ff1e7
  c = NULL;
Packit 3ff1e7
Packit 3ff1e7
  /* Set default values. */
Packit 3ff1e7
Packit 3ff1e7
  if (opts.type == NULL)
Packit 3ff1e7
    opts.type = g_strdup("any");
Packit 3ff1e7
Packit 3ff1e7
  /* Check input. */
Packit 3ff1e7
Packit 3ff1e7
  if (chk_type_values() == FALSE)
Packit 3ff1e7
    return (EXIT_FAILURE);
Packit 3ff1e7
Packit 3ff1e7
  if (opts.url == NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      g_printerr("error: no input URL\n");
Packit 3ff1e7
      return (EXIT_FAILURE);
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  return (r);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static void opts_free()
Packit 3ff1e7
{
Packit 3ff1e7
  g_strfreev(opts.url);
Packit 3ff1e7
  opts.url = NULL;
Packit 3ff1e7
Packit 3ff1e7
  g_free(opts.type);
Packit 3ff1e7
  opts.type = NULL;
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static QuviSupportsMode mode = QUVI_SUPPORTS_MODE_OFFLINE;
Packit 3ff1e7
static QuviSupportsType type = QUVI_SUPPORTS_TYPE_ANY;
Packit 3ff1e7
Packit 3ff1e7
static void chk_support(const gchar *url)
Packit 3ff1e7
{
Packit 3ff1e7
  const QuviBoolean r = quvi_supports(q, url, mode, type);
Packit 3ff1e7
Packit 3ff1e7
  /* Always check for any network errors with QUVI_SUPPORTS_MODE_ONLINE. */
Packit 3ff1e7
  if (r == FALSE && mode == QUVI_SUPPORTS_MODE_ONLINE)
Packit 3ff1e7
    {
Packit 3ff1e7
      const glong ec = quvi_errcode(q);
Packit 3ff1e7
      if (ec != QUVI_ERROR_NO_SUPPORT)
Packit 3ff1e7
        {
Packit 3ff1e7
          g_printerr("\nerror: %s\n", quvi_errmsg(q));
Packit 3ff1e7
          return;
Packit 3ff1e7
        }
Packit 3ff1e7
    }
Packit 3ff1e7
  g_print("%s: %s\n", url, (r == QUVI_TRUE) ? "yes":"no");
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
typedef quvi_callback_status qcs;
Packit 3ff1e7
Packit 3ff1e7
gint main(gint argc, gchar **argv)
Packit 3ff1e7
{
Packit 3ff1e7
  gint i,r;
Packit 3ff1e7
Packit 3ff1e7
  g_assert(q == NULL);
Packit 3ff1e7
Packit 3ff1e7
  memset(&opts, 0, sizeof(struct _opts_s));
Packit 3ff1e7
  setlocale(LC_ALL, "");
Packit 3ff1e7
Packit 3ff1e7
  r = opts_new(argc, argv);
Packit 3ff1e7
  if (r != EXIT_SUCCESS)
Packit 3ff1e7
    return (r);
Packit 3ff1e7
Packit 3ff1e7
  q = quvi_new();
Packit 3ff1e7
  examples_exit_if_error();
Packit 3ff1e7
Packit 3ff1e7
  if (opts.autoproxy == TRUE)
Packit 3ff1e7
    examples_enable_autoproxy();
Packit 3ff1e7
Packit 3ff1e7
  if (opts.verbose == TRUE)
Packit 3ff1e7
    examples_enable_verbose();
Packit 3ff1e7
Packit 3ff1e7
  if (opts.online == TRUE)
Packit 3ff1e7
    mode = QUVI_SUPPORTS_MODE_ONLINE;
Packit 3ff1e7
Packit 3ff1e7
  quvi_set(q, QUVI_OPTION_CALLBACK_STATUS, (qcs) examples_status);
Packit 3ff1e7
  type = type_n();
Packit 3ff1e7
Packit 3ff1e7
  g_printerr("[%s] type=%s (0x%x), mode=0x%x\n",
Packit 3ff1e7
             __func__, opts.type, type, mode);
Packit 3ff1e7
Packit 3ff1e7
  for (i=0; opts.url[i] != NULL; ++i)
Packit 3ff1e7
    chk_support(opts.url[i]);
Packit 3ff1e7
Packit 3ff1e7
  opts_free();
Packit 3ff1e7
  examples_cleanup();
Packit 3ff1e7
Packit 3ff1e7
  g_assert(q == NULL);
Packit 3ff1e7
  return (r);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* vim: set ts=2 sw=2 tw=72 expandtab: */