[gnome-news/wip/gbsneto/improvements] search: implement search



commit abe908cd580be1fb6e2d164e222caa7816909ebe
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Sat Mar 5 14:16:23 2016 -0300

    search: implement search
    
    This doesn't quite work yet, probably because of an
    error in Sparql query. But we finally have something
    to work on.

 gnomenews/toolbar.py |   19 +++++++++++++++++--
 gnomenews/view.py    |   31 ++++++++++++++++++++++++++++++-
 gnomenews/window.py  |   29 ++++++++++++++++++++++++++++-
 3 files changed, 75 insertions(+), 4 deletions(-)
---
diff --git a/gnomenews/toolbar.py b/gnomenews/toolbar.py
index 32eb4c3..287edef 100644
--- a/gnomenews/toolbar.py
+++ b/gnomenews/toolbar.py
@@ -16,6 +16,8 @@
 
 from gi.repository import Gtk, GObject, GLib
 
+from gettext import gettext as _
+
 from gnomenews import log
 import logging
 logger = logging.getLogger(__name__)
@@ -60,6 +62,11 @@ class Toolbar(GObject.GObject):
         self._back_button.connect('clicked', self.window.on_back_button_clicked)
 
         self._search_button = self._ui.get_object('search-button')
+        self._search_button.bind_property('active',
+                                          self.window.search_bar, 'search-mode-enabled',
+                                          GObject.BindingFlags.BIDIRECTIONAL)
+
+        self.window.search_entry.connect('search-changed', self._search_changed)
 
         self.set_state(ToolbarState.MAIN)
 
@@ -70,6 +77,15 @@ class Toolbar(GObject.GObject):
         self.header_bar.set_custom_title(self._stack_switcher)
 
     @log
+    def _search_changed(self, entry, data=None):
+        if entry.get_text_length() > 0:
+            self.set_state(ToolbarState.SEARCH_VIEW)
+            self.header_bar.set_title(_("Searching for %s") % entry.get_text())
+        else:
+            self.set_state(ToolbarState.MAIN)
+            self.header_bar.set_title(_("News"))
+
+    @log
     def set_stack(self, stack):
         self._stack_switcher.set_stack(stack)
 
@@ -98,7 +114,6 @@ class Toolbar(GObject.GObject):
         else:
             self.reset_header_title()
 
-        self._search_button.set_visible(self._state != ToolbarState.SEARCH_VIEW)
         self._back_button.set_visible(self._state == ToolbarState.CHILD_VIEW)
         self.add_toggle_button.set_visible(self._state != ToolbarState.CHILD_VIEW)
 
@@ -106,7 +121,7 @@ class Toolbar(GObject.GObject):
     def _add_new_feed(self, button):
         new_url = self.new_url.get_text()
         self.window.tracker.add_channel(new_url)
-        self.new_url.set_text("")
+        self.new_url.set_text('')
         self.add_popover.hide()
 
     @log
diff --git a/gnomenews/view.py b/gnomenews/view.py
index 4efa573..1c1f092 100644
--- a/gnomenews/view.py
+++ b/gnomenews/view.py
@@ -142,7 +142,6 @@ class GenericFeedsView(Gtk.Stack):
             if new_feed['url'] not in self.feeds:
                 logger.info("Adding channel %s" % new_feed['url'])
                 self._add_new_feed(new_feed)
-                self.feeds[new_feed['url']] = new_feed
 
     @log
     def setup_layout(self):
@@ -311,5 +310,35 @@ class StarredView(GenericFeedsView):
 
 
 class SearchView(GenericFeedsView):
+
+    __gproperties__ = {
+        'search-query': (str, 'Query', 'Search query', "", GObject.ParamFlags.READWRITE)
+    }
+
     def __init__(self, tracker):
         GenericFeedsView.__init__(self, tracker, 'search')
+
+        self.search_query = ""
+
+    @log
+    def do_get_property(self, prop):
+        if prop.name == 'search-query':
+            return self.search_query
+        else:
+            raise AttributeError('Unknown property %s' % prop.name)
+
+    @log
+    def do_set_property(self, prop, value):
+        if prop.name == 'search-query':
+            self.search_query = value
+            self.update_search()
+        else:
+            raise AttributeError('Unknown property %s' % prop.name)
+
+    @log
+    def update_search(self):
+        [self.flowbox.remove(old_feed) for old_feed in self.flowbox.get_children()]
+
+        posts = self.tracker.get_text_matches(self.search_query, 10)
+        [self._add_a_new_preview(post) for post in posts]
+        self.show_all()
diff --git a/gnomenews/window.py b/gnomenews/window.py
index bbc4f4d..b0b3591 100644
--- a/gnomenews/window.py
+++ b/gnomenews/window.py
@@ -14,7 +14,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from gi.repository import Gtk, Gio, GLib
+from gi.repository import Gtk, Gio, GLib, GObject
 from gettext import gettext as _
 
 from gnomenews.toolbar import Toolbar, ToolbarState
@@ -64,6 +64,7 @@ class Window(Gtk.ApplicationWindow):
             self.maximize()
 
         # Save changes to window size
+        self.connect("key-press-event", self._on_key_press_event)
         self.connect("window-state-event", self._on_window_state_event)
         self.configure_event_handler = self.connect("configure-event", self._on_configure_event)
 
@@ -74,6 +75,9 @@ class Window(Gtk.ApplicationWindow):
         with self.handler_block(self.configure_event_handler):
             GLib.idle_add(self._store_window_size_and_position, widget, priority=GLib.PRIORITY_LOW)
 
+    def _on_key_press_event(self, widget, event):
+        return self.search_bar.handle_event(event)
+
     def _store_window_size_and_position(self, widget):
         size = widget.get_size()
         self.settings.set_value('window-size', GLib.Variant('ai', [size[0], size[1]]))
@@ -98,6 +102,12 @@ class Window(Gtk.ApplicationWindow):
         # Action bar
         self.action_bar = self._ui.get_object('action_bar')
 
+        # Search bar
+        self.search_bar = self._ui.get_object('search_bar')
+        self.search_entry = self._ui.get_object('search_entry')
+
+        self.search_entry.connect('search-changed', self.on_search_changed)
+
         # Header bar
         self.toolbar = Toolbar(self)
         self.set_titlebar(self.toolbar.header_bar)
@@ -130,6 +140,12 @@ class Window(Gtk.ApplicationWindow):
         self.toolbar.set_stack(self._stack)
         self._stack.set_visible_child(self.views[0])
 
+        # Search view
+        self.search_view = view.SearchView(self.tracker)
+
+        self.search_entry.bind_property('text', self.search_view, 'search-query',
+                                        GObject.BindingFlags.BIDIRECTIONAL)
+
         self.tracker.connect('items-updated', self.views[0].update_new_items)
         self.tracker.connect('feeds-updated', self.views[1].update_feeds)
 
@@ -149,3 +165,14 @@ class Window(Gtk.ApplicationWindow):
         self.toolbar.set_state(ToolbarState.MAIN)
         self.feed_view.disconnect(self.tracker.post_read_signal)
         self.feed_view = None
+
+    @log
+    def on_search_changed(self, entry, data=None):
+        if entry.get_text_length() > 0:
+            # Add the view if it's not added yet
+            if self.search_view not in self._stack.get_children():
+                self._stack.add_named(self.search_view, 'search_view')
+
+            self._stack.set_visible_child(self.search_view)
+        else:
+            self._stack.remove(self.search_view)


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