/*
 * Hornsey - Moblin Media Player.
 * Copyright © 2009 Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA
 */

/* sub-setted from hrn-video-player.c from Hornsey ... */

/* FIXME: ClutterMedia could have a set_subtitle_uri method */
static void
player_set_subtitle_uri (HrnIfacePlayer *player,
                         const char     *uri)
{
  HrnVideoPlayer *video = (HrnVideoPlayer *) player;
  HrnVideoPlayerPrivate *priv = video->priv;
  ClutterGstVideoTexture *video_texture;
  GstElement *playbin2;

  video_texture = CLUTTER_GST_VIDEO_TEXTURE (priv->video_player);
  playbin2 = clutter_gst_video_texture_get_pipeline (video_texture);

  g_message ("set subtitle: %s", uri);
  g_object_set (playbin2, "suburi", uri, NULL);
}

static void
look_for_locale_dependant_subtitle (HrnIfacePlayer *player,
                                    const char     *uri)
{
  gchar *scheme, *path, *dot;
  const gchar * const *languages;
  GFile *video;
  gint i, j;
  static gchar *subtitle_extensions[] = { ".srt" };

  scheme = g_uri_parse_scheme (uri);
  if (scheme == NULL || strcmp (scheme, "file") != 0) {
    /* do not try to look for subtitle files if the video file is not
     * mounted locally */
    g_free (scheme);
    return;
  }
  g_free (scheme);

  video = g_file_new_for_uri (uri);
  path = g_file_get_path (video);
  g_object_unref (video);
  if (path == NULL)
    return;

  dot = strrchr (path, '.');
  if (dot == NULL) {
    g_free (path);
    return;
  }
  *dot = '\0';

  languages = g_get_language_names ();
  for (i = 0; languages[i]; i++) {
    for (j = 0; j < G_N_ELEMENTS (subtitle_extensions); j++) {
      gchar *subtitle_path;
      GFile *subtitle;

      subtitle_path = g_strconcat (path,
                                   "-",
                                   languages[i],
                                   subtitle_extensions[j],
                                   NULL);
      subtitle = g_file_new_for_path (subtitle_path);
      if (g_file_query_exists (subtitle, NULL)) {
        gchar *uri;

        uri = g_file_get_uri (subtitle);
        player_set_subtitle_uri (player, uri);

        g_object_unref (subtitle);
        g_free (subtitle_path);
        g_free (uri);
        g_free (path);
        return;
      }

      g_object_unref (subtitle);
      g_free (subtitle_path);
    }
  }

  g_free (path);
}

static void
player_set_uri (HrnIfacePlayer *player,
                const char     *uri)
{
  HrnVideoPlayer        *video = (HrnVideoPlayer *) player;
  HrnVideoPlayerPrivate *priv  = video->priv;

  priv->duration = 0;
  clutter_media_set_uri ((ClutterMedia *) priv->video_player, uri);
  look_for_locale_dependant_subtitle (player, uri);
}

