Blame src/api/subtitle_select.c

Packit 3ff1e7
/* libquvi
Packit 3ff1e7
 * Copyright (C) 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 library 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 library 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 library.  If not, see
Packit 3ff1e7
 * <http://www.gnu.org/licenses/>.
Packit 3ff1e7
 */
Packit 3ff1e7
Packit 3ff1e7
/** @file subtitle_select.c */
Packit 3ff1e7
Packit 3ff1e7
#include "config.h"
Packit 3ff1e7
Packit 3ff1e7
#include <glib/gi18n-lib.h>
Packit 3ff1e7
#include <glib.h>
Packit 3ff1e7
Packit 3ff1e7
#include "quvi.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "_quvi_s.h"
Packit 3ff1e7
#include "_quvi_subtitle_s.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "misc/re.h"
Packit 3ff1e7
Packit 3ff1e7
static _quvi_subtitle_lang_t _match(_quvi_subtitle_t qsub, const gchar *id)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_subtitle_type_t qst;
Packit 3ff1e7
  _quvi_subtitle_lang_t qsl;
Packit 3ff1e7
Packit 3ff1e7
  quvi_subtitle_type_reset(qsub);
Packit 3ff1e7
Packit 3ff1e7
  while ( (qst = quvi_subtitle_type_next(qsub)) != NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      quvi_subtitle_lang_reset(qst);
Packit 3ff1e7
      while ( (qsl = quvi_subtitle_lang_next(qst)) != NULL)
Packit 3ff1e7
        {
Packit 3ff1e7
          if (m_match(qsl->id->str, id) == TRUE)
Packit 3ff1e7
            return (qsl);
Packit 3ff1e7
        }
Packit 3ff1e7
    }
Packit 3ff1e7
  return (NULL);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* Return the first available language of the first available type. */
Packit 3ff1e7
static _quvi_subtitle_lang_t _default(_quvi_subtitle_t qsub)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_subtitle_type_t qst;
Packit 3ff1e7
Packit 3ff1e7
  quvi_subtitle_type_reset(qsub);
Packit 3ff1e7
  qst = quvi_subtitle_type_next(qsub);
Packit 3ff1e7
Packit 3ff1e7
  if (qst == NULL)
Packit 3ff1e7
    return (NULL);
Packit 3ff1e7
Packit 3ff1e7
  quvi_subtitle_lang_reset(qst);
Packit 3ff1e7
  return (quvi_subtitle_lang_next(qst));
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static _quvi_subtitle_lang_t _select(_quvi_subtitle_t qsub, const gchar *id)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_subtitle_lang_t qsl;
Packit 3ff1e7
  gchar **r;
Packit 3ff1e7
  _quvi_t q;
Packit 3ff1e7
  gint i;
Packit 3ff1e7
Packit 3ff1e7
  r = g_strsplit(id, ",", 0);
Packit 3ff1e7
  q = qsub->handle.quvi;
Packit 3ff1e7
Packit 3ff1e7
  q->status.rc = QUVI_OK;
Packit 3ff1e7
  qsl = NULL;
Packit 3ff1e7
Packit 3ff1e7
  for (i=0; (r[i] != NULL && qsl == NULL); ++i)
Packit 3ff1e7
    {
Packit 3ff1e7
      if (g_strcmp0(r[i], "croak") ==0)
Packit 3ff1e7
        {
Packit 3ff1e7
          q->status.rc = QUVI_ERROR_KEYWORD_CROAK;
Packit 3ff1e7
          break;
Packit 3ff1e7
        }
Packit 3ff1e7
      else
Packit 3ff1e7
        qsl = _match(qsub, r[i]);
Packit 3ff1e7
    }
Packit 3ff1e7
  g_strfreev(r);
Packit 3ff1e7
  return ((qsl == NULL && q->status.rc == QUVI_OK)
Packit 3ff1e7
          ? _default(qsub)
Packit 3ff1e7
          : qsl);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/** @brief Select a @ref sub_lang matching a @ref sub_lang_id
Packit 3ff1e7
Packit 3ff1e7
Matches the @ref sub_lang_id (pattern) to the available subtitle
Packit 3ff1e7
language IDs and selects the language. This function returns immediately
Packit 3ff1e7
if a matching ID was found.  The ID value may be a comma-separated value
Packit 3ff1e7
(e.g. "foo,bar,baz"). The ID may also contain the keyword 'croak' (see
Packit 3ff1e7
the notes below).
Packit 3ff1e7
@note
Packit 3ff1e7
  - ID value is used as regular expression pattern
Packit 3ff1e7
  - ID may contain the keyword 'croak'
Packit 3ff1e7
    - This will cause the function to exit immediately when it is reached
Packit 3ff1e7
    - The result may be checked with @ref quvi_ok
Packit 3ff1e7
      - The code may be retrieved using @ref quvi_get
Packit 3ff1e7
      - The error message may be retrieved using @ref quvi_errmsg
Packit 3ff1e7
  - If nothing matched (and the 'croak' keyword was not specified) the
Packit 3ff1e7
    function will either:
Packit 3ff1e7
      - return the first (default) available language, or
Packit 3ff1e7
      - NULL if the library failed to find any subtitle languages for
Packit 3ff1e7
        the media
Packit 3ff1e7
  - Always confirm the result with @ref quvi_ok
Packit 3ff1e7
  - Calling this function will reset the list pointers for both
Packit 3ff1e7
    @ref sub_type and @ref sub_lang
Packit 3ff1e7
@sa @ref parse_subtitle
Packit 3ff1e7
@sa quvi_subtitle_type_reset
Packit 3ff1e7
@sa quvi_subtitle_lang_reset
Packit 3ff1e7
@sa quvi_subtitle_type_next
Packit 3ff1e7
@sa quvi_subtitle_lang_next
Packit 3ff1e7
@sa quvi_subtitle_new
Packit 3ff1e7
@ingroup subprop
Packit 3ff1e7
*/
Packit 3ff1e7
const quvi_subtitle_lang_t
Packit 3ff1e7
quvi_subtitle_select(quvi_subtitle_t handle, const char *id)
Packit 3ff1e7
{
Packit 3ff1e7
  /* If G_DISABLE_CHECKS is defined then the check is not performed. */
Packit 3ff1e7
  g_return_val_if_fail(handle != NULL, NULL);
Packit 3ff1e7
  g_return_val_if_fail(id != NULL, NULL);
Packit 3ff1e7
Packit 3ff1e7
  return (_select(handle, id));
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* vim: set ts=2 sw=2 tw=72 expandtab: */