[gnome-documents/wip/abono/sort-by: 4/5] query, view: Change the ORDER BY clause



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

    query, view: Change the ORDER BY clause
    
    https://bugzilla.gnome.org/show_bug.cgi?id=683563

 src/query.js             |   34 ++++++++++++++++++++++++----------
 src/trackerController.js |   24 +++++++++++++++++++++---
 2 files changed, 45 insertions(+), 13 deletions(-)
---
diff --git a/src/query.js b/src/query.js
index f1dd34b..548bfcd 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
@@ -168,8 +182,8 @@ const QueryBuilder = new Lang.Class({
         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/trackerController.js b/src/trackerController.js
index 3a71a4c..53d7ef3 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]