Blame src/misc/scan_scripts.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 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
#include "config.h"
Packit 3ff1e7
Packit 3ff1e7
#include <string.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_media_s.h"
Packit 3ff1e7
#include "_quvi_subtitle_export_s.h"
Packit 3ff1e7
#include "_quvi_subtitle_s.h"
Packit 3ff1e7
#include "_quvi_playlist_s.h"
Packit 3ff1e7
#include "_quvi_script_s.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "misc/script_free.h"
Packit 3ff1e7
#include "misc/subtitle_export.h"
Packit 3ff1e7
#include "misc/subtitle.h"
Packit 3ff1e7
#include "misc/playlist.h"
Packit 3ff1e7
#include "misc/media.h"
Packit 3ff1e7
#include "misc/re.h"
Packit 3ff1e7
#include "lua/exec.h"
Packit 3ff1e7
Packit 3ff1e7
/* Return path to script file. */
Packit 3ff1e7
static GString *_get_fpath(const gchar *path, const gchar *fname)
Packit 3ff1e7
{
Packit 3ff1e7
  GString *r;
Packit 3ff1e7
  gchar *s;
Packit 3ff1e7
Packit 3ff1e7
  s = g_build_filename(path, fname, NULL);
Packit 3ff1e7
  r = g_string_new(s);
Packit 3ff1e7
Packit 3ff1e7
  g_free(s);
Packit 3ff1e7
  s = NULL;
Packit 3ff1e7
Packit 3ff1e7
  return (r);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* Return SHA1 for script file. */
Packit 3ff1e7
static GString *_file_sha1(const GString *c)
Packit 3ff1e7
{
Packit 3ff1e7
  GString *r = g_string_new(NULL);
Packit 3ff1e7
  if (c != NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      gchar *s = g_compute_checksum_for_string(G_CHECKSUM_SHA1, c->str, -1);
Packit 3ff1e7
      g_string_assign(r, s);
Packit 3ff1e7
      g_free(s);
Packit 3ff1e7
      s = NULL;
Packit 3ff1e7
    }
Packit 3ff1e7
  return (r);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* Return file contents in a GString. */
Packit 3ff1e7
static GString *_contents(GString *fpath)
Packit 3ff1e7
{
Packit 3ff1e7
  gchar *c = NULL;
Packit 3ff1e7
  g_file_get_contents(fpath->str, &c, NULL, NULL);
Packit 3ff1e7
  if (c != NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      GString *s = g_string_new(c);
Packit 3ff1e7
      g_free(c);
Packit 3ff1e7
      c = NULL;
Packit 3ff1e7
      return (s);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (NULL);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static gboolean excl_scripts_dir;
Packit 3ff1e7
static const gchar *scripts_dir;
Packit 3ff1e7
static const gchar *show_dir;
Packit 3ff1e7
const gchar *show_script;
Packit 3ff1e7
Packit 3ff1e7
typedef QuviError (*exec_script_ident_callback)(gpointer, GSList*);
Packit 3ff1e7
typedef gpointer (*new_ident_callback)(_quvi_t, const gchar*);
Packit 3ff1e7
typedef void (*free_ident_callback)(gpointer);
Packit 3ff1e7
Packit 3ff1e7
/* Parses the values returned by the ident function. */
Packit 3ff1e7
static void _chk_script_ident(_quvi_t q, _quvi_script_t qs, gboolean *ok,
Packit 3ff1e7
                              new_ident_callback cb_new,
Packit 3ff1e7
                              exec_script_ident_callback cb_exec,
Packit 3ff1e7
                              free_ident_callback cb_free)
Packit 3ff1e7
{
Packit 3ff1e7
  static const gchar URL[] = "http://foo";
Packit 3ff1e7
Packit 3ff1e7
  QuviError r;
Packit 3ff1e7
  gpointer p;
Packit 3ff1e7
  GSList *s;
Packit 3ff1e7
Packit 3ff1e7
  s = g_slist_prepend(NULL, qs);
Packit 3ff1e7
  p = cb_new(q, URL);
Packit 3ff1e7
  r = cb_exec(p, s);
Packit 3ff1e7
Packit 3ff1e7
  g_slist_free(s);
Packit 3ff1e7
  cb_free(p);
Packit 3ff1e7
Packit 3ff1e7
  /* Script ident function should return "no support". If anything else
Packit 3ff1e7
   * is returned, there's something wrong with the script. */
Packit 3ff1e7
  if (r == QUVI_ERROR_NO_SUPPORT)
Packit 3ff1e7
    *ok = TRUE;
Packit 3ff1e7
  else
Packit 3ff1e7
    {
Packit 3ff1e7
      g_critical("[%s] %s", __func__, q->status.errmsg->str);
Packit 3ff1e7
      *ok = FALSE;
Packit 3ff1e7
    }
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* Check if a pattern matches in a string. */
Packit 3ff1e7
static gboolean _chk(const gchar *s, const gchar *p)
Packit 3ff1e7
{
Packit 3ff1e7
  const gboolean r = m_match(s, p);
Packit 3ff1e7
  if (show_script != NULL && strlen(show_script) >0)
Packit 3ff1e7
    {
Packit 3ff1e7
      if (r == FALSE)
Packit 3ff1e7
        {
Packit 3ff1e7
          g_message("[%s] libquvi: nothing matched the pattern `%s'",
Packit 3ff1e7
                    __func__, p);
Packit 3ff1e7
        }
Packit 3ff1e7
    }
Packit 3ff1e7
  return (r);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* New script */
Packit 3ff1e7
static gpointer _script_new(const gchar *fpath, const gchar *fname, GString *c)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_script_t qs = g_new0(struct _quvi_script_s, 1);
Packit 3ff1e7
  qs->export.format = g_string_new(NULL);
Packit 3ff1e7
  qs->domains = g_string_new(NULL);
Packit 3ff1e7
  qs->fpath = g_string_new(fpath);
Packit 3ff1e7
  qs->fname = g_string_new(fname);
Packit 3ff1e7
  qs->sha1 = _file_sha1(c);
Packit 3ff1e7
  g_string_free(c, TRUE);
Packit 3ff1e7
  return (qs);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* New media script. */
Packit 3ff1e7
static gpointer _new_media_script(_quvi_t q, const gchar *path,
Packit 3ff1e7
                                  const gchar *fname)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_script_t qs;
Packit 3ff1e7
  GString *fpath;
Packit 3ff1e7
  GString *c;
Packit 3ff1e7
Packit 3ff1e7
  fpath = _get_fpath(path, fname);
Packit 3ff1e7
  c = _contents(fpath);
Packit 3ff1e7
  qs = NULL;
Packit 3ff1e7
Packit 3ff1e7
  if (c != NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      gboolean OK =
Packit 3ff1e7
        (_chk(c->str, "^\\-\\-\\s+libquvi\\-scripts") == TRUE
Packit 3ff1e7
         && _chk(c->str, "^function ident") == TRUE
Packit 3ff1e7
         && _chk(c->str, "^function parse") == TRUE);
Packit 3ff1e7
Packit 3ff1e7
      if (OK == TRUE)
Packit 3ff1e7
        {
Packit 3ff1e7
          qs = _script_new(fpath->str, fname, c);
Packit 3ff1e7
Packit 3ff1e7
          _chk_script_ident(q, qs, &OK, m_media_new,
Packit 3ff1e7
                            l_exec_media_script_ident,
Packit 3ff1e7
                            (free_ident_callback) m_media_free);
Packit 3ff1e7
        }
Packit 3ff1e7
Packit 3ff1e7
      if (OK == FALSE)
Packit 3ff1e7
        {
Packit 3ff1e7
          m_script_free(qs, NULL);
Packit 3ff1e7
          qs = NULL;
Packit 3ff1e7
        }
Packit 3ff1e7
    }
Packit 3ff1e7
  g_string_free(fpath, TRUE);
Packit 3ff1e7
  return (qs);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* New subtitle export script. */
Packit 3ff1e7
static gpointer
Packit 3ff1e7
_new_subtitle_export_script(_quvi_t q, const gchar *path, const gchar *fname)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_script_t qs;
Packit 3ff1e7
  GString *fpath;
Packit 3ff1e7
  GString *c;
Packit 3ff1e7
Packit 3ff1e7
  fpath = _get_fpath(path, fname);
Packit 3ff1e7
  c = _contents(fpath);
Packit 3ff1e7
  qs = NULL;
Packit 3ff1e7
Packit 3ff1e7
  if (c != NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      gboolean OK =
Packit 3ff1e7
        (_chk(c->str, "^\\-\\-\\s+libquvi\\-scripts") == TRUE
Packit 3ff1e7
         && _chk(c->str, "^function ident") == TRUE
Packit 3ff1e7
         && _chk(c->str, "^function export") == TRUE);
Packit 3ff1e7
Packit 3ff1e7
      if (OK == TRUE)
Packit 3ff1e7
        {
Packit 3ff1e7
          qs = _script_new(fpath->str, fname, c);
Packit 3ff1e7
Packit 3ff1e7
          _chk_script_ident(q, qs, &OK, m_subtitle_export_new,
Packit 3ff1e7
                            l_exec_subtitle_export_script_ident,
Packit 3ff1e7
                            (free_ident_callback) m_subtitle_export_free);
Packit 3ff1e7
        }
Packit 3ff1e7
Packit 3ff1e7
      if (OK == FALSE)
Packit 3ff1e7
        {
Packit 3ff1e7
          m_script_free(qs, NULL);
Packit 3ff1e7
          qs = NULL;
Packit 3ff1e7
        }
Packit 3ff1e7
    }
Packit 3ff1e7
  g_string_free(fpath, TRUE);
Packit 3ff1e7
  return (qs);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* New subtitle script. */
Packit 3ff1e7
static gpointer _new_subtitle_script(_quvi_t q, const gchar *path,
Packit 3ff1e7
                                     const gchar *fname)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_script_t qs;
Packit 3ff1e7
  GString *fpath;
Packit 3ff1e7
  GString *c;
Packit 3ff1e7
Packit 3ff1e7
  fpath = _get_fpath(path, fname);
Packit 3ff1e7
  c = _contents(fpath);
Packit 3ff1e7
  qs = NULL;
Packit 3ff1e7
Packit 3ff1e7
  if (c != NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      gboolean OK =
Packit 3ff1e7
        (_chk(c->str, "^\\-\\-\\s+libquvi\\-scripts") == TRUE
Packit 3ff1e7
         && _chk(c->str, "^function ident") == TRUE
Packit 3ff1e7
         && _chk(c->str, "^function parse") == TRUE);
Packit 3ff1e7
Packit 3ff1e7
      if (OK == TRUE)
Packit 3ff1e7
        {
Packit 3ff1e7
          qs = _script_new(fpath->str, fname, c);
Packit 3ff1e7
Packit 3ff1e7
          _chk_script_ident(q, qs, &OK, m_subtitle_new,
Packit 3ff1e7
                            l_exec_subtitle_script_ident,
Packit 3ff1e7
                            (free_ident_callback) m_subtitle_free);
Packit 3ff1e7
        }
Packit 3ff1e7
Packit 3ff1e7
      if (OK == FALSE)
Packit 3ff1e7
        {
Packit 3ff1e7
          m_script_free(qs, NULL);
Packit 3ff1e7
          qs = NULL;
Packit 3ff1e7
        }
Packit 3ff1e7
    }
Packit 3ff1e7
  g_string_free(fpath, TRUE);
Packit 3ff1e7
  return (qs);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* New playlist script. */
Packit 3ff1e7
static gpointer _new_playlist_script(_quvi_t q, const gchar *path,
Packit 3ff1e7
                                     const gchar *fname)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_script_t qs;
Packit 3ff1e7
  GString *fpath;
Packit 3ff1e7
  GString *c;
Packit 3ff1e7
Packit 3ff1e7
  fpath = _get_fpath(path, fname);
Packit 3ff1e7
  c = _contents(fpath);
Packit 3ff1e7
  qs = NULL;
Packit 3ff1e7
Packit 3ff1e7
  if (c != NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      gboolean OK =
Packit 3ff1e7
        (_chk(c->str, "^\\-\\-\\s+libquvi\\-scripts") == TRUE
Packit 3ff1e7
         && _chk(c->str, "^function ident") == TRUE
Packit 3ff1e7
         && _chk(c->str, "^function parse") == TRUE);
Packit 3ff1e7
Packit 3ff1e7
      if (OK == TRUE)
Packit 3ff1e7
        {
Packit 3ff1e7
          qs = _script_new(fpath->str, fname, c);
Packit 3ff1e7
Packit 3ff1e7
          _chk_script_ident(q, qs, &OK, m_playlist_new,
Packit 3ff1e7
                            l_exec_playlist_script_ident,
Packit 3ff1e7
                            (free_ident_callback) m_playlist_free);
Packit 3ff1e7
        }
Packit 3ff1e7
Packit 3ff1e7
      if (OK == FALSE)
Packit 3ff1e7
        {
Packit 3ff1e7
          m_script_free(qs, NULL);
Packit 3ff1e7
          qs = NULL;
Packit 3ff1e7
        }
Packit 3ff1e7
    }
Packit 3ff1e7
  g_string_free(fpath, TRUE);
Packit 3ff1e7
  return (qs);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* New scan script. */
Packit 3ff1e7
static gpointer _new_scan_script(_quvi_t q, const gchar *path,
Packit 3ff1e7
                                 const gchar *fname)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_script_t qs;
Packit 3ff1e7
  GString *fpath;
Packit 3ff1e7
  GString *c;
Packit 3ff1e7
Packit 3ff1e7
  fpath = _get_fpath(path, fname);
Packit 3ff1e7
  c = _contents(fpath);
Packit 3ff1e7
  qs = NULL;
Packit 3ff1e7
Packit 3ff1e7
  if (c != NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      gboolean OK =
Packit 3ff1e7
        (_chk(c->str, "^\\-\\-\\s+libquvi\\-scripts") == TRUE
Packit 3ff1e7
         && _chk(c->str, "^function parse") == TRUE);
Packit 3ff1e7
Packit 3ff1e7
      if (OK == TRUE)
Packit 3ff1e7
        qs = _script_new(fpath->str, fname, c);
Packit 3ff1e7
Packit 3ff1e7
      if (OK == FALSE)
Packit 3ff1e7
        {
Packit 3ff1e7
          m_script_free(qs, NULL);
Packit 3ff1e7
          qs = NULL;
Packit 3ff1e7
        }
Packit 3ff1e7
    }
Packit 3ff1e7
  g_string_free(fpath, TRUE);
Packit 3ff1e7
  return (qs);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* New utility script. */
Packit 3ff1e7
static gpointer _new_util_script(_quvi_t q, const gchar *path,
Packit 3ff1e7
                                 const gchar *fname)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_script_t qs;
Packit 3ff1e7
  GString *fpath;
Packit 3ff1e7
  GString *c;
Packit 3ff1e7
Packit 3ff1e7
  fpath = _get_fpath(path, fname);
Packit 3ff1e7
  c = _contents(fpath);
Packit 3ff1e7
  qs = NULL;
Packit 3ff1e7
Packit 3ff1e7
  if (c != NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      const gboolean OK =
Packit 3ff1e7
        (_chk(c->str, "^\\-\\-\\s+libquvi\\-scripts") == TRUE);
Packit 3ff1e7
Packit 3ff1e7
      if (OK == TRUE)
Packit 3ff1e7
        qs = _script_new(fpath->str, fname, c);
Packit 3ff1e7
Packit 3ff1e7
      if (OK == FALSE)
Packit 3ff1e7
        {
Packit 3ff1e7
          m_script_free(qs, NULL);
Packit 3ff1e7
          qs = NULL;
Packit 3ff1e7
        }
Packit 3ff1e7
    }
Packit 3ff1e7
  g_string_free(fpath, TRUE);
Packit 3ff1e7
  return (qs);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* Check for duplicate script. */
Packit 3ff1e7
static gboolean _chkdup_script(_quvi_t q, gpointer script, GSList *l)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_script_t a, b;
Packit 3ff1e7
  GSList *curr;
Packit 3ff1e7
Packit 3ff1e7
  a = (_quvi_script_t) script;
Packit 3ff1e7
  curr = l;
Packit 3ff1e7
Packit 3ff1e7
  while (curr != NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      b = (_quvi_script_t) curr->data;
Packit 3ff1e7
Packit 3ff1e7
      if (g_string_equal(a->sha1, b->sha1) == TRUE)
Packit 3ff1e7
        return (TRUE);
Packit 3ff1e7
Packit 3ff1e7
      curr = g_slist_next(curr);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (FALSE);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* Include '*.lua' files only. */
Packit 3ff1e7
static gint _lua_files_only(const gchar *fpath)
Packit 3ff1e7
{
Packit 3ff1e7
  const gchar *ext = strrchr(fpath, '.');
Packit 3ff1e7
  return (fpath[0] != '.' && ext != NULL && strcmp(ext, ".lua") == 0);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
typedef gpointer (*new_script_callback)(_quvi_t, const gchar*, const gchar*);
Packit 3ff1e7
typedef gboolean (*chkdup_script_callback)(_quvi_t, gpointer, GSList*);
Packit 3ff1e7
typedef void (*free_script_callback)(gpointer, gpointer);
Packit 3ff1e7
Packit 3ff1e7
static gboolean _glob_scripts_dir(_quvi_t q, const gchar *path, GSList **dst,
Packit 3ff1e7
                                  new_script_callback cb_new,
Packit 3ff1e7
                                  free_script_callback cb_free,
Packit 3ff1e7
                                  chkdup_script_callback cb_chkdup)
Packit 3ff1e7
{
Packit 3ff1e7
  const gchar *fname;
Packit 3ff1e7
  GDir *dir;
Packit 3ff1e7
Packit 3ff1e7
  if (show_dir != NULL && strlen(show_dir) >0)
Packit 3ff1e7
    g_message("[%s] libquvi: %s", __func__, path);
Packit 3ff1e7
Packit 3ff1e7
  dir = g_dir_open(path, 0, NULL);
Packit 3ff1e7
  if (dir == NULL)
Packit 3ff1e7
    return (FALSE);
Packit 3ff1e7
Packit 3ff1e7
  while ( (fname = g_dir_read_name(dir)) != NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      if (_lua_files_only(fname) != 0)
Packit 3ff1e7
        {
Packit 3ff1e7
          gpointer s = cb_new(q, path, fname);
Packit 3ff1e7
          if (s == NULL)
Packit 3ff1e7
            {
Packit 3ff1e7
              /* Either file read failed or this is not a valid
Packit 3ff1e7
               * libquvi-script. */
Packit 3ff1e7
              if (show_script != NULL && strlen(show_script) >0)
Packit 3ff1e7
                {
Packit 3ff1e7
                  g_message("[%s] libquvi: rejected: %s [INVALID]",
Packit 3ff1e7
                            __func__, fname);
Packit 3ff1e7
                }
Packit 3ff1e7
            }
Packit 3ff1e7
          else
Packit 3ff1e7
            {
Packit 3ff1e7
              /* Valid libquvi-script file. */
Packit 3ff1e7
              const gboolean r = cb_chkdup(q, s, *dst);
Packit 3ff1e7
Packit 3ff1e7
              if (r == FALSE)
Packit 3ff1e7
                *dst = g_slist_prepend(*dst, s);
Packit 3ff1e7
              else
Packit 3ff1e7
                {
Packit 3ff1e7
                  cb_free(s, NULL);
Packit 3ff1e7
                  s = NULL;
Packit 3ff1e7
                }
Packit 3ff1e7
Packit 3ff1e7
              if (show_script != NULL && strlen(show_script) >0)
Packit 3ff1e7
                {
Packit 3ff1e7
                  g_message("[%s] libquvi: %s: %s [%s]",
Packit 3ff1e7
                            __func__,
Packit 3ff1e7
                            (r == FALSE) ? "accepted" : "rejected",
Packit 3ff1e7
                            fname,
Packit 3ff1e7
                            (r == FALSE) ? "OK" : "DUPLICATE");
Packit 3ff1e7
                }
Packit 3ff1e7
            }
Packit 3ff1e7
        }
Packit 3ff1e7
    }
Packit 3ff1e7
  g_dir_close(dir);
Packit 3ff1e7
  dir = NULL;
Packit 3ff1e7
Packit 3ff1e7
  if (*dst != NULL)
Packit 3ff1e7
    *dst = g_slist_reverse(*dst);
Packit 3ff1e7
Packit 3ff1e7
  return (*dst != NULL);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
typedef enum
Packit 3ff1e7
{
Packit 3ff1e7
  GLOB_SUBTITLE_EXPORT_SCRIPTS,
Packit 3ff1e7
  GLOB_SUBTITLE_SCRIPTS,
Packit 3ff1e7
  GLOB_PLAYLIST_SCRIPTS,
Packit 3ff1e7
  GLOB_MEDIA_SCRIPTS,
Packit 3ff1e7
  GLOB_SCAN_SCRIPTS,
Packit 3ff1e7
  GLOB_UTIL_SCRIPTS,
Packit 3ff1e7
  _GLOB_COUNT
Packit 3ff1e7
} GlobType;
Packit 3ff1e7
Packit 3ff1e7
static const gchar *glob_dir[_GLOB_COUNT] =
Packit 3ff1e7
{
Packit 3ff1e7
  "subtitle/export/",
Packit 3ff1e7
  "subtitle/",
Packit 3ff1e7
  "playlist/",
Packit 3ff1e7
  "media/",
Packit 3ff1e7
  "scan/",
Packit 3ff1e7
  "util/"
Packit 3ff1e7
};
Packit 3ff1e7
Packit 3ff1e7
static gboolean _glob_scripts(_quvi_t q, const GlobType t)
Packit 3ff1e7
{
Packit 3ff1e7
  chkdup_script_callback cb_chkdup;
Packit 3ff1e7
  free_script_callback cb_free;
Packit 3ff1e7
  new_script_callback cb_new;
Packit 3ff1e7
  GSList **dst;
Packit 3ff1e7
  gchar *path;
Packit 3ff1e7
Packit 3ff1e7
  switch (t)
Packit 3ff1e7
    {
Packit 3ff1e7
    case GLOB_SUBTITLE_EXPORT_SCRIPTS:
Packit 3ff1e7
      cb_new = _new_subtitle_export_script;
Packit 3ff1e7
      dst = &q->scripts.subtitle_export;
Packit 3ff1e7
      break;
Packit 3ff1e7
    case GLOB_SUBTITLE_SCRIPTS:
Packit 3ff1e7
      cb_new = _new_subtitle_script;
Packit 3ff1e7
      dst = &q->scripts.subtitle;
Packit 3ff1e7
      break;
Packit 3ff1e7
    case GLOB_PLAYLIST_SCRIPTS:
Packit 3ff1e7
      cb_new = _new_playlist_script;
Packit 3ff1e7
      dst = &q->scripts.playlist;
Packit 3ff1e7
      break;
Packit 3ff1e7
    case GLOB_MEDIA_SCRIPTS:
Packit 3ff1e7
      cb_new = _new_media_script;
Packit 3ff1e7
      dst = &q->scripts.media;
Packit 3ff1e7
      break;
Packit 3ff1e7
    case GLOB_SCAN_SCRIPTS:
Packit 3ff1e7
      cb_new = _new_scan_script;
Packit 3ff1e7
      dst = &q->scripts.scan;
Packit 3ff1e7
      break;
Packit 3ff1e7
    case GLOB_UTIL_SCRIPTS:
Packit 3ff1e7
      cb_new = _new_util_script;
Packit 3ff1e7
      dst = &q->scripts.util;
Packit 3ff1e7
      break;
Packit 3ff1e7
    default:
Packit 3ff1e7
      g_error("%s: %d: invalid mode", __func__, __LINE__);
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  cb_chkdup = _chkdup_script;
Packit 3ff1e7
  cb_free = m_script_free;
Packit 3ff1e7
Packit 3ff1e7
  {
Packit 3ff1e7
    /* LIBQUVI_SCRIPTS_DIR */
Packit 3ff1e7
Packit 3ff1e7
    if (scripts_dir != NULL && strlen(scripts_dir) >0)
Packit 3ff1e7
      {
Packit 3ff1e7
        gchar **r;
Packit 3ff1e7
        gint i;
Packit 3ff1e7
Packit 3ff1e7
        r = g_strsplit(scripts_dir, G_SEARCHPATH_SEPARATOR_S, 0);
Packit 3ff1e7
        for (i=0; r[i] != NULL; ++i)
Packit 3ff1e7
          {
Packit 3ff1e7
            path = g_build_path(G_DIR_SEPARATOR_S, r[i], glob_dir[t], NULL);
Packit 3ff1e7
            _glob_scripts_dir(q, path, dst, cb_new, cb_free, cb_chkdup);
Packit 3ff1e7
Packit 3ff1e7
            g_free(path);
Packit 3ff1e7
            path = NULL;
Packit 3ff1e7
          }
Packit 3ff1e7
        g_strfreev(r);
Packit 3ff1e7
Packit 3ff1e7
        if (excl_scripts_dir == TRUE)
Packit 3ff1e7
          return ((*dst != NULL) ? TRUE:FALSE);
Packit 3ff1e7
      }
Packit 3ff1e7
  }
Packit 3ff1e7
Packit 3ff1e7
  {
Packit 3ff1e7
    /* Current working directory. */
Packit 3ff1e7
Packit 3ff1e7
    gchar *cwd = g_get_current_dir();
Packit 3ff1e7
    path = g_build_path(G_DIR_SEPARATOR_S, cwd, glob_dir[t], NULL);
Packit 3ff1e7
    g_free(cwd);
Packit 3ff1e7
Packit 3ff1e7
    _glob_scripts_dir(q, path, dst, cb_new, cb_free, cb_chkdup);
Packit 3ff1e7
    g_free(path);
Packit 3ff1e7
  }
Packit 3ff1e7
Packit 3ff1e7
#ifdef SCRIPTSDIR
Packit 3ff1e7
  {
Packit 3ff1e7
    /* SCRIPTSDIR from config.h */
Packit 3ff1e7
Packit 3ff1e7
    path = g_build_path(G_DIR_SEPARATOR_S,
Packit 3ff1e7
                        SCRIPTSDIR, VERSION_MM, glob_dir[t], NULL);
Packit 3ff1e7
Packit 3ff1e7
    _glob_scripts_dir(q, path, dst, cb_new, cb_free, cb_chkdup);
Packit 3ff1e7
    g_free(path);
Packit 3ff1e7
Packit 3ff1e7
    /* SCRIPTSDIR: Without the VERSION_MM. */
Packit 3ff1e7
Packit 3ff1e7
    path = g_build_path(G_DIR_SEPARATOR_S, SCRIPTSDIR, glob_dir[t], NULL);
Packit 3ff1e7
    _glob_scripts_dir(q, path, dst, cb_new, cb_free, cb_chkdup);
Packit 3ff1e7
    g_free(path);
Packit 3ff1e7
  }
Packit 3ff1e7
#endif /* SCRIPTSDIR */
Packit 3ff1e7
Packit 3ff1e7
  return ((*dst != NULL) ? TRUE:FALSE);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static gboolean _dir_exists(const gchar *path)
Packit 3ff1e7
{
Packit 3ff1e7
  GDir *dir = g_dir_open(path, 0, NULL);
Packit 3ff1e7
Packit 3ff1e7
  if (dir == NULL)
Packit 3ff1e7
    return (FALSE);
Packit 3ff1e7
Packit 3ff1e7
  g_dir_close(dir);
Packit 3ff1e7
  dir = NULL;
Packit 3ff1e7
Packit 3ff1e7
  return (TRUE);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
extern void l_modify_pkgpath(_quvi_t, const gchar*);
Packit 3ff1e7
Packit 3ff1e7
#define Q_COMMON_DIR "common"
Packit 3ff1e7
Packit 3ff1e7
/*
Packit 3ff1e7
 * Check for the "common" directory, if found, append the path to the
Packit 3ff1e7
 * Lua's package.path setting.  We're not interested in the contents of
Packit 3ff1e7
 * this directory at this stage.
Packit 3ff1e7
 */
Packit 3ff1e7
static void _chk_common_scripts(_quvi_t q)
Packit 3ff1e7
{
Packit 3ff1e7
  gchar *path = NULL;
Packit 3ff1e7
Packit 3ff1e7
  {
Packit 3ff1e7
    /* LIBQUVI_SCRIPTS_DIR */
Packit 3ff1e7
Packit 3ff1e7
    if (scripts_dir != NULL && strlen(scripts_dir) >0)
Packit 3ff1e7
      {
Packit 3ff1e7
        gchar **r;
Packit 3ff1e7
        gint i;
Packit 3ff1e7
Packit 3ff1e7
        r = g_strsplit(scripts_dir, G_SEARCHPATH_SEPARATOR_S, 0);
Packit 3ff1e7
        for (i=0; r[i] != NULL; ++i)
Packit 3ff1e7
          {
Packit 3ff1e7
            path = g_build_path(G_DIR_SEPARATOR_S,
Packit 3ff1e7
                                scripts_dir, Q_COMMON_DIR, NULL);
Packit 3ff1e7
Packit 3ff1e7
            if (_dir_exists(path) == TRUE)
Packit 3ff1e7
              l_modify_pkgpath(q, path);
Packit 3ff1e7
Packit 3ff1e7
            g_free(path);
Packit 3ff1e7
            path = NULL;
Packit 3ff1e7
          }
Packit 3ff1e7
        g_strfreev(r);
Packit 3ff1e7
        r = NULL;
Packit 3ff1e7
Packit 3ff1e7
        if (excl_scripts_dir == TRUE)
Packit 3ff1e7
          return;
Packit 3ff1e7
      }
Packit 3ff1e7
  }
Packit 3ff1e7
Packit 3ff1e7
  {
Packit 3ff1e7
    /* Current working directory. */
Packit 3ff1e7
Packit 3ff1e7
    gchar *cwd = g_get_current_dir();
Packit 3ff1e7
    path = g_build_path(G_DIR_SEPARATOR_S, cwd, Q_COMMON_DIR, NULL);
Packit 3ff1e7
Packit 3ff1e7
    if (_dir_exists(path) == TRUE)
Packit 3ff1e7
      l_modify_pkgpath(q, path);
Packit 3ff1e7
Packit 3ff1e7
    g_free(path);
Packit 3ff1e7
    path = NULL;
Packit 3ff1e7
Packit 3ff1e7
    g_free(cwd);
Packit 3ff1e7
    cwd = NULL;
Packit 3ff1e7
  }
Packit 3ff1e7
Packit 3ff1e7
#ifdef SCRIPTSDIR
Packit 3ff1e7
  {
Packit 3ff1e7
    /* SCRIPTSDIR from config.h */
Packit 3ff1e7
Packit 3ff1e7
    path = g_build_path(G_DIR_SEPARATOR_S,
Packit 3ff1e7
                        SCRIPTSDIR, VERSION_MM, Q_COMMON_DIR, NULL);
Packit 3ff1e7
Packit 3ff1e7
    if (_dir_exists(path) == TRUE)
Packit 3ff1e7
      l_modify_pkgpath(q, path);
Packit 3ff1e7
Packit 3ff1e7
    g_free(path);
Packit 3ff1e7
Packit 3ff1e7
    /* SCRIPTSDIR: Without the VERSION_MM. */
Packit 3ff1e7
Packit 3ff1e7
    path = g_build_path(G_DIR_SEPARATOR_S, SCRIPTSDIR, Q_COMMON_DIR, NULL);
Packit 3ff1e7
Packit 3ff1e7
    if (_dir_exists(path) == TRUE)
Packit 3ff1e7
      l_modify_pkgpath(q, path);
Packit 3ff1e7
Packit 3ff1e7
    g_free(path);
Packit 3ff1e7
    path = NULL;
Packit 3ff1e7
  }
Packit 3ff1e7
#endif /* SCRIPTSDIR */
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
#undef Q_COMMON_DIR
Packit 3ff1e7
Packit 3ff1e7
QuviError m_scan_scripts(_quvi_t q)
Packit 3ff1e7
{
Packit 3ff1e7
  QuviError r, e;
Packit 3ff1e7
  GlobType t;
Packit 3ff1e7
Packit 3ff1e7
  {
Packit 3ff1e7
    const gchar *s = g_getenv("LIBQUVI_EXCLUSIVE_SCRIPTS_DIR");
Packit 3ff1e7
Packit 3ff1e7
    excl_scripts_dir = (s != NULL && strlen(s) >0)
Packit 3ff1e7
                       ? TRUE
Packit 3ff1e7
                       : FALSE;
Packit 3ff1e7
  }
Packit 3ff1e7
Packit 3ff1e7
  scripts_dir = g_getenv("LIBQUVI_SCRIPTS_DIR");
Packit 3ff1e7
  show_script = g_getenv("LIBQUVI_SHOW_SCRIPT");
Packit 3ff1e7
  show_dir = g_getenv("LIBQUVI_SHOW_DIR");
Packit 3ff1e7
Packit 3ff1e7
  _chk_common_scripts(q);
Packit 3ff1e7
Packit 3ff1e7
  e = QUVI_ERROR_CALLBACK_ABORTED+1;
Packit 3ff1e7
  r = QUVI_OK;
Packit 3ff1e7
  t = -1;
Packit 3ff1e7
Packit 3ff1e7
  while (++t <_GLOB_COUNT && r ==QUVI_OK)
Packit 3ff1e7
    {
Packit 3ff1e7
      r = (_glob_scripts(q,t) == TRUE)  ? QUVI_OK : e;
Packit 3ff1e7
      ++e;
Packit 3ff1e7
    }
Packit 3ff1e7
  return (r);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* vim: set ts=2 sw=2 tw=72 expandtab: */