[gnome-documents] documents: strip extensions from filenames and sanitize doc titles



commit beb31c1131a6858aca2299e52f66cf2924bee4c8
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Wed Aug 31 02:16:02 2011 -0400

    documents: strip extensions from filenames and sanitize doc titles

 src/documents.js   |   14 +++++++++++++-
 src/lib/gd-utils.c |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/lib/gd-utils.h |    2 ++
 src/query.js       |   38 ++++++++++++++++++++------------------
 4 files changed, 86 insertions(+), 19 deletions(-)
---
diff --git a/src/documents.js b/src/documents.js
index 3e691b5..c53c9ae 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -100,10 +100,13 @@ DocCommon.prototype = {
             }));
     },
 
+    _sanitizeTitle: function() {
+        this.title = this.title.replace('Microsoft Word - ', '', 'g');
+    },
+
     populateFromCursor: function(cursor) {
         this.uri = cursor.get_string(Query.QueryColumns.URI)[0];
         this.urn = cursor.get_string(Query.QueryColumns.URN)[0];
-        this.title = cursor.get_string(Query.QueryColumns.TITLE)[0];
         this.author = cursor.get_string(Query.QueryColumns.AUTHOR)[0];
         this.mtime = cursor.get_string(Query.QueryColumns.MTIME)[0];
         this.resourceUrn = cursor.get_string(Query.QueryColumns.RESOURCE_URN)[0];
@@ -116,6 +119,15 @@ DocCommon.prototype = {
         if (!this.uri)
             this.uri = '';
 
+        let title = cursor.get_string(Query.QueryColumns.TITLE)[0];
+        if (title && title != '')
+            this.title = title;
+        else
+            this.title = Gd.filename_strip_extension(
+                cursor.get_string(Query.QueryColumns.FILENAME)[0]);
+
+        this._sanitizeTitle();
+
         this.refreshIcon();
     },
 
diff --git a/src/lib/gd-utils.c b/src/lib/gd-utils.c
index fcc5322..6085505 100644
--- a/src/lib/gd-utils.c
+++ b/src/lib/gd-utils.c
@@ -382,3 +382,54 @@ gd_embed_image_in_frame (GdkPixbuf *source_image,
 
 	return result_pixbuf;
 }
+
+static char *
+gd_filename_get_extension_offset (const char *filename)
+{
+	char *end, *end2;
+
+	end = strrchr (filename, '.');
+
+	if (end && end != filename) {
+		if (strcmp (end, ".gz") == 0 ||
+		    strcmp (end, ".bz2") == 0 ||
+		    strcmp (end, ".sit") == 0 ||
+		    strcmp (end, ".Z") == 0) {
+			end2 = end - 1;
+			while (end2 > filename &&
+			       *end2 != '.') {
+				end2--;
+			}
+			if (end2 != filename) {
+				end = end2;
+			}
+		}
+	}
+
+	return end;
+}
+
+/**
+ * gd_filename_strip_extension:
+ * @filename_with_extension:
+ *
+ * Returns: (transfer full):
+ */
+char *
+gd_filename_strip_extension (const char * filename_with_extension)
+{
+	char *filename, *end;
+
+	if (filename_with_extension == NULL) {
+		return NULL;
+	}
+
+	filename = g_strdup (filename_with_extension);
+	end = gd_filename_get_extension_offset (filename);
+
+	if (end && end != filename) {
+		*end = '\0';
+	}
+
+	return filename;
+}
diff --git a/src/lib/gd-utils.h b/src/lib/gd-utils.h
index 16edb47..82f6806 100644
--- a/src/lib/gd-utils.h
+++ b/src/lib/gd-utils.h
@@ -63,5 +63,7 @@ GdkPixbuf * gd_embed_image_in_frame (GdkPixbuf *source_image,
                                      int right_offset,
                                      int bottom_offset);
 
+char *gd_filename_strip_extension (const char * filename_with_extension);
+
 #endif /* __GD_UTILS_H__ */
                                   
diff --git a/src/query.js b/src/query.js
index 1c0ce5a..76c6378 100644
--- a/src/query.js
+++ b/src/query.js
@@ -27,14 +27,15 @@ const GLib = imports.gi.GLib;
 const QueryColumns = {
     URN: 0,
     URI: 1,
-    TITLE: 2,
-    AUTHOR: 3,
-    MTIME: 4,
-    IDENTIFIER: 5,
-    TYPE: 6,
-    RESOURCE_URN: 7,
-    FAVORITE: 8,
-    SHARED: 9
+    FILENAME: 2,
+    TITLE: 3,
+    AUTHOR: 4,
+    MTIME: 5,
+    IDENTIFIER: 6,
+    TYPE: 7,
+    RESOURCE_URN: 8,
+    FAVORITE: 9,
+    SHARED: 10
 };
 
 function QueryBuilder() {
@@ -141,16 +142,17 @@ QueryBuilder.prototype = {
 
         let sparql =
             'SELECT DISTINCT ?urn ' + // urn
-             'nie:url(?urn) ' + // uri
-             'tracker:coalesce(nie:title(?urn), nfo:fileName(?urn)) ' + // title
-             'tracker:coalesce(nco:fullname(?creator), nco:fullname(?publisher), \'\') ' + // author
-             'tracker:coalesce(nfo:fileLastModified(?urn), nie:contentLastModified(?urn)) AS ?mtime ' + // mtime
-             'nao:identifier(?urn) ' + // identifier
-             'rdf:type(?urn) ' + // type
-             'nie:dataSource(?urn) ' + // resource URN
-             '( EXISTS { ?urn nao:hasTag nao:predefined-tag-favorite } ) ' + // favorite
-             '( EXISTS { ?urn nco:contributor ?contributor FILTER ( ?contributor != ?creator ) } ) ' + // shared
-             globalSparql;
+            'nie:url(?urn) ' + // uri
+            'nfo:fileName(?urn)' + // filename
+            'nie:title(?urn) ' + // title
+            'tracker:coalesce(nco:fullname(?creator), nco:fullname(?publisher), \'\') ' + // author
+            'tracker:coalesce(nfo:fileLastModified(?urn), nie:contentLastModified(?urn)) AS ?mtime ' + // mtime
+            'nao:identifier(?urn) ' + // identifier
+            'rdf:type(?urn) ' + // type
+            'nie:dataSource(?urn) ' + // resource URN
+            '( EXISTS { ?urn nao:hasTag nao:predefined-tag-favorite } ) ' + // favorite
+            '( EXISTS { ?urn nco:contributor ?contributor FILTER ( ?contributor != ?creator ) } ) ' + // shared
+            globalSparql;
 
         return sparql;
     },



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