new dbus api: SearchNotesSorted?



Working on the gnome-shell search provider extension I realized that the
SearchNotes dbus call returns the notes in an order that is not
particularly useful. It also does not return the match integer used by
the normal GUI for indicating the relevance of a match (preventing the
returned results from being sorted). It would be helpful to have the
results returned in a useful order.

Attached is a patch which adds a new SearchNotesSorted api which takes
the additional step of sorting the results in descending order by the
match integer.

Is this the right approach? Would it be better to make the existing
SearchNotes dbus method return sorted results?

-casey

>From b53888c4ae9b98846bd7ba53cc9c23b538a39482 Mon Sep 17 00:00:00 2001
From: Casey Harkins <caseyharkins gmail com>
Date: Tue, 18 Oct 2011 21:28:19 -0500
Subject: [PATCH] Add new SearchNotesSorted dbus method.

This new method will return the search results ordered by the
number of matches.
---
 src/dbus/gnote-introspect.xml   |    5 +++++
 src/dbus/remotecontrol-glue.cpp |    6 ++++++
 src/dbus/remotecontrol-glue.hpp |    2 ++
 src/dbus/remotecontrol.cpp      |   39 ++++++++++++++++++++++++++++++++++++++-
 src/dbus/remotecontrol.hpp      |   10 ++++++++++
 5 files changed, 61 insertions(+), 1 deletions(-)

diff --git a/src/dbus/gnote-introspect.xml b/src/dbus/gnote-introspect.xml
index 092e848..ceda78c 100644
--- a/src/dbus/gnote-introspect.xml
+++ b/src/dbus/gnote-introspect.xml
@@ -91,6 +91,11 @@
       <arg type="b" name="case_sensitive" direction="in"/>
       <arg type="as" name="ret" direction="out"/>
     </method>
+    <method name="SearchNotesSorted">
+      <arg type="s" name="query" direction="in"/>
+      <arg type="b" name="case_sensitive" direction="in"/>
+      <arg type="as" name="ret" direction="out"/>
+    </method>
     <method name="SetNoteCompleteXml">
       <arg type="s" name="uri" direction="in"/>
       <arg type="s" name="xml_contents" direction="in"/>
