[tracker] Add a convenience sparql function to get title-like strings out of filenames.



commit a10685f696abc3825321db0a3815fe59a03da61f
Author: Mikael Ottela <mikael ottela ixonos com>
Date:   Wed Jan 13 23:55:20 2010 +0200

    Add a convenience sparql function to get title-like strings out of filenames.

 src/libtracker-data/tracker-sparql-query.vala |    6 +++
 src/libtracker-db/tracker-db-manager.c        |   44 ++++++++++++++++++++++++-
 2 files changed, 49 insertions(+), 1 deletions(-)
---
diff --git a/src/libtracker-data/tracker-sparql-query.vala b/src/libtracker-data/tracker-sparql-query.vala
index fd010d9..262e159 100644
--- a/src/libtracker-data/tracker-sparql-query.vala
+++ b/src/libtracker-data/tracker-sparql-query.vala
@@ -1421,6 +1421,12 @@ public class Tracker.SparqlQuery : Object {
 			sql.append (")");
 
 			return PropertyType.STRING;
+		} else if (uri == TRACKER_NS + "string-from-filename") {
+			sql.append ("SparqlStringFromFilename(");
+			translate_expression_as_string (sql);
+			sql.append (")");
+
+			return PropertyType.STRING;
 		} else {
 			// support properties as functions
 			var prop = Ontology.get_property_by_uri (uri);
diff --git a/src/libtracker-db/tracker-db-manager.c b/src/libtracker-db/tracker-db-manager.c
index 62b9862..ec9e9f7 100644
--- a/src/libtracker-db/tracker-db-manager.c
+++ b/src/libtracker-db/tracker-db-manager.c
@@ -305,6 +305,45 @@ tracker_db_manager_get_flags (void)
 	return old_flags;
 }
 
+/* Create a title-type string from the filename for replacing missing ones */
+static GValue
+function_sparql_string_from_filename (TrackerDBInterface *interface,
+				      gint                argc,
+				      GValue              values[])
+{
+	GValue  result = { 0, };
+	gchar  *name = NULL;
+	gchar  *suffix = NULL;
+
+	if (argc != 1) {
+		g_critical ("Invalid argument count");
+		return result;
+	}
+
+	/* "/home/user/path/title_of_the_movie.movie" -> "title of the movie"
+	   Only for local files currently, do we need to change? */
+
+	name = g_filename_display_basename (g_value_get_string (&values[0]));
+	if (!name) {
+		return result;
+	}
+
+	suffix = g_strrstr (name, ".");
+
+	if (suffix) {
+		*suffix = '\0';
+	}
+
+	g_strdelimit (name, "._", ' ');
+
+	g_value_init (&result, G_TYPE_STRING);
+	g_value_set_string (&result, name);
+
+	g_free (name);
+
+	return result;
+}
+
 static void
 function_group_concat_step (TrackerDBInterface *interface,
                             void               *aggregate_context,
@@ -694,7 +733,10 @@ db_set_params (TrackerDBInterface *iface,
 		                                             "SparqlRegex",
 		                                             function_sparql_regex,
 		                                             3);
-
+		tracker_db_interface_sqlite_create_function (iface,
+		                                             "SparqlStringFromFilename",
+		                                             function_sparql_string_from_filename,
+		                                             1);
 		tracker_db_interface_sqlite_create_function (iface,
 		                                             "uncompress",
 		                                             function_uncompress,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]