[gnome-documents] Change the ORDER BY clause when sort-by changes



commit 059ae8e1f8c0033013405dd1bb54e40bf39168dd
Author: Alessandro Bono <abono gnome org>
Date:   Mon Oct 19 21:43:11 2015 +0200

    Change the ORDER BY clause when sort-by changes
    
    https://bugzilla.gnome.org/show_bug.cgi?id=683563

 src/query.js               |   36 +++++++++++++++++++++++++-----------
 src/shellSearchProvider.js |    3 ++-
 src/trackerController.js   |   24 +++++++++++++++++++++---
 3 files changed, 48 insertions(+), 15 deletions(-)
---
diff --git a/src/query.js b/src/query.js
index f1dd34b..3063871 100644
--- a/src/query.js
+++ b/src/query.js
@@ -19,6 +19,7 @@
  *
  */
 
+const Gd = imports.gi.Gd;
 const GdPrivate = imports.gi.GdPrivate;
 const Gio = imports.gi.Gio;
 const GLib = imports.gi.GLib;
@@ -124,11 +125,11 @@ const QueryBuilder = new Lang.Class({
         return whereSparql;
     },
 
-    _buildQueryInternal: function(global, flags, offsetController) {
+    _buildQueryInternal: function(global, flags, offsetController, sortBy) {
         let whereSparql = this._buildWhere(global, flags);
         let tailSparql = '';
 
-        // order results by mtime
+        // order results depending on sortBy
         if (global) {
             let offset = 0;
             let step = Search.OFFSET_STEP;
@@ -138,18 +139,31 @@ const QueryBuilder = new Lang.Class({
                 step = offsetController.getOffsetStep();
             }
 
-            tailSparql +=
-                'ORDER BY DESC (?mtime)' +
-                ('LIMIT %d OFFSET %d').format(step, offset);
+            switch (sortBy) {
+            case Gd.MainColumns.PRIMARY_TEXT:
+                tailSparql += 'ORDER BY ASC(?title) ASC(?filename)';
+                break;
+            case Gd.MainColumns.SECONDARY_TEXT:
+                tailSparql += 'ORDER BY ASC(?author)';
+                break;
+            case Gd.MainColumns.MTIME:
+                tailSparql += 'ORDER BY DESC(?mtime)';
+                break;
+            default:
+                tailSparql += 'ORDER BY DESC(?mtime)';
+                break;
+            }
+
+            tailSparql += ('LIMIT %d OFFSET %d').format(step, offset);
         }
 
         let sparql =
             'SELECT DISTINCT ?urn ' + // urn
             'nie:url(?urn) ' + // uri
-            'nfo:fileName(?urn)' + // filename
+            'nfo:fileName(?urn) AS ?filename ' + // filename
             'nie:mimeType(?urn)' + // mimetype
-            'nie:title(?urn) ' + // title
-            'tracker:coalesce(nco:fullname(?creator), nco:fullname(?publisher), \'\') ' + // author
+            'nie:title(?urn) AS ?title ' + // title
+            'tracker:coalesce(nco:fullname(?creator), nco:fullname(?publisher), \'\') AS ?author ' + // 
author
             'tracker:coalesce(nfo:fileLastModified(?urn), nie:contentLastModified(?urn)) AS ?mtime ' + // 
mtime
             'nao:identifier(?urn) ' + // identifier
             'rdf:type(?urn) ' + // type
@@ -162,14 +176,14 @@ const QueryBuilder = new Lang.Class({
     },
 
     buildSingleQuery: function(flags, resource) {
-        let sparql = this._buildQueryInternal(false, flags);
+        let sparql = this._buildQueryInternal(false, flags, null);
         sparql = sparql.replace('?urn', '<' + resource + '>', 'g');
 
         return this._createQuery(sparql);
     },
 
-    buildGlobalQuery: function(flags, offsetController) {
-        return this._createQuery(this._buildQueryInternal(true, flags, offsetController));
+    buildGlobalQuery: function(flags, offsetController, sortBy) {
+        return this._createQuery(this._buildQueryInternal(true, flags, offsetController, sortBy));
     },
 
     buildCountQuery: function(flags) {
diff --git a/src/shellSearchProvider.js b/src/shellSearchProvider.js
index 55784c8..d30647a 100644
--- a/src/shellSearchProvider.js
+++ b/src/shellSearchProvider.js
@@ -315,7 +315,8 @@ const FetchIdsJob = new Lang.Class({
         this._cancellable = cancellable;
         searchController.setString(this._terms.join(' '));
 
-        let query = queryBuilder.buildGlobalQuery(Query.QueryFlags.SEARCH, null);
+        let sortBy = Application.settings.get_enum('sort-by');
+        let query = queryBuilder.buildGlobalQuery(Query.QueryFlags.SEARCH, null, sortBy);
         Application.connectionQueue.add(query.sparql, this._cancellable, Lang.bind(this,
             function(object, res) {
                 let cursor = null;
diff --git a/src/trackerController.js b/src/trackerController.js
index 3a71a4c..2c4c37e 100644
--- a/src/trackerController.js
+++ b/src/trackerController.js
@@ -120,6 +120,7 @@ const TrackerController = new Lang.Class({
         this._querying = false;
         this._isStarted = false;
         this._refreshPending = false;
+        this.sortBy = null;
 
         // useful for debugging
         this._lastQueryTime = 0;
@@ -135,6 +136,10 @@ const TrackerController = new Lang.Class({
 
         this._offsetController = this.getOffsetController();
         this._offsetController.connect('offset-changed', Lang.bind(this, this._performCurrentQuery));
+
+        this._sortSettingsId = Application.application.connect('action-state-changed::sort-by',
+            Lang.bind(this, this._updateSortForSettings));
+        this._updateSortForSettings();
     },
 
     getOffsetController: function() {
@@ -272,6 +277,16 @@ const TrackerController = new Lang.Class({
             this._refreshPending = true;
     },
 
+    _updateSortForSettings: function() {
+        let sortBy = Application.settings.get_enum('sort-by');
+
+        if(this.sortBy == sortBy)
+            return;
+
+        this.sortBy = sortBy;
+        this._refreshInternal(RefreshFlags.RESET_OFFSET);
+    },
+
     start: function() {
         if (this._isStarted)
             return;
@@ -310,7 +325,8 @@ const TrackerCollectionsController = new Lang.Class({
             flags = Query.QueryFlags.COLLECTIONS;
 
         return Application.queryBuilder.buildGlobalQuery(flags,
-                                                         Application.offsetCollectionsController);
+                                                         Application.offsetCollectionsController,
+                                                         this.sortBy);
     },
 });
 
@@ -328,7 +344,8 @@ const TrackerDocumentsController = new Lang.Class({
 
     getQuery: function() {
         return Application.queryBuilder.buildGlobalQuery(Query.QueryFlags.DOCUMENTS,
-                                                         Application.offsetDocumentsController);
+                                                         Application.offsetDocumentsController,
+                                                         this.sortBy);
     },
 });
 
@@ -367,6 +384,7 @@ const TrackerSearchController = new Lang.Class({
 
     getQuery: function() {
         return Application.queryBuilder.buildGlobalQuery(Query.QueryFlags.SEARCH,
-                                                         Application.offsetSearchController);
+                                                         Application.offsetSearchController,
+                                                         this.sortBy);
     },
 });


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