diff --git a/src/dbus/remotecontrol-glue.cpp b/src/dbus/remotecontrol-glue.cpp
index 867e60b..d1716c8 100644
--- a/src/dbus/remotecontrol-glue.cpp
+++ b/src/dbus/remotecontrol-glue.cpp
@@ -59,6 +59,7 @@ RemoteControl_adaptor::RemoteControl_adaptor(const Glib::RefPtr<Gio::DBus::Conne
   m_stubs["NoteExists"] = &RemoteControl_adaptor::NoteExists_stub;
   m_stubs["RemoveTagFromNote"] = &RemoteControl_adaptor::RemoveTagFromNote_stub;
   m_stubs["SearchNotes"] = &RemoteControl_adaptor::SearchNotes_stub;
+  m_stubs["SearchNotesSorted"] = &RemoteControl_adaptor::SearchNotesSorted_stub;
   m_stubs["SetNoteCompleteXml"] = &RemoteControl_adaptor::SetNoteCompleteXml_stub;
   m_stubs["SetNoteContents"] = &RemoteControl_adaptor::SetNoteContents_stub;
   m_stubs["SetNoteContentsXml"] = &RemoteControl_adaptor::SetNoteContentsXml_stub;
@@ -263,6 +264,11 @@ Glib::VariantContainerBase RemoteControl_adaptor::SearchNotes_stub(const Glib::V
   return stub_vectorstring_string_bool(parameters, &RemoteControl_adaptor::SearchNotes);
 }
 
+Glib::VariantContainerBase RemoteControl_adaptor::SearchNotesSorted_stub(const Glib::VariantContainerBase & parameters)
+{
+  return stub_vectorstring_string_bool(parameters, &RemoteControl_adaptor::SearchNotesSorted);
+}
+
 
 Glib::VariantContainerBase RemoteControl_adaptor::SetNoteCompleteXml_stub(const Glib::VariantContainerBase & parameters)
 {
diff --git a/src/dbus/remotecontrol-glue.hpp b/src/dbus/remotecontrol-glue.hpp
index 5fbac10..49ad469 100644
--- a/src/dbus/remotecontrol-glue.hpp
+++ b/src/dbus/remotecontrol-glue.hpp
@@ -58,6 +58,7 @@ public:
   virtual bool NoteExists(const std::string& uri) = 0;
   virtual bool RemoveTagFromNote(const std::string& uri, const std::string& tag_name) = 0;
   virtual std::vector<std::string> SearchNotes(const std::string& query, const bool& case_sensitive) = 0;
+  virtual std::vector<std::string> SearchNotesSorted(const std::string& query, const bool& case_sensitive) = 0;
   virtual bool SetNoteCompleteXml(const std::string& uri, const std::string& xml_contents) = 0;
   virtual bool SetNoteContents(const std::string& uri, const std::string& text_contents) = 0;
   virtual bool SetNoteContentsXml(const std::string& uri, const std::string& xml_contents) = 0;
@@ -98,6 +99,7 @@ private:
   Glib::VariantContainerBase NoteExists_stub(const Glib::VariantContainerBase &);
   Glib::VariantContainerBase RemoveTagFromNote_stub(const Glib::VariantContainerBase &);
   Glib::VariantContainerBase SearchNotes_stub(const Glib::VariantContainerBase &);
+  Glib::VariantContainerBase SearchNotesSorted_stub(const Glib::VariantContainerBase &);
   Glib::VariantContainerBase SetNoteCompleteXml_stub(const Glib::VariantContainerBase &);
   Glib::VariantContainerBase SetNoteContents_stub(const Glib::VariantContainerBase &);
   Glib::VariantContainerBase SetNoteContentsXml_stub(const Glib::VariantContainerBase &);
diff --git a/src/dbus/remotecontrol.cpp b/src/dbus/remotecontrol.cpp
index 44c6b5a..f906b6b 100644
--- a/src/dbus/remotecontrol.cpp
+++ b/src/dbus/remotecontrol.cpp
@@ -26,7 +26,6 @@
 #include "notewindow.hpp"
 #include "recentchanges.hpp"
 #include "remotecontrolproxy.hpp"
-#include "search.hpp"
 #include "tag.hpp"
 #include "tagmanager.hpp"
 #include "dbus/remotecontrol.hpp"
@@ -338,6 +337,34 @@ std::vector< std::string > RemoteControl::SearchNotes(const std::string& query,
 }
 
 
+std::vector< std::string > RemoteControl::SearchNotesSorted(const std::string& query,
+                                                            const bool& case_sensitive)
+{
+  if (query.empty())
+    return std::vector< std::string >();
+
+  std::priority_queue<std::pair<std::string,int>, std::vector<std::pair<std::string,int> >,
+    SearchResultComparison> pq_results;
+
+  Search search(m_manager);
+  std::vector< std::string > list;
+  Search::ResultsPtr results =
+    search.search_notes(query, case_sensitive, notebooks::Notebook::Ptr());
+
+  for(Search::Results::const_iterator iter = results->begin();
+      iter != results->end(); ++iter) {
+    pq_results.push(std::make_pair(iter->first->uri(), iter->second));
+  }
+
+  while(!pq_results.empty()) {
+    list.push_back(pq_results.top().first);
+    pq_results.pop();
+  }
+
+  return list;
+}
+
+
 bool RemoteControl::SetNoteCompleteXml(const std::string& uri, 
                                        const std::string& xml_contents)
 {
@@ -409,5 +436,15 @@ void RemoteControl::on_note_saved(const Note::Ptr & note)
   }
 }
 
+SearchResultComparison::SearchResultComparison()
+{
+}
+
+bool SearchResultComparison::operator()(const std::pair<std::string,int>& lhs,
+                                        const std::pair<std::string,int>& rhs)
+const
+{
+    return (lhs.second < rhs.second);
+}
 
 }
diff --git a/src/dbus/remotecontrol.hpp b/src/dbus/remotecontrol.hpp
index b0b323d..0832648 100644
--- a/src/dbus/remotecontrol.hpp
+++ b/src/dbus/remotecontrol.hpp
@@ -23,11 +23,13 @@
 
 #include <string>
 #include <vector>
+#include <queue>
 
 #include <giomm/dbusconnection.h>
 
 #include "dbus/iremotecontrol.hpp"
 #include "note.hpp"
+#include "search.hpp"
 
 
 namespace gnote {
@@ -65,6 +67,7 @@ public:
   virtual bool NoteExists(const std::string& uri);
   virtual bool RemoveTagFromNote(const std::string& uri, const std::string& tag_name);
   virtual std::vector< std::string > SearchNotes(const std::string& query, const bool& case_sensitive);
+  virtual std::vector< std::string > SearchNotesSorted(const std::string& query, const bool& case_sensitive);
   virtual bool SetNoteCompleteXml(const std::string& uri, const std::string& xml_contents);
   virtual bool SetNoteContents(const std::string& uri, const std::string& text_contents);
   virtual bool SetNoteContentsXml(const std::string& uri, const std::string& xml_contents);
@@ -78,6 +81,13 @@ private:
   NoteManager & m_manager;
 };
 
+class SearchResultComparison
+{
+public:
+    SearchResultComparison();
+    bool operator()(const std::pair<std::string,int>& lhs, const std::pair<std::string,int>& rhs) const;
+};
+
 
 }
 
-- 
1.7.6.4



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