2
* Copyright (C) 2012 Bastien Nocera <hadess@hadess.net>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
* The Totem project hereby grant permission for non-gpl compatible GStreamer
20
* plugins to be used and distributed together with GStreamer and Totem. This
21
* permission are above and beyond the permissions granted by the GPL license
22
* Totem is covered by.
24
* Monday 7th February 2005: Christian Schaller: Add exception clause.
25
* See license_change file for details.
32
#include <glib-object.h>
35
#include "totem-plugin.h"
38
#define TOTEM_TYPE_AUTOLOAD_SUBTITLES_PLUGIN (totem_autoload_subtitles_plugin_get_type ())
39
#define TOTEM_AUTOLOAD_SUBTITLES_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), TOTEM_TYPE_AUTOLOAD_SUBTITLES_PLUGIN, TotemAutoloadSubtitlesPlugin))
45
gboolean autoload_subs;
46
} TotemAutoloadSubtitlesPluginPrivate;
48
TOTEM_PLUGIN_REGISTER(TOTEM_TYPE_AUTOLOAD_SUBTITLES_PLUGIN, TotemAutoloadSubtitlesPlugin, totem_autoload_subtitles_plugin)
50
/* List from xine-lib's demux_sputext.c.
51
* Keep in sync with the list in totem_setup_file_filters() in this file.
52
* Don't add .txt extensions, as there are too many false positives. */
53
static const char subtitle_ext[][4] = {
63
totem_uri_exists (const char *uri)
65
GFile *file = g_file_new_for_uri (uri);
67
if (g_file_query_exists (file, NULL)) {
68
g_object_unref (file);
71
g_object_unref (file);
77
totem_uri_get_subtitle_for_uri (const char *uri)
83
g_return_val_if_fail (uri != NULL, NULL);
85
/* Find the filename suffix delimiter */
87
for (suffix = len - 1; suffix > 0; suffix--) {
88
if (uri[suffix] == G_DIR_SEPARATOR ||
89
(uri[suffix] == '/')) {
90
/* This filename has no extension; we'll need to add one */
94
if (uri[suffix] == '.') {
95
/* Found our extension marker */
102
/* Generate a subtitle string with room at the end to store the
103
* 3 character extensions for which we want to search */
104
subtitle = g_malloc0 (suffix + 4 + 1);
105
g_return_val_if_fail (subtitle != NULL, NULL);
106
g_strlcpy (subtitle, uri, suffix + 4 + 1);
107
g_strlcpy (subtitle + suffix, ".???", 5);
109
/* Search for any files with one of our known subtitle extensions */
110
for (i = 0; i < G_N_ELEMENTS (subtitle_ext) ; i++) {
111
char *subtitle_ext_upper;
112
memcpy (subtitle + suffix + 1, subtitle_ext[i], 3);
114
if (totem_uri_exists (subtitle))
117
/* Check with upper-cased extension */
118
subtitle_ext_upper = g_ascii_strup (subtitle_ext[i], -1);
119
memcpy (subtitle + suffix + 1, subtitle_ext_upper, 3);
120
g_free (subtitle_ext_upper);
122
if (totem_uri_exists (subtitle))
130
totem_uri_get_subtitle_in_subdir (GFile *file, const char *subdir)
132
char *filename, *subtitle, *full_path_str;
133
GFile *parent, *full_path, *directory;
135
/* Get the sibling directory @subdir of the file @file */
136
parent = g_file_get_parent (file);
137
directory = g_file_get_child (parent, subdir);
138
g_object_unref (parent);
140
/* Get the file of the same name as @file in the @subdir directory */
141
filename = g_file_get_basename (file);
142
full_path = g_file_get_child (directory, filename);
143
g_object_unref (directory);
146
/* Get the subtitles from that URI */
147
full_path_str = g_file_get_uri (full_path);
148
g_object_unref (full_path);
149
subtitle = totem_uri_get_subtitle_for_uri (full_path_str);
150
g_free (full_path_str);
156
totem_uri_get_cached_subtitle_for_uri (const char *uri)
158
char *filename, *basename, *fake_filename, *fake_uri, *ret;
160
filename = g_filename_from_uri (uri, NULL, NULL);
161
if (filename == NULL)
164
basename = g_path_get_basename (filename);
166
if (basename == NULL || strcmp (basename, ".") == 0) {
171
fake_filename = g_build_filename (g_get_user_cache_dir (),
177
fake_uri = g_filename_to_uri (fake_filename, NULL, NULL);
178
g_free (fake_filename);
180
ret = totem_uri_get_subtitle_for_uri (fake_uri);
187
totem_uri_get_subtitle_uri (const char *uri)
192
if (g_str_has_prefix (uri, "http") != FALSE ||
193
g_str_has_prefix (uri, "rtsp") != FALSE ||
194
g_str_has_prefix (uri, "rtmp") != FALSE)
197
/* Has the user specified a subtitle file manually? */
198
if (strstr (uri, "#subtitle:") != NULL)
201
/* Does the file exist? */
202
file = g_file_new_for_uri (uri);
203
if (g_file_query_exists (file, NULL) != TRUE) {
204
g_object_unref (file);
208
/* Try in the cached subtitles directory */
209
subtitle = totem_uri_get_cached_subtitle_for_uri (uri);
210
if (subtitle != NULL) {
211
g_object_unref (file);
215
/* Try in the current directory */
216
subtitle = totem_uri_get_subtitle_for_uri (uri);
217
if (subtitle != NULL) {
218
g_object_unref (file);
222
subtitle = totem_uri_get_subtitle_in_subdir (file, "subtitles");
223
g_object_unref (file);
231
get_text_subtitle_cb (TotemObject *totem,
233
TotemAutoloadSubtitlesPlugin *pi)
237
if (pi->priv->autoload_subs == FALSE)
240
sub = totem_uri_get_subtitle_uri (mrl);
246
autoload_subs_changed (GSettings *settings,
248
TotemAutoloadSubtitlesPlugin *pi)
250
pi->priv->autoload_subs = g_settings_get_boolean (settings, "autoload-subtitles");
254
impl_activate (PeasActivatable *plugin)
256
TotemAutoloadSubtitlesPlugin *pi = TOTEM_AUTOLOAD_SUBTITLES_PLUGIN (plugin);
258
pi->priv->totem = g_object_ref (g_object_get_data (G_OBJECT (plugin), "object"));
259
pi->priv->settings = g_settings_new ("org.gnome.totem");
260
pi->priv->autoload_subs = g_settings_get_boolean (pi->priv->settings, "autoload-subtitles");
261
g_signal_connect (pi->priv->settings, "changed::autoload-subtitles",
262
G_CALLBACK (autoload_subs_changed), pi);
263
pi->priv->signal_id = g_signal_connect (G_OBJECT (pi->priv->totem), "get-text-subtitle",
264
G_CALLBACK (get_text_subtitle_cb), pi);
268
impl_deactivate (PeasActivatable *plugin)
270
TotemAutoloadSubtitlesPlugin *pi = TOTEM_AUTOLOAD_SUBTITLES_PLUGIN (plugin);
272
if (pi->priv->signal_id) {
273
g_signal_handler_disconnect (pi->priv->totem, pi->priv->signal_id);
274
pi->priv->signal_id = 0;
276
if (pi->priv->totem) {
277
g_object_unref (pi->priv->totem);
278
pi->priv->totem = NULL;
280
if (pi->priv->settings) {
281
g_object_unref (pi->priv->settings);
282
pi->priv->settings = NULL;