2 * This file is a part of MAFW
4 * Copyright (C) 2007, 2008, 2009 Nokia Corporation, all rights reserved.
6 * Contact: Visa Smolander <visa.smolander@nokia.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
34 #include "mafw-gst-renderer-utils.h"
37 #define G_LOG_DOMAIN "mafw-gst-renderer-utils"
42 * @dst: location for utf8 version of @src.
44 * Tries to convert @src into UTF-8, placing it into @dst.
46 * Returns: TRUE on success.
48 gboolean convert_utf8(const gchar *src, gchar **dst)
54 if (g_utf8_validate(src, -1, NULL)) {
59 *dst = g_locale_to_utf8(src, -1, NULL, NULL, &error);
61 g_warning("utf8 conversion failed '%s' (%d: %s)",
62 src, error->code, error->message);
69 gboolean uri_is_playlist(const gchar *uri) {
70 /* TODO: Return if the uri is a playlist or not, using the mime type
71 instead of the file extension. */
72 if ((g_str_has_suffix(uri, ".pls")) ||
73 (g_str_has_suffix(uri, ".m3u")) ||
74 (g_str_has_suffix(uri, ".smil")) ||
75 (g_str_has_suffix(uri, ".smi")) ||
76 (g_str_has_suffix(uri, ".wpl")) ||
77 (g_str_has_suffix(uri, ".wax")) ||
78 (g_str_has_suffix(uri, ".uni")) ||
79 (g_str_has_suffix(uri, ".ram")) ||
80 /* (g_str_has_suffix(uri, ".ra")) || */
81 (g_str_has_suffix(uri, ".asx")) ||
82 (g_str_has_suffix(uri, ".rpm")))
91 * @uri: the URI to be checked.
93 * Check if given URI is a stream (not a local resource). To not depend on
94 * gnomevfs for this, we assume everything that doesn't start with "file://" is
97 * Returns: TRUE if the URI is not local.
99 gboolean uri_is_stream(const gchar *uri)
104 return !g_str_has_prefix(uri, "file://");
109 * Imported from totem-uri.c
110 * Copyright (C) 2004 Bastien Nocera
113 /* List from xine-lib's demux_sputext.c */
114 static const char subtitle_ext[][4] = {
123 static inline gboolean
124 uri_exists (const char *uri)
126 GFile *file = g_file_new_for_uri (uri);
128 if (g_file_query_exists (file, NULL)) {
129 g_object_unref (file);
132 g_object_unref (file);
138 uri_get_subtitle_for_uri (const char *uri)
144 /* Find the filename suffix delimiter */
146 for (suffix = len - 1; suffix > 0; suffix--) {
147 if (uri[suffix] == G_DIR_SEPARATOR ||
148 (uri[suffix] == '/')) {
149 /* This filename has no extension; we'll need to
154 if (uri[suffix] == '.') {
155 /* Found our extension marker */
162 /* Generate a subtitle string with room at the end to store the
163 * 3 character extensions for which we want to search */
164 subtitle = g_malloc0 (suffix + 4 + 1);
165 g_return_val_if_fail (subtitle != NULL, NULL);
166 g_strlcpy (subtitle, uri, suffix + 4 + 1);
167 g_strlcpy (subtitle + suffix, ".???", 5);
169 /* Search for any files with one of our known subtitle extensions */
170 for (i = 0; i < G_N_ELEMENTS (subtitle_ext) ; i++) {
171 char *subtitle_ext_upper;
172 memcpy (subtitle + suffix + 1, subtitle_ext[i], 3);
174 if (uri_exists (subtitle))
177 /* Check with upper-cased extension */
178 subtitle_ext_upper = g_ascii_strup (subtitle_ext[i], -1);
179 memcpy (subtitle + suffix + 1, subtitle_ext_upper, 3);
180 g_free (subtitle_ext_upper);
182 if (uri_exists (subtitle))
190 uri_get_subtitle_in_subdir (GFile *file, const char *subdir)
192 char *filename, *subtitle, *full_path_str;
193 GFile *parent, *full_path, *directory;
195 /* Get the sibling directory @subdir of the file @file */
196 parent = g_file_get_parent (file);
197 directory = g_file_get_child (parent, subdir);
198 g_object_unref (parent);
200 /* Get the file of the same name as @file in the @subdir directory */
201 filename = g_file_get_basename (file);
202 full_path = g_file_get_child (directory, filename);
203 g_object_unref (directory);
206 /* Get the subtitles from that URI */
207 full_path_str = g_file_get_uri (full_path);
208 g_object_unref (full_path);
209 subtitle = uri_get_subtitle_for_uri (full_path_str);
210 g_free (full_path_str);
216 uri_get_subtitle_uri (const char *uri)
221 if (g_str_has_prefix (uri, "http") != FALSE)
224 /* Has the user specified a subtitle file manually? */
225 if (strstr (uri, "#subtitle:") != NULL)
228 /* Does the file exist? */
229 file = g_file_new_for_uri (uri);
230 if (g_file_query_exists (file, NULL) != TRUE) {
231 g_object_unref (file);
235 /* Try in the current directory */
236 subtitle = uri_get_subtitle_for_uri (uri);
237 if (subtitle != NULL) {
238 g_object_unref (file);
242 subtitle = uri_get_subtitle_in_subdir (file, "subtitles");
243 g_object_unref (file);
248 /* vi: set noexpandtab ts=8 sw=8 cino=t0,(0: */