summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBastien Nocera <hadess@hadess.net>2019-11-07 17:28:20 +0100
committerBastien Nocera <hadess@hadess.net>2019-11-09 14:16:54 +0100
commita6f21e789f125d9e8b1aa73b5d530211224584d5 (patch)
treea88c8d9399879b00dccd69c056e473be6c2d2866
parent49854e9aa25586ab275ecbc1a05eec039ab47deb (diff)
downloadtotem-pl-parser-a6f21e789f125d9e8b1aa73b5d530211224584d5.tar.gz
plparser: Add totem_pl_parser_add_ignored_glob()
Add a way to ignore globs, such as "*.txt", as ignoring mime-types doesn't do everything that we might want to do with files, eg. even if "text/plain" text files could be a playlist, a "*.txt" file would almost never be.
-rw-r--r--docs/reference/totem-pl-parser-sections.txt1
-rw-r--r--plparse/plparser.map1
-rw-r--r--plparse/totem-pl-parser.c55
-rw-r--r--plparse/totem-pl-parser.h2
4 files changed, 59 insertions, 0 deletions
diff --git a/docs/reference/totem-pl-parser-sections.txt b/docs/reference/totem-pl-parser-sections.txt
index b8bede0..23b77af 100644
--- a/docs/reference/totem-pl-parser-sections.txt
+++ b/docs/reference/totem-pl-parser-sections.txt
@@ -18,6 +18,7 @@ totem_pl_parser_parse_duration
totem_pl_parser_parse_date
totem_pl_parser_add_ignored_scheme
totem_pl_parser_add_ignored_mimetype
+totem_pl_parser_add_ignored_glob
totem_pl_parser_can_parse_from_data
totem_pl_parser_can_parse_from_filename
totem_pl_parser_can_parse_from_uri
diff --git a/plparse/plparser.map b/plparse/plparser.map
index de28e97..280308b 100644
--- a/plparse/plparser.map
+++ b/plparse/plparser.map
@@ -10,6 +10,7 @@ LIBTOTEM_PL_PARSER_MINI_1.0 {
totem_disc_media_type_quark;
totem_pl_parser_add_ignored_mimetype;
totem_pl_parser_add_ignored_scheme;
+ totem_pl_parser_add_ignored_glob;
totem_pl_parser_can_parse_from_data;
totem_pl_parser_can_parse_from_filename;
totem_pl_parser_can_parse_from_uri;
diff --git a/plparse/totem-pl-parser.c b/plparse/totem-pl-parser.c
index 0d34ceb..e63a191 100644
--- a/plparse/totem-pl-parser.c
+++ b/plparse/totem-pl-parser.c
@@ -124,6 +124,7 @@
#include "config.h"
#include <string.h>
+#include <fnmatch.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>
@@ -257,6 +258,7 @@ static void totem_pl_parser_get_property (GObject *object,
struct TotemPlParserPrivate {
GHashTable *ignore_schemes; /* key = char *, value = boolean */
GHashTable *ignore_mimetypes; /*key = char *, value = boolean */
+ GHashTable *ignore_globs; /*key = char *, value = boolean */
GMutex ignore_mutex;
GThread *main_thread; /* see CALL_ASYNC() in *-private.h */
@@ -1281,6 +1283,7 @@ totem_pl_parser_init (TotemPlParser *parser)
g_mutex_init (&parser->priv->ignore_mutex);
parser->priv->ignore_schemes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
parser->priv->ignore_mimetypes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+ parser->priv->ignore_globs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
}
static void
@@ -1293,6 +1296,7 @@ totem_pl_parser_finalize (GObject *object)
g_clear_pointer (&priv->ignore_schemes, g_hash_table_destroy);
g_clear_pointer (&priv->ignore_mimetypes, g_hash_table_destroy);
+ g_clear_pointer (&priv->ignore_globs, g_hash_table_destroy);
g_mutex_clear (&priv->ignore_mutex);
@@ -1584,6 +1588,28 @@ totem_pl_parser_mimetype_is_ignored (TotemPlParser *parser,
return ret;
}
+static gboolean
+totem_pl_parser_glob_is_ignored (TotemPlParser *parser,
+ const char *filename)
+{
+ GHashTableIter iter;
+ gpointer key;
+ int ret;
+
+ g_mutex_lock (&parser->priv->ignore_mutex);
+ g_hash_table_iter_init (&iter, parser->priv->ignore_globs);
+ while (g_hash_table_iter_next (&iter, &key, NULL)) {
+ const char *glob = key;
+
+ ret = fnmatch (glob, filename, 0);
+ if (ret == 0)
+ break;
+ }
+ g_mutex_unlock (&parser->priv->ignore_mutex);
+
+ return (ret == 0);
+}
+
/**
* totem_pl_parser_ignore:
* @parser: a #TotemPlParser
@@ -1608,6 +1634,9 @@ totem_pl_parser_ignore (TotemPlParser *parser, const char *uri)
g_autoptr(GFile) file;
guint i;
+ if (totem_pl_parser_glob_is_ignored (parser, uri) != FALSE)
+ return TRUE;
+
file = g_file_new_for_path (uri);
if (totem_pl_parser_scheme_is_ignored (parser, file) != FALSE)
return TRUE;
@@ -1822,6 +1851,11 @@ totem_pl_parser_parse_internal (TotemPlParser *parser,
}
}
+ if (uri != NULL) {
+ if (totem_pl_parser_glob_is_ignored (parser, uri))
+ return TOTEM_PL_PARSER_RESULT_IGNORED;
+ }
+
/* In force mode we want to get the data */
if (parse_data->force != FALSE) {
mimetype = my_g_file_info_get_mime_type_with_data (file, &data, parser);
@@ -2228,6 +2262,27 @@ totem_pl_parser_add_ignored_mimetype (TotemPlParser *parser,
}
/**
+ * totem_pl_parser_add_ignored_glob:
+ * @parser: a #TotemPlParser
+ * @glob: a glob to ignore
+ *
+ * Adds a glob to the list of mimetypes to ignore, so that
+ * any URI of that glob is ignored during playlist parsing.
+ *
+ * Since: 3.26.4
+ **/
+void
+totem_pl_parser_add_ignored_glob (TotemPlParser *parser,
+ const char *glob)
+{
+ g_return_if_fail (TOTEM_IS_PL_PARSER (parser));
+
+ g_mutex_lock (&parser->priv->ignore_mutex);
+ g_hash_table_insert (parser->priv->ignore_globs, g_strdup (glob), GINT_TO_POINTER (1));
+ g_mutex_unlock (&parser->priv->ignore_mutex);
+}
+
+/**
* totem_pl_parser_parse_duration:
* @duration: the duration string to parse
* @debug: %TRUE if debug statements should be printed
diff --git a/plparse/totem-pl-parser.h b/plparse/totem-pl-parser.h
index 924fc4c..7e83401 100644
--- a/plparse/totem-pl-parser.h
+++ b/plparse/totem-pl-parser.h
@@ -352,6 +352,8 @@ void totem_pl_parser_add_ignored_scheme (TotemPlParser *parser,
const char *scheme);
void totem_pl_parser_add_ignored_mimetype (TotemPlParser *parser,
const char *mimetype);
+void totem_pl_parser_add_ignored_glob (TotemPlParser *parser,
+ const char *glob);
TotemPlParserResult totem_pl_parser_parse (TotemPlParser *parser,
const char *uri, gboolean fallback);